Advertisement
VladNitu

Untitled

Apr 5th, 2023
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.99 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.   def desugarListMultiply(es: List[ExprExt], number: ExprC): ExprC = es match {
  17.     case h :: t => ConsC(MultC(desugar(h), number), desugarListMultiply(t, number))
  18.     case _ => NilC()
  19.   }
  20.  
  21.   def desugar(e: ExprExt): ExprC = e match {
  22.     case NumExt(n)           => NumC(n)
  23.     case PlusExt(e1, e2)     => PlusC(desugar(e1), desugar(e2))
  24.     case MultExt(e1, e2)     => MultC(desugar(e1), desugar(e2))
  25.     case FdExt(x, e)         => FdC(x, desugar(e))
  26.     case AppExt(f, a)        => AppC(desugar(f), desugar(a))
  27.     case IdExt(x)            => IdC(x)
  28.     case ConsExt(h, t)       => ConsC(desugar(h), desugar(t))
  29.     case NilExt()            => NilC()
  30.    
  31.     case ListExt(es) =>
  32.       desugarList(es)
  33.      
  34.     case ListMapExt(f, l) =>
  35.       ListMapC(desugar(f), desugar(l))
  36.    
  37.     case MatrixExt(rs) =>
  38.       val rows: List[ExprC] = rs.map(row => row match {
  39.           case RowExt(es) => desugarList(es)
  40.         })
  41.       makeList(rows)
  42.        
  43.        
  44.     case ScalarMultExt(n, m) => (desugar(n), m) match {
  45.      
  46.       case (number@NumC(x), MatrixExt(rs)) =>
  47.         val rows: List[ExprC] = rs.map(row => row match {
  48.           case RowExt(es) => desugarListMultiply(es, number)
  49.         })
  50.         println(rows)
  51.         makeList(rows)
  52.        
  53.       case _ => throw new RuntimeException(s"($n, $m) Does not evaluate to int - matrix")
  54.      
  55.     }
  56.  
  57.      
  58.   }
  59.  
  60. }
  61. //Expected ConsV(ConsV(NumV(3),ConsV(NumV(6),ConsV(NumV(9),NilV()))),ConsV(ConsV(NumV(12),ConsV(NumV(15),ConsV(NumV(18),NilV()))),NilV())),
  62. // 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