Skip to content

JohnKesko/langlab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangLab

(Work in progress...)

LangLab (language lab) is a lightweight, recursive-descent math expression evaluator written in Rust.
It transforms raw strings into an Abstract Syntax Tree (AST) while strictly respecting operator precedence (PEMDAS).


Quick Start

Configure input

Place your math expression in query.txt:

2 * (5 - 2)
[
  {
    "Binary": {
      "left": {
        "Integer": 2
      },
      "op": "Multiply",
      "right": {
        "Binary": {
          "left": {
            "Integer": 5
          },
          "op": "Minus",
          "right": {
            "Integer": 2
          }
        }
      }
    }
  }
]
-----------------
Input: 2 * (5 - 2)
 => Result: 6

Run the evaluator

cargo run

Run the test suite

cargo test

Structure

  • AST Generation: Visual representation using serde
  • Lexical Analysis: Lexer that handles numbers, operators and whitespace
  • Precedence Climbing: Parser that respects operator priority (* before +)
  • Parentheses Support: Nest expressions as deep as you want using (...)
  • Integer Arithmetic: Uses i32 for fast evaluation
.
├── src/
│   ├── lexer.rs        # Token definitions & scanner
│   ├── parser.rs       # AST, Parser logic & Evaluator
│   ├── lib.rs          # Module exports
│   └── main.rs         # CLI entry point
├── tests/
│   └── lexer_tests.rs  # Test tokenization
│   └── parser_tests.rs # Test AST & Evaluation
└── query.txt           # Input file

LangLab follows a three-stage pipeline:

  1. Tokenize The Lexer scans the input and produces a stream of tokens

  2. Parse The Parser builds a BinaryExpr tree using operator precedence

  3. Evaluate The Evaluator walks the tree and computes the final result


Supported Operators

Operator Action Precedence
= Equality (returns 1 for true, 0 for false) 1
+ - Addition and Subtraction 2
* / Multiplication and Division 3

Limitations

  • Integer Only: Uses integer division (e.g., 1 / 2 = 0)
  • Error Handling: Uses panic! for division by zero and syntax errors
  • Types: Limited to i32 range

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages