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:
41
main.c
41
main.c
@@ -21,6 +21,43 @@ typedef struct
|
|||||||
uint8_t memory[MEMORY_DEFAULT];
|
uint8_t memory[MEMORY_DEFAULT];
|
||||||
} machine_t;
|
} 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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
@@ -36,6 +73,8 @@ int main(int argc, char *argv[])
|
|||||||
buffer_t *buffer;
|
buffer_t *buffer;
|
||||||
struct PResult res;
|
struct PResult res;
|
||||||
|
|
||||||
|
machine_t machine = {0};
|
||||||
|
|
||||||
for (int i = 1; i < argc; ++i)
|
for (int i = 1; i < argc; ++i)
|
||||||
{
|
{
|
||||||
filepath = argv[i];
|
filepath = argv[i];
|
||||||
@@ -61,6 +100,8 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
printf("%s=>%s\n", filepath, str);
|
printf("%s=>%s\n", filepath, str);
|
||||||
|
|
||||||
|
interpret(&machine, res.nodes, res.size);
|
||||||
|
|
||||||
free(str);
|
free(str);
|
||||||
free(res.nodes);
|
free(res.nodes);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|||||||
Reference in New Issue
Block a user