tinymux/parser
Stephen Dennis 0f8745cbaf Optimize hir_opt.cpp with commutative CSE and update eval utility
- hir_opt.cpp: Normalize commutative binary ops (ADD, MUL, etc.) in CSE to improve redundancy detection.
- parser/eval.cpp: Add IF and EVAL support to study tool for better Issue 2 verification.
2026-03-15 23:13:25 -06:00
..
.gitignore Add AST evaluator with 50+ builtin functions 2026-03-07 10:06:59 -07:00
eval.cpp Optimize hir_opt.cpp with commutative CSE and update eval utility 2026-03-15 23:13:25 -06:00
Makefile Add AST evaluator with 50+ builtin functions 2026-03-07 10:06:59 -07:00
mux_parse.h Factor shared tokenizer/parser/AST into mux_parse.h 2026-03-07 10:06:59 -07:00
parse.cpp Factor shared tokenizer/parser/AST into mux_parse.h 2026-03-07 10:06:59 -07:00
README.md Add AST evaluator with 50+ builtin functions 2026-03-07 10:06:59 -07:00
test_corpus.txt Add recursive-descent parser and enhanced tokenizer 2026-03-07 10:06:59 -07:00
test_eval.sh Add eval flag support to AST evaluator 2026-03-07 10:06:59 -07:00
tokenize.cpp Factor shared tokenizer/parser/AST into mux_parse.h 2026-03-07 10:06:59 -07:00

MUX Parser Study

Standalone tools for studying the TinyMUX expression parser. See ../docs/PARSER.md for the full architecture analysis.

Contents

  • tokenize.cpp — Stage 1: tokenizer (flat token stream)
  • parse.cpp — Stage 2: recursive-descent parser (AST)
  • test_corpus.txt — Test expressions
  • Makefile — Build rules

Usage

make
echo 'add(1,mul(2,3))' | ./tokenize
echo '[setq(0,hello)]%q0 world' | ./parse
./parse < test_corpus.txt

Stages

  1. Tokenizer — Character-level scanning that mirrors eval.cpp's isSpecial tables. Identifies literals, function names, brackets, braces, %-substitutions, escapes, and structural characters.

  2. Parser — Recursive-descent parser that builds an AST from the token stream. Handles static function calls (FuncCall), dynamic/computed calls (DynCall), eval brackets, brace groups, and nested argument lists.

  3. Evaluator — Tree-walking evaluator for pure expressions. Supports ~50 builtin functions (arithmetic, string, list, logic, comparison, registers). Demonstrates that pure-expression MUX softcode CAN be evaluated from an AST.

    echo '[add(1,mul(2,3))]' | ./eval
    echo '[if(eq(%0,hello),yes,no)]' | ./eval
    echo '[sort(3 1 4 1 5)]' | ./eval
    ./eval --ast  # show AST alongside output
    

    Key finding: FN_NOEVAL functions (iter, switch, case, if) need deferred argument evaluation. The evaluator must pass unevaluated AST subtrees to these handlers rather than pre-evaluated strings. This is solvable but requires a different dispatch mechanism.

Purpose

This is an exploration tool, not production code. The goal is to understand the existing parser well enough to determine whether a Ragel scanner + recursive-descent parser can reproduce its behavior.