@@ -159,6 +159,7 @@ let lsts-has-assign(tokens: List<Token>): U64 = (
159159 [Token{key:c"if"} .. rest] => (if depth == 0_i64 {tokens = [] :: List<Token>;} else {tokens = rest;});
160160 [Token{key:c"then"} .. rest] => (if depth == 0_i64 {tokens = [] :: List<Token>;} else {tokens = rest;});
161161 [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;});
162163 [_ .. rest] => tokens = rest;
163164 }; if depth < 0_i64 { tokens = [] :: List<Token>; }; };
164165 has-assign
@@ -839,20 +840,24 @@ let lsts-parse-function-signature(tokens: List<Token>, loc: SourceLocation): Tup
839840 while non-zero(tokens) && lsts-parse-head(tokens)!=c")" {
840841 lsts-parse-expect(c"Identifier", lsts-is-ident-head(lsts-parse-head(tokens)), tokens);
841842 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+ };
845850 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(
847852 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) ;
850855 if is( out.args-list, ASTNil{} ) {
851856 out.args-list = arg-binding;
852- out.args-type = type-rest.first ;
857+ out.args-type = arg-type ;
853858 } else {
854859 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 );
856861 };
857862 };
858863 lsts-parse-expect(c")", tokens); tokens = tail(tokens);
@@ -1056,12 +1061,15 @@ let lsts-parse-small-expression(tokens: List<Token>): Tuple<AST,List<Token>> = (
10561061 lsts-parse-expect(c"=", tokens); tokens = tail(tokens);
10571062 let rhs-rest = lsts-parse-small-expression(tokens);
10581063 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+ #);
10591068 base = mk-app(
10601069 mk-app(
1061- Var{ c"let", with-location(mk-token("let"),loc) } ,
1070+ mk-var( with-location(mk-token("let"),loc)) ,
10621071 lhs-rest.first
1063- ),
1064- rhs-rest.first
1072+ ), rhs-rest.first
10651073 );
10661074 );
10671075 [Token{key:c"while"}.. rest] => (
@@ -1504,6 +1512,21 @@ let lsts-parse-atom-without-tail(tokens: List<Token>): Tuple<AST,List<Token>> =
15041512 Var{ c"sizeof", with-location(mk-token(c"sizeof"),loc) },
15051513 mk-atype(term-rest.first)
15061514 );
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);
15071530 } else if lsts-parse-head(tokens)==c"&" {
15081531 let amp = head(tokens);
15091532 tokens = tail(tokens);
0 commit comments