Advertisement
VladNitu

Untitled

Apr 5th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 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 _ => NilC()
  9. }
  10.  
  11. def makeList(rows: List[ExprC]): ExprC = rows match {
  12. case rowList :: t => ConsC(rowList, makeList(t))
  13. case Nil => NilC()
  14. }
  15.  
  16.  
  17. def desugarListMultiply(matrixC: ExprC, number: ExprC): ExprC = matrixC match {
  18. case ConsC(h, t) => ConsC(MultC(h, number), desugarListMultiply(t, number))
  19. case _ => NilC()
  20. }
  21.  
  22. def desugar(e: ExprExt): ExprC = e match {
  23. case NumExt(n) => NumC(n)
  24. case PlusExt(e1, e2) => PlusC(desugar(e1), desugar(e2))
  25. case MultExt(e1, e2) => MultC(desugar(e1), desugar(e2))
  26. case FdExt(x, e) => FdC(x, desugar(e))
  27. case AppExt(f, a) => AppC(desugar(f), desugar(a))
  28. case IdExt(x) => IdC(x)
  29. case ConsExt(h, t) => ConsC(desugar(h), desugar(t))
  30. case NilExt() => NilC()
  31.  
  32. case ListExt(es) =>
  33. desugarList(es)
  34.  
  35. case ListMapExt(f, l) =>
  36. ListMapC(desugar(f), desugar(l))
  37.  
  38. case MatrixExt(rs) =>
  39. val rows: List[ExprC] = rs.map(row => row match {
  40. case RowExt(es) => desugarList(es)
  41. })
  42. makeList(rows)
  43.  
  44.  
  45. case ScalarMultExt(n, m) => ???
  46.  
  47.  
  48.  
  49. }
  50.  
  51. }
  52. //Expected ConsV(ConsV(NumV(3),ConsV(NumV(6),ConsV(NumV(9),NilV()))),ConsV(ConsV(NumV(12),ConsV(NumV(15),ConsV(NumV(18),NilV()))),NilV())),
  53. // 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