cons_expr is:
- Scheme-inspired
- scheme syntax and features
- does not support pairs or macros
- any value once captured is const
- any redefinition of a value shadows the previous value
- Constexpr-capable
cons_expr::cons_exprisconstevalmeaning that it is always 0-cost to construct a cons_expr interpreter- Any script can be executed at compile time.
- Why execute script at compile time?
- I don't know your other use cases
- But you can use this feature to set up your entire scripting environment at compile time, then execute scripts at runtime
- Simple and small
- I'm attempting to keep the code as compact as possible. As of now it is "feature complete." Any additional features will go as add-ons in the "utility" header
- I'm not happy with the
fix_identifiersfamily of functions. They need to be simplified and made better. - Compilation of entire engine is just a few seconds, even with optimizations.
- Easy to use
- Include one header and go! See examples.
- Embeddable scripting language
- Bi-directional support for functions between script and C++.
- All supported types must be known at compile time.
- For C++23
- Currently only known to work with GCC 13.1.
It can be as simple as:
#include<cons_expr/cons_expr.hpp>
#include<print>voidgreet(std::string_view name) { std::println("Hello {}!", name); }
intmain() {
lefticus::cons_expr<> evaluator;
// add C++ function
evaluator.add<greet>("greet");
// call C++ function from scriptconstauto _ = evaluator.evaluate(R"( (greet "Jason"))");
}Play with this in Compiler Explorer.
#include<cons_expr/cons_expr.hpp>
#include<iostream>intmain() {
lefticus::cons_expr<> evaluator;
// you should check this for errors[[maybe_unused]]constauto result = evaluator.evaluate(R"( (define math (lambda (x y) (- (* x y) (+ x y)) ) ))"); // we can treat script functions as C++ functionsauto func = evaluator.make_callable<int (int, int)>("math");
std::cout << func(evaluator, 5, 4).value();
// or bind them and make them more naturalauto math = std::bind_front(func, std::ref(evaluator));
std::cout << math(5, 4).value();
// or bind them *by value* and make them self containedauto math2 = std::bind_front(func, evaluator);
std::cout << math2(5, 4).value();
}Play with this example in Compiler Explorer.
#include<cmath>
#include<cons_expr/cons_expr.hpp>
#include<cons_expr/utility.hpp>
#include<numbers>namespacelefticus {
constexprdoublecos(double input) { returnstd::cos(input); }
} // namespace lefticusintmain() {
lefticus::cons_expr<> evaluator;
// adding a function
evaluator.add<lefticus::cos>("cos");
// adding a lambda, not the + to force it into a // function pointer
evaluator.add<+[](double input) { returnstd::sin(input); }>("sin");
// adding a global
evaluator.add("pi", std::numbers::pi_v<double>);
// calling the functions I just added// (floating point isn't exact :D)constauto result = evaluator.evaluate("(+ (cos pi) (sin pi))");
// if the above had an error then we'd get a pretty-print// of it with this helper function// using the to_string helper from the utility.hppstd::puts(lefticus::to_string(evaluator, true, result).c_str());
}Play with this example in Compiler Explorer.
#include<https://raw.githubusercontent.com/lefticus/cons_expr/refs/heads/develop/include/cons_expr/cons_expr.hpp>// the entire system is constexpr capableconstevalintdo_math(int x, int y) {
lefticus::cons_expr<> evaluator;
// you should check this for errors[[maybe_unused]]constauto result = evaluator.evaluate(R"( (define math (lambda (x y) (- (* x y) (+ x y)) ) ))"); auto func = evaluator.make_callable<int (int, int)>("math");
returnfunc(evaluator, x, y).value();
}
intmain() {
// doing this in a consteval function above guarantees this is done // at compile time.returndo_math(4, 2);
}Play with this example in Compiler Explorer.
ccons_expr can be used to execute scripts and inspect the state of the runtime system live
- Objects are never destroyed, but because they are treated as immutable, they are reused as much as possible.
- All types contained in the script are required to be
trivial - If you expand beyond the statically allocated storage, dynamic storage is utilized. If you maintain dynamic storage from compile-time to run-time, you will get a compile time error.