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
284fa4ce
Commit
284fa4ce
authored
Mar 20, 2021
by
Sapphie
Browse files
Implement add primitive
parent
68ff58c4
Changes
1
Hide whitespace changes
Inline
Side-by-side
compiler/src/l3/CPSValueRepresenter.scala
View file @
284fa4ce
package
l3
object
CPSValueRepresenter
extends
(
Any
=>
Nothing
)
{
def
apply
(
tree
:
Any
)
:
Nothing
=
???
import
l3.
{
SymbolicCPSTreeModule
=>
H
}
import
l3.
{
SymbolicCPSTreeModuleLow
=>
L
}
import
l3.
{
L3Primitive
=>
L3
}
import
l3.
{
CPSValuePrimitive
=>
CPS
}
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
))
}
}
// 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
=
{
val
lArgs
=
args
.
map
{
case
Left
(
atom
)
=>
rewrite
(
atom
)
case
Right
(
int
)
=>
L
.
AtomL
(
int
)
}
val
tmpName
=
Symbol
.
fresh
(
"x"
)
val
innerLetP
=
mkBody
(
L
.
AtomN
(
tmpName
))
L
.
LetP
(
tmpName
,
p
,
lArgs
,
innerLetP
)
}
private
def
rewrite
(
a
:
H.Atom
)
:
L.Atom
=
a
match
{
case
H
.
AtomN
(
n
)
=>
L
.
AtomN
(
n
)
case
H
.
AtomL
(
IntLit
(
i
))
=>
L
.
AtomL
((
i
.
toInt
<<
1
)
|
0x1
)
case
H
.
AtomL
(
CharLit
(
c
))
=>
L
.
AtomL
((
c
.
toInt
<<
3
)
|
0x6
)
// 110
case
H
.
AtomL
(
BooleanLit
(
b
))
=>
if
(
b
)
L
.
AtomL
(
0x1a
)
// 11010
else
L
.
AtomL
(
0x0a
)
// 01010
case
H
.
AtomL
(
UnitLit
)
=>
L
.
AtomL
(
2
)
}
}
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