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).
This commit is contained in:
2023-09-02 16:24:55 +01:00
parent e5cba43a0e
commit a4f548f3ee

41
main.c
View File

@@ -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);