From 353ec7a364b7081cfe71271aebedb4b6bb14eb48 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Sun, 1 Nov 2026 01:06:36 -0600 Subject: [PATCH] Updated grammars. --- src/grammar.y | 10 ++++++++-- test/integration/test_grammar.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 127e0d6..03f71aa 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -131,7 +131,7 @@ %precedence ELSE THEN %precedence STOP // High -%type expr arrow left_arrow equality addition product unary postfix call atom +%type expr function_def arrow left_arrow equality addition product unary postfix call atom %type sequence args args_opt tuple parameters names names_opt comparisons %destructor { free($$); } %destructor { argarr_destroy($$); } @@ -156,7 +156,11 @@ expr: | WORD COLON arrow EQ expr { $$ = ast_init(AST_TYPE_DEF, ast_def_data_init($1, $3, $5)); } - | call EQ expr { + | function_def { $$ = $1; } + ; + +function_def: + call EQ expr { $$ = function_definition($1, $3); if (!$$) YYERROR; } @@ -170,6 +174,8 @@ arrow: left_arrow { $$ = $1; } | arrow RARROW left_arrow { $$ = apply($3, $1); } | tuple RARROW left_arrow { $$ = make_call($3, $1); } + | arrow RARROW function_def { $$ = apply($3, $1); } + | tuple RARROW function_def { $$ = make_call($3, $1); } ; left_arrow: diff --git a/test/integration/test_grammar.py b/test/integration/test_grammar.py index 6b1b3fb..624518d 100644 --- a/test/integration/test_grammar.py +++ b/test/integration/test_grammar.py @@ -51,6 +51,22 @@ class GrammarTests(SCLTestCase): 120, ) + def test_named_function_definitions_as_arrow_targets(self): + for source, expected in ( + ("2 -> f(x) = x + 1", 3), + ("(2,3) -> f(x,y) = x + y", 5), + ("g(x) = x * 2; 2 -> f(x) = x + 1 -> g; f(4)", 10), + ("g(x) = x * 2; 2 -> f(x) = x + 1 $ -> g; f(4)", 5), + ("g(x) = x * 2; (2,3) -> f(x,y) = x + y $ -> g", 10), + ( + "factorial(n) = (n,1) -> f(n,a) = " + "if n == 1 then a else f(n - 1,a * n); factorial(5)", + 120, + ), + ): + with self.subTest(source=source): + self.assert_number(source, expected) + def test_typed_binding_and_nested_function_definition(self): self.assert_number("x: Num = 3; x", 3) self.assert_number("apply(f,x) = f(x); apply(g(y) = y + 1,2)", 3) @@ -88,6 +104,7 @@ class GrammarTests(SCLTestCase): "f(x) x + 1", "f(x) =", "f(1) = 2", "f(x = 1) = x", "f()(x) = x", r"\1 -> 2", "if c then a else", "f(1,)", "1#", "1;", "(1,2)", "1\n2", "then = 1", + "2 -> f(1) = 2", "(2,3) -> f(x,1) = x", ): with self.subTest(source=source): self.assert_syntax_error(source)