From ee34fc8d14804afb5236eda2af2972ff505cbfc5 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Thu, 28 Mar 2024 12:55:16 +0100 Subject: [PATCH] syntax fix + assembly calculate a^b --- src/Makefile | 9 ++++++++- src/a_pow_b.1 | 11 +++++++++++ src/ram.l | 2 +- src/ram.y | 4 ++-- 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/a_pow_b.1 diff --git a/src/Makefile b/src/Makefile index 98bc8ce..3c31373 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,13 @@ LDFLAGS="-L/usr/local/opt/flex/lib" -machine.py: ram +a_pow_b: ram + ./ram < a_pow_b.1 + echo "### machine.py ###" + cat machine.py + echo "### execution ###" + python machine.py + +test: ram ./ram < test.1 echo "### machine.py ###" cat machine.py diff --git a/src/a_pow_b.1 b/src/a_pow_b.1 new file mode 100644 index 0000000..1857b13 --- /dev/null +++ b/src/a_pow_b.1 @@ -0,0 +1,11 @@ +(2,4,5) +ADD(i1,0,r0) +ADD(i1,0,r1) +ADD(i1,0,r2) +ADD(1,0,r3) +JE(i2,r3,4) +MULT(r2,r0,r2) +ADD(1,r3,r3) +JUMP(-3) +ADD(0,1,o0) +ADD(r2,0,o1) diff --git a/src/ram.l b/src/ram.l index 345fa84..bc071f6 100644 --- a/src/ram.l +++ b/src/ram.l @@ -11,7 +11,7 @@ %% [rio][0-9]* { yylval = strdup(yytext); return REGISTER;} [rio]@[rio][0-9]* { yylval = strdup(yytext); return REGISTER_REF;} -[0-9]* { yylval = strdup(yytext); return VALUE;} +[\-0-9]* { yylval = strdup(yytext); return VALUE;} ADD|SUB|MULT|DIV { yylval = strdup(yytext); return OP;} JUMP|JE|JL { yylval = strdup(yytext); return OP_CTRL;} [ ]*,[ ]* {return COMMA;} diff --git a/src/ram.y b/src/ram.y index d9b711e..961a8d2 100644 --- a/src/ram.y +++ b/src/ram.y @@ -84,9 +84,9 @@ args: ; args_ctrl: - VALUE COMMA arg COMMA arg { + arg COMMA arg COMMA VALUE { char* tmp = ""; - asprintf(&tmp, "%c, %s, %s", $1, $3, $5); + asprintf(&tmp, "%s, %s, %s", $5, $1, $3); $$ = tmp; } | VALUE {