lmcode-cpp

lmcode-cpp logo

lmcode-cpp is the experimental C++17 header-only runtime for lmscript _lmcode_ bytecode: loader, verifier, VM and host registry in include/dslvm.hpp. Every default Registry provides the 226 signatures of the lmcode_std_v3 standard catalog, including the standard REST and CLI interfaces.

Install and build

git clone https://github.com/lmctl-ai/lmcode-cpp
g++ -std=c++17 -Ilmcode-cpp/include host.cpp -o host
make test
make conformance

There is no compiled library and no third-party dependency. The runtime reads bytecode version 6 only; use a matching lmscript compiler from the published @lmctl-ai/lmctl npm package.

Host protocol

CommandEffect
HOST --lmcode-schema --jsonPrints the host schema JSON that lmscript compiles against.
HOST --lmcode-signaturesPrints the registry's sorted canonical signatures, host and standard.
HOST --lmcode FILE.dslbLoads and runs a compiled program file.
lmscript --lmcode-signatures schema.jsonCompiler-side signatures to diff against the host.
Exit codes0 success, 2 command-line failure, 3 load or run failure, 4 application failure.

In lmscript a block is written prog = _lmcode_ cli="HOST" install="github:lmctl-ai/HOST@latest"; ... end _lmcode_ and executed with r = run_lmcode(prog), which returns ok and result.

C++ API quick reference

NameRole
dslvm::RegistryDeclares context, function and method bindings with schema type strings; includes the standard catalog unless built with StdMode::Disabled.
Registry::context, method, functionBind host context variables, methods and functions.
Registry::signatures()Sorted canonical signatures for the parity check.
dslvm::Program::load(bytecode, schema, registry)Verifies the file against the exact schema bytes; refuses before anything runs.
Program::run(context, registry)Runs the body.
Program::entries(), Program::call(i, context, registry, args)Lists and invokes annotated rule entries, the mechanism behind REST and CLI handlers.
dslvm::ContextMaps context names to live host objects; set_dry(true) selects dry execution.
dslvm::ValueINT, FLOAT, STR, BOOL or OBJ; Value::none() for void.
dslvm::ErrorLoad, run and host failures.

Minimal host

#include "dslvm.hpp"
struct Greeter { std::string last; };

int main(int argc, char** argv) {
  dslvm::Registry reg;
  reg.context("g", "Greeter");
  reg.method("Greeter", "say", {"string"}, "void", [](void* self, dslvm::Args& a) {
    static_cast<Greeter*>(self)->last = a[0].s; return dslvm::Value::none(); });

  dslvm::Program program;
  try { program = dslvm::Program::load(dslvm::read_file(argv[1]), dslvm::read_file(argv[2]), reg); }
  catch (const dslvm::Error& e) { std::cerr << "refused: " << e.what() << "\n"; return 3; }

  Greeter greeter;
  dslvm::Context ctx; ctx.set("g", &greeter);
  try { program.run(ctx, reg); } catch (const dslvm::Error& e) { std::cerr << "dsl error: " << e.what() << "\n"; return 3; }
  std::cout << greeter.last << "\n";
}

Standard catalog and REST/CLI interfaces

The canonical std/lmcode-std.schema.json (lmcode_std_v3, std.lock version 3) holds 226 signatures in ten groups: string, math, collections, containers, json, encoding, file, result, testing and rest. Hosts do not register these; keep host schemas limited to host calls.

ItemNames
Interfaces, each (RestExchange) -> voidRestGet, RestPost, RestPut, RestDelete, RestPatch, RestHead, RestOptions, RestHandler, CliCommand, CliOption
Functionsrest_config(string, string) -> void, rest_route(string, string, string) -> void, rest_serve() -> void, rest_stop() -> void, rest_cli(StrList) -> string
RestExchange request methodsmethod, path, query, has_query, param, header, has_header, body
RestExchange response methodsstatus, set_header, send, json
RestExchange shared state methodsget, set, increment
Route templates{param} and {rest...}, or :param and :rest...

Requests are HTTP/1.1 read from an input stream and answered on an output stream; there is no network listener. rest_cli is a stateless CLI mode that dispatches an argument list to options, commands and REST routes.

More