| commit | 231861d38d139e5a9fbb27fcda3d6a74a5c1b65d | [log] [tgz] |
|---|---|---|
| author | Sketchπ΄οΈ <skallywag@sketch.dev> | Sat Feb 28 21:12:18 2026 +0400 |
| committer | Sketchπ΄οΈ <skallywag@sketch.dev> | Sat Feb 28 21:12:18 2026 +0400 |
| tree | 7345ee44716147a46b39109907161d646afef673 | |
| parent | 519412600929d0dc64638699a9fd497a4f92416a [diff] |
README
A math expression evaluator with user-defined functions and an interactive REPL, written in Go.
+, -, *, /.5 syntax)* and / bind tighter than + and -)f(x) = x + 1 syntaxf(x, y) = x + ygo build -o matheval ./cmd/matheval
Run the REPL:
./matheval
Then type expressions and define functions:
>> 2 + 3 * 4 14 >> (2 + 3) * 4 20 >> 7 / 2 3.5 >> 1 / 0 error: division by zero >> square(x) = x * x defined square >> square(5) 25 >> add(x, y) = x + y defined add >> add(2, 3) 5 >> hyp(a, b) = square(a) + square(b) defined hyp >> hyp(3, 4) 25
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, Ident, FuncCall, FuncDef) |
parser | Recursive-descent parser (expressions and function definitions) |
evaluator | Stateful evaluator with function registry |
repl | Read-eval-print loop with persistent state |
line β funcdef | expr
funcdef β IDENT '(' params ')' '=' expr
params β IDENT (',' IDENT)*
expr β term (('+' | '-') term)*
term β factor (('*' | '/') factor)*
factor β NUMBER | IDENT '(' args ')' | IDENT | '(' expr ')'
args β expr (',' expr)*
name(params) = body β body is any expression using the parametersname(args) β arguments are arbitrary expressionsgo test ./...
This runs unit tests for each package plus integration tests covering the full pipeline.