blob: c115543448109dd885790e0b890a25d0a36b1dc8 [file] [log] [blame] [view]
SketchπŸ•΄οΈa22d3522026-02-28 19:36:42 +04001# matheval
2
3A 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
17go build -o matheval ./cmd/matheval
18```
19
20## Usage
21
22Run the REPL:
23
24```sh
25./matheval
26```
27
28Then type expressions:
29
30```
31>> 2 + 3 * 4
3214
33>> (2 + 3) * 4
3420
35>> 7 / 2
363.5
37>> 1 / 0
38error: division by zero
39```
40
41Press `Ctrl+D` (EOF) to exit.
42
43You can also pipe input:
44
45```sh
46echo "2 + 3" | ./matheval
47```
48
49## Architecture
50
51```
52Input 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```
67expr β†’ term (('+' | '-') term)*
68term β†’ factor (('*' | '/') factor)*
69factor β†’ NUMBER | '(' expr ')'
70```
71
72## Tests
73
74```sh
75go test ./...
76```
77
78This runs unit tests for each package plus integration tests covering the full pipeline.