Prototype of a math interpreter. Because this project was created only for the learning purpose so there are some limitations sorry :
- supports five binary operators:
+
,-
,*
,/
,^
- supports two unary operators:
+
,-
- supports only integers
i32
- Unoptimized debug build:
cargo build
- Optimized production build:
cargo build --release
- Run app from debug build:
cargo run
- Run app from release build:
cargo run --release
- Run all tests:
cargo test
Input string is being converted by the lexer (lexical analyzer, scanner or tokenizer) into stream of tokens.This process is being called lexical analysis. Then these tokens is being passed to the parser (semantic analyzer).Parser converts them into AST. This process is being called syntax analysis or parsing. Finally interpreter runs a visitor mechanism (tree walker) on the AST using postorder traversal algorithm and finally evaluate a result.
Typical algebraic expression (might) consists of Terms, Factors, Powers, Coefficients and Constants.In this case I've made little simplification. It's easily visible in the grammar (for the pragmatic reason I'm using EBNF notation):
expr = power ((PLUS | MINUS) power)*
power = term (POW term)*
term = factor ((MUL | DIV) power)*
factor = (PLUS | MINUS) factor | INTEGER | LPAREN expr RPAREN