Updated grammars.

This commit is contained in:
jacob committed 2026-11-01 01:06:36 -06:00
1 parent 285e558c87
commit 353ec7a364
2 files changed
+25 -2

No files matched your search

+8 -2
View File
@@ -131,7 +131,7 @@
%precedence ELSE THEN
%precedence STOP // High
%type<ast> expr arrow left_arrow equality addition product unary postfix call atom
%type<ast> expr function_def arrow left_arrow equality addition product unary postfix call atom
%type<argarr> sequence args args_opt tuple parameters names names_opt comparisons
%destructor { free($$); } <strval>
%destructor { argarr_destroy($$); } <argarr>
@@ -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:
+17
View File
@@ -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)