Advertisement
VladNitu

3 exam2017-2018 resit cpk

Apr 2nd, 2023
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.63 KB | None | 0 0
  1. class Interp {
  2.  
  3.  
  4.   def lookup(x: String, nv: List[Bind]): Result = nv match {
  5.     case h :: t => if (x == h.x) Ok(h.v) else lookup(x, t)
  6.     case Nil => throw new InterpException(s"$x was not found in the environment") // Undef. behavior
  7.   }
  8.  
  9.   def interp(e: ExprC, nv: List[Bind]): Result = e match {
  10.     case NumC(n) =>
  11.       Ok(NumV(n))
  12.     case PlusC(l, r) =>
  13.       interp(l, nv) match {
  14.         case Ok(NumV(nl)) =>
  15.           interp(r, nv) match {
  16.             case Ok(NumV(nr)) =>
  17.               Ok(NumV(nl + nr))
  18.             case Ok(v) =>
  19.               throw InterpException("expected number, but found: " + v)
  20.             case Exc() => Exc()
  21.           }
  22.         case Ok(v) =>
  23.           throw InterpException("expected number, but found: " + v)
  24.         case Exc() => Exc()
  25.       }
  26.  
  27.     case RaiseC() =>
  28.       Exc()
  29.      
  30.     case HandleC(e, h) => interp(e, nv) match {
  31.       case Ok(v) => Ok(v)
  32.       case _ => interp(h, nv)
  33.     }
  34.      
  35.     case IdC(x) =>
  36.       lookup(x, nv)
  37.      
  38.     case fd@FdC(x, body) =>
  39.         Ok(ClosV(fd, nv))
  40.      
  41.     case AppC(f, a) => interp(f, nv) match {
  42.        
  43.         case Ok(ClosV(FdC(x, body), fenv)) => interp(a, nv) match {
  44.           case Ok(argV) => interp(body, Bind(x, argV) :: fenv)
  45.           case Exc() => Exc()
  46.         }
  47.         case Exc() =>  Exc() // throw new InterpException(s"$f not a closure")
  48.       }
  49.     case LetC(x, e1, e2) => interp(e1, nv) match {
  50.       case Ok(v) =>  
  51.           val bind = Bind(x, v)
  52.           val newEnv = bind :: nv
  53.           interp(e2, newEnv)
  54.  
  55.      case Exc() => Exc()
  56.       }
  57.   }
  58.  
  59. }
  60.  
  61. // DO NOT EDIT THESE DEFINITIONS
  62.  
  63. sealed abstract class ExprC
  64. case class NumC(n: Int)                           extends ExprC // n
  65. case class PlusC(l: ExprC, r: ExprC)              extends ExprC // (+ e1 e2)
  66. case class IdC  (x: String)                       extends ExprC // x
  67. case class FdC  (arg: String, body: ExprC)        extends ExprC // (lambda (x) e)
  68. case class AppC (f: ExprC, a: ExprC)              extends ExprC // (f a)
  69. case class LetC (x: String, e1: ExprC, e2: ExprC) extends ExprC // (let (x e) e)
  70. case class RaiseC()                               extends ExprC // (raise)
  71. case class HandleC(e: ExprC, h: ExprC)            extends ExprC // (handle e e)
  72.  
  73. sealed abstract class Value
  74. case class NumV(n: Int)                    extends Value
  75. case class ClosV(fdc: FdC, nv: List[Bind]) extends Value
  76.  
  77. sealed abstract class Result
  78. case class Ok(v: Value) extends Result
  79. case class Exc() extends Result
  80.  
  81. case class Bind(x: String, v: Value)
  82.  
  83. case class NotImplementedException() extends RuntimeException("not implemented")
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement