* Fix #552: Support nested namespaces via dotted names Namespaces can now be nested using dotted name syntax, both from C++ (register_namespace(gen, "constants.si")) and from script (namespace("constants.si")). Parent namespaces are auto-registered when absent, and child namespaces are automatically nested into their parent on import. This allows clean hierarchical organization like constants.si.mu_B instead of flat names like constants_si. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: use :: instead of . as nested namespace separator Switch from dotted names (e.g. "constants.si") to C++-style :: separator (e.g. "constants::si") for nested namespace declarations, both in the C++ API (register_namespace) and in script (namespace()). The original implementation used . because namespace members are accessed via dot notation at runtime (constants.si.mu_B), making the declaration separator match the access syntax. However, :: is more consistent with C++ namespace conventions and aligns with ChaiScript's existing use of :: for method (def Class::method) and attribute (attr Class::attr) declarations. Member access in scripts remains dot-based (constants.si.mu_B) since that is ChaiScript's member access operator. Requested by @lefticus in PR #675 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: C++-style namespace scoping and block declarations Add :: scope resolution operator for member access (ns::func works like ns.func). Add block namespace declarations: namespace x::y { def func() { ... } } Functions and variables declared inside a namespace block are added as members of the namespace, accessible via :: or dot notation. Namespaces can be reopened to add more members, matching C++ behavior. Requested by @lefticus in PR #675 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: extract shared make_proxy_function from Def_AST_Node Namespace_Block_AST_Node was duplicating the entire proxy function creation logic from Def_AST_Node::eval_internal. Extract a static make_proxy_function helper so both nodes share the same code path, eliminating fragile duplication that would drift if Def handling changes. Requested by @lefticus in PR #675 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: reject non-declaration statements inside namespace blocks Only def, var, auto, and global declarations are now allowed inside namespace { } blocks. Arbitrary expressions, assignments, and function calls are rejected with an eval_error. Added compiled tests verifying that expressions, function calls, and assignments are rejected. Requested by @lefticus in PR #675 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: remove -j parameter from unix builds Ninja handles parallelism intelligently on its own; the explicit -j flag was causing memory pressure on sanitizer builds. Windows (non-Ninja) build retains -j. Requested by @lefticus in PR #675 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: leftibot <leftibot@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> |
||
|---|---|---|
| .github | ||
| cmake | ||
| contrib | ||
| emscripten | ||
| grammar | ||
| include/chaiscript | ||
| performance_tests | ||
| samples | ||
| src | ||
| static_libs | ||
| unittests | ||
| .buckconfig | ||
| .clang-format | ||
| .decent_ci-Linux.yaml | ||
| .decent_ci-MacOS.yaml | ||
| .decent_ci-Windows.yaml | ||
| .decent_ci.yaml | ||
| .gitignore | ||
| .travis.yml | ||
| biicode.conf | ||
| BUCK | ||
| buckaroo.json | ||
| cheatsheet.md | ||
| CMakeLists.txt | ||
| description.txt | ||
| DesignGoals.md | ||
| Doxyfile.in | ||
| LICENSE | ||
| license.txt | ||
| readme.md | ||
| releasenotes.md | ||
| supporters.md | ||
ChaiScript
(c) 2009-2012 Jonathan Turner (c) 2009-2017 Jason Turner
Release under the BSD license, see "license.txt" for details.
Introduction
ChaiScript is one of the only embedded scripting language designed from the ground up to directly target C++ and take advantage of modern C++ development techniques, working with the developer how they would expect it to work. Being a native C++ application, it has some advantages over existing embedded scripting languages:
- It uses a header-only approach, which makes it easy to integrate with existing projects.
- It maintains type safety between your C++ application and the user scripts.
- It supports a variety of C++ techniques including callbacks, overloaded functions, class methods, and stl containers.
Requirements
ChaiScript requires a C++17 compiler to build with support for variadic templates. It has been tested with gcc 7 and clang 6 (with libcxx).
Installation using vcpkg
You can download and install ChaiScript using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install chaiscript
The ChaiScript port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
Usage
- Add the ChaiScript include directory to your project's header search path
- Add
#include <chaiscript/chaiscript.hpp>to your source file - Instantiate the ChaiScript engine in your application. For example, create a
new engine with the name
chailike so:chaiscript::ChaiScript chai - The default behavior is to load the ChaiScript standard library from a loadable module. A second option is to compile the library into your code, see below for an example.
Once instantiated, the engine is ready to start running ChaiScript source. You
have two main options for processing ChaiScript source: a line at a time using
chai.eval(string) and a file at a time using chai.eval_file(fname)
To make functions in your C++ code visible to scripts, they must be registered with the scripting engine. To do so, call add:
chai.add(chaiscript::fun(&my_function), "my_function_name");
Once registered the function will be visible to scripts as "my_function_name"
Examples
ChaiScript is similar to ECMAScript (aka JavaScript(tm)), but with some modifications to make it easier to use. For usage examples see the "samples" directory, and for more in-depth look at the language, the unit tests in the "unittests" directory cover the most ground.
For examples of how to register parts of your C++ application, see "example.cpp" in the "samples" directory. Example.cpp is verbose and shows every possible way of working with the library. For further documentation generate the doxygen documentation in the build folder or see the website http://www.chaiscript.com.
Grammar
A formal EBNF grammar for ChaiScript is available in grammar/chaiscript.ebnf. To view it as a railroad diagram, paste the grammar into mingodad's railroad diagram generator or bottlecaps.de/rr.
The shortest complete example possible follows:
/// main.cpp
#include <chaiscript/chaiscript.hpp>
double function(int i, double j)
{
return i * j;
}
int main()
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&function), "function");
double d = chai.eval<double>("function(3, 4.75);");
}

