반응형
case LetRecExt(binds, body) => {
val newBody = binds.reverse.foldLeft(desugar(body)) {
case (accBody, bind) => SeqC(SetC(bind.name, desugar(bind.value)), accBody)
}
AppC(FdC(binds.map(_.name), newBody), binds.map(bind => UninitializedC()))
}
-----------------------------------------------------------------------------------
case IdC(c) => {
val loc = lookup(c, nv)
val value = fetch(loc, st1)
(value, st1)
}
case AppC(f, args) => interp(f, nv, st1) match {
case (PointerClosV(FdC(params, body), closureEnv), fst) => {
if (params.length != args.length) throw new InterpE("")
val (values, initialStore) = args.foldLeft((List[Value](), fst)) {
case ((v, st), arg) => {
val (value, updatedStore) = interp(arg, nv, st)
(v :+ value, updatedStore)
}
}
val (newEnv, finalStore) = createBind(params, values, closureEnv, initialStore)
interp(body, newEnv, finalStore)
}
case _ => throw new InterpE("")
}
case BoxC(v) => {
val (value, st2) = interp(v, nv, st1)
val loc = newLoc(st2)
val newStore = addCell(Cell(loc, value), st2)
(BoxV(loc), newStore)
}
case UnboxC(b) => interp(b, nv, st1) match {
case (BoxV(loc), st) => (fetch(loc, st), st)
case _ => throw new InterpE("")
}
case SetboxC(b, v) => interp(b, nv, st1) match {
case (BoxV(loc), st) => {
val (value, st2) = interp(v, nv, st)
val newStore = update(loc, value, st2)
(value, newStore)
}
case _ => throw new InterpE("")
}
case SetC(v, b) => {
val loc = lookup(v, nv)
val (value, st2) = interp(b, nv, st1)
val newStore = update(loc, value, st2)
(value, newStore)
}
case SeqC(b1, b2) => interp(b1, nv, st1) match {
case (v, st2) => interp(b2, nv, st2)
case _ => throw new InterpE("")
}
}
-----------------------------------------------------------
def createBind(names: List[String], values: List[Value], nv: PointerEnvironment, st: Store) : (PointerEnvironment, Store) = (names, values) match {
case (n :: ns, v :: vs) => {
val loc = newLoc(st)
val newEnv = Pointer(n, loc) :: nv
val updatedStore = addCell(Cell(loc, v), st)
createBind(ns, vs, newEnv, updatedStore)
}
case (Nil, Nil) => (nv, st)
case _ => throw new InterpE("")
}
반응형
'학교 > CPL' 카테고리의 다른 글
Lecture 11: Type Checking (1) | 2024.04.06 |
---|---|
Lecture 9-10: Parallel and Concurrent (0) | 2024.04.06 |
Lecture 7-8: Mutation and State (0) | 2024.04.01 |
CPL 3: Function Interpretation and Environments (0) | 2024.03.31 |
Lecture 6: Environments and Scoping (0) | 2024.03.29 |
댓글