From 9e9b9ff853995a4478747f6c12e00c01cbb34705 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Wed, 15 May 2019 19:54:29 -0400 Subject: Implement sizeof in order --- bin/order.y | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'bin/order.y') diff --git a/bin/order.y b/bin/order.y index 84fcc2cc..4327c00a 100644 --- a/bin/order.y +++ b/bin/order.y @@ -21,6 +21,7 @@ #include #include #include +#include #include static void yyerror(const char *str) { @@ -43,7 +44,7 @@ static int yylex(void); %} -%token Var Arr Inc Dec Shl Shr Le Ge Eq Ne And Or +%token Var Arr Inc Dec Sizeof Shl Shr Le Ge Eq Ne And Or %left ',' %right '?' ':' @@ -57,7 +58,7 @@ static int yylex(void); %left Shl Shr %left '+' '-' %left '*' '/' '%' -%right '!' '~' Inc Dec +%right '!' '~' Inc Dec Sizeof %left '[' ']' Arr '.' %% @@ -81,6 +82,7 @@ expr: | '-' expr { $$ = fmt("(-%s)", $2); } | '*' expr { $$ = fmt("(*%s)", $2); } | '&' expr { $$ = fmt("(&%s)", $2); } + | Sizeof expr { $$ = fmt("(sizeof %s)", $2); } | expr '*' expr { $$ = fmt("(%s * %s)", $1, $3); } | expr '/' expr { $$ = fmt("(%s / %s)", $1, $3); } | expr '%' expr { $$ = fmt("(%s %% %s)", $1, $3); } @@ -116,6 +118,10 @@ static int yylex(void) { int len; for (len = 0; isalnum(input[len]) || input[len] == '_'; ++len); if (len) { + if (!strncmp(input, "sizeof", len)) { + input += len; + return Sizeof; + } yylval = fmt("%.*s", len, input); input += len; return Var; -- cgit 1.4.1