LightningIsMyName

[Workshop] AST.scala

Apr 23rd, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.55 KB | None | 0 0
  1. object Indented {
  2.   var space = ""
  3. }
  4. class Indenter {
  5.   val result = new StringBuffer()
  6.   def doIndent() = { Indented.space = Indented.space + "  " }
  7.   def unIndent() = { Indented.space = Indented.space.substring(2) }
  8.   def iprintln(a:Any) = result append (Indented.space + a + "\n")
  9.   def println(a:Any) = result append (a + "\n")
  10.   override def toString = result.toString
  11. }
  12.  
  13. object TextStyle extends Enumeration {
  14.   type TextStyle = Value;
  15.   val bold, italic, regular = Value;
  16. }
  17.  
  18. object HAlign extends Enumeration {
  19.   type HAlign = Value;
  20.   val left, center, right = Value;
  21. }
  22.  
  23. object VAlign extends Enumeration {
  24.   type VAlign = Value;
  25.   val top, middle, bottom = Value;
  26. }
  27.  
  28. // Note reversed order!! BBGGRR
  29. class Color(hexa:String) {
  30.   assume(hexa.length == 8 && hexa.startsWith("0x"), "Invalid Color Format " + hexa)
  31.   val blue = hexa.substring(2,4).toInt
  32.   val green = hexa.substring(4,6).toInt
  33.   val red = hexa.substring(6,8).toInt
  34.   override def toString = "rgb("+red+","+green+","+blue+")"
  35. }
  36.  
  37. case class Font(face:String,size:Int,style:TextStyle.TextStyle)
  38.  
  39. /* TODO: currently we don't support conditional sizes and this *may* require
  40.  *       changes. Anyway, when traversing the AST simply make sure that the
  41.  *       Expr is a Dim */
  42. trait Expr
  43. trait DirectExpr extends Expr /* Anything but conditions */
  44. case class Literal[T](value:T) extends DirectExpr
  45.  
  46. case class Var(id:String) extends DirectExpr
  47. case class PropVar(loc:String, id:String) extends DirectExpr
  48. case class Sum(add:List[DirectExpr], sub:List[DirectExpr]) extends DirectExpr
  49. case class Prod(mul:List[DirectExpr], div:List[DirectExpr]) extends DirectExpr
  50. case class Conj(elems:List[DirectExpr]) extends DirectExpr
  51. case class Disnj(elems:List[DirectExpr]) extends DirectExpr
  52. case class Comp(left:DirectExpr,right:DirectExpr) extends DirectExpr
  53.  
  54. case class Cond(conds:List[(Expr,Expr)], otherwise:Expr) extends Expr
  55.  
  56. /**
  57.  * None = Automatic size
  58.  * Some(DirectExpr) = Custom size
  59.  */
  60. trait Widget {
  61.   def kind(): String
  62.   def properties(): Map[String,Expr]
  63.   def width(): Option[DirectExpr]
  64.   def height(): Option[DirectExpr]
  65.   def hasChildren (): Boolean
  66.   def getChildren (): Iterable[Widget]
  67. }
  68.  
  69. abstract class WidgetImp(k:String, w:Option[DirectExpr],h:Option[DirectExpr],p:Map[String,Expr],hc:Boolean,gc:Iterable[Widget]) extends Widget {
  70.   def kind = k
  71.   def properties = p
  72.   def width = w
  73.   def height = h
  74.   def hasChildren = hc
  75.   def getChildren = gc
  76.  
  77.   override def toString(): String = {
  78.     var i = new Indenter();
  79.     i.iprintln("Widget of type: " + k)
  80.     i.iprintln("It's dimensions are: " + width + "x" + height)
  81.     i.iprintln("It's properties are:")
  82.     i.doIndent()
  83.     p.keys map { key => i.iprintln(key + " = " + p(key))}
  84.     i.unIndent()
  85.     if (hc) {
  86.       i.iprintln ("It has the following children: ")
  87.       i.doIndent()
  88.         gc map { child => i.println(child)}
  89.       i.unIndent()
  90.     }
  91.     return i.toString
  92.   }
  93. }
  94.  
  95. /* An atomic widget is either one of the built in widgets, or one which was defined
  96.  * as a "subprogram" somewhere else
  97.  * Note that for the empty widget, we will use the empty string as it's kind
  98.  */
  99. class AtomicWidget(k:String, w:Option[DirectExpr],h:Option[DirectExpr],p:Map[String,Expr]) extends
  100.   WidgetImp(k,w,h,p,false,List())
  101.  
  102. class HVContainer(ws:List[Widget], isVertical:Boolean) extends
  103.   WidgetImp(if (isVertical) "---" else "|", None, None, Map(), true, ws)
  104.  
  105. class OneContainer(ws:Widget, w:Option[DirectExpr], h:Option[DirectExpr], p:Map[String,Expr]) extends
  106.   WidgetImp("@", w, h, p, true, List(ws))
Advertisement
Add Comment
Please, Sign In to add comment