This commit is contained in:
NikolajDanger
2022-06-08 14:31:43 +02:00
parent e46e002e9f
commit 305725a986
18 changed files with 2672 additions and 3223 deletions

View File

@ -62,13 +62,9 @@ let rec removeDeadBindingsInExp (e : TypedExp) : (bool * DBRtab * TypedExp) =
ArrayLit (es', t, pos) )
(* ToDO: Task 3: implement the cases of `Var`, `Index` and `Let` expressions below *)
| Var (name, pos) ->
(* Task 3, Hints for the `Var` case:
- 1st element of result tuple: can a variable name contain IO?
- 2nd element of result tuple: you have discovered a name, hence
you need to record it in a new symbol table.
- 3rd element of the tuple: should be the optimised expression.
*)
failwith "Unimplemented removeDeadBindingsInExp for Var"
let symtab = SymTab.empty()
let symtab = recordUse name symtab
(false, symtab, Var(name, pos))
| Plus (x, y, pos) ->
let (xios, xuses, x') = removeDeadBindingsInExp x
let (yios, yuses, y') = removeDeadBindingsInExp y
@ -113,12 +109,9 @@ let rec removeDeadBindingsInExp (e : TypedExp) : (bool * DBRtab * TypedExp) =
List.fold SymTab.combine (SymTab.empty()) uses,
Apply (fname, args', pos))
| Index (name, e, t, pos) ->
(* Task 3, `Index` case: is similar to the `Var` case, except that,
additionally, you also need to recursively optimize the index
expression `e` and to propagate its results (in addition
to recording the use of `name`).
*)
failwith "Unimplemented removeDeadBindingsInExp for Index"
let (eio, symtab, e') = removeDeadBindingsInExp e
let symtab = recordUse name symtab
(eio, symtab, Index(name, e', t, pos))
| Let (Dec (name, e, decpos), body, pos) ->
(* Task 3, Hints for the `Let` case:
@ -144,7 +137,16 @@ let rec removeDeadBindingsInExp (e : TypedExp) : (bool * DBRtab * TypedExp) =
Let-expression.
*)
failwith "Unimplemented removeDeadBindingsInExp for Let"
let (eio, esymtab, e') = removeDeadBindingsInExp e
let (bodyio, bodysymtab, body') = removeDeadBindingsInExp body
if ((isUsed name bodysymtab) || eio) then
let io = eio || bodyio
let symtab = SymTab.combine esymtab bodysymtab
let exp = Let (Dec (name, e', decpos), body', pos)
(io, symtab, exp)
else
(bodyio, bodysymtab, body')
| Iota (e, pos) ->
let (io, uses, e') = removeDeadBindingsInExp e
(io,