| commit | dc6d8f6f5a59956fb65dd84b065c16a4290dcbee | [log] [tgz] |
|---|---|---|
| author | Sketch🕴️ <skallywag@sketch.dev> | Sat Feb 28 20:45:13 2026 +0400 |
| committer | Sketch🕴️ <skallywag@sketch.dev> | Sat Feb 28 20:45:13 2026 +0400 |
| tree | 6d5086d7146fe6017e6218c4a48ed1b91072ad49 | |
| parent | 5337c2bc7ab93112e7edd4c772d98987ba596e49 [diff] |
lexer: recognize identifiers, comma, and equals tokens - Add identifier scanning: starts with letter/underscore, continues with letters/digits/underscores. Produces Ident tokens. - Add comma and equals to single-char token switch. - Add isLetter() helper. - Add 9 new tests covering: single ident, multi-char ident, ident with digits, ident with underscore, comma, equals, function definition syntax, function call with args, multi-param func def, func call in expression.
A math expression evaluator with an interactive REPL, written in Go.
+, -, *, /.5 syntax)* and / bind tighter than + and -)go build -o matheval ./cmd/matheval
Run the REPL:
./matheval
Then type expressions:
>> 2 + 3 * 4 14 >> (2 + 3) * 4 20 >> 7 / 2 3.5 >> 1 / 0 error: division by zero
Press Ctrl+D (EOF) to exit.
You can also pipe input:
echo "2 + 3" | ./matheval
Input string → Lexer → Parser → AST → Evaluator → Result
| Package | Responsibility |
|---|---|
token | Token types and data structures |
lexer | Tokenizes input string |
ast | AST node types (NumberLit, BinaryExpr) |
parser | Recursive-descent parser |
evaluator | Walks AST and computes result |
repl | Read-eval-print loop |
expr → term (('+' | '-') term)*
term → factor (('*' | '/') factor)*
factor → NUMBER | '(' expr ')'
go test ./...
This runs unit tests for each package plus integration tests covering the full pipeline.