Skip to content

Commit 5c9e68c

Browse files
Merge pull request #1511 from andrew-johnson-4/port-more-macros-fasee
Port more macros fasee
2 parents d1c8f48 + 7daed41 commit 5c9e68c

File tree

9 files changed

+30785
-30543
lines changed

9 files changed

+30785
-30543
lines changed

BOOTSTRAP/cli.c

Lines changed: 30735 additions & 30534 deletions
Large diffs are not rendered by default.

PLATFORM/C/LIB/common-macros.lsts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,9 @@ typed macro macro::for(item: lazy, iter: Vector<?>, loop: lazy): lazy = (
6464
);
6565

6666
typed macro macro::while(cond: lazy, body: lazy): lazy = (
67-
$"primitive::while"( body as Nil, into-branch-conditional(cond) )
67+
primitive::while( body as Nil, into-branch-conditional(cond) )
68+
);
69+
70+
typed macro macro::assert(cond: lazy): lazy = (
71+
if not(cond) then fail("Assertion Failed", macro::location(cond))
6872
);

PLATFORM/C/LIB/io.lm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ fail := λ(: msg SmartString). (: (
1919
(as (exit 1_u32) Never)
2020
) Never);
2121

22+
fail := λ(: msg SmartString)(: loc SmartString). (: (
23+
(print msg)(print '\sat\s_s)(print loc)
24+
(as (exit 1_u32) Never)
25+
) Never);
26+
2227
fail := λ(: msg String). (: (
2328
(print msg)
2429
(as (exit 1_u32) Never)

PLUGINS/FRONTEND/LSTS/lsts-parse.lsts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,14 @@ let lsts-make-maybe-var(tokens: List<Token>): Tuple<AST,List<Token>> = (
525525
lsts-parse-lit(tokens)
526526
} else {
527527
if not(non-zero(tokens)) { lsts-parse-expect(c"Identifier", tokens); };
528+
let loc = head(tokens).location;
528529
let base = ASTEOF;
529530
if lsts-is-lit(head(tokens).key) {
530531
base = lsts-make-lit(head(tokens));
531532
tokens = tail(tokens);
532533
} else {
533534
(let var-name, tokens) = lsts-parse-identifier(tokens);
534-
base = mk-var(var-name);
535+
base = with-location(mk-var(var-name), loc);
535536
};
536537
Tuple { base, tokens }
537538
}
@@ -1286,9 +1287,7 @@ let lsts-make-lit(t: Token): AST = (
12861287
while non-zero(s) && s != c"_ss" {
12871288
if s.has-prefix(c"\\{") {
12881289
if non-zero(buffer) {
1289-
buffer = buffer + SAtom{c"_ss"};
1290-
let bs = clone-rope(buffer);
1291-
let be = Lit { bs, with-location(mk-token(bs),loc) };
1290+
let be = mk-lit(with-location(mk-token(clone-rope(buffer)),loc)).ascript(t1(c"SmartString"));
12921291
if non-zero(base) {
12931292
base = mk-app(
12941293
Var{ c"+", with-location(mk-token("+"),loc) },
@@ -1330,9 +1329,7 @@ let lsts-make-lit(t: Token): AST = (
13301329
}
13311330
};
13321331
if non-zero(buffer) {
1333-
buffer = buffer + SAtom{ c"_ss" };
1334-
let bs = clone-rope(buffer);
1335-
let be = Lit{ bs, with-location(mk-token(bs),loc) };
1332+
let be = mk-lit(with-location(mk-token(clone-rope(buffer)),loc)).ascript(t1(c"SmartString"));
13361333
if non-zero(base) {
13371334
base = mk-app(
13381335
Var{ c"+", with-location(mk-token("+"),loc) },
@@ -1342,6 +1339,8 @@ let lsts-make-lit(t: Token): AST = (
13421339
base = be;
13431340
};
13441341
};
1342+
} else if t.key.has-suffix(c"_ss") {
1343+
base = with-location(mk-lit(t.key.remove-suffix(c"_ss")),loc).ascript(t1(c"SmartString"));
13451344
};
13461345
base
13471346
);
@@ -1493,6 +1492,8 @@ let lsts-parse-atom-without-tail(tokens: List<Token>): Tuple<AST,List<Token>> =
14931492
tokens = term-rest.second;
14941493
lsts-parse-expect(c")", tokens); tokens = tail(tokens);
14951494
term = AType{ term-rest.first };
1495+
} else if lsts-parse-head(tokens).has-suffix(c"_ss") {
1496+
(term, tokens) = lsts-parse-lit(tokens);
14961497
} else if lsts-parse-head(tokens)==c"sizeof" {
14971498
let loc = head(tokens).location;
14981499
tokens = tail(tokens);

SRC/macro-table.lsts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
let index-macro-table = {} :: HashtableEq<CString,List<(Type,Type,AST)>>;
33
index-macro-table = index-macro-table.bind(c"macro::concat", [] :: List<(Type,Type,AST)>);
4+
index-macro-table = index-macro-table.bind(c"macro::location", [] :: List<(Type,Type,AST)>);
45

56
let bind-new-macro(mname: CString, mterm: AST): Nil = (
67
let row = index-macro-table.lookup(mname, [] :: List<(Type,Type,AST)>);

SRC/preprocess-apply.lm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@ preprocess-apply := λ(: program AST). (: (
2727
(close(preprocess-apply( nt )))
2828
)))
2929
))
30+
( (App( is-cons-1 (App( is-cons-2 (Lit( ':_s ctk )) (App( is-cons-3 (@( t (Lit( l ltk )) )) (AType tt) )) )) nt )) (
31+
(if (not(.is-t( tt 'SmartString_s ))) (set t (preprocess-apply t)) ())
32+
(set r (App( is-cons-1
33+
(close(App( is-cons-2 (close(Lit( ':_s (unique ctk) ))) (close(App( is-cons-3 (close t) (close(AType tt)) ))) )))
34+
(close(preprocess-apply( nt )))
35+
)))
36+
) )
3037
( (App( is-cons-1 (App( is-cons-2 (Lit( ':_s ctk )) (App( is-cons-3 t (AType tt) )) )) nt )) (
3138
(set r (App( is-cons-1
3239
(close(App( is-cons-2 (close(Lit( ':_s (unique ctk) ))) (close(App( is-cons-3 (close(preprocess-apply t)) (close(AType tt)) ))) )))
3340
(close(preprocess-apply( nt )))
3441
)))
3542
))
43+
( (App( is-cons-2 (Lit( ':_s ctk )) (App( is-cons-3 (@( t (Lit _) )) (AType tt) )) )) (
44+
(if (not(.is-t( tt 'SmartString_s ))) (set t (preprocess-apply t)) ())
45+
(set r (App( is-cons-2
46+
(close(Lit( ':_s (unique ctk) )))
47+
(close(App( is-cons-3 (close t) (close(AType tt)) )))
48+
)))
49+
) )
3650
( (App( is-cons-1 (App( is-cons-2 (Lit( ':_s ctk )) mvar )) (Lit( mtype mtk )) )) (
3751
(if is-cons-1
3852
(set r (App( is-cons-1 (close(App( is-cons-2 (close(Lit( ':_s ctk ))) (close(preprocess-apply mvar)) ))) (close(Lit( mtype mtk ))) )))

SRC/std-apply-macro.lsts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ let std-apply-macro-concat(tctx: Maybe<TContext>, mname: CString, margs: AST): (
1717
}
1818
);
1919

20+
let std-apply-macro-location(tctx: Maybe<TContext>, mname: CString, margs: AST): (TContext?, AST) = (
21+
(tctx, mk-lit(to-smart-string(margs.location)).ascript(t1(c"SmartString")))
22+
);
23+
2024
let std-apply-macro(tctx: Maybe<TContext>, mname: CString, margs: AST, strong: Bool): (TContext?, AST) = (
2125
let result = ASTEOF;
2226
if mname==c"macro::concat" then (tctx, result) = std-apply-macro-concat(tctx, mname, margs)
27+
else if mname==c"macro::location" then (tctx, result) = std-apply-macro-location(tctx, mname, margs)
2328
else {
2429
let row = index-macro-table.lookup(mname, [] :: List<(Type,Type,AST)>);
2530
let peep-holes = TAny;

tests/unit/ast-macros.lsts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,16 @@ type A = A{a:U64};
2525
print(a.a);
2626
);
2727

28+
(
29+
let wi = 0_u64;
30+
macro::while( wi < 4, ( print(wi); wi = wi + 1; ) )
31+
);
32+
33+
macro::assert(true);
34+
35+
# match
36+
37+
# for
2838
#$"macro::for"(x, [1, 2], print(x));
2939
#$"macro::for"(x, mk-vector(type(U64)).push(3).push(4), print(x));
40+

tests/unit/ast-macros.lsts.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[3,4]2
1+
[3,4]20123

0 commit comments

Comments
 (0)