VladNitu

SubstResit20-21Nistor

May 25th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.44 KB | None | 0 0
  1. import Library._
  2.  
  3. object Substitution {
  4.  
  5.   def subst(e: ExprC, nv: List[Bind]): ExprC = e match {
  6.     case NumC(n) => NumC(n)
  7.     case PlusC(l, r) => PlusC(subst(l, nv), subst(r, nv))
  8.     case MultC(l, r) => MultC(subst(l, nv), subst(r, nv))
  9.     case MinusC(l, r) => MinusC(subst(l, nv), subst(r, nv))
  10.     case TrueC() => TrueC()
  11.     case FalseC() => FalseC()
  12.     case EqNumC(l, r) => EqNumC(subst(l, nv), subst(r, nv))
  13.    
  14.     // Implement the constructs below this line
  15.    
  16.     case IfC(c, t, e) =>  
  17.       IfC(subst(c, nv), subst(t, nv), subst(e, nv))
  18.     case FdC(param, body) =>
  19.       val substNv = nv.filter(bind => bind.x != param)
  20.       FdC(param, subst(body, substNv))
  21.     case LetC(x, e, body) =>
  22.       val substNv = nv.filter(bind => bind.x != x)
  23.       LetC(x, subst(e, nv), subst(body, substNv))
  24.     case AppC(l, r) =>
  25.       AppC(subst(l, nv), subst(r, nv))
  26.     case IdC(x) => nv.find(bind => bind.x == x) match {
  27.       case Some(Bind(_, v)) => ValC(v)
  28.       case None => IdC(x)
  29.     }
  30.     case MsgC(e, method, args) =>
  31.       MsgC(subst(e, nv), method, args.map(arg => subst(arg, nv)))
  32.     case ObjectC(fields, methods) =>
  33.       ObjectC(
  34.         fields.map(field => FieldC(field.name, subst(field.expr, nv))),
  35.         methods.map(meth => MethodC(meth.name, meth.params, subst(meth.body, nv.filter(bind => !(fields.map(_.name) ::: meth.params).contains(bind.x)))))
  36.         )
  37.     case ValC(v) => ValC(v)
  38.   }
  39.  
  40. }
  41. // import Library._
  42. //
  43. // object Substitution {
  44. //  
  45. //   def subst(e: ExprC, nv: List[Bind]): ExprC = e match {
  46. //     case NumC(n) => NumC(n)
  47. //     case PlusC(l, r) => PlusC(subst(l, nv), subst(r, nv))
  48. //     case MultC(l, r) => MultC(subst(l, nv), subst(r, nv))
  49. //     case MinusC(l, r) => MinusC(subst(l, nv), subst(r, nv))
  50. //     case TrueC() => TrueC()
  51. //     case FalseC() => FalseC()
  52. //     case EqNumC(l, r) => EqNumC(subst(l, nv), subst(r, nv))
  53. //    
  54. //     // Implement the constructs below this line
  55. //    
  56.     // case IfC(c, t, e) =>
  57.     //   IfC(subst(c, nv), subst(t, nv), subst(e, nv))
  58.     // case FdC(param, body) =>
  59.     //   FdC(param, subst(body, nv.filter(bind => bind.x != param)))
  60.     // case LetC(x, e, body) =>
  61.     //   LetC(x, subst(e, nv), subst(body, nv.filter(bind => bind.x != x)))
  62. //    
  63.     // case AppC(l, r) =>
  64.     //   AppC(subst(l, nv), subst(r, nv))
  65.      
  66.     // case IdC(x) => nv.find(bind => bind.x == x) match {
  67.     //   case Some(Bind(_, vl)) => ValC(vl)
  68.     //   case None => IdC(x)
  69.     // }
  70.     // case MsgC(e, method, args) =>
  71.     //   MsgC(subst(e, nv), method, args.map(arg => subst(arg, nv)))
  72. //      
  73.     // case ObjectC(fields, methods) =>
  74.     //   val fieldNames = fields.map(_.name)
  75.     //   ObjectC(fields.map(field => FieldC(field.name, subst(field.expr, nv))), // Field initialization expressions have the same scope as the scope of the surrounding object language expression; i.e., no shadowing.
  76.     //   methods.map(method => MethodC(method.name, method.params, subst(method.body, nv.filter(bind => !(fieldNames ::: method.params).contains(bind.x))))))
  77.     // // For method bodies, the bindings from the surrounding scope of the object language expression may be shadowed by either a declared field name, or by formal parameters of the method.
  78.      
  79. //     case ValC(v) => ValC(v)
  80. //   }
  81. //  
  82. // }
  83. // // import Library._
  84. // //
  85. // // object Substitution {
  86. // //  
  87. // //   def mayBeShadowedBy(bind: Bind, fieldNames: List[String], params: List[String]): Boolean =  {
  88. // //     val shadow = fieldNames ::: params
  89. // //     if (!shadow.contains(bind.x)) true else false
  90. // //   }
  91. // //  
  92. // //   def subst(e: ExprC, nv: List[Bind]): ExprC = e match { // substitue with what cannot shadow
  93. // //     case NumC(n) => NumC(n)
  94. // //     case PlusC(l, r) => PlusC(subst(l, nv), subst(r, nv))
  95. // //     case MultC(l, r) => MultC(subst(l, nv), subst(r, nv))
  96. // //     case MinusC(l, r) => MinusC(subst(l, nv), subst(r, nv))
  97. // //     case TrueC() => TrueC()
  98. // //     case FalseC() => FalseC()
  99. // //     case EqNumC(l, r) => EqNumC(subst(l, nv), subst(r, nv))
  100. // //    
  101. // //     // Implement the constructs below this line
  102. // //    
  103. // //     case IfC(c, t, e) => IfC(subst(c, nv), subst(t, nv), subst(e, nv))
  104. // //    
  105. // //     case FdC(param, body) =>
  106. // //       val fdcNv = nv.filter(bind => bind.x != param) // `param` of lambda should not be shadowed
  107. // //       FdC(param, subst(body, fdcNv))
  108. // //      
  109. // //     case LetC(x, e, body) =>
  110. // //       val letcNv = nv.filter(bind => bind.x != x) // `x` defined in let should not be shadowed
  111. // //       LetC(x, subst(e, nv), subst(body, letcNv))
  112. // //      
  113. // //     case AppC(l, r) =>
  114. // //       AppC(subst(l, nv), subst(r, nv))
  115. // //      
  116.     // case IdC(x) => (nv.find(bind => bind.x == x)) match {
  117.     //   case Some(Bind(name, value)) => ValC(value)
  118.     //   case _ => IdC(x)
  119.     // }
  120. // //      
  121. // //     case MsgC(e, method, args) =>
  122. // //       MsgC(subst(e, nv), method, args.map(arg => subst(arg, nv)))
  123. // //      
  124. // //     case ObjectC(fields, methods) =>
  125. // //     val fieldNames = fields.map(field => field.name)
  126. // //     ObjectC(fields.map(field => FieldC(field.name, subst(field.expr, nv))),
  127. // //             methods.map(method =>
  128. // //               MethodC(method.name, method.params, subst(method.body, nv.filter(bind => mayBeShadowedBy(bind, fieldNames, method.params))))))
  129. // //      
  130. // //     case ValC(v) => ValC(v)
  131. // //   }
  132. // //  
  133. // // }
  134.  
Add Comment
Please, Sign In to add comment