From 633e6598859fc5104bb28d5cccac845cb5d5198e Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Wed, 18 Jan 2017 20:12:49 -0600 Subject: nqasm: support binary arithmetic operators in immediate operands --- parser.y | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'parser.y') diff --git a/parser.y b/parser.y index cb1343f..ed33aa4 100644 --- a/parser.y +++ b/parser.y @@ -16,7 +16,7 @@ void yyerror(const char *msg) #define MKARGVALUE(v) (struct argument) { .type = ARG_INTEGER, .value = (v) } #define MKARGLABEL(l) (struct argument) { .type = ARG_LABEL, .label = (l) } #define MKARGTYPE(t) (struct argument) { .type = (t) } -#define MKARGEXPR(op,c) (struct argument) { .type = (op), .children = c } +#define MKARGEXPR(op,...) (struct argument) { .type = (op), .children = { __VA_ARGS__ } } %} @@ -87,8 +87,16 @@ void yyerror(const char *msg) %token T_OPERATOR_ADD "+" %token T_OPERATOR_SUB "-" +%token T_OPERATOR_MUL "*" +%token T_OPERATOR_DIV "/" %token T_OPERATOR_NOT "!" %token T_OPERATOR_INV "~" +%token T_LPAREN "(" +%token T_RPAREN ")" + +%left "-" "+" +%left "*" "/" +%precedence NEG %type inst %type reg @@ -184,13 +192,18 @@ reg: T_LABEL { } expr: - intlit { $$ = MKARGVALUE($1); } - | T_LABEL { $$ = MKARGLABEL($1); } - | "." { $$ = MKARGTYPE(ARG_PC); } - | "+" expr { $$ = MKARGEXPR(ARG_UNARY_ADD, { argdup(&$2) }); } - | "-" expr { $$ = MKARGEXPR(ARG_UNARY_SUB, { argdup(&$2) }); } - | "!" expr { $$ = MKARGEXPR(ARG_UNARY_NOT, { argdup(&$2) }); } - | "~" expr { $$ = MKARGEXPR(ARG_UNARY_INV, { argdup(&$2) }); } + intlit { $$ = MKARGVALUE($1); } + | T_LABEL { $$ = MKARGLABEL($1); } + | "." { $$ = MKARGTYPE(ARG_PC); } + | "+" expr %prec NEG { $$ = MKARGEXPR(ARG_UNARY_ADD, argdup(&$2)); } + | "-" expr %prec NEG { $$ = MKARGEXPR(ARG_UNARY_SUB, argdup(&$2)); } + | "!" expr %prec NEG { $$ = MKARGEXPR(ARG_UNARY_NOT, argdup(&$2)); } + | "~" expr %prec NEG { $$ = MKARGEXPR(ARG_UNARY_INV, argdup(&$2)); } + | expr "+" expr { $$ = MKARGEXPR(ARG_ADD, argdup(&$1), argdup(&$3)); } + | expr "-" expr { $$ = MKARGEXPR(ARG_SUB, argdup(&$1), argdup(&$3)); } + | expr "*" expr { $$ = MKARGEXPR(ARG_MUL, argdup(&$1), argdup(&$3)); } + | expr "/" expr { $$ = MKARGEXPR(ARG_DIV, argdup(&$1), argdup(&$3)); } + | "(" expr ")" { $$ = $2; } ; intlit: T_INT -- cgit v1.2.3