Skip to content

Commit 1eb5624

Browse files
fixed a problem where cons were being lost
1 parent a1fa5be commit 1eb5624

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

SRC/std-apply-macro-candidates.lsts

Lines changed: 13 additions & 5 deletions
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 = with-location(substitute(mctx.get-or-panic, mrhs), margs.location);
14+
if mctx.is-some then t = with-location-preserve(substitute(mctx.get-or-panic, mrhs), margs.location);
1515
}
1616
};
1717
(tctx, t) = std-infer-expr(tctx, t, false, Used, TAny);
@@ -43,10 +43,18 @@ let std-try-destructure-macro(tctx: TContext?, margs: AST, mtype: Type, mcandida
4343
App{left:Lit{key:c":"}, right:App{mstruct=left, right:AType{tt=tt}}} => (
4444
if non-zero(std-macro-helper-name(tt))
4545
then {
46-
(tctx, let helped) = std-apply-macro-weak(tctx, std-macro-helper-name(tt), margs);
47-
if non-zero(helped)
48-
then (tctx, std-direct-destructure-macro(helped, mstruct))
49-
else (tctx, no)
46+
let macro-helper = std-macro-helper-name(tt);
47+
match margs {
48+
App{left:Var{maybe-helper=key}, helper-args=right} => (
49+
if macro-helper==maybe-helper {
50+
(tctx, let helped) = std-apply-macro-weak(tctx, macro-helper, helper-args);
51+
if non-zero(helped)
52+
then (tctx, std-direct-destructure-macro(helped, mstruct))
53+
else (tctx, no)
54+
} else (tctx, no)
55+
);
56+
_ => (tctx, no);
57+
}
5058
} else (tctx, std-direct-destructure-macro(margs, mstruct))
5159
);
5260
_ => (tctx, no);

SRC/std-apply-macro.lsts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let std-apply-macro(tctx: Maybe<TContext>, mname: CString, margs: AST): (TContex
1111
let std-apply-macro-weak(tctx: Maybe<TContext>, mname: CString, margs: AST): (TContext?, AST) = std-apply-macro(tctx, mname, margs, false);
1212

1313
let std-apply-macro(tctx: Maybe<TContext>, mname: CString, margs: AST, strong: Bool): (TContext?, AST) = (
14+
print("Apply Macro \{mname} \{margs}\n");
1415
let row = index-macro-table.lookup(mname, [] :: List<(Type,Type,AST)>);
1516
let peep-holes = TAny;
1617
let peeped = TAny;
@@ -47,7 +48,7 @@ let std-apply-macro(tctx: Maybe<TContext>, mname: CString, margs: AST, strong: B
4748
};
4849
(tctx, let result) = std-apply-macro-candidates(tctx, mname, margs, candidates);
4950
if strong && not(non-zero(result)) then exit-error("Failed to Apply Macro: \{mname}", margs);
50-
print("Apply Macro \{mname} \{margs}\nResult: \{result}\n");
51+
print("Applied Macro \{mname} \{margs}\nResult: \{result}\n");
5152
(tctx, result)
5253
);
5354

SRC/substitute.lsts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ let substitute(ctx: AContext, term: AST): AST = (
5252
for vector s in seq { ret = ret.push(substitute(ctx,s)) };
5353
Seq{ret}
5454
);
55-
App{is-cons=is-cons,left=left,right=right} => if is-cons
56-
then mk-cons(substitute(ctx,left), substitute(ctx,right))
57-
else mk-app(substitute(ctx,left), substitute(ctx,right));
55+
App{is-cons=is-cons,left=left,right=right} => mk-cons-or-app(is-cons,substitute(ctx,left),substitute(ctx,right));
5856
Typedef{lhs=lhs,rhs=rhs} => mk-typedef(substitute(ctx,lhs), substitute(ctx,rhs));
5957
Abs{lhs=lhs,rhs=rhs,tt=tt} => mk-abs(substitute(ctx,lhs), substitute(ctx,rhs), tt);
6058
Glb{key=key,val=val} => mk-glb(key, substitute(ctx,val));

SRC/with-location.lsts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,20 @@ let with-location(term: AST, loc: SourceLocation): AST = (
1919
let with-location(t: Token, loc: SourceLocation): Token = (
2020
if non-zero(t.location) then t else Token{ t.skey, t.key, iuid(), loc }
2121
);
22+
23+
let with-location-preserve(term: AST, loc: SourceLocation): AST = (
24+
match term {
25+
Var{key=key,token=token} => Var{key,with-location(token,loc)};
26+
Lit{key=key,token=token} => Lit{key,with-location(token,loc)};
27+
App{is-cons=is-cons,left=left,right=right} => mk-cons-or-app(is-cons,with-location-preserve(left,loc),with-location-preserve(right,loc));
28+
Seq{seq=seq} => (
29+
let ret = mk-vector(type(AST), seq.length);
30+
for vector s in seq { ret = ret.push(with-location-preserve(s,loc)) };
31+
Seq{ret}
32+
);
33+
Abs{lhs=lhs,rhs=rhs,tt=tt} => Abs{close(with-location-preserve(lhs,loc)),close(with-location-preserve(rhs,loc)),tt};
34+
Typedef{lhs=lhs,rhs=rhs} => Typedef{close(with-location-preserve(lhs,loc)),close(with-location-preserve(rhs,loc))};
35+
Glb{key=key,val=val} => Glb{with-location(key,loc),close(with-location-preserve(val,loc))};
36+
_ => term;
37+
}
38+
);

0 commit comments

Comments
 (0)