GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - lefticus/cons_expr

BranchesTags
Open more actions menu

Latest commit

 

History

154 Commits
154 Commits

Folders and files

NameName
Last commit message
Last commit date
.devcontainer
.devcontainer
 
 
.github
.github
 
 
cmake
cmake
 
 
configured_files
configured_files
 
 
examples
examples
 
 
fuzz_test
fuzz_test
 
 
include/cons_expr
include/cons_expr
 
 
src
src
 
 
test
test
 
 
web
web
 
 
.clang-format
.clang-format
 
 
.clang-tidy
.clang-tidy
 
 
.cmake-format.yaml
.cmake-format.yaml
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.gitlab-ci.yml
.gitlab-ci.yml
 
 
.lgtm.yml
.lgtm.yml
 
 
CLAUDE.md
CLAUDE.md
 
 
CMakeLists.txt
CMakeLists.txt
 
 
CMakePresets.json
CMakePresets.json
 
 
Dependencies.cmake
Dependencies.cmake
 
 
LICENSE
LICENSE
 
 
ProjectOptions.cmake
ProjectOptions.cmake
 
 
README.md
README.md
 
 
README_building.md
README_building.md
 
 
README_dependencies.md
README_dependencies.md
 
 
README_docker.md
README_docker.md
 
 
TODO.md
TODO.md
 
 
build_and_test_coverage.sh
build_and_test_coverage.sh
 
 

Repository files navigation

cons_expr

ci codecov CodeQL

About cons_expr

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_expr is consteval meaning 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_identifiers family 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.

Examples

Basic Example

It can be as simple as:

#include <cons_expr/cons_expr.hpp>
#include <print>

void greet(std::string_view name) { std::println("Hello {}!", name); }

int main() {
  lefticus::cons_expr<> evaluator;

  // add C++ function
  evaluator.add<greet>("greet");

  // call C++ function from script
  const auto _ = evaluator.evaluate(R"(
    (greet "Jason")
  )");
}

Play with this in Compiler Explorer.

Calling Script Functions from C++

#include <cons_expr/cons_expr.hpp>
#include <iostream>

int main() {
  lefticus::cons_expr<> evaluator;

  // you should check this for errors
  [[maybe_unused]] const auto result = evaluator.evaluate(R"(
    (define math
      (lambda (x y) 
        (- (* x y) (+ x y))
      )
    )
  )");  

  // we can treat script functions as C++ functions
  auto func = evaluator.make_callable<int (int, int)>("math");

  std::cout << func(evaluator, 5, 4).value();

  // or bind them and make them more natural
  auto math = std::bind_front(func, std::ref(evaluator));

  std::cout << math(5, 4).value();

  // or bind them *by value* and make them self contained
  auto math2 = std::bind_front(func, evaluator);

  std::cout << math2(5, 4).value();
}

Play with this example in Compiler Explorer.

More Complete Bi-Directional Example

#include <cmath>
#include <cons_expr/cons_expr.hpp>
#include <cons_expr/utility.hpp>
#include <numbers>

namespace lefticus {
constexpr double cos(double input) { return std::cos(input); }
}  // namespace lefticus

int main() {
  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) { return std::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)
  const auto 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.hpp
  std::puts(lefticus::to_string(evaluator, true, result).c_str());
}

Play with this example in Compiler Explorer.

And Remember: Everything is constexpr Capable

#include <https://raw.githubusercontent.com/lefticus/cons_expr/refs/heads/develop/include/cons_expr/cons_expr.hpp>

// the entire system is constexpr capable
consteval int do_math(int x, int y) {
  lefticus::cons_expr<> evaluator;

  // you should check this for errors
  [[maybe_unused]] const auto result = evaluator.evaluate(R"(
    (define math
      (lambda (x y) 
        (- (* x y) (+ x y))
      )
    )
  )");  

  auto func = evaluator.make_callable<int (int, int)>("math");
  return func(evaluator, x, y).value();
}

int main() {
  // doing this in a consteval function above guarantees this is done 
  // at compile time.
  return do_math(4, 2);
}

Play with this example in Compiler Explorer.

Command Line Inspection Tool

ccons_expr can be used to execute scripts and inspect the state of the runtime system live

asciicast

Online Builds To Play With The Syntax

  • Main: https://lefticus.github.io/cons_expr/
  • Develop: https://lefticus.github.io/cons_expr/develop/

Important Notes

  • 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.

About

No description, website, or topics provided.

Resources

Readme
MIT license
Activity

Stars

89 stars

Watchers

4 watching

Forks

4 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

Generated from cpp-best-practices/cmake_template
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.