From af82eb5b00cf8658303ae81b6344e3b830febbfc Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Thu, 28 Mar 2024 12:23:16 +0100 Subject: [PATCH] generate machine.py file --- src/Makefile | 3 +++ src/ram.py | 23 +++++++++++++++-------- src/ram.y | 10 +++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Makefile b/src/Makefile index c49fe56..98bc8ce 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,10 @@ LDFLAGS="-L/usr/local/opt/flex/lib" machine.py: ram ./ram < test.1 + echo "### machine.py ###" cat machine.py + echo "### execution ###" + python machine.py ram: ram.yy.c ram.tab.c gcc -o $@ $^ -ly -lfl diff --git a/src/ram.py b/src/ram.py index 973a987..6367c9a 100644 --- a/src/ram.py +++ b/src/ram.py @@ -100,15 +100,22 @@ class Ram(object): ### example -input_registers = [10, 5, 1] +# input_registers = [10, 5, 1] +# +# instructions = [ +# {'op': Ram.op, 'args': ('ADD', ('i', 0), ('value', 1), ('o', 2))}, +# {'op': Ram.op, 'args': ('ADD', ('value', 0), ('@', 2, 'i', 'i'), ('o', 1))}, +# ] -instructions = [ - {'op': Ram.op, 'args': ('ADD', ('i', 0), ('value', 1), ('o', 2))}, - {'op': Ram.op, 'args': ('ADD', ('value', 0), ('@', 2, 'i', 'i'), ('o', 1))}, -] +# input_registers = [10, 5, 1, 2, 4] +# instructions = [ {"op": Ram.op, "args": ('ADD', ('i', 0), ('value', 1), ('r', 0))}, +# {"op": Ram.op, "args": ('SUB', ('i', 1), ('value', 3), ('r', 1))}, +# {"op": Ram.op, "args": ('MULT', ('r', 1), ('r', 0), ('o', 1))}, +# ] -ram = Ram(instructions, input_registers) -ram.execute() +# ram = Ram(instructions, input_registers) -print("Result:", ram.output_registers) +# ram.execute() + +# print("Result:", ram.output_registers) diff --git a/src/ram.y b/src/ram.y index d96c4a5..d9b711e 100644 --- a/src/ram.y +++ b/src/ram.y @@ -22,10 +22,10 @@ int yyerror(char* s); %% program: input SEPARATION instruction { - asprintf(&result, "input = [%s]\ninstructions = [ %s]\n", $1, $3); + asprintf(&result, "input_registers = [%s]\ninstructions = [ %s]\n", $1, $3); } | instruction { - asprintf(&result, "input = []\ninstructions = [ %s]\n", $1); + asprintf(&result, "input_registers = []\ninstructions = [ %s]\n", $1); } ; @@ -63,12 +63,12 @@ instruction: line: OP_CTRL PAR_O args_ctrl PAR_C { char* tmp = ""; - asprintf(&tmp, "{\"op\": Ram.op_ctrl, \"args\": ('%s', (%s))},\n", $1, $3); + asprintf(&tmp, "{\"op\": Ram.op_ctrl, \"args\": ('%s', %s)},\n", $1, $3); $$ = tmp; } | OP PAR_O args PAR_C { char* tmp = ""; - asprintf(&tmp, "{\"op\": Ram.op, \"args\": ('%s', (%s))},\n", $1, $3); + asprintf(&tmp, "{\"op\": Ram.op, \"args\": ('%s', %s)},\n", $1, $3); $$ = tmp; } ; @@ -128,7 +128,7 @@ int main() { fprintf(stderr, "Error creating file machine.py\n"); return 1; } else { - fprintf(file, "%s", result); + fprintf(file, "from ram import *\n\n%s\nram = Ram(instructions, input_registers)\nram.execute()\nprint(\"Result:\", ram.output_registers)\n", result); fclose(file); } return 0;