Code Monkey home page Code Monkey logo

weval's Introduction

About

weval was made with the intent of having a portable and reliable eval function, the library can currently run on wasm (wasm-bindgen) and natively pretty much everywhere since the number of third party dependencies other than rust is almost absent.

as of now the library does not support unary operators and is not customizable.

as of version 0.1.1 the library can be considered usable in the sense that it won't crash in your face as long as you input a valid expression, but a syntax checker and unary operators support has to yet be introduced, do not expect API stability yet though.

Later, if I find the time I also plan to implement support for higher precision decimals such as Decimal128 and maybe support for arbitrary operators (as functions).

Build

  1. Install rust
  2. Install wasm-pack: cargo install wasm-pack
  3. Install cargo-make cargo install cargo-make
  4. then build using cargo make web or cargo make node

Usage

once built just get the js package, import then in your web project as a module like this

import init, { eval_float64 } from './pkg-web/weval.js';
await init();
let num = eval_float64('33+454*(2**3)');
console.log(num);

you may also look into index.html for more insight, some functions like the tokenizer are also exposed, though it is more for educational purposes if you will

here how you may convert an expression from infix to postfix:

import init, { eval_float64, tokenizer, infix_to_postfix_tokens } from './pkg-web/weval.js';
await init();
let expression = '(-34 + 2**3) * 3 + 2'

let tokens = tokenizer(expression);

//execute the shunting yard algorithm on the tokens
let postfixTokens = infix_to_postfix_tokens(tokens)

console.log('look mum, i made a postfix expression: ',postfixTokens.join(' '))

note that in order to have the demo, it being index.html work you have to build the library for web first.

have fun!

weval's People

Contributors

gabrielesilinic 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.