From 579c7bad92d15d7bc35391cd82a6a3c2294010e4 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 10 Sep 2020 16:04:44 -0700 Subject: [PATCH] Enable more syntax. --- code/compiler/13/parser.y | 37 ++++++++++++++++++++++++++++++------- code/compiler/13/scanner.l | 1 + 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/code/compiler/13/parser.y b/code/compiler/13/parser.y index 44c4215..528f75f 100644 --- a/code/compiler/13/parser.y +++ b/code/compiler/13/parser.y @@ -18,6 +18,7 @@ using yyscan_t = void*; } %token BACKSLASH +%token BACKTICK %token PLUS %token TIMES %token MINUS @@ -52,9 +53,10 @@ using yyscan_t = void*; %type > branches %type > constructors %type > typeList +%type anyBinop %type definitions %type type nonArrowType typeListElement -%type aEq aAdd aMul case let lambda app appBase +%type aInfix aEq aAdd aMul case let lambda app appBase %type data %type defn %type branch @@ -76,7 +78,7 @@ definitions ; defn - : DEFN LID lowercaseParams EQUAL OCURLY aEq CCURLY + : DEFN LID lowercaseParams EQUAL OCURLY aInfix CCURLY { $$ = definition_defn_ptr( new definition_defn(std::move($2), std::move($3), std::move($6), @$)); } ; @@ -86,6 +88,16 @@ lowercaseParams | lowercaseParams LID { $$ = std::move($1); $$.push_back(std::move($2)); } ; +aInfix + : aInfix BACKTICK LID BACKTICK aEq + { $$ = ast_ptr(new ast_app( + ast_ptr(new ast_app(ast_ptr(new ast_lid(std::move($3))), std::move($1))), std::move($5))); } + | aInfix BACKTICK UID BACKTICK aEq + { $$ = ast_ptr(new ast_app( + ast_ptr(new ast_app(ast_ptr(new ast_uid(std::move($3))), std::move($1))), std::move($5))); } + | aEq { $$ = std::move($1); } + ; + aEq : aAdd EQUALS aAdd { $$ = ast_ptr(new ast_binop(EQUALS, std::move($1), std::move($3), @$)); } | aAdd LESS_EQUALS aAdd { $$ = ast_ptr(new ast_binop(LESS_EQUALS, std::move($1), std::move($3), @$)); } @@ -114,24 +126,35 @@ appBase : INT { $$ = ast_ptr(new ast_int($1, @$)); } | LID { $$ = ast_ptr(new ast_lid(std::move($1), @$)); } | UID { $$ = ast_ptr(new ast_uid(std::move($1), @$)); } - | OPAREN aEq CPAREN { $$ = std::move($2); } + | OPAREN aInfix CPAREN { $$ = std::move($2); } + | OPAREN anyBinop CPAREN { $$ = ast_ptr(new ast_lid(op_name($2))); } | case { $$ = std::move($1); } | let { $$ = std::move($1); } | lambda { $$ = std::move($1); } ; +anyBinop + : PLUS { $$ = PLUS; } + | MINUS { $$ = MINUS; } + | TIMES { $$ = TIMES; } + | DIVIDE { $$ = DIVIDE; } + | MODULO { $$ = MODULO; } + | EQUALS { $$ = EQUALS; } + | LESS_EQUALS { $$ = LESS_EQUALS; } + ; + let - : LET OCURLY definitions CCURLY IN OCURLY aAdd CCURLY + : LET OCURLY definitions CCURLY IN OCURLY aInfix CCURLY { $$ = ast_ptr(new ast_let(std::move($3), std::move($7), @$)); } ; lambda - : BACKSLASH lowercaseParams ARROW OCURLY aAdd CCURLY + : BACKSLASH lowercaseParams ARROW OCURLY aInfix CCURLY { $$ = ast_ptr(new ast_lambda(std::move($2), std::move($5), @$)); } ; case - : CASE aAdd OF OCURLY branches CCURLY + : CASE aInfix OF OCURLY branches CCURLY { $$ = ast_ptr(new ast_case(std::move($2), std::move($5), @$)); } ; @@ -141,7 +164,7 @@ branches ; branch - : pattern ARROW OCURLY aAdd CCURLY + : pattern ARROW OCURLY aInfix CCURLY { $$ = branch_ptr(new branch(std::move($1), std::move($4))); } ; diff --git a/code/compiler/13/scanner.l b/code/compiler/13/scanner.l index 162643d..ddd46da 100644 --- a/code/compiler/13/scanner.l +++ b/code/compiler/13/scanner.l @@ -29,6 +29,7 @@ % { return yy::parser::make_MODULO(drv.location); } == { return yy::parser::make_EQUALS(drv.location); } \<= { return yy::parser::make_LESS_EQUALS(drv.location); } +` { return yy::parser::make_BACKTICK(drv.location); } [0-9]+ { return yy::parser::make_INT(atoi(yytext), drv.location); } defn { return yy::parser::make_DEFN(drv.location); } data { return yy::parser::make_DATA(drv.location); }