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
|
machine.py: ram
|
||||||
./ram < test.1
|
./ram < test.1
|
||||||
|
echo "### machine.py ###"
|
||||||
cat machine.py
|
cat machine.py
|
||||||
|
echo "### execution ###"
|
||||||
|
python machine.py
|
||||||
|
|
||||||
ram: ram.yy.c ram.tab.c
|
ram: ram.yy.c ram.tab.c
|
||||||
gcc -o $@ $^ -ly -lfl
|
gcc -o $@ $^ -ly -lfl
|
||||||
|
23
src/ram.py
23
src/ram.py
@ -100,15 +100,22 @@ class Ram(object):
|
|||||||
|
|
||||||
|
|
||||||
### example
|
### 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 = [
|
# input_registers = [10, 5, 1, 2, 4]
|
||||||
{'op': Ram.op, 'args': ('ADD', ('i', 0), ('value', 1), ('o', 2))},
|
# instructions = [ {"op": Ram.op, "args": ('ADD', ('i', 0), ('value', 1), ('r', 0))},
|
||||||
{'op': Ram.op, 'args': ('ADD', ('value', 0), ('@', 2, 'i', 'i'), ('o', 1))},
|
# {"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:
|
program:
|
||||||
input SEPARATION instruction {
|
input SEPARATION instruction {
|
||||||
asprintf(&result, "input = [%s]\ninstructions = [ %s]\n", $1, $3);
|
asprintf(&result, "input_registers = [%s]\ninstructions = [ %s]\n", $1, $3);
|
||||||
}
|
}
|
||||||
| instruction {
|
| instruction {
|
||||||
asprintf(&result, "input = []\ninstructions = [ %s]\n", $1);
|
asprintf(&result, "input_registers = []\ninstructions = [ %s]\n", $1);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -63,12 +63,12 @@ instruction:
|
|||||||
line:
|
line:
|
||||||
OP_CTRL PAR_O args_ctrl PAR_C {
|
OP_CTRL PAR_O args_ctrl PAR_C {
|
||||||
char* tmp = "";
|
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;
|
$$ = tmp;
|
||||||
}
|
}
|
||||||
| OP PAR_O args PAR_C {
|
| OP PAR_O args PAR_C {
|
||||||
char* tmp = "";
|
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;
|
$$ = tmp;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
@ -128,7 +128,7 @@ int main() {
|
|||||||
fprintf(stderr, "Error creating file machine.py\n");
|
fprintf(stderr, "Error creating file machine.py\n");
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} 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);
|
fclose(file);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user