solution/cw5/fun_parser.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 10 Jan 2022 23:52:51 +0000
changeset 867 54bcd848d716
parent 864 b5b1bc0a603b
child 868 8fb3b6d3be70
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// A parser for the Fun language
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//================================
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// call with 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
//     amm fun_parser.sc fact.fun
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//     amm fun_parser.sc defs.fun
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// this will generate a parse-tree from a list
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
// of tokens
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
import scala.language.implicitConversions    
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
import scala.language.reflectiveCalls
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
import $file.fun_tokens, fun_tokens._ 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
// Parser combinators
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
//    type parameter I needs to be of Seq-type
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
abstract class Parser[I, T](implicit ev: I => Seq[_]) {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  def parse(ts: I): Set[(T, I)]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  def parse_single(ts: I) : T = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
    parse(ts).partition(_._2.isEmpty) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
      case (good, _) if !good.isEmpty => good.head._1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
      case (good, err) if err.isEmpty => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
        println (s"Parse Error\n $good \n $err") ; sys.exit(-1) }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
      case (_, err) => { 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
	println (s"Parse Error\n${err.minBy(_._2.length)}") ; sys.exit(-1) }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
// convenience for writing grammar rules
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
case class ~[+A, +B](_1: A, _2: B)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
class SeqParser[I, T, S](p: => Parser[I, T], 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
                         q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, ~[T, S]] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  def parse(sb: I) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
class AltParser[I, T](p: => Parser[I, T], 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
                      q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
class FunParser[I, T, S](p: => Parser[I, T], 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
                         f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  def parse(sb: I) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
// convenient combinators
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
def ListParser[I, T, S](p: => Parser[I, T], 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
                        q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  (p ==> ((s) => List(s))) ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  (p ~ q ~ ListParser(p, q)) ==> { case x ~ _ ~ z => x :: z : List[T] }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
case class TokParser(tok: Token) extends Parser[List[Token], Token] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
    case t::ts if (t == tok) => Set((t, ts)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
    case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
implicit def token2tparser(t: Token) = TokParser(t)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
implicit def TokOps(t: Token) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
case object EmptyParser extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
  def parse(ts: List[Token]) = Set(("", ts))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
case object NumParser extends Parser[List[Token], Int] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
    case T_NUM(n)::ts => Set((n, ts)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
    case _ => Set ()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
case object FNumParser extends Parser[List[Token], Float] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
    case T_FNUM(x)::ts => Set((x, ts)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
    case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
case object IdParser extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
    case T_ID(s)::ts => Set((s, ts)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
    case _ => Set ()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
case object CharConstParser extends Parser[List[Token], Int] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
    case T_CHR(c)::ts => Set((c, ts)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
    case _ => Set ()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
case object TyParser extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
    case T_TY(s)::ts => Set((s, ts)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
    case _ => Set ()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
// Abstract syntax trees for the Fun language
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
abstract class Exp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
abstract class BExp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
abstract class Decl 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
case class Def(name: String, args: List[(String, String)], ty: String, body: Exp) extends Decl
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
case class Main(e: Exp) extends Decl
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
case class Const(name: String, v: Int) extends Decl
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
case class FConst(name: String, x: Float) extends Decl
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
case class Call(name: String, args: List[Exp]) extends Exp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
case class Var(s: String) extends Exp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
case class Num(i: Int) extends Exp     // integer numbers
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
case class FNum(i: Float) extends Exp  // floating numbers
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
case class ChConst(c: Int) extends Exp // char constant
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
case class Sequence(e1: Exp, e2: Exp) extends Exp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
// arithmetic expressions (there needs to be an F in the SEMICOLON case)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
lazy val Exp: Parser[List[Token], Exp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==>
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
    { case _ ~ x ~ _ ~ y ~ _ ~ z => If(x, y, z): Exp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  (F ~ T_SEMI ~ Exp) ==> { case x ~ _ ~ y => Sequence(x, y): Exp } || L
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
lazy val L: Parser[List[Token], Exp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
  (T ~ T_OP("+") ~ Exp) ==> { case x ~ _ ~ z => Aop("+", x, z): Exp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  (T ~ T_OP("-") ~ Exp) ==> { case x ~ _ ~ z => Aop("-", x, z): Exp } || T  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
lazy val T: Parser[List[Token], Exp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
  (F ~ T_OP("*") ~ T) ==> { case x ~ _ ~ z => Aop("*", x, z): Exp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
  (F ~ T_OP("/") ~ T) ==> { case x ~ _ ~ z => Aop("/", x, z): Exp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  (F ~ T_OP("%") ~ T) ==> { case x ~ _ ~ z => Aop("%", x, z): Exp } || F
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
lazy val F: Parser[List[Token], Exp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
  (IdParser ~ T_LPAREN ~ T_RPAREN) ==> 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
    { case x ~ _ ~ _ => Call(x, Nil): Exp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
  (IdParser ~ T_LPAREN ~ T_RPAREN) ==> { case x ~ _ ~ _ => Call(x, Nil): Exp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
  (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> { case x ~ _ ~ z ~ _ => Call(x, z): Exp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
  (T_LPAREN ~ Exp ~ T_RPAREN) ==> { case _ ~ y ~ _ => y: Exp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
  IdParser ==> { case x => Var(x): Exp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
  NumParser ==> { case x => Num(x): Exp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
  CharConstParser ==> { case x => ChConst(x): Exp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  FNumParser ==> { case x => FNum(x): Exp }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
// boolean expressions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
lazy val BExp: Parser[List[Token], BExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
  (Exp ~ T_OP("==") ~ Exp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
  (Exp ~ T_OP("!=") ~ Exp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
  (Exp ~ T_OP("<") ~ Exp)  ==> { case x ~ _ ~ z => Bop("<",  x, z): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
  (Exp ~ T_OP(">") ~ Exp)  ==> { case x ~ _ ~ z => Bop("<",  z, x): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
  (Exp ~ T_OP("<=") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", x, z): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  (Exp ~ T_OP("=>") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", z, x): BExp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  (T_LPAREN ~ BExp ~ T_RPAREN) ==> { case _ ~ b ~ _ => b : BExp } 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
lazy val Arg : Parser[List[Token], (String, String)] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
  (IdParser ~ T_COLON ~ TyParser) ==> { case x ~ _ ~ ty => (x, ty) }  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
lazy val Defn: Parser[List[Token], Decl] = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
   (T_KWD("def") ~ IdParser ~ T_LPAREN ~ T_RPAREN ~ T_COLON ~ TyParser ~ T_OP("=") ~ Exp) ==>
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
     { case _ ~ y ~ _ ~ _ ~ _~ ty ~ _ ~ r => Def(y, Nil, ty, r): Decl } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
   (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(Arg, T_COMMA) ~ T_RPAREN ~ T_COLON ~ TyParser ~ T_OP("=") ~ Exp) ==>
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
     { case _ ~ y ~ _ ~ w ~ _ ~ _~ ty ~ _ ~ r => Def(y, w, ty, r): Decl }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
lazy val Const_decl: Parser[List[Token], Decl] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
   (T_KWD("val") ~ Arg ~ T_OP("=") ~ NumParser) ==>
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
     { case _ ~ x ~ _ ~ v => Const(x._1, v): Decl } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
   (T_KWD("val") ~ Arg ~ T_OP("=") ~ FNumParser) ==>
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
     { case _ ~ x ~ _ ~ v => FConst(x._1, v): Decl } 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
lazy val Prog: Parser[List[Token], List[Decl]] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
  (Defn ~ T_SEMI ~ Prog) ==> { case x ~ _ ~ z => x :: z : List[Decl] } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
  (Const_decl ~ T_SEMI ~ Prog) ==> { case x ~ _ ~ z => x :: z : List[Decl] } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  (Exp ==> ((s) => List(Main(s)) : List[Decl]))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
// Reading tokens and Writing parse trees
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
867
54bcd848d716 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   203
//import ammonite.ops._
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
def parse_tks(tks: List[Token]) : List[Decl] = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
  //println(Prog.parse(tks))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
  Prog.parse_single(tks)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
//@doc("Parses a file.")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
@main
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
def main(fname: String) : Unit = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
  val tks = tokenise(os.read(os.pwd / fname))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
  println(parse_tks(tks))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217