Skip to content

Commit 9ded835

Browse files
Merge pull request #1513 from andrew-johnson-4/start-porting-match-macro
Start porting match macro
2 parents 5c9e68c + bbc9e27 commit 9ded835

17 files changed

+30601
-30451
lines changed

BOOTSTRAP/cli.c

Lines changed: 30524 additions & 30427 deletions
Large diffs are not rendered by default.

PLATFORM/C/LIB/common-macros.lsts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,26 @@ typed macro macro::while(cond: lazy, body: lazy): lazy = (
6868
);
6969

7070
typed macro macro::assert(cond: lazy): lazy = (
71-
if not(cond) then fail("Assertion Failed", macro::location(cond))
71+
if not(cond) then fail("Assertion Failed", macro::location(here))
72+
);
73+
74+
typed macro macro::match(t: lazy, ps: lazy): lazy = (
75+
let uuid(term) = open(t);
76+
macro::match-pats( uuid(term), ps );
77+
);
78+
79+
typed macro macro::match-case(lhs: lazy, rhs: lazy, remainder: lazy): lazy = (
80+
lhs; rhs; remainder
81+
);
82+
83+
typed macro macro::match-pats(term: lazy, remainder: lazy): lazy = (
84+
remainder
85+
);
86+
87+
typed macro macro::match-pats(term: lazy, lhs rhs remainder: macro::match-case): lazy = (
88+
scope(if macro::match-arm(lhs,term) then rhs else macro::match-pats(term,remainder))
89+
);
90+
91+
typed macro macro::match-arm(v: macro::variable, term: lazy): lazy = (
92+
(let v = term; true)
7293
);

