GithubHelp home page GithubHelp logo

njlr / expr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zserge/expr

0.0 2.0 0.0 40 KB

Fast and lightweight math expression evaluator in C99

License: MIT License

Makefile 2.55% C 96.68% Python 0.77%

expr's Introduction

expr

Build Status

Expr is a mathematical expression evaluator written in C. It takes string as input and returns floating-point number as a result.

Features

  • Supports arithmetic, bitwise and logical operators
  • Supports variables
  • Can be extended with custom functions
  • Simple evaluation takes ~50 nanoseconds on an average PC
  • Low memory usage makes it suitable for embedded systems
  • Pure C99 with no external dependencies
  • Good test coverage
  • Easy to understand (~600 LOC in a single header file)

Example

#include "expr.h"

// Custom function that returns the sum of its two arguments
static float add(struct expr_func *f, vec_expr_t args, void *c) {
  float a = expr_eval(&vec_nth(&args, 0));
  float b = expr_eval(&vec_nth(&args, 1));
  return a + b;
}

static struct expr_func funcs[] = {
	{"add", add, 0},
	{NULL, NULL, 0},
};

int main() {
	const char *s = "x = 5, add(2, x)";
  struct expr_var_list vars = {0};
  struct expr *e = expr_create(s, strlen(s), &vars, user_funcs);
  if (e == NULL) {
    printf("Syntax error");
		return 1;
	}

	float result = expr_eval(e);
	printf("result: %f\n", result);

	expr_destroy(e, &vars);
	return 0;
}

Output: result: 7.000000

API

struct expr *expr_create(const char *s, size_t len, struct expr_var_list *vars, struct expr_func *funcs) - returns compiled expression from the given string. If expression uses variables - they are bound to vars, so you can modify values before evaluation or check the results after the evaluation.

float expr_eval(struct expr *e) - evaluates compiled expression.

void expr_destroy(struct expr *e, struct expr_var_list *vars) - cleans up memory. Parameters can be NULL (e.g. if you want to clean up expression, but reuse variables for another expression).

struct expr_var *expr_var(struct expr_var *vars, const char *s, size_t len) - returns/creates variable of the given name in the given list. This can be used to get variable references to get/set them manually.

Supported operators

  • Arithmetics: +, -, *, /, % (remainder), ** (power)
  • Bitwise: <<, >>, &, |, ^ (xor or unary bitwise negation)
  • Logical: <, >, ==, !=, <=, >=, &&, ||, ! (unary not)
  • Other: = (assignment, e.g. x=y=5), , (separates expressions or function parameters)

Only the following functions from libc are used to reduce the footprint and make it easier to use:

  • calloc, realloc and free - memory management
  • isnan, isinf, fmodf, powf - math operations
  • strlen, strncmp, strncpy, strtof - tokenizing and parsing

Running tests

To run all the tests and benchmarks do make test. This will be using your default compiler and will do no code coverage.

To see the code coverage you may either do make llvm-cov or make gcov depending on whether you use GCC or LLVM/Clang.

Since people may have different compiler versions, one may specify a version explicitly, e.g. make llvm-cov LLVM_VER=-3.8 or make gcov GCC_VER=-5.

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

expr's People

Contributors

zserge avatar njlr avatar

Watchers

James Cloos avatar  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.