Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Haley Sapphie Owsianko
ACC-project
Commits
4ff877f5
Commit
4ff877f5
authored
Mar 20, 2021
by
Sapphie
Browse files
Restructure code and implement mult primitive representation
parent
6ee6828e
Changes
1
Hide whitespace changes
Inline
Side-by-side
compiler/src/l3/CPSValueRepresenter.scala
View file @
4ff877f5
...
...
@@ -8,25 +8,42 @@ import l3.{CPSTestPrimitive => CPST}
object
CPSValueRepresenter
extends
(
H
.
Tree
=>
L
.
Tree
)
{
def
apply
(
tree
:
H.Tree
)
:
L.Tree
=
tree
match
{
case
H
.
LetP
(
n
,
L3
.
IntAdd
,
Seq
(
x
,
y
),
body
)
=>
tempLetP
(
CPS
.
Sub
,
Seq
(
Left
(
x
),
Right
(
1
)))
{
x1
=>
L
.
LetP
(
n
,
CPS
.
Add
,
Seq
(
x1
,
rewrite
(
y
)),
apply
(
body
))
}
case
H
.
LetP
(
n
,
L3
.
IntSub
,
Seq
(
x
,
y
),
body
)
=>
tempLetP
(
CPS
.
Add
,
Seq
(
Left
(
x
),
Right
(
1
)))
{
x1
=>
L
.
LetP
(
n
,
CPS
.
Sub
,
Seq
(
x1
,
rewrite
(
y
)),
apply
(
body
))
}
case
H
.
LetP
(
n
,
prim
,
Seq
(
x
,
y
),
body
)
=>
applyLetPBin
(
n
,
prim
,
x
,
y
,
body
)
case
_
=>
???
}
// Transforms primitives that take 2 arguments
private
def
applyLetPBin
(
n
:
H.Name
,
prim
:
L3
,
x
:
H.Atom
,
y
:
H.Atom
,
body
:
H.Tree
)
:
L.LetP
=
{
val
lAtomOne
:
Either
[
H.Atom
,
L.Atom
]
=
Right
(
L
.
AtomL
(
1
))
prim
match
{
case
L3
.
IntAdd
=>
tempLetP
(
CPS
.
Sub
,
Seq
(
Left
(
x
),
lAtomOne
))
{
x1
=>
L
.
LetP
(
n
,
CPS
.
Add
,
Seq
(
x1
,
rewrite
(
y
)),
apply
(
body
))
}
case
L3
.
IntSub
=>
tempLetP
(
CPS
.
Add
,
Seq
(
Left
(
x
),
lAtomOne
))
{
x1
=>
L
.
LetP
(
n
,
CPS
.
Sub
,
Seq
(
x1
,
rewrite
(
y
)),
apply
(
body
))
}
case
L3
.
IntMul
=>
tempLetP
(
CPS
.
Sub
,
Seq
(
Left
(
x
),
lAtomOne
))
{
x1
=>
tempLetP
(
CPS
.
ShiftRight
,
Seq
(
Left
(
y
),
lAtomOne
))
{
y1
=>
tempLetP
(
CPS
.
Mul
,
Seq
(
Right
(
x1
),
Right
(
y1
)))
{
z
=>
L
.
LetP
(
n
,
CPS
.
Add
,
Seq
(
z
,
L
.
AtomL
(
1
)),
apply
(
body
))
}
}
}
}
}
// Creates an outer LetP, and binds the result to a name,
// then passes the name to mkBody
// Works similarly to transform in the CL3->CPS translation
// Does *not* tag the integers given as arguments
private
def
tempLetP
(
p
:
CPS
,
args
:
Seq
[
Either
[
H.Atom
,
Int
]])
(
mkBody
:
L.Atom
=>
L
.
LetP
)
:
L.LetP
=
{
private
def
tempLetP
(
p
:
CPS
,
args
:
Seq
[
Either
[
H.Atom
,
L.Atom
]])
(
mkBody
:
L.Atom
=>
L
.
LetP
)
:
L.LetP
=
{
val
lArgs
=
args
.
map
{
case
Left
(
a
tom
)
=>
rewrite
(
a
tom
)
case
Right
(
int
)
=>
L
.
Atom
L
(
int
)
case
Left
(
hA
tom
)
=>
rewrite
(
hA
tom
)
case
Right
(
lAtom
)
=>
l
Atom
}
val
tmpName
=
Symbol
.
fresh
(
"x"
)
val
innerLetP
=
mkBody
(
L
.
AtomN
(
tmpName
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment