Created
February 18, 2020 18:36
-
-
Save okram/529a631331aa78ef23f7a5a410de219e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object mmLangParser extends JavaTokenParsers { | |
def op:Parser[String] = """[a-z]+""".r | |
def expr:Parser[OType] = canonicalType ~ inst ^^ (x => x._1.asInstanceOf[IntType].plus(x._2.arg[IntValue]())) | |
def intValue:Parser[IntValue] = """[0-9]+""".r ^^ (x => int(x.toLong)) | |
def canonicalType:Parser[OType] = (Tokens.int | Tokens.str) ^^ ({ | |
case Tokens.int => int | |
case Tokens.str => str | |
}) | |
def inst:Parser[Inst] = "[" ~ op ~ "," ~ intValue ~ "]" ^^ (x => PlusOp(x._1._2)) | |
} | |
object LocalApp extends App { | |
override def main(args:Array[String]):Unit ={ | |
mmLangParser.parseAll(mmLangParser.expr,"int[plus,2]") match { | |
case mmLangParser.Success(result,_) => println(result) | |
case _ => println("Could not parse the input string.") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment