Arithmetic evaluation: Difference between revisions
Content added Content deleted
m (→Icon and Unicon: header simplification) |
|||
Line 1,998: | Line 1,998: | ||
is practically non-existent, to avoid obscuring the code. |
is practically non-existent, to avoid obscuring the code. |
||
<lang scala> |
<lang scala> |
||
package org.rosetta.arithmetic_evaluator.scala |
|||
object ArithmeticParser extends scala.util.parsing.combinator.RegexParsers { |
object ArithmeticParser extends scala.util.parsing.combinator.RegexParsers { |
||
def readExpression(input: String) : Option[()=> |
def readExpression(input: String) : Option[()=>Int] = { |
||
parseAll(expr, input) match { |
|||
case Success(result, _) => |
|||
Some(result) |
|||
case other => |
|||
println(other) |
|||
None |
|||
} |
} |
||
} |
} |
||
private |
private def expr : Parser[()=>Int] = { |
||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
private def |
private def term : Parser[()=>Int] = { |
||
( |
(factor<~"*")~term ^^ { case l~r => () => l() * r() } | |
||
⚫ | |||
factor |
|||
} |
|||
private def |
private def factor : Parser[()=>Int] = { |
||
"("~>expr<~")" | |
|||
(factor~("*" | "/")~term ^^ toExpr) | factor |
|||
"\\d+".r ^^ { x => () => x.toInt } | |
|||
⚫ | |||
private def factor : Parser[AST] = |
|||
⚫ | |||
private def digits = |
|||
"\\d+".r |
|||
private def toLiteral(partialResult: String) = |
|||
() => partialResult.toInt |
|||
private def toExpr(partialResult: ~[~[AST, String], AST]) = { |
|||
partialResult match { |
|||
case (l~op)~r => |
|||
op match { |
|||
⚫ | |||
⚫ | |||
case "*" => () => l() * r() |
|||
⚫ | |||
case x => error("Unknown operation " + x + ".") |
|||
} |
|||
⚫ | |||
} |
} |
||
} |
} |
||
Line 2,055: | Line 2,044: | ||
} while (input != "q") |
} while (input != "q") |
||
} |
} |
||
} |
|||
</lang> |
|||
Example: |
Example: |