- The execution mechanism is highly influenced by my work presto-utils
- The visitor pattern is inspired by trino-parser
- Some part of it uses Shunting-yard algorithm
Directly :
./gradlew ':kalkulator-cli:run' --args=" -e '5*4 + 3/2' "
OR
Build and then run
./gradlew clean shadowJar
java -jar ./kalkulator-cli/build/libs/kalkulator-cli-*-all.jar -e '5*4 + 3/2'
- Create a token pattern e.g.
val MODULO_TOK = PatternBasedTokenType.fromString("MODULO", "%")
- Create an operator
val MODULO = BinaryOperator(MODULO_TOK, Associativity.LEFT_TO_RIGHT)
- Create an operator module to add precedence and evaluation code
Check more at StandardOperatorModules
newModule(MODULO, MULT_DIV_PRECEDENCE, Double::mod)
Well since we are not using any parser generator like antlr4
or lark
it is hard to introduce a new grammar rule. The following components need to be modified
- Parser (maybe lexer as well)
- AstVisitor
- ExpressionEvaluator (as a sub class of AstVisitor)
- Try to make parenthesis as a first class unary operator
- Re-implement expression-parser using a parser generator like
antlr4
orlark