All Projects → zserge → Expr

zserge / Expr

Licence: mit
Fast and lightweight math expression evaluator in C99

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Expr

Embedded Device Lab
embedded-device-lab是一个利用qemu模拟真实世界中物联网漏洞的测试环境。由于物联网架构的特殊性,调试分析漏洞通常需要使用qemu模拟执行不同架构的可执行文件。而各种搭建环境,交叉编译是一件费事费力,令人忧伤的工作。 embedded-device-lab利用docker-compose,将各种漏洞调试环境一键化。简单使用两条命令,就可以直接使用gdb或者IDA动态分析相关漏洞。
Stars: ✭ 48 (-21.31%)
Mutual labels:  embedded
Pycm
Multi-class confusion matrix library in Python
Stars: ✭ 1,076 (+1663.93%)
Mutual labels:  evaluation
Reed Solomon
Reed Solomon BCH encoder and decoder
Stars: ✭ 57 (-6.56%)
Mutual labels:  embedded
Punchboot
Punchboot
Stars: ✭ 49 (-19.67%)
Mutual labels:  embedded
Wasmjit
Small Embeddable WebAssembly Runtime
Stars: ✭ 1,063 (+1642.62%)
Mutual labels:  embedded
Gdbstub
A simple, dependency-free GDB stub that can be easily dropped in to your project.
Stars: ✭ 56 (-8.2%)
Mutual labels:  embedded
Django Access
Django-Access - the application introducing dynamic evaluation-based instance-level (row-level) access rights control for Django
Stars: ✭ 47 (-22.95%)
Mutual labels:  evaluation
Spiffs
Wear-leveled SPI flash file system for embedded devices
Stars: ✭ 1,105 (+1711.48%)
Mutual labels:  embedded
Mylinux
myLinux is a small UNIX like OS for embedded systems based on Westermo NetBox
Stars: ✭ 53 (-13.11%)
Mutual labels:  embedded
View Finding Network
A deep ranking network that learns to find good compositions in a photograph.
Stars: ✭ 57 (-6.56%)
Mutual labels:  evaluation
Cocoos
A cooperative operating system based on coroutines
Stars: ✭ 50 (-18.03%)
Mutual labels:  embedded
Webfsd
A simple HTTP server for mostly static content written in C
Stars: ✭ 50 (-18.03%)
Mutual labels:  embedded
Qtwebserver
Qt based web application server
Stars: ✭ 56 (-8.2%)
Mutual labels:  embedded
Debootstick
Generate a bootable live image from any Debian/Ubuntu filesystem tree.
Stars: ✭ 48 (-21.31%)
Mutual labels:  embedded
Couchbase Lite C
C language bindings for the Couchbase Lite embedded NoSQL database engine
Stars: ✭ 58 (-4.92%)
Mutual labels:  embedded
Lmchallenge
A library & tools to evaluate predictive language models.
Stars: ✭ 47 (-22.95%)
Mutual labels:  evaluation
Luos
Luos technology main lib
Stars: ✭ 55 (-9.84%)
Mutual labels:  embedded
Awesome Embedded Linux
A curated list of awesome Embedded Linux resources.
Stars: ✭ 60 (-1.64%)
Mutual labels:  embedded
Gobusybox
Tools for compiling many Go commands into one binary to save space. Builds are supported in vendor-based Go, module-based Go, and bazel with Starlark.
Stars: ✭ 60 (-1.64%)
Mutual labels:  embedded
Evalai
☁️ 🚀 📊 📈 Evaluating state of the art in AI
Stars: ✭ 1,087 (+1681.97%)
Mutual labels:  evaluation

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 user_funcs[] = {
    {"add", add, NULL, 0},
    {NULL, 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].