From a4f548f3ee1ff2ed9e198527644c76737b3a1464 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sat, 2 Sep 2023 16:24:55 +0100 Subject: Interpreter COMPLETE! Really easy implementation considering opcodes are super simple. I was right to implement the loop_ref system so at parse time we have resolved references and don't need to do it at runtime (helps with compilation I guess as well). --- main.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/main.c b/main.c index dad7e92..0743578 100644 --- a/main.c +++ b/main.c @@ -21,6 +21,43 @@ typedef struct uint8_t memory[MEMORY_DEFAULT]; } machine_t; +void interpret(machine_t *cpu, node_t *ast, size_t num) +{ + for (size_t i = 0; i < num; ++i) + { + node_t node = ast[i]; + switch (node.type) + { + case NEXT: + ++cpu->dp; + break; + case PREV: + --cpu->dp; + break; + case INC: + ++cpu->memory[cpu->dp]; + break; + case DEC: + --cpu->memory[cpu->dp]; + break; + case OUT: + putchar(cpu->memory[cpu->dp]); + break; + case READ: + cpu->memory[cpu->dp] = getchar(); + break; + case LIN: + if (cpu->memory[cpu->dp] == 0) + i = node.loop_ref; + break; + case LOUT: + if (cpu->memory[cpu->dp] != 0) + i = node.loop_ref; + break; + } + } +} + int main(int argc, char *argv[]) { if (argc == 1) @@ -36,6 +73,8 @@ int main(int argc, char *argv[]) buffer_t *buffer; struct PResult res; + machine_t machine = {0}; + for (int i = 1; i < argc; ++i) { filepath = argv[i]; @@ -61,6 +100,8 @@ int main(int argc, char *argv[]) printf("%s=>%s\n", filepath, str); + interpret(&machine, res.nodes, res.size); + free(str); free(res.nodes); free(buffer); -- cgit v1.2.3-13-gbd6f