Advertisement
Guest User

Karel Syntax

a guest
Nov 7th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.33 KB | None | 0 0
  1.   def program = rep(commandDefinition) ^^ Program
  2.  
  3.   def commandDefinition = positioned(("void\\b".r ~> ident <~ "(" ~ ")" ~ "{") ~ rep(statement) ~ closingBrace ^^ {
  4.     case identifier ~ statements ~ r3turn => CommandDefinition(identifier, Block(statements :+ r3turn))
  5.   })
  6.  
  7.   def block: Parser[Block] = "{" ~> rep(statement) <~ "}" ^^ Block
  8.  
  9.   def closingBrace = positioned("}" ^^^ Return())
  10.  
  11.   def statement = positioned(
  12.     ifStatement |
  13.       assertStatement |
  14.       whileStatement |
  15.       repeatStatement |
  16.       commandCall)
  17.  
  18.   def ifStatement =
  19.     ("if" ~ "(" ~> condition <~ ")") ~ block ~ opt("else" ~! block) ^^ {
  20.       case cond ~ th3n ~ None => IfThen(cond, th3n)
  21.       case cond ~ th3n ~ Some(_ ~ e1se) => IfThenElse(cond, th3n, e1se)
  22.     }
  23.  
  24.   def assertStatement =
  25.     ("assert" ~ "(" ~> condition <~ ")" ~ ";") ^^ Assert
  26.    
  27.   def whileStatement =
  28.     ("while" ~ "(" ~> condition <~ ")") ~ block ^^ { case cond ~ block => While(cond, block) }
  29.  
  30.   def repeatStatement =
  31.     ("repeat" ~ "(" ~> integer <~ ")") ~ block ^^ { case times ~ block => Repeat(times.toInt, block) }
  32.  
  33.   def integer = "\\d+".r
  34.  
  35.   def commandCall =
  36.     command <~ "(" ~ ")" ~ ";" ^^ CommandCall
  37.  
  38.   def command =
  39.     positioned(ident ^^ {
  40.       case "moveForward" => MoveForward
  41.       case "turnLeft" => TurnLeft
  42.       case "turnAround" => TurnAround
  43.       case "turnRight" => TurnRight
  44.       case "pickBeeper" => PickBeeper
  45.       case "dropBeeper" => DropBeeper
  46.       case "exit" => Exit
  47.       case "interrupt" => Interrupt
  48.       case identifier => UserDefinedCommand(identifier)
  49.     })
  50.  
  51.   def condition = disjunction
  52.  
  53.   def disjunction = rep1sep(conjunction, "||") ^^ {
  54.     case x :: Nil => x
  55.     case xs => Disjunction(xs)
  56.   }
  57.  
  58.   def conjunction = rep1sep(primaryCondition, "&&") ^^ {
  59.     case x :: Nil => x
  60.     case xs => Conjunction(xs)
  61.   }
  62.  
  63.   def primaryCondition: Parser[Condition] =
  64.     booleanLiteral |
  65.       builtinPredicate <~ "(" ~ ")" |
  66.       "!" ~> primaryCondition ^^ Not |
  67.       "(" ~> condition <~ ")" ^^ Parenthesized
  68.  
  69.   def booleanLiteral =
  70.     "false" ^^^ False |
  71.       "true" ^^^ True
  72.  
  73.   def builtinPredicate =
  74.     "onBeeper" ^^^ OnBeeper |
  75.       "beeperAhead" ^^^ BeeperAhead |
  76.       "leftIsClear" ^^^ LeftIsClear |
  77.       "frontIsClear" ^^^ FrontIsClear |
  78.       "rightIsClear" ^^^ RightIsClear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement