Skip to content

pourplusquoi/evalexpr-port

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Porting Rust evalexpr Library to C++

Document for Rust library: evalexpr.

Example

#include <iostream>

#include "port.h"

using namespace evalexpr;

int main() {
    auto expr = port::parse("a + b * f(c)");
    if (!expr) {
        return -1;
    }

    auto ctx = port::make_context();
    ctx->set_int("a", 1);
    ctx->set_int("b", 2);
    ctx->set_string("c", "hello");
    ctx->set_function("f", [](const port::Value& v) -> port::ValueOr {
        auto [sv, ok] = v.get_string();
        if (!ok) {
            return port::ValueOr::empty();
        }
        return port::ValueOr::from_int(sv->length);
    });

    auto [res, ok2] = expr->eval_int(*ctx);
    if (!ok2) {
        return -1;
    }

    // prints: answer = 11
    std::cout << "answer = " << res << std::endl;
    return 0;
}

About

A powerful expression evaluation library in C++.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published