From 7fce68649af1e2248f8658221bd2d29573548417 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 9 Jun 2019 16:56:20 +0900 Subject: [PATCH] Implement comma expressions Doesn't quite work (attempt to suppress warning for return a = 3, 5; failed). --- tools/qfcc/source/qc-parse.y | 20 ++++++++++++--- tools/qfcc/test/Makefile.am | 10 ++++++++ tools/qfcc/test/comma-expr.r | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tools/qfcc/test/comma-expr.r diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index 308512b6b..c8c269d27 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -180,7 +180,7 @@ int yylex (void); %type methoddef %type opt_initializer var_initializer local_def -%type opt_init opt_expr fexpr expr element_list element +%type opt_init opt_expr cexpr fexpr expr element_list element %type optional_state_expr texpr vector_expr %type statement statements compound_statement %type else label break_label continue_label @@ -1175,7 +1175,7 @@ statement break_label = $2; continue_label = $3; } - | fexpr ';' + | cexpr ';' { $$ = $1; } @@ -1227,7 +1227,7 @@ switch_block ; opt_init - : fexpr + : cexpr | type init_var_decl_list { $$ = $2; } | /* empty */ { @@ -1260,7 +1260,7 @@ init_var_decl ; opt_expr - : fexpr + : cexpr | /* empty */ { $$ = 0; @@ -1346,6 +1346,18 @@ texpr : fexpr { $$ = convert_bool ($1, 1); } ; +cexpr + : arg_list + { + if ($1->next) { + expr_t *res = $1; + $1 = build_block_expr ($1->next); + $1->e.block.result = res; + } + $$ = $1; + } + ; + opt_arg_list : /* emtpy */ { $$ = 0; } | arg_list { $$ = $1; } diff --git a/tools/qfcc/test/Makefile.am b/tools/qfcc/test/Makefile.am index 87742d8e0..c0d7bade0 100644 --- a/tools/qfcc/test/Makefile.am +++ b/tools/qfcc/test/Makefile.am @@ -31,6 +31,7 @@ fail_bins= test_progs_dat=\ chewed-alias.dat \ chewed-return.dat \ + comma-expr.dat \ deadbool.dat \ fordecl.dat \ func-expr.dat \ @@ -89,6 +90,15 @@ chewed-return.run: Makefile build-run include ./$(DEPDIR)/chewed-return.Qo # am--include-marker r_depfiles_remade += ./$(DEPDIR)/chewed-return.Qo +comma_expr_dat_SOURCES=comma-expr.r +comma_expr_obj=$(comma_expr_dat_SOURCES:.r=.qfo) +comma-expr.dat$(EXEEXT): $(comma_expr_obj) $(QFCC_DEP) + $(QFCC) $(QCFLAGS) -o $@ $(comma_expr_obj) +comma-expr.run: Makefile build-run + $(srcdir)/build-run $@ +include ./$(DEPDIR)/comma-expr.Qo # am--include-marker +r_depfiles_remade += ./$(DEPDIR)/comma-expr.Qo + deadbool_dat_SOURCES=deadbool.r deadbool_obj=$(deadbool_dat_SOURCES:.r=.qfo) deadbool.dat$(EXEEXT): $(deadbool_obj) $(QFCC_DEP) diff --git a/tools/qfcc/test/comma-expr.r b/tools/qfcc/test/comma-expr.r new file mode 100644 index 000000000..ea145c0a2 --- /dev/null +++ b/tools/qfcc/test/comma-expr.r @@ -0,0 +1,50 @@ +void printf (string fmt, ...) = #0; + +int a = 0; +int b = 0; + +int +return_comma() +{ + return a = 3, 5; +} + +int +test_for_comma () +{ + int fail = 1; + int count = 5; + int i = -1; + float j = -1; + + for (i = 3, j = 5; count-- > 0; ) { + i += 2; + j += 3; + } + if (i == 13 && j == 20) { + fail = 0; + } + + return fail; +} + +int +test_comma () +{ + int fail = 1; + if (return_comma() == 5) { + if (a == 3) { + fail = 0; + } + } + return fail; +} + +int +main () +{ + int fail = 0; + fail |= test_comma (); + fail |= test_for_comma (); + return fail; +}