diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-09-02 16:24:55 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-09-02 16:24:55 +0100 |
commit | a4f548f3ee1ff2ed9e198527644c76737b3a1464 (patch) | |
tree | da25c5f5498795d5e7ea06e90868007ef7c60328 /main.c | |
parent | e5cba43a0e5a19360d87594ae22cdb94872e536c (diff) | |
download | obf-a4f548f3ee1ff2ed9e198527644c76737b3a1464.tar.gz obf-a4f548f3ee1ff2ed9e198527644c76737b3a1464.tar.bz2 obf-a4f548f3ee1ff2ed9e198527644c76737b3a1464.zip |
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).
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -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); |