Welcome to my mini project! This initiative allowed me to delve into Yew, a compelling Rust framework that serves as an alternative to React. I chose Yew to further my development skills in Rust and explore its potential in building web applications.
Along the way, I decided to create my own math parser from scratch. This involved implementing an Abstract Syntax Tree (AST) and utilizing the Shunting Yard algorithm.
This really basic parser provides the following capabilities:
- Tokenizing the input code into a stream of tokens.
- Parsing the token stream to build an AST.
- Checking the syntax and structure of the code.
- Resolving variable and function references.
- Reporting any syntax errors or semantic issues.
It supports the following operators:
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Exponentiation:
^ - Modulo:
%
These operators can be used to perform basic arithmetic operations within the math parser. Additionally, parentheses () can be used to group expressions and control the order of operations.
Please note that the math parser follows the standard precedence rules for operators, where exponentiation has the highest precedence, followed by multiplication, division, and modulo, and finally addition and subtraction.
This parser is used in the website's code as a library (written in src/parser) that is used as follows:
use crate::parser::parse;
let str_value = "3*(4+5)";
let result = parse(str_value);
assert_eq!(result, Some(27));This website is available as a GitHub page here.
Running it locally (Extracted from the Yew Trunk Minimal Template)
If you don't already have it installed, it's time to install Rust: https://www.rust-lang.org/tools/install.
The rest of this guide assumes a typical Rust installation which contains both rustup and Cargo.
To compile Rust to WASM, we need to have the wasm32-unknown-unknown target installed.
If you don't already have it, install it with the following command:
rustup target add wasm32-unknown-unknownNow that we have our basics covered, it's time to install the star of the show: Trunk. Simply run the following command to install it:
cargo install trunk wasm-bindgen-cliFinally, we can serve the website:
trunk serve