Skip to content

Commit

Permalink
Change to eliminate unnecessary EmptyB and EmptyD syntax nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Mar 1, 2020
1 parent 092f8b6 commit 789ffb2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ dec :
| atdec
{ $1 }
| atdec SEMI dec
{ SeqD($1, $3)@@at() }
{ seqD($1, $3)@@at() }
;

dotpathexp :
Expand Down Expand Up @@ -378,7 +378,7 @@ bind :
| atbind
{ $1 }
| atbind SEMI bind
{ SeqB($1, $3)@@at() }
{ seqB($1, $3)@@at() }
;

atpat :
Expand Down
28 changes: 20 additions & 8 deletions syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,24 @@ let typParam param =
let typParamList paramList =
List.map typParam paramList

let seqD(l, r) =
match l.it, r.it with
| EmptyD, _ -> r.it
| _, EmptyD -> l.it
| _ -> SeqD(l, r)

let seqB(l, r) =
match l.it, r.it with
| EmptyB, _ -> r.it
| _, EmptyB -> l.it
| _ -> SeqB(l, r)

(* Sugar *)

let letE(b, e) =
let x' = var "let" in
let b2 = VarB(x'@@e.at, e)@@e.at in
DotE(StrE(SeqB(b, b2)@@span[b.at; e.at])@@span[b.at; e.at], x'@@e.at)
DotE(StrE(seqB(b, b2)@@span[b.at; e.at])@@span[b.at; e.at], x'@@e.at)

let letT(b, t) = PathT(letE(b, TypE(t)@@t.at)@@span[b.at; t.at])
let letB(b, b') = InclB(letE(b, StrE(b')@@b'.at)@@span[b.at; b'.at])
Expand All @@ -94,15 +106,15 @@ and tupT' n = function
| [] -> EmptyD@@nowhere_region
| t::ts ->
let d = tupT' (n + 1) ts in
SeqD(VarD((index n)@@t.at, t)@@t.at, d)@@
seqD(VarD((index n)@@t.at, t)@@t.at, d)@@
(match d.it with EmptyD -> t.at | _ -> span[t.at; d.at])

let rec tupE(es) = StrE(tupE' 1 es)
and tupE' n = function
| [] -> EmptyB@@nowhere_region
| e::es ->
let b = tupE' (n + 1) es in
SeqB(VarB((index n)@@e.at, e)@@e.at, b)@@
seqB(VarB((index n)@@e.at, e)@@e.at, b)@@
(match b.it with EmptyB -> e.at | _ -> span[e.at; b.at])

let rec funT(ps, t, f) = (funT'(ps, t, f)).it
Expand Down Expand Up @@ -138,7 +150,7 @@ let doB(e) = letB(VarB("_"@@e.at, e)@@e.at, EmptyB@@e.at)

let seqE(es) =
let b =
List.fold_right (fun e b -> SeqB(doB(e)@@e.at, b)@@span[e.at; b.at])
List.fold_right (fun e b -> seqB(doB(e)@@e.at, b)@@span[e.at; b.at])
es (EmptyB@@(after (Lib.List.last es).at))
in
doE(StrE(b)@@@(List.map at es))
Expand Down Expand Up @@ -172,7 +184,7 @@ let appE(e1, e2) =
let x2' = var "app2" in
let b1 = VarB(x1'@@e1.at, e1)@@e1.at in
let b2 = VarB(x2'@@e2.at, e2)@@e2.at in
let b = SeqB(b1, b2)@@span[e1.at; e2.at] in
let b = seqB(b1, b2)@@span[e1.at; e2.at] in
letE(b, AppE(x1'@@e1.at, x2'@@e2.at)@@span[e1.at; e2.at])

let wrapE(e, t) =
Expand Down Expand Up @@ -272,7 +284,7 @@ let asTopt(to1, to2) =
let asP(p1, p2) =
let b1, to1 = p1.it in
let b2, to2 = p2.it in
SeqB(b1.it@@p1.at, b2.it@@p2.at)@@span[p1.at; p2.at], asTopt(to1, to2)
seqB(b1.it@@p1.at, b2.it@@p2.at)@@span[p1.at; p2.at], asTopt(to1, to2)

let annotP(p, t2) =
let b, to1 = p.it in
Expand All @@ -299,9 +311,9 @@ let strP(xps, region) =
List.fold_right (fun xp (b, d) ->
let x, p = xp.it in
let _, t = (defaultP p).it in
SeqB(patB(p, DotE(VarE("$"@@xp.at)@@xp.at, x)@@xp.at)@@xp.at, b)
seqB(patB(p, DotE(VarE("$"@@xp.at)@@xp.at, x)@@xp.at)@@xp.at, b)
@@span[b.at; p.at],
SeqD(VarD(x, t.it@@p.at)@@xp.at, d)@@span[d.at; p.at]
seqD(VarD(x, t.it@@p.at)@@xp.at, d)@@span[d.at; p.at]
) xps (EmptyB@@xp.at, EmptyD@@xp.at)
in b, Some (StrT(d)@@d.at)

Expand Down

0 comments on commit 789ffb2

Please sign in to comment.