ast: add Ident, FuncCall nodes and Statement interface (ExprStmt, FuncDef)

- Ident{Name}: variable reference node (sealed, implements Node)
- FuncCall{Name, Args}: function call node (sealed, implements Node)
- Statement interface: sealed, for top-level REPL input
- ExprStmt{Expr}: wraps expression as statement
- FuncDef{Name, Params, Body}: function definition statement
1 file changed
tree: 6e65b6d1f29c19b04742b9f035c5f65776fbfb4b
  1. ast/
  2. cmd/
  3. docs/
  4. evaluator/
  5. lexer/
  6. parser/
  7. repl/
  8. sessions/
  9. token/
  10. go.mod
  11. integration_test.go
  12. matheval
  13. README.md
README.md

matheval

A math expression evaluator with an interactive REPL, written in Go.

Features

  • Arithmetic operators: +, -, *, /
  • Parentheses for grouping
  • Floating point numbers (including .5 syntax)
  • Correct operator precedence (* and / bind tighter than + and -)
  • Left-to-right associativity
  • Clear error messages with position reporting

Build

go build -o matheval ./cmd/matheval

Usage

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

Architecture

Input string → Lexer → Parser → AST → Evaluator → Result
PackageResponsibility
tokenToken types and data structures
lexerTokenizes input string
astAST node types (NumberLit, BinaryExpr)
parserRecursive-descent parser
evaluatorWalks AST and computes result
replRead-eval-print loop

Grammar

expr   → term (('+' | '-') term)*
term   → factor (('*' | '/') factor)*
factor → NUMBER | '(' expr ')'

Tests

go test ./...

This runs unit tests for each package plus integration tests covering the full pipeline.