Skip to main content

Crate gyard

Crate gyard 

Source
Expand description

A generic Shunting yard algorithm implementation This crate only provides the shunting yard algorithm. You need to write your own parser and convert its result to InputTokens The types for values, functions and operators are generic. Operators must implement the Operator trait.

This crate contains definitions for some operators in the op module.

use gyard::{InputToken, OutputToken, op::Math, to_postfix};
// 5 + 2 * sin(123)
let infix = [
    InputToken::Value(5),
    InputToken::Operator(Math::Add),
    InputToken::Value(2),
    InputToken::Operator(Math::Mul),
    InputToken::Function("sin"),
    InputToken::LeftParen,
    InputToken::Value(123),
    InputToken::RightParen,
];
let postfix = to_postfix(infix);
assert_eq!(postfix, Ok(vec![
    OutputToken::Value(5),
    OutputToken::Value(2),
    OutputToken::Value(123),
    OutputToken::Function("sin"),
    OutputToken::Operator(Math::Mul),
    OutputToken::Operator(Math::Add),
]));

Modules§

op
This module contains predefined operators. The precedence is based on the JavaScript definition. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence

Structs§

ParenMissmatchError
This error is returned if the parentheses inside a expression do not match.

Enums§

InputToken
All valid input tokens
OutputToken
All valid output tokens

Traits§

Operator
Mark any struct or enum as an Operator. Each operator has to define its precedence and if it is left associative.

Functions§

to_postfix
Convert a infix expression into a postfix expression. It is highly recomended to wrap function arguments in parentheses as the result may be unexpected otherwise. Example: