Skip to content

Commit 9f6386c

Browse files
Merge pull request #1507 from andrew-johnson-4/gradually-port-macros-to-lsts
Gradually port macros to lsts
2 parents e13e410 + bb541f9 commit 9f6386c

File tree

7 files changed

+30107
-30081
lines changed

7 files changed

+30107
-30081
lines changed

BOOTSTRAP/cli.c

Lines changed: 30048 additions & 30046 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CC = cc
22
CFLAGS = -O3 -march=native -mtune=native -ferror-limit=50
33

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

PLATFORM/C/LIB/common-macros.lsts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@ typed macro macro::let(lhs: lazy, rhs: lazy): lazy = (
33
(fn(lhs) = ()) (rhs)
44
);
55

6+
typed macro macro::set(lhs: lazy, rhs: lazy): lazy = (
7+
mov(rhs, lhs)
8+
);
9+
10+
#typed macro macro::set(base field: macro::lhs-field, rhs: lazy): lazy = (
11+
# macro::concat(l"set.", field) (base, rhs)
12+
#);
13+
14+
#typed macro macro::set(base index: macro::lhs-index, rhs: lazy): lazy = (
15+
# $"set[]"( macro::lhs-as-rhs(base), index, rhs )
16+
# macro::concat(l"set.", field) (base, rhs)
17+
#);
18+
19+
#typed macro macro::lhs-as-rhs(v: macro::variable): lazy = (
20+
# v
21+
#);
22+
23+
#typed macro macro::lhs-as-rhs(_: macro::literal<"&">, v: lazy): lazy = (
24+
# &v
25+
#);
26+
27+
#typed macro macro::lhs-as-rhs(base field: macro::lhs-index): lazy = (
28+
# macro::concat(l".", field)(base)
29+
#);
30+
31+
#typed macro macro::lhs-index(base: lazy, field: lazy): lazy = (
32+
# base; field
33+
#);
34+
635
typed macro macro::for(item: lazy, iter: List<?>, loop: lazy): lazy = (
736
let uuid(iter-term) = iter;
837
while uuid(iter-term).has-head { match uuid(iter-term) {
@@ -11,3 +40,18 @@ typed macro macro::for(item: lazy, iter: List<?>, loop: lazy): lazy = (
1140
);
1241
}}
1342
);
43+
44+
typed macro macro::for(item: lazy, iter: Vector<?>, loop: lazy): lazy = (
45+
let uuid(iter-term) = iter;
46+
let uuid(iter-i) = 0_u64;
47+
let uuid(iter-length) = uuid(iter-term).length;
48+
while uuid(iter-i) < uuid(iter-length) { match uuid(iter-term)[uuid(iter-i)] {
49+
item => (
50+
loop; uuid(iter-i) = uuid(iter-i) + 1;
51+
);
52+
}}
53+
);
54+
55+
typed macro macro::while(cond: lazy, body: lazy): lazy = (
56+
$"primitive::while"( body as Nil, into-branch-conditional(cond) )
57+
);

PLUGINS/FRONTEND/LSTS/lsts-parse.lsts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,12 +1065,6 @@ let lsts-parse-small-expression(tokens: List<Token>): Tuple<AST,List<Token>> = (
10651065
mk-var(with-location(mk-token("macro::let"),loc)),
10661066
mk-cons(lhs-rest.first, rhs-rest.first)
10671067
);
1068-
#base = mk-app(
1069-
# mk-app(
1070-
# mk-var(with-location(mk-token("let"),loc)),
1071-
# lhs-rest.first
1072-
# ), rhs-rest.first
1073-
#);
10741068
);
10751069
[Token{key:c"while"}.. rest] => (
10761070
let loc = head(tokens).location; tokens = rest;
@@ -1081,13 +1075,13 @@ let lsts-parse-small-expression(tokens: List<Token>): Tuple<AST,List<Token>> = (
10811075
tokens = rhs-rest.second;
10821076
lsts-parse-expect(c"}", tokens); tokens = tail(tokens);
10831077
base = mk-app(
1084-
mk-app(
1085-
Var{ c"while", with-location(mk-token("while"),loc) },
1086-
c-rest.first
1087-
),
1088-
mk-app(
1089-
Var{ c"scope", with-location(mk-token("scope"),loc) },
1090-
rhs-rest.first
1078+
Var{ c"macro::while", with-location(mk-token("macro::while"),loc) },
1079+
mk-cons(
1080+
c-rest.first,
1081+
mk-app(
1082+
Var{ c"scope", with-location(mk-token("scope"),loc) },
1083+
rhs-rest.first
1084+
)
10911085
)
10921086
);
10931087
);
@@ -1220,11 +1214,8 @@ let lsts-parse-assign(tokens: List<Token>): Tuple<AST,List<Token>> = (
12201214
let rhs-tmp-name = uuid();
12211215
let rhs-tmp = Var{ rhs-tmp-name, with-location(mk-token(rhs-tmp-name),loc) };
12221216
base = mk-app(
1223-
mk-app(
1224-
Var{ c"let", with-location(mk-token(c"let"),loc) },
1225-
rhs-tmp
1226-
),
1227-
rhs-rest.first
1217+
Var{ c"macro::let", with-location(mk-token(c"macro::let"),loc) },
1218+
mk-cons( rhs-tmp, rhs-rest.first )
12281219
);
12291220

12301221
let i = 0_u64;
@@ -1251,11 +1242,8 @@ let lsts-parse-assign(tokens: List<Token>): Tuple<AST,List<Token>> = (
12511242

12521243
ASTOrIdentId { id=id } => (
12531244
let x = mk-app(
1254-
mk-app(
1255-
Var{ c"let", with-location(mk-token("let"),loc) },
1256-
id
1257-
),
1258-
rexpr
1245+
Var{ c"macro::let", with-location(mk-token("macro::let"),loc) },
1246+
mk-cons( id, rexpr )
12591247
);
12601248
base = mk-cons(base, x);
12611249
);

SRC/std-infer-expr.lsts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ let std-infer-expr(tctx: Maybe<TContext>, term: AST, is-scoped: Bool, used: IsUs
9696
);
9797
Glb{key=key, val=val} => (
9898
let rough-tt = typeof(term);
99-
if rough-tt.is-arrow && not(rough-tt.is-open) {
99+
if rough-tt.is-arrow && not(rough-tt.is-open) && not(rough-tt.is-t(c"TypedMacro")) {
100100
(_, let new-val) = std-infer-expr(tctx, val, is-scoped, Used, TAny);
101101
if not(is(val,new-val)) then {
102102
let new-term = mk-glb(key,new-val);

tests/unit/ast-macros.lsts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,5 @@ typed macro ast-macros-test-add(x: I64, y: lazy): lazy = (
1111
assert( ast-macros-test-add(1_u64, 2_u64) == 3_u64 );
1212
assert( ast-macros-test-add(2_i64, 3_i64) == 5_i64 );
1313

14-
typed macro macro::for(item: lazy, iter: List<?>, loop: lazy): lazy = (
15-
let uuid(iter-term) = iter;
16-
while uuid(iter-term).has-head { match uuid(iter-term) {
17-
[item.. uuid(tl)] => (
18-
loop; uuid(iter-term) = uuid(tl);
19-
);
20-
}}
21-
);
22-
23-
$"macro::for"(x, [1, 2], print(x));
14+
#$"macro::for"(x, [1, 2], print(x));
15+
#$"macro::for"(x, mk-vector(type(U64)).push(3).push(4), print(x));

tests/unit/ast-macros.lsts.skip

Whitespace-only changes.

0 commit comments

Comments
 (0)