Git Product home page Git Product logo

rusty-yard's Introduction

Rusty Yard

This crate provides a generic implementation of the shunting yard algorithm

Disclaimer! API IS NOT STABLE

This is a simple project to learn the way of rust. As such the api can be rough in some places and is subject to change at any moment (patch versions are probably fine).

But Why?

This crate was created with a single purpose, to learn the rust language and ecosystem.

The use case for this library is very niche (if there is a use case). However, I would be pleased to know if somebody has found the use case for this crate.

Usage

For now the crate is only available via git:

[dependencies]
rusty_yard = { git = 'https://github.com/hunter04d/rusty-yard' }

Here is a simple example:

use rusty_yard::evaluator;

fn main() {
    let result = evaluator::eval_str("10 + 10 * 10").unwrap();

    assert_eq!(110.0, result);
}

You can also define variables using HashMap<String, f64> and evaluate the string with variables:

use std::collections::HashMap;

use rusty_yard::evaluator;

fn main() {

    let mut vars = HashMap::new();
    vars.insert("a".to_owned(), 1.0);
    vars.insert("b".to_owned(), 2.0);
    vars.insert("c".to_owned(), 3.0);
    // vars is mut because macros can modify the content of the map
    let result = evaluator::eval_str_with_vars("a + b * c", &mut vars).unwrap();

    assert_eq!(7.0, result);
}

Custom context

The crate provides Ctx type with allows you to define your own operators / functions:

use std::collections::HashMap;

use rusty_yard::{evaluator, Ctx};
use rusty_yard::operators::UOp;

fn main() {

    let mut vars = HashMap::new();
    // default ctx with operators one might expect for shunting yard
    let mut ctx = Ctx::default();
    // add unary '$$$' operator with some action
    ctx.u_ops.push(UOp {
        token: "$$$".to_owned(),
        func: |v| v * 1000.0,
    });
    // vars is mut because macros can modify the content of the map
    let result = evaluator::eval_str_with_vars_and_ctx("$$$42.0", &mut vars, &ctx).unwrap();

    assert_eq!(7.0, result);
}

Macros

An interesting feature of this crate are macros. They allow you to hook into the execution of expression and to anything rust can do.

For example the = operator is defined as a macro:

use std::collections::HashMap;

use rusty_yard::{evaluator, Ctx};
use rusty_yard::operators::UOp;

fn main() {
    let mut vars = HashMap::new();
    // use default ctx with macros
    let ctx = Ctx::default_with_macros();

    // this is why vars is &mut
    // currently, only assign macro is defined
    let result = evaluator::eval_str_with_vars_and_ctx("a = 22.0 + 20.0", &mut vars, &ctx).unwrap();

    // a has been defined as 42.0
    assert_eq!(42.0, vars["a"]);
}

To implement your own macro you need to implement Macro and ParsedMacro trait. See Assign macro for an example.

Note: the macros are even more experimental than the rest of the crate. Implementing your own macros is not recommended at this moment.

Things to do

  • Provide better crate documentation
  • More tests, a lot more tests
  • Allow anything that implements FromStr to be used as primitive
  • Allow customizing definition of id/num in tokenizer
  • Helpers for matching, tokenizing, parsing, executing
  • Think about the macro interface
  • Postfix operators (factorial)

rusty-yard's People

Contributors

hunter04d avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.