Written in C, compiled to WASM | https://liam-ilan.github.io/math-interpreter/
This project is a simple math interpeter. It supports expressions involving additon ("+"), subtraction ("-"), multiplication ("*"), division ("/"), brackets ("(" and ")"), and both floating point and integer arithmetic. The grammar for the inperpreter, in EBNF, is:
unary = ("-", unary) | int | float | ("(", expression, ")");
factor = (factor, ("*" | "/"), unary) | unary;
expression = (expression, ("+" | "-"), factor) | factor;Or graphically,
Generated with Vincent Jacques' DrawGrammar
The interpeter is implemented in around 600 lines of C, and then compiled to WASM with Emscripten to run on the web. Play around with the interpreter here.
All interpreter code can be found in main.c. To compile the interpreter with gcc (not emscripten), run
gcc main.c -o ./mainThe version of ./main included in this repo was built for Linux.
When compiled without emscripten, the interpreter reads from a supplied file. Run
./main file_name.hereto run the math expression contained in the given file. code.txt contains a sample test expression to get started.
This project utilzes Emscripten and WASM to compile and run on the web here.
To compile the interpreter with Emscripten, run,
emcc main.c -o public/run.js -s EXPORTED_FUNCTIONS="['_run']" -s EXPORTED_RUNTIME_METHODS="['cwrap']"
Note: To run locally, navigate to the
./publicdirectory, and run with the local webserver of your choice. The site will not work when loaded throughfile://, as sites loaded this way do not support XHR. Read more at https://emscripten.org/docs/getting_started/Tutorial.html.
- Build by Liam Ilan.
