| Sketchπ΄οΈ | a22d352 | 2026-02-28 19:36:42 +0400 | [diff] [blame] | 1 | # matheval |
| 2 | |
| 3 | A math expression evaluator with an interactive REPL, written in Go. |
| 4 | |
| 5 | ## Features |
| 6 | |
| 7 | - Arithmetic operators: `+`, `-`, `*`, `/` |
| 8 | - Parentheses for grouping |
| 9 | - Floating point numbers (including `.5` syntax) |
| 10 | - Correct operator precedence (`*` and `/` bind tighter than `+` and `-`) |
| 11 | - Left-to-right associativity |
| 12 | - Clear error messages with position reporting |
| 13 | |
| 14 | ## Build |
| 15 | |
| 16 | ```sh |
| 17 | go build -o matheval ./cmd/matheval |
| 18 | ``` |
| 19 | |
| 20 | ## Usage |
| 21 | |
| 22 | Run the REPL: |
| 23 | |
| 24 | ```sh |
| 25 | ./matheval |
| 26 | ``` |
| 27 | |
| 28 | Then type expressions: |
| 29 | |
| 30 | ``` |
| 31 | >> 2 + 3 * 4 |
| 32 | 14 |
| 33 | >> (2 + 3) * 4 |
| 34 | 20 |
| 35 | >> 7 / 2 |
| 36 | 3.5 |
| 37 | >> 1 / 0 |
| 38 | error: division by zero |
| 39 | ``` |
| 40 | |
| 41 | Press `Ctrl+D` (EOF) to exit. |
| 42 | |
| 43 | You can also pipe input: |
| 44 | |
| 45 | ```sh |
| 46 | echo "2 + 3" | ./matheval |
| 47 | ``` |
| 48 | |
| 49 | ## Architecture |
| 50 | |
| 51 | ``` |
| 52 | Input string β Lexer β Parser β AST β Evaluator β Result |
| 53 | ``` |
| 54 | |
| 55 | | Package | Responsibility | |
| 56 | |-------------|---------------------------------------| |
| 57 | | `token` | Token types and data structures | |
| 58 | | `lexer` | Tokenizes input string | |
| 59 | | `ast` | AST node types (`NumberLit`, `BinaryExpr`) | |
| 60 | | `parser` | Recursive-descent parser | |
| 61 | | `evaluator` | Walks AST and computes result | |
| 62 | | `repl` | Read-eval-print loop | |
| 63 | |
| 64 | ## Grammar |
| 65 | |
| 66 | ``` |
| 67 | expr β term (('+' | '-') term)* |
| 68 | term β factor (('*' | '/') factor)* |
| 69 | factor β NUMBER | '(' expr ')' |
| 70 | ``` |
| 71 | |
| 72 | ## Tests |
| 73 | |
| 74 | ```sh |
| 75 | go test ./... |
| 76 | ``` |
| 77 | |
| 78 | This runs unit tests for each package plus integration tests covering the full pipeline. |