@@ -159,6 +159,7 @@ let lsts-has-assign(tokens: List<Token>): U64 = (
159
159
[Token{key:c"if"} .. rest] => (if depth == 0_i64 {tokens = [] :: List<Token>;} else {tokens = rest;});
160
160
[Token{key:c"then"} .. rest] => (if depth == 0_i64 {tokens = [] :: List<Token>;} else {tokens = rest;});
161
161
[Token{key:c"else"} .. rest] => (if depth == 0_i64 {tokens = [] :: List<Token>;} else {tokens = rest;});
162
+ [Token{key:c"fn"} .. rest] => (if depth == 0_i64 {tokens = [] :: List<Token>;} else {tokens = rest;});
162
163
[_ .. rest] => tokens = rest;
163
164
}; if depth < 0_i64 { tokens = [] :: List<Token>; }; };
164
165
has-assign
@@ -839,20 +840,24 @@ let lsts-parse-function-signature(tokens: List<Token>, loc: SourceLocation): Tup
839
840
while non-zero(tokens) && lsts-parse-head(tokens)!=c")" {
840
841
lsts-parse-expect(c"Identifier", lsts-is-ident-head(lsts-parse-head(tokens)), tokens);
841
842
let arg-name = head(tokens); tokens = tail(tokens);
842
- lsts-parse-expect(c":", tokens); tokens = tail(tokens);
843
- let type-rest = lsts-parse-type(tokens);
844
- tokens = type-rest.second;
843
+ let had-type = false;
844
+ let arg-type = TAny;
845
+ if lsts-parse-head(tokens)==c":" {
846
+ had-type = true;
847
+ lsts-parse-expect(c":", tokens); tokens = tail(tokens);
848
+ (arg-type, tokens) = lsts-parse-type(tokens);
849
+ };
845
850
if lsts-parse-head(tokens)==c"," { tokens = tail(tokens); } else { lsts-parse-expect(c")", tokens); };
846
- let arg-binding = mk-cons(
851
+ let arg-binding = if had-type then mk-cons(
847
852
Lit{ c":", with-location(mk-token(":"),loc) },
848
- mk-cons(Var{ arg-name.key, arg-name }, AType{ type-rest.first } )
849
- );
853
+ mk-cons(mk-var( arg-name), mk-atype( arg-type) )
854
+ ) else mk-var(arg-name) ;
850
855
if is( out.args-list, ASTNil{} ) {
851
856
out.args-list = arg-binding;
852
- out.args-type = type-rest.first ;
857
+ out.args-type = arg-type ;
853
858
} else {
854
859
out.args-list = mk-cons(out.args-list, arg-binding);
855
- out.args-type = t3(c"Cons", out.args-type, type-rest.first );
860
+ out.args-type = t3(c"Cons", out.args-type, arg-type );
856
861
};
857
862
};
858
863
lsts-parse-expect(c")", tokens); tokens = tail(tokens);
@@ -1056,12 +1061,15 @@ let lsts-parse-small-expression(tokens: List<Token>): Tuple<AST,List<Token>> = (
1056
1061
lsts-parse-expect(c"=", tokens); tokens = tail(tokens);
1057
1062
let rhs-rest = lsts-parse-small-expression(tokens);
1058
1063
tokens = rhs-rest.second;
1064
+ #base = mk-app(
1065
+ # mk-var(with-location(mk-token("macro::let"),loc)),
1066
+ # mk-cons(lhs-rest.first, rhs-rest.first)
1067
+ #);
1059
1068
base = mk-app(
1060
1069
mk-app(
1061
- Var{ c"let", with-location(mk-token("let"),loc) } ,
1070
+ mk-var( with-location(mk-token("let"),loc)) ,
1062
1071
lhs-rest.first
1063
- ),
1064
- rhs-rest.first
1072
+ ), rhs-rest.first
1065
1073
);
1066
1074
);
1067
1075
[Token{key:c"while"}.. rest] => (
@@ -1504,6 +1512,21 @@ let lsts-parse-atom-without-tail(tokens: List<Token>): Tuple<AST,List<Token>> =
1504
1512
Var{ c"sizeof", with-location(mk-token(c"sizeof"),loc) },
1505
1513
mk-atype(term-rest.first)
1506
1514
);
1515
+ } else if lsts-parse-head(tokens)==c"fn" {
1516
+ lsts-parse-expect(c"fn", tokens); let loc = head(tokens).location; tokens = tail(tokens);
1517
+ let misc-type = TAny;
1518
+ if lsts-parse-head(tokens)==c":" {
1519
+ lsts-parse-expect(c":", tokens); tokens = tail(tokens);
1520
+ (misc-type, tokens) = lsts-parse-type(tokens);
1521
+ };
1522
+ let rec-id = c"";
1523
+ if lsts-parse-head(tokens)!=c"(" then (rec-id, tokens) = lsts-parse-identifier(tokens);
1524
+ if non-zero(rec-id) then fail("TODO: recursive let \{rec-id}\n");
1525
+ (let func-sig, tokens) = lsts-parse-function-signature(tokens, loc);
1526
+ lsts-parse-expect(c"=", tokens); tokens = tail(tokens);
1527
+ (let rhs, tokens) = lsts-parse-small-expression(tokens);
1528
+ if non-zero(func-sig.return-type) then rhs = mk-app(mk-lit(c":"), mk-cons(rhs, mk-atype(func-sig.return-type)));
1529
+ term = mk-abs(func-sig.args-list, rhs, misc-type);
1507
1530
} else if lsts-parse-head(tokens)==c"&" {
1508
1531
let amp = head(tokens);
1509
1532
tokens = tail(tokens);
0 commit comments