Skip to content

lindig/expr

Repository files navigation

Build

Expr

A simple OCaml library to evaluate an arithmetic expression. This allows an application to accept expressions rather than just literal values from the command line or any other input:

    match Expr.Eval.simple "3.0 * 12" with
    | Expr.Eval.Float x -> Printf.printf "%f" x
    | _
    | exception _ -> failwith "something went wrong"
$ make utop
utop # Expr.Eval.simple "3+4*5 == 23";;
- : Expr.Eval.value = Expr.Eval.Bool true

utop # let env = Expr.Eval.env ["pi", Float.pi] in
  Expr.Eval.string env "pi * pi";;
- : Expr.Eval.value = Expr.Eval.Float 9.86960440108935799

Features:

  • Floating point and boolean expressions
  • Expressions may contain floating point variables
  • Proper precedence and associativity

CLI

This code is intended as a library but includes a minimal command-line binary that accept the string to evaluate as an argument:

./_build/default/bin/main.exe "3.0 * 3.0 * pi"
28.274334

Installation

The code is so simple that I would suggest to copy the lib/ directory and use it and not bother with an opam installation:

$ opam install expr

or for the development version

$ opam pin add expr https://github.com/lindig/expr.git

once this package has been merged into the official Opam repository.

Documentation

To be done; but take a look at:

  • eval.mli for the API
  • parser.mly for the grammar
  • scammer.mll for the syntax of tokens
$ make utop
utop # let eval = Expr.Eval.simple;;
val eval : string -> Expr.Eval.value = <fun>

utop # eval "3+4*4";;
- : Expr.Eval.value = Expr.Eval.Float 19.

utop # eval "-3 < 0";;
- : Expr.Eval.value = Expr.Eval.Bool true

utop # eval "(5+3)*3";;
- : Expr.Eval.value = Expr.Eval.Float 24.

utop # eval "4 = [0,5]";;
- : Expr.Eval.value = Expr.Eval.Bool true

Contribute

If you find this useful, please contribute back by raising pull requests for improvements you made.

About

Simple arithmetic expression parser and evaluation library

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published