This commit is contained in:
NikolajDanger
2022-06-01 14:28:43 +02:00
parent 3cce70432d
commit bb45db22e0
7 changed files with 350 additions and 268 deletions

View File

@ -28,33 +28,46 @@ let rec copyConstPropFoldExp (vtable : VarTable)
exists and if so, it should replace the current expression
with the variable or constant to be propagated.
*)
failwith "Unimplemented copyConstPropFold for Var"
let name' = SymTab.lookup name vtable
match name' with
| Some (ConstProp x) -> Constant (x, pos)
| Some (VarProp x) -> Var (x, pos)
| _ -> Var (name, pos)
| Index (name, e, t, pos) ->
(* TODO project task 3:
Should probably do the same as the `Var` case, for
the array name, and optimize the index expression `e` as well.
*)
failwith "Unimplemented copyConstPropFold for Index"
let name' = SymTab.lookup name vtable
let e' = copyConstPropFoldExp vtable e
match name' with
| Some (VarProp x) -> Index (x, e', t, pos)
| _ -> Index (name, e', t, pos)
| Let (Dec (name, e, decpos), body, pos) ->
let e' = copyConstPropFoldExp vtable e
match e' with
| Var (_, _) ->
| Var (name', pos') ->
(* TODO project task 3:
Hint: I have discovered a variable-copy statement `let x = a`.
I should probably record it in the `vtable` by
associating `x` with a variable-propagatee binding,
and optimize the `body` of the let.
*)
failwith "Unimplemented copyConstPropFold for Let with Var"
| Constant (_, _) ->
let vtable' = SymTab.bind name (VarProp name') vtable
let body' = copyConstPropFoldExp vtable' body
Let (Dec (name, e', decpos), body', pos)
| Constant (constval, pos') ->
(* TODO project task 3:
Hint: I have discovered a constant-copy statement `let x = 5`.
I should probably record it in the `vtable` by
associating `x` with a constant-propagatee binding,
and optimize the `body` of the let.
*)
failwith "Unimplemented copyConstPropFold for Let with Constant"
| Let (_, _, _) ->
let vtable' = SymTab.bind name (ConstProp constval) vtable
let body' = copyConstPropFoldExp vtable' body
Let (Dec (name, e', decpos), body', pos)
| Let (Dec (name', e'', decpos'), body', pos') ->
(* TODO project task 3:
Hint: this has the structure
`let y = (let x = e1 in e2) in e3`
@ -66,23 +79,24 @@ let rec copyConstPropFoldExp (vtable : VarTable)
restructured, semantically-equivalent expression:
`let x = e1 in let y = e2 in e3`
*)
failwith "Unimplemented copyConstPropFold for Let with Let"
let let' = Let (Dec (name, body', decpos), body, pos)
copyConstPropFoldExp vtable (Let (Dec (name', e'', decpos'), let', pos'))
| _ -> (* Fallthrough - for everything else, do nothing *)
let body' = copyConstPropFoldExp vtable body
Let (Dec (name, e', decpos), body', pos)
| Times (_, _, _) ->
| Times (e1, e2, pos) ->
(* TODO project task 3: implement as many safe algebraic
simplifications as you can think of. You may inspire
yourself from the case of `Plus`. For example:
1 * x = ?
x * 0 = ?
*)
failwith "Unimplemented copyConstPropFold for multiplication"
Times (e1, e2, pos)
| And (e1, e2, pos) ->
(* TODO project task 3: see above. You may inspire yourself from
`Or` below, but that only scratches the surface of what's possible *)
failwith "Unimplemented copyConstPropFold for &&"
And (e1, e2, pos)
| Constant (x,pos) -> Constant (x,pos)
| StringLit (x,pos) -> StringLit (x,pos)
| ArrayLit (es, t, pos) ->