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
9871602d
Commit
9871602d
authored
Apr 13, 2021
by
Sapphie
Browse files
Fix bug
parent
32c98d65
Changes
3
Hide whitespace changes
Inline
Side-by-side
compiler/src/l3/CPSValueRepresenter.scala
View file @
9871602d
...
...
@@ -32,11 +32,11 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
private
def
transformLetF
(
initialFuns
:
Seq
[
H.Fun
],
body
:
H.Tree
)
:
L.LetF
=
{
// for each function, closes it and returns all the variables that used be free in it
// as well as the associated
w
or
ker
function
// as well as the associated or
iginal
function
name
def
transformFunAbs
(
funs
:
Seq
[
H.Fun
])
:
Seq
[(
L.Fun
,
Seq
[
Symbol
]
,
Symbol
)]
=
funs
match
{
case
Nil
=>
Nil
case
f
::
fs
=>
val
workerFun
=
Symbol
.
fresh
(
"w"
)
val
workerFun
=
Symbol
.
fresh
(
"w
orker_function
"
)
val
envName
=
Symbol
.
fresh
(
"env"
)
val
newArgs
=
envName
+:
f
.
args
val
funBody
=
apply
(
f
.
body
)
...
...
@@ -49,14 +49,14 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
substitute
(
funBody
)(
accSubst
)
case
freeVar
::
vs
=>
// Bind the fresh variable v to a block get
val
v
=
Symbol
.
fresh
(
"
v
"
)
val
v
=
Symbol
.
fresh
(
"
block_variable
"
)
L
.
LetP
(
v
,
CPS
.
BlockGet
,
Seq
(
L
.
AtomN
(
envName
),
L
.
AtomL
(
counter
)),
argsBindings
(
vs
,
counter
+
1
,
accSubst
+
(
freeVar
->
v
)))
}
val
newFunBody
=
argsBindings
(
fv
,
1
,
Map
())
val
newFun
=
L
.
Fun
(
workerFun
,
f
.
retC
,
newArgs
,
newFunBody
)
(
newFun
,
fv
,
workerFun
)
+:
transformFunAbs
(
fs
)
(
newFun
,
fv
,
f
.
name
)
+:
transformFunAbs
(
fs
)
}
def
allocInitFun
(
funsAndVars
:
Seq
[(
L.Fun
,
Seq
[
Symbol
]
,
Symbol
)],
lastBody
:
L.Tree
)
:
L.Tree
=
{
...
...
@@ -70,13 +70,13 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
funsAndVars
match
{
case
Nil
=>
lastBody
case
(
f
un
,
vars
,
w
or
kerFun
)
::
rest
=>
val
blockAtom
=
L
.
AtomN
(
fun
.
n
ame
)
case
(
workerF
un
,
vars
,
or
iginalFunName
)
::
rest
=>
val
blockAtom
=
L
.
AtomN
(
originalFunN
ame
)
val
varInits
=
initFun
(
vars
,
1
,
blockAtom
,
rest
)
val
t1
=
Symbol
.
fresh
(
"
t1
"
)
val
prim
Args
=
Seq
(
blockAtom
,
L
.
AtomL
(
0
),
L
.
AtomN
(
workerFun
))
val
nextBody
=
L
.
LetP
(
t1
,
CPS
.
BlockSet
,
prim
Args
,
varInits
)
L
.
LetP
(
fun
.
n
ame
,
CPS
.
BlockAlloc
(
202
),
Seq
(
L
.
AtomL
(
vars
.
length
+
1
)),
nextBody
)
val
t1
=
Symbol
.
fresh
(
"
blockset_unused
"
)
val
blockSet
Args
=
Seq
(
blockAtom
,
L
.
AtomL
(
0
),
L
.
AtomN
(
workerFun
.
name
))
val
nextBody
=
L
.
LetP
(
t1
,
CPS
.
BlockSet
,
blockSet
Args
,
varInits
)
L
.
LetP
(
originalFunN
ame
,
CPS
.
BlockAlloc
(
202
),
Seq
(
L
.
AtomL
(
vars
.
length
+
1
)),
nextBody
)
}
}
...
...
@@ -85,7 +85,9 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
val
lastBody
=
apply
(
body
)
val
closureAllocInit
=
allocInitFun
(
funsAndVars
,
lastBody
)
L
.
LetF
(
funsAndVars
.
unzip3
.
_1
,
closureAllocInit
)
val
res
=
L
.
LetF
(
funsAndVars
.
unzip3
.
_1
,
closureAllocInit
)
println
(
res
)
res
}
// Substitutes _free_ variables in `tree`
...
...
compiler/src/l3/Main.scala
View file @
9871602d
...
...
@@ -13,6 +13,7 @@ object Main {
def
main
(
args
:
Array
[
String
])
:
Unit
=
{
val
backEnd
:
Tree
=>
TerminalPhaseResult
=
(
CL3ToCPSTranslator
andThen
treePrinter
(
"---------- After CPS translation"
)
andThen
CPSValueRepresenter
andThen
treePrinter
(
"---------- After value representation"
)
andThen
treeChecker
...
...
tests/stmt-defrec.l3
View file @
9871602d
...
...
@@ -2,13 +2,15 @@
;; Test the "defrec" statement
(@byte-write 67)
;;
(@byte-write 67)
(defrec succ (fun (x) (@+ x 1)))
(@byte-write (succ 64))
(defrec fact (fun (x) (if (@= x 0) 1 (@* x (fact (@- x 1))))))
(@byte-write (@- (fact 5) 54))
(defrec fact (fun (x) (@+ x 2)))
(@byte-write (fact 65))
(defrec id (fun (x) (x)))
(id 1)
;;(defrec succ (fun (x) (@+ x 1)))
;;(@byte-write (succ 64))
;;
;;(defrec fact (fun (x) (if (@= x 0) 1 (@* x (fact (@- x 1))))))
;;(@byte-write (@- (fact 5) 54))
;;
;;(defrec fact (fun (x) (@+ x 2)))
;;(@byte-write (fact 65))
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