Add: brainfuck interpreter; documentation; uptime

This commit is contained in:
xamidev
2024-08-06 15:29:13 +02:00
parent fc17e5eade
commit 98b79d7fcf
10 changed files with 156 additions and 10 deletions

6
src/drivers/kb.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef KB_H
#define KB_H
char keyboard_getchar();
#endif

View File

@@ -19,3 +19,8 @@ void delay(int ticks)
eticks = global_ticks + ticks;
while (global_ticks < eticks);
}
int uptime()
{
return global_ticks;
}

View File

@@ -21,7 +21,7 @@ void shell_install()
}
else if (strcmp(input_buffer, "help") == 0)
{
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\n");
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\tuptime\n");
}
else if (strcmp(input_buffer, "panic") == 0)
{
@@ -47,6 +47,14 @@ void shell_install()
{
program_math();
}
else if (strcmp(input_buffer, "bf") == 0)
{
program_bf();
}
else if (strcmp(input_buffer, "uptime") == 0)
{
program_uptime();
}
else {
printf("Unknown command %s\n", input_buffer);
}

View File

@@ -22,6 +22,7 @@ void delay(int ticks);
void keyboard_install();
char keyboard_getchar();
void shell_install();
int uptime();
extern volatile unsigned long global_ticks;

55
src/programs/bf.c Normal file
View File

@@ -0,0 +1,55 @@
#include "../kernel/system.h"
#include "../libc/stdio.h"
#define BUF_SIZE 256
void brainfuck(char* input)
{
unsigned char tape[30000] = {0};
unsigned char* ptr = tape;
char current_char;
size_t i;
size_t loop;
for (i=0; input[i] != 0; i++)
{
current_char = input[i];
if (current_char == '>') {
++ptr;
} else if (current_char == '<') {
--ptr;
} else if (current_char == '+') {
++*ptr;
} else if (current_char == '-') {
--*ptr;
} else if (current_char == '.') {
putc(*ptr);
} else if (current_char == ',') {
*ptr = keyboard_getchar();
} else if (current_char == '[') {
continue;
} else if (current_char == ']' && *ptr) {
loop = 1;
while (loop > 0)
{
current_char = input[--i];
if (current_char == '[') {
loop--;
} else if (current_char == ']') {
loop++;
}
}
}
}
}
void program_bf()
{
char input_buffer[BUF_SIZE];
puts("Brainfuck code? ");
get_input(input_buffer, BUF_SIZE);
brainfuck(input_buffer);
//brainfuck(",[.[-],]");
puts("\n");
}

View File

@@ -1,6 +1,7 @@
// Miscellaneous small programs
#include "../libc/stdio.h"
#include "../kernel/system.h"
// Print a rainbow colorful text for testing
@@ -29,3 +30,10 @@ void program_clear()
{
for (int i=0; i<ROWS; i++) scroll(1);
}
// Get uptime in ticks
void program_uptime()
{
printf("%d ticks\n", uptime());
}

View File

@@ -4,8 +4,11 @@
void program_words();
void program_primes();
void program_math();
void program_bf();
// Misc
void program_rainbow();
void program_clear();
void program_uptime();
#endif