Skip to content

NSchiffmacher/RustMathParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deployment Unit tests

Yew Calculator & Rust Math Parser

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.

Math Parser

Capabilities

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.

Usage as a library

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));

Website

Browsing it online

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-unknown

Now 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-cli

Finally, we can serve the website:

trunk serve

About

Yew Calculator & Rust Math Parser

Topics

Resources

License

Stars

Watchers

Forks