at15 / reika

[WIP] A DSL for query and benchmark TSDB
MIT License
1 stars 0 forks source link

[antlr] Precedence of arithmetic operators #19

Closed at15 closed 6 years ago

at15 commented 6 years ago

If we mix all the binary operators (+, -, *, /) together, we lost the precedence, and user need to explicitly add () to have multiply executed before plus. The reason we put them together is we want to avoid duplicated code in visitor when building AST, because the operator is the only difference

Currently we have (this is wrong)

term
    : literal # TmLiteral
    | '-' term # TmNegative
    | term BINARY_OP term # TmBinaryOp
//    | list # TmList
//    | record # TmRecord
    | '(' term ')' # TmBrackets
    ;

BINARY_OP
    : '+'
    | '-'
    | '*'
    | '/'
    ;

for 1 + 2 * 3 we have

     *
  +   3
1  2

but we should have

   +
1   *
   2  3