diff options
Diffstat (limited to 'src/interpreter.c')
-rw-r--r-- | src/interpreter.c | 128 |
1 files changed, 37 insertions, 91 deletions
diff --git a/src/interpreter.c b/src/interpreter.c index ed5c8fd..273d985 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -1,71 +1,22 @@ -#include "interpreter.h" -#include "program.h" +#include "types.h" #include <stddef.h> -#include <stdint.h> -#include <stdio.h> #include <stdlib.h> #include <string.h> -static void loop_end(Program* program) { - uint32_t count = 1; - Symbol s; - - while (true) { - program_next(program, &s); - if (s == StartLoop) { - count++; - } else if (s == EndLoop) { - count--; - } else if (s == Eof) { - return; - } - - if (count == 0) return; - } -} - -static void loop_start(Program* program) { - uint32_t count = 1; - Symbol s; - - program_last(program, &s); - while (true) { - program_last(program, &s); - if (s == StartLoop) { - count--; - } else if (s == EndLoop) { - count++; - } else if (s == Eof) { - return; - } +void interpret(Program* program) { + Stack tape_stack; + stack_init(&tape_stack, sizeof(void*)); - if (count == 0) { - return; - }; - } -} + Tape* tape = malloc(sizeof(Tape)); + tape_init(tape, sizeof(void*)); -static void recurse_program(Program* program, Tape* tape) { + stack_push(&tape_stack, tape); + Symbol s; - next: - program_next(program, &s); - - // printf("["); - // for(uint32_t i = 0; i < tape->len; i++) { - // printf("0x%02x ", tape->data[i]); - // } - // printf("\b]\n "); - // for(uint32_t i = 0; i < tape->len; i++) { - // if (i == tape->index) { - // printf(" ^ "); - // } else { - // printf(" "); - // } - // } - // printf(" \ + tape->indexn"); - + program_get(program, &s); + switch (s) { case MoveLeft: tape_left(tape); @@ -74,77 +25,72 @@ next: tape_right(tape); break; case Increment: - tape_set(tape, tape_get(tape) + 1); + tape_increment(tape); break; case Decrement: - tape_set(tape, tape_get(tape) - 1); + tape_decrement(tape); break; case StartLoop: - if (tape_get(tape) == 0) - loop_end(program); + if (tape_get(tape) == 0) { + program_seek(program, program_get_jump(program)); + } break; case EndLoop: - if (tape_get(tape) != 0) - loop_start(program); + if (tape_get(tape) != 0) { + program_seek(program, program_get_jump(program)); + } break; case PutChar: { printf("%c", tape_get(tape)); break; } case GetChar: { - uint8_t c = (uint8_t) getchar(); - tape_set(tape, c); + tape_set(tape, getchar()); break; } case Allocate: { - uint8_t len = tape_get(tape); Tape* new = malloc(sizeof(Tape)); - tape_init(len, new); - void* ptr = (void*) tape_ptr(tape); - memcpy(ptr, &new, sizeof(Tape*)); + tape_init(new, tape_get(tape)); + memcpy(tape_ptr(tape), &new, sizeof(Tape*)); break; } case Free: { Tape* old; - void* ptr = (void*) tape_ptr(tape); - memcpy(&old, ptr, sizeof(Tape*)); - memset(ptr, 0, sizeof(Tape*)); + memcpy(&old, tape_ptr(tape), sizeof(Tape*)); tape_free(old); free(old); break; } case EnterTape: { - Tape* cur; - void* ptr = (void*) tape_ptr(tape); - memcpy(&cur, ptr, sizeof(Tape*)); - recurse_program(program, cur); + memcpy(&tape, tape_ptr(tape), sizeof(Tape*)); + stack_push(&tape_stack, tape); + break; + } + case LeaveTape: { + tape = stack_pop(&tape_stack); + if (tape == NULL) goto end; break; } case GetString: { - uint8_t len = tape_get(tape); - void* ptr = (void*) tape_ptr(tape); - fgets(ptr, len, stdin); + fgets((char*) tape_ptr(tape), tape_get(tape), stdin); break; } case PutString: { - char* ptr = (char*) tape_ptr(tape); - printf("%s", ptr); + printf("%s", (char*) tape_ptr(tape)); break; } case Clear: printf("\033c"); break; - case LeaveTape: case Eof: - return; + goto end; } + program_next(program); goto next; -} +end: + tape_free(tape); + free(tape); -void run_program(Program* program) { - Tape tape; - tape_init(sizeof(void*), &tape); - recurse_program(program, &tape); - tape_free(&tape); + stack_free(&tape_stack); } |