PLUGINS/BACKEND/C/cc-blob.lm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cc-blob := λ(: caller-ctx FContext)(: function-name String)(: args AST)(: offse
44
# caller context is provided but not propagated
55
(let r (mk-fragment()))
66

7-
(let f (find-global-callable( function-name (typeof args) )))
7+
(let f (find-global-callable( function-name (typeof args) args )))
88
(match f (
99
()
1010
( (Glb( _ (Abs( lhs rhs tlt )) )) (
@@ -21,7 +21,7 @@ cc-blob := λ(: caller-ctx FContext)(: function-name String)(: args AST)(: offse
2121

2222
cc-blob := λ(: callee-ctx FContext)(: function-name String)(: args-tt Type)(: offset I64)(: blame AST). (: (
2323
(let r (mk-fragment()))
24-
(let f (find-global-callable( function-name args-tt )))
24+
(let f (find-global-callable( function-name args-tt blame )))
2525
(match f (
2626
()
2727
( (Glb( _ (Abs( lhs rhs tlt )) )) (

PLUGINS/BACKEND/C/compile-expr.lm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ compile-expr := λ(: ctx FContext)(: term AST)(: stack-offset I64)(: used IsUsed
44
(match used (
55
()
66
( Return (
7-
(let fterm (find-global-callable( 'cdecl::return_s (typeof term) )))
7+
(let fterm (find-global-callable( 'cdecl::return_s (typeof term) term )))
88
(set e (std-c-compile-call( ctx 'cdecl::return_s fterm term )))
99
))
1010
( _ (

PLUGINS/BACKEND/C/std-c-compile-call.lsts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
let std-c-compile-call(ctx: FContext, fname: CString, fterm: AST, args: AST): Fragment = (
3-
fterm = find-global-callable(fname, typeof(args));
3+
fterm = find-global-callable(fname, typeof(args), args);
44
if not(non-zero(fterm)) then fail("std-c-compile-call Function was null: \{fterm}\nArguments: \{typeof(args)}\n");
55
if typeof(fterm).is-t(c"Blob") {
66
let r = mk-fragment();
@@ -21,7 +21,7 @@ let std-c-compile-call(ctx: FContext, fname: CString, fterm: AST, args: AST): Fr
2121
let inner-ctx = mk-fctx().bind(c"ictx", t1(c"ImplicitContext"), ictx)
2222
.bind(c"args", typeof(args), push-args);
2323
let r = mk-fragment();
24-
match find-global-callable(c"primitive::call", t3(c"Cons",t1(c"ImplicitContext"),typeof(args))) {
24+
match find-global-callable(c"primitive::call", t3(c"Cons",t1(c"ImplicitContext"),typeof(args)), args) {
2525
Glb{val:Abs{lhs=lhs, rhs=rhs}} => (
2626
r = blob-render(inner-ctx, rhs, r, 0);
2727
r.context = close(ctx);

PLUGINS/BACKEND/C/std-c-compile-expr.lsts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ let std-c-compile-expr(ctx: FContext, t: AST, is-stmt: Bool): Fragment = (
6666
guid = uuid();
6767
compile-smart-string-index = compile-smart-string-index.bind(val,guid);
6868
let lit = mk-lit(val); ascript-normal(lit, t1(c"String"));
69-
let intern-ss = std-c-compile-call(ctx, c"intern", find-global-callable(c"intern", t1(c"String")), lit);
69+
let intern-ss = std-c-compile-call(ctx, c"intern", find-global-callable(c"intern", t1(c"String"), t), lit);
7070
assemble-header-section = assemble-header-section +
7171
mangle-c-type(typeof(t)) + SAtom{c" "} + SAtom{guid} + SAtom{c";\n"};
7272
assemble-string-initializer-section = assemble-string-initializer-section +

PLUGINS/FRONTEND/LSTS/lsts-parse.lsts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ let lsts-parse-typed-macro(tokens: List<Token>): (AST, List<Token>) = (
296296
lsts-parse-expect(c";", tokens); tokens = tail(tokens);
297297
(mk-glb(
298298
mk-token(mname),
299-
mk-abs(margs, mk-app(mk-lit(c":"), mk-cons(mbody,mk-atype(mrett))), t1(c"TypedMacro"))
299+
mk-abs(margs, mk-app(mk-lit(c":"), mk-cons(without-location(mbody),mk-atype(mrett))), t1(c"TypedMacro"))
300300
), tokens)
301301
);
302302

SRC/find-global-callable.lsts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
let find-global-callable(fname: CString, arg-types: Type): AST = (
2+
let find-global-callable(fname: CString, arg-types: Type, blame: AST): AST = (
33
arg-types = denormalize-strong(arg-types);
44
fname = find-alias(fname, arg-types);
55
let match-set = mk-vector(type((Type,AST)), 16);
@@ -24,7 +24,7 @@ let find-global-callable(fname: CString, arg-types: Type): AST = (
2424
};
2525
exit(1);
2626
};
27-
if not(non-zero(result)) then fail("Unable to find appropriate global callable: \{fname} \{arg-types}\n");
27+
if not(non-zero(result)) then fail("Unable to find appropriate global callable: \{fname} \{arg-types}\nAt: \{blame.location}\n");
2828
result
2929
);
3030

SRC/macro-table.lsts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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)>);
44
index-macro-table = index-macro-table.bind(c"macro::location", [] :: List<(Type,Type,AST)>);
5+
index-macro-table = index-macro-table.bind(c"macro::variable", [] :: List<(Type,Type,AST)>);
56

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

SRC/std-apply-macro-candidates.lsts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ let std-try-destructure-macro(tctx: TContext?, margs: AST, mtype: Type, mcandida
4646
match margs {
4747
App{left:Var{maybe-helper=key}, helper-args=right} => (
4848
if macro-helper==maybe-helper {
49-
(tctx, let helped) = std-apply-macro-weak(tctx, macro-helper, helper-args);
49+
(tctx, let helped) = std-apply-macro-weak(tctx, macro-helper, helper-args, Used);
50+
if non-zero(helped)
51+
then (tctx, std-direct-destructure-macro(helped, mstruct))
52+
else (tctx, no)
53+
} else (tctx, no)
54+
);
55+
_ => (
56+
if macro-helper==c"macro::variable" {
57+
(tctx, let helped) = std-apply-macro-weak(tctx, macro-helper, margs, Used);
5058
if non-zero(helped)
5159
then (tctx, std-direct-destructure-macro(helped, mstruct))
5260
else (tctx, no)
5361
} else (tctx, no)
5462
);
55-
_ => (tctx, no);
5663
}
5764
} else (tctx, std-direct-destructure-macro(margs, mstruct))
5865
);

0 commit comments

Comments
 (0)