|
| 1 | + |
| 2 | +let try-destructure-macro(loc: SourceLocation, lhs: AST, term: AST): AContext? = ( |
| 3 | + let r = None : AContext?; |
| 4 | + let yes = Some([] : AContext); |
| 5 | + match (lhs, term) { |
| 6 | + Tuple{first:ASTNil{}, second:ASTNil{}} => r = yes; |
| 7 | + Tuple{ |
| 8 | + first:App{left:App{left:Var{}, right:Var{}}, right:Var{}}, |
| 9 | + second:App{left:App{left:Lit{}}} |
| 10 | + } => (); |
| 11 | + Tuple{ |
| 12 | + first:App{left:Lit{key:c":Literal:"},right:Var{pv=key}}, |
| 13 | + second:App{left:Lit{key:c":",ctk=token}, right:App{left:Lit{},right:AType{}}} |
| 14 | + } => r = r.bind(pv,term.with-location(ctk.location)); |
| 15 | + Tuple{ |
| 16 | + first:App{left:Var{},right:Var{}}, |
| 17 | + second:App{left:Lit{tag=key}} |
| 18 | + } => ( |
| 19 | + if is-lone-tag(tag) or tag.is-suffixed { |
| 20 | + match (lhs,term) { |
| 21 | + Tuple{first:App{pl=left,pr=right}, second:App{el=left,er=right}} => ( |
| 22 | + let ll = try-destructure-macro(loc,pl,el); |
| 23 | + if non-zero(ll) { |
| 24 | + let rl = try-destructure-macro(loc,pr,er); |
| 25 | + if non-zero(rl) then r = union(ll,rl); |
| 26 | + } |
| 27 | + ); |
| 28 | + } |
| 29 | + } |
| 30 | + ); |
| 31 | + Tuple{ first:App{pl1=left,pr1=right}, second:App{el1=left,er1=right} } => ( |
| 32 | + let ll1 = try-destructure-macro(loc,pl1,el1); |
| 33 | + if non-zero(ll1) { |
| 34 | + let rl1 = try-destructure-macro(loc,pr1,er1); |
| 35 | + if non-zero(rl1) then r = union(ll1,rl1); |
| 36 | + } |
| 37 | + ); |
| 38 | + Tuple{ first:Abs{pl2=lhs,pr2=rhs,ptlt2=tt}, second:Abs{el2=lhs,er2=rhs,etlt2=tt} } => ( |
| 39 | + let ll2 = try-destructure-macro(loc,pl2,el2); |
| 40 | + if non-zero(ll2) { |
| 41 | + let rl2 = try-destructure-macro(loc,pr2,er2); |
| 42 | + if non-zero(rl2) then r = union(ll2,rl2); |
| 43 | + } |
| 44 | + ); |
| 45 | + Tuple{ first:Lit{key:c":Any:"}, second:Var{key:c"_"} } => r = yes; |
| 46 | + Tuple{ first:Lit{pl3=key}, second:Var{el3=key} } => if pl3==el3 then r = yes; |
| 47 | + Tuple{ first:Lit{pl4=key}, second:Lit{el4=key} } => if pl4==el4 then r = yes; |
| 48 | + Tuple{ first:App{left:Lit{key:c":Literal:"}, right:Var{pv5=key}}, second:Lit{el5=key,eltk5=token} } => ( |
| 49 | + if index-of-tag(el5)==unknown-index-of-tag then r = r.bind(pv5,term.with-location(eltk5.location)); |
| 50 | + ); |
| 51 | + Tuple{ first:App{left:Lit{key:c":Field:"}, right:Var{pv6=key}}, second:Lit{el6=key,eltk6=token} } => ( |
| 52 | + if el6.has-prefix(c".") then r = r.bind(pv6,term.with-location(eltk6.location)); |
| 53 | + ); |
| 54 | + Tuple{ first:App{left:Lit{key:c":Variable:"}, right:Var{pv7=key}}, second:Var{key:c"_"} } => ( |
| 55 | + ); |
| 56 | + Tuple{ first:App{left:Lit{key:c":Variable:"}, right:Var{pv8=key}}, second:Var{el8=key,eltk8=token} } => ( |
| 57 | + r = r.bind(pv8,term.with-location(eltk8.location)); |
| 58 | + ); |
| 59 | + Tuple{ first:App{ left:App{ left:Lit{key:c":Tag:"}, right:Var{pv9=key} }, right:Var{pt9=key} }, second:Lit{el9=key,eltk9=token} } => ( |
| 60 | + if index-of-tag(el9)!=unknown-index-of-tag { |
| 61 | + r = yes; |
| 62 | + let tag-i = index-of-tag(el9).into(type(CString)); |
| 63 | + r = r.bind(pv9, mk-app( mk-lit(c":",with-key(eltk9,c":")), mk-app( mk-lit(tag-i,with-key(eltk9,tag-i)), mk-atype(t0(c"Constant") && t0(c"Literal") && t0(c"U64")) ) )); |
| 64 | + let tag-tt = c"Tag::" + el9; |
| 65 | + r = r.bind(pt9, mk-lit(tag-tt,with-key(eltk9,tag-tt))); |
| 66 | + } |
| 67 | + ); |
| 68 | + Tuple{ first:Var{pv10=key}, t10=second } => r = r.bind(pv10,term.with-location(t10.location)); |
| 69 | + _ => (); |
| 70 | + }; r |
| 71 | +); |
0 commit comments