Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def program = rep(commandDefinition) ^^ Program
- def commandDefinition = positioned(("void\\b".r ~> ident <~ "(" ~ ")" ~ "{") ~ rep(statement) ~ closingBrace ^^ {
- case identifier ~ statements ~ r3turn => CommandDefinition(identifier, Block(statements :+ r3turn))
- })
- def block: Parser[Block] = "{" ~> rep(statement) <~ "}" ^^ Block
- def closingBrace = positioned("}" ^^^ Return())
- def statement = positioned(
- ifStatement |
- assertStatement |
- whileStatement |
- repeatStatement |
- commandCall)
- def ifStatement =
- ("if" ~ "(" ~> condition <~ ")") ~ block ~ opt("else" ~! block) ^^ {
- case cond ~ th3n ~ None => IfThen(cond, th3n)
- case cond ~ th3n ~ Some(_ ~ e1se) => IfThenElse(cond, th3n, e1se)
- }
- def assertStatement =
- ("assert" ~ "(" ~> condition <~ ")" ~ ";") ^^ Assert
- def whileStatement =
- ("while" ~ "(" ~> condition <~ ")") ~ block ^^ { case cond ~ block => While(cond, block) }
- def repeatStatement =
- ("repeat" ~ "(" ~> integer <~ ")") ~ block ^^ { case times ~ block => Repeat(times.toInt, block) }
- def integer = "\\d+".r
- def commandCall =
- command <~ "(" ~ ")" ~ ";" ^^ CommandCall
- def command =
- positioned(ident ^^ {
- case "moveForward" => MoveForward
- case "turnLeft" => TurnLeft
- case "turnAround" => TurnAround
- case "turnRight" => TurnRight
- case "pickBeeper" => PickBeeper
- case "dropBeeper" => DropBeeper
- case "exit" => Exit
- case "interrupt" => Interrupt
- case identifier => UserDefinedCommand(identifier)
- })
- def condition = disjunction
- def disjunction = rep1sep(conjunction, "||") ^^ {
- case x :: Nil => x
- case xs => Disjunction(xs)
- }
- def conjunction = rep1sep(primaryCondition, "&&") ^^ {
- case x :: Nil => x
- case xs => Conjunction(xs)
- }
- def primaryCondition: Parser[Condition] =
- booleanLiteral |
- builtinPredicate <~ "(" ~ ")" |
- "!" ~> primaryCondition ^^ Not |
- "(" ~> condition <~ ")" ^^ Parenthesized
- def booleanLiteral =
- "false" ^^^ False |
- "true" ^^^ True
- def builtinPredicate =
- "onBeeper" ^^^ OnBeeper |
- "beeperAhead" ^^^ BeeperAhead |
- "leftIsClear" ^^^ LeftIsClear |
- "frontIsClear" ^^^ FrontIsClear |
- "rightIsClear" ^^^ RightIsClear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement