|
| 1 | + |
| 2 | +let preprocess-apply(program: AST): AST = preprocess-apply(program, false); |
| 3 | + |
| 4 | +let preprocess-apply(program: AST, is-lhs: Bool): AST = ( |
| 5 | + match program { |
| 6 | + App{ left:App{ left:Var{key:c"macro::concat",ctk1=token}, right:App{ left:Var{lk1=key}, right:Var{rk1=key} } }, nt1=right } => ( |
| 7 | + mk-app( mk-var(lk1+rk1, ctk1.unique), preprocess-apply(nt1) ) |
| 8 | + ); |
| 9 | + App{ left:App{ left:Var{key:c"macro::concat",ctk2=token}, right:App{ left:Var{lk2=key}, right:Lit{rk2=key} } }, nt2=right } => ( |
| 10 | + mk-app( mk-var(lk2+rk2, ctk2.unique), preprocess-apply(nt2) ) |
| 11 | + ); |
| 12 | + App{ left:App{ left:Var{key:c"macro::concat",ctk3=token}, right:App{ left:Lit{lk3=key}, right:Var{rk3=key} } }, nt3=right } => ( |
| 13 | + mk-app( mk-var(lk3+rk3, ctk3.unique), preprocess-apply(nt3) ) |
| 14 | + ); |
| 15 | + App{ left:App{ left:Var{key:c"macro::concat",ctk4=token}, right:App{ left:Lit{lk4=key}, right:Lit{rk4=key} } }, nt4=right } => ( |
| 16 | + mk-app( mk-var(lk4+rk4, ctk4.unique), preprocess-apply(nt4) ) |
| 17 | + ); |
| 18 | + App{ is-cons-14=is-cons, |
| 19 | + left:App{is-cons-24=is-cons, |
| 20 | + left:Lit{key:c":",ctk4=token}, |
| 21 | + right:App{is-cons-34=is-cons, |
| 22 | + t4=left:Lit{l4=key,ltk4=token}, |
| 23 | + right:AType{tt4=tt} |
| 24 | + } |
| 25 | + }, nt4=right } => ( |
| 26 | + if not(tt4.is-t(c"String",0)) then t4 = preprocess-apply(t4); |
| 27 | + mk-cons-or-app(is-cons-14, |
| 28 | + mk-cons-or-app(is-cons-24, |
| 29 | + mk-lit(c":",ctk4.unique), |
| 30 | + mk-cons-or-app(is-cons-34,t4,mk-atype(tt4)) |
| 31 | + ), preprocess-apply(nt4,is-lhs) |
| 32 | + ); |
| 33 | + ); |
| 34 | + App{ is-cons-15=is-cons, |
| 35 | + left:App{is-cons-25=is-cons, |
| 36 | + left:Lit{key:c":",ctk5=token}, |
| 37 | + right:App{is-cons-35=is-cons, |
| 38 | + t5=left, |
| 39 | + right:AType{tt5=tt} |
| 40 | + } |
| 41 | + }, nt5=right } => ( |
| 42 | + tt5 = phi-as-state(tt5).expand-implied-phi; |
| 43 | + mk-cons-or-app(is-cons-15, |
| 44 | + mk-cons-or-app(is-cons-25, |
| 45 | + mk-lit(c":",ctk5.unique), |
| 46 | + mk-cons-or-app(is-cons-35,preprocess-apply(t5,is-lhs),mk-atype(tt5)) |
| 47 | + ), preprocess-apply(nt5,is-lhs) |
| 48 | + ); |
| 49 | + ); |
| 50 | + App{is-cons-26=is-cons, left:Lit{key:c":",ctk6=token}, right:App{is-cons-36=is-cons,t6=left:Lit{},right:AType{tt6=tt}}} => ( |
| 51 | + if not(tt6.is-t(c"String",0)) then t6 = preprocess-apply(t6); |
| 52 | + mk-cons-or-app(is-cons-26, |
| 53 | + mk-lit(c":",ctk6.unique), |
| 54 | + mk-cons-or-app(is-cons-36,t6,mk-atype(tt6)) |
| 55 | + ); |
| 56 | + ); |
| 57 | + App{is-cons-27=is-cons, left:Lit{key:c":",ctk7=token}, right:App{is-cons-37=is-cons,t7=left,right:AType{tt7=tt}}} => ( |
| 58 | + tt7 = phi-as-state(tt7).expand-implied-phi; |
| 59 | + t7 = preprocess-apply(t7); |
| 60 | + mk-cons-or-app(is-cons-27, |
| 61 | + mk-lit(c":",ctk7.unique), |
| 62 | + mk-cons-or-app(is-cons-37,t7,mk-atype(tt7)) |
| 63 | + ); |
| 64 | + ); |
| 65 | + App{ |
| 66 | + left:App{ |
| 67 | + left:Var{key:c"as",atk8=token}, |
| 68 | + right:App{ t8=left, right:AType{tt8=tt} } |
| 69 | + }, nt8=right |
| 70 | + } => ( |
| 71 | + mk-app( mk-app( mk-var(c"as",atk8.unique), mk-app(preprocess-apply(t8),mk-atype(tt8)) ), preprocess-apply(nt8) ) |
| 72 | + ); |
| 73 | + Lit{l9=key,ltk9=token} => ( |
| 74 | + for Tuple{sfxs9=first, sfxtt9=second} in parse-suffixes { |
| 75 | + if l9.has-suffix(sfxs9) { |
| 76 | + let lpfx9 = l9.remove-suffix(sfxs9).get-or(c""); |
| 77 | + program = mk-app( mk-lit(c":",with-key(ltk9,c":")), mk-app( mk-lit(lpfx9,with-key(ltk9,lpfx9)), mk-atype(sfxtt9) ) ) |
| 78 | + } |
| 79 | + }; program |
| 80 | + ); |
| 81 | + Var{l10=key,ltk10=token} => ( |
| 82 | + for Tuple{sfxs10=first, sfxtt10=second} in parse-suffixes { |
| 83 | + if l10.has-suffix(sfxs10) { |
| 84 | + let lpfx10 = l10.remove-suffix(sfxs10).get-or(c""); |
| 85 | + program = mk-app( mk-lit(c":",with-key(ltk10,c":")), mk-app( mk-lit(lpfx10,with-key(ltk10,lpfx10)), mk-atype(sfxtt10) ) ) |
| 86 | + } |
| 87 | + }; program |
| 88 | + ); |
| 89 | + App{ is-cons11=is-cons, left:Var{vn11=key,vntk11=token}, vt11=right } => ( |
| 90 | + if is-macro-head(vn11,1) { |
| 91 | + preprocess-apply-hard(vn11,1,program) |
| 92 | + } else { |
| 93 | + mk-cons-or-app(is-cons11, preprocess-apply(mk-var(vn11,vntk11.unique)), preprocess-apply(vt11)) |
| 94 | + } |
| 95 | + ); |
| 96 | + App{ is-cons-112=is-cons, left:App{ is-cons-212=is-cons, left:Var{vn12=key,vntk12=token}, vt012=right }, vt112=right } => ( |
| 97 | + if is-macro-head(vn12,2) { |
| 98 | + preprocess-apply-hard(vn12,2,program) |
| 99 | + } else { |
| 100 | + mk-cons-or-app(is-cons-112, |
| 101 | + preprocess-apply(mk-cons-or-app(is-cons-212, |
| 102 | + mk-var(vn12,vntk12.unique), vt012 |
| 103 | + ),is-lhs), preprocess-apply(vt112,is-lhs) |
| 104 | + ) |
| 105 | + } |
| 106 | + ); |
| 107 | + Seq{seq=seq} => ( |
| 108 | + program = mk-eof(); |
| 109 | + for s in seq { program = program + preprocess-apply(s) }; |
| 110 | + program |
| 111 | + ); |
| 112 | + App{is-cons14=is-cons, al14=left, ar14=right} => ( |
| 113 | + mk-cons-or-app( is-cons14, preprocess-apply(al14,is-lhs), preprocess-apply(ar14,is-lhs) ) |
| 114 | + ); |
| 115 | + Abs{al15=lhs, ar15=rhs, tlt15=tt} => ( |
| 116 | + if not(tlt15.is-t(c"TypedMacro",0)) |
| 117 | + then mk-abs(preprocess-apply(al15,true),preprocess-apply(ar15),tlt15) |
| 118 | + else program |
| 119 | + ); |
| 120 | + Glb{k16=key, ar16=val} => mk-glb(k16.unique, preprocess-apply(ar16)); |
| 121 | + _ => program; |
| 122 | + } |
| 123 | +); |
| 124 | + |
0 commit comments