generate machine.py file
This commit is contained in:
parent
fa70beb1a4
commit
af82eb5b00
@ -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
|
||||
|
23
src/ram.py
23
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)
|
||||
|
10
src/ram.y
10
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user