Skip to content

Commit 46a7852

Browse files
committed
[lec9] Hands-on lab3 live code snippets
1 parent 875ccb2 commit 46a7852

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ Material: plt = course book, dragon = Dragon book. Slides follow closely the plt
5050
| Tue 26/11 | 13-14 | Hands-on with Lab 2 (Haskell) | [script](notes/monads.html), [live code](live/2024/lab2-live-haskell.zip) |
5151
| Tue 26/11 | 14-15 | Hands-on with Lab 2 (Java) | [script](notes/java.html), [Annotated.java](notes/Annotated.java) |
5252
| Thu 28/11 | 13-15 **SB-H5** | Code generation | [slides](plt-book/ipl-book/slides/6-slides-ipl-book.pdf), plt 6, dragon 6,7, [notes](notes/compilation.html), [prime.c](notes/prime.c), [prime.j](notes/prime.j) |
53-
| Tue 03/12 | 13-14 | Hands-on with Lab 3 (Haskell) | |
54-
| Tue 03/12 | 14-15 | Hands-on with Lab 3 (Java) | |
53+
| Tue 03/12 | 13-14 | Hands-on with Lab 3 (Haskell) | [live code snippet](live/2024/lab3-live-haskell.hs) |
54+
| Tue 03/12 | 14-15 | Hands-on with Lab 3 (Java) | [live code snippet](live/2024/lab3-live-java.java) |
5555
| *Wed 04/12* | *23* | *Lab 2 deadline* | |
5656
| Thu 05/12 | 13-15 | Functional programming languages | [slides](plt-book/ipl-book/slides/7-slides-ipl-book.pdf), plt 7, dragon 6.5,7.3, [script](notes/cbn-cbv.html) |
5757
| Tue 10/12 | 13-15 | Type inference and polymorphism | plt 7.7-9, [script](notes/typing.html) |

live/2024/lab3-live-haskell.hs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
-- | Compile a function, to be called in initial environment.
3+
4+
compileFun :: Type -> [Arg] -> [Stm] -> Compile ()
5+
compileFun t args ss = do
6+
mapM_ (\ (ADecl t' x) -> newVar x t') args
7+
mapM_ compileStm ss
8+
-- Default return
9+
-- Push 0 on the stack, depending on t
10+
if t == Type_double then emit $ DConst 0.0 else
11+
if t /= Type_void then emit $ IConst 0
12+
else pure ()
13+
emit $ Return t
14+
-- | Compile a statement.
15+
16+
compileStm :: Stm -> Compile ()
17+
compileStm s0 = do
18+
19+
-- Output a comment with the statement to compile.
20+
-- TODO
21+
22+
-- Compile the statement.
23+
case s0 of
24+
25+
SDecl t x -> newVar x t
26+
SExp t e -> do
27+
compileExp e
28+
emit $ Pop t
29+
SReturn t e -> do
30+
compileExp e
31+
emit $ Return t
32+
SBlock b -> compileBlock b
33+
s -> error $ "Not yet implemented: compileStm " ++ printTree s
34+
35+
-- | Compile a block.
36+
37+
compileBlock :: Block -> Compile ()
38+
compileBlock (Block ss) = do
39+
inNewBlock $ mapM_ compileStm ss
40+
41+
42+
-- | Compile an expression to leave its value on the stack.
43+
44+
compileExp :: Exp -> Compile ()
45+
compileExp = \case
46+
EInt i -> emit $ IConst i
47+
EBool b -> emit $ IConst $ if b then 1 else 0
48+
EId x -> do
49+
(a, t) <- lookupVar x
50+
emit $ Load t a
51+
EAss x e -> do
52+
compileExp e
53+
(a, t) <- lookupVar x
54+
emit $ Store t a
55+
emit $ Load t a
56+
EApp t x es -> do
57+
mapM_ compileExp es
58+
m <- gets sig
59+
let f = Map.findWithDefault (error "undefined fun") x m
60+
emit $ Call f
61+
62+
e -> error $ "Not yet implemented: compileExp " ++ printTree e

live/2024/lab3-live-java.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
void compile(Ann.Stm s) {
3+
switch(s) {
4+
case Ann.SDecl p -> { newVar(p.name(), p.type()); }
5+
case Ann.SBlock p -> { newBlock(); compile(p.stms()); popBlock(); }
6+
case Ann.SExp p -> { compile(p.exp()); emit(new Code.Pop(p.exp().type())); }
7+
case Ann.SReturn p -> { compile(p.exp()); emit(new Code.Return(p.exp().type())); }
8+
9+
default -> throw new RuntimeException("compile(Stm): case nyi");
10+
}
11+
}
12+
13+
void compile(Ann.Exp e) {
14+
switch(e) {
15+
case Ann.EInt p -> { emit (new Code.IConst(p.lit()));}
16+
case Ann.EAss p -> {
17+
int addr = lookupVar(p.name()).addr();
18+
compile(p.exp());
19+
emit (new Code.Store(p.type(), addr));
20+
emit (new Code.Load(p.type(), addr));
21+
}
22+
case Ann.EId p -> {
23+
int addr = lookupVar(p.name()).addr();
24+
emit (new Code.Load(p.type(), addr));
25+
}
26+
case Ann.EApp p -> {
27+
for (Ann.Exp e1 : p.args()) compile(e1);
28+
Fun f = lookupFun(p.fun());
29+
emit(new Code.Call(f));
30+
}
31+
default -> throw new RuntimeException("compile(Exp): case nyi");
32+
}
33+
}

0 commit comments

Comments
 (0)