Advertisement
VladNitu

desugarVariantaLab

Apr 6th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import Library._
  2.  
  3. object Desugar {
  4.  
  5.  
  6. def desugarList(es: List[ExprExt]): ExprC = es match {
  7. case h :: t => ConsC(desugar(h), desugarList(t))
  8. case h :: Nil => desugar(h)
  9. case _ => NilC()
  10. }
  11.  
  12. def makeList(rows: List[ExprC]): ExprC = rows match {
  13. case rowList :: t => ConsC(rowList, makeList(t))
  14. case Nil => NilC()
  15. }
  16.  
  17. def solveScalarMult(n: ExprC, m: ExprC): ExprC = m match {
  18. case ConsC(h, t) => ConsC(ListMapC(
  19. FdC("scalar-mult", MultC(n, IdC("scalar-mult"))), h)
  20. , solveScalarMult(n, t))
  21. case NilC() => NilC()
  22. }
  23.  
  24. def desugar(e: ExprExt): ExprC = e match {
  25. case NumExt(n) => NumC(n)
  26. case PlusExt(e1, e2) => PlusC(desugar(e1), desugar(e2))
  27. case MultExt(e1, e2) => MultC(desugar(e1), desugar(e2))
  28. case FdExt(x, e) => FdC(x, desugar(e))
  29. case AppExt(f, a) => AppC(desugar(f), desugar(a))
  30. case IdExt(x) => IdC(x)
  31. case ConsExt(h, t) => ConsC(desugar(h), desugar(t))
  32. case NilExt() => NilC()
  33.  
  34. case ListExt(es) =>
  35. desugarList(es)
  36.  
  37. case ListMapExt(f, l) =>
  38. ListMapC(desugar(f), desugar(l))
  39.  
  40. case MatrixExt(rs) =>
  41. val rows: List[ExprC] = rs.map(row => desugarList(row.es))
  42. makeList(rows)
  43.  
  44. case ScalarMultExt(n, m) => solveScalarMult(desugar(n), desugar(m))
  45. // KEY
  46. // println(desugar(m))
  47. // desugar(m) match {
  48. // case ConsC(h, t) => println(h)
  49. // }
  50. // ListMapC(
  51. // FdC("scalar-mult", MultC(desugar(n), IdC("scalar-mult"))), desugar(m)
  52. // )
  53.  
  54. // m match {
  55. // case MatrixExt(rs) =>
  56.  
  57. // desugar(n) match {
  58. // case num: NumC =>
  59. // val rows: List[ExprC] = rs.map(row => row match {
  60. // case RowExt(es) => desugarListMultiply(es, num)
  61. // })
  62. // //println(rows)
  63. // makeList(rows)
  64. // case _ => throw new RuntimeException(s"$n Does not evaluate to a number")
  65. // }
  66.  
  67. // case _ => throw new RuntimeException(s"$m Does not evaluate to a matrix")
  68.  
  69. // }
  70.  
  71.  
  72.  
  73. }
  74.  
  75. }
  76. //Expected ConsV(ConsV(NumV(3),ConsV(NumV(6),ConsV(NumV(9),NilV()))),ConsV(ConsV(NumV(12),ConsV(NumV(15),ConsV(NumV(18),NilV()))),NilV())),
  77. // but got ConsV(ConsV(NumV(3),ConsV(NumV(2),ConsV(NumV(3),NilV()))),ConsV(ConsV(NumV(12),ConsV(NumV(5),ConsV(NumV(6),NilV()))),NilV()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement