Advertisement
VladNitu

2020VianExam

Apr 9th, 2023
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.45 KB | None | 0 0
  1. import Library._
  2.  
  3. object Desugar {
  4.  
  5.   def desugarProg(p: Prog): ExprC = p match {
  6.     case ProgExt(ds, e) =>
  7.       createDefs(ds, updateDefs(ds, desugar(e)))
  8.   }
  9.  
  10.   // helper
  11.   def getFunctionNames(ds: List[DefExt]): List[String] = ds match {
  12.     case DefineExt(f, xs, expr) :: t => f :: getFunctionNames(t)
  13.     case _ => Nil
  14.   }
  15.  
  16.   def createDefs(ds: List[DefExt], e: ExprC): ExprC = {
  17.     val fNames = getFunctionNames(ds)
  18.     AppC(FdC(fNames, e), fNames.map(_ => UninitializedC()))
  19.   }
  20.  
  21.   def updateDefs(ds: List[DefExt], e: ExprC): ExprC = ds match {
  22.     case DefineExt(f, xs, expr) :: t => SeqC(SetC(f, FdC(xs, desugar(expr))), updateDefs(t, e))
  23.     case _ => e
  24.    
  25.   }
  26.    
  27.  
  28.   def desugar(e: ExprExt): ExprC = e match {
  29.     case NumExt(n) => NumC(n)
  30.     case PlusExt(e1, e2) => PlusC(desugar(e1), desugar(e2))
  31.     case MinusExt(e1, e2) => MinusC(desugar(e1), desugar(e2))
  32.     case MultExt(e1, e2) => MultC(desugar(e1), desugar(e2))
  33.     case LtExt(e1, e2) => LtC(desugar(e1), desugar(e2))
  34.     case IfExt(e, e1, e2) => IfC(desugar(e), desugar(e1), desugar(e2))
  35.     case TrueExt() => TrueC()
  36.     case FalseExt() => FalseC()
  37.     case FdExt(xs, e) => FdC(xs, desugar(e))
  38.     case AppExt(e, es) => AppC(desugar(e), es.map(desugar))
  39.     case IdExt(x) => IdC(x)
  40.     case SetExt(x, e) => SetC(x, desugar(e))
  41.     case SeqExt(e1, e2) => SeqC(desugar(e1), desugar(e2))
  42.   }
  43.  
  44. }
  45.  
  46.  
  47. // DO NOT EDIT BELOW THIS LINE
  48.  
  49. sealed abstract class Prog
  50. case class ProgExt(ds: List[DefExt], e: ExprExt)       extends Prog
  51.  
  52. sealed abstract class DefExt
  53. case class DefineExt(f: String, xs: List[String], e: ExprExt) extends DefExt
  54.  
  55. sealed abstract class ExprExt
  56. case class NumExt(n: Int)                              extends ExprExt
  57. case class PlusExt(e1: ExprExt, e2: ExprExt)           extends ExprExt
  58. case class MinusExt(e1: ExprExt, e2: ExprExt)          extends ExprExt
  59. case class MultExt(e1: ExprExt, e2: ExprExt)           extends ExprExt
  60. case class LtExt(e1: ExprExt, e2: ExprExt)             extends ExprExt
  61. case class IfExt(e: ExprExt, e1: ExprExt, e2: ExprExt) extends ExprExt
  62. case class TrueExt()                                   extends ExprExt
  63. case class FalseExt()                                  extends ExprExt
  64. case class FdExt(xs: List[String], e: ExprExt)         extends ExprExt
  65. case class AppExt(e: ExprExt, es: List[ExprExt])       extends ExprExt
  66. case class IdExt(x: String)                            extends ExprExt
  67. case class SetExt(x: String, e: ExprExt)               extends ExprExt
  68. case class SeqExt(e1: ExprExt, e2: ExprExt)            extends ExprExt
  69.  
  70. sealed abstract class ExprC
  71. case class NumC(n: Int)                        extends ExprC
  72. case class PlusC(e1: ExprC, e2: ExprC)         extends ExprC
  73. case class MinusC(e1: ExprC, e2: ExprC)        extends ExprC
  74. case class MultC(e1: ExprC, e2: ExprC)         extends ExprC
  75. case class LtC(e1: ExprC, e2: ExprC)           extends ExprC
  76. case class IfC(e: ExprC, e1: ExprC, e2: ExprC) extends ExprC
  77. case class TrueC()                             extends ExprC
  78. case class FalseC()                            extends ExprC
  79. case class FdC(xs: List[String], e: ExprC)     extends ExprC
  80. case class AppC(e: ExprC, es: List[ExprC])     extends ExprC
  81. case class IdC(x: String)                      extends ExprC
  82. case class SetC(x: String, e: ExprC)           extends ExprC
  83. case class SeqC(e1: ExprC, e2: ExprC)          extends ExprC
  84. case class UninitializedC()                    extends ExprC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement