{"id":16838516,"url":"https://github.com/zserge/expr","last_synced_at":"2025-03-17T04:33:27.059Z","repository":{"id":37849542,"uuid":"62868544","full_name":"zserge/expr","owner":"zserge","description":"Fast and lightweight math expression evaluator in C99","archived":false,"fork":false,"pushed_at":"2020-06-05T02:47:19.000Z","size":44,"stargazers_count":124,"open_issues_count":6,"forks_count":23,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-14T12:23:36.749Z","etag":null,"topics":["arithmetic","c","embedded","evaluation","expr"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zserge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-08T07:36:32.000Z","updated_at":"2024-10-14T03:09:25.000Z","dependencies_parsed_at":"2022-08-19T22:10:35.259Z","dependency_job_id":null,"html_url":"https://github.com/zserge/expr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zserge%2Fexpr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zserge%2Fexpr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zserge%2Fexpr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zserge%2Fexpr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zserge","download_url":"https://codeload.github.com/zserge/expr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221672136,"owners_count":16861384,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arithmetic","c","embedded","evaluation","expr"],"created_at":"2024-10-13T12:24:06.294Z","updated_at":"2024-10-27T11:59:19.881Z","avatar_url":"https://github.com/zserge.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# expr\n\n[![Build Status](https://travis-ci.org/zserge/expr.svg?branch=master)](https://travis-ci.org/zserge/expr)\n\nExpr is a mathematical expression evaluator written in C. It takes string as\ninput and returns floating-point number as a result.\n\n## Features\n\n* Supports arithmetic, bitwise and logical operators\n* Supports variables\n* Can be extended with custom functions\n* Simple evaluation takes ~50 nanoseconds on an average PC\n* Low memory usage makes it suitable for embedded systems\n* Pure C99 with no external dependencies\n* Good test coverage\n* Easy to understand (~600 LOC in a single header file)\n\n## Example\n\n```c\n#include \"expr.h\"\n\n// Custom function that returns the sum of its two arguments\nstatic float add(struct expr_func *f, vec_expr_t *args, void *c) {\n  float a = expr_eval(\u0026vec_nth(args, 0));\n  float b = expr_eval(\u0026vec_nth(args, 1));\n  return a + b;\n}\n\nstatic struct expr_func user_funcs[] = {\n    {\"add\", add, NULL, 0},\n    {NULL, NULL, NULL, 0},\n};\n\nint main() {\n  const char *s = \"x = 5, add(2, x)\";\n  struct expr_var_list vars = {0};\n  struct expr *e = expr_create(s, strlen(s), \u0026vars, user_funcs);\n  if (e == NULL) {\n    printf(\"Syntax error\");\n    return 1;\n  }\n\n  float result = expr_eval(e);\n  printf(\"result: %f\\n\", result);\n\n  expr_destroy(e, \u0026vars);\n  return 0;\n}\n```\n\nOutput: `result: 7.000000`\n\n## API\n\n`struct expr *expr_create(const char *s, size_t len, struct expr_var_list\n*vars, struct expr_func *funcs)` - returns compiled expression from the given\nstring. If expression uses variables - they are bound to `vars`, so you can\nmodify values before evaluation or check the results after the evaluation.\n\n`float expr_eval(struct expr *e)` - evaluates compiled expression.\n\n`void expr_destroy(struct expr *e, struct expr_var_list *vars)` - cleans up\nmemory. Parameters can be NULL (e.g. if you want to clean up expression, but\nreuse variables for another expression).\n\n`struct expr_var *expr_var(struct expr_var *vars, const char *s, size_t len)` -\nreturns/creates variable of the given name in the given list. This can be used\nto get variable references to get/set them manually.\n\n## Supported operators\n\n* Arithmetics: `+`, `-`, `*`, `/`, `%` (remainder), `**` (power)\n* Bitwise: `\u003c\u003c`, `\u003e\u003e`, `\u0026`, `|`, `^` (xor or unary bitwise negation)\n* Logical: `\u003c`, `\u003e`, `==`, `!=`, `\u003c=`, `\u003e=`, `\u0026\u0026`, `||`, `!` (unary not)\n* Other: `=` (assignment, e.g. `x=y=5`), `,` (separates expressions or function parameters)\n\nOnly the following functions from libc are used to reduce the footprint and\nmake it easier to use:\n\n* calloc, realloc and free - memory management\n* isnan, isinf, fmodf, powf - math operations\n* strlen, strncmp, strncpy, strtof - tokenizing and parsing\n\n## Running tests\n\nTo run all the tests and benchmarks do `make test`. This will be using your\ndefault compiler and will do no code coverage.\n\nTo see the code coverage you may either do `make llvm-cov` or `make gcov`\ndepending on whether you use GCC or LLVM/Clang.\n\nSince people may have different compiler versions, one may specify a version\nexplicitly, e.g. `make llvm-cov LLVM_VER=-3.8` or `make gcov GCC_VER=-5`.\n\n## License\n\nCode is distributed under MIT license, feel free to use it in your proprietary\nprojects as well.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzserge%2Fexpr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzserge%2Fexpr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzserge%2Fexpr/lists"}