Advertisement
VladNitu

InterpFunctionsResit20-21Nistor

May 25th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.72 KB | None | 0 0
  1. import Library._
  2.  
  3. object Interp {
  4.  
  5. def interp(e: ExprC, nv: List[Bind]): Value = e match {
  6. case NumC(n) => NumV(n)
  7. case PlusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  8. case (NumV(n1), NumV(n2)) => NumV(n1 + n2)
  9. case p => throw InterpException("Bad addition. Expected two numbers, but got: " + p)
  10. }
  11. case MultC(l, r) => (interp(l, nv), interp(r, nv)) match {
  12. case (NumV(n1), NumV(n2)) => NumV(n1 * n2)
  13. case p => throw InterpException("Bad multiplication. Expected two numbers, but got: " + p)
  14. }
  15. case MinusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  16. case (NumV(n1), NumV(n2)) => NumV(n1 - n2)
  17. case p => throw InterpException("Bad subtraction. Expected two numbers, but got: " + p)
  18. }
  19. case TrueC() => BoolV(true)
  20. case FalseC() => BoolV(false)
  21. case EqNumC(l, r) => (interp(l, nv), interp(r, nv)) match {
  22. case (NumV(n1), NumV(n2)) => BoolV(n1 == n2)
  23. case p => throw InterpException("Bad number equality. Expected two numbers, but got: " + p)
  24. }
  25. case IfC(c, t, e) => interp(c, nv) match {
  26. case BoolV(b) => b match {
  27. case true => interp(t, nv)
  28. case false => interp(e, nv)
  29. }
  30. case _ => throw InterpException("If err")
  31. }
  32. case AppC(f, arg) => interp(f, nv) match {
  33. case ClosV(param, body, nv_clos) =>
  34. val argInterp = interp(arg, nv)
  35. interp(body, Bind(param, argInterp) :: nv_clos)
  36.  
  37. case _ => throw InterpException("AppC err")
  38. }
  39. case IdC(x) => nv.find(bd => bd.x == x) match {
  40. case Some(Bind(_, vl)) => vl
  41. case _ => {
  42. println(nv)
  43. throw InterpException("x not found in nv")
  44. }
  45. }
  46. case FunctionsC(funs, cont) =>
  47. val closures = funs.map(fun => ClosV(fun.param, fun.body, Nil))
  48. val funs_clos = funs.zip(closures).map(fun_clos => Bind(fun_clos._1.f, fun_clos._2))
  49. var largeNv = nv
  50. funs_clos.foreach(fun_clos => largeNv = fun_clos :: largeNv)
  51.  
  52. closures.foreach(clos => clos.nv = largeNv)
  53. interp(cont, funs_clos ::: nv)
  54.  
  55.  
  56. }
  57. }
  58.  
  59. // import Library._
  60. //
  61. // object Interp {
  62. //
  63. // // def interpFun(fun: FunC, nv: List[Bind]): ClosV = {
  64. //
  65. //
  66. //
  67. // // }
  68. //
  69. // def interp(e: ExprC, nv: List[Bind]): Value = e match {
  70. // case NumC(n) => NumV(n)
  71. // case PlusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  72. // case (NumV(n1), NumV(n2)) => NumV(n1 + n2)
  73. // case p => throw InterpException("Bad addition. Expected two numbers, but got: " + p)
  74. // }
  75. // case MultC(l, r) => (interp(l, nv), interp(r, nv)) match {
  76. // case (NumV(n1), NumV(n2)) => NumV(n1 * n2)
  77. // case p => throw InterpException("Bad multiplication. Expected two numbers, but got: " + p)
  78. // }
  79. // case MinusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  80. // case (NumV(n1), NumV(n2)) => NumV(n1 - n2)
  81. // case p => throw InterpException("Bad subtraction. Expected two numbers, but got: " + p)
  82. // }
  83. // case TrueC() => BoolV(true)
  84. // case FalseC() => BoolV(false)
  85. // case EqNumC(l, r) => (interp(l, nv), interp(r, nv)) match {
  86. // case (NumV(n1), NumV(n2)) => BoolV(n1 == n2)
  87. // case p => throw InterpException("Bad number equality. Expected two numbers, but got: " + p)
  88. // }
  89. // case IfC(c, t, e) => interp(c, nv) match {
  90. // case BoolV(true) => interp(t, nv)
  91. // case BoolV(false) => interp(e, nv)
  92. // case _ => throw InterpException(s"$c does not evaluate to Boolean")
  93. // }
  94. // case AppC(f, arg) => interp(f, nv) match {
  95. // case ClosV(param, body, nv_clos) =>
  96. // val interpArg = interp(arg, nv)
  97. // interp(body, Bind(param, interpArg) :: nv_clos)
  98. //
  99. // case _ => throw throw InterpException(s"$f does not eval. to ClosV")
  100. // }
  101. //
  102. // case IdC(x) => nv.find(bnd => bnd.x == x) match {
  103. // case Some(Bind(_, v)) => v
  104. // case _ => throw throw InterpException(s"$x not present in $nv")
  105. // }
  106. // case FunctionsC(funs, cont) =>
  107. //
  108. // var newNv = nv
  109. // val closures = funs.map(fun => ClosV(fun.param, fun.body, Nil))
  110. //
  111. // val funs_clos = funs.map(_.f).zip(closures).map(fun_clos => Bind(fun_clos._1, fun_clos._2)) // (fName, Clos)
  112. //
  113. // funs_clos.foreach(fun_clos => newNv = fun_clos :: newNv)
  114. //
  115. // closures.foreach(clos => clos.nv = newNv)
  116. //
  117. // interp(cont, newNv)
  118. //
  119. //
  120. //
  121. // // var newNv = nv
  122. // // val closures = funs.map(fun => ClosV(fun.param, fun.body, Nil))
  123. // // val functionBinds = funs.map(_.f).zip(closures).map(fun_clos => Bind(fun_clos._1, fun_clos._2))
  124. //
  125. // // // println(functionBinds)
  126. // // // functionBinds.foreach(x => print(x.v))
  127. // // // functionBinds.foreach(x => print(x.v match {
  128. // // // case ClosV(_, _, nv) => nv
  129. // // // }))
  130. //
  131. // // closures.foreach(clos => clos.nv = functionBinds ::: nv)
  132. //
  133. // // interp(cont, functionBinds ::: nv)
  134. //
  135. // }
  136. // }
  137. //
  138. // // import Library._
  139. // //
  140. // // object Interp {
  141. // //
  142. // //
  143. // // def bindFun(fun: FuncC, curNv: List[Bind]): ClosV = {
  144. // // val newNv = Bind(fun.f, ClosV(fun.param, fun.body, curNv)) :: curNv
  145. // // ClosV(fun.f, fun.body, newNv)
  146. // // }
  147. // //
  148. // // def interp(e: ExprC, nv: List[Bind]): Value = e match {
  149. // // case NumC(n) => NumV(n)
  150. // // case PlusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  151. // // case (NumV(n1), NumV(n2)) => NumV(n1 + n2)
  152. // // case p => throw InterpException("Bad addition. Expected two numbers, but got: " + p)
  153. // // }
  154. // //
  155. // // case MultC(l, r) => (interp(l, nv), interp(r, nv)) match {
  156. // // case (NumV(n1), NumV(n2)) => NumV(n1 * n2)
  157. // // case p => throw InterpException("Bad multiplication. Expected two numbers, but got: " + p)
  158. // // }
  159. // // case MinusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  160. // // case (NumV(n1), NumV(n2)) => NumV(n1 - n2)
  161. // // case p => throw InterpException("Bad subtraction. Expected two numbers, but got: " + p)
  162. // // }
  163. // // case TrueC() => BoolV(true)
  164. // // case FalseC() => BoolV(false)
  165. // // case EqNumC(l, r) => (interp(l, nv), interp(r, nv)) match {
  166. // // case (NumV(n1), NumV(n2)) => BoolV(n1 == n2)
  167. // // case p => throw InterpException("Bad number equality. Expected two numbers, but got: " + p)
  168. // // }
  169. // // case IfC(c, t, e) => interp(c, nv) match {
  170. // // case BoolV(true) => interp(t, nv)
  171. // // case BoolV(false) => interp(e, nv)
  172. // // case _ => throw InterpException(s"IF: $c not a Boolean")
  173. // // }
  174. // // case AppC(f, arg) => interp(f, nv) match {
  175. // // case ClosV(x, body, nv_clos) => {
  176. // // val argInterp = interp(arg, nv)
  177. // // val newNv = Bind(x, argInterp) :: nv_clos
  178. // //
  179. // // interp(body, newNv ::: nv)
  180. // //
  181. // // }
  182. // // case _ =>throw InterpException(s"AppC: $f does evaluate to a function")
  183. // // }
  184. // // case IdC(x) => nv.find(bind => bind. x == x) match {
  185. // // case Some(Bind(_, v)) => v
  186. // // case None => throw InterpException(s"$x not found in nv")
  187. // // }
  188. // // case FunctionsC(funs, cont) =>
  189. // // var curNv = nv
  190. // // funs.map(fun => {
  191. // // val bindsFun: ClosV = bindFun(fun, curNv)
  192. // // curNv = bindsFun.nv
  193. // //
  194. // // })
  195. // //
  196. // // interp(cont, curNv)
  197. // // }
  198. // // }
  199. // //
  200. // // // import Library._
  201. // // //
  202. // // // object Interp {
  203. // // //
  204. // // // def lookup(x: String, nv: List[Bind]): Value = nv match {
  205. // // // case bind :: t => if (bind.x == x) bind.v else lookup(x, t)
  206. // // // case _ => throw InterpException(s"$x not found in nv_lookup")
  207. // // // }
  208. // // //
  209. // // // def bindFunction(fun: FuncC, nv: List[Bind]): ClosV = {
  210. // // //
  211. // // // val bind = Bind(fun.f, ClosV(fun.param, fun.body, nv))
  212. // // // var newEnv = bind :: nv
  213. // // // ClosV(fun.f, fun.body, newEnv)
  214. // // // }
  215. // // //
  216. // // // def interp(e: ExprC, nv: List[Bind]): Value = e match {
  217. // // // case NumC(n) => NumV(n)
  218. // // // case PlusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  219. // // // case (NumV(n1), NumV(n2)) => NumV(n1 + n2)
  220. // // // case p => throw InterpException("Bad addition. Expected two numbers, but got: " + p)
  221. // // // }
  222. // // // case MultC(l, r) => (interp(l, nv), interp(r, nv)) match {
  223. // // // case (NumV(n1), NumV(n2)) => NumV(n1 * n2)
  224. // // // case p => throw InterpException("Bad multiplication. Expected two numbers, but got: " + p)
  225. // // // }
  226. // // // case MinusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  227. // // // case (NumV(n1), NumV(n2)) => NumV(n1 - n2)
  228. // // // case p => throw InterpException("Bad subtraction. Expected two numbers, but got: " + p)
  229. // // // }
  230. // // // case TrueC() => BoolV(true)
  231. // // // case FalseC() => BoolV(false)
  232. // // // case EqNumC(l, r) => (interp(l, nv), interp(r, nv)) match {
  233. // // // case (NumV(n1), NumV(n2)) => BoolV(n1 == n2)
  234. // // // case p => throw InterpException("Bad number equality. Expected two numbers, but got: " + p)
  235. // // // }
  236. // // // case IfC(c, t, e) => interp(c, nv) match {
  237. // // // case BoolV(b) => if (b) interp(t, nv) else interp(e, nv)
  238. // // // case _ => throw InterpException(s"$c not Bool")
  239. // // // }
  240. // //
  241. // // //
  242. // // // case AppC(f, arg) => interp(f, nv) match {
  243. // // // case ClosV(x, e, nv_clos) =>
  244. // // //
  245. // // // val interpArg = interp(arg, nv)
  246. // // // val binds = Bind(x, interpArg) :: nv_clos
  247. // // // interp(e, binds ::: nv)
  248. // // // // val params_nv = Bind(x, interp(e, nv_clos)) :: nv_clos
  249. // // // // val interpArg = interp(arg, nv)
  250. // // // // interp(e, )
  251. // // // case _ => throw InterpException(s"$f is not a ClosV")
  252. // // //
  253. // // // }
  254. // // // case IdC(x) => lookup(x, nv)
  255. // // // case FunctionsC(funs, cont) =>
  256. // // // var nvGl: List[Bind] = nv
  257. // // // funs.map(fun => {
  258. // // // val closv: ClosV = bindFunction(fun, nvGl)
  259. // // // nvGl = closv.nv
  260. // // // }
  261. // // // )
  262. // // //
  263. // // // interp(cont, nvGl)
  264. // // // }
  265. // // // }
  266. // // //
  267. // // // // import Library._
  268. // // // //
  269. // // // // object Interp {
  270. // // // //
  271. // // // // def lookup(x: String, nv: List[Bind]): Value = nv match {
  272. // // // // case Bind(name, value) :: t => if (name == x) value else lookup(x, t)
  273. // // // // case Nil => throw InterpException(s"$x was not found in env. $nv")
  274. // // // // }
  275. // // // //
  276. // // // // def bindFunction(fun: FuncC, nv: List[Bind]): ClosV = {
  277. // // // // val closBind = Bind(fun.f, ClosV(fun.param, fun.body, nv))
  278. // // // // var newEnv = closBind :: nv
  279. // // // //
  280. // // // // newEnv(0).v match {
  281. // // // // case ClosV(_, _, nvOld) => ClosV(_, _, newEnv)
  282. // // // // case _ => throw new InterpException("")
  283. // // // // }
  284. // // // //
  285. // // // // ClosV(fun.f, fun.body, newEnv)
  286. // // // // }
  287. // // // //
  288. // // // //
  289. // // // // def interp(e: ExprC, nv: List[Bind]): Value = e match {
  290. // // // // case NumC(n) => NumV(n)
  291. // // // // case PlusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  292. // // // // case (NumV(n1), NumV(n2)) => NumV(n1 + n2)
  293. // // // // case p => throw InterpException("Bad addition. Expected two numbers, but got: " + p)
  294. // // // // }
  295. // // // // case MultC(l, r) => (interp(l, nv), interp(r, nv)) match {
  296. // // // // case (NumV(n1), NumV(n2)) => NumV(n1 * n2)
  297. // // // // case p => throw InterpException("Bad multiplication. Expected two numbers, but got: " + p)
  298. // // // // }
  299. // // // // case MinusC(l, r) => (interp(l, nv), interp(r, nv)) match {
  300. // // // // case (NumV(n1), NumV(n2)) => NumV(n1 - n2)
  301. // // // // case p => throw InterpException("Bad subtraction. Expected two numbers, but got: " + p)
  302. // // // // }
  303. // // // // case TrueC() => BoolV(true)
  304. // // // // case FalseC() => BoolV(false)
  305. // // // // case EqNumC(l, r) => (interp(l, nv), interp(r, nv)) match {
  306. // // // // case (NumV(n1), NumV(n2)) => BoolV(n1 == n2)
  307. // // // // case p => throw InterpException("Bad number equality. Expected two numbers, but got: " + p)
  308. // // // // }
  309. // // // // case IfC(c, t, e) => interp(c, nv) match {
  310. // // // // case BoolV(b) => if (b) interp(t, nv) else interp(e, nv)
  311. // // // // case _ => throw new InterpException("Should not reach this")
  312. // // // // }
  313. // // // // case AppC(f, arg) => interp(f, nv) match {
  314. // // // // case ClosV(x, body, nv_clos) =>
  315. // // // // val argInterp = interp(arg, nv)
  316. // // // // val newNv = Bind(x, argInterp) :: nv_clos ::: nv
  317. // // // // interp(body, newNv)
  318. // // // //
  319. // // // //
  320. // // // // case _ => throw new InterpException(s"$f is not a Closure")
  321. // // // // }
  322. // // // // case IdC(x) => lookup(x, nv)
  323. // // // //
  324. // // // // case FunctionsC(funs, cont) => {
  325. // // // // var lastNv = nv;
  326. // // // // val ans = funs.map(fun => {
  327. // // // // val closv: ClosV = bindFunction(fun, lastNv)
  328. // // // // lastNv = closv.nv
  329. // // // // })
  330. // // // //
  331. // // // // interp(cont, lastNv)
  332. // // // // }
  333. // // // //
  334. // // // // }
  335. // // // // }
  336. // // // //
  337.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement