Skip to content

Commit 040c236

Browse files
Merge pull request #1503 from andrew-johnson-4/port-more-and-start-using-macros-2.0
Port more and start using macros 2.0
2 parents a595dc3 + 7a487c2 commit 040c236

File tree

10 files changed

+30388
-30227
lines changed

10 files changed

+30388
-30227
lines changed

BOOTSTRAP/cli.c

+30,328-30,214
Large diffs are not rendered by default.

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CC = cc
22
CFLAGS = -O3 -march=native -mtune=native
33

44
dev: install-production
5-
lm tests/unit/ast-macros.lsts
5+
lm t.lsts
66
cc tmp.c
77
./a.out
88

PLATFORM/C/LIB/common-macros.lsts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#typed macro macro::let(lhs: lazy, rhs: lazy): lazy = (
3+
# (fn(lhs) = ()) (rhs)
4+
#);
5+
6+
typed macro macro::for(item: lazy, iter: List<?>, loop: lazy): lazy = (
7+
let uuid(iter-term) = iter;
8+
while uuid(iter-term).has-head { match uuid(iter-term) {
9+
[item.. uuid(tl)] => (
10+
loop; uuid(iter-term) = uuid(tl);
11+
);
12+
}}
13+
);

PLATFORM/C/LIB/default.lsts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import PLATFORM/C/LIB/c-shim.lm;
33
import PLATFORM/C/LIB/common-macros.lm;
4+
import PLATFORM/C/LIB/common-macros.lsts;
45
import PLATFORM/C/LIB/platform-macros.lm;
56
import PLATFORM/C/LIB/blob.lm;
67
import PLATFORM/C/LIB/primitives.lm;

PLATFORM/C/LIB/minimal.lsts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import PLATFORM/C/LIB/c-shim.lm;
33
import PLATFORM/C/LIB/common-macros.lm;
4+
import PLATFORM/C/LIB/common-macros.lsts;
45
import PLATFORM/C/LIB/platform-macros.lm;
56
import PLATFORM/C/LIB/blob.lm;
67
import PLATFORM/C/LIB/primitives.lm;

PLUGINS/FRONTEND/LSTS/lsts-parse.lsts

+34-11
Original file line numberDiff line numberDiff line change
@@ -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);

SRC/mk-lit.lsts

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ let mk-lit(val: CString): AST = (
66
let mk-lit(val: SmartString): AST = (
77
Lit{ untern(val), mk-token(val) }
88
);
9+
10+
let mk-lit(val: Token): AST = (
11+
Lit{ val.key, val }
12+
);

SRC/mk-var.lsts

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ let mk-var(val: CString): AST = (
66
let mk-var(val: SmartString): AST = (
77
Var{ untern(val), mk-token(val) }
88
);
9+
10+
let mk-var(val: Token): AST = (
11+
Var{ val.key, val }
12+
);

SRC/std-apply-macro-candidates.lsts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let std-apply-macro-candidates(tctx: TContext?, mname: CString, margs: AST, cand
1111
if not(non-zero(t)) {
1212
(let new-tctx, let mctx) = std-try-destructure-macro(tctx, margs, ctype, mlhs);
1313
if mctx.is-some then mctx = union(mctx, Some{extract-uuids(mrhs)});
14-
if mctx.is-some then t = substitute(mctx.get-or-panic, mrhs);
14+
if mctx.is-some then t = with-location(substitute(mctx.get-or-panic, mrhs.unique), margs.location);
1515
}
1616
};
1717
if not(non-zero(t)) then fail("Macro Application failed match during destructuring: \{mname}\n");

SRC/std-infer-expr.lsts

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ let std-infer-expr(tctx: Maybe<TContext>, term: AST, is-scoped: Bool, used: IsUs
175175
mark-var-to-def-todo(tctx, var-name-if-var(l), typeof(r), l);
176176
};
177177
);
178+
_ => fail("Unexpected Term in std-infer-expr\n\{term}\n");
178179
};
179180
(tctx, term);
180181
);

0 commit comments

Comments
 (0)