Step 7: Add integration tests for function definitions - define+call, multi-param, cross-function, nested, errors
1 file changed
tree: ec487f79571f7f017692253e75e54ad182e63470
  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.