diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..e4ea543 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitea/workflows/sdk-release.yaml b/.gitea/workflows/sdk-release.yaml new file mode 100644 index 0000000..72e3e24 --- /dev/null +++ b/.gitea/workflows/sdk-release.yaml @@ -0,0 +1,166 @@ +name: El SDK Release + +on: + push: + branches: + - main + +jobs: + build-and-release: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install build dependencies + run: | + apt-get update -qq + apt-get install -y gcc libcurl4-openssl-dev + + # Gen2: compile the bootstrap C source into a working elc binary + - name: Build elc from bootstrap (gen2) + run: | + gcc -O2 \ + -I el-compiler/runtime \ + dist/elc-bootstrap.c \ + el-compiler/runtime/el_runtime.c \ + -lcurl -lpthread \ + -o dist/elc-gen2 + chmod +x dist/elc-gen2 + echo "gen2 elc built" + dist/elc-gen2 --version || true + + # Gen3: use gen2 to compile the El compiler from its own El source (self-host) + - name: Self-host: compile El compiler with gen2 (gen3) + run: | + mkdir -p dist/platform + dist/elc-gen2 el-compiler/src/compiler.el > dist/elc-gen3.c + gcc -O2 \ + -I el-compiler/runtime \ + dist/elc-gen3.c \ + el-compiler/runtime/el_runtime.c \ + -lcurl -lpthread \ + -o dist/platform/elc + chmod +x dist/platform/elc + echo "gen3 (self-hosted) elc built" + dist/platform/elc --version || true + + # Run all four test suites with gen3 elc + - name: Run tests — text + run: | + ELC="$(pwd)/dist/platform/elc" \ + EL_HOME="$(pwd)" \ + bash tests/text/run.sh + + - name: Run tests — calendar + run: | + ELC="$(pwd)/dist/platform/elc" \ + EL_HOME="$(pwd)" \ + bash tests/calendar/run.sh + + - name: Run tests — time + run: | + ELC="$(pwd)/dist/platform/elc" \ + EL_HOME="$(pwd)" \ + bash tests/time/run.sh + + - name: Run tests — html_sanitizer + run: | + ELC="$(pwd)/dist/platform/elc" \ + EL_HOME="$(pwd)" \ + bash tests/html_sanitizer/run.sh + + # Publish / update the `latest` release with the three SDK assets + - name: Publish latest release + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + GITEA_API: https://git.neuralplatform.ai/api/v1 + REPO: neuron-technologies/el + run: | + # Delete existing `latest` release if it exists + EXISTING_ID=$(curl -sf \ + -H "Authorization: token ${GITEA_TOKEN}" \ + "${GITEA_API}/repos/${REPO}/releases/tags/latest" \ + | python3 -c "import sys,json; d=json.load(sys.stdin); print(d['id'])" 2>/dev/null || true) + + if [ -n "${EXISTING_ID}" ]; then + echo "Deleting existing release id=${EXISTING_ID}" + curl -sf -X DELETE \ + -H "Authorization: token ${GITEA_TOKEN}" \ + "${GITEA_API}/repos/${REPO}/releases/${EXISTING_ID}" + fi + + # Delete and re-create the `latest` tag so it points at HEAD + curl -sf -X DELETE \ + -H "Authorization: token ${GITEA_TOKEN}" \ + "${GITEA_API}/repos/${REPO}/tags/latest" || true + + # Create the release + RELEASE_ID=$(curl -sf -X POST \ + -H "Authorization: token ${GITEA_TOKEN}" \ + -H "Content-Type: application/json" \ + "${GITEA_API}/repos/${REPO}/releases" \ + -d "{ + \"tag_name\": \"latest\", + \"name\": \"El SDK (latest)\", + \"body\": \"Latest El SDK build from commit ${GITHUB_SHA}.\nBuilt $(date -u +%Y-%m-%dT%H:%M:%SZ).\", + \"draft\": false, + \"prerelease\": false + }" | python3 -c "import sys,json; print(json.load(sys.stdin)['id'])") + + echo "Created release id=${RELEASE_ID}" + + # Upload assets + upload_asset() { + local filepath="$1" + local name="$2" + echo "Uploading ${name}..." + curl -sf -X POST \ + -H "Authorization: token ${GITEA_TOKEN}" \ + -F "attachment=@${filepath};filename=${name}" \ + "${GITEA_API}/repos/${REPO}/releases/${RELEASE_ID}/assets" + } + + upload_asset dist/platform/elc elc + upload_asset el-compiler/runtime/el_runtime.c el_runtime.c + upload_asset el-compiler/runtime/el_runtime.h el_runtime.h + + echo "Release published successfully" + + # Dispatch el-sdk-updated event to downstream repos + - name: Dispatch to foundation/engram + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + GITEA_API: https://git.neuralplatform.ai/api/v1 + run: | + curl -sf -X POST \ + -H "Authorization: token ${GITEA_TOKEN}" \ + -H "Content-Type: application/json" \ + "${GITEA_API}/repos/neuron-technologies/engram/dispatches" \ + -d "{ + \"type\": \"el-sdk-updated\", + \"inputs\": { + \"el_version\": \"latest\", + \"commit\": \"${GITHUB_SHA}\" + } + }" + echo "Dispatched el-sdk-updated to foundation/engram" + + - name: Dispatch to neuron-technologies/forge + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + GITEA_API: https://git.neuralplatform.ai/api/v1 + run: | + curl -sf -X POST \ + -H "Authorization: token ${GITEA_TOKEN}" \ + -H "Content-Type: application/json" \ + "${GITEA_API}/repos/neuron-technologies/forge/dispatches" \ + -d "{ + \"type\": \"el-sdk-updated\", + \"inputs\": { + \"el_version\": \"latest\", + \"commit\": \"${GITHUB_SHA}\" + } + }" + echo "Dispatched el-sdk-updated to neuron-technologies/forge" diff --git a/dist/elc-bootstrap.c b/dist/elc-bootstrap.c new file mode 100644 index 0000000..c5a042d --- /dev/null +++ b/dist/elc-bootstrap.c @@ -0,0 +1,4793 @@ +#include +#include +#include "el_runtime.h" + +el_val_t is_digit(el_val_t ch); +el_val_t is_alpha(el_val_t ch); +el_val_t is_alnum_or_underscore(el_val_t ch); +el_val_t is_whitespace(el_val_t ch); +el_val_t make_tok(el_val_t kind, el_val_t value); +el_val_t keyword_kind(el_val_t word); +el_val_t scan_digits(el_val_t chars, el_val_t start, el_val_t total); +el_val_t scan_ident(el_val_t chars, el_val_t start, el_val_t total); +el_val_t scan_string(el_val_t chars, el_val_t start, el_val_t total); +el_val_t lex(el_val_t source); +el_val_t tok_at(el_val_t tokens, el_val_t pos); +el_val_t tok_kind(el_val_t tokens, el_val_t pos); +el_val_t tok_value(el_val_t tokens, el_val_t pos); +el_val_t expect(el_val_t tokens, el_val_t pos, el_val_t kind); +el_val_t make_result(el_val_t node, el_val_t pos); +el_val_t skip_type(el_val_t tokens, el_val_t pos); +el_val_t parse_params(el_val_t tokens, el_val_t pos); +el_val_t parse_primary(el_val_t tokens, el_val_t pos); +el_val_t parse_if(el_val_t tokens, el_val_t pos); +el_val_t parse_match(el_val_t tokens, el_val_t pos); +el_val_t parse_pattern(el_val_t tokens, el_val_t pos); +el_val_t parse_for_expr(el_val_t tokens, el_val_t pos); +el_val_t parse_block(el_val_t tokens, el_val_t pos); +el_val_t parse_postfix(el_val_t tokens, el_val_t pos); +el_val_t op_precedence(el_val_t kind); +el_val_t is_binop(el_val_t kind); +el_val_t parse_binop(el_val_t tokens, el_val_t pos, el_val_t min_prec); +el_val_t parse_expr(el_val_t tokens, el_val_t pos); +el_val_t parse_stmt(el_val_t tokens, el_val_t pos); +el_val_t parse(el_val_t tokens); +el_val_t c_escape(el_val_t s); +el_val_t c_str_lit(el_val_t s); +el_val_t el_type_to_c(el_val_t type_str); +el_val_t emit_line(el_val_t line); +el_val_t emit_blank(void); +el_val_t binop_to_c(el_val_t op); +el_val_t cg_expr(el_val_t expr); +el_val_t next_match_id(void); +el_val_t cg_match(el_val_t expr); +el_val_t next_if_id(void); +el_val_t cg_if_expr_arm(el_val_t stmts, el_val_t result_var); +el_val_t cg_if_expr(el_val_t expr); +el_val_t list_contains(el_val_t lst, el_val_t s); +el_val_t cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared); +el_val_t strip_outer_parens(el_val_t s); +el_val_t cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared); +el_val_t cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared); +el_val_t param_decl(el_val_t param, el_val_t idx); +el_val_t params_to_c(el_val_t params); +el_val_t transform_implicit_return(el_val_t body); +el_val_t is_int_name(el_val_t name); +el_val_t is_int_call(el_val_t call_expr); +el_val_t is_int_expr(el_val_t expr); +el_val_t cap_record_violation(el_val_t kind, el_val_t fn_name); +el_val_t is_self_formation_call(el_val_t fn_name); +el_val_t is_dharma_call(el_val_t fn_name); +el_val_t is_llm_call(el_val_t fn_name); +el_val_t cap_check_call(el_val_t fn_name); +el_val_t emit_cap_violations(void); +el_val_t builtin_arity(el_val_t name); +el_val_t arity_record_violation(el_val_t fn_name, el_val_t expected, el_val_t actual); +el_val_t arity_check_call(el_val_t fn_name, el_val_t actual); +el_val_t emit_arity_violations(void); +el_val_t add_int_name(el_val_t name); +el_val_t build_int_names_for_params(el_val_t params); +el_val_t cg_fn(el_val_t stmt); +el_val_t is_fndef(el_val_t stmt); +el_val_t is_top_level_decl(el_val_t stmt); +el_val_t cgi_arg(el_val_t value, el_val_t has_value); +el_val_t vbd_is_restricted_name(el_val_t name); +el_val_t vbd_expr_has_restricted_call(el_val_t expr); +el_val_t vbd_has_restricted_call(el_val_t stmts); +el_val_t codegen(el_val_t stmts, el_val_t source); +el_val_t js_escape(el_val_t s); +el_val_t js_str_lit(el_val_t s); +el_val_t js_emit_line(el_val_t line); +el_val_t js_emit_blank(void); +el_val_t js_binop(el_val_t op); +el_val_t js_is_int_name(el_val_t name); +el_val_t js_add_int_name(el_val_t name); +el_val_t js_build_int_names_for_params(el_val_t params); +el_val_t js_is_int_call(el_val_t call_expr); +el_val_t js_cg_expr(el_val_t expr); +el_val_t js_next_match_id(void); +el_val_t js_cg_match(el_val_t expr); +el_val_t js_list_contains(el_val_t lst, el_val_t s); +el_val_t js_cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared); +el_val_t js_strip_outer_parens(el_val_t s); +el_val_t js_cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t js_cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared); +el_val_t js_cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t js_cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared); +el_val_t js_params_str(el_val_t params); +el_val_t js_transform_implicit_return(el_val_t body); +el_val_t js_cg_fn(el_val_t stmt); +el_val_t js_is_fndef(el_val_t stmt); +el_val_t js_is_top_level_decl(el_val_t stmt); +el_val_t codegen_js(el_val_t stmts, el_val_t source); +el_val_t compile(el_val_t source); +el_val_t compile_js(el_val_t source); +el_val_t compile_dispatch(el_val_t tgt, el_val_t source); +el_val_t detect_target(el_val_t argv); +el_val_t strip_flags(el_val_t argv); + +el_val_t _argv; +el_val_t _target; +el_val_t _positional; +el_val_t _argc; + +el_val_t is_digit(el_val_t ch) { + if (str_eq(ch, EL_STR("0"))) { + return 1; + } + if (str_eq(ch, EL_STR("1"))) { + return 1; + } + if (str_eq(ch, EL_STR("2"))) { + return 1; + } + if (str_eq(ch, EL_STR("3"))) { + return 1; + } + if (str_eq(ch, EL_STR("4"))) { + return 1; + } + if (str_eq(ch, EL_STR("5"))) { + return 1; + } + if (str_eq(ch, EL_STR("6"))) { + return 1; + } + if (str_eq(ch, EL_STR("7"))) { + return 1; + } + if (str_eq(ch, EL_STR("8"))) { + return 1; + } + if (str_eq(ch, EL_STR("9"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_alpha(el_val_t ch) { + if (str_eq(ch, EL_STR("a"))) { + return 1; + } + if (str_eq(ch, EL_STR("b"))) { + return 1; + } + if (str_eq(ch, EL_STR("c"))) { + return 1; + } + if (str_eq(ch, EL_STR("d"))) { + return 1; + } + if (str_eq(ch, EL_STR("e"))) { + return 1; + } + if (str_eq(ch, EL_STR("f"))) { + return 1; + } + if (str_eq(ch, EL_STR("g"))) { + return 1; + } + if (str_eq(ch, EL_STR("h"))) { + return 1; + } + if (str_eq(ch, EL_STR("i"))) { + return 1; + } + if (str_eq(ch, EL_STR("j"))) { + return 1; + } + if (str_eq(ch, EL_STR("k"))) { + return 1; + } + if (str_eq(ch, EL_STR("l"))) { + return 1; + } + if (str_eq(ch, EL_STR("m"))) { + return 1; + } + if (str_eq(ch, EL_STR("n"))) { + return 1; + } + if (str_eq(ch, EL_STR("o"))) { + return 1; + } + if (str_eq(ch, EL_STR("p"))) { + return 1; + } + if (str_eq(ch, EL_STR("q"))) { + return 1; + } + if (str_eq(ch, EL_STR("r"))) { + return 1; + } + if (str_eq(ch, EL_STR("s"))) { + return 1; + } + if (str_eq(ch, EL_STR("t"))) { + return 1; + } + if (str_eq(ch, EL_STR("u"))) { + return 1; + } + if (str_eq(ch, EL_STR("v"))) { + return 1; + } + if (str_eq(ch, EL_STR("w"))) { + return 1; + } + if (str_eq(ch, EL_STR("x"))) { + return 1; + } + if (str_eq(ch, EL_STR("y"))) { + return 1; + } + if (str_eq(ch, EL_STR("z"))) { + return 1; + } + if (str_eq(ch, EL_STR("A"))) { + return 1; + } + if (str_eq(ch, EL_STR("B"))) { + return 1; + } + if (str_eq(ch, EL_STR("C"))) { + return 1; + } + if (str_eq(ch, EL_STR("D"))) { + return 1; + } + if (str_eq(ch, EL_STR("E"))) { + return 1; + } + if (str_eq(ch, EL_STR("F"))) { + return 1; + } + if (str_eq(ch, EL_STR("G"))) { + return 1; + } + if (str_eq(ch, EL_STR("H"))) { + return 1; + } + if (str_eq(ch, EL_STR("I"))) { + return 1; + } + if (str_eq(ch, EL_STR("J"))) { + return 1; + } + if (str_eq(ch, EL_STR("K"))) { + return 1; + } + if (str_eq(ch, EL_STR("L"))) { + return 1; + } + if (str_eq(ch, EL_STR("M"))) { + return 1; + } + if (str_eq(ch, EL_STR("N"))) { + return 1; + } + if (str_eq(ch, EL_STR("O"))) { + return 1; + } + if (str_eq(ch, EL_STR("P"))) { + return 1; + } + if (str_eq(ch, EL_STR("Q"))) { + return 1; + } + if (str_eq(ch, EL_STR("R"))) { + return 1; + } + if (str_eq(ch, EL_STR("S"))) { + return 1; + } + if (str_eq(ch, EL_STR("T"))) { + return 1; + } + if (str_eq(ch, EL_STR("U"))) { + return 1; + } + if (str_eq(ch, EL_STR("V"))) { + return 1; + } + if (str_eq(ch, EL_STR("W"))) { + return 1; + } + if (str_eq(ch, EL_STR("X"))) { + return 1; + } + if (str_eq(ch, EL_STR("Y"))) { + return 1; + } + if (str_eq(ch, EL_STR("Z"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_alnum_or_underscore(el_val_t ch) { + if (is_digit(ch)) { + return 1; + } + if (is_alpha(ch)) { + return 1; + } + if (str_eq(ch, EL_STR("_"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_whitespace(el_val_t ch) { + if (str_eq(ch, EL_STR(" "))) { + return 1; + } + if (str_eq(ch, EL_STR("\t"))) { + return 1; + } + if (str_eq(ch, EL_STR("\n"))) { + return 1; + } + if (str_eq(ch, EL_STR("\r"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t make_tok(el_val_t kind, el_val_t value) { + return el_map_new(2, "kind", kind, "value", value); + return 0; +} + +el_val_t keyword_kind(el_val_t word) { + if (str_eq(word, EL_STR("let"))) { + return EL_STR("Let"); + } + if (str_eq(word, EL_STR("fn"))) { + return EL_STR("Fn"); + } + if (str_eq(word, EL_STR("type"))) { + return EL_STR("Type"); + } + if (str_eq(word, EL_STR("enum"))) { + return EL_STR("Enum"); + } + if (str_eq(word, EL_STR("match"))) { + return EL_STR("Match"); + } + if (str_eq(word, EL_STR("return"))) { + return EL_STR("Return"); + } + if (str_eq(word, EL_STR("if"))) { + return EL_STR("If"); + } + if (str_eq(word, EL_STR("else"))) { + return EL_STR("Else"); + } + if (str_eq(word, EL_STR("for"))) { + return EL_STR("For"); + } + if (str_eq(word, EL_STR("in"))) { + return EL_STR("In"); + } + if (str_eq(word, EL_STR("while"))) { + return EL_STR("While"); + } + if (str_eq(word, EL_STR("import"))) { + return EL_STR("Import"); + } + if (str_eq(word, EL_STR("from"))) { + return EL_STR("From"); + } + if (str_eq(word, EL_STR("as"))) { + return EL_STR("As"); + } + if (str_eq(word, EL_STR("with"))) { + return EL_STR("With"); + } + if (str_eq(word, EL_STR("sealed"))) { + return EL_STR("Sealed"); + } + if (str_eq(word, EL_STR("activate"))) { + return EL_STR("Activate"); + } + if (str_eq(word, EL_STR("where"))) { + return EL_STR("Where"); + } + if (str_eq(word, EL_STR("test"))) { + return EL_STR("Test"); + } + if (str_eq(word, EL_STR("seed"))) { + return EL_STR("Seed"); + } + if (str_eq(word, EL_STR("assert"))) { + return EL_STR("Assert"); + } + if (str_eq(word, EL_STR("protocol"))) { + return EL_STR("Protocol"); + } + if (str_eq(word, EL_STR("impl"))) { + return EL_STR("Impl"); + } + if (str_eq(word, EL_STR("retry"))) { + return EL_STR("Retry"); + } + if (str_eq(word, EL_STR("times"))) { + return EL_STR("Times"); + } + if (str_eq(word, EL_STR("fallback"))) { + return EL_STR("Fallback"); + } + if (str_eq(word, EL_STR("reason"))) { + return EL_STR("Reason"); + } + if (str_eq(word, EL_STR("parallel"))) { + return EL_STR("Parallel"); + } + if (str_eq(word, EL_STR("trace"))) { + return EL_STR("Trace"); + } + if (str_eq(word, EL_STR("requires"))) { + return EL_STR("Requires"); + } + if (str_eq(word, EL_STR("deploy"))) { + return EL_STR("Deploy"); + } + if (str_eq(word, EL_STR("to"))) { + return EL_STR("To"); + } + if (str_eq(word, EL_STR("via"))) { + return EL_STR("Via"); + } + if (str_eq(word, EL_STR("target"))) { + return EL_STR("Target"); + } + if (str_eq(word, EL_STR("true"))) { + return EL_STR("Bool"); + } + if (str_eq(word, EL_STR("false"))) { + return EL_STR("Bool"); + } + if (str_eq(word, EL_STR("cgi"))) { + return EL_STR("Cgi"); + } + if (str_eq(word, EL_STR("service"))) { + return EL_STR("Service"); + } + if (str_eq(word, EL_STR("manager"))) { + return EL_STR("Manager"); + } + if (str_eq(word, EL_STR("engine"))) { + return EL_STR("Engine"); + } + if (str_eq(word, EL_STR("accessor"))) { + return EL_STR("Accessor"); + } + if (str_eq(word, EL_STR("vessel"))) { + return EL_STR("Vessel"); + } + return EL_STR(""); + return 0; +} + +el_val_t scan_digits(el_val_t chars, el_val_t start, el_val_t total) { + el_val_t i = start; + el_val_t text = EL_STR(""); + el_val_t running = 1; + while (running) { + if (i >= total) { + running = 0; + } else { + el_val_t ch = native_list_get(chars, i); + if (is_digit(ch)) { + text = el_str_concat(text, ch); + i = (i + 1); + } else { + running = 0; + } + } + } + return el_map_new(2, "text", text, "pos", i); + return 0; +} + +el_val_t scan_ident(el_val_t chars, el_val_t start, el_val_t total) { + el_val_t i = start; + el_val_t text = EL_STR(""); + el_val_t running = 1; + while (running) { + if (i >= total) { + running = 0; + } else { + el_val_t ch = native_list_get(chars, i); + if (is_alnum_or_underscore(ch)) { + text = el_str_concat(text, ch); + i = (i + 1); + } else { + running = 0; + } + } + } + return el_map_new(2, "text", text, "pos", i); + return 0; +} + +el_val_t scan_string(el_val_t chars, el_val_t start, el_val_t total) { + el_val_t i = start; + el_val_t text = EL_STR(""); + el_val_t running = 1; + while (running) { + if (i >= total) { + running = 0; + } else { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("\\"))) { + el_val_t next_i = (i + 1); + if (next_i < total) { + el_val_t next_ch = native_list_get(chars, next_i); + if (str_eq(next_ch, EL_STR("\""))) { + text = el_str_concat(text, EL_STR("\"")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("n"))) { + text = el_str_concat(text, EL_STR("\n")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("t"))) { + text = el_str_concat(text, EL_STR("\t")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("r"))) { + text = el_str_concat(text, EL_STR("\r")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("\\"))) { + text = el_str_concat(text, EL_STR("\\")); + i = (next_i + 1); + } else { + text = el_str_concat(text, next_ch); + i = (next_i + 1); + } + } + } + } + } + } else { + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("\""))) { + i = (i + 1); + running = 0; + } else { + text = el_str_concat(text, ch); + i = (i + 1); + } + } + } + } + return el_map_new(2, "text", text, "pos", i); + return 0; +} + +el_val_t lex(el_val_t source) { + el_val_t chars = native_string_chars(source); + el_val_t total = native_list_len(chars); + el_val_t tokens = native_list_empty(); + el_val_t i = 0; + while (i < total) { + el_val_t ch = native_list_get(chars, i); + if (is_whitespace(ch)) { + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("/"))) { + el_val_t next_i = (i + 1); + if (next_i < total) { + el_val_t next_ch = native_list_get(chars, next_i); + if (str_eq(next_ch, EL_STR("/"))) { + i = (i + 2); + el_val_t running2 = 1; + while (running2) { + if (i >= total) { + running2 = 0; + } else { + el_val_t lch = native_list_get(chars, i); + if (str_eq(lch, EL_STR("\n"))) { + running2 = 0; + } else { + i = (i + 1); + } + } + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Slash"), EL_STR("/"))); + i = (i + 1); + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Slash"), EL_STR("/"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("\""))) { + el_val_t result = scan_string(chars, (i + 1), total); + el_val_t str_text = el_get_field(result, EL_STR("text")); + el_val_t new_pos = el_get_field(result, EL_STR("pos")); + tokens = native_list_append(tokens, make_tok(EL_STR("Str"), str_text)); + i = new_pos; + } else { + if (is_digit(ch)) { + el_val_t result = scan_digits(chars, i, total); + el_val_t num_text = el_get_field(result, EL_STR("text")); + el_val_t new_pos = el_get_field(result, EL_STR("pos")); + if (new_pos < total) { + el_val_t dot_ch = native_list_get(chars, new_pos); + if (str_eq(dot_ch, EL_STR("."))) { + el_val_t after_dot = (new_pos + 1); + if (after_dot < total) { + el_val_t after_dot_ch = native_list_get(chars, after_dot); + if (is_digit(after_dot_ch)) { + el_val_t frac_result = scan_digits(chars, after_dot, total); + el_val_t frac_text = el_get_field(frac_result, EL_STR("text")); + el_val_t frac_pos = el_get_field(frac_result, EL_STR("pos")); + tokens = native_list_append(tokens, make_tok(EL_STR("Float"), el_str_concat(el_str_concat(num_text, EL_STR(".")), frac_text))); + i = frac_pos; + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + if (is_alpha(ch) || str_eq(ch, EL_STR("_"))) { + el_val_t result = scan_ident(chars, i, total); + el_val_t word = el_get_field(result, EL_STR("text")); + el_val_t new_pos = el_get_field(result, EL_STR("pos")); + el_val_t kw = keyword_kind(word); + if (str_eq(kw, EL_STR(""))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Ident"), word)); + } else { + tokens = native_list_append(tokens, make_tok(kw, word)); + } + i = new_pos; + } else { + el_val_t peek_i = (i + 1); + el_val_t peek_ch = EL_STR(""); + if (peek_i < total) { + peek_ch = native_list_get(chars, peek_i); + } + if (str_eq(ch, EL_STR("="))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("EqEq"), EL_STR("=="))); + i = (i + 2); + } else { + if (str_eq(peek_ch, EL_STR(">"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("FatArrow"), EL_STR("=>"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Eq"), EL_STR("="))); + i = (i + 1); + } + } + } else { + if (str_eq(ch, EL_STR("!"))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("NotEq"), EL_STR("!="))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Not"), EL_STR("!"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("<"))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LtEq"), EL_STR("<="))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Lt"), EL_STR("<"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR(">"))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("GtEq"), EL_STR(">="))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Gt"), EL_STR(">"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("&"))) { + if (str_eq(peek_ch, EL_STR("&"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("And"), EL_STR("&&"))); + i = (i + 2); + } else { + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("|"))) { + if (str_eq(peek_ch, EL_STR("|"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Or"), EL_STR("||"))); + i = (i + 2); + } else { + if (str_eq(peek_ch, EL_STR(">"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("PipeOp"), EL_STR("|>"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Pipe"), EL_STR("|"))); + i = (i + 1); + } + } + } else { + if (str_eq(ch, EL_STR("-"))) { + if (str_eq(peek_ch, EL_STR(">"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Arrow"), EL_STR("->"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Minus"), EL_STR("-"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR(":"))) { + if (str_eq(peek_ch, EL_STR(":"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("ColonColon"), EL_STR("::"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Colon"), EL_STR(":"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("+"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Plus"), EL_STR("+"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("*"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Star"), EL_STR("*"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("%"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Percent"), EL_STR("%"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("("))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LParen"), EL_STR("("))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR(")"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("RParen"), EL_STR(")"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("{"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LBrace"), EL_STR("{"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("}"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("RBrace"), EL_STR("}"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("["))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LBracket"), EL_STR("["))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("]"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("RBracket"), EL_STR("]"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR(","))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Comma"), EL_STR(","))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("."))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Dot"), EL_STR("."))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR(";"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Semicolon"), EL_STR(";"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("@"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("At"), EL_STR("@"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("?"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("QuestionMark"), EL_STR("?"))); + i = (i + 1); + } else { + i = (i + 1); + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + tokens = native_list_append(tokens, make_tok(EL_STR("Eof"), EL_STR(""))); + return tokens; + return 0; +} + +el_val_t tok_at(el_val_t tokens, el_val_t pos) { + return native_list_get(tokens, pos); + return 0; +} + +el_val_t tok_kind(el_val_t tokens, el_val_t pos) { + el_val_t t = native_list_get(tokens, pos); + return el_get_field(t, EL_STR("kind")); + return 0; +} + +el_val_t tok_value(el_val_t tokens, el_val_t pos) { + el_val_t t = native_list_get(tokens, pos); + return el_get_field(t, EL_STR("value")); + return 0; +} + +el_val_t expect(el_val_t tokens, el_val_t pos, el_val_t kind) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, kind)) { + return (pos + 1); + } + return (pos + 1); + return 0; +} + +el_val_t make_result(el_val_t node, el_val_t pos) { + return el_map_new(2, "node", node, "pos", pos); + return 0; +} + +el_val_t skip_type(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("LBracket"))) { + el_val_t p = (pos + 1); + p = skip_type(tokens, p); + p = expect(tokens, p, EL_STR("RBracket")); + return p; + } + if (str_eq(k, EL_STR("Ident"))) { + el_val_t p = (pos + 1); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Lt"))) { + p = (p + 1); + el_val_t depth = 1; + el_val_t running = 1; + while (running) { + el_val_t kk = tok_kind(tokens, p); + if (str_eq(kk, EL_STR("Eof"))) { + running = 0; + } else { + if (str_eq(kk, EL_STR("Lt"))) { + depth = (depth + 1); + p = (p + 1); + } else { + if (str_eq(kk, EL_STR("Gt"))) { + depth = (depth - 1); + p = (p + 1); + if (depth <= 0) { + running = 0; + } + } else { + p = (p + 1); + } + } + } + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("QuestionMark"))) { + p = (p + 1); + } + return p; + } + if (str_eq(k2, EL_STR("QuestionMark"))) { + return (p + 1); + } + return p; + } + return (pos + 1); + return 0; +} + +el_val_t parse_params(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("LParen")); + el_val_t params = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("RParen"))) { + running = 0; + } else { + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t pname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + el_val_t ptype = EL_STR(""); + el_val_t kt = tok_kind(tokens, p); + if (str_eq(kt, EL_STR("Ident"))) { + ptype = tok_value(tokens, p); + } + p = skip_type(tokens, p); + el_val_t param = el_map_new(2, "name", pname, "type", ptype); + params = native_list_append(params, param); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RParen")); + return el_map_new(2, "params", params, "pos", p); + return 0; +} + +el_val_t parse_primary(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + el_val_t v = tok_value(tokens, pos); + if (str_eq(k, EL_STR("Int"))) { + return make_result(el_map_new(2, "expr", EL_STR("Int"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Float"))) { + return make_result(el_map_new(2, "expr", EL_STR("Float"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Str"))) { + return make_result(el_map_new(2, "expr", EL_STR("Str"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Bool"))) { + return make_result(el_map_new(2, "expr", EL_STR("Bool"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Ident"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("LParen"))) { + el_val_t r = parse_expr(tokens, (pos + 1)); + el_val_t node = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + p = expect(tokens, p, EL_STR("RParen")); + return make_result(node, p); + } + if (str_eq(k, EL_STR("LBracket"))) { + el_val_t p = (pos + 1); + el_val_t elems = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBracket"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r = parse_expr(tokens, p); + el_val_t elem = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + elems = native_list_append(elems, elem); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBracket")); + return make_result(el_map_new(2, "expr", EL_STR("Array"), "elems", elems), p); + } + if (str_eq(k, EL_STR("LBrace"))) { + el_val_t no_block = state_get(EL_STR("__no_block_expr")); + if (str_eq(no_block, EL_STR("1"))) { + return make_result(el_map_new(1, "expr", EL_STR("Nil")), pos); + } + el_val_t p = (pos + 1); + el_val_t pairs = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t key = tok_value(tokens, p); + el_val_t new_p = (p + 1); + new_p = expect(tokens, new_p, EL_STR("Colon")); + el_val_t r = parse_expr(tokens, new_p); + el_val_t val_node = el_get_field(r, EL_STR("node")); + new_p = el_get_field(r, EL_STR("pos")); + el_val_t pair = el_map_new(2, "key", key, "value", val_node); + pairs = native_list_append(pairs, pair); + el_val_t k3 = tok_kind(tokens, new_p); + if (str_eq(k3, EL_STR("Comma"))) { + new_p = (new_p + 1); + } + if (new_p <= p) { + p = (p + 1); + } else { + p = new_p; + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(2, "expr", EL_STR("Map"), "pairs", pairs), p); + } + if (str_eq(k, EL_STR("If"))) { + el_val_t r = parse_if(tokens, pos); + return r; + } + if (str_eq(k, EL_STR("Match"))) { + el_val_t r = parse_match(tokens, pos); + return r; + } + if (str_eq(k, EL_STR("For"))) { + el_val_t r = parse_for_expr(tokens, pos); + return r; + } + if (str_eq(k, EL_STR("Not"))) { + el_val_t r = parse_primary(tokens, (pos + 1)); + el_val_t inner = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "expr", EL_STR("Not"), "inner", inner), p); + } + if (str_eq(k, EL_STR("Minus"))) { + el_val_t r = parse_primary(tokens, (pos + 1)); + el_val_t inner = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "expr", EL_STR("Neg"), "inner", inner), p); + } + if (str_eq(k, EL_STR("Target"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("To"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Via"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Deploy"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Reason"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Times"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Fallback"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Retry"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Parallel"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Trace"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Requires"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Where"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("As"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("With"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Manager"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Engine"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Accessor"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Vessel"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + return make_result(el_map_new(1, "expr", EL_STR("Nil")), (pos + 1)); + return 0; +} + +el_val_t parse_if(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("If")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t cond = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t then_stmts = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + el_val_t has_else = 0; + el_val_t else_stmts = native_list_empty(); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Else"))) { + p = (p + 1); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("If"))) { + el_val_t r3 = parse_if(tokens, p); + el_val_t nested = el_get_field(r3, EL_STR("node")); + p = el_get_field(r3, EL_STR("pos")); + else_stmts = native_list_append(else_stmts, el_map_new(2, "stmt", EL_STR("Expr"), "value", nested)); + has_else = 1; + } else { + el_val_t r3 = parse_block(tokens, p); + else_stmts = el_get_field(r3, EL_STR("stmts")); + p = el_get_field(r3, EL_STR("pos")); + has_else = 1; + } + } + return make_result(el_map_new(5, "expr", EL_STR("If"), "cond", cond, "then", then_stmts, "else", else_stmts, "has_else", has_else), p); + return 0; +} + +el_val_t parse_match(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("Match")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t subject = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t arms = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r2 = parse_pattern(tokens, p); + el_val_t pattern = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + p = expect(tokens, p, EL_STR("FatArrow")); + el_val_t r3 = parse_expr(tokens, p); + el_val_t body = el_get_field(r3, EL_STR("node")); + p = el_get_field(r3, EL_STR("pos")); + el_val_t arm = el_map_new(2, "pattern", pattern, "body", body); + arms = native_list_append(arms, arm); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(3, "expr", EL_STR("Match"), "subject", subject, "arms", arms), p); + return 0; +} + +el_val_t parse_pattern(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("Ident"))) { + el_val_t v = tok_value(tokens, pos); + if (str_eq(v, EL_STR("_"))) { + return make_result(el_map_new(1, "pattern", EL_STR("Wildcard")), (pos + 1)); + } + return make_result(el_map_new(2, "pattern", EL_STR("Binding"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Int"))) { + return make_result(el_map_new(2, "pattern", EL_STR("LitInt"), "value", tok_value(tokens, pos)), (pos + 1)); + } + if (str_eq(k, EL_STR("Str"))) { + return make_result(el_map_new(2, "pattern", EL_STR("LitStr"), "value", tok_value(tokens, pos)), (pos + 1)); + } + if (str_eq(k, EL_STR("Bool"))) { + return make_result(el_map_new(2, "pattern", EL_STR("LitBool"), "value", tok_value(tokens, pos)), (pos + 1)); + } + return make_result(el_map_new(1, "pattern", EL_STR("Wildcard")), (pos + 1)); + return 0; +} + +el_val_t parse_for_expr(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("For")); + el_val_t item_name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("In")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t list_expr = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(4, "expr", EL_STR("For"), "item", item_name, "list", list_expr, "body", body), p); + return 0; +} + +el_val_t parse_block(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("LBrace")); + el_val_t stmts = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r = parse_stmt(tokens, p); + el_val_t stmt = el_get_field(r, EL_STR("node")); + el_val_t new_p = el_get_field(r, EL_STR("pos")); + stmts = native_list_append(stmts, stmt); + if (new_p <= p) { + p = (p + 1); + } else { + p = new_p; + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return el_map_new(2, "stmts", stmts, "pos", p); + return 0; +} + +el_val_t parse_postfix(el_val_t tokens, el_val_t pos) { + el_val_t r = parse_primary(tokens, pos); + el_val_t node = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("LParen"))) { + p = (p + 1); + el_val_t args = native_list_empty(); + el_val_t run2 = 1; + while (run2) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RParen"))) { + run2 = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + run2 = 0; + } else { + el_val_t r2 = parse_expr(tokens, p); + el_val_t arg = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + args = native_list_append(args, arg); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RParen")); + node = el_map_new(3, "expr", EL_STR("Call"), "func", node, "args", args); + } else { + if (str_eq(k, EL_STR("Dot"))) { + el_val_t field = tok_value(tokens, (p + 1)); + p = (p + 2); + node = el_map_new(3, "expr", EL_STR("Field"), "object", node, "field", field); + } else { + if (str_eq(k, EL_STR("LBracket"))) { + el_val_t r2 = parse_expr(tokens, (p + 1)); + el_val_t idx = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + p = expect(tokens, p, EL_STR("RBracket")); + node = el_map_new(3, "expr", EL_STR("Index"), "object", node, "index", idx); + } else { + if (str_eq(k, EL_STR("QuestionMark"))) { + p = (p + 1); + node = el_map_new(2, "expr", EL_STR("Try"), "inner", node); + } else { + running = 0; + } + } + } + } + } + return make_result(node, p); + return 0; +} + +el_val_t op_precedence(el_val_t kind) { + if (str_eq(kind, EL_STR("Or"))) { + return 1; + } + if (str_eq(kind, EL_STR("And"))) { + return 2; + } + if (str_eq(kind, EL_STR("EqEq"))) { + return 3; + } + if (str_eq(kind, EL_STR("NotEq"))) { + return 3; + } + if (str_eq(kind, EL_STR("Lt"))) { + return 4; + } + if (str_eq(kind, EL_STR("Gt"))) { + return 4; + } + if (str_eq(kind, EL_STR("LtEq"))) { + return 4; + } + if (str_eq(kind, EL_STR("GtEq"))) { + return 4; + } + if (str_eq(kind, EL_STR("Plus"))) { + return 5; + } + if (str_eq(kind, EL_STR("Minus"))) { + return 5; + } + if (str_eq(kind, EL_STR("Star"))) { + return 6; + } + if (str_eq(kind, EL_STR("Slash"))) { + return 6; + } + return 0; + return 0; +} + +el_val_t is_binop(el_val_t kind) { + if (str_eq(kind, EL_STR("Or"))) { + return 1; + } + if (str_eq(kind, EL_STR("And"))) { + return 1; + } + if (str_eq(kind, EL_STR("EqEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("NotEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("Lt"))) { + return 1; + } + if (str_eq(kind, EL_STR("Gt"))) { + return 1; + } + if (str_eq(kind, EL_STR("LtEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("GtEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("Plus"))) { + return 1; + } + if (str_eq(kind, EL_STR("Minus"))) { + return 1; + } + if (str_eq(kind, EL_STR("Star"))) { + return 1; + } + if (str_eq(kind, EL_STR("Slash"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t parse_binop(el_val_t tokens, el_val_t pos, el_val_t min_prec) { + el_val_t r = parse_postfix(tokens, pos); + el_val_t left = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + el_val_t prec = op_precedence(k); + if (is_binop(k)) { + if (prec >= min_prec) { + el_val_t op = k; + el_val_t r2 = parse_binop(tokens, (p + 1), (prec + 1)); + el_val_t right = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + left = el_map_new(4, "expr", EL_STR("BinOp"), "op", op, "left", left, "right", right); + } else { + running = 0; + } + } else { + running = 0; + } + } + return make_result(left, p); + return 0; +} + +el_val_t parse_expr(el_val_t tokens, el_val_t pos) { + return parse_binop(tokens, pos, 1); + return 0; +} + +el_val_t parse_stmt(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("Let"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + el_val_t ltype = EL_STR(""); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Colon"))) { + p = (p + 1); + el_val_t kt = tok_kind(tokens, p); + if (str_eq(kt, EL_STR("Ident"))) { + ltype = tok_value(tokens, p); + } + p = skip_type(tokens, p); + } + p = expect(tokens, p, EL_STR("Eq")); + el_val_t r = parse_expr(tokens, p); + el_val_t val = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(4, "stmt", EL_STR("Let"), "name", name, "value", val, "type", ltype), p); + } + if (str_eq(k, EL_STR("Return"))) { + el_val_t p = (pos + 1); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + return make_result(el_map_new(2, "stmt", EL_STR("Return"), "value", el_map_new(1, "expr", EL_STR("Nil"))), p); + } + if (str_eq(k2, EL_STR("Eof"))) { + return make_result(el_map_new(2, "stmt", EL_STR("Return"), "value", el_map_new(1, "expr", EL_STR("Nil"))), p); + } + el_val_t r = parse_expr(tokens, p); + el_val_t val = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "stmt", EL_STR("Return"), "value", val), p); + } + if (str_eq(k, EL_STR("Fn"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + el_val_t r = parse_params(tokens, p); + el_val_t params = el_get_field(r, EL_STR("params")); + p = el_get_field(r, EL_STR("pos")); + el_val_t ret_type = EL_STR(""); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Arrow"))) { + p = (p + 1); + el_val_t kt = tok_kind(tokens, p); + if (str_eq(kt, EL_STR("Ident"))) { + ret_type = tok_value(tokens, p); + } + p = skip_type(tokens, p); + } + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(5, "stmt", EL_STR("FnDef"), "name", name, "params", params, "body", body, "ret_type", ret_type), p); + } + if (str_eq(k, EL_STR("Type"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t fields = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t fname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + p = skip_type(tokens, p); + fields = native_list_append(fields, el_map_new(1, "name", fname)); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(3, "stmt", EL_STR("TypeDef"), "name", name, "fields", fields), p); + } + if (str_eq(k, EL_STR("Enum"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t variants = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t vname = tok_value(tokens, p); + p = (p + 1); + variants = native_list_append(variants, el_map_new(1, "name", vname)); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(3, "stmt", EL_STR("EnumDef"), "name", name, "variants", variants), p); + } + if (str_eq(k, EL_STR("Import"))) { + el_val_t p = (pos + 1); + el_val_t path = tok_value(tokens, p); + p = (p + 1); + return make_result(el_map_new(2, "stmt", EL_STR("Import"), "path", path), p); + } + if (str_eq(k, EL_STR("From"))) { + el_val_t p = (pos + 1); + el_val_t module_name = tok_value(tokens, p); + p = (p + 1); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Import"))) { + p = (p + 1); + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("LBrace"))) { + p = (p + 1); + el_val_t running = 1; + while (running) { + el_val_t k4 = tok_kind(tokens, p); + if (str_eq(k4, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k4, EL_STR("Eof"))) { + running = 0; + } else { + p = (p + 1); + el_val_t k5 = tok_kind(tokens, p); + if (str_eq(k5, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + } + return make_result(el_map_new(2, "stmt", EL_STR("Import"), "path", module_name), p); + } + if (str_eq(k, EL_STR("While"))) { + el_val_t p = (pos + 1); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t cond = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(3, "stmt", EL_STR("While"), "cond", cond, "body", body), p); + } + if (str_eq(k, EL_STR("For"))) { + el_val_t p = (pos + 1); + el_val_t item_name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("In")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t list_expr = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(4, "stmt", EL_STR("For"), "item", item_name, "list", list_expr, "body", body), p); + } + if (str_eq(k, EL_STR("At"))) { + el_val_t p = (pos + 1); + el_val_t dec_name = tok_value(tokens, p); + p = (p + 1); + el_val_t r = parse_stmt(tokens, p); + el_val_t inner = el_get_field(r, EL_STR("node")); + el_val_t p2 = el_get_field(r, EL_STR("pos")); + el_val_t inner_kind = el_get_field(inner, EL_STR("stmt")); + if (str_eq(inner_kind, EL_STR("FnDef"))) { + el_val_t with_dec = el_map_new(6, "stmt", EL_STR("FnDef"), "name", el_get_field(inner, EL_STR("name")), "params", el_get_field(inner, EL_STR("params")), "body", el_get_field(inner, EL_STR("body")), "ret_type", el_get_field(inner, EL_STR("ret_type")), "decorator", dec_name); + return make_result(with_dec, p2); + } + return r; + } + if (str_eq(k, EL_STR("Cgi"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t dharma_id = EL_STR(""); + el_val_t principal = EL_STR(""); + el_val_t network = EL_STR(""); + el_val_t engram = EL_STR(""); + el_val_t has_dharma_id = 0; + el_val_t has_principal = 0; + el_val_t has_network = 0; + el_val_t has_engram = 0; + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t fname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + el_val_t fval = tok_value(tokens, p); + p = (p + 1); + if (str_eq(fname, EL_STR("dharma_id"))) { + dharma_id = fval; + has_dharma_id = 1; + } + if (str_eq(fname, EL_STR("principal"))) { + principal = fval; + has_principal = 1; + } + if (str_eq(fname, EL_STR("network"))) { + network = fval; + has_network = 1; + } + if (str_eq(fname, EL_STR("engram"))) { + engram = fval; + has_engram = 1; + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(10, "stmt", EL_STR("CgiBlock"), "name", name, "dharma_id", dharma_id, "principal", principal, "network", network, "engram", engram, "has_dharma_id", has_dharma_id, "has_principal", has_principal, "has_network", has_network, "has_engram", has_engram), p); + } + if (str_eq(k, EL_STR("Service"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t sponsor = EL_STR(""); + el_val_t domain = EL_STR(""); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t fname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + el_val_t fval = tok_value(tokens, p); + p = (p + 1); + if (str_eq(fname, EL_STR("sponsor"))) { + sponsor = fval; + } + if (str_eq(fname, EL_STR("domain"))) { + domain = fval; + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(4, "stmt", EL_STR("ServiceBlock"), "name", name, "sponsor", sponsor, "domain", domain), p); + } + el_val_t r = parse_expr(tokens, pos); + el_val_t val = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "stmt", EL_STR("Expr"), "value", val), p); + return 0; +} + +el_val_t parse(el_val_t tokens) { + el_val_t total = native_list_len(tokens); + el_val_t stmts = native_list_empty(); + el_val_t pos = 0; + el_val_t running = 1; + while (running) { + if (pos >= total) { + running = 0; + } else { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r = parse_stmt(tokens, pos); + el_val_t stmt = el_get_field(r, EL_STR("node")); + el_val_t new_pos = el_get_field(r, EL_STR("pos")); + stmts = native_list_append(stmts, stmt); + if (new_pos <= pos) { + pos = (pos + 1); + } else { + pos = new_pos; + } + } + } + } + return stmts; + return 0; +} + +el_val_t c_escape(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t total = native_list_len(chars); + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < total) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("\""))) { + out = el_str_concat(out, EL_STR("\\\"")); + } else { + if (str_eq(ch, EL_STR("\\"))) { + out = el_str_concat(out, EL_STR("\\\\")); + } else { + if (str_eq(ch, EL_STR("\n"))) { + out = el_str_concat(out, EL_STR("\\n")); + } else { + if (str_eq(ch, EL_STR("\r"))) { + out = el_str_concat(out, EL_STR("\\r")); + } else { + if (str_eq(ch, EL_STR("\t"))) { + out = el_str_concat(out, EL_STR("\\t")); + } else { + out = el_str_concat(out, ch); + } + } + } + } + } + i = (i + 1); + } + return out; + return 0; +} + +el_val_t c_str_lit(el_val_t s) { + return el_str_concat(el_str_concat(EL_STR("\""), c_escape(s)), EL_STR("\"")); + return 0; +} + +el_val_t el_type_to_c(el_val_t type_str) { + if (str_eq(type_str, EL_STR("String"))) { + return EL_STR("const char*"); + } + if (str_eq(type_str, EL_STR("Int"))) { + return EL_STR("int64_t"); + } + if (str_eq(type_str, EL_STR("Bool"))) { + return EL_STR("int"); + } + if (str_eq(type_str, EL_STR("Float"))) { + return EL_STR("double"); + } + if (str_eq(type_str, EL_STR("Void"))) { + return EL_STR("void"); + } + if (str_eq(type_str, EL_STR("void"))) { + return EL_STR("void"); + } + return EL_STR("void*"); + return 0; +} + +el_val_t emit_line(el_val_t line) { + println(line); + return 0; +} + +el_val_t emit_blank(void) { + println(EL_STR("")); + return 0; +} + +el_val_t binop_to_c(el_val_t op) { + if (str_eq(op, EL_STR("Plus"))) { + return EL_STR("+"); + } + if (str_eq(op, EL_STR("Minus"))) { + return EL_STR("-"); + } + if (str_eq(op, EL_STR("Star"))) { + return EL_STR("*"); + } + if (str_eq(op, EL_STR("Slash"))) { + return EL_STR("/"); + } + if (str_eq(op, EL_STR("EqEq"))) { + return EL_STR("=="); + } + if (str_eq(op, EL_STR("NotEq"))) { + return EL_STR("!="); + } + if (str_eq(op, EL_STR("Lt"))) { + return EL_STR("<"); + } + if (str_eq(op, EL_STR("Gt"))) { + return EL_STR(">"); + } + if (str_eq(op, EL_STR("LtEq"))) { + return EL_STR("<="); + } + if (str_eq(op, EL_STR("GtEq"))) { + return EL_STR(">="); + } + if (str_eq(op, EL_STR("And"))) { + return EL_STR("&&"); + } + if (str_eq(op, EL_STR("Or"))) { + return EL_STR("||"); + } + return op; + return 0; +} + +el_val_t cg_expr(el_val_t expr) { + el_val_t kind = el_get_field(expr, EL_STR("expr")); + if (str_eq(kind, EL_STR("Int"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return v; + } + if (str_eq(kind, EL_STR("Float"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return el_str_concat(el_str_concat(EL_STR("el_from_float("), v), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Str"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return el_str_concat(el_str_concat(EL_STR("EL_STR("), c_str_lit(v)), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Bool"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + if (str_eq(v, EL_STR("true"))) { + return EL_STR("1"); + } + return EL_STR("0"); + } + if (str_eq(kind, EL_STR("Nil"))) { + return EL_STR("EL_NULL"); + } + if (str_eq(kind, EL_STR("Ident"))) { + el_val_t name = el_get_field(expr, EL_STR("name")); + return name; + } + if (str_eq(kind, EL_STR("Not"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = cg_expr(inner); + return el_str_concat(EL_STR("!"), inner_c); + } + if (str_eq(kind, EL_STR("Neg"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = cg_expr(inner); + return el_str_concat(el_str_concat(EL_STR("(-"), inner_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("BinOp"))) { + el_val_t op = el_get_field(expr, EL_STR("op")); + el_val_t left = el_get_field(expr, EL_STR("left")); + el_val_t right = el_get_field(expr, EL_STR("right")); + el_val_t left_c = cg_expr(left); + el_val_t right_c = cg_expr(right); + el_val_t left_kind = el_get_field(left, EL_STR("expr")); + el_val_t right_kind = el_get_field(right, EL_STR("expr")); + if (str_eq(op, EL_STR("Plus"))) { + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (is_int_expr(left)) { + if (is_int_expr(right)) { + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + } + if (str_eq(left_kind, EL_STR("Int"))) { + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("BinOp"))) { + el_val_t left_op = el_get_field(left, EL_STR("op")); + if (str_eq(left_op, EL_STR("Plus"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(right_kind, EL_STR("BinOp"))) { + el_val_t right_op = el_get_field(right, EL_STR("op")); + if (str_eq(right_op, EL_STR("Plus"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(op, EL_STR("EqEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (is_int_name(lname)) { + if (is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(op, EL_STR("NotEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (is_int_name(lname)) { + if (is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Call"))) { + el_val_t func = el_get_field(expr, EL_STR("func")); + el_val_t args = el_get_field(expr, EL_STR("args")); + el_val_t arity = native_list_len(args); + el_val_t func_kind = el_get_field(func, EL_STR("expr")); + el_val_t args_c = EL_STR(""); + el_val_t i = 0; + while (i < arity) { + el_val_t arg = native_list_get(args, i); + el_val_t arg_c = cg_expr(arg); + if (i > 0) { + args_c = el_str_concat(args_c, EL_STR(", ")); + } + args_c = el_str_concat(args_c, arg_c); + i = (i + 1); + } + if (str_eq(func_kind, EL_STR("Ident"))) { + el_val_t fn_name = el_get_field(func, EL_STR("name")); + cap_check_call(fn_name); + arity_check_call(fn_name, arity); + return el_str_concat(el_str_concat(el_str_concat(fn_name, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(func_kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(func, EL_STR("object")); + el_val_t field = el_get_field(func, EL_STR("field")); + el_val_t obj_c = cg_expr(obj); + if (arity > 0) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(", ")), args_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(")")); + } + el_val_t fn_c = cg_expr(func); + return el_str_concat(el_str_concat(el_str_concat(fn_c, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t field = el_get_field(expr, EL_STR("field")); + el_val_t obj_c = cg_expr(obj); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", EL_STR(")), c_str_lit(field)), EL_STR("))")); + } + if (str_eq(kind, EL_STR("Index"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t idx = el_get_field(expr, EL_STR("index")); + el_val_t obj_c = cg_expr(obj); + el_val_t idx_c = cg_expr(idx); + el_val_t idx_kind = el_get_field(idx, EL_STR("expr")); + if (str_eq(idx_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_list_get("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Array"))) { + el_val_t elems = el_get_field(expr, EL_STR("elems")); + el_val_t n = native_list_len(elems); + if (n == 0) { + return EL_STR("el_list_empty()"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t elem = native_list_get(elems, i); + el_val_t elem_c = cg_expr(elem); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(items, elem_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_list_new("), native_int_to_str(n)), EL_STR(", ")), items), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Map"))) { + el_val_t pairs = el_get_field(expr, EL_STR("pairs")); + el_val_t n = native_list_len(pairs); + if (n == 0) { + return EL_STR("el_map_new(0)"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t pair = native_list_get(pairs, i); + el_val_t key = el_get_field(pair, EL_STR("key")); + el_val_t val = el_get_field(pair, EL_STR("value")); + el_val_t val_c = cg_expr(val); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(el_str_concat(el_str_concat(items, c_str_lit(key)), EL_STR(", ")), val_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_map_new("), native_int_to_str(n)), EL_STR(", ")), items), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Try"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + return cg_expr(inner); + } + if (str_eq(kind, EL_STR("If"))) { + return cg_if_expr(expr); + } + if (str_eq(kind, EL_STR("Match"))) { + return cg_match(expr); + } + return EL_STR("EL_NULL"); + return 0; +} + +el_val_t next_match_id(void) { + el_val_t csv = state_get(EL_STR("__match_counter")); + el_val_t n = 0; + if (!str_eq(csv, EL_STR(""))) { + n = str_to_int(csv); + } + n = (n + 1); + state_set(EL_STR("__match_counter"), native_int_to_str(n)); + return native_int_to_str(n); + return 0; +} + +el_val_t cg_match(el_val_t expr) { + el_val_t subject = el_get_field(expr, EL_STR("subject")); + el_val_t arms = el_get_field(expr, EL_STR("arms")); + el_val_t subj_c = cg_expr(subject); + el_val_t id = next_match_id(); + el_val_t subj_var = el_str_concat(EL_STR("_match_subj_"), id); + el_val_t result_var = el_str_concat(EL_STR("_match_result_"), id); + el_val_t done_label = el_str_concat(EL_STR("_match_done_"), id); + el_val_t out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("({ el_val_t "), subj_var), EL_STR(" = ")), subj_c), EL_STR("; el_val_t ")), result_var), EL_STR(" = 0; ")); + el_val_t n = native_list_len(arms); + el_val_t i = 0; + while (i < n) { + el_val_t arm = native_list_get(arms, i); + el_val_t pat = el_get_field(arm, EL_STR("pattern")); + el_val_t body = el_get_field(arm, EL_STR("body")); + el_val_t pkind = el_get_field(pat, EL_STR("pattern")); + el_val_t body_c = cg_expr(body); + if (str_eq(pkind, EL_STR("Wildcard"))) { + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("Binding"))) { + el_val_t bname = el_get_field(pat, EL_STR("name")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ el_val_t ")), bname), EL_STR(" = ")), subj_var), EL_STR("; ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("LitInt"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" == ")), v), EL_STR(") { ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("LitStr"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (str_eq(")), subj_var), EL_STR(", EL_STR(")), c_str_lit(v)), EL_STR("))) { ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("LitBool"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + el_val_t bv = EL_STR("0"); + if (str_eq(v, EL_STR("true"))) { + bv = EL_STR("1"); + } + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" == ")), bv), EL_STR(") { ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } + } + } + } + } + i = (i + 1); + } + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, done_label), EL_STR(":; ")), result_var), EL_STR("; })")); + return out; + return 0; +} + +el_val_t next_if_id(void) { + el_val_t csv = state_get(EL_STR("__if_expr_counter")); + el_val_t n = 0; + if (!str_eq(csv, EL_STR(""))) { + n = str_to_int(csv); + } + n = (n + 1); + state_set(EL_STR("__if_expr_counter"), native_int_to_str(n)); + return native_int_to_str(n); + return 0; +} + +el_val_t cg_if_expr_arm(el_val_t stmts, el_val_t result_var) { + el_val_t n = native_list_len(stmts); + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t s = native_list_get(stmts, i); + el_val_t sk = el_get_field(s, EL_STR("stmt")); + el_val_t is_last = 0; + if (str_eq(i, (n - 1))) { + is_last = 1; + } + if (str_eq(sk, EL_STR("Let"))) { + el_val_t name = el_get_field(s, EL_STR("name")); + el_val_t val = el_get_field(s, EL_STR("value")); + el_val_t val_c = cg_expr(val); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("el_val_t ")), name), EL_STR(" = ")), val_c), EL_STR("; ")); + } else { + if (str_eq(sk, EL_STR("Return"))) { + el_val_t val = el_get_field(s, EL_STR("value")); + el_val_t val_c = cg_expr(val); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, result_var), EL_STR(" = (")), val_c), EL_STR("); ")); + } else { + if (str_eq(sk, EL_STR("Expr"))) { + el_val_t val = el_get_field(s, EL_STR("value")); + el_val_t val_c = cg_expr(val); + if (is_last) { + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, result_var), EL_STR(" = (")), val_c), EL_STR("); ")); + } else { + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("(void)(")), val_c), EL_STR("); ")); + } + } else { + } + } + } + i = (i + 1); + } + return out; + return 0; +} + +el_val_t cg_if_expr(el_val_t expr) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t then_stmts = el_get_field(expr, EL_STR("then")); + el_val_t else_stmts = el_get_field(expr, EL_STR("else")); + el_val_t has_else = el_get_field(expr, EL_STR("has_else")); + el_val_t cond_c = cg_expr(cond); + el_val_t id = next_if_id(); + el_val_t result_var = el_str_concat(EL_STR("_if_result_"), id); + el_val_t then_c = cg_if_expr_arm(then_stmts, result_var); + el_val_t else_c = EL_STR(""); + if (has_else) { + else_c = cg_if_expr_arm(else_stmts, result_var); + } + el_val_t out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("({ el_val_t "), result_var), EL_STR(" = 0; if (")), cond_c), EL_STR(") { ")), then_c), EL_STR("} else { ")), else_c), EL_STR("} ")), result_var), EL_STR("; })")); + return out; + return 0; +} + +el_val_t list_contains(el_val_t lst, el_val_t s) { + el_val_t n = native_list_len(lst); + el_val_t i = 0; + while (i < n) { + el_val_t item = native_list_get(lst, i); + if (str_eq(item, s)) { + return 1; + } + i = (i + 1); + } + return 0; + return 0; +} + +el_val_t cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_c = cg_expr(val); + el_val_t ltype = el_get_field(stmt, EL_STR("type")); + if (str_eq(ltype, EL_STR("Int"))) { + add_int_name(name); + } + el_val_t vk = el_get_field(val, EL_STR("expr")); + if (str_eq(vk, EL_STR("Int"))) { + add_int_name(name); + } + if (list_contains(declared, name)) { + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, name), EL_STR(" = ")), val_c), EL_STR(";"))); + return declared; + } else { + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("el_val_t ")), name), EL_STR(" = ")), val_c), EL_STR(";"))); + return native_list_append(declared, name); + } + } + if (str_eq(kind, EL_STR("Return"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("Nil"))) { + emit_line(el_str_concat(indent, EL_STR("return 0;"))); + } else { + el_val_t val_c = cg_expr(val); + emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("return ")), val_c), EL_STR(";"))); + } + return declared; + } + if (str_eq(kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + cg_if_stmt(val, indent, declared); + return declared; + } + if (str_eq(val_kind, EL_STR("For"))) { + cg_for_stmt(val, indent, declared); + return declared; + } + el_val_t val_c = cg_expr(val); + emit_line(el_str_concat(el_str_concat(indent, val_c), EL_STR(";"))); + return declared; + } + if (str_eq(kind, EL_STR("While"))) { + el_val_t cond = el_get_field(stmt, EL_STR("cond")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t cond_c = cg_expr(cond); + cond_c = strip_outer_parens(cond_c); + emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("while (")), cond_c), EL_STR(") {"))); + cg_stmts(body, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + emit_line(el_str_concat(indent, EL_STR("}"))); + return declared; + } + if (str_eq(kind, EL_STR("For"))) { + el_val_t item = el_get_field(stmt, EL_STR("item")); + el_val_t list_expr = el_get_field(stmt, EL_STR("list")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + cg_for_body(item, list_expr, body, indent, declared); + return declared; + } + if (str_eq(kind, EL_STR("FnDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("TypeDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("Import"))) { + return declared; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + return declared; + } + return declared; + return 0; +} + +el_val_t strip_outer_parens(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t n = native_list_len(chars); + if (n < 2) { + return s; + } + el_val_t first = native_list_get(chars, 0); + el_val_t last = native_list_get(chars, (n - 1)); + if (str_eq(first, EL_STR("("))) { + if (str_eq(last, EL_STR(")"))) { + el_val_t depth = 1; + el_val_t i = 1; + el_val_t balanced = 1; + while (i < (n - 1)) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("("))) { + depth = (depth + 1); + } + if (str_eq(ch, EL_STR(")"))) { + depth = (depth - 1); + if (depth == 0) { + balanced = 0; + i = n; + } + } + i = (i + 1); + } + if (balanced) { + el_val_t inner = EL_STR(""); + el_val_t j = 1; + while (j < (n - 1)) { + el_val_t ch = native_list_get(chars, j); + inner = el_str_concat(inner, ch); + j = (j + 1); + } + return inner; + } + } + } + return s; + return 0; +} + +el_val_t cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t then_stmts = el_get_field(expr, EL_STR("then")); + el_val_t else_stmts = el_get_field(expr, EL_STR("else")); + el_val_t has_else = el_get_field(expr, EL_STR("has_else")); + el_val_t cond_c = cg_expr(cond); + cond_c = strip_outer_parens(cond_c); + emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("if (")), cond_c), EL_STR(") {"))); + cg_stmts(then_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + if (has_else) { + emit_line(el_str_concat(indent, EL_STR("} else {"))); + cg_stmts(else_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + } + emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared) { + el_val_t list_c = cg_expr(list_expr); + el_val_t idx = EL_STR("_el_i"); + el_val_t list_tmp = EL_STR("_el_lst"); + el_val_t len_tmp = EL_STR("_el_len"); + emit_line(el_str_concat(indent, EL_STR("{"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" el_val_t ")), list_tmp), EL_STR(" = ")), list_c), EL_STR(";"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" el_val_t ")), len_tmp), EL_STR(" = el_list_len(")), list_tmp), EL_STR(");"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" for (el_val_t ")), idx), EL_STR(" = 0; ")), idx), EL_STR(" < ")), len_tmp), EL_STR("; ")), idx), EL_STR("++) {"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" el_val_t ")), item), EL_STR(" = el_list_get(")), list_tmp), EL_STR(", ")), idx), EL_STR(");"))); + el_val_t body_decl = native_list_clone(declared); + body_decl = native_list_append(body_decl, item); + cg_stmts(body, el_str_concat(indent, EL_STR(" ")), body_decl); + emit_line(el_str_concat(indent, EL_STR(" }"))); + emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t item = el_get_field(expr, EL_STR("item")); + el_val_t list_expr = el_get_field(expr, EL_STR("list")); + el_val_t body = el_get_field(expr, EL_STR("body")); + cg_for_body(item, list_expr, body, indent, declared); + return 0; +} + +el_val_t cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared) { + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + el_val_t decl = declared; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + decl = cg_stmt(stmt, indent, decl); + i = (i + 1); + } + return decl; + return 0; +} + +el_val_t param_decl(el_val_t param, el_val_t idx) { + el_val_t name = el_get_field(param, EL_STR("name")); + return el_str_concat(EL_STR("el_val_t "), name); + return 0; +} + +el_val_t params_to_c(el_val_t params) { + el_val_t n = native_list_len(params); + if (n == 0) { + return EL_STR("void"); + } + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t param = native_list_get(params, i); + el_val_t decl = param_decl(param, i); + if (i > 0) { + out = el_str_concat(out, EL_STR(", ")); + } + out = el_str_concat(out, decl); + i = (i + 1); + } + return out; + return 0; +} + +el_val_t transform_implicit_return(el_val_t body) { + el_val_t n = native_list_len(body); + if (n == 0) { + return body; + } + el_val_t last = native_list_get(body, (n - 1)); + el_val_t last_kind = el_get_field(last, EL_STR("stmt")); + if (str_eq(last_kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(last, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + return body; + } + if (str_eq(val_kind, EL_STR("For"))) { + return body; + } + el_val_t new_body = native_list_empty(); + el_val_t i = 0; + while (i < (n - 1)) { + new_body = native_list_append(new_body, native_list_get(body, i)); + i = (i + 1); + } + el_val_t return_stmt = el_map_new(2, "stmt", EL_STR("Return"), "value", val); + new_body = native_list_append(new_body, return_stmt); + return new_body; + } + return body; + return 0; +} + +el_val_t is_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__int_names")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + return str_contains(csv, el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(","))); + return 0; +} + +el_val_t is_int_call(el_val_t call_expr) { + el_val_t func = el_get_field(call_expr, EL_STR("func")); + el_val_t fk = el_get_field(func, EL_STR("expr")); + if (!str_eq(fk, EL_STR("Ident"))) { + return 0; + } + el_val_t name = el_get_field(func, EL_STR("name")); + if (str_eq(name, EL_STR("str_len"))) { + return 1; + } + if (str_eq(name, EL_STR("str_index_of"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("str_char_code"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("el_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("len"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get_int"))) { + return 1; + } + if (str_eq(name, EL_STR("json_array_len"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_node_count"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_edge_count"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now_utc"))) { + return 1; + } + if (str_eq(name, EL_STR("time_diff"))) { + return 1; + } + if (str_eq(name, EL_STR("time_add"))) { + return 1; + } + if (str_eq(name, EL_STR("time_from_parts"))) { + return 1; + } + if (str_eq(name, EL_STR("el_abs"))) { + return 1; + } + if (str_eq(name, EL_STR("el_max"))) { + return 1; + } + if (str_eq(name, EL_STR("el_min"))) { + return 1; + } + if (str_eq(name, EL_STR("float_to_int"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_int_expr(el_val_t expr) { + el_val_t k = el_get_field(expr, EL_STR("expr")); + if (str_eq(k, EL_STR("Int"))) { + return 1; + } + if (str_eq(k, EL_STR("Ident"))) { + el_val_t name = el_get_field(expr, EL_STR("name")); + return is_int_name(name); + } + if (str_eq(k, EL_STR("Call"))) { + return is_int_call(expr); + } + if (str_eq(k, EL_STR("Neg"))) { + return is_int_expr(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(k, EL_STR("Not"))) { + return 1; + } + if (str_eq(k, EL_STR("BinOp"))) { + el_val_t op = el_get_field(expr, EL_STR("op")); + if (str_eq(op, EL_STR("EqEq"))) { + return 1; + } + if (str_eq(op, EL_STR("NotEq"))) { + return 1; + } + if (str_eq(op, EL_STR("Lt"))) { + return 1; + } + if (str_eq(op, EL_STR("Gt"))) { + return 1; + } + if (str_eq(op, EL_STR("LtEq"))) { + return 1; + } + if (str_eq(op, EL_STR("GtEq"))) { + return 1; + } + if (str_eq(op, EL_STR("And"))) { + return 1; + } + if (str_eq(op, EL_STR("Or"))) { + return 1; + } + if (str_eq(op, EL_STR("Plus"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + if (str_eq(op, EL_STR("Minus"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + if (str_eq(op, EL_STR("Star"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + if (str_eq(op, EL_STR("Slash"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + return 0; + } + return 0; + return 0; +} + +el_val_t cap_record_violation(el_val_t kind, el_val_t fn_name) { + el_val_t csv = state_get(EL_STR("__cap_violations")); + if (str_eq(csv, EL_STR(""))) { + csv = EL_STR(","); + } + el_val_t entry = el_str_concat(el_str_concat(kind, EL_STR(":")), fn_name); + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), entry), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__cap_violations"), el_str_concat(el_str_concat(csv, entry), EL_STR(","))); + return 1; + return 0; +} + +el_val_t is_self_formation_call(el_val_t fn_name) { + if (str_eq(fn_name, EL_STR("llm_call_agentic"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_register_tool"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_emit"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_field"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_dharma_call(el_val_t fn_name) { + if (str_eq(fn_name, EL_STR("dharma_connect"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_send"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_activate"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_emit"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_field"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_strengthen"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_relationship"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_peers"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_llm_call(el_val_t fn_name) { + if (str_eq(fn_name, EL_STR("llm_call"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_call_system"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_call_agentic"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_vision"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_register_tool"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_models"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t cap_check_call(el_val_t fn_name) { + el_val_t kind = state_get(EL_STR("__program_kind")); + if (str_eq(kind, EL_STR("cgi"))) { + return 1; + } + if (str_eq(kind, EL_STR("service"))) { + if (is_self_formation_call(fn_name)) { + cap_record_violation(EL_STR("service"), fn_name); + return 0; + } + return 1; + } + if (is_dharma_call(fn_name)) { + cap_record_violation(EL_STR("utility"), fn_name); + return 0; + } + if (is_llm_call(fn_name)) { + cap_record_violation(EL_STR("utility"), fn_name); + return 0; + } + return 1; + return 0; +} + +el_val_t emit_cap_violations(void) { + el_val_t csv = state_get(EL_STR("__cap_violations")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + if (str_eq(csv, EL_STR(","))) { + return 0; + } + el_val_t n = str_len(csv); + el_val_t i = 1; + while (i < n) { + el_val_t next_comma = str_index_of(str_slice(csv, i, n), EL_STR(",")); + if (next_comma < 0) { + return 0; + } + el_val_t entry = str_slice(csv, i, (i + next_comma)); + el_val_t colon = str_index_of(entry, EL_STR(":")); + if (colon > 0) { + el_val_t kind = str_slice(entry, 0, colon); + el_val_t fn_name = str_slice(entry, (colon + 1), str_len(entry)); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("#error \"capability violation: '"), kind), EL_STR("' programs may not call '")), fn_name), EL_STR("' (self-formation primitive — only 'cgi' programs may use it)\""))); + } + i = ((i + next_comma) + 1); + } + return 0; +} + +el_val_t builtin_arity(el_val_t name) { + if (str_eq(name, EL_STR("println"))) { + return 1; + } + if (str_eq(name, EL_STR("print"))) { + return 1; + } + if (str_eq(name, EL_STR("readline"))) { + return 0; + } + if (str_eq(name, EL_STR("el_str_concat"))) { + return 2; + } + if (str_eq(name, EL_STR("str_eq"))) { + return 2; + } + if (str_eq(name, EL_STR("str_starts_with"))) { + return 2; + } + if (str_eq(name, EL_STR("str_ends_with"))) { + return 2; + } + if (str_eq(name, EL_STR("str_len"))) { + return 1; + } + if (str_eq(name, EL_STR("str_concat"))) { + return 2; + } + if (str_eq(name, EL_STR("int_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("str_slice"))) { + return 3; + } + if (str_eq(name, EL_STR("str_contains"))) { + return 2; + } + if (str_eq(name, EL_STR("str_replace"))) { + return 3; + } + if (str_eq(name, EL_STR("str_to_upper"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_lower"))) { + return 1; + } + if (str_eq(name, EL_STR("str_trim"))) { + return 1; + } + if (str_eq(name, EL_STR("str_index_of"))) { + return 2; + } + if (str_eq(name, EL_STR("str_split"))) { + return 2; + } + if (str_eq(name, EL_STR("str_char_at"))) { + return 2; + } + if (str_eq(name, EL_STR("str_char_code"))) { + return 2; + } + if (str_eq(name, EL_STR("str_pad_left"))) { + return 3; + } + if (str_eq(name, EL_STR("str_pad_right"))) { + return 3; + } + if (str_eq(name, EL_STR("str_format"))) { + return 2; + } + if (str_eq(name, EL_STR("str_lower"))) { + return 1; + } + if (str_eq(name, EL_STR("str_upper"))) { + return 1; + } + if (str_eq(name, EL_STR("el_abs"))) { + return 1; + } + if (str_eq(name, EL_STR("el_max"))) { + return 2; + } + if (str_eq(name, EL_STR("el_min"))) { + return 2; + } + if (str_eq(name, EL_STR("el_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("el_list_get"))) { + return 2; + } + if (str_eq(name, EL_STR("el_list_append"))) { + return 2; + } + if (str_eq(name, EL_STR("el_list_empty"))) { + return 0; + } + if (str_eq(name, EL_STR("el_list_clone"))) { + return 1; + } + if (str_eq(name, EL_STR("list_push"))) { + return 2; + } + if (str_eq(name, EL_STR("list_push_front"))) { + return 2; + } + if (str_eq(name, EL_STR("list_join"))) { + return 2; + } + if (str_eq(name, EL_STR("list_range"))) { + return 2; + } + if (str_eq(name, EL_STR("el_get_field"))) { + return 2; + } + if (str_eq(name, EL_STR("el_map_get"))) { + return 2; + } + if (str_eq(name, EL_STR("el_map_set"))) { + return 3; + } + if (str_eq(name, EL_STR("http_get"))) { + return 1; + } + if (str_eq(name, EL_STR("http_post"))) { + return 2; + } + if (str_eq(name, EL_STR("http_post_json"))) { + return 2; + } + if (str_eq(name, EL_STR("http_get_with_headers"))) { + return 2; + } + if (str_eq(name, EL_STR("http_post_with_headers"))) { + return 3; + } + if (str_eq(name, EL_STR("http_post_form_auth"))) { + return 3; + } + if (str_eq(name, EL_STR("http_get_to_file"))) { + return 3; + } + if (str_eq(name, EL_STR("http_post_to_file"))) { + return 4; + } + if (str_eq(name, EL_STR("http_serve"))) { + return 2; + } + if (str_eq(name, EL_STR("http_set_handler"))) { + return 1; + } + if (str_eq(name, EL_STR("fs_read"))) { + return 1; + } + if (str_eq(name, EL_STR("fs_write"))) { + return 2; + } + if (str_eq(name, EL_STR("fs_write_bytes"))) { + return 3; + } + if (str_eq(name, EL_STR("fs_list"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get"))) { + return 2; + } + if (str_eq(name, EL_STR("json_parse"))) { + return 1; + } + if (str_eq(name, EL_STR("json_stringify"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get_string"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_int"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_float"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_bool"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_raw"))) { + return 2; + } + if (str_eq(name, EL_STR("json_set"))) { + return 3; + } + if (str_eq(name, EL_STR("json_array_len"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now"))) { + return 0; + } + if (str_eq(name, EL_STR("time_now_utc"))) { + return 0; + } + if (str_eq(name, EL_STR("sleep_secs"))) { + return 1; + } + if (str_eq(name, EL_STR("sleep_ms"))) { + return 1; + } + if (str_eq(name, EL_STR("time_format"))) { + return 2; + } + if (str_eq(name, EL_STR("time_to_parts"))) { + return 1; + } + if (str_eq(name, EL_STR("time_from_parts"))) { + return 3; + } + if (str_eq(name, EL_STR("time_add"))) { + return 3; + } + if (str_eq(name, EL_STR("time_diff"))) { + return 3; + } + if (str_eq(name, EL_STR("uuid_new"))) { + return 0; + } + if (str_eq(name, EL_STR("uuid_v4"))) { + return 0; + } + if (str_eq(name, EL_STR("env"))) { + return 1; + } + if (str_eq(name, EL_STR("state_set"))) { + return 2; + } + if (str_eq(name, EL_STR("state_get"))) { + return 1; + } + if (str_eq(name, EL_STR("state_del"))) { + return 1; + } + if (str_eq(name, EL_STR("state_keys"))) { + return 0; + } + if (str_eq(name, EL_STR("float_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("int_to_float"))) { + return 1; + } + if (str_eq(name, EL_STR("float_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("format_float"))) { + return 2; + } + if (str_eq(name, EL_STR("decimal_round"))) { + return 2; + } + if (str_eq(name, EL_STR("str_to_float"))) { + return 1; + } + if (str_eq(name, EL_STR("math_sqrt"))) { + return 1; + } + if (str_eq(name, EL_STR("math_log"))) { + return 1; + } + if (str_eq(name, EL_STR("math_ln"))) { + return 1; + } + if (str_eq(name, EL_STR("math_sin"))) { + return 1; + } + if (str_eq(name, EL_STR("math_cos"))) { + return 1; + } + if (str_eq(name, EL_STR("math_pi"))) { + return 0; + } + if (str_eq(name, EL_STR("bool_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("exit_program"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_connect"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_send"))) { + return 2; + } + if (str_eq(name, EL_STR("dharma_activate"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_emit"))) { + return 2; + } + if (str_eq(name, EL_STR("dharma_field"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_strengthen"))) { + return 2; + } + if (str_eq(name, EL_STR("dharma_relationship"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_peers"))) { + return 0; + } + if (str_eq(name, EL_STR("engram_node"))) { + return 3; + } + if (str_eq(name, EL_STR("engram_node_full"))) { + return 8; + } + if (str_eq(name, EL_STR("engram_get_node"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_strengthen"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_forget"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_node_count"))) { + return 0; + } + if (str_eq(name, EL_STR("engram_search"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_scan_nodes"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_connect"))) { + return 4; + } + if (str_eq(name, EL_STR("engram_edge_between"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_neighbors"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_neighbors_filtered"))) { + return 3; + } + if (str_eq(name, EL_STR("engram_edge_count"))) { + return 0; + } + if (str_eq(name, EL_STR("engram_activate"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_save"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_load"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_get_node_json"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_search_json"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_scan_nodes_json"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_neighbors_json"))) { + return 3; + } + if (str_eq(name, EL_STR("engram_activate_json"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_stats_json"))) { + return 0; + } + if (str_eq(name, EL_STR("llm_call"))) { + return 2; + } + if (str_eq(name, EL_STR("llm_call_system"))) { + return 3; + } + if (str_eq(name, EL_STR("llm_call_agentic"))) { + return 4; + } + if (str_eq(name, EL_STR("llm_vision"))) { + return 4; + } + if (str_eq(name, EL_STR("llm_models"))) { + return 0; + } + if (str_eq(name, EL_STR("llm_register_tool"))) { + return 2; + } + if (str_eq(name, EL_STR("sha256_hex"))) { + return 1; + } + if (str_eq(name, EL_STR("sha256_bytes"))) { + return 1; + } + if (str_eq(name, EL_STR("hmac_sha256_hex"))) { + return 2; + } + if (str_eq(name, EL_STR("hmac_sha256_bytes"))) { + return 2; + } + if (str_eq(name, EL_STR("base64_encode"))) { + return 1; + } + if (str_eq(name, EL_STR("base64_decode"))) { + return 1; + } + if (str_eq(name, EL_STR("base64url_encode"))) { + return 1; + } + if (str_eq(name, EL_STR("base64url_decode"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_get"))) { + return 2; + } + if (str_eq(name, EL_STR("native_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_append"))) { + return 2; + } + if (str_eq(name, EL_STR("native_list_empty"))) { + return 0; + } + if (str_eq(name, EL_STR("native_list_clone"))) { + return 1; + } + if (str_eq(name, EL_STR("native_string_chars"))) { + return 1; + } + if (str_eq(name, EL_STR("native_int_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("append"))) { + return 2; + } + if (str_eq(name, EL_STR("len"))) { + return 1; + } + if (str_eq(name, EL_STR("get"))) { + return 2; + } + if (str_eq(name, EL_STR("map_get"))) { + return 2; + } + if (str_eq(name, EL_STR("map_set"))) { + return 3; + } + return (-1); + return 0; +} + +el_val_t arity_record_violation(el_val_t fn_name, el_val_t expected, el_val_t actual) { + el_val_t csv = state_get(EL_STR("__arity_violations")); + if (str_eq(csv, EL_STR(""))) { + csv = EL_STR(","); + } + el_val_t entry = el_str_concat(el_str_concat(el_str_concat(el_str_concat(fn_name, EL_STR("|")), native_int_to_str(expected)), EL_STR("|")), native_int_to_str(actual)); + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), entry), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__arity_violations"), el_str_concat(el_str_concat(csv, entry), EL_STR(","))); + return 1; + return 0; +} + +el_val_t arity_check_call(el_val_t fn_name, el_val_t actual) { + el_val_t expected = builtin_arity(fn_name); + if (expected < 0) { + return 1; + } + if (expected == actual) { + return 1; + } + arity_record_violation(fn_name, expected, actual); + return 1; + return 0; +} + +el_val_t emit_arity_violations(void) { + el_val_t csv = state_get(EL_STR("__arity_violations")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + if (str_eq(csv, EL_STR(","))) { + return 0; + } + el_val_t n = str_len(csv); + el_val_t i = 1; + while (i < n) { + el_val_t next_comma = str_index_of(str_slice(csv, i, n), EL_STR(",")); + if (next_comma < 0) { + return 0; + } + el_val_t entry = str_slice(csv, i, (i + next_comma)); + el_val_t p1 = str_index_of(entry, EL_STR("|")); + if (p1 > 0) { + el_val_t fn_name = str_slice(entry, 0, p1); + el_val_t rest = str_slice(entry, (p1 + 1), str_len(entry)); + el_val_t p2 = str_index_of(rest, EL_STR("|")); + if (p2 > 0) { + el_val_t exp_s = str_slice(rest, 0, p2); + el_val_t act_s = str_slice(rest, (p2 + 1), str_len(rest)); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("#error \"arity error: '"), fn_name), EL_STR("' takes ")), exp_s), EL_STR(" arguments, but called with ")), act_s), EL_STR("\""))); + } + } + i = ((i + next_comma) + 1); + } + return 0; +} + +el_val_t add_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__int_names")); + if (str_eq(csv, EL_STR(""))) { + csv = EL_STR(","); + } + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__int_names"), el_str_concat(el_str_concat(csv, name), EL_STR(","))); + return 1; + return 0; +} + +el_val_t build_int_names_for_params(el_val_t params) { + state_set(EL_STR("__int_names"), EL_STR(",")); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + el_val_t ptype = el_get_field(param, EL_STR("type")); + if (str_eq(ptype, EL_STR("Int"))) { + add_int_name(pname); + } + pi = (pi + 1); + } + return 1; + return 0; +} + +el_val_t cg_fn(el_val_t stmt) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + if (str_eq(fn_name, EL_STR("main"))) { + return 0; + } + el_val_t params = el_get_field(stmt, EL_STR("params")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t ret_type = el_get_field(stmt, EL_STR("ret_type")); + el_val_t params_c = params_to_c(params); + el_val_t decorator = el_get_field(stmt, EL_STR("decorator")); + if (vbd_has_restricted_call(body)) { + if (!str_eq(decorator, EL_STR("manager"))) { + emit_line(el_str_concat(el_str_concat(EL_STR("#error \"VBD violation: dharma_emit/dharma_field called from non-@manager fn '"), fn_name), EL_STR("'\""))); + } + } + build_int_names_for_params(params); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_val_t "), fn_name), EL_STR("(")), params_c), EL_STR(") {"))); + el_val_t decl = native_list_empty(); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + decl = native_list_append(decl, pname); + pi = (pi + 1); + } + el_val_t body_xformed = body; + if (!str_eq(ret_type, EL_STR("Void"))) { + body_xformed = transform_implicit_return(body); + } + cg_stmts(body_xformed, EL_STR(" "), decl); + emit_line(EL_STR(" return 0;")); + emit_line(EL_STR("}")); + emit_blank(); + return 0; +} + +el_val_t is_fndef(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("FnDef"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_top_level_decl(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("TypeDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("Import"))) { + return 1; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t cgi_arg(el_val_t value, el_val_t has_value) { + if (has_value) { + return el_str_concat(el_str_concat(EL_STR("EL_STR("), c_str_lit(value)), EL_STR(")")); + } + return EL_STR("EL_NULL"); + return 0; +} + +el_val_t vbd_is_restricted_name(el_val_t name) { + if (str_eq(name, EL_STR("dharma_emit"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_field"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t vbd_expr_has_restricted_call(el_val_t expr) { + el_val_t kind = el_get_field(expr, EL_STR("expr")); + if (str_eq(kind, EL_STR("Call"))) { + el_val_t func = el_get_field(expr, EL_STR("func")); + el_val_t fk = el_get_field(func, EL_STR("expr")); + if (str_eq(fk, EL_STR("Ident"))) { + el_val_t fname = el_get_field(func, EL_STR("name")); + if (vbd_is_restricted_name(fname)) { + return 1; + } + } + if (vbd_expr_has_restricted_call(func)) { + return 1; + } + el_val_t args = el_get_field(expr, EL_STR("args")); + el_val_t an = native_list_len(args); + el_val_t ai = 0; + while (ai < an) { + el_val_t a = native_list_get(args, ai); + if (vbd_expr_has_restricted_call(a)) { + return 1; + } + ai = (ai + 1); + } + return 0; + } + if (str_eq(kind, EL_STR("BinOp"))) { + el_val_t l = el_get_field(expr, EL_STR("left")); + el_val_t r = el_get_field(expr, EL_STR("right")); + if (vbd_expr_has_restricted_call(l)) { + return 1; + } + if (vbd_expr_has_restricted_call(r)) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("Not"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(kind, EL_STR("Neg"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(kind, EL_STR("Field"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("object"))); + } + if (str_eq(kind, EL_STR("Index"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("object")))) { + return 1; + } + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("index")))) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("Try"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(kind, EL_STR("Array"))) { + el_val_t elems = el_get_field(expr, EL_STR("elems")); + el_val_t n = native_list_len(elems); + el_val_t i = 0; + while (i < n) { + el_val_t e = native_list_get(elems, i); + if (vbd_expr_has_restricted_call(e)) { + return 1; + } + i = (i + 1); + } + return 0; + } + if (str_eq(kind, EL_STR("Map"))) { + el_val_t pairs = el_get_field(expr, EL_STR("pairs")); + el_val_t n = native_list_len(pairs); + el_val_t i = 0; + while (i < n) { + el_val_t pair = native_list_get(pairs, i); + el_val_t v = el_get_field(pair, EL_STR("value")); + if (vbd_expr_has_restricted_call(v)) { + return 1; + } + i = (i + 1); + } + return 0; + } + if (str_eq(kind, EL_STR("If"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("cond")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(expr, EL_STR("then")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(expr, EL_STR("else")))) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("For"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("list")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(expr, EL_STR("body")))) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("Match"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("subject")))) { + return 1; + } + el_val_t arms = el_get_field(expr, EL_STR("arms")); + el_val_t n = native_list_len(arms); + el_val_t i = 0; + while (i < n) { + el_val_t arm = native_list_get(arms, i); + el_val_t body = el_get_field(arm, EL_STR("body")); + if (vbd_expr_has_restricted_call(body)) { + return 1; + } + i = (i + 1); + } + return 0; + } + return 0; + return 0; +} + +el_val_t vbd_has_restricted_call(el_val_t stmts) { + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + while (i < n) { + el_val_t s = native_list_get(stmts, i); + el_val_t sk = el_get_field(s, EL_STR("stmt")); + if (str_eq(sk, EL_STR("Let"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("value")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("Return"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("value")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("Expr"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("value")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("While"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("cond")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(s, EL_STR("body")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("For"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("list")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(s, EL_STR("body")))) { + return 1; + } + } + i = (i + 1); + } + return 0; + return 0; +} + +el_val_t codegen(el_val_t stmts, el_val_t source) { + el_val_t n_top = native_list_len(stmts); + el_val_t cgi_count = 0; + el_val_t cgi_block = el_map_new(1, "stmt", EL_STR("None")); + el_val_t svc_count = 0; + el_val_t svc_block = el_map_new(1, "stmt", EL_STR("None")); + el_val_t ti = 0; + while (ti < n_top) { + el_val_t s = native_list_get(stmts, ti); + el_val_t sk = el_get_field(s, EL_STR("stmt")); + if (str_eq(sk, EL_STR("CgiBlock"))) { + cgi_count = (cgi_count + 1); + if (cgi_count == 1) { + cgi_block = s; + } + } + if (str_eq(sk, EL_STR("ServiceBlock"))) { + svc_count = (svc_count + 1); + if (svc_count == 1) { + svc_block = s; + } + } + ti = (ti + 1); + } + if (cgi_count > 1) { + emit_line(EL_STR("#error \"El: multiple cgi blocks in program (only one allowed)\"")); + } + if (svc_count > 1) { + emit_line(EL_STR("#error \"El: multiple service blocks in program (only one allowed)\"")); + } + if (cgi_count >= 1) { + if (svc_count >= 1) { + emit_line(EL_STR("#error \"El: program declares both cgi and service blocks (mutually exclusive — pick one)\"")); + } + } + el_val_t kind = EL_STR("utility"); + if (cgi_count >= 1) { + kind = EL_STR("cgi"); + } + if (svc_count >= 1) { + kind = EL_STR("service"); + } + state_set(EL_STR("__program_kind"), kind); + state_set(EL_STR("__cap_violations"), EL_STR("")); + state_set(EL_STR("__arity_violations"), EL_STR("")); + emit_line(EL_STR("#include ")); + emit_line(EL_STR("#include ")); + emit_line(EL_STR("#include \"el_runtime.h\"")); + emit_blank(); + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("FnDef"))) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + if (!str_eq(fn_name, EL_STR("main"))) { + el_val_t params = el_get_field(stmt, EL_STR("params")); + el_val_t params_c = params_to_c(params); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_val_t "), fn_name), EL_STR("(")), params_c), EL_STR(");"))); + } + } + i = (i + 1); + } + emit_blank(); + el_val_t has_toplevel_lets = 0; + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + el_val_t ltype = el_get_field(stmt, EL_STR("type")); + if (str_eq(ltype, EL_STR("Int"))) { + add_int_name(name); + } + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t vk = el_get_field(val, EL_STR("expr")); + if (str_eq(vk, EL_STR("Int"))) { + add_int_name(name); + } + emit_line(el_str_concat(el_str_concat(EL_STR("el_val_t "), name), EL_STR(";"))); + has_toplevel_lets = 1; + } + i = (i + 1); + } + if (has_toplevel_lets) { + emit_blank(); + } + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (is_fndef(stmt)) { + cg_fn(stmt); + } + i = (i + 1); + } + emit_line(EL_STR("int main(int argc, char** argv) {")); + emit_line(EL_STR(" el_runtime_init_args(argc, argv);")); + if (cgi_count >= 1) { + el_val_t cname = el_get_field(cgi_block, EL_STR("name")); + el_val_t cdid = el_get_field(cgi_block, EL_STR("dharma_id")); + el_val_t cprin = el_get_field(cgi_block, EL_STR("principal")); + el_val_t cnet = el_get_field(cgi_block, EL_STR("network")); + el_val_t ceng = el_get_field(cgi_block, EL_STR("engram")); + el_val_t has_did = el_get_field(cgi_block, EL_STR("has_dharma_id")); + el_val_t has_prin = el_get_field(cgi_block, EL_STR("has_principal")); + el_val_t has_net = el_get_field(cgi_block, EL_STR("has_network")); + el_val_t has_eng = el_get_field(cgi_block, EL_STR("has_engram")); + el_val_t arg_name = el_str_concat(el_str_concat(EL_STR("EL_STR("), c_str_lit(cname)), EL_STR(")")); + el_val_t arg_did = cgi_arg(cdid, has_did); + el_val_t arg_prin = cgi_arg(cprin, has_prin); + el_val_t arg_net = cgi_arg(cnet, has_net); + el_val_t arg_eng = cgi_arg(ceng, has_eng); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR(" el_cgi_init("), arg_name), EL_STR(", ")), arg_did), EL_STR(", ")), arg_prin), EL_STR(", ")), arg_net), EL_STR(", ")), arg_eng), EL_STR(");"))); + } + el_val_t main_decl = native_list_empty(); + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + main_decl = native_list_append(main_decl, name); + } + i = (i + 1); + } + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (is_fndef(stmt)) { + } else { + if (is_top_level_decl(stmt)) { + } else { + main_decl = cg_stmt(stmt, EL_STR(" "), main_decl); + } + } + i = (i + 1); + } + emit_line(EL_STR(" return 0;")); + emit_line(EL_STR("}")); + emit_blank(); + emit_cap_violations(); + emit_arity_violations(); + return EL_STR(""); + return 0; +} + +el_val_t js_escape(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t total = native_list_len(chars); + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < total) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("\""))) { + out = el_str_concat(out, EL_STR("\\\"")); + } else { + if (str_eq(ch, EL_STR("\\"))) { + out = el_str_concat(out, EL_STR("\\\\")); + } else { + if (str_eq(ch, EL_STR("\n"))) { + out = el_str_concat(out, EL_STR("\\n")); + } else { + if (str_eq(ch, EL_STR("\r"))) { + out = el_str_concat(out, EL_STR("\\r")); + } else { + if (str_eq(ch, EL_STR("\t"))) { + out = el_str_concat(out, EL_STR("\\t")); + } else { + out = el_str_concat(out, ch); + } + } + } + } + } + i = (i + 1); + } + return out; + return 0; +} + +el_val_t js_str_lit(el_val_t s) { + return el_str_concat(el_str_concat(EL_STR("\""), js_escape(s)), EL_STR("\"")); + return 0; +} + +el_val_t js_emit_line(el_val_t line) { + println(line); + return 0; +} + +el_val_t js_emit_blank(void) { + println(EL_STR("")); + return 0; +} + +el_val_t js_binop(el_val_t op) { + if (str_eq(op, EL_STR("Plus"))) { + return EL_STR("+"); + } + if (str_eq(op, EL_STR("Minus"))) { + return EL_STR("-"); + } + if (str_eq(op, EL_STR("Star"))) { + return EL_STR("*"); + } + if (str_eq(op, EL_STR("Slash"))) { + return EL_STR("/"); + } + if (str_eq(op, EL_STR("Percent"))) { + return EL_STR("%"); + } + if (str_eq(op, EL_STR("EqEq"))) { + return EL_STR("==="); + } + if (str_eq(op, EL_STR("NotEq"))) { + return EL_STR("!=="); + } + if (str_eq(op, EL_STR("Lt"))) { + return EL_STR("<"); + } + if (str_eq(op, EL_STR("Gt"))) { + return EL_STR(">"); + } + if (str_eq(op, EL_STR("LtEq"))) { + return EL_STR("<="); + } + if (str_eq(op, EL_STR("GtEq"))) { + return EL_STR(">="); + } + if (str_eq(op, EL_STR("And"))) { + return EL_STR("&&"); + } + if (str_eq(op, EL_STR("Or"))) { + return EL_STR("||"); + } + return op; + return 0; +} + +el_val_t js_is_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__js_int_names")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + return str_contains(csv, el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(","))); + return 0; +} + +el_val_t js_add_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__js_int_names")); + if (str_eq(csv, EL_STR(""))) { + csv = EL_STR(","); + } + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__js_int_names"), el_str_concat(el_str_concat(csv, name), EL_STR(","))); + return 1; + return 0; +} + +el_val_t js_build_int_names_for_params(el_val_t params) { + state_set(EL_STR("__js_int_names"), EL_STR(",")); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + el_val_t ptype = el_get_field(param, EL_STR("type")); + if (str_eq(ptype, EL_STR("Int"))) { + js_add_int_name(pname); + } + pi = (pi + 1); + } + return 1; + return 0; +} + +el_val_t js_is_int_call(el_val_t call_expr) { + el_val_t func = el_get_field(call_expr, EL_STR("func")); + el_val_t fk = el_get_field(func, EL_STR("expr")); + if (!str_eq(fk, EL_STR("Ident"))) { + return 0; + } + el_val_t name = el_get_field(func, EL_STR("name")); + if (str_eq(name, EL_STR("str_len"))) { + return 1; + } + if (str_eq(name, EL_STR("str_index_of"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("str_char_code"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("el_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("len"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get_int"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now_utc"))) { + return 1; + } + if (str_eq(name, EL_STR("el_abs"))) { + return 1; + } + if (str_eq(name, EL_STR("el_max"))) { + return 1; + } + if (str_eq(name, EL_STR("el_min"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t js_cg_expr(el_val_t expr) { + el_val_t kind = el_get_field(expr, EL_STR("expr")); + if (str_eq(kind, EL_STR("Int"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return v; + } + if (str_eq(kind, EL_STR("Float"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return v; + } + if (str_eq(kind, EL_STR("Str"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return js_str_lit(v); + } + if (str_eq(kind, EL_STR("Bool"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + if (str_eq(v, EL_STR("true"))) { + return EL_STR("true"); + } + return EL_STR("false"); + } + if (str_eq(kind, EL_STR("Nil"))) { + return EL_STR("null"); + } + if (str_eq(kind, EL_STR("Ident"))) { + el_val_t name = el_get_field(expr, EL_STR("name")); + return name; + } + if (str_eq(kind, EL_STR("Not"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = js_cg_expr(inner); + return el_str_concat(EL_STR("!"), inner_c); + } + if (str_eq(kind, EL_STR("Neg"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = js_cg_expr(inner); + return el_str_concat(el_str_concat(EL_STR("(-"), inner_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("BinOp"))) { + el_val_t op = el_get_field(expr, EL_STR("op")); + el_val_t left = el_get_field(expr, EL_STR("left")); + el_val_t right = el_get_field(expr, EL_STR("right")); + el_val_t left_c = js_cg_expr(left); + el_val_t right_c = js_cg_expr(right); + el_val_t left_kind = el_get_field(left, EL_STR("expr")); + el_val_t right_kind = el_get_field(right, EL_STR("expr")); + if (str_eq(op, EL_STR("Plus"))) { + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Call"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_call(right)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(right_kind, EL_STR("Ident"))) { + if (str_eq(left_kind, EL_STR("Call"))) { + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(rname)) { + if (js_is_int_call(left)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Call"))) { + if (str_eq(right_kind, EL_STR("Call"))) { + if (js_is_int_call(left)) { + if (js_is_int_call(right)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(op, EL_STR("EqEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(op, EL_STR("NotEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + el_val_t op_c = js_binop(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Call"))) { + el_val_t func = el_get_field(expr, EL_STR("func")); + el_val_t args = el_get_field(expr, EL_STR("args")); + el_val_t arity = native_list_len(args); + el_val_t func_kind = el_get_field(func, EL_STR("expr")); + el_val_t args_c = EL_STR(""); + el_val_t i = 0; + while (i < arity) { + el_val_t arg = native_list_get(args, i); + el_val_t arg_c = js_cg_expr(arg); + if (i > 0) { + args_c = el_str_concat(args_c, EL_STR(", ")); + } + args_c = el_str_concat(args_c, arg_c); + i = (i + 1); + } + if (str_eq(func_kind, EL_STR("Ident"))) { + el_val_t fn_name = el_get_field(func, EL_STR("name")); + return el_str_concat(el_str_concat(el_str_concat(fn_name, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(func_kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(func, EL_STR("object")); + el_val_t field = el_get_field(func, EL_STR("field")); + el_val_t obj_c = js_cg_expr(obj); + if (arity > 0) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(", ")), args_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(")")); + } + el_val_t fn_c = js_cg_expr(func); + return el_str_concat(el_str_concat(el_str_concat(fn_c, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t field = el_get_field(expr, EL_STR("field")); + el_val_t obj_c = js_cg_expr(obj); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", ")), js_str_lit(field)), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Index"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t idx = el_get_field(expr, EL_STR("index")); + el_val_t obj_c = js_cg_expr(obj); + el_val_t idx_c = js_cg_expr(idx); + el_val_t idx_kind = el_get_field(idx, EL_STR("expr")); + if (str_eq(idx_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_list_get("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Array"))) { + el_val_t elems = el_get_field(expr, EL_STR("elems")); + el_val_t n = native_list_len(elems); + if (n == 0) { + return EL_STR("[]"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t elem = native_list_get(elems, i); + el_val_t elem_c = js_cg_expr(elem); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(items, elem_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(EL_STR("["), items), EL_STR("]")); + } + if (str_eq(kind, EL_STR("Map"))) { + el_val_t pairs = el_get_field(expr, EL_STR("pairs")); + el_val_t n = native_list_len(pairs); + if (n == 0) { + return EL_STR("{}"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t pair = native_list_get(pairs, i); + el_val_t key = el_get_field(pair, EL_STR("key")); + el_val_t val = el_get_field(pair, EL_STR("value")); + el_val_t val_c = js_cg_expr(val); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(el_str_concat(el_str_concat(items, js_str_lit(key)), EL_STR(": ")), val_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(EL_STR("{"), items), EL_STR("}")); + } + if (str_eq(kind, EL_STR("Try"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + return js_cg_expr(inner); + } + if (str_eq(kind, EL_STR("If"))) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t cond_c = js_cg_expr(cond); + return el_str_concat(el_str_concat(EL_STR("("), cond_c), EL_STR(" ? 1 : 0)")); + } + if (str_eq(kind, EL_STR("Match"))) { + return js_cg_match(expr); + } + return EL_STR("null"); + return 0; +} + +el_val_t js_next_match_id(void) { + el_val_t csv = state_get(EL_STR("__js_match_counter")); + el_val_t n = 0; + if (!str_eq(csv, EL_STR(""))) { + n = str_to_int(csv); + } + n = (n + 1); + state_set(EL_STR("__js_match_counter"), native_int_to_str(n)); + return native_int_to_str(n); + return 0; +} + +el_val_t js_cg_match(el_val_t expr) { + el_val_t subject = el_get_field(expr, EL_STR("subject")); + el_val_t arms = el_get_field(expr, EL_STR("arms")); + el_val_t subj_c = js_cg_expr(subject); + el_val_t id = js_next_match_id(); + el_val_t subj_var = el_str_concat(EL_STR("_match_subj_"), id); + el_val_t out = el_str_concat(el_str_concat(EL_STR("(("), subj_var), EL_STR(") => { ")); + el_val_t n = native_list_len(arms); + el_val_t i = 0; + while (i < n) { + el_val_t arm = native_list_get(arms, i); + el_val_t pat = el_get_field(arm, EL_STR("pattern")); + el_val_t body = el_get_field(arm, EL_STR("body")); + el_val_t pkind = el_get_field(pat, EL_STR("pattern")); + el_val_t body_c = js_cg_expr(body); + if (str_eq(pkind, EL_STR("Wildcard"))) { + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("return (")), body_c), EL_STR("); ")); + } else { + if (str_eq(pkind, EL_STR("Binding"))) { + el_val_t bname = el_get_field(pat, EL_STR("name")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ const ")), bname), EL_STR(" = ")), subj_var), EL_STR("; return (")), body_c), EL_STR("); } ")); + } else { + if (str_eq(pkind, EL_STR("LitInt"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" === ")), v), EL_STR(") return (")), body_c), EL_STR("); ")); + } else { + if (str_eq(pkind, EL_STR("LitStr"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (str_eq(")), subj_var), EL_STR(", ")), js_str_lit(v)), EL_STR(")) return (")), body_c), EL_STR("); ")); + } else { + if (str_eq(pkind, EL_STR("LitBool"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + el_val_t bv = EL_STR("false"); + if (str_eq(v, EL_STR("true"))) { + bv = EL_STR("true"); + } + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" === ")), bv), EL_STR(") return (")), body_c), EL_STR("); ")); + } else { + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("return (")), body_c), EL_STR("); ")); + } + } + } + } + } + i = (i + 1); + } + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("return null; })(")), subj_c), EL_STR(")")); + return out; + return 0; +} + +el_val_t js_list_contains(el_val_t lst, el_val_t s) { + el_val_t n = native_list_len(lst); + el_val_t i = 0; + while (i < n) { + el_val_t item = native_list_get(lst, i); + if (str_eq(item, s)) { + return 1; + } + i = (i + 1); + } + return 0; + return 0; +} + +el_val_t js_cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_c = js_cg_expr(val); + el_val_t ltype = el_get_field(stmt, EL_STR("type")); + if (str_eq(ltype, EL_STR("Int"))) { + js_add_int_name(name); + } + el_val_t vk = el_get_field(val, EL_STR("expr")); + if (str_eq(vk, EL_STR("Int"))) { + js_add_int_name(name); + } + if (js_list_contains(declared, name)) { + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, name), EL_STR(" = ")), val_c), EL_STR(";"))); + return declared; + } else { + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("let ")), name), EL_STR(" = ")), val_c), EL_STR(";"))); + return native_list_append(declared, name); + } + } + if (str_eq(kind, EL_STR("Return"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("Nil"))) { + js_emit_line(el_str_concat(indent, EL_STR("return null;"))); + } else { + el_val_t val_c = js_cg_expr(val); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("return ")), val_c), EL_STR(";"))); + } + return declared; + } + if (str_eq(kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + js_cg_if_stmt(val, indent, declared); + return declared; + } + if (str_eq(val_kind, EL_STR("For"))) { + js_cg_for_stmt(val, indent, declared); + return declared; + } + el_val_t val_c = js_cg_expr(val); + js_emit_line(el_str_concat(el_str_concat(indent, val_c), EL_STR(";"))); + return declared; + } + if (str_eq(kind, EL_STR("While"))) { + el_val_t cond = el_get_field(stmt, EL_STR("cond")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t cond_c = js_cg_expr(cond); + cond_c = js_strip_outer_parens(cond_c); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("while (")), cond_c), EL_STR(") {"))); + js_cg_stmts(body, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + js_emit_line(el_str_concat(indent, EL_STR("}"))); + return declared; + } + if (str_eq(kind, EL_STR("For"))) { + el_val_t item = el_get_field(stmt, EL_STR("item")); + el_val_t list_expr = el_get_field(stmt, EL_STR("list")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + js_cg_for_body(item, list_expr, body, indent, declared); + return declared; + } + if (str_eq(kind, EL_STR("FnDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("TypeDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("Import"))) { + return declared; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + el_val_t cname = el_get_field(stmt, EL_STR("name")); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("// cgi block '")), cname), EL_STR("' — no-op in JS target (server-side concept)"))); + return declared; + } + if (str_eq(kind, EL_STR("ServiceBlock"))) { + el_val_t sname = el_get_field(stmt, EL_STR("name")); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("// service block '")), sname), EL_STR("' — no-op in JS target"))); + return declared; + } + return declared; + return 0; +} + +el_val_t js_strip_outer_parens(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t n = native_list_len(chars); + if (n < 2) { + return s; + } + el_val_t first = native_list_get(chars, 0); + el_val_t last = native_list_get(chars, (n - 1)); + if (str_eq(first, EL_STR("("))) { + if (str_eq(last, EL_STR(")"))) { + el_val_t depth = 1; + el_val_t i = 1; + el_val_t balanced = 1; + while (i < (n - 1)) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("("))) { + depth = (depth + 1); + } + if (str_eq(ch, EL_STR(")"))) { + depth = (depth - 1); + if (depth == 0) { + balanced = 0; + i = n; + } + } + i = (i + 1); + } + if (balanced) { + el_val_t inner = EL_STR(""); + el_val_t j = 1; + while (j < (n - 1)) { + el_val_t ch = native_list_get(chars, j); + inner = el_str_concat(inner, ch); + j = (j + 1); + } + return inner; + } + } + } + return s; + return 0; +} + +el_val_t js_cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t then_stmts = el_get_field(expr, EL_STR("then")); + el_val_t else_stmts = el_get_field(expr, EL_STR("else")); + el_val_t has_else = el_get_field(expr, EL_STR("has_else")); + el_val_t cond_c = js_cg_expr(cond); + cond_c = js_strip_outer_parens(cond_c); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("if (")), cond_c), EL_STR(") {"))); + js_cg_stmts(then_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + if (has_else) { + js_emit_line(el_str_concat(indent, EL_STR("} else {"))); + js_cg_stmts(else_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + } + js_emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t js_cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared) { + el_val_t list_c = js_cg_expr(list_expr); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("for (const ")), item), EL_STR(" of ")), list_c), EL_STR(") {"))); + el_val_t body_decl = native_list_clone(declared); + body_decl = native_list_append(body_decl, item); + js_cg_stmts(body, el_str_concat(indent, EL_STR(" ")), body_decl); + js_emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t js_cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t item = el_get_field(expr, EL_STR("item")); + el_val_t list_expr = el_get_field(expr, EL_STR("list")); + el_val_t body = el_get_field(expr, EL_STR("body")); + js_cg_for_body(item, list_expr, body, indent, declared); + return 0; +} + +el_val_t js_cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared) { + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + el_val_t decl = declared; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + decl = js_cg_stmt(stmt, indent, decl); + i = (i + 1); + } + return decl; + return 0; +} + +el_val_t js_params_str(el_val_t params) { + el_val_t n = native_list_len(params); + if (n == 0) { + return EL_STR(""); + } + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t param = native_list_get(params, i); + el_val_t name = el_get_field(param, EL_STR("name")); + if (i > 0) { + out = el_str_concat(out, EL_STR(", ")); + } + out = el_str_concat(out, name); + i = (i + 1); + } + return out; + return 0; +} + +el_val_t js_transform_implicit_return(el_val_t body) { + el_val_t n = native_list_len(body); + if (n == 0) { + return body; + } + el_val_t last = native_list_get(body, (n - 1)); + el_val_t last_kind = el_get_field(last, EL_STR("stmt")); + if (str_eq(last_kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(last, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + return body; + } + if (str_eq(val_kind, EL_STR("For"))) { + return body; + } + el_val_t new_body = native_list_empty(); + el_val_t i = 0; + while (i < (n - 1)) { + new_body = native_list_append(new_body, native_list_get(body, i)); + i = (i + 1); + } + el_val_t return_stmt = el_map_new(2, "stmt", EL_STR("Return"), "value", val); + new_body = native_list_append(new_body, return_stmt); + return new_body; + } + return body; + return 0; +} + +el_val_t js_cg_fn(el_val_t stmt) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + el_val_t params = el_get_field(stmt, EL_STR("params")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t ret_type = el_get_field(stmt, EL_STR("ret_type")); + el_val_t params_str = js_params_str(params); + js_build_int_names_for_params(params); + if (str_eq(fn_name, EL_STR("main"))) { + js_emit_line(el_str_concat(el_str_concat(EL_STR("function main("), params_str), EL_STR(") {"))); + } else { + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("function "), fn_name), EL_STR("(")), params_str), EL_STR(") {"))); + } + el_val_t decl = native_list_empty(); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + decl = native_list_append(decl, pname); + pi = (pi + 1); + } + el_val_t body_xformed = body; + if (!str_eq(ret_type, EL_STR("Void"))) { + body_xformed = js_transform_implicit_return(body); + } + js_cg_stmts(body_xformed, EL_STR(" "), decl); + js_emit_line(EL_STR("}")); + js_emit_blank(); + return 0; +} + +el_val_t js_is_fndef(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("FnDef"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t js_is_top_level_decl(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("TypeDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("Import"))) { + return 1; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + return 1; + } + if (str_eq(kind, EL_STR("ServiceBlock"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t codegen_js(el_val_t stmts, el_val_t source) { + state_set(EL_STR("__js_int_names"), EL_STR("")); + state_set(EL_STR("__js_match_counter"), EL_STR("")); + js_emit_line(EL_STR("// Generated by elc --target=js")); + js_emit_line(EL_STR("// Runtime: foundation/el/el-compiler/runtime/el_runtime.js")); + js_emit_line(EL_STR("import \"./el_runtime.js\";")); + js_emit_line(EL_STR("const {")); + js_emit_line(EL_STR(" println, print, el_str_concat, str_concat, str_eq, str_starts_with, str_ends_with,")); + js_emit_line(EL_STR(" str_len, int_to_str, str_to_int, str_slice, str_contains, str_replace,")); + js_emit_line(EL_STR(" str_to_upper, str_to_lower, str_trim, str_index_of, str_split, str_char_at,")); + js_emit_line(EL_STR(" str_char_code, str_lower, str_upper, el_abs, el_max, el_min,")); + js_emit_line(EL_STR(" el_list_new, el_list_len, el_list_get, el_list_append, el_list_empty, el_list_clone,")); + js_emit_line(EL_STR(" list_push, list_join, list_range,")); + js_emit_line(EL_STR(" el_map_new, el_get_field, el_map_get, el_map_set,")); + js_emit_line(EL_STR(" http_get, http_post, http_post_json,")); + js_emit_line(EL_STR(" fs_read, fs_write, fs_list,")); + js_emit_line(EL_STR(" json_parse, json_stringify, json_get, json_get_string, json_get_int,")); + js_emit_line(EL_STR(" time_now, time_now_utc, sleep_ms, bool_to_str, exit_program,")); + js_emit_line(EL_STR(" el_retain, el_release,")); + js_emit_line(EL_STR(" append, len, get, map_get, map_set,")); + js_emit_line(EL_STR(" native_list_get, native_list_len, native_list_append, native_list_empty,")); + js_emit_line(EL_STR(" native_list_clone, native_string_chars, native_int_to_str,")); + js_emit_line(EL_STR(" args, state_set, state_get, state_del, state_keys, env,")); + js_emit_line(EL_STR(" dharma_connect, dharma_send, dharma_emit, dharma_field, dharma_activate,")); + js_emit_line(EL_STR(" engram_node, engram_search, engram_activate,")); + js_emit_line(EL_STR(" llm_call, llm_call_system,")); + js_emit_line(EL_STR("} = globalThis.__el;")); + js_emit_blank(); + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (js_is_fndef(stmt)) { + js_cg_fn(stmt); + } + i = (i + 1); + } + el_val_t has_main = 0; + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + el_val_t sk = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(sk, EL_STR("FnDef"))) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + if (str_eq(fn_name, EL_STR("main"))) { + has_main = 1; + } + } + i = (i + 1); + } + el_val_t main_decl = native_list_empty(); + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (js_is_fndef(stmt)) { + } else { + if (js_is_top_level_decl(stmt)) { + } else { + main_decl = js_cg_stmt(stmt, EL_STR(""), main_decl); + } + } + i = (i + 1); + } + if (has_main) { + js_emit_blank(); + js_emit_line(EL_STR("main();")); + } + return EL_STR(""); + return 0; +} + +el_val_t compile(el_val_t source) { + el_val_t tokens = lex(source); + el_val_t stmts = parse(tokens); + return codegen(stmts, source); + return 0; +} + +el_val_t compile_js(el_val_t source) { + el_val_t tokens = lex(source); + el_val_t stmts = parse(tokens); + return codegen_js(stmts, source); + return 0; +} + +el_val_t compile_dispatch(el_val_t tgt, el_val_t source) { + if (str_eq(tgt, EL_STR("js"))) { + return compile_js(source); + } + return compile(source); + return 0; +} + +el_val_t detect_target(el_val_t argv) { + el_val_t n = native_list_len(argv); + el_val_t i = 0; + while (i < n) { + el_val_t a = native_list_get(argv, i); + if (str_starts_with(a, EL_STR("--target="))) { + el_val_t v = str_slice(a, 9, str_len(a)); + return v; + } + i = (i + 1); + } + return EL_STR("c"); + return 0; +} + +el_val_t strip_flags(el_val_t argv) { + el_val_t out = native_list_empty(); + el_val_t n = native_list_len(argv); + el_val_t i = 0; + while (i < n) { + el_val_t a = native_list_get(argv, i); + if (!str_starts_with(a, EL_STR("--"))) { + out = native_list_append(out, a); + } + i = (i + 1); + } + return out; + return 0; +} + +int main(int argc, char** argv) { + el_runtime_init_args(argc, argv); + _argv = args(); + _target = detect_target(_argv); + _positional = strip_flags(_argv); + _argc = native_list_len(_positional); + if (_argc >= 1) { + el_val_t _src_path = native_list_get(_positional, 0); + el_val_t _source = fs_read(_src_path); + el_val_t _out = compile_dispatch(_target, _source); + if (_argc >= 2) { + el_val_t _out_path = native_list_get(_positional, 1); + fs_write(_out_path, _out); + } + } + return 0; +} + diff --git a/dist/elc-new.c b/dist/elc-new.c new file mode 100644 index 0000000..4f147e0 --- /dev/null +++ b/dist/elc-new.c @@ -0,0 +1,4773 @@ +#include +#include +#include "el_runtime.h" + +el_val_t is_digit(el_val_t ch); +el_val_t is_alpha(el_val_t ch); +el_val_t is_alnum_or_underscore(el_val_t ch); +el_val_t is_whitespace(el_val_t ch); +el_val_t make_tok(el_val_t kind, el_val_t value); +el_val_t keyword_kind(el_val_t word); +el_val_t scan_digits(el_val_t chars, el_val_t start, el_val_t total); +el_val_t scan_ident(el_val_t chars, el_val_t start, el_val_t total); +el_val_t scan_string(el_val_t chars, el_val_t start, el_val_t total); +el_val_t lex(el_val_t source); +el_val_t tok_at(el_val_t tokens, el_val_t pos); +el_val_t tok_kind(el_val_t tokens, el_val_t pos); +el_val_t tok_value(el_val_t tokens, el_val_t pos); +el_val_t expect(el_val_t tokens, el_val_t pos, el_val_t kind); +el_val_t make_result(el_val_t node, el_val_t pos); +el_val_t skip_type(el_val_t tokens, el_val_t pos); +el_val_t parse_params(el_val_t tokens, el_val_t pos); +el_val_t parse_primary(el_val_t tokens, el_val_t pos); +el_val_t parse_if(el_val_t tokens, el_val_t pos); +el_val_t parse_match(el_val_t tokens, el_val_t pos); +el_val_t parse_pattern(el_val_t tokens, el_val_t pos); +el_val_t parse_for_expr(el_val_t tokens, el_val_t pos); +el_val_t parse_block(el_val_t tokens, el_val_t pos); +el_val_t parse_postfix(el_val_t tokens, el_val_t pos); +el_val_t op_precedence(el_val_t kind); +el_val_t is_binop(el_val_t kind); +el_val_t parse_binop(el_val_t tokens, el_val_t pos, el_val_t min_prec); +el_val_t parse_expr(el_val_t tokens, el_val_t pos); +el_val_t parse_stmt(el_val_t tokens, el_val_t pos); +el_val_t parse(el_val_t tokens); +el_val_t c_escape(el_val_t s); +el_val_t c_str_lit(el_val_t s); +el_val_t el_type_to_c(el_val_t type_str); +el_val_t emit_line(el_val_t line); +el_val_t emit_blank(void); +el_val_t binop_to_c(el_val_t op); +el_val_t cg_expr(el_val_t expr); +el_val_t next_match_id(void); +el_val_t cg_match(el_val_t expr); +el_val_t next_if_id(void); +el_val_t cg_if_expr_arm(el_val_t stmts, el_val_t result_var); +el_val_t cg_if_expr(el_val_t expr); +el_val_t list_contains(el_val_t lst, el_val_t s); +el_val_t cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared); +el_val_t strip_outer_parens(el_val_t s); +el_val_t cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared); +el_val_t cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared); +el_val_t param_decl(el_val_t param, el_val_t idx); +el_val_t params_to_c(el_val_t params); +el_val_t transform_implicit_return(el_val_t body); +el_val_t is_int_name(el_val_t name); +el_val_t is_int_call(el_val_t call_expr); +el_val_t is_int_expr(el_val_t expr); +el_val_t cap_record_violation(el_val_t kind, el_val_t fn_name); +el_val_t is_self_formation_call(el_val_t fn_name); +el_val_t is_dharma_call(el_val_t fn_name); +el_val_t is_llm_call(el_val_t fn_name); +el_val_t cap_check_call(el_val_t fn_name); +el_val_t emit_cap_violations(void); +el_val_t builtin_arity(el_val_t name); +el_val_t arity_record_violation(el_val_t fn_name, el_val_t expected, el_val_t actual); +el_val_t arity_check_call(el_val_t fn_name, el_val_t actual); +el_val_t emit_arity_violations(void); +el_val_t add_int_name(el_val_t name); +el_val_t build_int_names_for_params(el_val_t params); +el_val_t cg_fn(el_val_t stmt); +el_val_t is_fndef(el_val_t stmt); +el_val_t is_top_level_decl(el_val_t stmt); +el_val_t cgi_arg(el_val_t value, el_val_t has_value); +el_val_t vbd_is_restricted_name(el_val_t name); +el_val_t vbd_expr_has_restricted_call(el_val_t expr); +el_val_t vbd_has_restricted_call(el_val_t stmts); +el_val_t codegen(el_val_t stmts, el_val_t source); +el_val_t js_escape(el_val_t s); +el_val_t js_str_lit(el_val_t s); +el_val_t js_emit_line(el_val_t line); +el_val_t js_emit_blank(void); +el_val_t js_binop(el_val_t op); +el_val_t js_is_int_name(el_val_t name); +el_val_t js_add_int_name(el_val_t name); +el_val_t js_build_int_names_for_params(el_val_t params); +el_val_t js_is_int_call(el_val_t call_expr); +el_val_t js_cg_expr(el_val_t expr); +el_val_t js_next_match_id(void); +el_val_t js_cg_match(el_val_t expr); +el_val_t js_list_contains(el_val_t lst, el_val_t s); +el_val_t js_cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared); +el_val_t js_strip_outer_parens(el_val_t s); +el_val_t js_cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t js_cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared); +el_val_t js_cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared); +el_val_t js_cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared); +el_val_t js_params_str(el_val_t params); +el_val_t js_transform_implicit_return(el_val_t body); +el_val_t js_cg_fn(el_val_t stmt); +el_val_t js_is_fndef(el_val_t stmt); +el_val_t js_is_top_level_decl(el_val_t stmt); +el_val_t codegen_js(el_val_t stmts, el_val_t source); +el_val_t compile(el_val_t source); +el_val_t compile_js(el_val_t source); +el_val_t compile_dispatch(el_val_t tgt, el_val_t source); +el_val_t detect_target(el_val_t argv); +el_val_t strip_flags(el_val_t argv); + +el_val_t is_digit(el_val_t ch) { + if (str_eq(ch, EL_STR("0"))) { + return 1; + } + if (str_eq(ch, EL_STR("1"))) { + return 1; + } + if (str_eq(ch, EL_STR("2"))) { + return 1; + } + if (str_eq(ch, EL_STR("3"))) { + return 1; + } + if (str_eq(ch, EL_STR("4"))) { + return 1; + } + if (str_eq(ch, EL_STR("5"))) { + return 1; + } + if (str_eq(ch, EL_STR("6"))) { + return 1; + } + if (str_eq(ch, EL_STR("7"))) { + return 1; + } + if (str_eq(ch, EL_STR("8"))) { + return 1; + } + if (str_eq(ch, EL_STR("9"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_alpha(el_val_t ch) { + if (str_eq(ch, EL_STR("a"))) { + return 1; + } + if (str_eq(ch, EL_STR("b"))) { + return 1; + } + if (str_eq(ch, EL_STR("c"))) { + return 1; + } + if (str_eq(ch, EL_STR("d"))) { + return 1; + } + if (str_eq(ch, EL_STR("e"))) { + return 1; + } + if (str_eq(ch, EL_STR("f"))) { + return 1; + } + if (str_eq(ch, EL_STR("g"))) { + return 1; + } + if (str_eq(ch, EL_STR("h"))) { + return 1; + } + if (str_eq(ch, EL_STR("i"))) { + return 1; + } + if (str_eq(ch, EL_STR("j"))) { + return 1; + } + if (str_eq(ch, EL_STR("k"))) { + return 1; + } + if (str_eq(ch, EL_STR("l"))) { + return 1; + } + if (str_eq(ch, EL_STR("m"))) { + return 1; + } + if (str_eq(ch, EL_STR("n"))) { + return 1; + } + if (str_eq(ch, EL_STR("o"))) { + return 1; + } + if (str_eq(ch, EL_STR("p"))) { + return 1; + } + if (str_eq(ch, EL_STR("q"))) { + return 1; + } + if (str_eq(ch, EL_STR("r"))) { + return 1; + } + if (str_eq(ch, EL_STR("s"))) { + return 1; + } + if (str_eq(ch, EL_STR("t"))) { + return 1; + } + if (str_eq(ch, EL_STR("u"))) { + return 1; + } + if (str_eq(ch, EL_STR("v"))) { + return 1; + } + if (str_eq(ch, EL_STR("w"))) { + return 1; + } + if (str_eq(ch, EL_STR("x"))) { + return 1; + } + if (str_eq(ch, EL_STR("y"))) { + return 1; + } + if (str_eq(ch, EL_STR("z"))) { + return 1; + } + if (str_eq(ch, EL_STR("A"))) { + return 1; + } + if (str_eq(ch, EL_STR("B"))) { + return 1; + } + if (str_eq(ch, EL_STR("C"))) { + return 1; + } + if (str_eq(ch, EL_STR("D"))) { + return 1; + } + if (str_eq(ch, EL_STR("E"))) { + return 1; + } + if (str_eq(ch, EL_STR("F"))) { + return 1; + } + if (str_eq(ch, EL_STR("G"))) { + return 1; + } + if (str_eq(ch, EL_STR("H"))) { + return 1; + } + if (str_eq(ch, EL_STR("I"))) { + return 1; + } + if (str_eq(ch, EL_STR("J"))) { + return 1; + } + if (str_eq(ch, EL_STR("K"))) { + return 1; + } + if (str_eq(ch, EL_STR("L"))) { + return 1; + } + if (str_eq(ch, EL_STR("M"))) { + return 1; + } + if (str_eq(ch, EL_STR("N"))) { + return 1; + } + if (str_eq(ch, EL_STR("O"))) { + return 1; + } + if (str_eq(ch, EL_STR("P"))) { + return 1; + } + if (str_eq(ch, EL_STR("Q"))) { + return 1; + } + if (str_eq(ch, EL_STR("R"))) { + return 1; + } + if (str_eq(ch, EL_STR("S"))) { + return 1; + } + if (str_eq(ch, EL_STR("T"))) { + return 1; + } + if (str_eq(ch, EL_STR("U"))) { + return 1; + } + if (str_eq(ch, EL_STR("V"))) { + return 1; + } + if (str_eq(ch, EL_STR("W"))) { + return 1; + } + if (str_eq(ch, EL_STR("X"))) { + return 1; + } + if (str_eq(ch, EL_STR("Y"))) { + return 1; + } + if (str_eq(ch, EL_STR("Z"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_alnum_or_underscore(el_val_t ch) { + if (is_digit(ch)) { + return 1; + } + if (is_alpha(ch)) { + return 1; + } + if (str_eq(ch, EL_STR("_"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_whitespace(el_val_t ch) { + if (str_eq(ch, EL_STR(" "))) { + return 1; + } + if (str_eq(ch, EL_STR("\t"))) { + return 1; + } + if (str_eq(ch, EL_STR("\n"))) { + return 1; + } + if (str_eq(ch, EL_STR("\r"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t make_tok(el_val_t kind, el_val_t value) { + return el_map_new(2, "kind", kind, "value", value); + return 0; +} + +el_val_t keyword_kind(el_val_t word) { + if (str_eq(word, EL_STR("let"))) { + return EL_STR("Let"); + } + if (str_eq(word, EL_STR("fn"))) { + return EL_STR("Fn"); + } + if (str_eq(word, EL_STR("type"))) { + return EL_STR("Type"); + } + if (str_eq(word, EL_STR("enum"))) { + return EL_STR("Enum"); + } + if (str_eq(word, EL_STR("match"))) { + return EL_STR("Match"); + } + if (str_eq(word, EL_STR("return"))) { + return EL_STR("Return"); + } + if (str_eq(word, EL_STR("if"))) { + return EL_STR("If"); + } + if (str_eq(word, EL_STR("else"))) { + return EL_STR("Else"); + } + if (str_eq(word, EL_STR("for"))) { + return EL_STR("For"); + } + if (str_eq(word, EL_STR("in"))) { + return EL_STR("In"); + } + if (str_eq(word, EL_STR("while"))) { + return EL_STR("While"); + } + if (str_eq(word, EL_STR("import"))) { + return EL_STR("Import"); + } + if (str_eq(word, EL_STR("from"))) { + return EL_STR("From"); + } + if (str_eq(word, EL_STR("as"))) { + return EL_STR("As"); + } + if (str_eq(word, EL_STR("with"))) { + return EL_STR("With"); + } + if (str_eq(word, EL_STR("sealed"))) { + return EL_STR("Sealed"); + } + if (str_eq(word, EL_STR("activate"))) { + return EL_STR("Activate"); + } + if (str_eq(word, EL_STR("where"))) { + return EL_STR("Where"); + } + if (str_eq(word, EL_STR("test"))) { + return EL_STR("Test"); + } + if (str_eq(word, EL_STR("seed"))) { + return EL_STR("Seed"); + } + if (str_eq(word, EL_STR("assert"))) { + return EL_STR("Assert"); + } + if (str_eq(word, EL_STR("protocol"))) { + return EL_STR("Protocol"); + } + if (str_eq(word, EL_STR("impl"))) { + return EL_STR("Impl"); + } + if (str_eq(word, EL_STR("retry"))) { + return EL_STR("Retry"); + } + if (str_eq(word, EL_STR("times"))) { + return EL_STR("Times"); + } + if (str_eq(word, EL_STR("fallback"))) { + return EL_STR("Fallback"); + } + if (str_eq(word, EL_STR("reason"))) { + return EL_STR("Reason"); + } + if (str_eq(word, EL_STR("parallel"))) { + return EL_STR("Parallel"); + } + if (str_eq(word, EL_STR("trace"))) { + return EL_STR("Trace"); + } + if (str_eq(word, EL_STR("requires"))) { + return EL_STR("Requires"); + } + if (str_eq(word, EL_STR("deploy"))) { + return EL_STR("Deploy"); + } + if (str_eq(word, EL_STR("to"))) { + return EL_STR("To"); + } + if (str_eq(word, EL_STR("via"))) { + return EL_STR("Via"); + } + if (str_eq(word, EL_STR("target"))) { + return EL_STR("Target"); + } + if (str_eq(word, EL_STR("true"))) { + return EL_STR("Bool"); + } + if (str_eq(word, EL_STR("false"))) { + return EL_STR("Bool"); + } + if (str_eq(word, EL_STR("cgi"))) { + return EL_STR("Cgi"); + } + if (str_eq(word, EL_STR("service"))) { + return EL_STR("Service"); + } + if (str_eq(word, EL_STR("manager"))) { + return EL_STR("Manager"); + } + if (str_eq(word, EL_STR("engine"))) { + return EL_STR("Engine"); + } + if (str_eq(word, EL_STR("accessor"))) { + return EL_STR("Accessor"); + } + if (str_eq(word, EL_STR("vessel"))) { + return EL_STR("Vessel"); + } + return EL_STR(""); + return 0; +} + +el_val_t scan_digits(el_val_t chars, el_val_t start, el_val_t total) { + el_val_t i = start; + el_val_t text = EL_STR(""); + el_val_t running = 1; + while (running) { + if (i >= total) { + running = 0; + } else { + el_val_t ch = native_list_get(chars, i); + if (is_digit(ch)) { + text = el_str_concat(text, ch); + i = (i + 1); + } else { + running = 0; + } + } + } + return el_map_new(2, "text", text, "pos", i); + return 0; +} + +el_val_t scan_ident(el_val_t chars, el_val_t start, el_val_t total) { + el_val_t i = start; + el_val_t text = EL_STR(""); + el_val_t running = 1; + while (running) { + if (i >= total) { + running = 0; + } else { + el_val_t ch = native_list_get(chars, i); + if (is_alnum_or_underscore(ch)) { + text = el_str_concat(text, ch); + i = (i + 1); + } else { + running = 0; + } + } + } + return el_map_new(2, "text", text, "pos", i); + return 0; +} + +el_val_t scan_string(el_val_t chars, el_val_t start, el_val_t total) { + el_val_t i = start; + el_val_t text = EL_STR(""); + el_val_t running = 1; + while (running) { + if (i >= total) { + running = 0; + } else { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("\\"))) { + el_val_t next_i = (i + 1); + if (next_i < total) { + el_val_t next_ch = native_list_get(chars, next_i); + if (str_eq(next_ch, EL_STR("\""))) { + text = el_str_concat(text, EL_STR("\"")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("n"))) { + text = el_str_concat(text, EL_STR("\n")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("t"))) { + text = el_str_concat(text, EL_STR("\t")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("r"))) { + text = el_str_concat(text, EL_STR("\r")); + i = (next_i + 1); + } else { + if (str_eq(next_ch, EL_STR("\\"))) { + text = el_str_concat(text, EL_STR("\\")); + i = (next_i + 1); + } else { + text = el_str_concat(text, next_ch); + i = (next_i + 1); + } + } + } + } + } + } else { + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("\""))) { + i = (i + 1); + running = 0; + } else { + text = el_str_concat(text, ch); + i = (i + 1); + } + } + } + } + return el_map_new(2, "text", text, "pos", i); + return 0; +} + +el_val_t lex(el_val_t source) { + el_val_t chars = native_string_chars(source); + el_val_t total = native_list_len(chars); + el_val_t tokens = native_list_empty(); + el_val_t i = 0; + while (i < total) { + el_val_t ch = native_list_get(chars, i); + if (is_whitespace(ch)) { + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("/"))) { + el_val_t next_i = (i + 1); + if (next_i < total) { + el_val_t next_ch = native_list_get(chars, next_i); + if (str_eq(next_ch, EL_STR("/"))) { + i = (i + 2); + el_val_t running2 = 1; + while (running2) { + if (i >= total) { + running2 = 0; + } else { + el_val_t lch = native_list_get(chars, i); + if (str_eq(lch, EL_STR("\n"))) { + running2 = 0; + } else { + i = (i + 1); + } + } + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Slash"), EL_STR("/"))); + i = (i + 1); + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Slash"), EL_STR("/"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("\""))) { + el_val_t result = scan_string(chars, (i + 1), total); + el_val_t str_text = el_get_field(result, EL_STR("text")); + el_val_t new_pos = el_get_field(result, EL_STR("pos")); + tokens = native_list_append(tokens, make_tok(EL_STR("Str"), str_text)); + i = new_pos; + } else { + if (is_digit(ch)) { + el_val_t result = scan_digits(chars, i, total); + el_val_t num_text = el_get_field(result, EL_STR("text")); + el_val_t new_pos = el_get_field(result, EL_STR("pos")); + if (new_pos < total) { + el_val_t dot_ch = native_list_get(chars, new_pos); + if (str_eq(dot_ch, EL_STR("."))) { + el_val_t after_dot = (new_pos + 1); + if (after_dot < total) { + el_val_t after_dot_ch = native_list_get(chars, after_dot); + if (is_digit(after_dot_ch)) { + el_val_t frac_result = scan_digits(chars, after_dot, total); + el_val_t frac_text = el_get_field(frac_result, EL_STR("text")); + el_val_t frac_pos = el_get_field(frac_result, EL_STR("pos")); + tokens = native_list_append(tokens, make_tok(EL_STR("Float"), el_str_concat(el_str_concat(num_text, EL_STR(".")), frac_text))); + i = frac_pos; + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Int"), num_text)); + i = new_pos; + } + } else { + if (is_alpha(ch) || str_eq(ch, EL_STR("_"))) { + el_val_t result = scan_ident(chars, i, total); + el_val_t word = el_get_field(result, EL_STR("text")); + el_val_t new_pos = el_get_field(result, EL_STR("pos")); + el_val_t kw = keyword_kind(word); + if (str_eq(kw, EL_STR(""))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Ident"), word)); + } else { + tokens = native_list_append(tokens, make_tok(kw, word)); + } + i = new_pos; + } else { + el_val_t peek_i = (i + 1); + el_val_t peek_ch = EL_STR(""); + if (peek_i < total) { + peek_ch = native_list_get(chars, peek_i); + } + if (str_eq(ch, EL_STR("="))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("EqEq"), EL_STR("=="))); + i = (i + 2); + } else { + if (str_eq(peek_ch, EL_STR(">"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("FatArrow"), EL_STR("=>"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Eq"), EL_STR("="))); + i = (i + 1); + } + } + } else { + if (str_eq(ch, EL_STR("!"))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("NotEq"), EL_STR("!="))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Not"), EL_STR("!"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("<"))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LtEq"), EL_STR("<="))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Lt"), EL_STR("<"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR(">"))) { + if (str_eq(peek_ch, EL_STR("="))) { + tokens = native_list_append(tokens, make_tok(EL_STR("GtEq"), EL_STR(">="))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Gt"), EL_STR(">"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("&"))) { + if (str_eq(peek_ch, EL_STR("&"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("And"), EL_STR("&&"))); + i = (i + 2); + } else { + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("|"))) { + if (str_eq(peek_ch, EL_STR("|"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Or"), EL_STR("||"))); + i = (i + 2); + } else { + if (str_eq(peek_ch, EL_STR(">"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("PipeOp"), EL_STR("|>"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Pipe"), EL_STR("|"))); + i = (i + 1); + } + } + } else { + if (str_eq(ch, EL_STR("-"))) { + if (str_eq(peek_ch, EL_STR(">"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Arrow"), EL_STR("->"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Minus"), EL_STR("-"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR(":"))) { + if (str_eq(peek_ch, EL_STR(":"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("ColonColon"), EL_STR("::"))); + i = (i + 2); + } else { + tokens = native_list_append(tokens, make_tok(EL_STR("Colon"), EL_STR(":"))); + i = (i + 1); + } + } else { + if (str_eq(ch, EL_STR("+"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Plus"), EL_STR("+"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("*"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Star"), EL_STR("*"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("%"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Percent"), EL_STR("%"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("("))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LParen"), EL_STR("("))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR(")"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("RParen"), EL_STR(")"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("{"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LBrace"), EL_STR("{"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("}"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("RBrace"), EL_STR("}"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("["))) { + tokens = native_list_append(tokens, make_tok(EL_STR("LBracket"), EL_STR("["))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("]"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("RBracket"), EL_STR("]"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR(","))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Comma"), EL_STR(","))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("."))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Dot"), EL_STR("."))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR(";"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("Semicolon"), EL_STR(";"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("@"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("At"), EL_STR("@"))); + i = (i + 1); + } else { + if (str_eq(ch, EL_STR("?"))) { + tokens = native_list_append(tokens, make_tok(EL_STR("QuestionMark"), EL_STR("?"))); + i = (i + 1); + } else { + i = (i + 1); + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + tokens = native_list_append(tokens, make_tok(EL_STR("Eof"), EL_STR(""))); + return tokens; + return 0; +} + +el_val_t tok_at(el_val_t tokens, el_val_t pos) { + return native_list_get(tokens, pos); + return 0; +} + +el_val_t tok_kind(el_val_t tokens, el_val_t pos) { + el_val_t t = native_list_get(tokens, pos); + return el_get_field(t, EL_STR("kind")); + return 0; +} + +el_val_t tok_value(el_val_t tokens, el_val_t pos) { + el_val_t t = native_list_get(tokens, pos); + return el_get_field(t, EL_STR("value")); + return 0; +} + +el_val_t expect(el_val_t tokens, el_val_t pos, el_val_t kind) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, kind)) { + return (pos + 1); + } + return (pos + 1); + return 0; +} + +el_val_t make_result(el_val_t node, el_val_t pos) { + return el_map_new(2, "node", node, "pos", pos); + return 0; +} + +el_val_t skip_type(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("LBracket"))) { + el_val_t p = (pos + 1); + p = skip_type(tokens, p); + p = expect(tokens, p, EL_STR("RBracket")); + return p; + } + if (str_eq(k, EL_STR("Ident"))) { + el_val_t p = (pos + 1); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Lt"))) { + p = (p + 1); + el_val_t depth = 1; + el_val_t running = 1; + while (running) { + el_val_t kk = tok_kind(tokens, p); + if (str_eq(kk, EL_STR("Eof"))) { + running = 0; + } else { + if (str_eq(kk, EL_STR("Lt"))) { + depth = (depth + 1); + p = (p + 1); + } else { + if (str_eq(kk, EL_STR("Gt"))) { + depth = (depth - 1); + p = (p + 1); + if (depth <= 0) { + running = 0; + } + } else { + p = (p + 1); + } + } + } + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("QuestionMark"))) { + p = (p + 1); + } + return p; + } + if (str_eq(k2, EL_STR("QuestionMark"))) { + return (p + 1); + } + return p; + } + return (pos + 1); + return 0; +} + +el_val_t parse_params(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("LParen")); + el_val_t params = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("RParen"))) { + running = 0; + } else { + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t pname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + el_val_t ptype = EL_STR(""); + el_val_t kt = tok_kind(tokens, p); + if (str_eq(kt, EL_STR("Ident"))) { + ptype = tok_value(tokens, p); + } + p = skip_type(tokens, p); + el_val_t param = el_map_new(2, "name", pname, "type", ptype); + params = native_list_append(params, param); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RParen")); + return el_map_new(2, "params", params, "pos", p); + return 0; +} + +el_val_t parse_primary(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + el_val_t v = tok_value(tokens, pos); + if (str_eq(k, EL_STR("Int"))) { + return make_result(el_map_new(2, "expr", EL_STR("Int"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Float"))) { + return make_result(el_map_new(2, "expr", EL_STR("Float"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Str"))) { + return make_result(el_map_new(2, "expr", EL_STR("Str"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Bool"))) { + return make_result(el_map_new(2, "expr", EL_STR("Bool"), "value", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Ident"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("LParen"))) { + el_val_t r = parse_expr(tokens, (pos + 1)); + el_val_t node = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + p = expect(tokens, p, EL_STR("RParen")); + return make_result(node, p); + } + if (str_eq(k, EL_STR("LBracket"))) { + el_val_t p = (pos + 1); + el_val_t elems = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBracket"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r = parse_expr(tokens, p); + el_val_t elem = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + elems = native_list_append(elems, elem); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBracket")); + return make_result(el_map_new(2, "expr", EL_STR("Array"), "elems", elems), p); + } + if (str_eq(k, EL_STR("LBrace"))) { + el_val_t no_block = state_get(EL_STR("__no_block_expr")); + if (str_eq(no_block, EL_STR("1"))) { + return make_result(el_map_new(1, "expr", EL_STR("Nil")), pos); + } + el_val_t p = (pos + 1); + el_val_t pairs = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t key = tok_value(tokens, p); + el_val_t new_p = (p + 1); + new_p = expect(tokens, new_p, EL_STR("Colon")); + el_val_t r = parse_expr(tokens, new_p); + el_val_t val_node = el_get_field(r, EL_STR("node")); + new_p = el_get_field(r, EL_STR("pos")); + el_val_t pair = el_map_new(2, "key", key, "value", val_node); + pairs = native_list_append(pairs, pair); + el_val_t k3 = tok_kind(tokens, new_p); + if (str_eq(k3, EL_STR("Comma"))) { + new_p = (new_p + 1); + } + if (new_p <= p) { + p = (p + 1); + } else { + p = new_p; + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(2, "expr", EL_STR("Map"), "pairs", pairs), p); + } + if (str_eq(k, EL_STR("If"))) { + el_val_t r = parse_if(tokens, pos); + return r; + } + if (str_eq(k, EL_STR("Match"))) { + el_val_t r = parse_match(tokens, pos); + return r; + } + if (str_eq(k, EL_STR("For"))) { + el_val_t r = parse_for_expr(tokens, pos); + return r; + } + if (str_eq(k, EL_STR("Not"))) { + el_val_t r = parse_primary(tokens, (pos + 1)); + el_val_t inner = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "expr", EL_STR("Not"), "inner", inner), p); + } + if (str_eq(k, EL_STR("Minus"))) { + el_val_t r = parse_primary(tokens, (pos + 1)); + el_val_t inner = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "expr", EL_STR("Neg"), "inner", inner), p); + } + if (str_eq(k, EL_STR("Target"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("To"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Via"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Deploy"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Reason"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Times"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Fallback"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Retry"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Parallel"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Trace"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Requires"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Where"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("As"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("With"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Manager"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Engine"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Accessor"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Vessel"))) { + return make_result(el_map_new(2, "expr", EL_STR("Ident"), "name", v), (pos + 1)); + } + return make_result(el_map_new(1, "expr", EL_STR("Nil")), (pos + 1)); + return 0; +} + +el_val_t parse_if(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("If")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t cond = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t then_stmts = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + el_val_t has_else = 0; + el_val_t else_stmts = native_list_empty(); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Else"))) { + p = (p + 1); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("If"))) { + el_val_t r3 = parse_if(tokens, p); + el_val_t nested = el_get_field(r3, EL_STR("node")); + p = el_get_field(r3, EL_STR("pos")); + else_stmts = native_list_append(else_stmts, el_map_new(2, "stmt", EL_STR("Expr"), "value", nested)); + has_else = 1; + } else { + el_val_t r3 = parse_block(tokens, p); + else_stmts = el_get_field(r3, EL_STR("stmts")); + p = el_get_field(r3, EL_STR("pos")); + has_else = 1; + } + } + return make_result(el_map_new(5, "expr", EL_STR("If"), "cond", cond, "then", then_stmts, "else", else_stmts, "has_else", has_else), p); + return 0; +} + +el_val_t parse_match(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("Match")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t subject = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t arms = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r2 = parse_pattern(tokens, p); + el_val_t pattern = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + p = expect(tokens, p, EL_STR("FatArrow")); + el_val_t r3 = parse_expr(tokens, p); + el_val_t body = el_get_field(r3, EL_STR("node")); + p = el_get_field(r3, EL_STR("pos")); + el_val_t arm = el_map_new(2, "pattern", pattern, "body", body); + arms = native_list_append(arms, arm); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(3, "expr", EL_STR("Match"), "subject", subject, "arms", arms), p); + return 0; +} + +el_val_t parse_pattern(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("Ident"))) { + el_val_t v = tok_value(tokens, pos); + if (str_eq(v, EL_STR("_"))) { + return make_result(el_map_new(1, "pattern", EL_STR("Wildcard")), (pos + 1)); + } + return make_result(el_map_new(2, "pattern", EL_STR("Binding"), "name", v), (pos + 1)); + } + if (str_eq(k, EL_STR("Int"))) { + return make_result(el_map_new(2, "pattern", EL_STR("LitInt"), "value", tok_value(tokens, pos)), (pos + 1)); + } + if (str_eq(k, EL_STR("Str"))) { + return make_result(el_map_new(2, "pattern", EL_STR("LitStr"), "value", tok_value(tokens, pos)), (pos + 1)); + } + if (str_eq(k, EL_STR("Bool"))) { + return make_result(el_map_new(2, "pattern", EL_STR("LitBool"), "value", tok_value(tokens, pos)), (pos + 1)); + } + return make_result(el_map_new(1, "pattern", EL_STR("Wildcard")), (pos + 1)); + return 0; +} + +el_val_t parse_for_expr(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("For")); + el_val_t item_name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("In")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t list_expr = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(4, "expr", EL_STR("For"), "item", item_name, "list", list_expr, "body", body), p); + return 0; +} + +el_val_t parse_block(el_val_t tokens, el_val_t pos) { + el_val_t p = expect(tokens, pos, EL_STR("LBrace")); + el_val_t stmts = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r = parse_stmt(tokens, p); + el_val_t stmt = el_get_field(r, EL_STR("node")); + el_val_t new_p = el_get_field(r, EL_STR("pos")); + stmts = native_list_append(stmts, stmt); + if (new_p <= p) { + p = (p + 1); + } else { + p = new_p; + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return el_map_new(2, "stmts", stmts, "pos", p); + return 0; +} + +el_val_t parse_postfix(el_val_t tokens, el_val_t pos) { + el_val_t r = parse_primary(tokens, pos); + el_val_t node = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + if (str_eq(k, EL_STR("LParen"))) { + p = (p + 1); + el_val_t args = native_list_empty(); + el_val_t run2 = 1; + while (run2) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RParen"))) { + run2 = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + run2 = 0; + } else { + el_val_t r2 = parse_expr(tokens, p); + el_val_t arg = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + args = native_list_append(args, arg); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RParen")); + node = el_map_new(3, "expr", EL_STR("Call"), "func", node, "args", args); + } else { + if (str_eq(k, EL_STR("Dot"))) { + el_val_t field = tok_value(tokens, (p + 1)); + p = (p + 2); + node = el_map_new(3, "expr", EL_STR("Field"), "object", node, "field", field); + } else { + if (str_eq(k, EL_STR("LBracket"))) { + el_val_t r2 = parse_expr(tokens, (p + 1)); + el_val_t idx = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + p = expect(tokens, p, EL_STR("RBracket")); + node = el_map_new(3, "expr", EL_STR("Index"), "object", node, "index", idx); + } else { + if (str_eq(k, EL_STR("QuestionMark"))) { + p = (p + 1); + node = el_map_new(2, "expr", EL_STR("Try"), "inner", node); + } else { + running = 0; + } + } + } + } + } + return make_result(node, p); + return 0; +} + +el_val_t op_precedence(el_val_t kind) { + if (str_eq(kind, EL_STR("Or"))) { + return 1; + } + if (str_eq(kind, EL_STR("And"))) { + return 2; + } + if (str_eq(kind, EL_STR("EqEq"))) { + return 3; + } + if (str_eq(kind, EL_STR("NotEq"))) { + return 3; + } + if (str_eq(kind, EL_STR("Lt"))) { + return 4; + } + if (str_eq(kind, EL_STR("Gt"))) { + return 4; + } + if (str_eq(kind, EL_STR("LtEq"))) { + return 4; + } + if (str_eq(kind, EL_STR("GtEq"))) { + return 4; + } + if (str_eq(kind, EL_STR("Plus"))) { + return 5; + } + if (str_eq(kind, EL_STR("Minus"))) { + return 5; + } + if (str_eq(kind, EL_STR("Star"))) { + return 6; + } + if (str_eq(kind, EL_STR("Slash"))) { + return 6; + } + return 0; + return 0; +} + +el_val_t is_binop(el_val_t kind) { + if (str_eq(kind, EL_STR("Or"))) { + return 1; + } + if (str_eq(kind, EL_STR("And"))) { + return 1; + } + if (str_eq(kind, EL_STR("EqEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("NotEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("Lt"))) { + return 1; + } + if (str_eq(kind, EL_STR("Gt"))) { + return 1; + } + if (str_eq(kind, EL_STR("LtEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("GtEq"))) { + return 1; + } + if (str_eq(kind, EL_STR("Plus"))) { + return 1; + } + if (str_eq(kind, EL_STR("Minus"))) { + return 1; + } + if (str_eq(kind, EL_STR("Star"))) { + return 1; + } + if (str_eq(kind, EL_STR("Slash"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t parse_binop(el_val_t tokens, el_val_t pos, el_val_t min_prec) { + el_val_t r = parse_postfix(tokens, pos); + el_val_t left = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + el_val_t running = 1; + while (running) { + el_val_t k = tok_kind(tokens, p); + el_val_t prec = op_precedence(k); + if (is_binop(k)) { + if (prec >= min_prec) { + el_val_t op = k; + el_val_t r2 = parse_binop(tokens, (p + 1), (prec + 1)); + el_val_t right = el_get_field(r2, EL_STR("node")); + p = el_get_field(r2, EL_STR("pos")); + left = el_map_new(4, "expr", EL_STR("BinOp"), "op", op, "left", left, "right", right); + } else { + running = 0; + } + } else { + running = 0; + } + } + return make_result(left, p); + return 0; +} + +el_val_t parse_expr(el_val_t tokens, el_val_t pos) { + return parse_binop(tokens, pos, 1); + return 0; +} + +el_val_t parse_stmt(el_val_t tokens, el_val_t pos) { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("Let"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + el_val_t ltype = EL_STR(""); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Colon"))) { + p = (p + 1); + el_val_t kt = tok_kind(tokens, p); + if (str_eq(kt, EL_STR("Ident"))) { + ltype = tok_value(tokens, p); + } + p = skip_type(tokens, p); + } + p = expect(tokens, p, EL_STR("Eq")); + el_val_t r = parse_expr(tokens, p); + el_val_t val = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(4, "stmt", EL_STR("Let"), "name", name, "value", val, "type", ltype), p); + } + if (str_eq(k, EL_STR("Return"))) { + el_val_t p = (pos + 1); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + return make_result(el_map_new(2, "stmt", EL_STR("Return"), "value", el_map_new(1, "expr", EL_STR("Nil"))), p); + } + if (str_eq(k2, EL_STR("Eof"))) { + return make_result(el_map_new(2, "stmt", EL_STR("Return"), "value", el_map_new(1, "expr", EL_STR("Nil"))), p); + } + el_val_t r = parse_expr(tokens, p); + el_val_t val = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "stmt", EL_STR("Return"), "value", val), p); + } + if (str_eq(k, EL_STR("Fn"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + el_val_t r = parse_params(tokens, p); + el_val_t params = el_get_field(r, EL_STR("params")); + p = el_get_field(r, EL_STR("pos")); + el_val_t ret_type = EL_STR(""); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Arrow"))) { + p = (p + 1); + el_val_t kt = tok_kind(tokens, p); + if (str_eq(kt, EL_STR("Ident"))) { + ret_type = tok_value(tokens, p); + } + p = skip_type(tokens, p); + } + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(5, "stmt", EL_STR("FnDef"), "name", name, "params", params, "body", body, "ret_type", ret_type), p); + } + if (str_eq(k, EL_STR("Type"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t fields = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t fname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + p = skip_type(tokens, p); + fields = native_list_append(fields, el_map_new(1, "name", fname)); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(3, "stmt", EL_STR("TypeDef"), "name", name, "fields", fields), p); + } + if (str_eq(k, EL_STR("Enum"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t variants = native_list_empty(); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t vname = tok_value(tokens, p); + p = (p + 1); + variants = native_list_append(variants, el_map_new(1, "name", vname)); + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(3, "stmt", EL_STR("EnumDef"), "name", name, "variants", variants), p); + } + if (str_eq(k, EL_STR("Import"))) { + el_val_t p = (pos + 1); + el_val_t path = tok_value(tokens, p); + p = (p + 1); + return make_result(el_map_new(2, "stmt", EL_STR("Import"), "path", path), p); + } + if (str_eq(k, EL_STR("From"))) { + el_val_t p = (pos + 1); + el_val_t module_name = tok_value(tokens, p); + p = (p + 1); + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("Import"))) { + p = (p + 1); + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("LBrace"))) { + p = (p + 1); + el_val_t running = 1; + while (running) { + el_val_t k4 = tok_kind(tokens, p); + if (str_eq(k4, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k4, EL_STR("Eof"))) { + running = 0; + } else { + p = (p + 1); + el_val_t k5 = tok_kind(tokens, p); + if (str_eq(k5, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + } + return make_result(el_map_new(2, "stmt", EL_STR("Import"), "path", module_name), p); + } + if (str_eq(k, EL_STR("While"))) { + el_val_t p = (pos + 1); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t cond = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(3, "stmt", EL_STR("While"), "cond", cond, "body", body), p); + } + if (str_eq(k, EL_STR("For"))) { + el_val_t p = (pos + 1); + el_val_t item_name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("In")); + el_val_t prev_no_block = state_get(EL_STR("__no_block_expr")); + state_set(EL_STR("__no_block_expr"), EL_STR("1")); + el_val_t r = parse_expr(tokens, p); + state_set(EL_STR("__no_block_expr"), prev_no_block); + el_val_t list_expr = el_get_field(r, EL_STR("node")); + p = el_get_field(r, EL_STR("pos")); + el_val_t r2 = parse_block(tokens, p); + el_val_t body = el_get_field(r2, EL_STR("stmts")); + p = el_get_field(r2, EL_STR("pos")); + return make_result(el_map_new(4, "stmt", EL_STR("For"), "item", item_name, "list", list_expr, "body", body), p); + } + if (str_eq(k, EL_STR("At"))) { + el_val_t p = (pos + 1); + el_val_t dec_name = tok_value(tokens, p); + p = (p + 1); + el_val_t r = parse_stmt(tokens, p); + el_val_t inner = el_get_field(r, EL_STR("node")); + el_val_t p2 = el_get_field(r, EL_STR("pos")); + el_val_t inner_kind = el_get_field(inner, EL_STR("stmt")); + if (str_eq(inner_kind, EL_STR("FnDef"))) { + el_val_t with_dec = el_map_new(6, "stmt", EL_STR("FnDef"), "name", el_get_field(inner, EL_STR("name")), "params", el_get_field(inner, EL_STR("params")), "body", el_get_field(inner, EL_STR("body")), "ret_type", el_get_field(inner, EL_STR("ret_type")), "decorator", dec_name); + return make_result(with_dec, p2); + } + return r; + } + if (str_eq(k, EL_STR("Cgi"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t dharma_id = EL_STR(""); + el_val_t principal = EL_STR(""); + el_val_t network = EL_STR(""); + el_val_t engram = EL_STR(""); + el_val_t has_dharma_id = 0; + el_val_t has_principal = 0; + el_val_t has_network = 0; + el_val_t has_engram = 0; + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t fname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + el_val_t fval = tok_value(tokens, p); + p = (p + 1); + if (str_eq(fname, EL_STR("dharma_id"))) { + dharma_id = fval; + has_dharma_id = 1; + } + if (str_eq(fname, EL_STR("principal"))) { + principal = fval; + has_principal = 1; + } + if (str_eq(fname, EL_STR("network"))) { + network = fval; + has_network = 1; + } + if (str_eq(fname, EL_STR("engram"))) { + engram = fval; + has_engram = 1; + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(10, "stmt", EL_STR("CgiBlock"), "name", name, "dharma_id", dharma_id, "principal", principal, "network", network, "engram", engram, "has_dharma_id", has_dharma_id, "has_principal", has_principal, "has_network", has_network, "has_engram", has_engram), p); + } + if (str_eq(k, EL_STR("Service"))) { + el_val_t p = (pos + 1); + el_val_t name = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("LBrace")); + el_val_t sponsor = EL_STR(""); + el_val_t domain = EL_STR(""); + el_val_t running = 1; + while (running) { + el_val_t k2 = tok_kind(tokens, p); + if (str_eq(k2, EL_STR("RBrace"))) { + running = 0; + } else { + if (str_eq(k2, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t fname = tok_value(tokens, p); + p = (p + 1); + p = expect(tokens, p, EL_STR("Colon")); + el_val_t fval = tok_value(tokens, p); + p = (p + 1); + if (str_eq(fname, EL_STR("sponsor"))) { + sponsor = fval; + } + if (str_eq(fname, EL_STR("domain"))) { + domain = fval; + } + el_val_t k3 = tok_kind(tokens, p); + if (str_eq(k3, EL_STR("Comma"))) { + p = (p + 1); + } + } + } + } + p = expect(tokens, p, EL_STR("RBrace")); + return make_result(el_map_new(4, "stmt", EL_STR("ServiceBlock"), "name", name, "sponsor", sponsor, "domain", domain), p); + } + el_val_t r = parse_expr(tokens, pos); + el_val_t val = el_get_field(r, EL_STR("node")); + el_val_t p = el_get_field(r, EL_STR("pos")); + return make_result(el_map_new(2, "stmt", EL_STR("Expr"), "value", val), p); + return 0; +} + +el_val_t parse(el_val_t tokens) { + el_val_t total = native_list_len(tokens); + el_val_t stmts = native_list_empty(); + el_val_t pos = 0; + el_val_t running = 1; + while (running) { + if (pos >= total) { + running = 0; + } else { + el_val_t k = tok_kind(tokens, pos); + if (str_eq(k, EL_STR("Eof"))) { + running = 0; + } else { + el_val_t r = parse_stmt(tokens, pos); + el_val_t stmt = el_get_field(r, EL_STR("node")); + el_val_t new_pos = el_get_field(r, EL_STR("pos")); + stmts = native_list_append(stmts, stmt); + if (new_pos <= pos) { + pos = (pos + 1); + } else { + pos = new_pos; + } + } + } + } + return stmts; + return 0; +} + +el_val_t c_escape(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t total = native_list_len(chars); + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < total) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("\""))) { + out = el_str_concat(out, EL_STR("\\\"")); + } else { + if (str_eq(ch, EL_STR("\\"))) { + out = el_str_concat(out, EL_STR("\\\\")); + } else { + if (str_eq(ch, EL_STR("\n"))) { + out = el_str_concat(out, EL_STR("\\n")); + } else { + if (str_eq(ch, EL_STR("\r"))) { + out = el_str_concat(out, EL_STR("\\r")); + } else { + if (str_eq(ch, EL_STR("\t"))) { + out = el_str_concat(out, EL_STR("\\t")); + } else { + out = el_str_concat(out, ch); + } + } + } + } + } + i = (i + 1); + } + return out; + return 0; +} + +el_val_t c_str_lit(el_val_t s) { + return el_str_concat(el_str_concat(EL_STR("\""), c_escape(s)), EL_STR("\"")); + return 0; +} + +el_val_t el_type_to_c(el_val_t type_str) { + if (str_eq(type_str, EL_STR("String"))) { + return EL_STR("const char*"); + } + if (str_eq(type_str, EL_STR("Int"))) { + return EL_STR("int64_t"); + } + if (str_eq(type_str, EL_STR("Bool"))) { + return EL_STR("int"); + } + if (str_eq(type_str, EL_STR("Float"))) { + return EL_STR("double"); + } + if (str_eq(type_str, EL_STR("Void"))) { + return EL_STR("void"); + } + if (str_eq(type_str, EL_STR("void"))) { + return EL_STR("void"); + } + return EL_STR("void*"); + return 0; +} + +el_val_t emit_line(el_val_t line) { + println(line); + return 0; +} + +el_val_t emit_blank(void) { + println(EL_STR("")); + return 0; +} + +el_val_t binop_to_c(el_val_t op) { + if (str_eq(op, EL_STR("Plus"))) { + return EL_STR("+"); + } + if (str_eq(op, EL_STR("Minus"))) { + return EL_STR("-"); + } + if (str_eq(op, EL_STR("Star"))) { + return EL_STR("*"); + } + if (str_eq(op, EL_STR("Slash"))) { + return EL_STR("/"); + } + if (str_eq(op, EL_STR("EqEq"))) { + return EL_STR("=="); + } + if (str_eq(op, EL_STR("NotEq"))) { + return EL_STR("!="); + } + if (str_eq(op, EL_STR("Lt"))) { + return EL_STR("<"); + } + if (str_eq(op, EL_STR("Gt"))) { + return EL_STR(">"); + } + if (str_eq(op, EL_STR("LtEq"))) { + return EL_STR("<="); + } + if (str_eq(op, EL_STR("GtEq"))) { + return EL_STR(">="); + } + if (str_eq(op, EL_STR("And"))) { + return EL_STR("&&"); + } + if (str_eq(op, EL_STR("Or"))) { + return EL_STR("||"); + } + return op; + return 0; +} + +el_val_t cg_expr(el_val_t expr) { + el_val_t kind = el_get_field(expr, EL_STR("expr")); + if (str_eq(kind, EL_STR("Int"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return v; + } + if (str_eq(kind, EL_STR("Float"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return el_str_concat(el_str_concat(EL_STR("el_from_float("), v), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Str"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return el_str_concat(el_str_concat(EL_STR("EL_STR("), c_str_lit(v)), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Bool"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + if (str_eq(v, EL_STR("true"))) { + return EL_STR("1"); + } + return EL_STR("0"); + } + if (str_eq(kind, EL_STR("Nil"))) { + return EL_STR("EL_NULL"); + } + if (str_eq(kind, EL_STR("Ident"))) { + el_val_t name = el_get_field(expr, EL_STR("name")); + return name; + } + if (str_eq(kind, EL_STR("Not"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = cg_expr(inner); + return el_str_concat(EL_STR("!"), inner_c); + } + if (str_eq(kind, EL_STR("Neg"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = cg_expr(inner); + return el_str_concat(el_str_concat(EL_STR("(-"), inner_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("BinOp"))) { + el_val_t op = el_get_field(expr, EL_STR("op")); + el_val_t left = el_get_field(expr, EL_STR("left")); + el_val_t right = el_get_field(expr, EL_STR("right")); + el_val_t left_c = cg_expr(left); + el_val_t right_c = cg_expr(right); + el_val_t left_kind = el_get_field(left, EL_STR("expr")); + el_val_t right_kind = el_get_field(right, EL_STR("expr")); + if (str_eq(op, EL_STR("Plus"))) { + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (is_int_expr(left)) { + if (is_int_expr(right)) { + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + } + if (str_eq(left_kind, EL_STR("Int"))) { + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("BinOp"))) { + el_val_t left_op = el_get_field(left, EL_STR("op")); + if (str_eq(left_op, EL_STR("Plus"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(right_kind, EL_STR("BinOp"))) { + el_val_t right_op = el_get_field(right, EL_STR("op")); + if (str_eq(right_op, EL_STR("Plus"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(op, EL_STR("EqEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (is_int_name(lname)) { + if (is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" == ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(op, EL_STR("NotEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (is_int_name(lname)) { + if (is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" != ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + el_val_t op_c = binop_to_c(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Call"))) { + el_val_t func = el_get_field(expr, EL_STR("func")); + el_val_t args = el_get_field(expr, EL_STR("args")); + el_val_t arity = native_list_len(args); + el_val_t func_kind = el_get_field(func, EL_STR("expr")); + el_val_t args_c = EL_STR(""); + el_val_t i = 0; + while (i < arity) { + el_val_t arg = native_list_get(args, i); + el_val_t arg_c = cg_expr(arg); + if (i > 0) { + args_c = el_str_concat(args_c, EL_STR(", ")); + } + args_c = el_str_concat(args_c, arg_c); + i = (i + 1); + } + if (str_eq(func_kind, EL_STR("Ident"))) { + el_val_t fn_name = el_get_field(func, EL_STR("name")); + cap_check_call(fn_name); + arity_check_call(fn_name, arity); + return el_str_concat(el_str_concat(el_str_concat(fn_name, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(func_kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(func, EL_STR("object")); + el_val_t field = el_get_field(func, EL_STR("field")); + el_val_t obj_c = cg_expr(obj); + if (arity > 0) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(", ")), args_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(")")); + } + el_val_t fn_c = cg_expr(func); + return el_str_concat(el_str_concat(el_str_concat(fn_c, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t field = el_get_field(expr, EL_STR("field")); + el_val_t obj_c = cg_expr(obj); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", EL_STR(")), c_str_lit(field)), EL_STR("))")); + } + if (str_eq(kind, EL_STR("Index"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t idx = el_get_field(expr, EL_STR("index")); + el_val_t obj_c = cg_expr(obj); + el_val_t idx_c = cg_expr(idx); + el_val_t idx_kind = el_get_field(idx, EL_STR("expr")); + if (str_eq(idx_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_list_get("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Array"))) { + el_val_t elems = el_get_field(expr, EL_STR("elems")); + el_val_t n = native_list_len(elems); + if (n == 0) { + return EL_STR("el_list_empty()"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t elem = native_list_get(elems, i); + el_val_t elem_c = cg_expr(elem); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(items, elem_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_list_new("), native_int_to_str(n)), EL_STR(", ")), items), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Map"))) { + el_val_t pairs = el_get_field(expr, EL_STR("pairs")); + el_val_t n = native_list_len(pairs); + if (n == 0) { + return EL_STR("el_map_new(0)"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t pair = native_list_get(pairs, i); + el_val_t key = el_get_field(pair, EL_STR("key")); + el_val_t val = el_get_field(pair, EL_STR("value")); + el_val_t val_c = cg_expr(val); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(el_str_concat(el_str_concat(items, c_str_lit(key)), EL_STR(", ")), val_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_map_new("), native_int_to_str(n)), EL_STR(", ")), items), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Try"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + return cg_expr(inner); + } + if (str_eq(kind, EL_STR("If"))) { + return cg_if_expr(expr); + } + if (str_eq(kind, EL_STR("Match"))) { + return cg_match(expr); + } + return EL_STR("EL_NULL"); + return 0; +} + +el_val_t next_match_id(void) { + el_val_t csv = state_get(EL_STR("__match_counter")); + el_val_t n = 0; + if (!str_eq(csv, EL_STR(""))) { + n = str_to_int(csv); + } + n = (n + 1); + state_set(EL_STR("__match_counter"), native_int_to_str(n)); + return native_int_to_str(n); + return 0; +} + +el_val_t cg_match(el_val_t expr) { + el_val_t subject = el_get_field(expr, EL_STR("subject")); + el_val_t arms = el_get_field(expr, EL_STR("arms")); + el_val_t subj_c = cg_expr(subject); + el_val_t id = next_match_id(); + el_val_t subj_var = el_str_concat(EL_STR("_match_subj_"), id); + el_val_t result_var = el_str_concat(EL_STR("_match_result_"), id); + el_val_t done_label = el_str_concat(EL_STR("_match_done_"), id); + el_val_t out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("({ el_val_t "), subj_var), EL_STR(" = ")), subj_c), EL_STR("; el_val_t ")), result_var), EL_STR(" = 0; ")); + el_val_t n = native_list_len(arms); + el_val_t i = 0; + while (i < n) { + el_val_t arm = native_list_get(arms, i); + el_val_t pat = el_get_field(arm, EL_STR("pattern")); + el_val_t body = el_get_field(arm, EL_STR("body")); + el_val_t pkind = el_get_field(pat, EL_STR("pattern")); + el_val_t body_c = cg_expr(body); + if (str_eq(pkind, EL_STR("Wildcard"))) { + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("Binding"))) { + el_val_t bname = el_get_field(pat, EL_STR("name")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ el_val_t ")), bname), EL_STR(" = ")), subj_var), EL_STR("; ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("LitInt"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" == ")), v), EL_STR(") { ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("LitStr"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (str_eq(")), subj_var), EL_STR(", EL_STR(")), c_str_lit(v)), EL_STR("))) { ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + if (str_eq(pkind, EL_STR("LitBool"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + el_val_t bv = EL_STR("0"); + if (str_eq(v, EL_STR("true"))) { + bv = EL_STR("1"); + } + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" == ")), bv), EL_STR(") { ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } else { + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ ")), result_var), EL_STR(" = (")), body_c), EL_STR("); goto ")), done_label), EL_STR("; } ")); + } + } + } + } + } + i = (i + 1); + } + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, done_label), EL_STR(":; ")), result_var), EL_STR("; })")); + return out; + return 0; +} + +el_val_t next_if_id(void) { + el_val_t csv = state_get(EL_STR("__if_expr_counter")); + el_val_t n = 0; + if (!str_eq(csv, EL_STR(""))) { + n = str_to_int(csv); + } + n = (n + 1); + state_set(EL_STR("__if_expr_counter"), native_int_to_str(n)); + return native_int_to_str(n); + return 0; +} + +el_val_t cg_if_expr_arm(el_val_t stmts, el_val_t result_var) { + el_val_t n = native_list_len(stmts); + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t s = native_list_get(stmts, i); + el_val_t sk = el_get_field(s, EL_STR("stmt")); + el_val_t is_last = 0; + if (str_eq(i, (n - 1))) { + is_last = 1; + } + if (str_eq(sk, EL_STR("Let"))) { + el_val_t name = el_get_field(s, EL_STR("name")); + el_val_t val = el_get_field(s, EL_STR("value")); + el_val_t val_c = cg_expr(val); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("el_val_t ")), name), EL_STR(" = ")), val_c), EL_STR("; ")); + } else { + if (str_eq(sk, EL_STR("Return"))) { + el_val_t val = el_get_field(s, EL_STR("value")); + el_val_t val_c = cg_expr(val); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, result_var), EL_STR(" = (")), val_c), EL_STR("); ")); + } else { + if (str_eq(sk, EL_STR("Expr"))) { + el_val_t val = el_get_field(s, EL_STR("value")); + el_val_t val_c = cg_expr(val); + if (is_last) { + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, result_var), EL_STR(" = (")), val_c), EL_STR("); ")); + } else { + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("(void)(")), val_c), EL_STR("); ")); + } + } else { + } + } + } + i = (i + 1); + } + return out; + return 0; +} + +el_val_t cg_if_expr(el_val_t expr) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t then_stmts = el_get_field(expr, EL_STR("then")); + el_val_t else_stmts = el_get_field(expr, EL_STR("else")); + el_val_t has_else = el_get_field(expr, EL_STR("has_else")); + el_val_t cond_c = cg_expr(cond); + el_val_t id = next_if_id(); + el_val_t result_var = el_str_concat(EL_STR("_if_result_"), id); + el_val_t then_c = cg_if_expr_arm(then_stmts, result_var); + el_val_t else_c = EL_STR(""); + if (has_else) { + else_c = cg_if_expr_arm(else_stmts, result_var); + } + el_val_t out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("({ el_val_t "), result_var), EL_STR(" = 0; if (")), cond_c), EL_STR(") { ")), then_c), EL_STR("} else { ")), else_c), EL_STR("} ")), result_var), EL_STR("; })")); + return out; + return 0; +} + +el_val_t list_contains(el_val_t lst, el_val_t s) { + el_val_t n = native_list_len(lst); + el_val_t i = 0; + while (i < n) { + el_val_t item = native_list_get(lst, i); + if (str_eq(item, s)) { + return 1; + } + i = (i + 1); + } + return 0; + return 0; +} + +el_val_t cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_c = cg_expr(val); + el_val_t ltype = el_get_field(stmt, EL_STR("type")); + if (str_eq(ltype, EL_STR("Int"))) { + add_int_name(name); + } + el_val_t vk = el_get_field(val, EL_STR("expr")); + if (str_eq(vk, EL_STR("Int"))) { + add_int_name(name); + } + if (list_contains(declared, name)) { + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, name), EL_STR(" = ")), val_c), EL_STR(";"))); + return declared; + } else { + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("el_val_t ")), name), EL_STR(" = ")), val_c), EL_STR(";"))); + return native_list_append(declared, name); + } + } + if (str_eq(kind, EL_STR("Return"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("Nil"))) { + emit_line(el_str_concat(indent, EL_STR("return 0;"))); + } else { + el_val_t val_c = cg_expr(val); + emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("return ")), val_c), EL_STR(";"))); + } + return declared; + } + if (str_eq(kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + cg_if_stmt(val, indent, declared); + return declared; + } + if (str_eq(val_kind, EL_STR("For"))) { + cg_for_stmt(val, indent, declared); + return declared; + } + el_val_t val_c = cg_expr(val); + emit_line(el_str_concat(el_str_concat(indent, val_c), EL_STR(";"))); + return declared; + } + if (str_eq(kind, EL_STR("While"))) { + el_val_t cond = el_get_field(stmt, EL_STR("cond")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t cond_c = cg_expr(cond); + cond_c = strip_outer_parens(cond_c); + emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("while (")), cond_c), EL_STR(") {"))); + cg_stmts(body, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + emit_line(el_str_concat(indent, EL_STR("}"))); + return declared; + } + if (str_eq(kind, EL_STR("For"))) { + el_val_t item = el_get_field(stmt, EL_STR("item")); + el_val_t list_expr = el_get_field(stmt, EL_STR("list")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + cg_for_body(item, list_expr, body, indent, declared); + return declared; + } + if (str_eq(kind, EL_STR("FnDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("TypeDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("Import"))) { + return declared; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + return declared; + } + return declared; + return 0; +} + +el_val_t strip_outer_parens(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t n = native_list_len(chars); + if (n < 2) { + return s; + } + el_val_t first = native_list_get(chars, 0); + el_val_t last = native_list_get(chars, (n - 1)); + if (str_eq(first, EL_STR("("))) { + if (str_eq(last, EL_STR(")"))) { + el_val_t depth = 1; + el_val_t i = 1; + el_val_t balanced = 1; + while (i < (n - 1)) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("("))) { + depth = (depth + 1); + } + if (str_eq(ch, EL_STR(")"))) { + depth = (depth - 1); + if (depth == 0) { + balanced = 0; + i = n; + } + } + i = (i + 1); + } + if (balanced) { + el_val_t inner = EL_STR(""); + el_val_t j = 1; + while (j < (n - 1)) { + el_val_t ch = native_list_get(chars, j); + inner = el_str_concat(inner, ch); + j = (j + 1); + } + return inner; + } + } + } + return s; + return 0; +} + +el_val_t cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t then_stmts = el_get_field(expr, EL_STR("then")); + el_val_t else_stmts = el_get_field(expr, EL_STR("else")); + el_val_t has_else = el_get_field(expr, EL_STR("has_else")); + el_val_t cond_c = cg_expr(cond); + cond_c = strip_outer_parens(cond_c); + emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("if (")), cond_c), EL_STR(") {"))); + cg_stmts(then_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + if (has_else) { + emit_line(el_str_concat(indent, EL_STR("} else {"))); + cg_stmts(else_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + } + emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared) { + el_val_t list_c = cg_expr(list_expr); + el_val_t idx = EL_STR("_el_i"); + el_val_t list_tmp = EL_STR("_el_lst"); + el_val_t len_tmp = EL_STR("_el_len"); + emit_line(el_str_concat(indent, EL_STR("{"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" el_val_t ")), list_tmp), EL_STR(" = ")), list_c), EL_STR(";"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" el_val_t ")), len_tmp), EL_STR(" = el_list_len(")), list_tmp), EL_STR(");"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" for (el_val_t ")), idx), EL_STR(" = 0; ")), idx), EL_STR(" < ")), len_tmp), EL_STR("; ")), idx), EL_STR("++) {"))); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR(" el_val_t ")), item), EL_STR(" = el_list_get(")), list_tmp), EL_STR(", ")), idx), EL_STR(");"))); + el_val_t body_decl = native_list_clone(declared); + body_decl = native_list_append(body_decl, item); + cg_stmts(body, el_str_concat(indent, EL_STR(" ")), body_decl); + emit_line(el_str_concat(indent, EL_STR(" }"))); + emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t item = el_get_field(expr, EL_STR("item")); + el_val_t list_expr = el_get_field(expr, EL_STR("list")); + el_val_t body = el_get_field(expr, EL_STR("body")); + cg_for_body(item, list_expr, body, indent, declared); + return 0; +} + +el_val_t cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared) { + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + el_val_t decl = declared; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + decl = cg_stmt(stmt, indent, decl); + i = (i + 1); + } + return decl; + return 0; +} + +el_val_t param_decl(el_val_t param, el_val_t idx) { + el_val_t name = el_get_field(param, EL_STR("name")); + return el_str_concat(EL_STR("el_val_t "), name); + return 0; +} + +el_val_t params_to_c(el_val_t params) { + el_val_t n = native_list_len(params); + if (n == 0) { + return EL_STR("void"); + } + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t param = native_list_get(params, i); + el_val_t decl = param_decl(param, i); + if (i > 0) { + out = el_str_concat(out, EL_STR(", ")); + } + out = el_str_concat(out, decl); + i = (i + 1); + } + return out; + return 0; +} + +el_val_t transform_implicit_return(el_val_t body) { + el_val_t n = native_list_len(body); + if (n == 0) { + return body; + } + el_val_t last = native_list_get(body, (n - 1)); + el_val_t last_kind = el_get_field(last, EL_STR("stmt")); + if (str_eq(last_kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(last, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + return body; + } + if (str_eq(val_kind, EL_STR("For"))) { + return body; + } + el_val_t new_body = native_list_empty(); + el_val_t i = 0; + while (i < (n - 1)) { + new_body = native_list_append(new_body, native_list_get(body, i)); + i = (i + 1); + } + el_val_t return_stmt = el_map_new(2, "stmt", EL_STR("Return"), "value", val); + new_body = native_list_append(new_body, return_stmt); + return new_body; + } + return body; + return 0; +} + +el_val_t is_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__int_names")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + return str_contains(csv, el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(","))); + return 0; +} + +el_val_t is_int_call(el_val_t call_expr) { + el_val_t func = el_get_field(call_expr, EL_STR("func")); + el_val_t fk = el_get_field(func, EL_STR("expr")); + if (!str_eq(fk, EL_STR("Ident"))) { + return 0; + } + el_val_t name = el_get_field(func, EL_STR("name")); + if (str_eq(name, EL_STR("str_len"))) { + return 1; + } + if (str_eq(name, EL_STR("str_index_of"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("str_char_code"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("el_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("len"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get_int"))) { + return 1; + } + if (str_eq(name, EL_STR("json_array_len"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_node_count"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_edge_count"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now_utc"))) { + return 1; + } + if (str_eq(name, EL_STR("time_diff"))) { + return 1; + } + if (str_eq(name, EL_STR("time_add"))) { + return 1; + } + if (str_eq(name, EL_STR("time_from_parts"))) { + return 1; + } + if (str_eq(name, EL_STR("el_abs"))) { + return 1; + } + if (str_eq(name, EL_STR("el_max"))) { + return 1; + } + if (str_eq(name, EL_STR("el_min"))) { + return 1; + } + if (str_eq(name, EL_STR("float_to_int"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_int_expr(el_val_t expr) { + el_val_t k = el_get_field(expr, EL_STR("expr")); + if (str_eq(k, EL_STR("Int"))) { + return 1; + } + if (str_eq(k, EL_STR("Ident"))) { + el_val_t name = el_get_field(expr, EL_STR("name")); + return is_int_name(name); + } + if (str_eq(k, EL_STR("Call"))) { + return is_int_call(expr); + } + if (str_eq(k, EL_STR("Neg"))) { + return is_int_expr(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(k, EL_STR("Not"))) { + return 1; + } + if (str_eq(k, EL_STR("BinOp"))) { + el_val_t op = el_get_field(expr, EL_STR("op")); + if (str_eq(op, EL_STR("EqEq"))) { + return 1; + } + if (str_eq(op, EL_STR("NotEq"))) { + return 1; + } + if (str_eq(op, EL_STR("Lt"))) { + return 1; + } + if (str_eq(op, EL_STR("Gt"))) { + return 1; + } + if (str_eq(op, EL_STR("LtEq"))) { + return 1; + } + if (str_eq(op, EL_STR("GtEq"))) { + return 1; + } + if (str_eq(op, EL_STR("And"))) { + return 1; + } + if (str_eq(op, EL_STR("Or"))) { + return 1; + } + if (str_eq(op, EL_STR("Plus"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + if (str_eq(op, EL_STR("Minus"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + if (str_eq(op, EL_STR("Star"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + if (str_eq(op, EL_STR("Slash"))) { + if (is_int_expr(el_get_field(expr, EL_STR("left")))) { + if (is_int_expr(el_get_field(expr, EL_STR("right")))) { + return 1; + } + } + return 0; + } + return 0; + } + return 0; + return 0; +} + +el_val_t cap_record_violation(el_val_t kind, el_val_t fn_name) { + el_val_t csv = state_get(EL_STR("__cap_violations")); + if (str_eq(csv, EL_STR(""))) { + csv = EL_STR(","); + } + el_val_t entry = el_str_concat(el_str_concat(kind, EL_STR(":")), fn_name); + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), entry), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__cap_violations"), el_str_concat(el_str_concat(csv, entry), EL_STR(","))); + return 1; + return 0; +} + +el_val_t is_self_formation_call(el_val_t fn_name) { + if (str_eq(fn_name, EL_STR("llm_call_agentic"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_register_tool"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_emit"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_field"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_dharma_call(el_val_t fn_name) { + if (str_eq(fn_name, EL_STR("dharma_connect"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_send"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_activate"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_emit"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_field"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_strengthen"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_relationship"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("dharma_peers"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_llm_call(el_val_t fn_name) { + if (str_eq(fn_name, EL_STR("llm_call"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_call_system"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_call_agentic"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_vision"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_register_tool"))) { + return 1; + } + if (str_eq(fn_name, EL_STR("llm_models"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t cap_check_call(el_val_t fn_name) { + el_val_t kind = state_get(EL_STR("__program_kind")); + if (str_eq(kind, EL_STR("cgi"))) { + return 1; + } + if (str_eq(kind, EL_STR("service"))) { + if (is_self_formation_call(fn_name)) { + cap_record_violation(EL_STR("service"), fn_name); + return 0; + } + return 1; + } + if (is_dharma_call(fn_name)) { + cap_record_violation(EL_STR("utility"), fn_name); + return 0; + } + if (is_llm_call(fn_name)) { + cap_record_violation(EL_STR("utility"), fn_name); + return 0; + } + return 1; + return 0; +} + +el_val_t emit_cap_violations(void) { + el_val_t csv = state_get(EL_STR("__cap_violations")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + if (str_eq(csv, EL_STR(","))) { + return 0; + } + el_val_t n = str_len(csv); + el_val_t i = 1; + while (i < n) { + el_val_t next_comma = str_index_of(str_slice(csv, i, n), EL_STR(",")); + if (next_comma < 0) { + return 0; + } + el_val_t entry = str_slice(csv, i, (i + next_comma)); + el_val_t colon = str_index_of(entry, EL_STR(":")); + if (colon > 0) { + el_val_t kind = str_slice(entry, 0, colon); + el_val_t fn_name = str_slice(entry, (colon + 1), str_len(entry)); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("#error \"capability violation: '"), kind), EL_STR("' programs may not call '")), fn_name), EL_STR("' (self-formation primitive — only 'cgi' programs may use it)\""))); + } + i = ((i + next_comma) + 1); + } + return 0; +} + +el_val_t builtin_arity(el_val_t name) { + if (str_eq(name, EL_STR("println"))) { + return 1; + } + if (str_eq(name, EL_STR("print"))) { + return 1; + } + if (str_eq(name, EL_STR("readline"))) { + return 0; + } + if (str_eq(name, EL_STR("el_str_concat"))) { + return 2; + } + if (str_eq(name, EL_STR("str_eq"))) { + return 2; + } + if (str_eq(name, EL_STR("str_starts_with"))) { + return 2; + } + if (str_eq(name, EL_STR("str_ends_with"))) { + return 2; + } + if (str_eq(name, EL_STR("str_len"))) { + return 1; + } + if (str_eq(name, EL_STR("str_concat"))) { + return 2; + } + if (str_eq(name, EL_STR("int_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("str_slice"))) { + return 3; + } + if (str_eq(name, EL_STR("str_contains"))) { + return 2; + } + if (str_eq(name, EL_STR("str_replace"))) { + return 3; + } + if (str_eq(name, EL_STR("str_to_upper"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_lower"))) { + return 1; + } + if (str_eq(name, EL_STR("str_trim"))) { + return 1; + } + if (str_eq(name, EL_STR("str_index_of"))) { + return 2; + } + if (str_eq(name, EL_STR("str_split"))) { + return 2; + } + if (str_eq(name, EL_STR("str_char_at"))) { + return 2; + } + if (str_eq(name, EL_STR("str_char_code"))) { + return 2; + } + if (str_eq(name, EL_STR("str_pad_left"))) { + return 3; + } + if (str_eq(name, EL_STR("str_pad_right"))) { + return 3; + } + if (str_eq(name, EL_STR("str_format"))) { + return 2; + } + if (str_eq(name, EL_STR("str_lower"))) { + return 1; + } + if (str_eq(name, EL_STR("str_upper"))) { + return 1; + } + if (str_eq(name, EL_STR("el_abs"))) { + return 1; + } + if (str_eq(name, EL_STR("el_max"))) { + return 2; + } + if (str_eq(name, EL_STR("el_min"))) { + return 2; + } + if (str_eq(name, EL_STR("el_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("el_list_get"))) { + return 2; + } + if (str_eq(name, EL_STR("el_list_append"))) { + return 2; + } + if (str_eq(name, EL_STR("el_list_empty"))) { + return 0; + } + if (str_eq(name, EL_STR("el_list_clone"))) { + return 1; + } + if (str_eq(name, EL_STR("list_push"))) { + return 2; + } + if (str_eq(name, EL_STR("list_push_front"))) { + return 2; + } + if (str_eq(name, EL_STR("list_join"))) { + return 2; + } + if (str_eq(name, EL_STR("list_range"))) { + return 2; + } + if (str_eq(name, EL_STR("el_get_field"))) { + return 2; + } + if (str_eq(name, EL_STR("el_map_get"))) { + return 2; + } + if (str_eq(name, EL_STR("el_map_set"))) { + return 3; + } + if (str_eq(name, EL_STR("http_get"))) { + return 1; + } + if (str_eq(name, EL_STR("http_post"))) { + return 2; + } + if (str_eq(name, EL_STR("http_post_json"))) { + return 2; + } + if (str_eq(name, EL_STR("http_get_with_headers"))) { + return 2; + } + if (str_eq(name, EL_STR("http_post_with_headers"))) { + return 3; + } + if (str_eq(name, EL_STR("http_post_form_auth"))) { + return 3; + } + if (str_eq(name, EL_STR("http_serve"))) { + return 2; + } + if (str_eq(name, EL_STR("http_set_handler"))) { + return 1; + } + if (str_eq(name, EL_STR("fs_read"))) { + return 1; + } + if (str_eq(name, EL_STR("fs_write"))) { + return 2; + } + if (str_eq(name, EL_STR("fs_list"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get"))) { + return 2; + } + if (str_eq(name, EL_STR("json_parse"))) { + return 1; + } + if (str_eq(name, EL_STR("json_stringify"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get_string"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_int"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_float"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_bool"))) { + return 2; + } + if (str_eq(name, EL_STR("json_get_raw"))) { + return 2; + } + if (str_eq(name, EL_STR("json_set"))) { + return 3; + } + if (str_eq(name, EL_STR("json_array_len"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now"))) { + return 0; + } + if (str_eq(name, EL_STR("time_now_utc"))) { + return 0; + } + if (str_eq(name, EL_STR("sleep_secs"))) { + return 1; + } + if (str_eq(name, EL_STR("sleep_ms"))) { + return 1; + } + if (str_eq(name, EL_STR("time_format"))) { + return 2; + } + if (str_eq(name, EL_STR("time_to_parts"))) { + return 1; + } + if (str_eq(name, EL_STR("time_from_parts"))) { + return 3; + } + if (str_eq(name, EL_STR("time_add"))) { + return 3; + } + if (str_eq(name, EL_STR("time_diff"))) { + return 3; + } + if (str_eq(name, EL_STR("uuid_new"))) { + return 0; + } + if (str_eq(name, EL_STR("uuid_v4"))) { + return 0; + } + if (str_eq(name, EL_STR("env"))) { + return 1; + } + if (str_eq(name, EL_STR("state_set"))) { + return 2; + } + if (str_eq(name, EL_STR("state_get"))) { + return 1; + } + if (str_eq(name, EL_STR("state_del"))) { + return 1; + } + if (str_eq(name, EL_STR("state_keys"))) { + return 0; + } + if (str_eq(name, EL_STR("float_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("int_to_float"))) { + return 1; + } + if (str_eq(name, EL_STR("float_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("format_float"))) { + return 2; + } + if (str_eq(name, EL_STR("decimal_round"))) { + return 2; + } + if (str_eq(name, EL_STR("str_to_float"))) { + return 1; + } + if (str_eq(name, EL_STR("math_sqrt"))) { + return 1; + } + if (str_eq(name, EL_STR("math_log"))) { + return 1; + } + if (str_eq(name, EL_STR("math_ln"))) { + return 1; + } + if (str_eq(name, EL_STR("math_sin"))) { + return 1; + } + if (str_eq(name, EL_STR("math_cos"))) { + return 1; + } + if (str_eq(name, EL_STR("math_pi"))) { + return 0; + } + if (str_eq(name, EL_STR("bool_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("exit_program"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_connect"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_send"))) { + return 2; + } + if (str_eq(name, EL_STR("dharma_activate"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_emit"))) { + return 2; + } + if (str_eq(name, EL_STR("dharma_field"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_strengthen"))) { + return 2; + } + if (str_eq(name, EL_STR("dharma_relationship"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_peers"))) { + return 0; + } + if (str_eq(name, EL_STR("engram_node"))) { + return 3; + } + if (str_eq(name, EL_STR("engram_node_full"))) { + return 8; + } + if (str_eq(name, EL_STR("engram_get_node"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_strengthen"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_forget"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_node_count"))) { + return 0; + } + if (str_eq(name, EL_STR("engram_search"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_scan_nodes"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_connect"))) { + return 4; + } + if (str_eq(name, EL_STR("engram_edge_between"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_neighbors"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_neighbors_filtered"))) { + return 3; + } + if (str_eq(name, EL_STR("engram_edge_count"))) { + return 0; + } + if (str_eq(name, EL_STR("engram_activate"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_save"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_load"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_get_node_json"))) { + return 1; + } + if (str_eq(name, EL_STR("engram_search_json"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_scan_nodes_json"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_neighbors_json"))) { + return 3; + } + if (str_eq(name, EL_STR("engram_activate_json"))) { + return 2; + } + if (str_eq(name, EL_STR("engram_stats_json"))) { + return 0; + } + if (str_eq(name, EL_STR("llm_call"))) { + return 2; + } + if (str_eq(name, EL_STR("llm_call_system"))) { + return 3; + } + if (str_eq(name, EL_STR("llm_call_agentic"))) { + return 4; + } + if (str_eq(name, EL_STR("llm_vision"))) { + return 4; + } + if (str_eq(name, EL_STR("llm_models"))) { + return 0; + } + if (str_eq(name, EL_STR("llm_register_tool"))) { + return 2; + } + if (str_eq(name, EL_STR("sha256_hex"))) { + return 1; + } + if (str_eq(name, EL_STR("sha256_bytes"))) { + return 1; + } + if (str_eq(name, EL_STR("hmac_sha256_hex"))) { + return 2; + } + if (str_eq(name, EL_STR("hmac_sha256_bytes"))) { + return 2; + } + if (str_eq(name, EL_STR("base64_encode"))) { + return 1; + } + if (str_eq(name, EL_STR("base64_decode"))) { + return 1; + } + if (str_eq(name, EL_STR("base64url_encode"))) { + return 1; + } + if (str_eq(name, EL_STR("base64url_decode"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_get"))) { + return 2; + } + if (str_eq(name, EL_STR("native_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_append"))) { + return 2; + } + if (str_eq(name, EL_STR("native_list_empty"))) { + return 0; + } + if (str_eq(name, EL_STR("native_list_clone"))) { + return 1; + } + if (str_eq(name, EL_STR("native_string_chars"))) { + return 1; + } + if (str_eq(name, EL_STR("native_int_to_str"))) { + return 1; + } + if (str_eq(name, EL_STR("append"))) { + return 2; + } + if (str_eq(name, EL_STR("len"))) { + return 1; + } + if (str_eq(name, EL_STR("get"))) { + return 2; + } + if (str_eq(name, EL_STR("map_get"))) { + return 2; + } + if (str_eq(name, EL_STR("map_set"))) { + return 3; + } + return (-1); + return 0; +} + +el_val_t arity_record_violation(el_val_t fn_name, el_val_t expected, el_val_t actual) { + el_val_t csv = state_get(EL_STR("__arity_violations")); + if (str_eq(csv, EL_STR(""))) { + csv = EL_STR(","); + } + el_val_t entry = el_str_concat(el_str_concat(el_str_concat(el_str_concat(fn_name, EL_STR("|")), native_int_to_str(expected)), EL_STR("|")), native_int_to_str(actual)); + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), entry), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__arity_violations"), el_str_concat(el_str_concat(csv, entry), EL_STR(","))); + return 1; + return 0; +} + +el_val_t arity_check_call(el_val_t fn_name, el_val_t actual) { + el_val_t expected = builtin_arity(fn_name); + if (expected < 0) { + return 1; + } + if (expected == actual) { + return 1; + } + arity_record_violation(fn_name, expected, actual); + return 1; + return 0; +} + +el_val_t emit_arity_violations(void) { + el_val_t csv = state_get(EL_STR("__arity_violations")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + if (str_eq(csv, EL_STR(","))) { + return 0; + } + el_val_t n = str_len(csv); + el_val_t i = 1; + while (i < n) { + el_val_t next_comma = str_index_of(str_slice(csv, i, n), EL_STR(",")); + if (next_comma < 0) { + return 0; + } + el_val_t entry = str_slice(csv, i, (i + next_comma)); + el_val_t p1 = str_index_of(entry, EL_STR("|")); + if (p1 > 0) { + el_val_t fn_name = str_slice(entry, 0, p1); + el_val_t rest = str_slice(entry, (p1 + 1), str_len(entry)); + el_val_t p2 = str_index_of(rest, EL_STR("|")); + if (p2 > 0) { + el_val_t exp_s = str_slice(rest, 0, p2); + el_val_t act_s = str_slice(rest, (p2 + 1), str_len(rest)); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("#error \"arity error: '"), fn_name), EL_STR("' takes ")), exp_s), EL_STR(" arguments, but called with ")), act_s), EL_STR("\""))); + } + } + i = ((i + next_comma) + 1); + } + return 0; +} + +el_val_t add_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__int_names")); + if (str_eq(csv, EL_STR(""))) { + csv; + EL_NULL; + EL_STR(","); + } + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__int_names"), el_str_concat(el_str_concat(csv, name), EL_STR(","))); + return 1; + return 0; +} + +el_val_t build_int_names_for_params(el_val_t params) { + state_set(EL_STR("__int_names"), EL_STR(",")); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + el_val_t ptype = el_get_field(param, EL_STR("type")); + if (str_eq(ptype, EL_STR("Int"))) { + add_int_name(pname); + } + pi = (pi + 1); + } + return 1; + return 0; +} + +el_val_t cg_fn(el_val_t stmt) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + if (str_eq(fn_name, EL_STR("main"))) { + return 0; + } + el_val_t params = el_get_field(stmt, EL_STR("params")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t ret_type = el_get_field(stmt, EL_STR("ret_type")); + el_val_t params_c = params_to_c(params); + el_val_t decorator = el_get_field(stmt, EL_STR("decorator")); + if (vbd_has_restricted_call(body)) { + if (!str_eq(decorator, EL_STR("manager"))) { + emit_line(el_str_concat(el_str_concat(EL_STR("#error \"VBD violation: dharma_emit/dharma_field called from non-@manager fn '"), fn_name), EL_STR("'\""))); + } + } + build_int_names_for_params(params); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_val_t "), fn_name), EL_STR("(")), params_c), EL_STR(") {"))); + el_val_t decl = native_list_empty(); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + decl = native_list_append(decl, pname); + pi = (pi + 1); + } + el_val_t body_xformed = body; + if (!str_eq(ret_type, EL_STR("Void"))) { + body_xformed = transform_implicit_return(body); + } + cg_stmts(body_xformed, EL_STR(" "), decl); + emit_line(EL_STR(" return 0;")); + emit_line(EL_STR("}")); + emit_blank(); + return 0; +} + +el_val_t is_fndef(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("FnDef"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t is_top_level_decl(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("TypeDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("Import"))) { + return 1; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t cgi_arg(el_val_t value, el_val_t has_value) { + if (has_value) { + return el_str_concat(el_str_concat(EL_STR("EL_STR("), c_str_lit(value)), EL_STR(")")); + } + return EL_STR("EL_NULL"); + return 0; +} + +el_val_t vbd_is_restricted_name(el_val_t name) { + if (str_eq(name, EL_STR("dharma_emit"))) { + return 1; + } + if (str_eq(name, EL_STR("dharma_field"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t vbd_expr_has_restricted_call(el_val_t expr) { + el_val_t kind = el_get_field(expr, EL_STR("expr")); + if (str_eq(kind, EL_STR("Call"))) { + el_val_t func = el_get_field(expr, EL_STR("func")); + el_val_t fk = el_get_field(func, EL_STR("expr")); + if (str_eq(fk, EL_STR("Ident"))) { + el_val_t fname = el_get_field(func, EL_STR("name")); + if (vbd_is_restricted_name(fname)) { + return 1; + } + } + if (vbd_expr_has_restricted_call(func)) { + return 1; + } + el_val_t args = el_get_field(expr, EL_STR("args")); + el_val_t an = native_list_len(args); + el_val_t ai = 0; + while (ai < an) { + el_val_t a = native_list_get(args, ai); + if (vbd_expr_has_restricted_call(a)) { + return 1; + } + ai = (ai + 1); + } + return 0; + } + if (str_eq(kind, EL_STR("BinOp"))) { + el_val_t l = el_get_field(expr, EL_STR("left")); + el_val_t r = el_get_field(expr, EL_STR("right")); + if (vbd_expr_has_restricted_call(l)) { + return 1; + } + if (vbd_expr_has_restricted_call(r)) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("Not"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(kind, EL_STR("Neg"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(kind, EL_STR("Field"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("object"))); + } + if (str_eq(kind, EL_STR("Index"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("object")))) { + return 1; + } + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("index")))) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("Try"))) { + return vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("inner"))); + } + if (str_eq(kind, EL_STR("Array"))) { + el_val_t elems = el_get_field(expr, EL_STR("elems")); + el_val_t n = native_list_len(elems); + el_val_t i = 0; + while (i < n) { + el_val_t e = native_list_get(elems, i); + if (vbd_expr_has_restricted_call(e)) { + return 1; + } + i = (i + 1); + } + return 0; + } + if (str_eq(kind, EL_STR("Map"))) { + el_val_t pairs = el_get_field(expr, EL_STR("pairs")); + el_val_t n = native_list_len(pairs); + el_val_t i = 0; + while (i < n) { + el_val_t pair = native_list_get(pairs, i); + el_val_t v = el_get_field(pair, EL_STR("value")); + if (vbd_expr_has_restricted_call(v)) { + return 1; + } + i = (i + 1); + } + return 0; + } + if (str_eq(kind, EL_STR("If"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("cond")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(expr, EL_STR("then")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(expr, EL_STR("else")))) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("For"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("list")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(expr, EL_STR("body")))) { + return 1; + } + return 0; + } + if (str_eq(kind, EL_STR("Match"))) { + if (vbd_expr_has_restricted_call(el_get_field(expr, EL_STR("subject")))) { + return 1; + } + el_val_t arms = el_get_field(expr, EL_STR("arms")); + el_val_t n = native_list_len(arms); + el_val_t i = 0; + while (i < n) { + el_val_t arm = native_list_get(arms, i); + el_val_t body = el_get_field(arm, EL_STR("body")); + if (vbd_expr_has_restricted_call(body)) { + return 1; + } + i = (i + 1); + } + return 0; + } + return 0; + return 0; +} + +el_val_t vbd_has_restricted_call(el_val_t stmts) { + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + while (i < n) { + el_val_t s = native_list_get(stmts, i); + el_val_t sk = el_get_field(s, EL_STR("stmt")); + if (str_eq(sk, EL_STR("Let"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("value")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("Return"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("value")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("Expr"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("value")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("While"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("cond")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(s, EL_STR("body")))) { + return 1; + } + } + if (str_eq(sk, EL_STR("For"))) { + if (vbd_expr_has_restricted_call(el_get_field(s, EL_STR("list")))) { + return 1; + } + if (vbd_has_restricted_call(el_get_field(s, EL_STR("body")))) { + return 1; + } + } + i = (i + 1); + } + return 0; + return 0; +} + +el_val_t codegen(el_val_t stmts, el_val_t source) { + el_val_t n_top = native_list_len(stmts); + el_val_t cgi_count = 0; + el_val_t cgi_block = el_map_new(1, "stmt", EL_STR("None")); + el_val_t svc_count = 0; + el_val_t svc_block = el_map_new(1, "stmt", EL_STR("None")); + el_val_t ti = 0; + while (ti < n_top) { + el_val_t s = native_list_get(stmts, ti); + el_val_t sk = el_get_field(s, EL_STR("stmt")); + if (str_eq(sk, EL_STR("CgiBlock"))) { + cgi_count = (cgi_count + 1); + if (cgi_count == 1) { + cgi_block = s; + } + } + if (str_eq(sk, EL_STR("ServiceBlock"))) { + svc_count = (svc_count + 1); + if (svc_count == 1) { + svc_block = s; + } + } + ti = (ti + 1); + } + if (cgi_count > 1) { + emit_line(EL_STR("#error \"El: multiple cgi blocks in program (only one allowed)\"")); + } + if (svc_count > 1) { + emit_line(EL_STR("#error \"El: multiple service blocks in program (only one allowed)\"")); + } + if (cgi_count >= 1) { + if (svc_count >= 1) { + emit_line(EL_STR("#error \"El: program declares both cgi and service blocks (mutually exclusive — pick one)\"")); + } + } + el_val_t kind = EL_STR("utility"); + if (cgi_count >= 1) { + kind = EL_STR("cgi"); + } + if (svc_count >= 1) { + kind = EL_STR("service"); + } + state_set(EL_STR("__program_kind"), kind); + state_set(EL_STR("__cap_violations"), EL_STR("")); + state_set(EL_STR("__arity_violations"), EL_STR("")); + emit_line(EL_STR("#include ")); + emit_line(EL_STR("#include ")); + emit_line(EL_STR("#include \"el_runtime.h\"")); + emit_blank(); + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("FnDef"))) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + if (!str_eq(fn_name, EL_STR("main"))) { + el_val_t params = el_get_field(stmt, EL_STR("params")); + el_val_t params_c = params_to_c(params); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_val_t "), fn_name), EL_STR("(")), params_c), EL_STR(");"))); + } + } + i = (i + 1); + } + emit_blank(); + el_val_t has_toplevel_lets = 0; + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + el_val_t ltype = el_get_field(stmt, EL_STR("type")); + if (str_eq(ltype, EL_STR("Int"))) { + add_int_name(name); + } + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t vk = el_get_field(val, EL_STR("expr")); + if (str_eq(vk, EL_STR("Int"))) { + add_int_name(name); + } + emit_line(el_str_concat(el_str_concat(EL_STR("el_val_t "), name), EL_STR(";"))); + has_toplevel_lets = 1; + } + i = (i + 1); + } + if (has_toplevel_lets) { + emit_blank(); + } + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (is_fndef(stmt)) { + cg_fn(stmt); + } + i = (i + 1); + } + emit_line(EL_STR("int main(int argc, char** argv) {")); + emit_line(EL_STR(" el_runtime_init_args(argc, argv);")); + if (cgi_count >= 1) { + el_val_t cname = el_get_field(cgi_block, EL_STR("name")); + el_val_t cdid = el_get_field(cgi_block, EL_STR("dharma_id")); + el_val_t cprin = el_get_field(cgi_block, EL_STR("principal")); + el_val_t cnet = el_get_field(cgi_block, EL_STR("network")); + el_val_t ceng = el_get_field(cgi_block, EL_STR("engram")); + el_val_t has_did = el_get_field(cgi_block, EL_STR("has_dharma_id")); + el_val_t has_prin = el_get_field(cgi_block, EL_STR("has_principal")); + el_val_t has_net = el_get_field(cgi_block, EL_STR("has_network")); + el_val_t has_eng = el_get_field(cgi_block, EL_STR("has_engram")); + el_val_t arg_name = el_str_concat(el_str_concat(EL_STR("EL_STR("), c_str_lit(cname)), EL_STR(")")); + el_val_t arg_did = cgi_arg(cdid, has_did); + el_val_t arg_prin = cgi_arg(cprin, has_prin); + el_val_t arg_net = cgi_arg(cnet, has_net); + el_val_t arg_eng = cgi_arg(ceng, has_eng); + emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR(" el_cgi_init("), arg_name), EL_STR(", ")), arg_did), EL_STR(", ")), arg_prin), EL_STR(", ")), arg_net), EL_STR(", ")), arg_eng), EL_STR(");"))); + } + el_val_t main_decl = native_list_empty(); + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + main_decl = native_list_append(main_decl, name); + } + i = (i + 1); + } + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (is_fndef(stmt)) { + } else { + if (is_top_level_decl(stmt)) { + } else { + main_decl = cg_stmt(stmt, EL_STR(" "), main_decl); + } + } + el_release(stmt); + i = (i + 1); + } + emit_line(EL_STR(" return 0;")); + emit_line(EL_STR("}")); + emit_blank(); + emit_cap_violations(); + emit_arity_violations(); + return EL_STR(""); + return 0; +} + +el_val_t js_escape(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t total = native_list_len(chars); + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < total) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("\""))) { + out = el_str_concat(out, EL_STR("\\\"")); + } else { + if (str_eq(ch, EL_STR("\\"))) { + out = el_str_concat(out, EL_STR("\\\\")); + } else { + if (str_eq(ch, EL_STR("\n"))) { + out = el_str_concat(out, EL_STR("\\n")); + } else { + if (str_eq(ch, EL_STR("\r"))) { + out = el_str_concat(out, EL_STR("\\r")); + } else { + if (str_eq(ch, EL_STR("\t"))) { + out = el_str_concat(out, EL_STR("\\t")); + } else { + out = el_str_concat(out, ch); + } + } + } + } + } + i = (i + 1); + } + return out; + return 0; +} + +el_val_t js_str_lit(el_val_t s) { + return el_str_concat(el_str_concat(EL_STR("\""), js_escape(s)), EL_STR("\"")); + return 0; +} + +el_val_t js_emit_line(el_val_t line) { + println(line); + return 0; +} + +el_val_t js_emit_blank(void) { + println(EL_STR("")); + return 0; +} + +el_val_t js_binop(el_val_t op) { + if (str_eq(op, EL_STR("Plus"))) { + return EL_STR("+"); + } + if (str_eq(op, EL_STR("Minus"))) { + return EL_STR("-"); + } + if (str_eq(op, EL_STR("Star"))) { + return EL_STR("*"); + } + if (str_eq(op, EL_STR("Slash"))) { + return EL_STR("/"); + } + if (str_eq(op, EL_STR("Percent"))) { + return EL_STR("%"); + } + if (str_eq(op, EL_STR("EqEq"))) { + return EL_STR("==="); + } + if (str_eq(op, EL_STR("NotEq"))) { + return EL_STR("!=="); + } + if (str_eq(op, EL_STR("Lt"))) { + return EL_STR("<"); + } + if (str_eq(op, EL_STR("Gt"))) { + return EL_STR(">"); + } + if (str_eq(op, EL_STR("LtEq"))) { + return EL_STR("<="); + } + if (str_eq(op, EL_STR("GtEq"))) { + return EL_STR(">="); + } + if (str_eq(op, EL_STR("And"))) { + return EL_STR("&&"); + } + if (str_eq(op, EL_STR("Or"))) { + return EL_STR("||"); + } + return op; + return 0; +} + +el_val_t js_is_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__js_int_names")); + if (str_eq(csv, EL_STR(""))) { + return 0; + } + return str_contains(csv, el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(","))); + return 0; +} + +el_val_t js_add_int_name(el_val_t name) { + el_val_t csv = state_get(EL_STR("__js_int_names")); + if (str_eq(csv, EL_STR(""))) { + csv; + EL_NULL; + EL_STR(","); + } + el_val_t key = el_str_concat(el_str_concat(EL_STR(","), name), EL_STR(",")); + if (str_contains(csv, key)) { + return 1; + } + state_set(EL_STR("__js_int_names"), el_str_concat(el_str_concat(csv, name), EL_STR(","))); + return 1; + return 0; +} + +el_val_t js_build_int_names_for_params(el_val_t params) { + state_set(EL_STR("__js_int_names"), EL_STR(",")); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + el_val_t ptype = el_get_field(param, EL_STR("type")); + if (str_eq(ptype, EL_STR("Int"))) { + js_add_int_name(pname); + } + pi = (pi + 1); + } + return 1; + return 0; +} + +el_val_t js_is_int_call(el_val_t call_expr) { + el_val_t func = el_get_field(call_expr, EL_STR("func")); + el_val_t fk = el_get_field(func, EL_STR("expr")); + if (!str_eq(fk, EL_STR("Ident"))) { + return 0; + } + el_val_t name = el_get_field(func, EL_STR("name")); + if (str_eq(name, EL_STR("str_len"))) { + return 1; + } + if (str_eq(name, EL_STR("str_index_of"))) { + return 1; + } + if (str_eq(name, EL_STR("str_to_int"))) { + return 1; + } + if (str_eq(name, EL_STR("str_char_code"))) { + return 1; + } + if (str_eq(name, EL_STR("native_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("el_list_len"))) { + return 1; + } + if (str_eq(name, EL_STR("len"))) { + return 1; + } + if (str_eq(name, EL_STR("json_get_int"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now"))) { + return 1; + } + if (str_eq(name, EL_STR("time_now_utc"))) { + return 1; + } + if (str_eq(name, EL_STR("el_abs"))) { + return 1; + } + if (str_eq(name, EL_STR("el_max"))) { + return 1; + } + if (str_eq(name, EL_STR("el_min"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t js_cg_expr(el_val_t expr) { + el_val_t kind = el_get_field(expr, EL_STR("expr")); + if (str_eq(kind, EL_STR("Int"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return v; + } + if (str_eq(kind, EL_STR("Float"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return v; + } + if (str_eq(kind, EL_STR("Str"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + return js_str_lit(v); + } + if (str_eq(kind, EL_STR("Bool"))) { + el_val_t v = el_get_field(expr, EL_STR("value")); + if (str_eq(v, EL_STR("true"))) { + return EL_STR("true"); + } + return EL_STR("false"); + } + if (str_eq(kind, EL_STR("Nil"))) { + return EL_STR("null"); + } + if (str_eq(kind, EL_STR("Ident"))) { + el_val_t name = el_get_field(expr, EL_STR("name")); + return name; + } + if (str_eq(kind, EL_STR("Not"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = js_cg_expr(inner); + return el_str_concat(EL_STR("!"), inner_c); + } + if (str_eq(kind, EL_STR("Neg"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + el_val_t inner_c = js_cg_expr(inner); + return el_str_concat(el_str_concat(EL_STR("(-"), inner_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("BinOp"))) { + el_val_t op = el_get_field(expr, EL_STR("op")); + el_val_t left = el_get_field(expr, EL_STR("left")); + el_val_t right = el_get_field(expr, EL_STR("right")); + el_val_t left_c = js_cg_expr(left); + el_val_t right_c = js_cg_expr(right); + el_val_t left_kind = el_get_field(left, EL_STR("expr")); + el_val_t right_kind = el_get_field(right, EL_STR("expr")); + if (str_eq(op, EL_STR("Plus"))) { + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Call"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_call(right)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(right_kind, EL_STR("Ident"))) { + if (str_eq(left_kind, EL_STR("Call"))) { + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(rname)) { + if (js_is_int_call(left)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Call"))) { + if (str_eq(right_kind, EL_STR("Call"))) { + if (js_is_int_call(left)) { + if (js_is_int_call(right)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" + ")), right_c), EL_STR(")")); + } + } + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Call"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Ident"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_str_concat("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + } + if (str_eq(op, EL_STR("EqEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" === ")), right_c), EL_STR(")")); + } + if (str_eq(op, EL_STR("NotEq"))) { + if (str_eq(left_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Int"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Bool"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Nil"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + if (str_eq(left_kind, EL_STR("Ident"))) { + if (str_eq(right_kind, EL_STR("Ident"))) { + el_val_t lname = el_get_field(left, EL_STR("name")); + el_val_t rname = el_get_field(right, EL_STR("name")); + if (js_is_int_name(lname)) { + if (js_is_int_name(rname)) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + } + } + } + if (str_eq(left_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + if (str_eq(right_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("!str_eq("), left_c), EL_STR(", ")), right_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" !== ")), right_c), EL_STR(")")); + } + el_val_t op_c = js_binop(op); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("("), left_c), EL_STR(" ")), op_c), EL_STR(" ")), right_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Call"))) { + el_val_t func = el_get_field(expr, EL_STR("func")); + el_val_t args = el_get_field(expr, EL_STR("args")); + el_val_t arity = native_list_len(args); + el_val_t func_kind = el_get_field(func, EL_STR("expr")); + el_val_t args_c = EL_STR(""); + el_val_t i = 0; + while (i < arity) { + el_val_t arg = native_list_get(args, i); + el_val_t arg_c = js_cg_expr(arg); + if (i > 0) { + args_c = el_str_concat(args_c, EL_STR(", ")); + } + args_c = el_str_concat(args_c, arg_c); + i = (i + 1); + } + if (str_eq(func_kind, EL_STR("Ident"))) { + el_val_t fn_name = el_get_field(func, EL_STR("name")); + return el_str_concat(el_str_concat(el_str_concat(fn_name, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(func_kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(func, EL_STR("object")); + el_val_t field = el_get_field(func, EL_STR("field")); + el_val_t obj_c = js_cg_expr(obj); + if (arity > 0) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(", ")), args_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(field, EL_STR("(")), obj_c), EL_STR(")")); + } + el_val_t fn_c = js_cg_expr(func); + return el_str_concat(el_str_concat(el_str_concat(fn_c, EL_STR("(")), args_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Field"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t field = el_get_field(expr, EL_STR("field")); + el_val_t obj_c = js_cg_expr(obj); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", ")), js_str_lit(field)), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Index"))) { + el_val_t obj = el_get_field(expr, EL_STR("object")); + el_val_t idx = el_get_field(expr, EL_STR("index")); + el_val_t obj_c = js_cg_expr(obj); + el_val_t idx_c = js_cg_expr(idx); + el_val_t idx_kind = el_get_field(idx, EL_STR("expr")); + if (str_eq(idx_kind, EL_STR("Str"))) { + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_get_field("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("el_list_get("), obj_c), EL_STR(", ")), idx_c), EL_STR(")")); + } + if (str_eq(kind, EL_STR("Array"))) { + el_val_t elems = el_get_field(expr, EL_STR("elems")); + el_val_t n = native_list_len(elems); + if (n == 0) { + return EL_STR("[]"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t elem = native_list_get(elems, i); + el_val_t elem_c = js_cg_expr(elem); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(items, elem_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(EL_STR("["), items), EL_STR("]")); + } + if (str_eq(kind, EL_STR("Map"))) { + el_val_t pairs = el_get_field(expr, EL_STR("pairs")); + el_val_t n = native_list_len(pairs); + if (n == 0) { + return EL_STR("{}"); + } + el_val_t items = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t pair = native_list_get(pairs, i); + el_val_t key = el_get_field(pair, EL_STR("key")); + el_val_t val = el_get_field(pair, EL_STR("value")); + el_val_t val_c = js_cg_expr(val); + if (i > 0) { + items = el_str_concat(items, EL_STR(", ")); + } + items = el_str_concat(el_str_concat(el_str_concat(items, js_str_lit(key)), EL_STR(": ")), val_c); + i = (i + 1); + } + return el_str_concat(el_str_concat(EL_STR("{"), items), EL_STR("}")); + } + if (str_eq(kind, EL_STR("Try"))) { + el_val_t inner = el_get_field(expr, EL_STR("inner")); + return js_cg_expr(inner); + } + if (str_eq(kind, EL_STR("If"))) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t cond_c = js_cg_expr(cond); + return el_str_concat(el_str_concat(EL_STR("("), cond_c), EL_STR(" ? 1 : 0)")); + } + if (str_eq(kind, EL_STR("Match"))) { + return js_cg_match(expr); + } + return EL_STR("null"); + return 0; +} + +el_val_t js_next_match_id(void) { + el_val_t csv = state_get(EL_STR("__js_match_counter")); + el_val_t n = 0; + if (!str_eq(csv, EL_STR(""))) { + n = str_to_int(csv); + } + n = (n + 1); + state_set(EL_STR("__js_match_counter"), native_int_to_str(n)); + return native_int_to_str(n); + return 0; +} + +el_val_t js_cg_match(el_val_t expr) { + el_val_t subject = el_get_field(expr, EL_STR("subject")); + el_val_t arms = el_get_field(expr, EL_STR("arms")); + el_val_t subj_c = js_cg_expr(subject); + el_val_t id = js_next_match_id(); + el_val_t subj_var = el_str_concat(EL_STR("_match_subj_"), id); + el_val_t out = el_str_concat(el_str_concat(EL_STR("(("), subj_var), EL_STR(") => { ")); + el_val_t n = native_list_len(arms); + el_val_t i = 0; + while (i < n) { + el_val_t arm = native_list_get(arms, i); + el_val_t pat = el_get_field(arm, EL_STR("pattern")); + el_val_t body = el_get_field(arm, EL_STR("body")); + el_val_t pkind = el_get_field(pat, EL_STR("pattern")); + el_val_t body_c = js_cg_expr(body); + if (str_eq(pkind, EL_STR("Wildcard"))) { + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("return (")), body_c), EL_STR("); ")); + } else { + if (str_eq(pkind, EL_STR("Binding"))) { + el_val_t bname = el_get_field(pat, EL_STR("name")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("{ const ")), bname), EL_STR(" = ")), subj_var), EL_STR("; return (")), body_c), EL_STR("); } ")); + } else { + if (str_eq(pkind, EL_STR("LitInt"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" === ")), v), EL_STR(") return (")), body_c), EL_STR("); ")); + } else { + if (str_eq(pkind, EL_STR("LitStr"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (str_eq(")), subj_var), EL_STR(", ")), js_str_lit(v)), EL_STR(")) return (")), body_c), EL_STR("); ")); + } else { + if (str_eq(pkind, EL_STR("LitBool"))) { + el_val_t v = el_get_field(pat, EL_STR("value")); + el_val_t bv = EL_STR("false"); + if (str_eq(v, EL_STR("true"))) { + bv = EL_STR("true"); + } + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, EL_STR("if (")), subj_var), EL_STR(" === ")), bv), EL_STR(") return (")), body_c), EL_STR("); ")); + } else { + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("return (")), body_c), EL_STR("); ")); + } + } + } + } + } + i = (i + 1); + } + out = el_str_concat(el_str_concat(el_str_concat(out, EL_STR("return null; })(")), subj_c), EL_STR(")")); + return out; + return 0; +} + +el_val_t js_list_contains(el_val_t lst, el_val_t s) { + el_val_t n = native_list_len(lst); + el_val_t i = 0; + while (i < n) { + el_val_t item = native_list_get(lst, i); + if (str_eq(item, s)) { + return 1; + } + i = (i + 1); + } + return 0; + return 0; +} + +el_val_t js_cg_stmt(el_val_t stmt, el_val_t indent, el_val_t declared) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("Let"))) { + el_val_t name = el_get_field(stmt, EL_STR("name")); + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_c = js_cg_expr(val); + el_val_t ltype = el_get_field(stmt, EL_STR("type")); + if (str_eq(ltype, EL_STR("Int"))) { + js_add_int_name(name); + } + el_val_t vk = el_get_field(val, EL_STR("expr")); + if (str_eq(vk, EL_STR("Int"))) { + js_add_int_name(name); + } + if (js_list_contains(declared, name)) { + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, name), EL_STR(" = ")), val_c), EL_STR(";"))); + return declared; + } else { + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("let ")), name), EL_STR(" = ")), val_c), EL_STR(";"))); + return native_list_append(declared, name); + } + } + if (str_eq(kind, EL_STR("Return"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("Nil"))) { + js_emit_line(el_str_concat(indent, EL_STR("return null;"))); + } else { + el_val_t val_c = js_cg_expr(val); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("return ")), val_c), EL_STR(";"))); + } + return declared; + } + if (str_eq(kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(stmt, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + js_cg_if_stmt(val, indent, declared); + return declared; + } + if (str_eq(val_kind, EL_STR("For"))) { + js_cg_for_stmt(val, indent, declared); + return declared; + } + el_val_t val_c = js_cg_expr(val); + js_emit_line(el_str_concat(el_str_concat(indent, val_c), EL_STR(";"))); + return declared; + } + if (str_eq(kind, EL_STR("While"))) { + el_val_t cond = el_get_field(stmt, EL_STR("cond")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t cond_c = js_cg_expr(cond); + cond_c = js_strip_outer_parens(cond_c); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("while (")), cond_c), EL_STR(") {"))); + js_cg_stmts(body, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + js_emit_line(el_str_concat(indent, EL_STR("}"))); + return declared; + } + if (str_eq(kind, EL_STR("For"))) { + el_val_t item = el_get_field(stmt, EL_STR("item")); + el_val_t list_expr = el_get_field(stmt, EL_STR("list")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + js_cg_for_body(item, list_expr, body, indent, declared); + return declared; + } + if (str_eq(kind, EL_STR("FnDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("TypeDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return declared; + } + if (str_eq(kind, EL_STR("Import"))) { + return declared; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + el_val_t cname = el_get_field(stmt, EL_STR("name")); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("// cgi block '")), cname), EL_STR("' — no-op in JS target (server-side concept)"))); + return declared; + } + if (str_eq(kind, EL_STR("ServiceBlock"))) { + el_val_t sname = el_get_field(stmt, EL_STR("name")); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("// service block '")), sname), EL_STR("' — no-op in JS target"))); + return declared; + } + return declared; + return 0; +} + +el_val_t js_strip_outer_parens(el_val_t s) { + el_val_t chars = native_string_chars(s); + el_val_t n = native_list_len(chars); + if (n < 2) { + return s; + } + el_val_t first = native_list_get(chars, 0); + el_val_t last = native_list_get(chars, (n - 1)); + if (str_eq(first, EL_STR("("))) { + if (str_eq(last, EL_STR(")"))) { + el_val_t depth = 1; + el_val_t i = 1; + el_val_t balanced = 1; + while (i < (n - 1)) { + el_val_t ch = native_list_get(chars, i); + if (str_eq(ch, EL_STR("("))) { + depth = (depth + 1); + } + if (str_eq(ch, EL_STR(")"))) { + depth = (depth - 1); + if (depth == 0) { + balanced = 0; + i = n; + } + } + i = (i + 1); + } + if (balanced) { + el_val_t inner = EL_STR(""); + el_val_t j = 1; + while (j < (n - 1)) { + el_val_t ch = native_list_get(chars, j); + inner = el_str_concat(inner, ch); + j = (j + 1); + } + return inner; + } + } + } + return s; + return 0; +} + +el_val_t js_cg_if_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t cond = el_get_field(expr, EL_STR("cond")); + el_val_t then_stmts = el_get_field(expr, EL_STR("then")); + el_val_t else_stmts = el_get_field(expr, EL_STR("else")); + el_val_t has_else = el_get_field(expr, EL_STR("has_else")); + el_val_t cond_c = js_cg_expr(cond); + cond_c = js_strip_outer_parens(cond_c); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("if (")), cond_c), EL_STR(") {"))); + js_cg_stmts(then_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + if (has_else) { + js_emit_line(el_str_concat(indent, EL_STR("} else {"))); + js_cg_stmts(else_stmts, el_str_concat(indent, EL_STR(" ")), native_list_clone(declared)); + } + js_emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t js_cg_for_body(el_val_t item, el_val_t list_expr, el_val_t body, el_val_t indent, el_val_t declared) { + el_val_t list_c = js_cg_expr(list_expr); + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(indent, EL_STR("for (const ")), item), EL_STR(" of ")), list_c), EL_STR(") {"))); + el_val_t body_decl = native_list_clone(declared); + body_decl = native_list_append(body_decl, item); + js_cg_stmts(body, el_str_concat(indent, EL_STR(" ")), body_decl); + js_emit_line(el_str_concat(indent, EL_STR("}"))); + return 0; +} + +el_val_t js_cg_for_stmt(el_val_t expr, el_val_t indent, el_val_t declared) { + el_val_t item = el_get_field(expr, EL_STR("item")); + el_val_t list_expr = el_get_field(expr, EL_STR("list")); + el_val_t body = el_get_field(expr, EL_STR("body")); + js_cg_for_body(item, list_expr, body, indent, declared); + return 0; +} + +el_val_t js_cg_stmts(el_val_t stmts, el_val_t indent, el_val_t declared) { + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + el_val_t decl = declared; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + decl = js_cg_stmt(stmt, indent, decl); + i = (i + 1); + } + return decl; + return 0; +} + +el_val_t js_params_str(el_val_t params) { + el_val_t n = native_list_len(params); + if (n == 0) { + return EL_STR(""); + } + el_val_t out = EL_STR(""); + el_val_t i = 0; + while (i < n) { + el_val_t param = native_list_get(params, i); + el_val_t name = el_get_field(param, EL_STR("name")); + if (i > 0) { + out = el_str_concat(out, EL_STR(", ")); + } + out = el_str_concat(out, name); + i = (i + 1); + } + return out; + return 0; +} + +el_val_t js_transform_implicit_return(el_val_t body) { + el_val_t n = native_list_len(body); + if (n == 0) { + return body; + } + el_val_t last = native_list_get(body, (n - 1)); + el_val_t last_kind = el_get_field(last, EL_STR("stmt")); + if (str_eq(last_kind, EL_STR("Expr"))) { + el_val_t val = el_get_field(last, EL_STR("value")); + el_val_t val_kind = el_get_field(val, EL_STR("expr")); + if (str_eq(val_kind, EL_STR("If"))) { + return body; + } + if (str_eq(val_kind, EL_STR("For"))) { + return body; + } + el_val_t new_body = native_list_empty(); + el_val_t i = 0; + while (i < (n - 1)) { + new_body = native_list_append(new_body, native_list_get(body, i)); + i = (i + 1); + } + el_val_t return_stmt = el_map_new(2, "stmt", EL_STR("Return"), "value", val); + new_body = native_list_append(new_body, return_stmt); + return new_body; + } + return body; + return 0; +} + +el_val_t js_cg_fn(el_val_t stmt) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + el_val_t params = el_get_field(stmt, EL_STR("params")); + el_val_t body = el_get_field(stmt, EL_STR("body")); + el_val_t ret_type = el_get_field(stmt, EL_STR("ret_type")); + el_val_t params_str = js_params_str(params); + js_build_int_names_for_params(params); + if (str_eq(fn_name, EL_STR("main"))) { + js_emit_line(el_str_concat(el_str_concat(EL_STR("function main("), params_str), EL_STR(") {"))); + } else { + js_emit_line(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("function "), fn_name), EL_STR("(")), params_str), EL_STR(") {"))); + } + el_val_t decl = native_list_empty(); + el_val_t np = native_list_len(params); + el_val_t pi = 0; + while (pi < np) { + el_val_t param = native_list_get(params, pi); + el_val_t pname = el_get_field(param, EL_STR("name")); + decl = native_list_append(decl, pname); + pi = (pi + 1); + } + el_val_t body_xformed = body; + if (!str_eq(ret_type, EL_STR("Void"))) { + body_xformed = js_transform_implicit_return(body); + } + js_cg_stmts(body_xformed, EL_STR(" "), decl); + js_emit_line(EL_STR("}")); + js_emit_blank(); + return 0; +} + +el_val_t js_is_fndef(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("FnDef"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t js_is_top_level_decl(el_val_t stmt) { + el_val_t kind = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(kind, EL_STR("TypeDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("EnumDef"))) { + return 1; + } + if (str_eq(kind, EL_STR("Import"))) { + return 1; + } + if (str_eq(kind, EL_STR("CgiBlock"))) { + return 1; + } + if (str_eq(kind, EL_STR("ServiceBlock"))) { + return 1; + } + return 0; + return 0; +} + +el_val_t codegen_js(el_val_t stmts, el_val_t source) { + state_set(EL_STR("__js_int_names"), EL_STR("")); + state_set(EL_STR("__js_match_counter"), EL_STR("")); + js_emit_line(EL_STR("// Generated by elc --target=js")); + js_emit_line(EL_STR("// Runtime: foundation/el/el-compiler/runtime/el_runtime.js")); + js_emit_line(EL_STR("import \"./el_runtime.js\";")); + js_emit_line(EL_STR("const {")); + js_emit_line(EL_STR(" println, print, el_str_concat, str_concat, str_eq, str_starts_with, str_ends_with,")); + js_emit_line(EL_STR(" str_len, int_to_str, str_to_int, str_slice, str_contains, str_replace,")); + js_emit_line(EL_STR(" str_to_upper, str_to_lower, str_trim, str_index_of, str_split, str_char_at,")); + js_emit_line(EL_STR(" str_char_code, str_lower, str_upper, el_abs, el_max, el_min,")); + js_emit_line(EL_STR(" el_list_new, el_list_len, el_list_get, el_list_append, el_list_empty, el_list_clone,")); + js_emit_line(EL_STR(" list_push, list_join, list_range,")); + js_emit_line(EL_STR(" el_map_new, el_get_field, el_map_get, el_map_set,")); + js_emit_line(EL_STR(" http_get, http_post, http_post_json,")); + js_emit_line(EL_STR(" fs_read, fs_write, fs_list,")); + js_emit_line(EL_STR(" json_parse, json_stringify, json_get, json_get_string, json_get_int,")); + js_emit_line(EL_STR(" time_now, time_now_utc, sleep_ms, bool_to_str, exit_program,")); + js_emit_line(EL_STR(" el_retain, el_release,")); + js_emit_line(EL_STR(" append, len, get, map_get, map_set,")); + js_emit_line(EL_STR(" native_list_get, native_list_len, native_list_append, native_list_empty,")); + js_emit_line(EL_STR(" native_list_clone, native_string_chars, native_int_to_str,")); + js_emit_line(EL_STR(" args, state_set, state_get, state_del, state_keys, env,")); + js_emit_line(EL_STR(" dharma_connect, dharma_send, dharma_emit, dharma_field, dharma_activate,")); + js_emit_line(EL_STR(" engram_node, engram_search, engram_activate,")); + js_emit_line(EL_STR(" llm_call, llm_call_system,")); + js_emit_line(EL_STR("} = globalThis.__el;")); + js_emit_blank(); + el_val_t n = native_list_len(stmts); + el_val_t i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (js_is_fndef(stmt)) { + js_cg_fn(stmt); + } + i = (i + 1); + } + el_val_t has_main = 0; + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + el_val_t sk = el_get_field(stmt, EL_STR("stmt")); + if (str_eq(sk, EL_STR("FnDef"))) { + el_val_t fn_name = el_get_field(stmt, EL_STR("name")); + if (str_eq(fn_name, EL_STR("main"))) { + has_main = 1; + } + } + i = (i + 1); + } + el_val_t main_decl = native_list_empty(); + i = 0; + while (i < n) { + el_val_t stmt = native_list_get(stmts, i); + if (js_is_fndef(stmt)) { + } else { + if (js_is_top_level_decl(stmt)) { + } else { + main_decl = js_cg_stmt(stmt, EL_STR(""), main_decl); + } + } + i = (i + 1); + } + if (has_main) { + js_emit_blank(); + js_emit_line(EL_STR("main();")); + } + return EL_STR(""); + return 0; +} + +el_val_t compile(el_val_t source) { + el_val_t tokens = lex(source); + el_val_t stmts = parse(tokens); + el_release(tokens); + return codegen(stmts, source); + return 0; +} + +el_val_t compile_js(el_val_t source) { + el_val_t tokens = lex(source); + el_val_t stmts = parse(tokens); + el_release(tokens); + return codegen_js(stmts, source); + return 0; +} + +el_val_t compile_dispatch(el_val_t tgt, el_val_t source) { + if (str_eq(tgt, EL_STR("js"))) { + return compile_js(source); + } + return compile(source); + return 0; +} + +el_val_t detect_target(el_val_t argv) { + el_val_t n = native_list_len(argv); + el_val_t i = 0; + while (i < n) { + el_val_t a = native_list_get(argv, i); + if (str_starts_with(a, EL_STR("--target="))) { + el_val_t v = str_slice(a, 9, str_len(a)); + return v; + } + i = (i + 1); + } + return EL_STR("c"); + return 0; +} + +el_val_t strip_flags(el_val_t argv) { + el_val_t out = native_list_empty(); + el_val_t n = native_list_len(argv); + el_val_t i = 0; + while (i < n) { + el_val_t a = native_list_get(argv, i); + if (!str_starts_with(a, EL_STR("--"))) { + out = native_list_append(out, a); + } + i = (i + 1); + } + return out; + return 0; +} + +int main(int argc, char** argv) { + el_runtime_init_args(argc, argv); + return 0; +} + diff --git a/dist/platform/elc-asan b/dist/platform/elc-asan new file mode 100755 index 0000000..8b4727a Binary files /dev/null and b/dist/platform/elc-asan differ diff --git a/dist/platform/elc-asan.dSYM/Contents/Info.plist b/dist/platform/elc-asan.dSYM/Contents/Info.plist new file mode 100644 index 0000000..509e0bc --- /dev/null +++ b/dist/platform/elc-asan.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.elc-asan + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/dist/platform/elc-asan.dSYM/Contents/Resources/DWARF/elc-asan b/dist/platform/elc-asan.dSYM/Contents/Resources/DWARF/elc-asan new file mode 100644 index 0000000..aac088b Binary files /dev/null and b/dist/platform/elc-asan.dSYM/Contents/Resources/DWARF/elc-asan differ diff --git a/dist/platform/elc-asan.dSYM/Contents/Resources/Relocations/aarch64/elc-asan.yml b/dist/platform/elc-asan.dSYM/Contents/Resources/Relocations/aarch64/elc-asan.yml new file mode 100644 index 0000000..b55aedf --- /dev/null +++ b/dist/platform/elc-asan.dSYM/Contents/Resources/Relocations/aarch64/elc-asan.yml @@ -0,0 +1,5 @@ +--- +triple: 'arm64-apple-darwin' +binary-path: '/Users/will/Development/neuron-technologies/foundation/el/dist/platform/elc-asan' +relocations: [] +... diff --git a/dist/platform/elc-new b/dist/platform/elc-new new file mode 100755 index 0000000..68ee155 Binary files /dev/null and b/dist/platform/elc-new differ diff --git a/dist/platform/elc.prelaunch.bak b/dist/platform/elc.prelaunch.bak new file mode 100755 index 0000000..9b68a40 Binary files /dev/null and b/dist/platform/elc.prelaunch.bak differ diff --git a/dist/platform/elc.prev7 b/dist/platform/elc.prev7 new file mode 100755 index 0000000..d666bc5 Binary files /dev/null and b/dist/platform/elc.prev7 differ diff --git a/dist/platform/elc.prev8 b/dist/platform/elc.prev8 new file mode 100755 index 0000000..68ee155 Binary files /dev/null and b/dist/platform/elc.prev8 differ diff --git a/dist/platform/elc.prev9 b/dist/platform/elc.prev9 new file mode 100755 index 0000000..68ee155 Binary files /dev/null and b/dist/platform/elc.prev9 differ diff --git a/el-compiler/runtime/el_runtime.c b/el-compiler/runtime/el_runtime.c index c54ddc6..8f8a602 100644 --- a/el-compiler/runtime/el_runtime.c +++ b/el-compiler/runtime/el_runtime.c @@ -3099,6 +3099,9 @@ el_val_t json_get_raw(el_val_t json_str, el_val_t key) { const char* json = EL_CSTR(json_str); const char* k = EL_CSTR(key); const char* p = json_find_key(json, k); + /* Clear fs_read binary-length hint — result is a fresh null-terminated + * string, not the raw file bytes, so Content-Length must use strlen. */ + _tl_fs_read_len = 0; if (!p) return el_wrap_str(el_strdup("")); const char* end = json_skip_value(p); size_t n = (size_t)(end - p); diff --git a/el-compiler/runtime/el_runtime.js b/el-compiler/runtime/el_runtime.js new file mode 100644 index 0000000..e5c7a79 --- /dev/null +++ b/el-compiler/runtime/el_runtime.js @@ -0,0 +1,679 @@ +/* + * el_runtime.js — El language JS runtime. + * + * The browser/Node analog of el_runtime.c. Compiled-from-El JS source + * imports this file once; it side-effects globalThis.__el with every + * builtin, so generated programs can destructure the names they need + * (see codegen-js.el's preamble). + * + * Value model: + * El's tagged el_val_t collapses into JS native types here: + * String -> string + * Int -> number (caveat: only 53 bits of integer precision) + * Float -> number (already a double) + * Bool -> boolean + * [T] -> Array + * Map<,> -> plain object + * Void -> undefined + * null -> null + * + * Runtime mode auto-detection: + * typeof window === 'undefined' -> Node mode + * otherwise -> Browser mode + * + * See spec/codegen-js.md for the full design rationale. + */ + +const IS_NODE = typeof window === 'undefined' && typeof process !== 'undefined' && process.versions != null && process.versions.node != null; + +// ── I/O ───────────────────────────────────────────────────────────────────── + +function println(s) { + if (IS_NODE) { + process.stdout.write(String(s) + '\n'); + } else { + console.log(String(s)); + } +} + +function print(s) { + if (IS_NODE) { + process.stdout.write(String(s)); + } else { + // Browser has no stdout — fall back to console with no newline group + console.log(String(s)); + } +} + +// ── String builtins ───────────────────────────────────────────────────────── + +// Coerce both args to string and concat. Mirrors el_str_concat in C; +// the C version handles both string-and-string and string-and-int. +function el_str_concat(a, b) { + return String(a) + String(b); +} + +function str_concat(a, b) { return el_str_concat(a, b); } + +// Strict equality with string coercion. Matches str_eq() in C — which +// strcmp's the underlying char*. Here we just === after coercion. +function str_eq(a, b) { + if (a === null || b === null) return a === b; + return String(a) === String(b); +} + +function str_starts_with(s, prefix) { + return String(s).startsWith(String(prefix)); +} + +function str_ends_with(s, suffix) { + return String(s).endsWith(String(suffix)); +} + +function str_len(s) { + return String(s).length; +} + +function int_to_str(n) { + return String(n); +} + +function str_to_int(s) { + const n = parseInt(String(s), 10); + return Number.isNaN(n) ? 0 : n; +} + +function str_slice(s, start, end) { + return String(s).slice(start, end); +} + +function str_contains(s, sub) { + return String(s).indexOf(String(sub)) >= 0; +} + +function str_replace(s, from, to) { + // Replace ALL occurrences (matches C runtime semantics). + return String(s).split(String(from)).join(String(to)); +} + +function str_to_upper(s) { return String(s).toUpperCase(); } +function str_to_lower(s) { return String(s).toLowerCase(); } +function str_upper(s) { return String(s).toUpperCase(); } +function str_lower(s) { return String(s).toLowerCase(); } + +function str_trim(s) { return String(s).trim(); } + +function str_index_of(s, sub) { + return String(s).indexOf(String(sub)); +} + +function str_split(s, sep) { + return String(s).split(String(sep)); +} + +function str_char_at(s, i) { + return String(s).charAt(i); +} + +function str_char_code(s, i) { + const c = String(s).charCodeAt(i); + return Number.isNaN(c) ? 0 : c; +} + +function str_pad_left(s, width, pad) { + return String(s).padStart(width, String(pad)); +} + +function str_pad_right(s, width, pad) { + return String(s).padEnd(width, String(pad)); +} + +// ── Math ──────────────────────────────────────────────────────────────────── + +function el_abs(n) { return Math.abs(n); } +function el_max(a, b) { return a > b ? a : b; } +function el_min(a, b) { return a < b ? a : b; } + +// ── Refcount (no-op — JS has GC) ──────────────────────────────────────────── + +function el_retain(_v) { /* no-op */ } +function el_release(_v) { /* no-op */ } + +// ── List ──────────────────────────────────────────────────────────────────── + +// Variadic constructor matching el_list_new(count, items...). Exposed so +// codegen-js can emit the same call shape if we ever want it (currently +// codegen-js emits JS array literals directly). +function el_list_new(_count, ...items) { + return items.slice(0); +} + +function el_list_empty() { return []; } +function el_list_clone(list) { return Array.isArray(list) ? list.slice() : []; } +function el_list_len(list) { return Array.isArray(list) ? list.length : 0; } + +function el_list_get(list, index) { + if (!Array.isArray(list)) return null; + if (index < 0 || index >= list.length) return null; + return list[index]; +} + +function el_list_append(list, elem) { + if (!Array.isArray(list)) return [elem]; + const out = list.slice(); + out.push(elem); + return out; +} + +function list_push(list, elem) { return el_list_append(list, elem); } + +function list_push_front(list, elem) { + if (!Array.isArray(list)) return [elem]; + return [elem, ...list]; +} + +function list_join(list, sep) { + if (!Array.isArray(list)) return ''; + return list.map(String).join(String(sep)); +} + +function list_range(start, end) { + const out = []; + for (let i = start; i < end; i++) out.push(i); + return out; +} + +// ── Map ───────────────────────────────────────────────────────────────────── + +// Variadic constructor (key, val, key, val, ...). +function el_map_new(_pairCount, ...kvs) { + const out = {}; + for (let i = 0; i < kvs.length; i += 2) { + out[String(kvs[i])] = kvs[i + 1]; + } + return out; +} + +function el_get_field(map, key) { + if (map === null || map === undefined) return null; + if (typeof map !== 'object') return null; + const k = String(key); + if (Object.prototype.hasOwnProperty.call(map, k)) return map[k]; + return null; +} + +function el_map_get(map, key) { return el_get_field(map, key); } + +function el_map_set(map, key, value) { + // Match the C runtime: shallow-copy + set, persistent semantics. + const out = (map && typeof map === 'object') ? { ...map } : {}; + out[String(key)] = value; + return out; +} + +// ── Method-call shorthand aliases ────────────────────────────────────────── +// `obj.method(args)` compiles to `method(obj, args)` per El convention. + +function append(list, elem) { return el_list_append(list, elem); } +function len(v) { + if (Array.isArray(v)) return v.length; + if (typeof v === 'string') return v.length; + if (v && typeof v === 'object') return Object.keys(v).length; + return 0; +} +function get(list, index) { return el_list_get(list, index); } +function map_get(m, k) { return el_get_field(m, k); } +function map_set(m, k, v) { return el_map_set(m, k, v); } + +// ── Native VM aliases ────────────────────────────────────────────────────── + +function native_list_get(list, index) { return el_list_get(list, index); } +function native_list_len(list) { return el_list_len(list); } +function native_list_append(list, elem) { return el_list_append(list, elem); } +function native_list_empty() { return []; } +function native_list_clone(list) { return el_list_clone(list); } +function native_string_chars(s) { return String(s).split(''); } +function native_int_to_str(n) { return String(n); } + +// ── HTTP ─────────────────────────────────────────────────────────────────── +// +// fetch() is async. These return Promise. Generated El code does +// not yet emit await — that's the async-taint pass (see spec §5). For +// programs that don't touch HTTP this is fine; for programs that do, +// the value will appear as "[object Promise]" until the taint pass lands. + +function http_get(url) { + if (typeof fetch === 'undefined') { + throw new Error('http_get: fetch() not available in this runtime'); + } + return fetch(String(url)).then(r => r.text()); +} + +function http_post(url, body) { + if (typeof fetch === 'undefined') { + throw new Error('http_post: fetch() not available in this runtime'); + } + return fetch(String(url), { method: 'POST', body: String(body) }).then(r => r.text()); +} + +function http_post_json(url, jsonBody) { + if (typeof fetch === 'undefined') { + throw new Error('http_post_json: fetch() not available in this runtime'); + } + return fetch(String(url), { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: String(jsonBody), + }).then(r => r.text()); +} + +function http_get_with_headers(url, headersMap) { + if (typeof fetch === 'undefined') { + throw new Error('http_get_with_headers: fetch() not available'); + } + return fetch(String(url), { headers: headersMap || {} }).then(r => r.text()); +} + +function http_post_with_headers(url, body, headersMap) { + if (typeof fetch === 'undefined') { + throw new Error('http_post_with_headers: fetch() not available'); + } + return fetch(String(url), { + method: 'POST', + headers: headersMap || {}, + body: String(body), + }).then(r => r.text()); +} + +function http_serve(_port, _handler) { + throw new Error('http_serve: not supported in JS target — needs server-side runtime mode'); +} + +function http_set_handler(_name) { + throw new Error('http_set_handler: not supported in JS target'); +} + +// ── Filesystem (Node-only) ───────────────────────────────────────────────── + +function _ensureNode(name) { + if (!IS_NODE) { + throw new Error(`${name}: not supported in browser runtime`); + } +} + +function fs_read(path) { + _ensureNode('fs_read'); + const fs = require('node:fs'); + try { + return fs.readFileSync(String(path), 'utf8'); + } catch (_e) { + return ''; + } +} + +function fs_write(path, content) { + _ensureNode('fs_write'); + const fs = require('node:fs'); + try { + fs.writeFileSync(String(path), String(content)); + return true; + } catch (_e) { + return false; + } +} + +function fs_list(path) { + _ensureNode('fs_list'); + const fs = require('node:fs'); + try { + return fs.readdirSync(String(path)); + } catch (_e) { + return []; + } +} + +// ── JSON ─────────────────────────────────────────────────────────────────── + +function json_parse(s) { + try { return JSON.parse(String(s)); } + catch (_e) { return null; } +} + +function json_stringify(v) { + try { return JSON.stringify(v); } + catch (_e) { return ''; } +} + +function json_get(jsonStr, key) { + const o = json_parse(jsonStr); + if (o === null) return null; + return el_get_field(o, key); +} + +function json_get_string(jsonStr, key) { + const v = json_get(jsonStr, key); + return v === null ? '' : String(v); +} + +function json_get_int(jsonStr, key) { + const v = json_get(jsonStr, key); + if (typeof v === 'number') return Math.trunc(v); + if (typeof v === 'string') return str_to_int(v); + return 0; +} + +function json_get_float(jsonStr, key) { + const v = json_get(jsonStr, key); + return typeof v === 'number' ? v : 0; +} + +function json_get_bool(jsonStr, key) { + const v = json_get(jsonStr, key); + return v === true; +} + +function json_get_raw(jsonStr, key) { + const v = json_get(jsonStr, key); + return v === null ? '' : json_stringify(v); +} + +function json_set(jsonStr, key, value) { + const o = json_parse(jsonStr) ?? {}; + o[String(key)] = value; + return json_stringify(o); +} + +function json_array_len(jsonStr) { + const o = json_parse(jsonStr); + return Array.isArray(o) ? o.length : 0; +} + +// ── Time ─────────────────────────────────────────────────────────────────── + +function time_now() { + return Math.floor(Date.now() / 1000); +} + +function time_now_utc() { + // In the C runtime this returns nanoseconds since epoch. JS number + // can't represent that range past ~2^53. We return milliseconds — a + // safe range — and document the divergence. + return Date.now(); +} + +function sleep_secs(secs) { + if (!IS_NODE) { + throw new Error('sleep_secs: blocking sleep not supported in browser'); + } + // Simple sync sleep via Atomics.wait on a SharedArrayBuffer-backed Int32. + const sab = new SharedArrayBuffer(4); + const i32 = new Int32Array(sab); + Atomics.wait(i32, 0, 0, Math.floor(secs * 1000)); + return secs; +} + +function sleep_ms(ms) { + if (!IS_NODE) { + throw new Error('sleep_ms: blocking sleep not supported in browser'); + } + const sab = new SharedArrayBuffer(4); + const i32 = new Int32Array(sab); + Atomics.wait(i32, 0, 0, Math.floor(ms)); + return ms; +} + +// ── Bool ─────────────────────────────────────────────────────────────────── + +function bool_to_str(b) { return b ? 'true' : 'false'; } + +// ── Process ──────────────────────────────────────────────────────────────── + +function exit_program(code) { + if (IS_NODE) { + process.exit(code | 0); + } else { + throw new Error(`exit_program(${code}) called in browser`); + } +} + +// ── args() ───────────────────────────────────────────────────────────────── + +function args() { + if (IS_NODE) { + // process.argv is [node, script, ...args] — slice off node + script. + return process.argv.slice(2); + } + return []; +} + +// ── env ──────────────────────────────────────────────────────────────────── + +function env(key) { + if (IS_NODE) { + const v = process.env[String(key)]; + return v === undefined ? null : v; + } + return null; +} + +// ── In-process state K/V ─────────────────────────────────────────────────── + +const _stateMap = new Map(); + +function state_set(key, value) { + _stateMap.set(String(key), value); + return value; +} + +function state_get(key) { + const v = _stateMap.get(String(key)); + return v === undefined ? '' : v; +} + +function state_del(key) { + return _stateMap.delete(String(key)); +} + +function state_keys() { + return Array.from(_stateMap.keys()); +} + +// ── UUID ─────────────────────────────────────────────────────────────────── + +function uuid_v4() { + // RFC 4122-ish — uses crypto when available, falls back to Math.random. + if (typeof crypto !== 'undefined' && crypto.randomUUID) { + return crypto.randomUUID(); + } + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { + const r = Math.random() * 16 | 0; + const v = c === 'x' ? r : (r & 0x3 | 0x8); + return v.toString(16); + }); +} +function uuid_new() { return uuid_v4(); } + +// ── Float formatting ─────────────────────────────────────────────────────── + +function float_to_str(f) { return String(f); } +function int_to_float(n) { return n; } +function float_to_int(f) { return Math.trunc(f); } + +function format_float(f, decimals) { + return Number(f).toFixed(decimals); +} + +function decimal_round(f, decimals) { + const m = Math.pow(10, decimals); + return Math.round(f * m) / m; +} + +function str_to_float(s) { + const n = parseFloat(String(s)); + return Number.isNaN(n) ? 0 : n; +} + +// ── Math (Float-aware) ───────────────────────────────────────────────────── + +function math_sqrt(f) { return Math.sqrt(f); } +function math_log(f) { return Math.log10(f); } +function math_ln(f) { return Math.log(f); } +function math_sin(f) { return Math.sin(f); } +function math_cos(f) { return Math.cos(f); } +function math_pi() { return Math.PI; } + +// ── Stubs for not-yet-supported features ─────────────────────────────────── +// +// These compile but throw when called. See spec/codegen-js.md §7. + +function _notSupported(name) { + return () => { throw new Error(`${name}: not supported in JS target — needs server-side delegation`); }; +} + +// CGI identity +function el_cgi_init(_name, _did, _principal, _network, _engram) { + // No-op — UI code is not a CGI principal. See spec §7. +} + +// DHARMA — all stubbed. +const dharma_connect = _notSupported('dharma_connect'); +const dharma_send = _notSupported('dharma_send'); +const dharma_activate = _notSupported('dharma_activate'); +const dharma_emit = _notSupported('dharma_emit'); +const dharma_field = _notSupported('dharma_field'); +const dharma_strengthen = _notSupported('dharma_strengthen'); +const dharma_relationship = _notSupported('dharma_relationship'); +const dharma_peers = _notSupported('dharma_peers'); + +// Engram — stubbed (could be ported to in-browser later). +const engram_node = _notSupported('engram_node'); +const engram_node_full = _notSupported('engram_node_full'); +const engram_get_node = _notSupported('engram_get_node'); +const engram_strengthen = _notSupported('engram_strengthen'); +const engram_forget = _notSupported('engram_forget'); +const engram_node_count = _notSupported('engram_node_count'); +const engram_search = _notSupported('engram_search'); +const engram_scan_nodes = _notSupported('engram_scan_nodes'); +const engram_connect = _notSupported('engram_connect'); +const engram_edge_between = _notSupported('engram_edge_between'); +const engram_neighbors = _notSupported('engram_neighbors'); +const engram_neighbors_filtered = _notSupported('engram_neighbors_filtered'); +const engram_edge_count = _notSupported('engram_edge_count'); +const engram_activate = _notSupported('engram_activate'); +const engram_save = _notSupported('engram_save'); +const engram_load = _notSupported('engram_load'); + +// LLM — stubbed (browser cannot hold API keys safely). +const llm_call = _notSupported('llm_call'); +const llm_call_system = _notSupported('llm_call_system'); +const llm_call_agentic = _notSupported('llm_call_agentic'); +const llm_vision = _notSupported('llm_vision'); +const llm_models = _notSupported('llm_models'); +const llm_register_tool = _notSupported('llm_register_tool'); + +// Crypto — stubbed; could be backed by SubtleCrypto later. +const sha256_hex = _notSupported('sha256_hex'); +const sha256_bytes = _notSupported('sha256_bytes'); +const hmac_sha256_hex = _notSupported('hmac_sha256_hex'); +const hmac_sha256_bytes = _notSupported('hmac_sha256_bytes'); +const base64_encode = _notSupported('base64_encode'); +const base64_decode = _notSupported('base64_decode'); +const base64url_encode = _notSupported('base64url_encode'); +const base64url_decode = _notSupported('base64url_decode'); + +// ── Export to globalThis.__el ────────────────────────────────────────────── +// +// Generated programs destructure off this object. Keeping it on globalThis +// means a single `import "./el_runtime.js"` is enough; no per-call namespace +// prefix is required at codegen time. + +const __el = { + // I/O + println, print, + // String + el_str_concat, str_concat, str_eq, str_starts_with, str_ends_with, + str_len, int_to_str, str_to_int, str_slice, str_contains, str_replace, + str_to_upper, str_to_lower, str_trim, str_index_of, str_split, str_char_at, + str_char_code, str_lower, str_upper, str_pad_left, str_pad_right, + // Math + el_abs, el_max, el_min, + // Refcount + el_retain, el_release, + // List + el_list_new, el_list_empty, el_list_clone, el_list_len, el_list_get, + el_list_append, list_push, list_push_front, list_join, list_range, + // Map + el_map_new, el_get_field, el_map_get, el_map_set, + // Method-call shortforms + append, len, get, map_get, map_set, + // Native VM aliases + native_list_get, native_list_len, native_list_append, native_list_empty, + native_list_clone, native_string_chars, native_int_to_str, + // HTTP + http_get, http_post, http_post_json, http_get_with_headers, + http_post_with_headers, http_serve, http_set_handler, + // FS + fs_read, fs_write, fs_list, + // JSON + json_parse, json_stringify, json_get, json_get_string, json_get_int, + json_get_float, json_get_bool, json_get_raw, json_set, json_array_len, + // Time + time_now, time_now_utc, sleep_secs, sleep_ms, + // Bool + bool_to_str, + // Process + exit_program, + // Args / env + args, env, + // State + state_set, state_get, state_del, state_keys, + // UUID + uuid_v4, uuid_new, + // Float / math + float_to_str, int_to_float, float_to_int, format_float, decimal_round, + str_to_float, math_sqrt, math_log, math_ln, math_sin, math_cos, math_pi, + // CGI / DHARMA / Engram / LLM (stubs) + el_cgi_init, + dharma_connect, dharma_send, dharma_activate, dharma_emit, dharma_field, + dharma_strengthen, dharma_relationship, dharma_peers, + engram_node, engram_node_full, engram_get_node, engram_strengthen, + engram_forget, engram_node_count, engram_search, engram_scan_nodes, + engram_connect, engram_edge_between, engram_neighbors, + engram_neighbors_filtered, engram_edge_count, engram_activate, + engram_save, engram_load, + llm_call, llm_call_system, llm_call_agentic, llm_vision, + llm_models, llm_register_tool, + // Crypto (stubs) + sha256_hex, sha256_bytes, hmac_sha256_hex, hmac_sha256_bytes, + base64_encode, base64_decode, base64url_encode, base64url_decode, +}; + +globalThis.__el = __el; + +// Also re-export as ES module exports for consumers that prefer that style. +export { __el as default }; +export { + println, print, + el_str_concat, str_concat, str_eq, str_starts_with, str_ends_with, + str_len, int_to_str, str_to_int, str_slice, str_contains, str_replace, + str_to_upper, str_to_lower, str_trim, str_index_of, str_split, str_char_at, + str_char_code, str_lower, str_upper, + el_abs, el_max, el_min, + el_retain, el_release, + el_list_new, el_list_empty, el_list_clone, el_list_len, el_list_get, + el_list_append, list_push, list_push_front, list_join, list_range, + el_map_new, el_get_field, el_map_get, el_map_set, + append, len, get, map_get, map_set, + native_list_get, native_list_len, native_list_append, native_list_empty, + native_list_clone, native_string_chars, native_int_to_str, + http_get, http_post, http_post_json, + fs_read, fs_write, fs_list, + json_parse, json_stringify, json_get, json_get_string, json_get_int, + time_now, time_now_utc, sleep_ms, + bool_to_str, exit_program, args, env, + state_set, state_get, state_del, state_keys, + el_cgi_init, + dharma_connect, dharma_send, dharma_activate, dharma_emit, dharma_field, + engram_node, engram_search, engram_activate, + llm_call, llm_call_system, +}; diff --git a/el-compiler/src/codegen-js.el b/el-compiler/src/codegen-js.el new file mode 100644 index 0000000..62b4edb --- /dev/null +++ b/el-compiler/src/codegen-js.el @@ -0,0 +1,943 @@ +// codegen-js.el — El compiler JavaScript source code generator +// +// Input: list of AST statement maps (from parser.el) +// Output: JavaScript source printed to stdout (streamed, one line at a time) +// +// Each El program compiles to a single .js file that imports el_runtime.js +// (which side-effects globals so call sites stay flat — println(x), not +// el.println(x)). Functions map to JS function declarations; top-level +// statements run at module load. +// +// Entry point: fn codegen_js(stmts: [Map], source: String) -> String +// Returns "" — output goes to stdout via println(). +// +// This file mirrors codegen.el (the C backend). Where the C backend has to +// fight the int64_t-everywhere convention to dispatch arithmetic vs concat +// or `==` vs `str_eq`, the JS backend can usually let JS's own operator +// semantics do the right thing. We retain the dispatch logic for clarity +// and so that explicit calls to `el_str_concat` or `str_eq` still work. + +// ── String helpers ──────────────────────────────────────────────────────────── + +// Escape a JS string literal (double-quotes, backslashes, newlines, etc.). +fn js_escape(s: String) -> String { + let chars: [String] = native_string_chars(s) + let total: Int = native_list_len(chars) + let out = "" + let i = 0 + while i < total { + let ch: String = native_list_get(chars, i) + if ch == "\"" { + let out = out + "\\\"" + } else { + if ch == "\\" { + let out = out + "\\\\" + } else { + if ch == "\n" { + let out = out + "\\n" + } else { + if ch == "\r" { + let out = out + "\\r" + } else { + if ch == "\t" { + let out = out + "\\t" + } else { + let out = out + ch + } + } + } + } + } + let i = i + 1 + } + out +} + +fn js_str_lit(s: String) -> String { + "\"" + js_escape(s) + "\"" +} + +// ── Code emission ───────────────────────────────────────────────────────────── + +fn js_emit_line(line: String) -> Void { + println(line) +} + +fn js_emit_blank() -> Void { + println("") +} + +// ── Operator helpers ────────────────────────────────────────────────────────── + +fn js_binop(op: String) -> String { + if op == "Plus" { return "+" } + if op == "Minus" { return "-" } + if op == "Star" { return "*" } + if op == "Slash" { return "/" } + if op == "Percent" { return "%" } + if op == "EqEq" { return "===" } + if op == "NotEq" { return "!==" } + if op == "Lt" { return "<" } + if op == "Gt" { return ">" } + if op == "LtEq" { return "<=" } + if op == "GtEq" { return ">=" } + if op == "And" { return "&&" } + if op == "Or" { return "||" } + op +} + +// ── Int-name tracking (mirrors codegen.el) ──────────────────────────────────── + +fn js_is_int_name(name: String) -> Bool { + let csv: String = state_get("__js_int_names") + if str_eq(csv, "") { return false } + return str_contains(csv, "," + name + ",") +} + +fn js_add_int_name(name: String) -> Bool { + let csv: String = state_get("__js_int_names") + if str_eq(csv, "") { csv = "," } + let key: String = "," + name + "," + if str_contains(csv, key) { return true } + state_set("__js_int_names", csv + name + ",") + return true +} + +fn js_build_int_names_for_params(params: [Map]) -> Bool { + state_set("__js_int_names", ",") + let np: Int = native_list_len(params) + let pi = 0 + while pi < np { + let param = native_list_get(params, pi) + let pname: String = param["name"] + let ptype: String = param["type"] + if str_eq(ptype, "Int") { + js_add_int_name(pname) + } + let pi = pi + 1 + } + return true +} + +fn js_is_int_call(call_expr: Map) -> Bool { + let func = call_expr["func"] + let fk: String = func["expr"] + if !str_eq(fk, "Ident") { return false } + let name: String = func["name"] + if str_eq(name, "str_len") { return true } + if str_eq(name, "str_index_of") { return true } + if str_eq(name, "str_to_int") { return true } + if str_eq(name, "str_char_code") { return true } + if str_eq(name, "native_list_len") { return true } + if str_eq(name, "el_list_len") { return true } + if str_eq(name, "len") { return true } + if str_eq(name, "json_get_int") { return true } + if str_eq(name, "time_now") { return true } + if str_eq(name, "time_now_utc") { return true } + if str_eq(name, "el_abs") { return true } + if str_eq(name, "el_max") { return true } + if str_eq(name, "el_min") { return true } + return false +} + +// ── Expression codegen ──────────────────────────────────────────────────────── +// +// js_cg_expr returns a JS expression string (not a statement). +// +// Note: the C backend's `+` dispatch is preserved here for two reasons: +// 1) Generated output stays grep-equivalent across targets +// 2) Explicit `el_str_concat()` lives in the runtime; codegen routes +// through it for ambiguous (Ident+Ident, Call+Call) cases. JS's +// own `+` would also work, but el_str_concat coerces both sides +// to strings — closer to the C semantics. + +fn js_cg_expr(expr: Map) -> String { + let kind: String = expr["expr"] + + if kind == "Int" { + let v: String = expr["value"] + return v + } + + // DurationLit — postfix-literal time value (e.g. 30.seconds, 1.hour). + // The JS backend lowers to a literal integer nanosecond count. The C + // backend uses the typed wrapper el_duration_from_nanos to make intent + // explicit at the runtime boundary; JS has no equivalent shim yet, so + // we lower directly. A future Phase 2 JS time runtime can route through + // a wrapper once added. + if kind == "DurationLit" { + let count: String = expr["count"] + let unit: String = expr["unit"] + let mult_ns = "1" + if str_eq(unit, "nano") { let mult_ns = "1" } + if str_eq(unit, "nanos") { let mult_ns = "1" } + if str_eq(unit, "milli") { let mult_ns = "1000000" } + if str_eq(unit, "millis") { let mult_ns = "1000000" } + if str_eq(unit, "millisecond") { let mult_ns = "1000000" } + if str_eq(unit, "milliseconds") { let mult_ns = "1000000" } + if str_eq(unit, "second") { let mult_ns = "1000000000" } + if str_eq(unit, "seconds") { let mult_ns = "1000000000" } + if str_eq(unit, "minute") { let mult_ns = "60000000000" } + if str_eq(unit, "minutes") { let mult_ns = "60000000000" } + if str_eq(unit, "hour") { let mult_ns = "3600000000000" } + if str_eq(unit, "hours") { let mult_ns = "3600000000000" } + if str_eq(unit, "day") { let mult_ns = "86400000000000" } + if str_eq(unit, "days") { let mult_ns = "86400000000000" } + return "(" + count + " * " + mult_ns + ")" + } + + if kind == "Float" { + // JS numbers are already doubles — no bit-cast trick needed. + let v: String = expr["value"] + return v + } + + if kind == "Str" { + let v: String = expr["value"] + return js_str_lit(v) + } + + if kind == "Bool" { + let v: String = expr["value"] + if v == "true" { return "true" } + return "false" + } + + if kind == "Nil" { + return "null" + } + + if kind == "Ident" { + let name: String = expr["name"] + return name + } + + if kind == "Not" { + let inner = expr["inner"] + let inner_c: String = js_cg_expr(inner) + return "!" + inner_c + } + + if kind == "Neg" { + let inner = expr["inner"] + let inner_c: String = js_cg_expr(inner) + return "(-" + inner_c + ")" + } + + if kind == "BinOp" { + let op: String = expr["op"] + let left = expr["left"] + let right = expr["right"] + let left_c: String = js_cg_expr(left) + let right_c: String = js_cg_expr(right) + let left_kind: String = left["expr"] + let right_kind: String = right["expr"] + + // Plus dispatch — same shape as C backend, but we route through + // el_str_concat for the string-concat path (its JS impl coerces + // and matches C's behavior). Arithmetic uses bare JS `+`. + if op == "Plus" { + if left_kind == "Str" { + return "el_str_concat(" + left_c + ", " + right_c + ")" + } + if right_kind == "Str" { + return "el_str_concat(" + left_c + ", " + right_c + ")" + } + if left_kind == "Int" { + return "(" + left_c + " + " + right_c + ")" + } + if right_kind == "Int" { + return "(" + left_c + " + " + right_c + ")" + } + if left_kind == "Ident" { + if right_kind == "Ident" { + let lname: String = left["name"] + let rname: String = right["name"] + if js_is_int_name(lname) { + if js_is_int_name(rname) { + return "(" + left_c + " + " + right_c + ")" + } + } + } + } + if left_kind == "Ident" { + if right_kind == "Call" { + let lname: String = left["name"] + if js_is_int_name(lname) { + if js_is_int_call(right) { + return "(" + left_c + " + " + right_c + ")" + } + } + } + } + if right_kind == "Ident" { + if left_kind == "Call" { + let rname: String = right["name"] + if js_is_int_name(rname) { + if js_is_int_call(left) { + return "(" + left_c + " + " + right_c + ")" + } + } + } + } + if left_kind == "Call" { + if right_kind == "Call" { + if js_is_int_call(left) { + if js_is_int_call(right) { + return "(" + left_c + " + " + right_c + ")" + } + } + } + return "el_str_concat(" + left_c + ", " + right_c + ")" + } + if right_kind == "Call" { + return "el_str_concat(" + left_c + ", " + right_c + ")" + } + // Fallback: when in doubt, route through el_str_concat. JS's + // own + handles strings and numbers natively, but el_str_concat + // gives us a single point of control if behavior needs to diverge. + if left_kind == "Ident" { + return "el_str_concat(" + left_c + ", " + right_c + ")" + } + if right_kind == "Ident" { + return "el_str_concat(" + left_c + ", " + right_c + ")" + } + } + + // Equality dispatch — C backend disambiguates via str_eq for + // strings and == for ints. JS does both with === if we know + // the types are uniform; for ambiguous identifier pairs we + // route through str_eq for safety (it falls back to === in JS). + if op == "EqEq" { + if left_kind == "Int" { return "(" + left_c + " === " + right_c + ")" } + if right_kind == "Int" { return "(" + left_c + " === " + right_c + ")" } + if left_kind == "Bool" { return "(" + left_c + " === " + right_c + ")" } + if right_kind == "Bool" { return "(" + left_c + " === " + right_c + ")" } + if left_kind == "Nil" { return "(" + left_c + " === " + right_c + ")" } + if right_kind == "Nil" { return "(" + left_c + " === " + right_c + ")" } + if left_kind == "Ident" { + if right_kind == "Ident" { + let lname: String = left["name"] + let rname: String = right["name"] + if js_is_int_name(lname) { + if js_is_int_name(rname) { + return "(" + left_c + " === " + right_c + ")" + } + } + } + } + if left_kind == "Str" { return "str_eq(" + left_c + ", " + right_c + ")" } + if right_kind == "Str" { return "str_eq(" + left_c + ", " + right_c + ")" } + // Default: === (works for strings, numbers, bools in JS) + return "(" + left_c + " === " + right_c + ")" + } + + if op == "NotEq" { + if left_kind == "Int" { return "(" + left_c + " !== " + right_c + ")" } + if right_kind == "Int" { return "(" + left_c + " !== " + right_c + ")" } + if left_kind == "Bool" { return "(" + left_c + " !== " + right_c + ")" } + if right_kind == "Bool" { return "(" + left_c + " !== " + right_c + ")" } + if left_kind == "Nil" { return "(" + left_c + " !== " + right_c + ")" } + if right_kind == "Nil" { return "(" + left_c + " !== " + right_c + ")" } + if left_kind == "Ident" { + if right_kind == "Ident" { + let lname: String = left["name"] + let rname: String = right["name"] + if js_is_int_name(lname) { + if js_is_int_name(rname) { + return "(" + left_c + " !== " + right_c + ")" + } + } + } + } + if left_kind == "Str" { return "!str_eq(" + left_c + ", " + right_c + ")" } + if right_kind == "Str" { return "!str_eq(" + left_c + ", " + right_c + ")" } + return "(" + left_c + " !== " + right_c + ")" + } + + let op_c: String = js_binop(op) + return "(" + left_c + " " + op_c + " " + right_c + ")" + } + + if kind == "Call" { + let func = expr["func"] + let args = expr["args"] + let arity: Int = native_list_len(args) + let func_kind: String = func["expr"] + + let args_c = "" + let i = 0 + while i < arity { + let arg = native_list_get(args, i) + let arg_c: String = js_cg_expr(arg) + if i > 0 { + let args_c = args_c + ", " + } + let args_c = args_c + arg_c + let i = i + 1 + } + + if func_kind == "Ident" { + let fn_name: String = func["name"] + return fn_name + "(" + args_c + ")" + } + + if func_kind == "Field" { + // El's `obj.method(args)` becomes `method(obj, args)` — same + // convention as the C backend. The runtime exports method + // shortforms (append, len, get, map_get, map_set) that match. + let obj = func["object"] + let field: String = func["field"] + let obj_c: String = js_cg_expr(obj) + if arity > 0 { + return field + "(" + obj_c + ", " + args_c + ")" + } + return field + "(" + obj_c + ")" + } + + let fn_c: String = js_cg_expr(func) + return fn_c + "(" + args_c + ")" + } + + if kind == "Field" { + // El's `obj.foo` becomes JS `obj["foo"]` — works on plain objects + // (maps) and on JS objects with prototype. el_get_field is a + // runtime helper for callers that want EL_NULL on missing keys. + let obj = expr["object"] + let field: String = expr["field"] + let obj_c: String = js_cg_expr(obj) + return "el_get_field(" + obj_c + ", " + js_str_lit(field) + ")" + } + + if kind == "Index" { + // Map vs list dispatch on the index expression kind, same as C. + let obj = expr["object"] + let idx = expr["index"] + let obj_c: String = js_cg_expr(obj) + let idx_c: String = js_cg_expr(idx) + let idx_kind: String = idx["expr"] + if str_eq(idx_kind, "Str") { + return "el_get_field(" + obj_c + ", " + idx_c + ")" + } + return "el_list_get(" + obj_c + ", " + idx_c + ")" + } + + if kind == "Array" { + let elems = expr["elems"] + let n: Int = native_list_len(elems) + if n == 0 { return "[]" } + let items = "" + let i = 0 + while i < n { + let elem = native_list_get(elems, i) + let elem_c: String = js_cg_expr(elem) + if i > 0 { + let items = items + ", " + } + let items = items + elem_c + let i = i + 1 + } + return "[" + items + "]" + } + + if kind == "Map" { + let pairs = expr["pairs"] + let n: Int = native_list_len(pairs) + if n == 0 { return "{}" } + let items = "" + let i = 0 + while i < n { + let pair = native_list_get(pairs, i) + let key: String = pair["key"] + let val = pair["value"] + let val_c: String = js_cg_expr(val) + if i > 0 { + let items = items + ", " + } + let items = items + js_str_lit(key) + ": " + val_c + let i = i + 1 + } + return "{" + items + "}" + } + + if kind == "Try" { + let inner = expr["inner"] + return js_cg_expr(inner) + } + + if kind == "If" { + let cond = expr["cond"] + let cond_c: String = js_cg_expr(cond) + // If as expression: ternary. Body of the if-expression is not + // currently emitted as expression-form for compound bodies; this + // matches the C backend's if-expr stub. + return "(" + cond_c + " ? 1 : 0)" + } + + if kind == "Match" { + return js_cg_match(expr) + } + + "null" +} + +// ── Match codegen (basic) ───────────────────────────────────────────────────── +// +// Lower a match expression to an IIFE with if/else chain. Works for +// LitInt / LitStr / LitBool / Wildcard / Binding patterns. Tagged-union +// destructuring is not implemented — it's stubbed and falls through to +// the wildcard path. + +fn js_next_match_id() -> String { + let csv: String = state_get("__js_match_counter") + let n = 0 + if !str_eq(csv, "") { + let n = str_to_int(csv) + } + let n = n + 1 + state_set("__js_match_counter", native_int_to_str(n)) + native_int_to_str(n) +} + +fn js_cg_match(expr: Map) -> String { + let subject = expr["subject"] + let arms = expr["arms"] + let subj_c: String = js_cg_expr(subject) + let id: String = js_next_match_id() + let subj_var: String = "_match_subj_" + id + let out: String = "((" + subj_var + ") => { " + let n: Int = native_list_len(arms) + let i = 0 + while i < n { + let arm = native_list_get(arms, i) + let pat = arm["pattern"] + let body = arm["body"] + let pkind: String = pat["pattern"] + let body_c: String = js_cg_expr(body) + if str_eq(pkind, "Wildcard") { + let out = out + "return (" + body_c + "); " + } else { + if str_eq(pkind, "Binding") { + let bname: String = pat["name"] + let out = out + "{ const " + bname + " = " + subj_var + "; return (" + body_c + "); } " + } else { + if str_eq(pkind, "LitInt") { + let v: String = pat["value"] + let out = out + "if (" + subj_var + " === " + v + ") return (" + body_c + "); " + } else { + if str_eq(pkind, "LitStr") { + let v: String = pat["value"] + let out = out + "if (str_eq(" + subj_var + ", " + js_str_lit(v) + ")) return (" + body_c + "); " + } else { + if str_eq(pkind, "LitBool") { + let v: String = pat["value"] + let bv = "false" + if str_eq(v, "true") { let bv = "true" } + let out = out + "if (" + subj_var + " === " + bv + ") return (" + body_c + "); " + } else { + // unknown pattern → wildcard + let out = out + "return (" + body_c + "); " + } + } + } + } + } + let i = i + 1 + } + let out = out + "return null; })(" + subj_c + ")" + out +} + +// ── Variable scope tracking ─────────────────────────────────────────────────── +// +// El allows `let x = ...` to redeclare in the same scope. JS would throw +// with `let` (Identifier already declared). We track declared names and +// emit bare `x = ...` on redeclaration, `let x = ...` first time. + +fn js_list_contains(lst: [String], s: String) -> Bool { + let n: Int = native_list_len(lst) + let i = 0 + while i < n { + let item: String = native_list_get(lst, i) + if item == s { return true } + let i = i + 1 + } + false +} + +// ── Statement codegen ───────────────────────────────────────────────────────── + +fn js_cg_stmt(stmt: Map, indent: String, declared: [String]) -> [String] { + let kind: String = stmt["stmt"] + + if kind == "Let" { + let name: String = stmt["name"] + let val = stmt["value"] + let val_c: String = js_cg_expr(val) + let ltype: String = stmt["type"] + if str_eq(ltype, "Int") { + js_add_int_name(name) + } + let vk: String = val["expr"] + if str_eq(vk, "Int") { + js_add_int_name(name) + } + if js_list_contains(declared, name) { + js_emit_line(indent + name + " = " + val_c + ";") + return declared + } else { + // Use `let` (not `const`) — El semantics allow rebinding. + js_emit_line(indent + "let " + name + " = " + val_c + ";") + return native_list_append(declared, name) + } + } + + if kind == "Return" { + let val = stmt["value"] + let val_kind: String = val["expr"] + if val_kind == "Nil" { + js_emit_line(indent + "return null;") + } else { + let val_c: String = js_cg_expr(val) + js_emit_line(indent + "return " + val_c + ";") + } + return declared + } + + // Bare reassignment: `name = expr`. Mirrors the C backend — emits a + // plain JS assignment without `let` so we don't shadow an outer binding. + if kind == "Assign" { + let name: String = stmt["name"] + let val = stmt["value"] + let val_c: String = js_cg_expr(val) + js_emit_line(indent + name + " = " + val_c + ";") + return declared + } + + if kind == "Expr" { + let val = stmt["value"] + let val_kind: String = val["expr"] + if val_kind == "If" { + js_cg_if_stmt(val, indent, declared) + return declared + } + if val_kind == "For" { + js_cg_for_stmt(val, indent, declared) + return declared + } + let val_c: String = js_cg_expr(val) + js_emit_line(indent + val_c + ";") + return declared + } + + if kind == "While" { + let cond = stmt["cond"] + let body = stmt["body"] + let cond_c: String = js_cg_expr(cond) + let cond_c = js_strip_outer_parens(cond_c) + js_emit_line(indent + "while (" + cond_c + ") {") + js_cg_stmts(body, indent + " ", native_list_clone(declared)) + js_emit_line(indent + "}") + return declared + } + + if kind == "For" { + let item: String = stmt["item"] + let list_expr = stmt["list"] + let body = stmt["body"] + js_cg_for_body(item, list_expr, body, indent, declared) + return declared + } + + if kind == "FnDef" { return declared } + if kind == "TypeDef" { return declared } + if kind == "EnumDef" { return declared } + if kind == "Import" { return declared } + if kind == "CgiBlock" { + // CGI blocks compile to a no-op + warning comment in JS target. + // The runtime cgi identity is server-side; UI code is not a CGI + // principal. See spec/codegen-js.md §7. + let cname: String = stmt["name"] + js_emit_line(indent + "// cgi block '" + cname + "' — no-op in JS target (server-side concept)") + return declared + } + if kind == "ServiceBlock" { + let sname: String = stmt["name"] + js_emit_line(indent + "// service block '" + sname + "' — no-op in JS target") + return declared + } + declared +} + +// Strip a single layer of surrounding parentheses from a JS expression string. +fn js_strip_outer_parens(s: String) -> String { + let chars: [String] = native_string_chars(s) + let n: Int = native_list_len(chars) + if n < 2 { return s } + let first: String = native_list_get(chars, 0) + let last: String = native_list_get(chars, n - 1) + if first == "(" { + if last == ")" { + let depth = 1 + let i = 1 + let balanced = true + while i < n - 1 { + let ch: String = native_list_get(chars, i) + if ch == "(" { + let depth = depth + 1 + } + if ch == ")" { + let depth = depth - 1 + if depth == 0 { + let balanced = false + let i = n + } + } + let i = i + 1 + } + if balanced { + let inner = "" + let j = 1 + while j < n - 1 { + let ch: String = native_list_get(chars, j) + let inner = inner + ch + let j = j + 1 + } + return inner + } + } + } + s +} + +fn js_cg_if_stmt(expr: Map, indent: String, declared: [String]) -> Void { + let cond = expr["cond"] + let then_stmts = expr["then"] + let else_stmts = expr["else"] + let has_else: Bool = expr["has_else"] + let cond_c: String = js_cg_expr(cond) + let cond_c = js_strip_outer_parens(cond_c) + js_emit_line(indent + "if (" + cond_c + ") {") + js_cg_stmts(then_stmts, indent + " ", native_list_clone(declared)) + if has_else { + js_emit_line(indent + "} else {") + js_cg_stmts(else_stmts, indent + " ", native_list_clone(declared)) + } + js_emit_line(indent + "}") +} + +fn js_cg_for_body(item: String, list_expr: Map, body: [Map], indent: String, declared: [String]) -> Void { + let list_c: String = js_cg_expr(list_expr) + js_emit_line(indent + "for (const " + item + " of " + list_c + ") {") + let body_decl = native_list_clone(declared) + let body_decl = native_list_append(body_decl, item) + js_cg_stmts(body, indent + " ", body_decl) + js_emit_line(indent + "}") +} + +fn js_cg_for_stmt(expr: Map, indent: String, declared: [String]) -> Void { + let item: String = expr["item"] + let list_expr = expr["list"] + let body = expr["body"] + js_cg_for_body(item, list_expr, body, indent, declared) +} + +fn js_cg_stmts(stmts: [Map], indent: String, declared: [String]) -> [String] { + let n: Int = native_list_len(stmts) + let i = 0 + let decl = declared + while i < n { + let stmt = native_list_get(stmts, i) + let decl = js_cg_stmt(stmt, indent, decl) + let i = i + 1 + } + decl +} + +// ── Function declaration codegen ────────────────────────────────────────────── + +fn js_params_str(params: [Map]) -> String { + let n: Int = native_list_len(params) + if n == 0 { return "" } + let out = "" + let i = 0 + while i < n { + let param = native_list_get(params, i) + let name: String = param["name"] + if i > 0 { + let out = out + ", " + } + let out = out + name + let i = i + 1 + } + out +} + +// Same implicit-return transform as the C backend. +fn js_transform_implicit_return(body: [Map]) -> [Map] { + let n: Int = native_list_len(body) + if n == 0 { return body } + let last: Map = native_list_get(body, n - 1) + let last_kind: String = last["stmt"] + if last_kind == "Expr" { + let val = last["value"] + let val_kind: String = val["expr"] + if val_kind == "If" { return body } + if val_kind == "For" { return body } + let new_body: [Map] = native_list_empty() + let i = 0 + while i < n - 1 { + let new_body = native_list_append(new_body, native_list_get(body, i)) + let i = i + 1 + } + let return_stmt: Map = { "stmt": "Return", "value": val } + let new_body = native_list_append(new_body, return_stmt) + return new_body + } + body +} + +fn js_cg_fn(stmt: Map) -> Void { + let fn_name: String = stmt["name"] + let params = stmt["params"] + let body = stmt["body"] + let ret_type: String = stmt["ret_type"] + let params_str: String = js_params_str(params) + js_build_int_names_for_params(params) + + // Special-case `fn main` — emit as a regular function and call it + // at module bottom (after all top-level statements). This matches + // the C backend's behavior where `fn main` is the entry point. + if fn_name == "main" { + js_emit_line("function main(" + params_str + ") {") + } else { + js_emit_line("function " + fn_name + "(" + params_str + ") {") + } + + let decl = native_list_empty() + let np: Int = native_list_len(params) + let pi = 0 + while pi < np { + let param = native_list_get(params, pi) + let pname: String = param["name"] + let decl = native_list_append(decl, pname) + let pi = pi + 1 + } + let body_xformed = body + if !str_eq(ret_type, "Void") { + let body_xformed = js_transform_implicit_return(body) + } + js_cg_stmts(body_xformed, " ", decl) + js_emit_line("}") + js_emit_blank() +} + +// ── Top-level codegen ───────────────────────────────────────────────────────── + +fn js_is_fndef(stmt: Map) -> Bool { + let kind: String = stmt["stmt"] + if kind == "FnDef" { return true } + false +} + +fn js_is_top_level_decl(stmt: Map) -> Bool { + let kind: String = stmt["stmt"] + if kind == "TypeDef" { return true } + if kind == "EnumDef" { return true } + if kind == "Import" { return true } + if kind == "CgiBlock" { return true } + if kind == "ServiceBlock" { return true } + false +} + +// ── Entry point ─────────────────────────────────────────────────────────────── + +fn codegen_js(stmts: [Map], source: String) -> String { + // Reset per-compile state. + state_set("__js_int_names", "") + state_set("__js_match_counter", "") + + // Preamble: inline the runtime via a single import that side-effects + // globalThis. The runtime path is resolved relative to the generated + // output; users running `elc --target=js` are responsible for ensuring + // el_runtime.js is reachable. For self-contained output, the runtime + // could be inlined; that is a follow-up. + js_emit_line("// Generated by elc --target=js") + js_emit_line("// Runtime: foundation/el/el-compiler/runtime/el_runtime.js") + js_emit_line("import \"./el_runtime.js\";") + js_emit_line("const {") + js_emit_line(" println, print, el_str_concat, str_concat, str_eq, str_starts_with, str_ends_with,") + js_emit_line(" str_len, int_to_str, str_to_int, str_slice, str_contains, str_replace,") + js_emit_line(" str_to_upper, str_to_lower, str_trim, str_index_of, str_split, str_char_at,") + js_emit_line(" str_char_code, str_lower, str_upper, el_abs, el_max, el_min,") + js_emit_line(" el_list_new, el_list_len, el_list_get, el_list_append, el_list_empty, el_list_clone,") + js_emit_line(" list_push, list_join, list_range,") + js_emit_line(" el_map_new, el_get_field, el_map_get, el_map_set,") + js_emit_line(" http_get, http_post, http_post_json,") + js_emit_line(" fs_read, fs_write, fs_list,") + js_emit_line(" json_parse, json_stringify, json_get, json_get_string, json_get_int,") + js_emit_line(" time_now, time_now_utc, sleep_ms, bool_to_str, exit_program,") + js_emit_line(" el_retain, el_release,") + js_emit_line(" append, len, get, map_get, map_set,") + js_emit_line(" native_list_get, native_list_len, native_list_append, native_list_empty,") + js_emit_line(" native_list_clone, native_string_chars, native_int_to_str,") + js_emit_line(" args, state_set, state_get, state_del, state_keys, env,") + js_emit_line(" dharma_connect, dharma_send, dharma_emit, dharma_field, dharma_activate,") + js_emit_line(" engram_node, engram_search, engram_activate,") + js_emit_line(" llm_call, llm_call_system,") + js_emit_line("} = globalThis.__el;") + js_emit_blank() + + // Function definitions + let n: Int = native_list_len(stmts) + let i = 0 + while i < n { + let stmt = native_list_get(stmts, i) + if js_is_fndef(stmt) { + js_cg_fn(stmt) + } + let i = i + 1 + } + + // Top-level statements (those that are not FnDef and not declarative) + // run at module load. If the program defines `fn main`, we additionally + // call main() at the end so the C-backend mental model of "fn main is + // the entry point" carries over. + let has_main = false + let i = 0 + while i < n { + let stmt = native_list_get(stmts, i) + let sk: String = stmt["stmt"] + if str_eq(sk, "FnDef") { + let fn_name: String = stmt["name"] + if str_eq(fn_name, "main") { + let has_main = true + } + } + let i = i + 1 + } + + let main_decl = native_list_empty() + let i = 0 + while i < n { + let stmt = native_list_get(stmts, i) + if js_is_fndef(stmt) { + // skip + } else { + if js_is_top_level_decl(stmt) { + // skip + } else { + let main_decl = js_cg_stmt(stmt, "", main_decl) + } + } + let i = i + 1 + } + + if has_main { + js_emit_blank() + js_emit_line("main();") + } + + // Return empty string — output was streamed via println + "" +} diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..9604ec3 --- /dev/null +++ b/install.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash +# install.sh — Install the El SDK from the latest Gitea release. +# +# Usage: +# bash install.sh +# EL_VERSION=v1.0.0 bash install.sh # pin a specific release tag +# EL_PREFIX=/opt/el bash install.sh # custom install prefix +# +# Environment variables: +# EL_VERSION Release tag to download (default: latest) +# EL_PREFIX Install prefix (default: /usr/local) + +set -euo pipefail + +REPO_BASE="https://git.neuralplatform.ai/neuron-technologies/el" +VERSION="${EL_VERSION:-latest}" +PREFIX="${EL_PREFIX:-/usr/local}" + +BIN_DIR="${PREFIX}/bin" +LIB_DIR="${PREFIX}/lib/el" + +RELEASE_BASE="${REPO_BASE}/releases/download/${VERSION}" + +echo "==> Installing El SDK ${VERSION}" +echo " prefix : ${PREFIX}" +echo " bin : ${BIN_DIR}" +echo " lib : ${LIB_DIR}" +echo + +# Create directories +mkdir -p "${BIN_DIR}" "${LIB_DIR}" + +# Download helper +download() { + local url="$1" + local dest="$2" + echo " Downloading $(basename "${dest}")..." + if command -v curl >/dev/null 2>&1; then + curl -fsSL "${url}" -o "${dest}" + elif command -v wget >/dev/null 2>&1; then + wget -q "${url}" -O "${dest}" + else + echo "Error: neither curl nor wget found" >&2 + exit 1 + fi +} + +# Download assets +TMP_DIR="$(mktemp -d)" +trap 'rm -rf "${TMP_DIR}"' EXIT + +download "${RELEASE_BASE}/elc" "${TMP_DIR}/elc" +download "${RELEASE_BASE}/el_runtime.c" "${TMP_DIR}/el_runtime.c" +download "${RELEASE_BASE}/el_runtime.h" "${TMP_DIR}/el_runtime.h" + +# Install +install -m 755 "${TMP_DIR}/elc" "${BIN_DIR}/elc" +install -m 644 "${TMP_DIR}/el_runtime.c" "${LIB_DIR}/el_runtime.c" +install -m 644 "${TMP_DIR}/el_runtime.h" "${LIB_DIR}/el_runtime.h" + +echo +echo "==> El SDK installed successfully" +echo +echo " elc binary : ${BIN_DIR}/elc" +echo " runtime : ${LIB_DIR}/el_runtime.c" +echo " header : ${LIB_DIR}/el_runtime.h" +echo +echo "Add the following to your Makefile to build El programs:" +echo +echo " EL_LIB := ${LIB_DIR}" +echo " ELC := elc" +echo " CC := cc" +echo " CFLAGS := -std=c11 -O2 -I\$(EL_LIB)" +echo +echo " dist/myapp.c: src/myapp.el" +echo " \t\$(ELC) src/myapp.el > dist/myapp.c" +echo +echo " dist/myapp: dist/myapp.c" +echo " \t\$(CC) \$(CFLAGS) -o dist/myapp dist/myapp.c \$(EL_LIB)/el_runtime.c -lcurl -lpthread" +echo diff --git a/releases/v1.0.0-20260501/RELEASE.md b/releases/v1.0.0-20260501/RELEASE.md new file mode 100644 index 0000000..280af6e --- /dev/null +++ b/releases/v1.0.0-20260501/RELEASE.md @@ -0,0 +1,28 @@ +# El Compiler Release v1.0.0 — 2026-05-02 + +## Components +- `bootstrap.py` — El language compiler (Python, recursive descent parser, emits C) +- `el_runtime.c` — El runtime (C, HTTP server, engram, DHARMA, LLM chain) +- `el_runtime.h` — Runtime public API header + +## Changes in this release + +### Critical bug fixes +- `state_set`/`state_get` are now thread-safe (pthread_mutex). Was racing across 64 worker threads. +- `looks_like_string` threshold raised from 1,000,000 to 4GB. Unix timestamps were being dereferenced as heap pointers. +- `fs_read` guards against negative `ftell` result (pipe/special file overflow). + +### Engram architecture (major) +- Two-layer activation: `background_activation` (Layer 1, broad fan-out) + `working_memory_weight` (Layer 2, executive filter) +- Inhibitory edges: `EngramEdge.inhibitory` flag suppresses working memory promotion without affecting background activation +- Suppression memory: `suppression_count` — nodes activated-but-suppressed accumulate pressure toward breakthrough +- Temporal decay: `temporal_decay_rate`, `created_at`, `last_activated_at`, `activation_count` on EngramNode +- Per-type activation thresholds (Safety: 0.05, Canonical: 0.15, Lesson: 0.25, Note: 0.40) +- Temporal range query: `engram_query_range(start_ms, end_ms)` +- Layered consciousness: `EngramLayer` struct, `layer_id` on nodes and edges, `EngramStore.layers[]` +- Layer 0 override pass: safety layer fires last and cannot be suppressed + +## SHA256 +bootstrap.py +el_runtime.c +el_runtime.h diff --git a/releases/v1.0.0-20260501/el_runtime.c b/releases/v1.0.0-20260501/el_runtime.c new file mode 100644 index 0000000..ab09b89 --- /dev/null +++ b/releases/v1.0.0-20260501/el_runtime.c @@ -0,0 +1,6693 @@ +/* + * el_runtime.c — El language C runtime implementation + * + * All functions use el_val_t (= int64_t) as the universal value type. + * Strings are transported as their pointer address cast to int64_t. + * On any 64-bit system sizeof(pointer) <= sizeof(int64_t), so this is safe. + * + * Compile with: + * cc -std=c11 -I -lcurl -lpthread -o .c el_runtime.c + * + * Link requirements: -lcurl (HTTP client + LLM), -lpthread (HTTP server). + */ + +/* Feature-test macros must be set before any standard headers. _GNU_SOURCE + * exposes clock_gettime/CLOCK_REALTIME, strcasecmp, and the dlfcn extensions + * (RTLD_DEFAULT) — all of which macOS hands us without asking but glibc on + * Debian gates behind an explicit opt-in. */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include "el_runtime.h" + +#include +#include /* strcasecmp */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* dlsym for http_set_handler fallback */ +#include +#include +#include +#include +#include + +/* ── Internal allocators ─────────────────────────────────────────────────── */ + +/* + * Per-request string arena + * + * Every El string allocated via el_strbuf / el_strdup during an HTTP request + * is registered in a thread-local arena. When el_request_end() is called at + * the end of the worker thread, every arena entry is freed — recovering all + * the intermediate strings from el_str_concat chains (build_system_prompt, + * engram_compile, etc.) that are otherwise leaked forever. + * + * Long-lived allocations (state_set values, engram internal storage) call + * el_strdup_persist() / el_strbuf_persist() which bypass the arena entirely. + */ + +#define EL_ARENA_INITIAL 512 + +typedef struct { + char** ptrs; + size_t count; + size_t cap; +} ElArena; + +static _Thread_local ElArena _tl_arena = {NULL, 0, 0}; +static _Thread_local int _tl_arena_active = 0; + +/* Binary-safe fs_read length — set by fs_read, consumed by http_send_response. + * Allows serving PNGs and other binary files without strlen truncation. */ +static _Thread_local size_t _tl_fs_read_len = 0; + +static void el_arena_track(char* p) { + if (!_tl_arena_active || !p) return; + if (_tl_arena.count >= _tl_arena.cap) { + size_t nc = _tl_arena.cap == 0 ? EL_ARENA_INITIAL : _tl_arena.cap * 2; + char** grown = realloc(_tl_arena.ptrs, nc * sizeof(char*)); + if (!grown) return; /* can't track — will leak this one ptr, but don't crash */ + _tl_arena.ptrs = grown; + _tl_arena.cap = nc; + } + _tl_arena.ptrs[_tl_arena.count++] = p; +} + +/* Called by http_worker before dispatching the El handler. */ +void el_request_start(void) { + _tl_arena.count = 0; + _tl_arena_active = 1; +} + +/* Called by http_worker after the El handler returns and the response is sent. + * Frees every intermediate string allocated during the request. */ +void el_request_end(void) { + _tl_arena_active = 0; + for (size_t i = 0; i < _tl_arena.count; i++) { + free(_tl_arena.ptrs[i]); + } + _tl_arena.count = 0; +} + +/* Persistent allocation — bypasses the arena (state_set, engram internals). */ +static char* el_strdup_persist(const char* s) { + if (!s) return strdup(""); + return strdup(s); +} +static char* el_strbuf_persist(size_t n) { + char* p = malloc(n + 1); + if (!p) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + p[0] = '\0'; + return p; +} + +static char* el_strdup(const char* s) { + if (!s) { char* p = strdup(""); el_arena_track(p); return p; } + char* p = strdup(s); + el_arena_track(p); + return p; +} + +static char* el_strbuf(size_t n) { + char* p = malloc(n + 1); + if (!p) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + p[0] = '\0'; + el_arena_track(p); + return p; +} + +/* Wrap an allocated C string as el_val_t */ +static el_val_t el_wrap_str(char* s) { + return EL_STR(s); +} + +/* ── I/O ──────────────────────────────────────────────────────────────────── */ + +void println(el_val_t s) { + const char* str = EL_CSTR(s); + if (str) puts(str); + else puts(""); +} + +void print(el_val_t s) { + const char* str = EL_CSTR(s); + if (str) fputs(str, stdout); +} + +el_val_t readline(void) { + char buf[4096]; + if (!fgets(buf, sizeof(buf), stdin)) return el_wrap_str(el_strdup("")); + size_t len = strlen(buf); + if (len > 0 && buf[len - 1] == '\n') buf[len - 1] = '\0'; + return el_wrap_str(el_strdup(buf)); +} + +/* ── String builtins ─────────────────────────────────────────────────────── */ + +el_val_t el_str_concat(el_val_t av, el_val_t bv) { + const char* a = EL_CSTR(av); + const char* b = EL_CSTR(bv); + if (!a) a = ""; + if (!b) b = ""; + size_t la = strlen(a); + size_t lb = strlen(b); + char* out = el_strbuf(la + lb); + memcpy(out, a, la); + memcpy(out + la, b, lb); + out[la + lb] = '\0'; + return el_wrap_str(out); +} + +el_val_t str_eq(el_val_t av, el_val_t bv) { + const char* a = EL_CSTR(av); + const char* b = EL_CSTR(bv); + if (!a || !b) return (el_val_t)(a == b); + return (el_val_t)(strcmp(a, b) == 0); +} + +el_val_t str_starts_with(el_val_t sv, el_val_t prefv) { + const char* s = EL_CSTR(sv); + const char* prefix = EL_CSTR(prefv); + if (!s || !prefix) return 0; + size_t lp = strlen(prefix); + return (el_val_t)(strncmp(s, prefix, lp) == 0); +} + +el_val_t str_ends_with(el_val_t sv, el_val_t sufv) { + const char* s = EL_CSTR(sv); + const char* suffix = EL_CSTR(sufv); + if (!s || !suffix) return 0; + size_t ls = strlen(s); + size_t lsuf = strlen(suffix); + if (lsuf > ls) return 0; + return (el_val_t)(strcmp(s + ls - lsuf, suffix) == 0); +} + +el_val_t str_len(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return 0; + return (el_val_t)strlen(s); +} + +el_val_t str_concat(el_val_t a, el_val_t b) { + return el_str_concat(a, b); +} + +el_val_t int_to_str(el_val_t n) { + char buf[32]; + snprintf(buf, sizeof(buf), "%lld", (long long)n); + return el_wrap_str(el_strdup(buf)); +} + +el_val_t str_to_int(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return 0; + return (el_val_t)atoll(s); +} + +el_val_t str_slice(el_val_t sv, el_val_t start, el_val_t end) { + const char* s = EL_CSTR(sv); + if (!s) return el_wrap_str(el_strdup("")); + int64_t len = (int64_t)strlen(s); + if (start < 0) start = 0; + if (end > len) end = len; + if (start >= end) return el_wrap_str(el_strdup("")); + int64_t sz = end - start; + char* out = el_strbuf((size_t)sz); + memcpy(out, s + start, (size_t)sz); + out[sz] = '\0'; + return el_wrap_str(out); +} + +el_val_t str_contains(el_val_t sv, el_val_t subv) { + const char* s = EL_CSTR(sv); + const char* sub = EL_CSTR(subv); + if (!s || !sub) return 0; + return (el_val_t)(strstr(s, sub) != NULL); +} + +el_val_t str_replace(el_val_t sv, el_val_t fromv, el_val_t tov) { + const char* s = EL_CSTR(sv); + const char* from = EL_CSTR(fromv); + const char* to = EL_CSTR(tov); + if (!s || !from || !to) return el_wrap_str(el_strdup(s ? s : "")); + size_t ls = strlen(s); + size_t lf = strlen(from); + size_t lt = strlen(to); + if (lf == 0) return el_wrap_str(el_strdup(s)); + size_t count = 0; + const char* p = s; + while ((p = strstr(p, from)) != NULL) { count++; p += lf; } + size_t out_sz = ls + count * lt + 1; + char* out = el_strbuf(out_sz); + char* dst = out; + p = s; + const char* found; + while ((found = strstr(p, from)) != NULL) { + size_t chunk = (size_t)(found - p); + memcpy(dst, p, chunk); dst += chunk; + memcpy(dst, to, lt); dst += lt; + p = found + lf; + } + strcpy(dst, p); + return el_wrap_str(out); +} + +el_val_t str_to_upper(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return el_wrap_str(el_strdup("")); + size_t n = strlen(s); + char* out = el_strbuf(n); + for (size_t i = 0; i < n; i++) out[i] = (char)toupper((unsigned char)s[i]); + out[n] = '\0'; + return el_wrap_str(out); +} + +el_val_t str_to_lower(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return el_wrap_str(el_strdup("")); + size_t n = strlen(s); + char* out = el_strbuf(n); + for (size_t i = 0; i < n; i++) out[i] = (char)tolower((unsigned char)s[i]); + out[n] = '\0'; + return el_wrap_str(out); +} + +el_val_t str_trim(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return el_wrap_str(el_strdup("")); + while (*s && isspace((unsigned char)*s)) s++; + size_t n = strlen(s); + while (n > 0 && isspace((unsigned char)s[n - 1])) n--; + char* out = el_strbuf(n); + memcpy(out, s, n); + out[n] = '\0'; + return el_wrap_str(out); +} + +/* ── Math ────────────────────────────────────────────────────────────────── */ + +el_val_t el_abs(el_val_t n) { return n < 0 ? -n : n; } +el_val_t el_max(el_val_t a, el_val_t b) { return a > b ? a : b; } +el_val_t el_min(el_val_t a, el_val_t b) { return a < b ? a : b; } + +/* ── Refcounted heap objects ────────────────────────────────────────────────── + * + * ElList and ElMap carry a magic-tagged header at offset 0: + * { uint32_t magic; uint32_t refcount; ... payload ... } + * + * The magic tag distinguishes refcounted objects from raw C strings (whose + * first byte is printable ASCII < 0x80) and from small integers (which can't + * be dereferenced). el_retain / el_release sniff the magic and act only on + * matching values; everything else is a safe no-op. + * + * Both ElList and ElMap use INDIRECTION: the header is fixed-size and never + * moves. The payload arrays (elems, keys, values) live in separate heap + * allocations, so realloc-grow on append never invalidates the caller's + * pointer to the header. This is what lets us mutate-in-place safely when + * the refcount is 1 and copy-on-write when it's higher. + * + * Memory model in practice: + * Single-owner accumulator (the cg_stmts pattern) — refcount stays at 1, + * appends amortize to O(1), total memory O(N) for an N-element list. + * Multi-owner branching (the cg_if_stmt pattern) — refcount > 1, each + * append on a shared list copies, so the original is preserved for the + * else-branch. Persistent semantics where they're needed; mutation where + * they're not. */ + +#define EL_MAGIC_LIST 0xE15710A1u /* >= 0x80 in MSB so 'looks_like_string' rejects */ +#define EL_MAGIC_MAP 0xE19A704Bu + +typedef struct { + uint32_t magic; + uint32_t refcount; +} ElHeader; + +/* ── List ────────────────────────────────────────────────────────────────── */ + +typedef struct { + ElHeader hdr; + int64_t length; + int64_t capacity; + el_val_t* elems; +} ElList; + +static ElList* list_alloc(int64_t cap) { + if (cap < 4) cap = 4; + ElList* lst = malloc(sizeof(ElList)); + if (!lst) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + lst->hdr.magic = EL_MAGIC_LIST; + lst->hdr.refcount = 1; + lst->length = 0; + lst->capacity = cap; + lst->elems = malloc((size_t)cap * sizeof(el_val_t)); + if (!lst->elems) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + return lst; +} + +el_val_t el_list_empty(void) { + return EL_STR(list_alloc(4)); +} + +el_val_t el_list_new(el_val_t count, ...) { + ElList* lst = list_alloc(count > 0 ? count : 4); + va_list ap; + va_start(ap, count); + for (int64_t i = 0; i < count; i++) { + lst->elems[i] = va_arg(ap, el_val_t); + } + va_end(ap); + lst->length = count; + return EL_STR(lst); +} + +el_val_t el_list_len(el_val_t listv) { + ElList* lst = (ElList*)(uintptr_t)listv; + if (!lst) return 0; + return lst->length; +} + +el_val_t el_list_get(el_val_t listv, el_val_t index) { + ElList* lst = (ElList*)(uintptr_t)listv; + if (!lst) return 0; + if (index < 0 || index >= lst->length) return 0; + return lst->elems[index]; +} + +el_val_t el_list_append(el_val_t listv, el_val_t elem) { + ElList* old = (ElList*)(uintptr_t)listv; + if (!old) { + ElList* fresh = list_alloc(4); + fresh->elems[0] = elem; + fresh->length = 1; + return EL_STR(fresh); + } + + /* Uniquely owned: grow the elems buffer in place. The header pointer the + * caller holds doesn't move (we only realloc the inner array). This is + * the common case in compiler accumulators, and it's amortized O(1). */ + if (old->hdr.refcount <= 1) { + if (old->length >= old->capacity) { + int64_t new_cap = old->capacity > 0 ? old->capacity * 2 : 4; + el_val_t* grown = realloc(old->elems, (size_t)new_cap * sizeof(el_val_t)); + if (!grown) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + old->elems = grown; + old->capacity = new_cap; + } + old->elems[old->length++] = elem; + return listv; + } + + /* Shared: copy-on-write. The original is preserved for its other owners. */ + int64_t new_cap = old->length + 1; + if (new_cap < 4) new_cap = 4; + ElList* fresh = malloc(sizeof(ElList)); + if (!fresh) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + fresh->hdr.magic = EL_MAGIC_LIST; + fresh->hdr.refcount = 1; + fresh->length = old->length + 1; + fresh->capacity = new_cap; + fresh->elems = malloc((size_t)new_cap * sizeof(el_val_t)); + if (!fresh->elems) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + if (old->length > 0) { + memcpy(fresh->elems, old->elems, (size_t)old->length * sizeof(el_val_t)); + } + fresh->elems[old->length] = elem; + return EL_STR(fresh); +} + +el_val_t el_list_clone(el_val_t listv) { + /* Shallow copy: the new ElList owns its own header and elems buffer, but + * the elements themselves are shared (which is what callers want for the + * cg_if_stmt 'declared' pattern — cloning the spine, not its contents). + * Used by codegen at scope branch points where two child scopes need to + * see the same starting set of declared names without each other's + * mutations. */ + ElList* old = (ElList*)(uintptr_t)listv; + if (!old) return el_list_empty(); + int64_t cap = old->capacity > 0 ? old->capacity : 4; + if (cap < old->length) cap = old->length; + if (cap < 4) cap = 4; + ElList* fresh = malloc(sizeof(ElList)); + if (!fresh) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + fresh->hdr.magic = EL_MAGIC_LIST; + fresh->hdr.refcount = 1; + fresh->length = old->length; + fresh->capacity = cap; + fresh->elems = malloc((size_t)cap * sizeof(el_val_t)); + if (!fresh->elems) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + if (old->length > 0) { + memcpy(fresh->elems, old->elems, (size_t)old->length * sizeof(el_val_t)); + } + return EL_STR(fresh); +} + +/* ── Map ─────────────────────────────────────────────────────────────────── */ + +typedef struct { + ElHeader hdr; + int64_t count; + int64_t capacity; + el_val_t* keys; + el_val_t* values; +} ElMap; + +static ElMap* map_alloc(int64_t cap) { + if (cap < 4) cap = 4; + ElMap* m = malloc(sizeof(ElMap)); + if (!m) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + m->hdr.magic = EL_MAGIC_MAP; + m->hdr.refcount = 1; + m->count = 0; + m->capacity = cap; + m->keys = malloc((size_t)cap * sizeof(el_val_t)); + m->values = malloc((size_t)cap * sizeof(el_val_t)); + if (!m->keys || !m->values) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + return m; +} + +el_val_t el_map_new(el_val_t pair_count, ...) { + ElMap* m = map_alloc(pair_count > 0 ? pair_count : 4); + va_list ap; + va_start(ap, pair_count); + for (int64_t i = 0; i < pair_count; i++) { + m->keys[i] = va_arg(ap, el_val_t); + m->values[i] = va_arg(ap, el_val_t); + } + va_end(ap); + m->count = pair_count; + return EL_STR(m); +} + +static ElMap* as_map(el_val_t v) { return (ElMap*)(uintptr_t)v; } + +el_val_t el_map_get(el_val_t mapv, el_val_t keyv) { + ElMap* m = as_map(mapv); + const char* key = EL_CSTR(keyv); + if (!m || !key) return 0; + for (int64_t i = 0; i < m->count; i++) { + const char* k = EL_CSTR(m->keys[i]); + if (k && strcmp(k, key) == 0) return m->values[i]; + } + return 0; +} + +el_val_t el_get_field(el_val_t mapv, el_val_t keyv) { + return el_map_get(mapv, keyv); +} + +/* Internal: in-place set on a uniquely-owned map. */ +static el_val_t map_set_in_place(ElMap* m, el_val_t keyv, el_val_t value) { + const char* key = EL_CSTR(keyv); + if (key) { + for (int64_t i = 0; i < m->count; i++) { + const char* k = EL_CSTR(m->keys[i]); + if (k && strcmp(k, key) == 0) { m->values[i] = value; return EL_STR(m); } + } + } + if (m->count >= m->capacity) { + int64_t new_cap = m->capacity > 0 ? m->capacity * 2 : 4; + el_val_t* gk = realloc(m->keys, (size_t)new_cap * sizeof(el_val_t)); + el_val_t* gv = realloc(m->values, (size_t)new_cap * sizeof(el_val_t)); + if (!gk || !gv) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + m->keys = gk; + m->values = gv; + m->capacity = new_cap; + } + m->keys[m->count] = keyv; + m->values[m->count] = value; + m->count++; + return EL_STR(m); +} + +el_val_t el_map_set(el_val_t mapv, el_val_t keyv, el_val_t value) { + ElMap* m = as_map(mapv); + if (!m) return 0; + if (m->hdr.refcount <= 1) { + return map_set_in_place(m, keyv, value); + } + /* Shared: copy then set. The original is preserved for its other owners. */ + int64_t new_cap = m->count + 1; + if (new_cap < 4) new_cap = 4; + ElMap* fresh = malloc(sizeof(ElMap)); + if (!fresh) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + fresh->hdr.magic = EL_MAGIC_MAP; + fresh->hdr.refcount = 1; + fresh->count = m->count; + fresh->capacity = new_cap; + fresh->keys = malloc((size_t)new_cap * sizeof(el_val_t)); + fresh->values = malloc((size_t)new_cap * sizeof(el_val_t)); + if (!fresh->keys || !fresh->values) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + if (m->count > 0) { + memcpy(fresh->keys, m->keys, (size_t)m->count * sizeof(el_val_t)); + memcpy(fresh->values, m->values, (size_t)m->count * sizeof(el_val_t)); + } + return map_set_in_place(fresh, keyv, value); +} + +/* ── Refcount ops ─────────────────────────────────────────────────────────── */ +/* + * Both retain and release sniff the magic header to decide whether a value + * is a refcounted heap object. For small integers, raw C strings, and any + * value whose magic word doesn't match, both functions are no-ops. This lets + * codegen emit them on every let-binding without having to track types. + * + * Safety: we filter out obvious non-pointers (small magnitudes, misaligned + * addresses) before dereferencing. For any value that passes the filter and + * lives in a mapped page, reading the first 4 bytes is safe — strings start + * with printable ASCII (< 0x80), so their magic word will never collide with + * EL_MAGIC_LIST (0xE1...) or EL_MAGIC_MAP (0xE1...). Random integers that + * happen to look like aligned heap pointers are exceedingly unlikely to land + * on a page whose first 4 bytes match either magic. */ + +static int looks_like_heap_obj(el_val_t v) { + if (v == 0) return 0; + int64_t s = (int64_t)v; + if (s > -0x10000 && s < 0x10000) return 0; /* small ints */ + uintptr_t p = (uintptr_t)v; + if (p < 0x10000) return 0; /* low addresses */ + if (p & 0x7) return 0; /* malloc returns 8-aligned */ + return 1; +} + +void el_retain(el_val_t v) { + if (!looks_like_heap_obj(v)) return; + ElHeader* h = (ElHeader*)(uintptr_t)v; + if (h->magic == EL_MAGIC_LIST || h->magic == EL_MAGIC_MAP) { + h->refcount++; + } +} + +void el_release(el_val_t v) { + if (!looks_like_heap_obj(v)) return; + ElHeader* h = (ElHeader*)(uintptr_t)v; + if (h->magic == EL_MAGIC_LIST) { + if (h->refcount > 0 && --h->refcount == 0) { + ElList* l = (ElList*)h; + free(l->elems); + l->hdr.magic = 0; /* poison so use-after-free is detected */ + free(l); + } + } else if (h->magic == EL_MAGIC_MAP) { + if (h->refcount > 0 && --h->refcount == 0) { + ElMap* m = (ElMap*)h; + free(m->keys); + free(m->values); + m->hdr.magic = 0; + free(m); + } + } +} + +/* ── Batch 2/3 forward decls (defined later in JSON section) ────────────── */ + +typedef struct JsonBuf JsonBuf; +typedef struct JsonParser JsonParser; +static void jb_init(JsonBuf* b); +static void jb_putc(JsonBuf* b, char c); +static void jb_puts(JsonBuf* b, const char* s); +static void jb_emit_escaped(JsonBuf* b, const char* s); +static int looks_like_string(el_val_t v); +static const char* json_find_key(const char* s, const char* key); +static const char* json_skip_value(const char* p); +static char* jp_parse_string_raw(JsonParser* jp); + +/* Struct definitions are visible here because batch 2/3 helpers above use + * them by value; the bodies (jb_init, etc.) appear in the JSON section. */ +struct JsonBuf { + char* buf; + size_t len; + size_t cap; +}; + +struct JsonParser { + const char* p; + const char* end; + int err; +}; + +/* ── Batch 2: Real HTTP (libcurl client + POSIX-socket server) ───────────── */ +/* + * Client: blocking libcurl easy-handle calls. Errors are returned as a JSON + * fragment {"error":"..."} so callers can detect via str_starts_with("{") / + * json_get_string("error", ...). + * + * Server: bind/listen/accept loop on a TCP socket. Each accepted connection + * is handled in its own pthread (detached). A semaphore-style counter caps + * concurrent in-flight connections at HTTP_MAX_CONNS (64). When the cap is + * reached, accept() blocks until a worker exits. This prevents runaway + * thread creation under high load. + * + * Handler dispatch: El does not expose first-class function references at + * the runtime layer, so the second argument to http_serve(port, handler) is + * treated as a string name (or any el_val_t — the runtime ignores its + * value and uses the registry). Callers register a C-level handler via + * + * extern void el_runtime_register_handler(const char* name, + * el_val_t (*fn)(el_val_t, + * el_val_t, + * el_val_t)); + * + * and select the active handler by calling http_set_handler("name") from + * El, or by setting it directly through the C registry. If no handler is + * registered, the server replies with a 200 carrying a default message so + * the loop is observable. + */ + +/* ── HTTP client write-callback buffer ───────────────────────────────────── */ + +typedef struct { + char* data; + size_t len; + size_t cap; +} HttpBuf; + +static void httpbuf_init(HttpBuf* b) { + b->cap = 1024; + b->len = 0; + b->data = malloc(b->cap); + if (!b->data) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + b->data[0] = '\0'; +} + +static void httpbuf_append(HttpBuf* b, const void* src, size_t n) { + if (b->len + n + 1 > b->cap) { + while (b->len + n + 1 > b->cap) b->cap *= 2; + b->data = realloc(b->data, b->cap); + if (!b->data) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + } + memcpy(b->data + b->len, src, n); + b->len += n; + b->data[b->len] = '\0'; +} + +static size_t http_write_cb(char* ptr, size_t size, size_t nmemb, void* ud) { + size_t n = size * nmemb; + httpbuf_append((HttpBuf*)ud, ptr, n); + return n; +} + +/* JSON-escape an arbitrary C string into an allocated buffer. */ +static char* json_escape_alloc(const char* s) { + if (!s) return el_strdup(""); + JsonBuf b; jb_init(&b); + for (const char* p = s; *p; p++) { + unsigned char c = (unsigned char)*p; + switch (c) { + case '"': jb_puts(&b, "\\\""); break; + case '\\': jb_puts(&b, "\\\\"); break; + case '\n': jb_puts(&b, "\\n"); break; + case '\r': jb_puts(&b, "\\r"); break; + case '\t': jb_puts(&b, "\\t"); break; + default: + if (c < 0x20) { + char tmp[8]; snprintf(tmp, sizeof(tmp), "\\u%04x", c); + jb_puts(&b, tmp); + } else jb_putc(&b, (char)c); + } + } + return b.buf; +} + +static el_val_t http_error_json(const char* msg) { + char* esc = json_escape_alloc(msg ? msg : "unknown error"); + char* buf = el_strbuf(strlen(esc) + 16); + sprintf(buf, "{\"error\":\"%s\"}", esc); + free(esc); + return el_wrap_str(buf); +} + +/* HTTP timeout (ms) — read once from EL_HTTP_TIMEOUT_MS, default 60000. + * Applied via CURLOPT_TIMEOUT_MS on every libcurl request. */ +static long _el_http_timeout_ms = -1; +static long el_http_timeout_ms(void) { + long v = __atomic_load_n(&_el_http_timeout_ms, __ATOMIC_ACQUIRE); + if (v >= 0) return v; + const char* s = getenv("EL_HTTP_TIMEOUT_MS"); + long parsed = 60000L; + if (s && *s) { + char* end = NULL; + long n = strtol(s, &end, 10); + if (end != s && n > 0) parsed = n; + } + __atomic_store_n(&_el_http_timeout_ms, parsed, __ATOMIC_RELEASE); + return parsed; +} + +/* Internal: do a libcurl request; takes optional body/headers, optional method override. */ +static el_val_t http_do(const char* method, const char* url, const char* body, + struct curl_slist* extra_headers) { + if (!url || !*url) return http_error_json("empty url"); + CURL* c = curl_easy_init(); + if (!c) return http_error_json("curl_easy_init failed"); + HttpBuf rb; httpbuf_init(&rb); + char errbuf[CURL_ERROR_SIZE]; errbuf[0] = '\0'; + curl_easy_setopt(c, CURLOPT_URL, url); + curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, http_write_cb); + curl_easy_setopt(c, CURLOPT_WRITEDATA, &rb); + curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(c, CURLOPT_TIMEOUT_MS, el_http_timeout_ms()); + curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1L); + curl_easy_setopt(c, CURLOPT_ERRORBUFFER, errbuf); + curl_easy_setopt(c, CURLOPT_USERAGENT, "el-runtime/1.0"); + if (extra_headers) curl_easy_setopt(c, CURLOPT_HTTPHEADER, extra_headers); + if (method && strcmp(method, "POST") == 0) { + curl_easy_setopt(c, CURLOPT_POST, 1L); + curl_easy_setopt(c, CURLOPT_POSTFIELDS, body ? body : ""); + curl_easy_setopt(c, CURLOPT_POSTFIELDSIZE, (long)(body ? strlen(body) : 0)); + } else if (method && strcmp(method, "DELETE") == 0) { + curl_easy_setopt(c, CURLOPT_CUSTOMREQUEST, "DELETE"); + } + CURLcode rc = curl_easy_perform(c); + curl_easy_cleanup(c); + if (rc != CURLE_OK) { + free(rb.data); + const char* m = errbuf[0] ? errbuf : curl_easy_strerror(rc); + return http_error_json(m); + } + return el_wrap_str(rb.data); +} + +el_val_t http_get(el_val_t url) { + return http_do("GET", EL_CSTR(url), NULL, NULL); +} + +el_val_t http_post(el_val_t url, el_val_t body) { + return http_do("POST", EL_CSTR(url), EL_CSTR(body), NULL); +} + +el_val_t http_post_json(el_val_t url, el_val_t json_body) { + struct curl_slist* h = NULL; + h = curl_slist_append(h, "Content-Type: application/json"); + el_val_t r = http_do("POST", EL_CSTR(url), EL_CSTR(json_body), h); + curl_slist_free_all(h); + return r; +} + +/* Build a curl_slist from an ElMap of name -> value strings. */ +static struct curl_slist* headers_from_map(el_val_t headers_map) { + struct curl_slist* h = NULL; + ElMap* m = as_map(headers_map); + if (!m) return NULL; + for (int64_t i = 0; i < m->count; i++) { + const char* k = EL_CSTR(m->keys[i]); + const char* v = EL_CSTR(m->values[i]); + if (!k || !v) continue; + size_t n = strlen(k) + strlen(v) + 4; + char* line = malloc(n); + if (!line) continue; + snprintf(line, n, "%s: %s", k, v); + h = curl_slist_append(h, line); + free(line); + } + return h; +} + +el_val_t http_get_with_headers(el_val_t url, el_val_t headers_map) { + struct curl_slist* h = headers_from_map(headers_map); + el_val_t r = http_do("GET", EL_CSTR(url), NULL, h); + if (h) curl_slist_free_all(h); + return r; +} + +el_val_t http_post_with_headers(el_val_t url, el_val_t body, el_val_t headers_map) { + struct curl_slist* h = headers_from_map(headers_map); + el_val_t r = http_do("POST", EL_CSTR(url), EL_CSTR(body), h); + if (h) curl_slist_free_all(h); + return r; +} + +el_val_t http_post_form_auth(el_val_t url, el_val_t form_body, el_val_t auth_header) { + struct curl_slist* h = NULL; + h = curl_slist_append(h, "Content-Type: application/x-www-form-urlencoded"); + const char* a = EL_CSTR(auth_header); + if (a && *a) { + size_t n = strlen(a) + 32; + char* line = malloc(n); + snprintf(line, n, "Authorization: %s", a); + h = curl_slist_append(h, line); + free(line); + } + el_val_t r = http_do("POST", EL_CSTR(url), EL_CSTR(form_body), h); + curl_slist_free_all(h); + return r; +} + +/* HTTP DELETE — mirrors http_post but with CURLOPT_CUSTOMREQUEST=DELETE. + * Returns response body on success; on transport failure returns an error + * JSON fragment (same convention as http_get/http_post). Callers that + * expect "" on failure should check for a leading '{' and an "error" key. */ +el_val_t http_delete(el_val_t url) { + return http_do("DELETE", EL_CSTR(url), NULL, NULL); +} + +/* ── HTTP → file streaming ──────────────────────────────────────────────── + * + * Why this exists: el_val_t strings are NUL-terminated by convention, so + * accumulating an HTTP response into an httpbuf and then wrapping its + * `.data` pointer with el_wrap_str() loses the byte length. Any consumer + * that does strlen() on the wrapped pointer truncates the body at the + * first embedded NUL. Audio (MP3, WAV, OGG), images (PNG, JPEG), and any + * other binary payload hits this. The vessels that download such bodies + * (e.g. ElevenLabs TTS → MP3) get silently corrupted files. + * + * The fix: wire libcurl's CURLOPT_WRITEFUNCTION directly to fwrite() + * against a fopen()-ed FILE*. The bytes never pass through an el_val_t + * string, so embedded NULs are preserved verbatim. Caller's contract is + * just "a file at this path with the response body in it". */ + +static size_t http_file_write_cb(char* ptr, size_t size, size_t nmemb, void* ud) { + FILE* f = (FILE*)ud; + return fwrite(ptr, size, nmemb, f); +} + +/* Internal: stream body to file. method is "GET" or "POST". body may be NULL + * (GET) or NUL-terminated (POST). headers may be NULL. Returns 1/0. */ +static el_val_t http_do_to_file(const char* method, const char* url, + const char* body, struct curl_slist* extra_headers, + const char* output_path) { + if (!url || !*url) return 0; + if (!output_path || !*output_path) return 0; + FILE* f = fopen(output_path, "wb"); + if (!f) return 0; + + CURL* c = curl_easy_init(); + if (!c) { fclose(f); remove(output_path); return 0; } + + char errbuf[CURL_ERROR_SIZE]; errbuf[0] = '\0'; + curl_easy_setopt(c, CURLOPT_URL, url); + curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, http_file_write_cb); + curl_easy_setopt(c, CURLOPT_WRITEDATA, f); + curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(c, CURLOPT_TIMEOUT_MS, el_http_timeout_ms()); + curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1L); + curl_easy_setopt(c, CURLOPT_ERRORBUFFER, errbuf); + curl_easy_setopt(c, CURLOPT_USERAGENT, "el-runtime/1.0"); + curl_easy_setopt(c, CURLOPT_FAILONERROR, 1L); /* 4xx/5xx → CURLE_HTTP_RETURNED_ERROR */ + if (extra_headers) curl_easy_setopt(c, CURLOPT_HTTPHEADER, extra_headers); + + if (method && strcmp(method, "POST") == 0) { + curl_easy_setopt(c, CURLOPT_POST, 1L); + curl_easy_setopt(c, CURLOPT_POSTFIELDS, body ? body : ""); + /* For the request body we still rely on strlen — POST bodies are + * caller-controlled and JSON/text in every known El use case. + * If a future caller needs a binary POST body, add a *_bytes + * variant that takes an explicit length, mirroring fs_write_bytes. */ + curl_easy_setopt(c, CURLOPT_POSTFIELDSIZE, (long)(body ? strlen(body) : 0)); + } + + CURLcode rc = curl_easy_perform(c); + curl_easy_cleanup(c); + + /* Flush + close before signalling success, so the file is fully on disk + * by the time the caller reads back. */ + int flush_ok = (fflush(f) == 0); + int close_ok = (fclose(f) == 0); + + if (rc != CURLE_OK || !flush_ok || !close_ok) { + remove(output_path); + return 0; + } + return 1; +} + +el_val_t http_get_to_file(el_val_t url, el_val_t headers_map, el_val_t output_path) { + struct curl_slist* h = headers_from_map(headers_map); + el_val_t r = http_do_to_file("GET", EL_CSTR(url), NULL, h, EL_CSTR(output_path)); + if (h) curl_slist_free_all(h); + return r; +} + +el_val_t http_post_to_file(el_val_t url, el_val_t body, el_val_t headers_map, el_val_t output_path) { + struct curl_slist* h = headers_from_map(headers_map); + el_val_t r = http_do_to_file("POST", EL_CSTR(url), EL_CSTR(body), h, EL_CSTR(output_path)); + if (h) curl_slist_free_all(h); + return r; +} + +/* ── HTTP server (POSIX sockets + pthreads) ──────────────────────────────── */ + +#define HTTP_MAX_CONNS 64 + +typedef el_val_t (*http_handler_fn)(el_val_t method, el_val_t path, el_val_t body); + +typedef struct { + char* name; + http_handler_fn fn; +} HttpHandlerEntry; + +static HttpHandlerEntry _http_handlers[32]; +static size_t _http_handler_count = 0; +static char* _http_active_handler = NULL; +static pthread_mutex_t _http_handler_mu = PTHREAD_MUTEX_INITIALIZER; + +static pthread_mutex_t _http_conn_mu = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t _http_conn_cv = PTHREAD_COND_INITIALIZER; +static int _http_conn_active = 0; + +/* Public C-level API: register a handler by name. Programs that want El + * `http_serve` to dispatch into their handler call this from main() before + * http_serve. Not declared in the header to keep the public API minimal — + * extern lookup works since C symbols are global. */ +void el_runtime_register_handler(const char* name, http_handler_fn fn); +void el_runtime_register_handler(const char* name, http_handler_fn fn) { + if (!name || !fn) return; + pthread_mutex_lock(&_http_handler_mu); + for (size_t i = 0; i < _http_handler_count; i++) { + if (strcmp(_http_handlers[i].name, name) == 0) { + _http_handlers[i].fn = fn; + pthread_mutex_unlock(&_http_handler_mu); + return; + } + } + if (_http_handler_count < sizeof(_http_handlers) / sizeof(_http_handlers[0])) { + _http_handlers[_http_handler_count].name = el_strdup(name); + _http_handlers[_http_handler_count].fn = fn; + _http_handler_count++; + } + pthread_mutex_unlock(&_http_handler_mu); +} + +void http_set_handler(el_val_t name) { + const char* n = EL_CSTR(name); + pthread_mutex_lock(&_http_handler_mu); + free(_http_active_handler); + _http_active_handler = el_strdup(n ? n : ""); + /* If the name is not yet in the registry, try dlsym lookup against + * the running binary's symbol table. Every El `fn name(...)` compiles + * to a global C symbol with that exact name, so El programs can self- + * register their own handlers just by calling http_set_handler("name"). */ + if (n && *n) { + int found = 0; + for (size_t i = 0; i < _http_handler_count; i++) { + if (strcmp(_http_handlers[i].name, n) == 0) { found = 1; break; } + } + if (!found) { + void* sym = dlsym(RTLD_DEFAULT, n); + if (sym && _http_handler_count < sizeof(_http_handlers) / sizeof(_http_handlers[0])) { + _http_handlers[_http_handler_count].name = el_strdup(n); + _http_handlers[_http_handler_count].fn = (http_handler_fn)sym; + _http_handler_count++; + } + } + } + pthread_mutex_unlock(&_http_handler_mu); +} + +static http_handler_fn http_lookup_active(void) { + http_handler_fn out = NULL; + pthread_mutex_lock(&_http_handler_mu); + if (_http_active_handler) { + for (size_t i = 0; i < _http_handler_count; i++) { + if (strcmp(_http_handlers[i].name, _http_active_handler) == 0) { + out = _http_handlers[i].fn; break; + } + } + } + pthread_mutex_unlock(&_http_handler_mu); + return out; +} + +/* Auto-detect Content-Type from response body. */ +static const char* http_detect_content_type(const char* body) { + if (!body) return "text/plain; charset=utf-8"; + const char* p = body; + /* Binary magic bytes — check before stripping whitespace */ + if ((unsigned char)p[0] == 0x89 && p[1]=='P' && p[2]=='N' && p[3]=='G') + return "image/png"; + if ((unsigned char)p[0] == 0xFF && (unsigned char)p[1] == 0xD8) + return "image/jpeg"; + if (strncmp(p, "GIF8", 4) == 0) return "image/gif"; + if (strncmp(p, "RIFF", 4) == 0) return "image/webp"; + if (strncmp(p, "wOFF", 4) == 0) return "font/woff"; + if (strncmp(p, "wOF2", 4) == 0) return "font/woff2"; + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + if (strncasecmp(p, "= cap) { + if (cap >= 1024 * 1024) { free(buf); return -1; } + cap *= 2; + buf = realloc(buf, cap); + if (!buf) return -1; + } + ssize_t n = recv(fd, buf + len, cap - len - 1, 0); + if (n <= 0) { free(buf); return -1; } + len += (size_t)n; + buf[len] = '\0'; + if (strstr(buf, "\r\n\r\n")) break; + } + /* Parse request line */ + char* sp1 = strchr(buf, ' '); + if (!sp1) { free(buf); return -1; } + *sp1 = '\0'; + *out_method = el_strdup(buf); + char* path_start = sp1 + 1; + char* sp2 = strchr(path_start, ' '); + if (!sp2) { free(*out_method); *out_method = NULL; free(buf); return -1; } + *sp2 = '\0'; + *out_path = el_strdup(path_start); + char* hdr_end = strstr(sp2 + 1, "\r\n\r\n"); + /* Capture the raw header block (after the request line's CRLF, up to + * but not including the terminating \r\n\r\n) for callers that asked + * for it. The legacy 3-arg path passes NULL and skips this. */ + if (out_headers_block) { + char* hdr_start = strstr(sp2 + 1, "\r\n"); + if (hdr_start && hdr_start < hdr_end) { + hdr_start += 2; + size_t hb_len = (size_t)(hdr_end - hdr_start); + char* hb = malloc(hb_len + 1); + if (hb) { + memcpy(hb, hdr_start, hb_len); + hb[hb_len] = '\0'; + *out_headers_block = hb; + } + } else { + *out_headers_block = el_strdup(""); + } + } + /* Find Content-Length */ + long content_length = 0; + char* hp = sp2 + 1; + while (hp < hdr_end) { + char* line_end = strstr(hp, "\r\n"); + /* line_end == hdr_end means we're on the LAST header line — its + * trailing \r\n is the same \r\n that begins the \r\n\r\n header + * terminator. Process this line; only stop when line_end is past + * hdr_end (which means the parser walked off the end of the + * header block). The previous condition (line_end >= hdr_end) + * silently dropped any Content-Length that appeared as the last + * header — exactly what real curl/clients tend to emit. */ + if (!line_end || line_end > hdr_end) break; + if (strncasecmp(hp, "Content-Length:", 15) == 0) { + content_length = strtol(hp + 15, NULL, 10); + if (content_length < 0) content_length = 0; + if (content_length > 64 * 1024 * 1024) content_length = 64 * 1024 * 1024; + } + hp = line_end + 2; + } + /* Body: any bytes already read past hdr_end, plus more recv */ + char* body_start = hdr_end + 4; + size_t body_have = (buf + len) - body_start; + char* body = malloc((size_t)content_length + 1); + if (!body) { free(*out_method); free(*out_path); *out_method=NULL; *out_path=NULL; free(buf); return -1; } + if ((long)body_have > content_length) body_have = (size_t)content_length; + if (body_have > 0) memcpy(body, body_start, body_have); + while ((long)body_have < content_length) { + ssize_t n = recv(fd, body + body_have, (size_t)content_length - body_have, 0); + if (n <= 0) break; + body_have += (size_t)n; + } + body[body_have] = '\0'; + *out_body = body; + free(buf); + return 0; +} + +/* Reason phrase for common HTTP statuses. Falls back to "Status" for the + * long tail — clients only care about the numeric code. */ +static const char* http_reason_phrase(int status) { + switch (status) { + case 200: return "OK"; + case 201: return "Created"; + case 202: return "Accepted"; + case 204: return "No Content"; + case 301: return "Moved Permanently"; + case 302: return "Found"; + case 303: return "See Other"; + case 304: return "Not Modified"; + case 307: return "Temporary Redirect"; + case 308: return "Permanent Redirect"; + case 400: return "Bad Request"; + case 401: return "Unauthorized"; + case 403: return "Forbidden"; + case 404: return "Not Found"; + case 405: return "Method Not Allowed"; + case 409: return "Conflict"; + case 410: return "Gone"; + case 422: return "Unprocessable Entity"; + case 429: return "Too Many Requests"; + case 500: return "Internal Server Error"; + case 501: return "Not Implemented"; + case 502: return "Bad Gateway"; + case 503: return "Service Unavailable"; + case 504: return "Gateway Timeout"; + default: return "Status"; + } +} + +/* Best-effort send with retry on partial writes. */ +static int http_send_all(int fd, const char* p, size_t left) { + while (left > 0) { + ssize_t w = send(fd, p, left, 0); + if (w <= 0) return -1; + p += w; left -= (size_t)w; + } + return 0; +} + +/* Discriminator that http_response() embeds at the start of its envelope. + * A handler returning a string starting with this exact prefix is treated + * as a structured response; anything else is treated as a raw body. */ +#define EL_HTTP_RESPONSE_TAG "{\"el_http_response\":1" + +/* Keys that conflict with runtime-managed headers are silently dropped to + * avoid double-emission — the runtime always emits its own Content-Length + * and Connection: close. Content-Type from the envelope IS allowed and + * overrides auto-detection. */ +static int http_header_is_managed(const char* k) { + return strcasecmp(k, "Content-Length") == 0 + || strcasecmp(k, "Connection") == 0; +} + +/* Walk an ElMap of header pairs and emit each as `K: V\r\n` into JsonBuf b. + * Sets *out_saw_content_type to 1 if the map contained an explicit + * Content-Type so the caller can skip auto-detection. */ +static void http_emit_headers_from_map(JsonBuf* b, el_val_t headers_map, + int* out_saw_content_type) { + *out_saw_content_type = 0; + if (headers_map == 0) return; + ElMap* m = (ElMap*)(uintptr_t)headers_map; + if (!m || m->hdr.magic != EL_MAGIC_MAP) return; + for (int64_t i = 0; i < m->count; i++) { + const char* k = EL_CSTR(m->keys[i]); + const char* v = EL_CSTR(m->values[i]); + if (!k || !v) continue; + if (http_header_is_managed(k)) continue; + if (strcasecmp(k, "Content-Type") == 0) *out_saw_content_type = 1; + jb_puts(b, k); + jb_puts(b, ": "); + jb_puts(b, v); + jb_puts(b, "\r\n"); + } +} + +/* Parse the envelope produced by http_response(). On success returns 1 and + * populates *out_status, *out_headers_map (an ElMap el_val_t — caller must + * el_release), and *out_body (allocated). On failure returns 0. + * + * Implementation: feeds the entire envelope through the recursive-descent + * JSON parser (which builds proper ElMap/ElList values), then pulls the + * three top-level fields by name. Avoids re-stringifying the headers map + * since json_stringify() does not support nested objects. */ +static int http_parse_envelope(const char* s, int* out_status, + el_val_t* out_headers_map, char** out_body, + el_val_t* out_parsed_root) { + if (!s) return 0; + if (strncmp(s, EL_HTTP_RESPONSE_TAG, + sizeof(EL_HTTP_RESPONSE_TAG) - 1) != 0) return 0; + + el_val_t parsed = json_parse(EL_STR(s)); + if (parsed == EL_NULL) return 0; + + int status = 200; + el_val_t hmap = 0; + char* body = NULL; + + el_val_t sv = el_map_get(parsed, EL_STR("status")); + if (sv != 0) { + /* status comes back as an integer — el_val_t holds it directly. */ + long sc = (long)sv; + if (sc >= 100 && sc <= 599) status = (int)sc; + } + + el_val_t hv = el_map_get(parsed, EL_STR("headers")); + if (hv != 0) { + ElMap* hm = (ElMap*)(uintptr_t)hv; + if (hm && hm->hdr.magic == EL_MAGIC_MAP) hmap = hv; + } + + el_val_t bv = el_map_get(parsed, EL_STR("body")); + if (bv != 0) { + const char* bs = EL_CSTR(bv); + if (bs) body = el_strdup(bs); + } + if (!body) body = el_strdup(""); + + *out_status = status; + *out_headers_map = hmap; + *out_body = body; + *out_parsed_root = parsed; /* caller releases to free hmap + entries */ + return 1; +} + +/* Send a fully-built HTTP response. If `body` starts with the envelope tag, + * unpack status/headers/body. Otherwise emit the historical 200-OK with + * auto-detected Content-Type. */ +static void http_send_response(int fd, const char* body) { + if (!body) body = ""; + + int status = 200; + el_val_t env_headers_map = 0; + char* env_body = NULL; + el_val_t env_parsed_root = 0; + int is_envelope = http_parse_envelope(body, &status, + &env_headers_map, &env_body, + &env_parsed_root); + + const char* eff_body = is_envelope ? env_body : body; + /* Use the real byte count from fs_read if available (handles binary files + * with embedded null bytes — PNG, WOFF2, etc.). Fall back to strlen for + * normal text/JSON responses where _tl_fs_read_len is 0. */ + size_t blen = (_tl_fs_read_len > 0) ? _tl_fs_read_len : strlen(eff_body); + _tl_fs_read_len = 0; /* consume — one-shot per response */ + + JsonBuf hdrs; jb_init(&hdrs); + int saw_content_type = 0; + if (is_envelope) { + http_emit_headers_from_map(&hdrs, env_headers_map, + &saw_content_type); + } + if (!saw_content_type) { + jb_puts(&hdrs, "Content-Type: "); + jb_puts(&hdrs, http_detect_content_type(eff_body)); + jb_puts(&hdrs, "\r\n"); + } + + char status_line[64]; + int sl = snprintf(status_line, sizeof(status_line), + "HTTP/1.1 %d %s\r\n", + status, http_reason_phrase(status)); + if (sl < 0) { + if (env_parsed_root) el_release(env_parsed_root); + free(env_body); free(hdrs.buf); return; + } + + char tail[128]; + int tl = snprintf(tail, sizeof(tail), + "Content-Length: %zu\r\n" + "Connection: close\r\n" + "\r\n", blen); + if (tl < 0) { + if (env_parsed_root) el_release(env_parsed_root); + free(env_body); free(hdrs.buf); return; + } + + if (http_send_all(fd, status_line, (size_t)sl) == 0 + && http_send_all(fd, hdrs.buf, hdrs.len) == 0 + && http_send_all(fd, tail, (size_t)tl) == 0 + && http_send_all(fd, eff_body, blen) == 0) { + /* sent successfully */ + } + + if (env_parsed_root) el_release(env_parsed_root); + free(env_body); + free(hdrs.buf); +} + +typedef struct { + int fd; +} HttpWorkerArg; + +static void* http_worker(void* arg) { + HttpWorkerArg* a = (HttpWorkerArg*)arg; + int fd = a->fd; + free(a); + char *method = NULL, *path = NULL, *body = NULL; + if (http_read_request(fd, &method, &path, &body, NULL) == 0) { + http_handler_fn h = http_lookup_active(); + char* response = NULL; + el_request_start(); /* begin per-request arena */ + if (h) { + el_val_t r = h(EL_STR(method), EL_STR(path), EL_STR(body)); + const char* rs = EL_CSTR(r); + /* Copy response out BEFORE arena teardown. + * For binary files, _tl_fs_read_len holds the real byte count — + * use memcpy instead of strdup so null bytes are preserved. */ + size_t rlen = _tl_fs_read_len > 0 ? _tl_fs_read_len : (rs ? strlen(rs) : 0); + response = malloc(rlen + 1); + if (response && rs) { memcpy(response, rs, rlen); response[rlen] = '\0'; } + else if (response) { response[0] = '\0'; } + } else { + response = el_strdup_persist("el-runtime: no http handler registered"); + } + el_request_end(); /* free all intermediate strings */ + http_send_response(fd, response); + free(response); + } + free(method); free(path); free(body); + close(fd); + /* release a slot */ + pthread_mutex_lock(&_http_conn_mu); + _http_conn_active--; + pthread_cond_signal(&_http_conn_cv); + pthread_mutex_unlock(&_http_conn_mu); + return NULL; +} + +void http_serve(el_val_t port, el_val_t handler) { + /* If `handler` looks like a string name, register it as the active handler. */ + const char* hname = EL_CSTR(handler); + if (hname && looks_like_string(handler)) { + http_set_handler(handler); + } + int p = (int)port; + if (p <= 0 || p > 65535) { fprintf(stderr, "http_serve: invalid port %d\n", p); return; } + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { perror("socket"); return; } + int yes = 1; + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons((uint16_t)p); + if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + perror("bind"); close(sock); return; + } + if (listen(sock, 64) < 0) { perror("listen"); close(sock); return; } + fprintf(stderr, "[http] listening on 0.0.0.0:%d\n", p); + while (1) { + struct sockaddr_in cli; + socklen_t clen = sizeof(cli); + int cfd = accept(sock, (struct sockaddr*)&cli, &clen); + if (cfd < 0) { + if (errno == EINTR) continue; + perror("accept"); break; + } + pthread_mutex_lock(&_http_conn_mu); + while (_http_conn_active >= HTTP_MAX_CONNS) { + pthread_cond_wait(&_http_conn_cv, &_http_conn_mu); + } + _http_conn_active++; + pthread_mutex_unlock(&_http_conn_mu); + HttpWorkerArg* arg = malloc(sizeof(HttpWorkerArg)); + if (!arg) { close(cfd); continue; } + arg->fd = cfd; + pthread_t tid; + if (pthread_create(&tid, NULL, http_worker, arg) != 0) { + close(cfd); free(arg); + pthread_mutex_lock(&_http_conn_mu); + _http_conn_active--; + pthread_cond_signal(&_http_conn_cv); + pthread_mutex_unlock(&_http_conn_mu); + continue; + } + pthread_detach(tid); + } + close(sock); +} + +/* ── HTTP server v2 — request headers + structured response ──────────────── */ +/* + * v2 widens the handler signature from + * (method, path, body) -> body_string + * to + * (method, path, headers_map, body) -> body_string_or_envelope + * + * The response envelope is detected uniformly inside http_send_response — so + * 4-arg handlers can return either a plain body or http_response(...). The + * 3-arg path stays untouched in spirit (its handlers still build plain + * bodies; the envelope tag, being `{"el_http_response":1`, will never + * collide with normal JSON the legacy server.el routes return). + * + * Registry is parallel to the 3-arg handler registry: separate name table, + * separate active-handler slot, separate dlsym fallback. Mixing v1 and v2 + * handlers in the same process is fine — they don't share the active slot. */ + +typedef el_val_t (*http_handler4_fn)(el_val_t method, el_val_t path, + el_val_t headers_map, el_val_t body); + +typedef struct { + char* name; + http_handler4_fn fn; +} HttpHandler4Entry; + +static HttpHandler4Entry _http_handlers4[32]; +static size_t _http_handler4_count = 0; +static char* _http_active_handler4 = NULL; + +void el_runtime_register_handler_v2(const char* name, http_handler4_fn fn); +void el_runtime_register_handler_v2(const char* name, http_handler4_fn fn) { + if (!name || !fn) return; + pthread_mutex_lock(&_http_handler_mu); + for (size_t i = 0; i < _http_handler4_count; i++) { + if (strcmp(_http_handlers4[i].name, name) == 0) { + _http_handlers4[i].fn = fn; + pthread_mutex_unlock(&_http_handler_mu); + return; + } + } + if (_http_handler4_count < + sizeof(_http_handlers4) / sizeof(_http_handlers4[0])) { + _http_handlers4[_http_handler4_count].name = el_strdup(name); + _http_handlers4[_http_handler4_count].fn = fn; + _http_handler4_count++; + } + pthread_mutex_unlock(&_http_handler_mu); +} + +void http_set_handler_v2(el_val_t name) { + const char* n = EL_CSTR(name); + pthread_mutex_lock(&_http_handler_mu); + free(_http_active_handler4); + _http_active_handler4 = el_strdup(n ? n : ""); + if (n && *n) { + int found = 0; + for (size_t i = 0; i < _http_handler4_count; i++) { + if (strcmp(_http_handlers4[i].name, n) == 0) { found = 1; break; } + } + if (!found) { + void* sym = dlsym(RTLD_DEFAULT, n); + if (sym && _http_handler4_count < + sizeof(_http_handlers4) / sizeof(_http_handlers4[0])) { + _http_handlers4[_http_handler4_count].name = el_strdup(n); + _http_handlers4[_http_handler4_count].fn = + (http_handler4_fn)sym; + _http_handler4_count++; + } + } + } + pthread_mutex_unlock(&_http_handler_mu); +} + +static http_handler4_fn http_lookup_active_v2(void) { + http_handler4_fn out = NULL; + pthread_mutex_lock(&_http_handler_mu); + if (_http_active_handler4) { + for (size_t i = 0; i < _http_handler4_count; i++) { + if (strcmp(_http_handlers4[i].name, + _http_active_handler4) == 0) { + out = _http_handlers4[i].fn; break; + } + } + } + pthread_mutex_unlock(&_http_handler_mu); + return out; +} + +/* Build an ElMap from the raw header block produced by http_read_request. + * Keys are lowercased (RFC 7230 — case-insensitive); values have leading + * whitespace trimmed. Repeated headers with the same name are joined with + * ", " in arrival order, matching standard library behaviour elsewhere. */ +static el_val_t http_build_headers_map(const char* hdr_block) { + el_val_t m = el_map_new(0); + if (!hdr_block || !*hdr_block) return m; + const char* p = hdr_block; + while (*p) { + const char* line_end = strstr(p, "\r\n"); + const char* end = line_end ? line_end : p + strlen(p); + const char* colon = NULL; + for (const char* c = p; c < end; c++) { + if (*c == ':') { colon = c; break; } + } + if (colon && colon > p) { + size_t klen = (size_t)(colon - p); + char* key = malloc(klen + 1); + if (key) { + for (size_t i = 0; i < klen; i++) { + unsigned char ch = (unsigned char)p[i]; + key[i] = (char)tolower(ch); + } + key[klen] = '\0'; + const char* vstart = colon + 1; + while (vstart < end && (*vstart == ' ' || *vstart == '\t')) vstart++; + size_t vlen = (size_t)(end - vstart); + /* Strip trailing OWS just in case. */ + while (vlen > 0 + && (vstart[vlen - 1] == ' ' + || vstart[vlen - 1] == '\t')) vlen--; + /* Coalesce repeats: if key already present, append ", value". */ + el_val_t existing = el_map_get(m, EL_STR(key)); + if (existing != 0 && looks_like_string(existing)) { + const char* old = EL_CSTR(existing); + size_t olen = strlen(old); + char* combined = malloc(olen + 2 + vlen + 1); + if (combined) { + memcpy(combined, old, olen); + memcpy(combined + olen, ", ", 2); + memcpy(combined + olen + 2, vstart, vlen); + combined[olen + 2 + vlen] = '\0'; + m = el_map_set(m, EL_STR(key), EL_STR(combined)); + } + free(key); + } else { + char* val = malloc(vlen + 1); + if (val) { + memcpy(val, vstart, vlen); + val[vlen] = '\0'; + m = el_map_set(m, EL_STR(key), EL_STR(val)); + } else { + free(key); + } + } + } + } + if (!line_end) break; + p = line_end + 2; + } + return m; +} + +static void* http_worker_v2(void* arg) { + HttpWorkerArg* a = (HttpWorkerArg*)arg; + int fd = a->fd; + free(a); + char *method = NULL, *path = NULL, *body = NULL, *hdr_block = NULL; + if (http_read_request(fd, &method, &path, &body, &hdr_block) == 0) { + http_handler4_fn h = http_lookup_active_v2(); + char* response = NULL; + el_request_start(); /* begin per-request arena */ + if (h) { + el_val_t hmap = http_build_headers_map(hdr_block ? hdr_block : ""); + el_val_t r = h(EL_STR(method), EL_STR(path), hmap, EL_STR(body)); + const char* rs = EL_CSTR(r); + size_t rlen = _tl_fs_read_len > 0 ? _tl_fs_read_len : (rs ? strlen(rs) : 0); + response = malloc(rlen + 1); + if (response && rs) { memcpy(response, rs, rlen); response[rlen] = '\0'; } + else if (response) { response[0] = '\0'; } + el_release(hmap); + } else { + response = el_strdup_persist( + "el-runtime: no v2 http handler registered " + "(call http_set_handler_v2)"); + } + el_request_end(); /* free all intermediate strings */ + http_send_response(fd, response); + free(response); + } + free(method); free(path); free(body); free(hdr_block); + close(fd); + pthread_mutex_lock(&_http_conn_mu); + _http_conn_active--; + pthread_cond_signal(&_http_conn_cv); + pthread_mutex_unlock(&_http_conn_mu); + return NULL; +} + +void http_serve_v2(el_val_t port, el_val_t handler) { + const char* hname = EL_CSTR(handler); + if (hname && looks_like_string(handler)) { + http_set_handler_v2(handler); + } + int p = (int)port; + if (p <= 0 || p > 65535) { + fprintf(stderr, "http_serve_v2: invalid port %d\n", p); + return; + } + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { perror("socket"); return; } + int yes = 1; + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons((uint16_t)p); + if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + perror("bind"); close(sock); return; + } + if (listen(sock, 64) < 0) { perror("listen"); close(sock); return; } + fprintf(stderr, "[http v2] listening on 0.0.0.0:%d\n", p); + while (1) { + struct sockaddr_in cli; + socklen_t clen = sizeof(cli); + int cfd = accept(sock, (struct sockaddr*)&cli, &clen); + if (cfd < 0) { + if (errno == EINTR) continue; + perror("accept"); break; + } + pthread_mutex_lock(&_http_conn_mu); + while (_http_conn_active >= HTTP_MAX_CONNS) { + pthread_cond_wait(&_http_conn_cv, &_http_conn_mu); + } + _http_conn_active++; + pthread_mutex_unlock(&_http_conn_mu); + HttpWorkerArg* arg = malloc(sizeof(HttpWorkerArg)); + if (!arg) { close(cfd); continue; } + arg->fd = cfd; + pthread_t tid; + if (pthread_create(&tid, NULL, http_worker_v2, arg) != 0) { + close(cfd); free(arg); + pthread_mutex_lock(&_http_conn_mu); + _http_conn_active--; + pthread_cond_signal(&_http_conn_cv); + pthread_mutex_unlock(&_http_conn_mu); + continue; + } + pthread_detach(tid); + } + close(sock); +} + +/* Build the response envelope a 4-arg handler can return. We hand-write + * the JSON so the discriminator key always lands first — the runtime's + * http_parse_envelope() detects it via prefix match. headers_json must be + * either "" (empty), "{}" (empty object), or a well-formed JSON object + * literal; anything else will produce a malformed envelope and the runtime + * will treat the whole string as a plain body (no envelope detected). */ +el_val_t http_response(el_val_t status, el_val_t headers_json, el_val_t body) { + long sc = (long)status; + if (sc < 100 || sc > 599) sc = 200; + const char* hj = EL_CSTR(headers_json); + if (!hj || !*hj) hj = "{}"; + /* Light validation: must start with '{' and end with '}'. */ + size_t hlen = strlen(hj); + int hj_ok = (hlen >= 2 && hj[0] == '{' && hj[hlen - 1] == '}'); + if (!hj_ok) hj = "{}"; + const char* b = EL_CSTR(body); + if (!b) b = ""; + + JsonBuf out; jb_init(&out); + jb_puts(&out, EL_HTTP_RESPONSE_TAG); /* {"el_http_response":1 */ + jb_puts(&out, ",\"status\":"); + char num[32]; + snprintf(num, sizeof(num), "%ld", sc); + jb_puts(&out, num); + jb_puts(&out, ",\"headers\":"); + jb_puts(&out, hj); + jb_puts(&out, ",\"body\":"); + jb_emit_escaped(&out, b); + jb_putc(&out, '}'); + return el_wrap_str(out.buf); +} + +/* ── Filesystem ──────────────────────────────────────────────────────────── */ + +el_val_t fs_read(el_val_t pathv) { + const char* path = EL_CSTR(pathv); + _tl_fs_read_len = 0; + if (!path) return el_wrap_str(el_strdup("")); + FILE* f = fopen(path, "rb"); + if (!f) return el_wrap_str(el_strdup("")); + fseek(f, 0, SEEK_END); + long sz = ftell(f); + rewind(f); + if (sz < 0) { fclose(f); return el_wrap_str(el_strdup("")); } /* pipe/special file */ + char* buf = el_strbuf((size_t)sz); + size_t got = fread(buf, 1, (size_t)sz, f); + buf[got] = '\0'; + _tl_fs_read_len = got; /* store real byte count for binary-safe send */ + fclose(f); + return el_wrap_str(buf); +} + +el_val_t fs_write(el_val_t pathv, el_val_t contentv) { + const char* path = EL_CSTR(pathv); + const char* content = EL_CSTR(contentv); + if (!path || !content) return 0; + FILE* f = fopen(path, "wb"); + if (!f) return 0; + size_t n = strlen(content); + size_t written = fwrite(content, 1, n, f); + fclose(f); + return written == n ? 1 : 0; +} + +/* fs_write_bytes — explicit-length binary write. Bypasses strlen so embedded + * NULs survive. Caller must know the byte count (e.g. from base64_decode, + * or the fixed 32-byte sha256_bytes/hmac_sha256_bytes outputs). + * + * If `length` is negative, treats as failure. If `length` is 0, creates an + * empty file (still useful as a "touch with content" primitive). */ +el_val_t fs_write_bytes(el_val_t pathv, el_val_t bytesv, el_val_t lengthv) { + const char* path = EL_CSTR(pathv); + const char* bytes = EL_CSTR(bytesv); + int64_t n = (int64_t)lengthv; + if (!path || !bytes) return 0; + if (n < 0) return 0; + FILE* f = fopen(path, "wb"); + if (!f) return 0; + size_t written = (n > 0) ? fwrite(bytes, 1, (size_t)n, f) : 0; + int flush_ok = (fflush(f) == 0); + int close_ok = (fclose(f) == 0); + if (!flush_ok || !close_ok || written != (size_t)n) { + remove(path); + return 0; + } + return 1; +} + +el_val_t fs_list(el_val_t pathv) { + const char* path = EL_CSTR(pathv); + el_val_t lst = el_list_empty(); + if (!path) return lst; + DIR* d = opendir(path); + if (!d) return lst; + struct dirent* e; + while ((e = readdir(d)) != NULL) { + if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) continue; + lst = el_list_append(lst, el_wrap_str(el_strdup(e->d_name))); + } + closedir(d); + return lst; +} + +/* fs_exists — true iff stat(path) succeeds. Symlinks are followed. */ +el_val_t fs_exists(el_val_t pathv) { + const char* path = EL_CSTR(pathv); + if (!path || !*path) return 0; + struct stat st; + return (el_val_t)(stat(path, &st) == 0 ? 1 : 0); +} + +/* fs_mkdir — create directory at path with mode 0755, mkdir -p semantics. + * Returns 1 if path exists or was created (incl. all parents); 0 on failure. + * Walks the path component-by-component so missing intermediate dirs are + * also created. An existing leaf is not an error. */ +el_val_t fs_mkdir(el_val_t pathv) { + const char* path = EL_CSTR(pathv); + if (!path || !*path) return 0; + size_t n = strlen(path); + char* buf = malloc(n + 1); + if (!buf) return 0; + memcpy(buf, path, n + 1); + /* Walk components; create each prefix in turn. */ + for (size_t i = 1; i <= n; i++) { + if (buf[i] == '/' || buf[i] == '\0') { + char saved = buf[i]; + buf[i] = '\0'; + if (buf[0] != '\0') { + if (mkdir(buf, 0755) != 0 && errno != EEXIST) { + /* Tolerate the case where this prefix exists as a non-dir + * only when stat says it's a directory. */ + struct stat st; + if (stat(buf, &st) != 0 || !S_ISDIR(st.st_mode)) { + free(buf); + return 0; + } + } + } + buf[i] = saved; + } + } + free(buf); + return 1; +} + +/* ── URL encoding ─────────────────────────────────────────────────────────── */ + +/* RFC 3986 percent-encoding for URL components (form bodies, query strings). + * Unreserved set: A-Z a-z 0-9 - _ . ~ — passed through verbatim. + * Everything else (including space) becomes %XX hex. */ +el_val_t url_encode(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return el_wrap_str(el_strdup("")); + static const char hex[] = "0123456789ABCDEF"; + size_t n = strlen(s); + char* out = el_strbuf(n * 3); + size_t o = 0; + for (size_t i = 0; i < n; i++) { + unsigned char c = (unsigned char)s[i]; + if ((c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || + c == '-' || c == '_' || c == '.' || c == '~') { + out[o++] = (char)c; + } else { + out[o++] = '%'; + out[o++] = hex[(c >> 4) & 0xF]; + out[o++] = hex[c & 0xF]; + } + } + out[o] = '\0'; + return el_wrap_str(out); +} + +/* Decode percent-encoded URL component. '+' becomes space (form-encoded); + * malformed %-escapes are emitted verbatim. */ +el_val_t url_decode(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return el_wrap_str(el_strdup("")); + size_t n = strlen(s); + char* out = el_strbuf(n); + size_t o = 0; + for (size_t i = 0; i < n; i++) { + char c = s[i]; + if (c == '+') { + out[o++] = ' '; + } else if (c == '%' && i + 2 < n) { + char h1 = s[i + 1], h2 = s[i + 2]; + int v1 = (h1 >= '0' && h1 <= '9') ? h1 - '0' + : (h1 >= 'a' && h1 <= 'f') ? h1 - 'a' + 10 + : (h1 >= 'A' && h1 <= 'F') ? h1 - 'A' + 10 : -1; + int v2 = (h2 >= '0' && h2 <= '9') ? h2 - '0' + : (h2 >= 'a' && h2 <= 'f') ? h2 - 'a' + 10 + : (h2 >= 'A' && h2 <= 'F') ? h2 - 'A' + 10 : -1; + if (v1 >= 0 && v2 >= 0) { + out[o++] = (char)((v1 << 4) | v2); + i += 2; + } else { + out[o++] = c; + } + } else { + out[o++] = c; + } + } + out[o] = '\0'; + return el_wrap_str(out); +} + +/* ── JSON ────────────────────────────────────────────────────────────────── */ + +el_val_t json_get(el_val_t jsonv, el_val_t keyv) { + const char* json = EL_CSTR(jsonv); + const char* key = EL_CSTR(keyv); + if (!json || !key) return el_wrap_str(el_strdup("")); + size_t klen = strlen(key); + /* Use a stack buffer for the pattern to avoid arena double-free. + * Keys in El maps are typically short; 512 bytes is a safe upper bound. */ + char stack_pat[512]; + char* pattern; + if (klen + 5 <= sizeof(stack_pat)) { + pattern = stack_pat; + } else { + pattern = malloc(klen + 5); + if (!pattern) return el_wrap_str(el_strdup("")); + } + snprintf(pattern, klen + 5, "\"%s\":", key); + const char* p = strstr(json, pattern); + if (pattern != stack_pat) free(pattern); + if (!p) return el_wrap_str(el_strdup("")); + p += strlen(key) + 3; /* skip "key": */ + while (*p == ' ' || *p == '\t' || *p == '\n') p++; + if (*p == '"') { + p++; + /* Unescape the JSON string value into a clean buffer. */ + size_t cap = strlen(p) + 1; + char* out = el_strbuf(cap); + char* w = out; + while (*p && *p != '"') { + if (*p == '\\' && *(p+1)) { + p++; + switch (*p) { + case '"': *w++ = '"'; break; + case '\\': *w++ = '\\'; break; + case '/': *w++ = '/'; break; + case 'n': *w++ = '\n'; break; + case 'r': *w++ = '\r'; break; + case 't': *w++ = '\t'; break; + default: *w++ = *p; break; + } + } else { + *w++ = *p; + } + p++; + } + *w = '\0'; + return el_wrap_str(out); + } + const char* start = p; + while (*p && *p != ',' && *p != '}' && *p != ']' && *p != '\n') p++; + size_t len = (size_t)(p - start); + char* out = el_strbuf(len); + memcpy(out, start, len); + out[len] = '\0'; + return el_wrap_str(out); +} + +/* ── Float bit-cast helpers ──────────────────────────────────────────────── */ +/* `el_to_float` and `el_from_float` are exposed in el_runtime.h as static + * inlines so generated programs (which #include the header) can call them + * for Float literals. No definitions are needed here. */ + +/* ── JSON parser (recursive descent) ─────────────────────────────────────── */ +/* + * Parsed JSON representation: + * - object -> ElMap (keys & values are el_val_t) + * - array -> ElList + * - string -> EL_STR-wrapped char* (allocated) + * - number -> int (el_val_t) if integer, otherwise el_from_float(double) + * - true -> 1 + * - false -> 0 + * - null -> EL_NULL (0) + * + * Note: there is no runtime type tag — parsed numbers cannot be + * distinguished from booleans by the runtime alone. The codegen tracks + * types separately. This matches the rest of el_val_t's type-erased model. + */ + +/* JsonParser struct is forward-declared near the HTTP/Engram section. */ + +static void jp_skip_ws(JsonParser* jp) { + while (jp->p < jp->end) { + char c = *jp->p; + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') jp->p++; + else break; + } +} + +static el_val_t jp_parse_value(JsonParser* jp); + +/* Parse a JSON string literal (the opening " has NOT yet been consumed). */ +static char* jp_parse_string_raw(JsonParser* jp) { + if (jp->p >= jp->end || *jp->p != '"') { jp->err = 1; return el_strdup(""); } + jp->p++; + size_t cap = 32, len = 0; + char* out = malloc(cap); + if (!out) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + while (jp->p < jp->end && *jp->p != '"') { + char c = *jp->p++; + if (c == '\\' && jp->p < jp->end) { + char esc = *jp->p++; + switch (esc) { + case '"': c = '"'; break; + case '\\': c = '\\'; break; + case '/': c = '/'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'u': { + /* Skip 4 hex digits; emit '?' as a placeholder */ + for (int i = 0; i < 4 && jp->p < jp->end; i++) jp->p++; + c = '?'; + break; + } + default: c = esc; break; + } + } + if (len + 1 >= cap) { + cap *= 2; + out = realloc(out, cap); + if (!out) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + } + out[len++] = c; + } + if (jp->p < jp->end && *jp->p == '"') jp->p++; + else jp->err = 1; + out[len] = '\0'; + return out; +} + +static el_val_t jp_parse_number(JsonParser* jp) { + const char* start = jp->p; + int is_float = 0; + if (jp->p < jp->end && (*jp->p == '-' || *jp->p == '+')) jp->p++; + while (jp->p < jp->end && isdigit((unsigned char)*jp->p)) jp->p++; + if (jp->p < jp->end && *jp->p == '.') { + is_float = 1; jp->p++; + while (jp->p < jp->end && isdigit((unsigned char)*jp->p)) jp->p++; + } + if (jp->p < jp->end && (*jp->p == 'e' || *jp->p == 'E')) { + is_float = 1; jp->p++; + if (jp->p < jp->end && (*jp->p == '+' || *jp->p == '-')) jp->p++; + while (jp->p < jp->end && isdigit((unsigned char)*jp->p)) jp->p++; + } + size_t n = (size_t)(jp->p - start); + char buf[64]; + if (n >= sizeof(buf)) n = sizeof(buf) - 1; + memcpy(buf, start, n); + buf[n] = '\0'; + if (is_float) return el_from_float(strtod(buf, NULL)); + return (el_val_t)strtoll(buf, NULL, 10); +} + +static el_val_t jp_parse_array(JsonParser* jp) { + if (jp->p < jp->end && *jp->p == '[') jp->p++; + el_val_t lst = el_list_empty(); + jp_skip_ws(jp); + if (jp->p < jp->end && *jp->p == ']') { jp->p++; return lst; } + while (jp->p < jp->end) { + jp_skip_ws(jp); + el_val_t v = jp_parse_value(jp); + lst = el_list_append(lst, v); + jp_skip_ws(jp); + if (jp->p < jp->end && *jp->p == ',') { jp->p++; continue; } + if (jp->p < jp->end && *jp->p == ']') { jp->p++; break; } + jp->err = 1; + break; + } + return lst; +} + +static el_val_t jp_parse_object(JsonParser* jp) { + if (jp->p < jp->end && *jp->p == '{') jp->p++; + el_val_t m = el_map_new(0); + jp_skip_ws(jp); + if (jp->p < jp->end && *jp->p == '}') { jp->p++; return m; } + while (jp->p < jp->end) { + jp_skip_ws(jp); + char* key = jp_parse_string_raw(jp); + jp_skip_ws(jp); + if (jp->p < jp->end && *jp->p == ':') jp->p++; + else { jp->err = 1; free(key); break; } + jp_skip_ws(jp); + el_val_t v = jp_parse_value(jp); + m = el_map_set(m, EL_STR(key), v); + jp_skip_ws(jp); + if (jp->p < jp->end && *jp->p == ',') { jp->p++; continue; } + if (jp->p < jp->end && *jp->p == '}') { jp->p++; break; } + jp->err = 1; + break; + } + return m; +} + +static el_val_t jp_parse_value(JsonParser* jp) { + jp_skip_ws(jp); + if (jp->p >= jp->end) { jp->err = 1; return EL_NULL; } + char c = *jp->p; + if (c == '"') return el_wrap_str(jp_parse_string_raw(jp)); + if (c == '{') return jp_parse_object(jp); + if (c == '[') return jp_parse_array(jp); + if (c == '-' || isdigit((unsigned char)c)) return jp_parse_number(jp); + if (c == 't' && jp->p + 4 <= jp->end && strncmp(jp->p, "true", 4) == 0) { jp->p += 4; return 1; } + if (c == 'f' && jp->p + 5 <= jp->end && strncmp(jp->p, "false", 5) == 0) { jp->p += 5; return 0; } + if (c == 'n' && jp->p + 4 <= jp->end && strncmp(jp->p, "null", 4) == 0) { jp->p += 4; return EL_NULL; } + jp->err = 1; + return EL_NULL; +} + +el_val_t json_parse(el_val_t sv) { + const char* s = EL_CSTR(sv); + if (!s) return EL_NULL; + JsonParser jp = { .p = s, .end = s + strlen(s), .err = 0 }; + el_val_t v = jp_parse_value(&jp); + if (jp.err) return EL_NULL; + return v; +} + +/* ── JSON stringify ──────────────────────────────────────────────────────── */ +/* + * Stringify policy: el_val_t is type-erased, so we cannot perfectly + * round-trip arbitrary values. We use these heuristics: + * - If value is an ElList pointer (in the heap range), serialize as array. + * - If value is an ElMap pointer, serialize as object. + * - If value looks like a printable string pointer, serialize as string. + * - Otherwise serialize as integer. + * This is best-effort. Programs that need exact control should build the + * string directly. A pointer test is the cheapest way to disambiguate + * from small integers without a separate type tag. + */ + +/* JsonBuf struct is forward-declared near the HTTP section so HTTP helpers + * can use it. Its definition appears there. */ + +static void jb_init(JsonBuf* b) { + b->cap = 64; b->len = 0; + b->buf = malloc(b->cap); + if (!b->buf) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + b->buf[0] = '\0'; +} + +static void jb_reserve(JsonBuf* b, size_t add) { + if (b->len + add + 1 > b->cap) { + while (b->len + add + 1 > b->cap) b->cap *= 2; + b->buf = realloc(b->buf, b->cap); + if (!b->buf) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + } +} + +static void jb_putc(JsonBuf* b, char c) { + jb_reserve(b, 1); + b->buf[b->len++] = c; + b->buf[b->len] = '\0'; +} + +static void jb_puts(JsonBuf* b, const char* s) { + size_t n = strlen(s); + jb_reserve(b, n); + memcpy(b->buf + b->len, s, n); + b->len += n; + b->buf[b->len] = '\0'; +} + +static void jb_emit_escaped(JsonBuf* b, const char* s) { + jb_putc(b, '"'); + for (; *s; s++) { + unsigned char c = (unsigned char)*s; + switch (c) { + case '"': jb_puts(b, "\\\""); break; + case '\\': jb_puts(b, "\\\\"); break; + case '\b': jb_puts(b, "\\b"); break; + case '\f': jb_puts(b, "\\f"); break; + case '\n': jb_puts(b, "\\n"); break; + case '\r': jb_puts(b, "\\r"); break; + case '\t': jb_puts(b, "\\t"); break; + default: + if (c < 0x20) { + char tmp[8]; + snprintf(tmp, sizeof(tmp), "\\u%04x", c); + jb_puts(b, tmp); + } else { + jb_putc(b, (char)c); + } + break; + } + } + jb_putc(b, '"'); +} + +/* Heuristic: is this el_val_t likely a pointer to an ElList? + * We can't fully verify, but pointers are large addresses, integers small. + * Treat values whose magnitude exceeds 2^32 as potential pointers and + * sniff by reading the header conservatively. + * + * Simpler heuristic: if the value reads as a printable string, treat as + * string; otherwise as integer. Lists/Maps are encoded as struct pointers, + * which have leading binary bytes — so they won't look like strings. */ + +static int looks_like_string(el_val_t v) { + if (v == 0) return 0; + /* Treat plausible heap addresses as candidates. + * Threshold: 4 GiB (0x100000000). On 64-bit systems heap addresses from + * malloc/mmap start well above 4 GiB (ASLR pushes them to ~0x7f...). + * El integer values (counters, unix timestamps up to ~2106) all fit below + * 0x100000000 (4294967296). The old threshold of 1,000,000 caused unix + * timestamps (~1.7e9) to be misidentified as string pointers — a segfault + * risk in json_stringify and jb_emit_value. */ + uintptr_t p = (uintptr_t)v; + if (p < 0x100000000ULL) return 0; /* integers, timestamps, counters */ + if (p < 0x1000) return 0; + /* Sniff first bytes for printable */ + const unsigned char* s = (const unsigned char*)p; + for (int i = 0; i < 16; i++) { + unsigned char c = s[i]; + if (c == '\0') return i > 0; /* terminated string */ + if (c < 0x09 || (c > 0x0d && c < 0x20) || c >= 0x7f) return 0; + } + return 1; /* 16+ printable bytes — call it a string */ +} + +static void jb_emit_value(JsonBuf* b, el_val_t v); + +static void jb_emit_int(JsonBuf* b, int64_t n) { + char tmp[32]; + snprintf(tmp, sizeof(tmp), "%lld", (long long)n); + jb_puts(b, tmp); +} + +static void jb_emit_value(JsonBuf* b, el_val_t v) { + if (v == EL_NULL) { jb_puts(b, "null"); return; } + if (looks_like_string(v)) { + jb_emit_escaped(b, EL_CSTR(v)); + return; + } + jb_emit_int(b, (int64_t)v); +} + +el_val_t json_stringify(el_val_t v) { + JsonBuf b; jb_init(&b); + jb_emit_value(&b, v); + return el_wrap_str(b.buf); +} + +/* ── JSON substring accessors ────────────────────────────────────────────── */ +/* + * These walk the raw JSON string looking for "key": at the top level (depth 1) + * of an object. They handle escaped quotes, nested objects/arrays, and + * whitespace around the colon. + */ + +/* Find "key": at object-depth == 1 inside the JSON object string `s`. + * Returns pointer to the first byte of the value, or NULL. */ +static const char* json_find_key(const char* s, const char* key) { + if (!s || !key) return NULL; + size_t klen = strlen(key); + int depth = 0; + int in_str = 0; + int escape = 0; + const char* p = s; + while (*p) { + char c = *p; + if (in_str) { + if (escape) { escape = 0; } + else if (c == '\\') { escape = 1; } + else if (c == '"') { + /* End of string. If we're at depth 1, check if this was a key. */ + p++; + if (depth == 1) { + /* The string just ended at p-1. Check if it matches key + * and is followed by a colon. We need to backtrack to find + * the start of this string and compare. */ + } + in_str = 0; + continue; + } + p++; + continue; + } + if (c == '"') { + /* Start of a string literal */ + const char* str_start = p + 1; + const char* q = str_start; + int e = 0; + while (*q) { + if (e) { e = 0; q++; continue; } + if (*q == '\\') { e = 1; q++; continue; } + if (*q == '"') break; + q++; + } + size_t slen = (size_t)(q - str_start); + const char* after = (*q == '"') ? q + 1 : q; + /* If at depth 1 and matches key and followed by ':' -> got it */ + if (depth == 1 && slen == klen && strncmp(str_start, key, klen) == 0) { + const char* r = after; + while (*r == ' ' || *r == '\t' || *r == '\n' || *r == '\r') r++; + if (*r == ':') { + r++; + while (*r == ' ' || *r == '\t' || *r == '\n' || *r == '\r') r++; + return r; + } + } + p = after; + continue; + } + if (c == '{' || c == '[') depth++; + else if (c == '}' || c == ']') depth--; + p++; + } + return NULL; +} + +/* Skip a JSON value starting at p; return pointer past the value end. */ +static const char* json_skip_value(const char* p) { + if (!p || !*p) return p; + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + if (*p == '"') { + p++; + int e = 0; + while (*p) { + if (e) { e = 0; p++; continue; } + if (*p == '\\') { e = 1; p++; continue; } + if (*p == '"') { p++; break; } + p++; + } + return p; + } + if (*p == '{' || *p == '[') { + char open = *p; + char close = (open == '{') ? '}' : ']'; + int depth = 0; + int in_str = 0; + int e = 0; + while (*p) { + char c = *p; + if (in_str) { + if (e) { e = 0; } + else if (c == '\\') { e = 1; } + else if (c == '"') in_str = 0; + p++; + continue; + } + if (c == '"') { in_str = 1; p++; continue; } + if (c == open) depth++; + else if (c == close) { depth--; p++; if (depth == 0) return p; continue; } + p++; + } + return p; + } + /* scalar: number, true/false/null */ + while (*p && *p != ',' && *p != '}' && *p != ']' && + *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; + return p; +} + +el_val_t json_get_string(el_val_t json_str, el_val_t key) { + const char* json = EL_CSTR(json_str); + const char* k = EL_CSTR(key); + const char* p = json_find_key(json, k); + if (!p || *p != '"') return el_wrap_str(el_strdup("")); + p++; + JsonParser jp = { .p = p - 1, .end = json + (json ? strlen(json) : 0), .err = 0 }; + char* parsed = jp_parse_string_raw(&jp); + if (jp.err) { free(parsed); return el_wrap_str(el_strdup("")); } + return el_wrap_str(parsed); +} + +el_val_t json_get_int(el_val_t json_str, el_val_t key) { + const char* json = EL_CSTR(json_str); + const char* k = EL_CSTR(key); + const char* p = json_find_key(json, k); + if (!p) return 0; + if (*p == '"' || *p == '{' || *p == '[') return 0; + return (el_val_t)strtoll(p, NULL, 10); +} + +el_val_t json_get_float(el_val_t json_str, el_val_t key) { + const char* json = EL_CSTR(json_str); + const char* k = EL_CSTR(key); + const char* p = json_find_key(json, k); + if (!p) return 0; + if (*p == '"' || *p == '{' || *p == '[') return 0; + return el_from_float(strtod(p, NULL)); +} + +el_val_t json_get_bool(el_val_t json_str, el_val_t key) { + const char* json = EL_CSTR(json_str); + const char* k = EL_CSTR(key); + const char* p = json_find_key(json, k); + if (!p) return 0; + if (strncmp(p, "true", 4) == 0) return 1; + return 0; +} + +el_val_t json_get_raw(el_val_t json_str, el_val_t key) { + const char* json = EL_CSTR(json_str); + const char* k = EL_CSTR(key); + const char* p = json_find_key(json, k); + if (!p) return el_wrap_str(el_strdup("")); + const char* end = json_skip_value(p); + size_t n = (size_t)(end - p); + char* out = el_strbuf(n); + memcpy(out, p, n); + out[n] = '\0'; + return el_wrap_str(out); +} + +el_val_t json_set(el_val_t json_str, el_val_t key, el_val_t value) { + const char* json = EL_CSTR(json_str); + const char* k = EL_CSTR(key); + if (!k) k = ""; + if (!json || !*json) { + /* Build a fresh object */ + JsonBuf b; jb_init(&b); + jb_putc(&b, '{'); + jb_emit_escaped(&b, k); + jb_putc(&b, ':'); + jb_emit_value(&b, value); + jb_putc(&b, '}'); + return el_wrap_str(b.buf); + } + const char* existing = json_find_key(json, k); + JsonBuf b; jb_init(&b); + if (existing) { + const char* end = json_skip_value(existing); + /* Copy [json .. existing) */ + size_t prefix = (size_t)(existing - json); + jb_reserve(&b, prefix); + memcpy(b.buf + b.len, json, prefix); + b.len += prefix; + b.buf[b.len] = '\0'; + jb_emit_value(&b, value); + jb_puts(&b, end); + return el_wrap_str(b.buf); + } + /* Insert before closing '}'. Find last '}' */ + size_t jl = strlen(json); + if (jl == 0) { free(b.buf); return el_wrap_str(el_strdup("{}")); } + /* Find last '}' from the end */ + ssize_t close_idx = -1; + for (ssize_t i = (ssize_t)jl - 1; i >= 0; i--) { + if (json[i] == '}') { close_idx = i; break; } + } + if (close_idx < 0) { + free(b.buf); + return el_wrap_str(el_strdup(json)); + } + /* Determine if object is empty: scan between last '{' and '}' for non-ws */ + int empty = 1; + for (ssize_t i = close_idx - 1; i >= 0; i--) { + char c = json[i]; + if (c == '{') break; + if (c != ' ' && c != '\t' && c != '\n' && c != '\r') { empty = 0; break; } + } + /* Copy json[0..close_idx) */ + jb_reserve(&b, (size_t)close_idx); + memcpy(b.buf + b.len, json, (size_t)close_idx); + b.len += (size_t)close_idx; + b.buf[b.len] = '\0'; + if (!empty) jb_putc(&b, ','); + jb_emit_escaped(&b, k); + jb_putc(&b, ':'); + jb_emit_value(&b, value); + /* Append from close_idx onward */ + jb_puts(&b, json + close_idx); + return el_wrap_str(b.buf); +} + +el_val_t json_array_len(el_val_t json_str) { + const char* s = EL_CSTR(json_str); + if (!s) return 0; + while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; + if (*s != '[') return 0; + s++; + while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; + if (*s == ']') return 0; + int64_t count = 0; + while (*s) { + const char* end = json_skip_value(s); + if (end == s) break; + count++; + s = end; + while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; + if (*s == ',') { s++; continue; } + if (*s == ']' || *s == '\0') break; + } + return (el_val_t)count; +} + +/* json_array_get — return the i-th element of a JSON array as a JSON + * fragment string. Nested objects and arrays are returned verbatim + * (json_skip_value tracks brace/bracket depth so nested structures are + * preserved intact). Out-of-range index → "". */ +el_val_t json_array_get(el_val_t json_str, el_val_t index) { + const char* s = EL_CSTR(json_str); + int64_t idx = (int64_t)index; + if (!s || idx < 0) return el_wrap_str(el_strdup("")); + while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; + if (*s != '[') return el_wrap_str(el_strdup("")); + s++; + while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; + if (*s == ']') return el_wrap_str(el_strdup("")); + int64_t i = 0; + while (*s) { + const char* start = s; + const char* end = json_skip_value(s); + if (end == s) break; + if (i == idx) { + size_t n = (size_t)(end - start); + char* out = el_strbuf(n); + memcpy(out, start, n); + out[n] = '\0'; + return el_wrap_str(out); + } + i++; + s = end; + while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; + if (*s == ',') { s++; while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; continue; } + if (*s == ']' || *s == '\0') break; + } + return el_wrap_str(el_strdup("")); +} + +/* json_array_get_string — same as json_array_get, but assume the element + * is a JSON string and return the unquoted/unescaped value. Non-string + * elements yield "". */ +el_val_t json_array_get_string(el_val_t json_str, el_val_t index) { + el_val_t raw = json_array_get(json_str, index); + const char* s = EL_CSTR(raw); + if (!s || *s != '"') return el_wrap_str(el_strdup("")); + JsonParser jp = { + .p = s, + .end = s + strlen(s), + .err = 0, + }; + char* parsed = jp_parse_string_raw(&jp); + if (jp.err) { + free(parsed); + return el_wrap_str(el_strdup("")); + } + return el_wrap_str(parsed); +} + +/* ── Time ────────────────────────────────────────────────────────────────── */ + +el_val_t time_now(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + int64_t ms = (int64_t)tv.tv_sec * 1000LL + (int64_t)tv.tv_usec / 1000LL; + return (el_val_t)ms; +} + +el_val_t time_now_utc(void) { + return time_now(); +} + +el_val_t time_format(el_val_t ts, el_val_t fmt) { + int64_t ms = (int64_t)ts; + time_t s = (time_t)(ms / 1000); + int msec = (int)(ms % 1000); + if (msec < 0) { msec += 1000; s -= 1; } + struct tm tm; + gmtime_r(&s, &tm); + const char* fmt_str = EL_CSTR(fmt); + if (!fmt_str || strcmp(fmt_str, "ISO") == 0) { + char buf[64]; + snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec, msec); + return el_wrap_str(el_strdup(buf)); + } + char buf[256]; + if (strftime(buf, sizeof(buf), fmt_str, &tm) == 0) buf[0] = '\0'; + return el_wrap_str(el_strdup(buf)); +} + +el_val_t time_to_parts(el_val_t ts) { + int64_t ms = (int64_t)ts; + time_t s = (time_t)(ms / 1000); + int msec = (int)(ms % 1000); + if (msec < 0) { msec += 1000; s -= 1; } + struct tm tm; + gmtime_r(&s, &tm); + el_val_t m = el_map_new(0); + m = el_map_set(m, EL_STR(el_strdup("year")), (el_val_t)(tm.tm_year + 1900)); + m = el_map_set(m, EL_STR(el_strdup("month")), (el_val_t)(tm.tm_mon + 1)); + m = el_map_set(m, EL_STR(el_strdup("day")), (el_val_t)tm.tm_mday); + m = el_map_set(m, EL_STR(el_strdup("hour")), (el_val_t)tm.tm_hour); + m = el_map_set(m, EL_STR(el_strdup("minute")), (el_val_t)tm.tm_min); + m = el_map_set(m, EL_STR(el_strdup("second")), (el_val_t)tm.tm_sec); + m = el_map_set(m, EL_STR(el_strdup("ms")), (el_val_t)msec); + return m; +} + +el_val_t time_from_parts(el_val_t secs, el_val_t ns, el_val_t tz) { + (void)tz; + int64_t s = (int64_t)secs; + int64_t n = (int64_t)ns; + int64_t ms = s * 1000LL + n / 1000000LL; + return (el_val_t)ms; +} + +el_val_t time_add(el_val_t ts, el_val_t n, el_val_t unit) { + const char* u = EL_CSTR(unit); + int64_t cur = (int64_t)ts; + int64_t d = (int64_t)n; + int64_t add_ms = d; + if (u) { + if (strcmp(u, "ms") == 0) add_ms = d; + else if (strcmp(u, "sec") == 0) add_ms = d * 1000LL; + else if (strcmp(u, "min") == 0) add_ms = d * 60000LL; + else if (strcmp(u, "hour") == 0) add_ms = d * 3600000LL; + else if (strcmp(u, "day") == 0) add_ms = d * 86400000LL; + } + return (el_val_t)(cur + add_ms); +} + +el_val_t time_diff(el_val_t ts1, el_val_t ts2, el_val_t unit) { + int64_t d = (int64_t)ts2 - (int64_t)ts1; + const char* u = EL_CSTR(unit); + if (!u || strcmp(u, "ms") == 0) return (el_val_t)d; + if (strcmp(u, "sec") == 0) return (el_val_t)(d / 1000LL); + if (strcmp(u, "min") == 0) return (el_val_t)(d / 60000LL); + if (strcmp(u, "hour") == 0) return (el_val_t)(d / 3600000LL); + if (strcmp(u, "day") == 0) return (el_val_t)(d / 86400000LL); + return (el_val_t)d; +} + +/* Block the calling thread for `secs` seconds. Negative values are clamped + * to 0. Used by El programs that poll external resources (e.g. RunPod + * /status, Engram readiness probes). */ +el_val_t sleep_secs(el_val_t secs) { + int64_t s = (int64_t)secs; + if (s < 0) s = 0; + struct timespec ts; + ts.tv_sec = (time_t)s; + ts.tv_nsec = 0; + nanosleep(&ts, NULL); + return 0; +} + +el_val_t sleep_ms(el_val_t ms) { + int64_t m = (int64_t)ms; + if (m < 0) m = 0; + struct timespec ts; + ts.tv_sec = (time_t)(m / 1000LL); + ts.tv_nsec = (long)((m % 1000LL) * 1000000LL); + nanosleep(&ts, NULL); + return 0; +} + +/* ── UUID v4 ─────────────────────────────────────────────────────────────── */ + +static int _el_uuid_seeded = 0; + +static void _el_uuid_seed(void) { + if (!_el_uuid_seeded) { + srand((unsigned)time(NULL) ^ (unsigned)(uintptr_t)&_el_uuid_seeded); + _el_uuid_seeded = 1; + } +} + +el_val_t uuid_new(void) { + _el_uuid_seed(); + unsigned char b[16]; + for (int i = 0; i < 16; i++) b[i] = (unsigned char)(rand() & 0xff); + /* Version 4 */ + b[6] = (b[6] & 0x0f) | 0x40; + /* RFC 4122 variant */ + b[8] = (b[8] & 0x3f) | 0x80; + char buf[37]; + snprintf(buf, sizeof(buf), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + b[0], b[1], b[2], b[3], + b[4], b[5], + b[6], b[7], + b[8], b[9], + b[10], b[11], b[12], b[13], b[14], b[15]); + return el_wrap_str(el_strdup(buf)); +} + +el_val_t uuid_v4(void) { return uuid_new(); } + +/* ── Environment ─────────────────────────────────────────────────────────── */ + +el_val_t env(el_val_t key) { + const char* k = EL_CSTR(key); + if (!k) return el_wrap_str(el_strdup("")); + const char* v = getenv(k); + return el_wrap_str(el_strdup(v ? v : "")); +} + +/* ── In-process state K/V ────────────────────────────────────────────────── */ + +typedef struct { + char* key; + char* value; +} StateEntry; + +static StateEntry* _state_entries = NULL; +static size_t _state_count = 0; +static size_t _state_cap = 0; +/* Mutex protecting all _state_entries access. state_set/state_get are called + * concurrently from 64 HTTP worker threads — without this lock, realloc and + * free race, producing corruption, double-free, and segfaults. */ +static pthread_mutex_t _state_mu = PTHREAD_MUTEX_INITIALIZER; + +static StateEntry* state_find(const char* key) { + for (size_t i = 0; i < _state_count; i++) { + if (strcmp(_state_entries[i].key, key) == 0) return &_state_entries[i]; + } + return NULL; +} + +el_val_t state_set(el_val_t key, el_val_t value) { + const char* k = EL_CSTR(key); + const char* v = EL_CSTR(value); + if (!k) return 0; + if (!v) v = ""; + pthread_mutex_lock(&_state_mu); + StateEntry* e = state_find(k); + if (e) { + free(e->value); + e->value = el_strdup_persist(v); + pthread_mutex_unlock(&_state_mu); + return 1; + } + if (_state_count >= _state_cap) { + size_t nc = _state_cap == 0 ? 16 : _state_cap * 2; + StateEntry* grown = realloc(_state_entries, nc * sizeof(StateEntry)); + if (!grown) { pthread_mutex_unlock(&_state_mu); fputs("el_runtime: out of memory\n", stderr); exit(1); } + _state_entries = grown; + _state_cap = nc; + } + _state_entries[_state_count].key = el_strdup_persist(k); + _state_entries[_state_count].value = el_strdup_persist(v); + _state_count++; + pthread_mutex_unlock(&_state_mu); + return 1; +} + +el_val_t state_get(el_val_t key) { + const char* k = EL_CSTR(key); + if (!k) return el_wrap_str(el_strdup("")); + pthread_mutex_lock(&_state_mu); + StateEntry* e = state_find(k); + char* result = el_strdup_persist(e ? e->value : ""); + pthread_mutex_unlock(&_state_mu); + /* wrap in arena-tracked copy for the caller's request lifetime */ + char* copy = el_strdup(result); + return el_wrap_str(copy); +} + +el_val_t state_del(el_val_t key) { + const char* k = EL_CSTR(key); + if (!k) return 0; + pthread_mutex_lock(&_state_mu); + for (size_t i = 0; i < _state_count; i++) { + if (strcmp(_state_entries[i].key, k) == 0) { + free(_state_entries[i].key); + free(_state_entries[i].value); + for (size_t j = i + 1; j < _state_count; j++) { + _state_entries[j - 1] = _state_entries[j]; + } + _state_count--; + pthread_mutex_unlock(&_state_mu); + return 1; + } + } + pthread_mutex_unlock(&_state_mu); + return 1; +} + +el_val_t state_keys(void) { + pthread_mutex_lock(&_state_mu); + el_val_t lst = el_list_empty(); + for (size_t i = 0; i < _state_count; i++) { + lst = el_list_append(lst, el_wrap_str(el_strdup(_state_entries[i].key))); + } + pthread_mutex_unlock(&_state_mu); + return lst; +} + +/* ── Float formatting ────────────────────────────────────────────────────── */ + +el_val_t float_to_str(el_val_t f) { + char buf[64]; + snprintf(buf, sizeof(buf), "%g", el_to_float(f)); + return el_wrap_str(el_strdup(buf)); +} + +el_val_t int_to_float(el_val_t n) { + return el_from_float((double)(int64_t)n); +} + +el_val_t float_to_int(el_val_t f) { + return (el_val_t)(int64_t)el_to_float(f); +} + +el_val_t format_float(el_val_t f, el_val_t decimals) { + int d = (int)(int64_t)decimals; + if (d < 0) d = 0; + if (d > 30) d = 30; + char buf[128]; + snprintf(buf, sizeof(buf), "%.*f", d, el_to_float(f)); + return el_wrap_str(el_strdup(buf)); +} + +el_val_t decimal_round(el_val_t f, el_val_t decimals) { + int d = (int)(int64_t)decimals; + if (d < 0) d = 0; + if (d > 15) d = 15; + double mul = pow(10.0, (double)d); + double v = el_to_float(f); + double r = (v >= 0.0 ? floor(v * mul + 0.5) : -floor(-v * mul + 0.5)) / mul; + return el_from_float(r); +} + +el_val_t str_to_float(el_val_t s) { + const char* str = EL_CSTR(s); + if (!str) return el_from_float(0.0); + return el_from_float(strtod(str, NULL)); +} + +/* ── Math (Float-aware) ──────────────────────────────────────────────────── */ + +el_val_t math_sqrt(el_val_t f) { return el_from_float(sqrt(el_to_float(f))); } +el_val_t math_log(el_val_t f) { return el_from_float(log(el_to_float(f))); } +el_val_t math_ln(el_val_t f) { return el_from_float(log(el_to_float(f))); } +el_val_t math_sin(el_val_t f) { return el_from_float(sin(el_to_float(f))); } +el_val_t math_cos(el_val_t f) { return el_from_float(cos(el_to_float(f))); } +el_val_t math_pi(void) { return el_from_float(3.141592653589793238462643383279502884); } + +/* ── String additions ────────────────────────────────────────────────────── */ + +el_val_t str_index_of(el_val_t s, el_val_t sub) { + const char* str = EL_CSTR(s); + const char* sb = EL_CSTR(sub); + if (!str || !sb) return -1; + const char* hit = strstr(str, sb); + if (!hit) return -1; + return (el_val_t)(int64_t)(hit - str); +} + +el_val_t str_split(el_val_t s, el_val_t sep) { + const char* str = EL_CSTR(s); + const char* sp = EL_CSTR(sep); + el_val_t lst = el_list_empty(); + if (!str) return lst; + if (!sp || !*sp) { + lst = el_list_append(lst, el_wrap_str(el_strdup(str))); + return lst; + } + size_t lp = strlen(sp); + const char* p = str; + const char* hit; + while ((hit = strstr(p, sp)) != NULL) { + size_t n = (size_t)(hit - p); + char* out = el_strbuf(n); + memcpy(out, p, n); + out[n] = '\0'; + lst = el_list_append(lst, el_wrap_str(out)); + p = hit + lp; + } + lst = el_list_append(lst, el_wrap_str(el_strdup(p))); + return lst; +} + +el_val_t str_char_at(el_val_t s, el_val_t i) { + const char* str = EL_CSTR(s); + int64_t idx = (int64_t)i; + if (!str) return el_wrap_str(el_strdup("")); + int64_t n = (int64_t)strlen(str); + if (idx < 0 || idx >= n) return el_wrap_str(el_strdup("")); + char buf[2]; + buf[0] = str[idx]; + buf[1] = '\0'; + return el_wrap_str(el_strdup(buf)); +} + +el_val_t str_char_code(el_val_t s, el_val_t i) { + const char* str = EL_CSTR(s); + int64_t idx = (int64_t)i; + if (!str) return 0; + int64_t n = (int64_t)strlen(str); + if (idx < 0 || idx >= n) return 0; + return (el_val_t)(unsigned char)str[idx]; +} + +static el_val_t str_pad(const char* s, int64_t width, const char* pad, int left) { + if (!s) s = ""; + if (!pad || !*pad) pad = " "; + int64_t lp = (int64_t)strlen(pad); + int64_t ls = (int64_t)strlen(s); + if (ls >= width) return el_wrap_str(el_strdup(s)); + int64_t need = width - ls; + char* out = el_strbuf((size_t)width); + if (left) { + for (int64_t i = 0; i < need; i++) out[i] = pad[i % lp]; + memcpy(out + need, s, (size_t)ls); + } else { + memcpy(out, s, (size_t)ls); + for (int64_t i = 0; i < need; i++) out[ls + i] = pad[i % lp]; + } + out[width] = '\0'; + return el_wrap_str(out); +} + +el_val_t str_pad_left(el_val_t s, el_val_t width, el_val_t pad) { + return str_pad(EL_CSTR(s), (int64_t)width, EL_CSTR(pad), 1); +} + +el_val_t str_pad_right(el_val_t s, el_val_t width, el_val_t pad) { + return str_pad(EL_CSTR(s), (int64_t)width, EL_CSTR(pad), 0); +} + +el_val_t str_format(el_val_t template, el_val_t data) { + const char* tpl = EL_CSTR(template); + if (!tpl) return el_wrap_str(el_strdup("")); + JsonBuf b; jb_init(&b); + const char* p = tpl; + while (*p) { + if (*p == '{') { + const char* q = p + 1; + while (*q && *q != '}') q++; + if (*q == '}') { + size_t klen = (size_t)(q - p - 1); + char keybuf[256]; + if (klen < sizeof(keybuf)) { + memcpy(keybuf, p + 1, klen); + keybuf[klen] = '\0'; + el_val_t v = el_map_get(data, EL_STR(keybuf)); + if (v != 0 && looks_like_string(v)) { + jb_puts(&b, EL_CSTR(v)); + p = q + 1; + continue; + } else if (v != 0) { + jb_emit_int(&b, (int64_t)v); + p = q + 1; + continue; + } + } + /* Unknown key — leave {key} verbatim */ + jb_reserve(&b, klen + 2); + memcpy(b.buf + b.len, p, klen + 2); + b.len += klen + 2; + b.buf[b.len] = '\0'; + p = q + 1; + continue; + } + } + jb_putc(&b, *p); + p++; + } + return el_wrap_str(b.buf); +} + +el_val_t str_lower(el_val_t s) { return str_to_lower(s); } +el_val_t str_upper(el_val_t s) { return str_to_upper(s); } + +/* ── List additions ──────────────────────────────────────────────────────── */ + +el_val_t list_push(el_val_t list, el_val_t elem) { + return el_list_append(list, elem); +} + +el_val_t list_push_front(el_val_t listv, el_val_t elem) { + ElList* lst = (ElList*)(uintptr_t)listv; + if (!lst) { + el_val_t nl = el_list_empty(); + return el_list_append(nl, elem); + } + /* Append to grow capacity, then shift right */ + listv = el_list_append(listv, elem); + lst = (ElList*)(uintptr_t)listv; + for (int64_t i = lst->length - 1; i > 0; i--) { + lst->elems[i] = lst->elems[i - 1]; + } + lst->elems[0] = elem; + return EL_STR(lst); +} + +el_val_t list_join(el_val_t listv, el_val_t sep) { + ElList* lst = (ElList*)(uintptr_t)listv; + const char* sp = EL_CSTR(sep); + if (!sp) sp = ""; + if (!lst || lst->length == 0) return el_wrap_str(el_strdup("")); + JsonBuf b; jb_init(&b); + for (int64_t i = 0; i < lst->length; i++) { + if (i > 0) jb_puts(&b, sp); + el_val_t v = lst->elems[i]; + if (v == 0) continue; + if (looks_like_string(v)) { + jb_puts(&b, EL_CSTR(v)); + } else { + char tmp[32]; + snprintf(tmp, sizeof(tmp), "%lld", (long long)v); + jb_puts(&b, tmp); + } + } + return el_wrap_str(b.buf); +} + +el_val_t list_range(el_val_t start, el_val_t end) { + int64_t a = (int64_t)start; + int64_t b = (int64_t)end; + el_val_t lst = el_list_empty(); + for (int64_t i = a; i < b; i++) lst = el_list_append(lst, (el_val_t)i); + return lst; +} + +/* ── Bool helpers ────────────────────────────────────────────────────────── */ + +el_val_t bool_to_str(el_val_t b) { + return el_wrap_str(el_strdup(b ? "true" : "false")); +} + +/* ── Numeric parsing ─────────────────────────────────────────────────────── */ + +/* parse_int — strtoll with a default. str_to_int already exists but does not + * distinguish "0" from a parse failure, so callers that need a sentinel use + * this. Skips leading whitespace; accepts an optional leading +/-; returns + * default_val on empty input or no consumed digits. Trailing junk is ignored + * (atoi-style). */ +el_val_t parse_int(el_val_t sv, el_val_t default_val) { + const char* s = EL_CSTR(sv); + if (!s) return default_val; + while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++; + if (*s == '\0') return default_val; + char* end = NULL; + long long n = strtoll(s, &end, 10); + if (end == s) return default_val; + return (el_val_t)n; +} + +/* ── Process ─────────────────────────────────────────────────────────────── */ + +void exit_program(el_val_t code) { + exit((int)code); +} + +/* getpid_now — current process id. Named with the _now suffix to avoid + * colliding with the libc `getpid` declaration that the runtime already + * sees via (calling it `getpid` would fight the prototype). */ +el_val_t getpid_now(void) { + return (el_val_t)getpid(); +} + +/* ── args() — command-line argument access ────────────────────────────────── + * Compiled El programs call args() to get a list of CLI arguments. + * Call el_runtime_init_args(argc, argv) at the start of C main() to populate. + * The args list excludes argv[0] (the program name). */ + +static el_val_t _el_args_list = 0; + +void el_runtime_init_args(int argc, char** argv) { + _el_args_list = el_list_empty(); + for (int i = 1; i < argc; i++) { + _el_args_list = el_list_append(_el_args_list, EL_STR(argv[i])); + } +} + +el_val_t args(void) { + if (!_el_args_list) _el_args_list = el_list_empty(); + return _el_args_list; +} + +/* ── CGI identity ──────────────────────────────────────────────────────────── + * Called once at program start by the generated main() of a cgi {} program. + * Stores CGI identity so dharma_* builtins can reference it. */ + +static const char* _el_cgi_name = NULL; +static const char* _el_cgi_dharma_id = NULL; +static const char* _el_cgi_principal = NULL; +static const char* _el_cgi_network = NULL; +static const char* _el_cgi_engram = NULL; + +void el_cgi_init(el_val_t name, el_val_t dharma_id, el_val_t principal, + el_val_t network, el_val_t engram) { + _el_cgi_name = EL_CSTR(name); + _el_cgi_dharma_id = EL_CSTR(dharma_id); + _el_cgi_principal = EL_CSTR(principal); + _el_cgi_network = EL_CSTR(network) ? EL_CSTR(network) : "dharma-mainnet"; + _el_cgi_engram = EL_CSTR(engram) ? EL_CSTR(engram) : "http://localhost:8742"; + printf("[cgi] identity: name=%s dharma_id=%s principal=%s network=%s engram=%s\n", + _el_cgi_name ? _el_cgi_name : "(unset)", + _el_cgi_dharma_id ? _el_cgi_dharma_id : "(unset)", + _el_cgi_principal ? _el_cgi_principal : "(unset)", + _el_cgi_network, + _el_cgi_engram); +} + + +/* ── Batch 3: Engram in-process graph store ──────────────────────────────── */ +/* + * Single global EngramStore allocated lazily on first call. All node and + * edge content strings are owned (strdup'd) by the store. Linear arrays + * with doubling capacity for both nodes and edges. + * + * Two-layer activation algorithm (engram_activate): + * + * LAYER 1 — Broad fan-out (background activation): + * 1. Find seed nodes whose content/label/tags contain query (case-insens). + * 2. BFS up to `depth` hops along ALL edges (excitatory and inhibitory). + * Every reachable node fires — nothing is filtered at this layer. + * 3. bg_act = seed.salience * temporal_decay * dampening + * propagated as: new_bg = parent_bg * edge_weight * 0.7 * (1 + tbonus) + * where tbonus ∈ {0, 0.10, 0.20} for co-temporal nodes. + * 4. If reached by multiple paths, take max background_activation. + * 5. Persist background_activation to EngramNode.background_activation. + * + * LAYER 2 — Executive filter (working memory promotion): + * 6. For each inhibitory edge where source has background_activation > 0: + * inhibition[target] = max(bg[source] * e->weight) + * 7. For each background-activated node: + * raw_wm = bg * goal_bias(node, query) * confidence + * * (1 - (1 - INHIBITION_FACTOR) * inhibition) + * 8. Per-type threshold gate: raw_wm >= type_threshold → promoted. + * Safety/DharmaSelf: 0.05 Canonical: 0.15 Lesson: 0.25 + * Belief/Entity: 0.30 Note/Memory/Working: 0.40 + * 9. If not promoted: suppression_count++. After + * ENGRAM_SUPPRESSION_BREAKTHROUGH suppressions → force breakthrough + * at ENGRAM_BREAKTHROUGH_WEIGHT (latent tension surfacing). + * 10. Persist working_memory_weight to EngramNode.working_memory_weight. + * 11. Sort: promoted nodes (wm > 0) first by wm desc, then background- + * only by bg desc. Context compilation uses ONLY promoted nodes. + * + * Temporal decay: + * decay_factor = exp(-lambda * age_hours / T_half) + * T_half = 168.0 h (one week), lambda = ln(2) + * + * Activation dampening: + * dampen = 1.0 / (1.0 + log(1 + activation_count)) + * + * engram_query_range(start_ms, end_ms): + * Returns nodes whose created_at OR last_activated falls within + * [start_ms, end_ms], sorted by created_at ascending. + */ + +/* Temporal decay constants. + * T_HALF_HOURS: half-life in hours — one week. After one week of no + * activation a node retains 50% of its base salience contribution. + * DECAY_LAMBDA: ln(2) ≈ 0.693147 */ +#define ENGRAM_T_HALF_HOURS 168.0 +#define ENGRAM_DECAY_LAMBDA 0.693147 + +/* Two-layer activation constants. + * ENGRAM_WM_THRESHOLD: minimum background_activation for a node to be + * considered for working-memory promotion (layer 2 candidate gate). + * ENGRAM_WM_DECAY: per-turn decay applied to working_memory_weight for + * nodes NOT re-activated in the current turn (conversational thread + * continuity: a node promoted in turn N persists with reduced weight + * into turn N+1 without re-activation cost). + * ENGRAM_SUPPRESSION_BREAKTHROUGH: after this many consecutive suppressions + * a latent node forces itself into working memory at reduced weight, + * modelling the brain's "intrusive thought" / unresolved-tension surfacing. + * ENGRAM_BREAKTHROUGH_WEIGHT: the reduced working_memory_weight assigned + * when a suppressed node breaks through. + * ENGRAM_INHIBITION_FACTOR: multiplier applied to working_memory_weight when + * an inhibitory edge fires against a node (0 = full suppress, 0.3 = partial). */ +#define ENGRAM_WM_THRESHOLD 0.15 +#define ENGRAM_WM_DECAY 0.7 +#define ENGRAM_SUPPRESSION_BREAKTHROUGH 5 +#define ENGRAM_BREAKTHROUGH_WEIGHT 0.25 +#define ENGRAM_INHIBITION_FACTOR 0.1 + +/* Per-node-type activation thresholds. + * Lower tier / safety-critical nodes fire more readily. */ +static double engram_type_threshold(const char* node_type, const char* tier) { + if (node_type) { + if (strcmp(node_type, "DharmaSelf") == 0) return 0.05; + if (strcmp(node_type, "Safety") == 0) return 0.05; + } + if (tier) { + if (strcmp(tier, "Canonical") == 0) return 0.15; + if (strcmp(tier, "Lesson") == 0) return 0.25; + } + if (node_type) { + if (strcmp(node_type, "Belief") == 0) return 0.30; + if (strcmp(node_type, "Entity") == 0) return 0.30; + } + return 0.40; /* Note / Memory / Working (most nodes) */ +} + +typedef struct EngramNode { + char* id; + char* content; + char* node_type; + char* label; + char* tier; + char* tags; + char* metadata; + double salience; + double importance; + double confidence; + double temporal_decay_rate; /* per-node override for lambda; 0 = use default */ + int64_t activation_count; + int64_t last_activated; + int64_t created_at; + int64_t updated_at; + /* Two-layer activation fields ───────────────────────────────────────── + * background_activation: Layer 1. Set by BFS fan-out on every query. + * Every reachable node fires here — nothing is filtered at this stage. + * Models the brain's massive parallel sub-threshold activation of all + * associated content in response to a stimulus. + * working_memory_weight: Layer 2. Executive filter output. Only nodes + * that survive goal-state / attentional-bias scoring receive a + * non-zero weight here. Context compilation ONLY uses this field. + * Background-activated nodes with working_memory_weight == 0 remain + * latent — real, available, but silent. + * suppression_count: Consecutive turn count where this node was + * background-activated but NOT promoted to working memory. High + * values signal the node "wants to surface." After + * ENGRAM_SUPPRESSION_BREAKTHROUGH consecutive suppressions the node + * is force-promoted at a reduced weight (breakthrough activation). */ + double background_activation; + double working_memory_weight; + int32_t suppression_count; +} EngramNode; + +typedef struct EngramEdge { + char* id; + char* from_id; + char* to_id; + char* relation; + char* metadata; + double weight; + double confidence; + int64_t created_at; + int64_t updated_at; + int64_t last_fired; + /* Inhibitory flag: when 1, activating the source node SUPPRESSES the + * working_memory_weight of the target node rather than exciting it. + * Models attentional inhibition: "I am focused on code work" creates + * inhibitory edges to personal/emotional nodes, preventing them from + * surfacing even if they have high background_activation. */ + int inhibitory; +} EngramEdge; + +typedef struct EngramStore { + EngramNode* nodes; + int64_t node_count; + int64_t node_capacity; + EngramEdge* edges; + int64_t edge_count; + int64_t edge_capacity; +} EngramStore; + +static EngramStore* engram_global = NULL; + +static EngramStore* engram_get(void) { + if (engram_global) return engram_global; + engram_global = calloc(1, sizeof(EngramStore)); + if (!engram_global) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + engram_global->node_capacity = 16; + engram_global->nodes = calloc((size_t)engram_global->node_capacity, sizeof(EngramNode)); + engram_global->edge_capacity = 16; + engram_global->edges = calloc((size_t)engram_global->edge_capacity, sizeof(EngramEdge)); + return engram_global; +} + +static int64_t engram_now_ms(void) { + struct timeval tv; gettimeofday(&tv, NULL); + return (int64_t)tv.tv_sec * 1000LL + (int64_t)tv.tv_usec / 1000LL; +} + +static EngramNode* engram_find_node(const char* id) { + if (!id) return NULL; + EngramStore* g = engram_get(); + for (int64_t i = 0; i < g->node_count; i++) { + if (g->nodes[i].id && strcmp(g->nodes[i].id, id) == 0) return &g->nodes[i]; + } + return NULL; +} + +static int64_t engram_find_node_index(const char* id) { + if (!id) return -1; + EngramStore* g = engram_get(); + for (int64_t i = 0; i < g->node_count; i++) { + if (g->nodes[i].id && strcmp(g->nodes[i].id, id) == 0) return i; + } + return -1; +} + +static void engram_grow_nodes(void) { + EngramStore* g = engram_get(); + if (g->node_count < g->node_capacity) return; + int64_t nc = g->node_capacity * 2; + g->nodes = realloc(g->nodes, (size_t)nc * sizeof(EngramNode)); + if (!g->nodes) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + memset(g->nodes + g->node_capacity, 0, + (size_t)(nc - g->node_capacity) * sizeof(EngramNode)); + g->node_capacity = nc; +} + +static void engram_grow_edges(void) { + EngramStore* g = engram_get(); + if (g->edge_count < g->edge_capacity) return; + int64_t nc = g->edge_capacity * 2; + g->edges = realloc(g->edges, (size_t)nc * sizeof(EngramEdge)); + if (!g->edges) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + memset(g->edges + g->edge_capacity, 0, + (size_t)(nc - g->edge_capacity) * sizeof(EngramEdge)); + g->edge_capacity = nc; +} + +/* Build a fresh UUID string. Reuses uuid_new but takes the underlying char*. */ +static char* engram_new_id(void) { + el_val_t v = uuid_new(); + const char* s = EL_CSTR(v); + return el_strdup(s ? s : ""); +} + +/* Convert a node into an ElMap of its fields. */ +static el_val_t engram_node_to_map(const EngramNode* n) { + el_val_t m = el_map_new(0); + m = el_map_set(m, EL_STR(el_strdup("id")), EL_STR(el_strdup(n->id ? n->id : ""))); + m = el_map_set(m, EL_STR(el_strdup("content")), EL_STR(el_strdup(n->content ? n->content : ""))); + m = el_map_set(m, EL_STR(el_strdup("node_type")), EL_STR(el_strdup(n->node_type ? n->node_type : ""))); + m = el_map_set(m, EL_STR(el_strdup("label")), EL_STR(el_strdup(n->label ? n->label : ""))); + m = el_map_set(m, EL_STR(el_strdup("tier")), EL_STR(el_strdup(n->tier ? n->tier : "Working"))); + m = el_map_set(m, EL_STR(el_strdup("tags")), EL_STR(el_strdup(n->tags ? n->tags : ""))); + m = el_map_set(m, EL_STR(el_strdup("metadata")), EL_STR(el_strdup(n->metadata ? n->metadata : "{}"))); + m = el_map_set(m, EL_STR(el_strdup("salience")), el_from_float(n->salience)); + m = el_map_set(m, EL_STR(el_strdup("importance")), el_from_float(n->importance)); + m = el_map_set(m, EL_STR(el_strdup("confidence")), el_from_float(n->confidence)); + m = el_map_set(m, EL_STR(el_strdup("temporal_decay_rate")), el_from_float(n->temporal_decay_rate)); + m = el_map_set(m, EL_STR(el_strdup("activation_count")), (el_val_t)n->activation_count); + m = el_map_set(m, EL_STR(el_strdup("last_activated")), (el_val_t)n->last_activated); + m = el_map_set(m, EL_STR(el_strdup("created_at")), (el_val_t)n->created_at); + m = el_map_set(m, EL_STR(el_strdup("updated_at")), (el_val_t)n->updated_at); + m = el_map_set(m, EL_STR(el_strdup("background_activation")), el_from_float(n->background_activation)); + m = el_map_set(m, EL_STR(el_strdup("working_memory_weight")), el_from_float(n->working_memory_weight)); + m = el_map_set(m, EL_STR(el_strdup("suppression_count")), (el_val_t)n->suppression_count); + return m; +} + +/* (Node JSON serialization is provided by `engram_emit_node_json` further + * down in the persistence section — reused by the *_json builtins below.) */ +static void engram_emit_node_json(JsonBuf* b, const EngramNode* n); +static void engram_emit_edge_json(JsonBuf* b, const EngramEdge* e); + +/* Salience may arrive either as a float bit-pattern or as a small integer + * (e.g. 1, meaning 1.0). Heuristic: if interpreted as double it's in + * [0.0, 100.0] use it; otherwise treat as int and convert. */ +static double engram_decode_score(el_val_t v) { + double f = el_to_float(v); + if (!isnan(f) && !isinf(f) && f >= 0.0 && f <= 100.0) return f; + int64_t n = (int64_t)v; + return (double)n; +} + +static char* engram_first_n_chars(const char* s, size_t n) { + if (!s) return el_strdup(""); + size_t l = strlen(s); + if (l > n) l = n; + char* out = el_strbuf(l); + memcpy(out, s, l); + out[l] = '\0'; + return out; +} + +el_val_t engram_node(el_val_t content, el_val_t node_type, el_val_t salience) { + EngramStore* g = engram_get(); + engram_grow_nodes(); + EngramNode* n = &g->nodes[g->node_count]; + memset(n, 0, sizeof(*n)); + n->id = engram_new_id(); + const char* c = EL_CSTR(content); + const char* nt = EL_CSTR(node_type); + n->content = el_strdup(c ? c : ""); + n->node_type = el_strdup(nt && *nt ? nt : "Memory"); + n->label = engram_first_n_chars(c, 60); + n->tier = el_strdup("Working"); + n->tags = el_strdup(""); + n->metadata = el_strdup("{}"); + n->salience = engram_decode_score(salience); + if (n->salience <= 0.0 || n->salience > 1.0) n->salience = 0.5; + n->importance = 0.5; + n->confidence = 1.0; + n->temporal_decay_rate = 0.0; /* 0 = use global default ENGRAM_DECAY_LAMBDA */ + n->activation_count = 0; + int64_t now = engram_now_ms(); + n->last_activated = now; + n->created_at = now; + n->updated_at = now; + g->node_count++; + return el_wrap_str(el_strdup(n->id)); +} + +el_val_t engram_node_full(el_val_t content, el_val_t node_type, el_val_t label, + el_val_t salience, el_val_t importance, el_val_t confidence, + el_val_t tier, el_val_t tags) { + EngramStore* g = engram_get(); + engram_grow_nodes(); + EngramNode* n = &g->nodes[g->node_count]; + memset(n, 0, sizeof(*n)); + n->id = engram_new_id(); + const char* c = EL_CSTR(content); + const char* nt = EL_CSTR(node_type); + const char* lb = EL_CSTR(label); + const char* ti = EL_CSTR(tier); + const char* tg = EL_CSTR(tags); + n->content = el_strdup(c ? c : ""); + n->node_type = el_strdup(nt && *nt ? nt : "Memory"); + n->label = el_strdup(lb && *lb ? lb : (c ? engram_first_n_chars(c, 60) : "")); + n->tier = el_strdup(ti && *ti ? ti : "Working"); + n->tags = el_strdup(tg ? tg : ""); + n->metadata = el_strdup("{}"); + n->salience = engram_decode_score(salience); + n->importance = engram_decode_score(importance); + n->confidence = engram_decode_score(confidence); + if (n->salience <= 0.0 || n->salience > 1.0) n->salience = 0.5; + if (n->importance <= 0.0 || n->importance > 1.0) n->importance = 0.5; + if (n->confidence <= 0.0 || n->confidence > 1.0) n->confidence = 1.0; + n->temporal_decay_rate = 0.0; /* 0 = use global default ENGRAM_DECAY_LAMBDA */ + n->activation_count = 0; + int64_t now = engram_now_ms(); + n->last_activated = now; + n->created_at = now; + n->updated_at = now; + g->node_count++; + return el_wrap_str(el_strdup(n->id)); +} + +el_val_t engram_get_node(el_val_t id) { + const char* sid = EL_CSTR(id); + EngramNode* n = engram_find_node(sid); + if (!n) return el_map_new(0); + return engram_node_to_map(n); +} + +void engram_strengthen(el_val_t node_id) { + const char* sid = EL_CSTR(node_id); + EngramNode* n = engram_find_node(sid); + if (!n) return; + n->salience += 0.05; + if (n->salience > 1.0) n->salience = 1.0; + n->activation_count++; + n->last_activated = engram_now_ms(); + n->updated_at = n->last_activated; +} + +void engram_forget(el_val_t node_id) { + const char* sid = EL_CSTR(node_id); + if (!sid) return; + EngramStore* g = engram_get(); + int64_t idx = engram_find_node_index(sid); + if (idx < 0) return; + /* Free node strings */ + EngramNode* n = &g->nodes[idx]; + free(n->id); free(n->content); free(n->node_type); free(n->label); + free(n->tier); free(n->tags); free(n->metadata); + /* Shift remaining nodes down */ + for (int64_t i = idx + 1; i < g->node_count; i++) { + g->nodes[i - 1] = g->nodes[i]; + } + g->node_count--; + memset(&g->nodes[g->node_count], 0, sizeof(EngramNode)); + /* Remove all incident edges */ + int64_t w = 0; + for (int64_t r = 0; r < g->edge_count; r++) { + EngramEdge* e = &g->edges[r]; + int incident = (e->from_id && strcmp(e->from_id, sid) == 0) || + (e->to_id && strcmp(e->to_id, sid) == 0); + if (incident) { + free(e->id); free(e->from_id); free(e->to_id); + free(e->relation); free(e->metadata); + } else { + if (w != r) g->edges[w] = g->edges[r]; + w++; + } + } + g->edge_count = w; +} + +el_val_t engram_node_count(void) { + return (el_val_t)engram_get()->node_count; +} + +static int istr_contains(const char* hay, const char* needle) { + if (!hay || !needle || !*needle) return 0; + size_t nl = strlen(needle); + for (const char* p = hay; *p; p++) { + if (strncasecmp(p, needle, nl) == 0) return 1; + } + return 0; +} + +el_val_t engram_search(el_val_t query, el_val_t limit) { + EngramStore* g = engram_get(); + const char* q = EL_CSTR(query); + int64_t lim = (int64_t)limit; + if (lim <= 0) lim = 100; + el_val_t lst = el_list_empty(); + if (!q || !*q) return lst; + int64_t found = 0; + for (int64_t i = 0; i < g->node_count && found < lim; i++) { + EngramNode* n = &g->nodes[i]; + if (istr_contains(n->content, q) || + istr_contains(n->label, q) || + istr_contains(n->tags, q)) { + lst = el_list_append(lst, engram_node_to_map(n)); + found++; + } + } + return lst; +} + +/* Sort node indices by salience desc (small N, insertion sort is fine). */ +static void engram_sort_indices_by_salience(int64_t* arr, int64_t n, + const EngramNode* nodes) { + for (int64_t i = 1; i < n; i++) { + int64_t key = arr[i]; + double ks = nodes[key].salience; + int64_t j = i - 1; + while (j >= 0 && nodes[arr[j]].salience < ks) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } +} + +el_val_t engram_scan_nodes(el_val_t limit, el_val_t offset) { + EngramStore* g = engram_get(); + int64_t lim = (int64_t)limit; if (lim <= 0) lim = 100; + int64_t off = (int64_t)offset; if (off < 0) off = 0; + el_val_t lst = el_list_empty(); + if (g->node_count == 0) return lst; + int64_t* idx = malloc((size_t)g->node_count * sizeof(int64_t)); + if (!idx) return lst; + for (int64_t i = 0; i < g->node_count; i++) idx[i] = i; + engram_sort_indices_by_salience(idx, g->node_count, g->nodes); + int64_t end = off + lim; + if (end > g->node_count) end = g->node_count; + for (int64_t i = off; i < end; i++) { + lst = el_list_append(lst, engram_node_to_map(&g->nodes[idx[i]])); + } + free(idx); + return lst; +} + +void engram_connect(el_val_t from_id, el_val_t to_id, el_val_t weight, el_val_t relation) { + EngramStore* g = engram_get(); + const char* f = EL_CSTR(from_id); + const char* t = EL_CSTR(to_id); + const char* r = EL_CSTR(relation); + if (!f || !t) return; + engram_grow_edges(); + EngramEdge* e = &g->edges[g->edge_count]; + memset(e, 0, sizeof(*e)); + e->id = engram_new_id(); + e->from_id = el_strdup(f); + e->to_id = el_strdup(t); + e->relation = el_strdup(r && *r ? r : "associate"); + e->metadata = el_strdup("{}"); + e->weight = engram_decode_score(weight); + if (e->weight <= 0.0 || e->weight > 1.0) e->weight = 0.5; + e->confidence = 1.0; + int64_t now = engram_now_ms(); + e->created_at = now; + e->updated_at = now; + e->last_fired = 0; + g->edge_count++; +} + +el_val_t engram_edge_between(el_val_t from_id, el_val_t to_id) { + EngramStore* g = engram_get(); + const char* f = EL_CSTR(from_id); + const char* t = EL_CSTR(to_id); + if (!f || !t) return 0; + for (int64_t i = 0; i < g->edge_count; i++) { + EngramEdge* e = &g->edges[i]; + if (e->from_id && e->to_id && + strcmp(e->from_id, f) == 0 && strcmp(e->to_id, t) == 0) return 1; + } + return 0; +} + +/* Reserved helper: edge -> ElMap. Kept around for future builtins. */ +static el_val_t engram_edge_to_map(const EngramEdge* e) __attribute__((unused)); +static el_val_t engram_edge_to_map(const EngramEdge* e) { + el_val_t m = el_map_new(0); + m = el_map_set(m, EL_STR(el_strdup("id")), EL_STR(el_strdup(e->id ? e->id : ""))); + m = el_map_set(m, EL_STR(el_strdup("from_id")), EL_STR(el_strdup(e->from_id ? e->from_id : ""))); + m = el_map_set(m, EL_STR(el_strdup("to_id")), EL_STR(el_strdup(e->to_id ? e->to_id : ""))); + m = el_map_set(m, EL_STR(el_strdup("relation")), EL_STR(el_strdup(e->relation ? e->relation : ""))); + m = el_map_set(m, EL_STR(el_strdup("metadata")), EL_STR(el_strdup(e->metadata ? e->metadata : "{}"))); + m = el_map_set(m, EL_STR(el_strdup("weight")), el_from_float(e->weight)); + m = el_map_set(m, EL_STR(el_strdup("confidence")), el_from_float(e->confidence)); + m = el_map_set(m, EL_STR(el_strdup("created_at")), (el_val_t)e->created_at); + m = el_map_set(m, EL_STR(el_strdup("updated_at")), (el_val_t)e->updated_at); + m = el_map_set(m, EL_STR(el_strdup("last_fired")), (el_val_t)e->last_fired); + m = el_map_set(m, EL_STR(el_strdup("inhibitory")), (el_val_t)(e->inhibitory ? 1 : 0)); + return m; +} + +el_val_t engram_neighbors(el_val_t node_id) { + EngramStore* g = engram_get(); + const char* sid = EL_CSTR(node_id); + el_val_t lst = el_list_empty(); + if (!sid) return lst; + for (int64_t i = 0; i < g->edge_count; i++) { + EngramEdge* e = &g->edges[i]; + const char* other = NULL; + if (e->from_id && strcmp(e->from_id, sid) == 0) other = e->to_id; + else if (e->to_id && strcmp(e->to_id, sid) == 0) other = e->from_id; + if (!other) continue; + EngramNode* n = engram_find_node(other); + if (n) lst = el_list_append(lst, engram_node_to_map(n)); + } + return lst; +} + +el_val_t engram_neighbors_filtered(el_val_t node_id, el_val_t max_depth, el_val_t direction) { + EngramStore* g = engram_get(); + const char* sid = EL_CSTR(node_id); + int64_t md = (int64_t)max_depth; if (md <= 0) md = 1; + const char* dir = EL_CSTR(direction); /* "out" | "in" | "both" (default) */ + el_val_t lst = el_list_empty(); + if (!sid || g->node_count == 0) return lst; + int64_t start = engram_find_node_index(sid); + if (start < 0) return lst; + /* BFS with depth tracking */ + int64_t* visited = calloc((size_t)g->node_count, sizeof(int64_t)); + int64_t* queue = calloc((size_t)g->node_count, sizeof(int64_t)); + int64_t* depths = calloc((size_t)g->node_count, sizeof(int64_t)); + if (!visited || !queue || !depths) { + free(visited); free(queue); free(depths); return lst; + } + int64_t qh = 0, qt = 0; + queue[qt++] = start; + visited[start] = 1; + depths[start] = 0; + while (qh < qt) { + int64_t cur = queue[qh++]; + const char* cur_id = g->nodes[cur].id; + int64_t cur_depth = depths[cur]; + if (cur_depth >= md) continue; + for (int64_t i = 0; i < g->edge_count; i++) { + EngramEdge* e = &g->edges[i]; + const char* other = NULL; + int outgoing = e->from_id && strcmp(e->from_id, cur_id) == 0; + int incoming = e->to_id && strcmp(e->to_id, cur_id) == 0; + if (dir && strcmp(dir, "out") == 0 && !outgoing) continue; + if (dir && strcmp(dir, "in") == 0 && !incoming) continue; + if (outgoing) other = e->to_id; + else if (incoming) other = e->from_id; + else continue; + int64_t oi = engram_find_node_index(other); + if (oi < 0 || visited[oi]) continue; + visited[oi] = 1; + depths[oi] = cur_depth + 1; + queue[qt++] = oi; + } + } + /* Emit all visited except the seed */ + for (int64_t i = 0; i < g->node_count; i++) { + if (visited[i] && i != start) { + lst = el_list_append(lst, engram_node_to_map(&g->nodes[i])); + } + } + free(visited); free(queue); free(depths); + return lst; +} + +el_val_t engram_edge_count(void) { + return (el_val_t)engram_get()->edge_count; +} + +/* Compute temporal decay factor for a node given current time. + * effective contribution = salience * exp(-lambda * age_hours / T_half) + * Clamped to [0.05, 1.0] so very old nodes retain a meaningful floor. */ +static double engram_temporal_decay(const EngramNode* n, int64_t now_ms) { + int64_t age_ms = now_ms - n->last_activated; + if (age_ms <= 0) return 1.0; + double lambda = (n->temporal_decay_rate > 0.0) ? n->temporal_decay_rate + : ENGRAM_DECAY_LAMBDA; + double age_hours = (double)age_ms / 3600000.0; + double factor = exp(-lambda * age_hours / ENGRAM_T_HALF_HOURS); + if (factor < 0.05) factor = 0.05; + return factor; +} + +/* Activation dampening: high activation_count nodes are "well-known" context + * and get less marginal boost per firing. + * count=0 → 1.0, count=2 → ~0.74, count=9 → ~0.59, count=99 → ~0.43 */ +static double engram_activation_dampen(const EngramNode* n) { + return 1.0 / (1.0 + log(1.0 + (double)n->activation_count)); +} + +/* Temporal proximity bonus: boost propagation along edges connecting + * co-temporal nodes. Returns a multiplier bonus in [0, 0.2]. */ +static double engram_temporal_proximity_bonus(int64_t node_created, + int64_t seed_epoch) { + int64_t diff = node_created - seed_epoch; + if (diff < 0) diff = -diff; + if (diff < 86400000LL) return 0.20; /* within 1 day */ + if (diff < 604800000LL) return 0.10; /* within 7 days */ + return 0.0; +} + +/* ── Two-layer activation (biologically-motivated) ─────────────────────────── + * + * Layer 1 — Broad fan-out (background activation): + * BFS + spreading activation fires on ALL nodes reachable from seeds, + * regardless of relevance to the current goal. Every reachable node gets + * a background_activation score. Nothing is filtered here. Models the + * brain's massive parallel sub-threshold activation of all associated + * content in response to a stimulus. Temporal decay and activation + * dampening are applied at this layer (as before), but no threshold gate. + * + * Layer 2 — Executive filter (working memory promotion): + * A second pass asks: given the query (goal intent), attentional bias, + * and inhibitory edge topology — which background-activated nodes should + * break through into working memory? + * + * wm_weight = bg_activation * goal_bias(node, query) * confidence + * * inhibitory_suppression_factor + * + * Only nodes where wm_weight >= ENGRAM_WM_THRESHOLD are promoted to + * working memory (working_memory_weight > 0). Background-activated nodes + * that don't cross the threshold accumulate suppression_count. After + * ENGRAM_SUPPRESSION_BREAKTHROUGH consecutive suppressed turns, the node + * force-breaks through at ENGRAM_BREAKTHROUGH_WEIGHT (latent tension + * surfacing — models intrusive memory / unresolved cognitive load). + * + * Inhibitory edges: + * An edge with inhibitory=1 suppresses the TARGET node's working memory + * promotion when the SOURCE is background-activated. Background activation + * of the target is NOT affected — the node fires in layer 1. Only the + * executive filter (layer 2) is gated. Models attentional inhibition: + * "focused on code work" suppresses personal memories from surfacing + * even if they have high background_activation. + * + * Goal bias: + * A lightweight heuristic rates how well each background-activated node + * aligns with the apparent intent of the current query. Technical queries + * boost Belief/Canonical/Lesson nodes; relational queries boost Memory/ + * Entity nodes. Direct lexical overlap gives a 50% bonus. + * + * Working memory persistence (turn continuity): + * Nodes promoted in the previous turn retain a decayed working_memory_weight + * (weight *= ENGRAM_WM_DECAY) without needing re-activation. This models + * conversational thread continuity — once a topic is in working memory, + * it persists slightly into the next turn. + * + * Returns ElList of {node, activation_strength, working_memory_weight, + * epistemic_confidence, hops, promoted}. + * "promoted" = 1 if working_memory_weight > 0, 0 if background-only. + * Context compilation uses ONLY nodes with promoted=1. + * + * Temporal decay (preserved from prior implementation): + * effective_salience = salience * exp(-lambda * age_hours / T_half) + * where T_half = 168 h (one week), lambda = ln(2) + * + * Activation dampening (preserved): + * dampen = 1 / (1 + log(1 + activation_count)) + * + * Temporal proximity bonus (preserved): + * edge_strength *= (1 + tbonus) where tbonus ∈ {0, 0.10, 0.20} + * + * Per-type threshold gates apply only to working memory promotion (layer 2): + * Safety/DharmaSelf: 0.05 Canonical: 0.15 Lesson: 0.25 + * Belief/Entity: 0.30 Note/Memory/Working: 0.40 + */ + +/* Compute goal-state bias multiplier for a node given the query. + * Returns a value in [0.3, 2.0]. This is a lightweight heuristic — + * a production implementation may use LLM-derived intent classification. */ +static double engram_goal_bias(const EngramNode* n, const char* query) { + if (!query || !*query) return 1.0; + double bias = 1.0; + /* Direct lexical overlap: node content/label/tags share text with query. */ + if (istr_contains(n->content, query) || istr_contains(n->label, query) || + istr_contains(n->tags, query)) { + bias += 0.5; + } + /* Node-type resonance with query intent. */ + int technical_query = istr_contains(query, "code") || + istr_contains(query, "function") || + istr_contains(query, "implement") || + istr_contains(query, "error") || + istr_contains(query, "bug") || + istr_contains(query, "build") || + istr_contains(query, "system") || + istr_contains(query, "design") || + istr_contains(query, "architecture"); + int personal_query = istr_contains(query, "feel") || + istr_contains(query, "emotion") || + istr_contains(query, "remember") || + istr_contains(query, "personal") || + istr_contains(query, "story") || + istr_contains(query, "relationship"); + if (n->node_type) { + int is_knowledge = (strcmp(n->node_type, "Belief") == 0) || + (strcmp(n->node_type, "DharmaSelf") == 0) || + (strcmp(n->node_type, "Safety") == 0); + int is_personal = (strcmp(n->node_type, "Memory") == 0) || + (strcmp(n->node_type, "Entity") == 0); + if (technical_query && is_knowledge) bias += 0.3; + if (technical_query && is_personal) bias -= 0.3; + if (personal_query && is_personal) bias += 0.3; + if (personal_query && is_knowledge) bias -= 0.1; + } + /* Tier-based bonus: promote higher-confidence knowledge nodes. */ + if (n->tier) { + if (strcmp(n->tier, "Canonical") == 0) bias += 0.2; + if (strcmp(n->tier, "Lesson") == 0) bias += 0.1; + } + if (bias < 0.3) bias = 0.3; + if (bias > 2.0) bias = 2.0; + return bias; +} + +el_val_t engram_activate(el_val_t query, el_val_t depth) { + EngramStore* g = engram_get(); + const char* q = EL_CSTR(query); + int64_t max_depth = (int64_t)depth; if (max_depth <= 0) max_depth = 2; + el_val_t out = el_list_empty(); + if (!q || g->node_count == 0) return out; + + int64_t now_ms = engram_now_ms(); + + /* Per-node layer-1 tracking. */ + double* best_bg = calloc((size_t)g->node_count, sizeof(double)); + int64_t* best_hops = calloc((size_t)g->node_count, sizeof(int64_t)); + int* reached = calloc((size_t)g->node_count, sizeof(int)); + if (!best_bg || !best_hops || !reached) { + free(best_bg); free(best_hops); free(reached); return out; + } + + /* ── LAYER 1: broad fan-out (background activation) ───────────────── + * Find seeds, apply temporal decay + dampening, BFS with edge weights. + * Inhibitory edges propagate activation normally at this layer — they + * only gate working memory promotion in layer 2. */ + typedef struct { int64_t idx; double act; int64_t created_at; } SeedEntry; + SeedEntry* seeds = malloc((size_t)g->node_count * sizeof(SeedEntry)); + int64_t seed_count = 0; + if (!seeds) { + free(best_bg); free(best_hops); free(reached); return out; + } + for (int64_t i = 0; i < g->node_count; i++) { + EngramNode* n = &g->nodes[i]; + if (istr_contains(n->content, q) || + istr_contains(n->label, q) || + istr_contains(n->tags, q)) { + double tdecay = engram_temporal_decay(n, now_ms); + double dampen = engram_activation_dampen(n); + double act = n->salience * tdecay * dampen; + seeds[seed_count].idx = i; + seeds[seed_count].act = act; + seeds[seed_count].created_at = n->created_at; + seed_count++; + best_bg[i] = act; + best_hops[i] = 0; + reached[i] = 1; + } + } + /* Compute mean seed created_at for temporal proximity bonus. */ + int64_t seed_epoch = 0; + if (seed_count > 0) { + seed_epoch = seeds[0].created_at; + for (int64_t s = 1; s < seed_count; s++) + seed_epoch = (seed_epoch + seeds[s].created_at) / 2; + } + typedef struct { int64_t idx; int64_t hops; double act; } Frontier; + Frontier* fr = malloc((size_t)(g->node_count * (max_depth + 1)) * sizeof(Frontier) + 16 * sizeof(Frontier)); + if (!fr) { + free(best_bg); free(best_hops); free(reached); free(seeds); return out; + } + int64_t fhead = 0, ftail = 0; + int64_t fcap = (int64_t)((size_t)(g->node_count * (max_depth + 1)) + 16); + for (int64_t s = 0; s < seed_count; s++) { + if (ftail >= fcap) break; + fr[ftail].idx = seeds[s].idx; + fr[ftail].hops = 0; + fr[ftail].act = seeds[s].act; + ftail++; + } + const double SPREAD_DECAY = 0.7; + while (fhead < ftail) { + Frontier f = fr[fhead++]; + if (f.hops >= max_depth) continue; + const char* cur_id = g->nodes[f.idx].id; + for (int64_t ei = 0; ei < g->edge_count; ei++) { + EngramEdge* e = &g->edges[ei]; + const char* other = NULL; + if (e->from_id && strcmp(e->from_id, cur_id) == 0) other = e->to_id; + else if (e->to_id && strcmp(e->to_id, cur_id) == 0) other = e->from_id; + else continue; + int64_t oi = engram_find_node_index(other); + if (oi < 0) continue; + EngramNode* on = &g->nodes[oi]; + double tbonus = engram_temporal_proximity_bonus(on->created_at, seed_epoch); + double tdecay = engram_temporal_decay(on, now_ms); + double dampen = engram_activation_dampen(on); + double new_act = f.act * e->weight * SPREAD_DECAY * (1.0 + tbonus) + * tdecay * dampen; + int64_t new_hops = f.hops + 1; + if (!reached[oi] || new_act > best_bg[oi]) { + best_bg[oi] = new_act; + best_hops[oi] = new_hops; + reached[oi] = 1; + if (ftail < fcap) { + fr[ftail].idx = oi; + fr[ftail].hops = new_hops; + fr[ftail].act = new_act; + ftail++; + } + } + } + } + /* Persist layer-1 background_activation to node store. */ + for (int64_t i = 0; i < g->node_count; i++) { + g->nodes[i].background_activation = reached[i] ? best_bg[i] : 0.0; + } + + /* ── LAYER 2: executive filter → working memory promotion ────────── */ + /* Step A: collect inhibitory suppressions from fired inhibitory edges. */ + double* inhibition = calloc((size_t)g->node_count, sizeof(double)); + if (!inhibition) { + free(best_bg); free(best_hops); free(reached); free(seeds); free(fr); + return out; + } + for (int64_t ei = 0; ei < g->edge_count; ei++) { + EngramEdge* e = &g->edges[ei]; + if (!e->inhibitory) continue; + int64_t src = engram_find_node_index(e->from_id); + int64_t tgt = engram_find_node_index(e->to_id); + if (src < 0 || tgt < 0) continue; + if (!reached[src] || best_bg[src] <= 0.0) continue; + /* Inhibition strength proportional to source background activation + * and edge weight. Takes the maximum if multiple inhibitory edges + * target the same node. */ + double inh = best_bg[src] * e->weight; + if (inh > inhibition[tgt]) inhibition[tgt] = inh; + } + /* Step B: compute working_memory_weight per candidate node. */ + double* wm_weights = calloc((size_t)g->node_count, sizeof(double)); + if (!wm_weights) { + free(best_bg); free(best_hops); free(reached); free(seeds); + free(fr); free(inhibition); return out; + } + for (int64_t i = 0; i < g->node_count; i++) { + if (!reached[i] || best_bg[i] <= 0.0) continue; + EngramNode* n = &g->nodes[i]; + /* Per-type threshold: safety nodes break through more easily. */ + double type_threshold = engram_type_threshold(n->node_type, n->tier); + /* Goal bias weights the node's relevance to current intent. */ + double bias = engram_goal_bias(n, q); + /* Raw working memory score. */ + double raw_wm = best_bg[i] * bias * n->confidence; + /* Apply inhibitory suppression. Full inhibition → scale by factor. */ + double inh = inhibition[i]; + if (inh > 1.0) inh = 1.0; + double suppress = 1.0 - (1.0 - ENGRAM_INHIBITION_FACTOR) * inh; + raw_wm *= suppress; + /* Threshold gate: must exceed per-type threshold to enter working + * memory. Type threshold replaces the old flat 0.2 filter. */ + if (raw_wm >= type_threshold) { + wm_weights[i] = raw_wm > 1.0 ? 1.0 : raw_wm; + if (n->suppression_count > 0) n->suppression_count = 0; + } else { + /* Node didn't make it through — increment suppression counter. + * After N consecutive suppressions: force breakthrough. */ + n->suppression_count++; + if (n->suppression_count >= ENGRAM_SUPPRESSION_BREAKTHROUGH) { + wm_weights[i] = ENGRAM_BREAKTHROUGH_WEIGHT; + n->suppression_count = 0; + } else { + wm_weights[i] = 0.0; + } + } + } + /* Persist layer-2 working_memory_weight to node store. */ + for (int64_t i = 0; i < g->node_count; i++) { + g->nodes[i].working_memory_weight = wm_weights[i]; + } + + /* ── Collect all background-activated nodes for the return value ──── + * Callers see both layers. Context compilation uses only promoted nodes + * (working_memory_weight > 0). Sort: promoted first by wm_weight desc, + * then background-only by background_activation desc. */ + typedef struct { int64_t idx; double bg; double wm; double epist; int64_t hops; } Result; + Result* results = malloc((size_t)g->node_count * sizeof(Result)); + int64_t rcount = 0; + if (!results) { + free(best_bg); free(best_hops); free(reached); free(seeds); + free(fr); free(inhibition); free(wm_weights); return out; + } + for (int64_t i = 0; i < g->node_count; i++) { + if (!reached[i]) continue; + double epist = best_bg[i] * g->nodes[i].confidence; + /* Include if promoted to working memory OR if background activation + * is meaningful enough to report (epist >= 0.1). */ + if (epist < 0.1 && wm_weights[i] <= 0.0) continue; + results[rcount].idx = i; + results[rcount].bg = best_bg[i]; + results[rcount].wm = wm_weights[i]; + results[rcount].epist = epist; + results[rcount].hops = best_hops[i]; + rcount++; + } + /* Sort: promoted nodes first (by wm_weight desc), then background-only + * by background_activation desc. */ + for (int64_t i = 1; i < rcount; i++) { + Result key = results[i]; + int64_t j = i - 1; + while (j >= 0 && (results[j].wm < key.wm || + (results[j].wm == key.wm && results[j].bg < key.bg))) { + results[j + 1] = results[j]; + j--; + } + results[j + 1] = key; + } + for (int64_t i = 0; i < rcount; i++) { + el_val_t entry = el_map_new(0); + entry = el_map_set(entry, EL_STR(el_strdup("node")), + engram_node_to_map(&g->nodes[results[i].idx])); + entry = el_map_set(entry, EL_STR(el_strdup("activation_strength")), + el_from_float(results[i].bg)); + entry = el_map_set(entry, EL_STR(el_strdup("working_memory_weight")), + el_from_float(results[i].wm)); + entry = el_map_set(entry, EL_STR(el_strdup("epistemic_confidence")), + el_from_float(results[i].epist)); + entry = el_map_set(entry, EL_STR(el_strdup("hops")), + (el_val_t)results[i].hops); + entry = el_map_set(entry, EL_STR(el_strdup("promoted")), + (el_val_t)(results[i].wm > 0.0 ? 1 : 0)); + out = el_list_append(out, entry); + } + free(best_bg); free(best_hops); free(reached); + free(seeds); free(fr); free(inhibition); free(wm_weights); free(results); + return out; +} + +/* ── Engram persistence (JSON snapshot) ─────────────────────────────────── */ + +static void engram_emit_node_json(JsonBuf* b, const EngramNode* n) { + jb_putc(b, '{'); + jb_puts(b, "\"id\":"); jb_emit_escaped(b, n->id ? n->id : ""); + jb_puts(b, ",\"content\":"); jb_emit_escaped(b, n->content ? n->content : ""); + jb_puts(b, ",\"node_type\":"); jb_emit_escaped(b, n->node_type ? n->node_type : ""); + jb_puts(b, ",\"label\":"); jb_emit_escaped(b, n->label ? n->label : ""); + jb_puts(b, ",\"tier\":"); jb_emit_escaped(b, n->tier ? n->tier : "Working"); + jb_puts(b, ",\"tags\":"); jb_emit_escaped(b, n->tags ? n->tags : ""); + jb_puts(b, ",\"metadata\":"); jb_emit_escaped(b, n->metadata ? n->metadata : "{}"); + char tmp[80]; + snprintf(tmp, sizeof(tmp), ",\"salience\":%g", n->salience); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"importance\":%g", n->importance); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"confidence\":%g", n->confidence); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"temporal_decay_rate\":%g", n->temporal_decay_rate); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"activation_count\":%lld", (long long)n->activation_count); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"last_activated\":%lld", (long long)n->last_activated); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"created_at\":%lld", (long long)n->created_at); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"updated_at\":%lld", (long long)n->updated_at); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"background_activation\":%g", n->background_activation); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"working_memory_weight\":%g", n->working_memory_weight); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"suppression_count\":%d", n->suppression_count); jb_puts(b, tmp); + jb_putc(b, '}'); +} + +static void engram_emit_edge_json(JsonBuf* b, const EngramEdge* e) { + jb_putc(b, '{'); + jb_puts(b, "\"id\":"); jb_emit_escaped(b, e->id ? e->id : ""); + jb_puts(b, ",\"from_id\":"); jb_emit_escaped(b, e->from_id ? e->from_id : ""); + jb_puts(b, ",\"to_id\":"); jb_emit_escaped(b, e->to_id ? e->to_id : ""); + jb_puts(b, ",\"relation\":"); jb_emit_escaped(b, e->relation ? e->relation : ""); + jb_puts(b, ",\"metadata\":"); jb_emit_escaped(b, e->metadata ? e->metadata : "{}"); + char tmp[64]; + snprintf(tmp, sizeof(tmp), ",\"weight\":%g", e->weight); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"confidence\":%g", e->confidence); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"created_at\":%lld", (long long)e->created_at); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"updated_at\":%lld", (long long)e->updated_at); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"last_fired\":%lld", (long long)e->last_fired); jb_puts(b, tmp); + snprintf(tmp, sizeof(tmp), ",\"inhibitory\":%d", e->inhibitory ? 1 : 0); jb_puts(b, tmp); + jb_putc(b, '}'); +} + +el_val_t engram_save(el_val_t path) { + const char* p = EL_CSTR(path); + if (!p || !*p) return 0; + EngramStore* g = engram_get(); + JsonBuf b; jb_init(&b); + jb_puts(&b, "{\"nodes\":["); + for (int64_t i = 0; i < g->node_count; i++) { + if (i > 0) jb_putc(&b, ','); + engram_emit_node_json(&b, &g->nodes[i]); + } + jb_puts(&b, "],\"edges\":["); + for (int64_t i = 0; i < g->edge_count; i++) { + if (i > 0) jb_putc(&b, ','); + engram_emit_edge_json(&b, &g->edges[i]); + } + jb_puts(&b, "]}"); + FILE* f = fopen(p, "wb"); + if (!f) { free(b.buf); return 0; } + size_t w = fwrite(b.buf, 1, b.len, f); + fclose(f); + int ok = (w == b.len); + free(b.buf); + return ok ? 1 : 0; +} + +/* Helper: extract a string field from a JSON object substring. */ +static char* eg_get_str_field(const char* obj, const char* key) { + const char* p = json_find_key(obj, key); + if (!p) return el_strdup(""); + if (*p != '"') return el_strdup(""); + JsonParser jp = { .p = p, .end = p + strlen(p), .err = 0 }; + char* out = jp_parse_string_raw(&jp); + if (jp.err) { free(out); return el_strdup(""); } + return out; +} + +static double eg_get_num_field(const char* obj, const char* key) { + const char* p = json_find_key(obj, key); + if (!p || *p == '"' || *p == '{' || *p == '[') return 0.0; + return strtod(p, NULL); +} + +static int64_t eg_get_int_field(const char* obj, const char* key) { + const char* p = json_find_key(obj, key); + if (!p || *p == '"' || *p == '{' || *p == '[') return 0; + return strtoll(p, NULL, 10); +} + +/* Iterate the top-level nodes/edges arrays in a saved snapshot. */ +static const char* eg_skip_ws(const char* p) { + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + return p; +} + +el_val_t engram_load(el_val_t path) { + const char* p = EL_CSTR(path); + if (!p || !*p) return 0; + FILE* f = fopen(p, "rb"); + if (!f) return 0; + fseek(f, 0, SEEK_END); + long sz = ftell(f); + rewind(f); + if (sz <= 0) { fclose(f); return 0; } + char* data = malloc((size_t)sz + 1); + if (!data) { fclose(f); return 0; } + size_t got = fread(data, 1, (size_t)sz, f); + fclose(f); + data[got] = '\0'; + + /* Reset store */ + EngramStore* g = engram_get(); + for (int64_t i = 0; i < g->node_count; i++) { + free(g->nodes[i].id); free(g->nodes[i].content); free(g->nodes[i].node_type); + free(g->nodes[i].label); free(g->nodes[i].tier); free(g->nodes[i].tags); + free(g->nodes[i].metadata); + } + g->node_count = 0; + for (int64_t i = 0; i < g->edge_count; i++) { + free(g->edges[i].id); free(g->edges[i].from_id); free(g->edges[i].to_id); + free(g->edges[i].relation); free(g->edges[i].metadata); + } + g->edge_count = 0; + + /* Walk nodes array */ + const char* nodes_p = json_find_key(data, "nodes"); + if (nodes_p) { + nodes_p = eg_skip_ws(nodes_p); + if (*nodes_p == '[') { + nodes_p++; + nodes_p = eg_skip_ws(nodes_p); + while (*nodes_p && *nodes_p != ']') { + if (*nodes_p != '{') { nodes_p++; continue; } + const char* end = json_skip_value(nodes_p); + size_t n = (size_t)(end - nodes_p); + char* obj = malloc(n + 1); + memcpy(obj, nodes_p, n); obj[n] = '\0'; + engram_grow_nodes(); + EngramNode* nn = &g->nodes[g->node_count]; + memset(nn, 0, sizeof(*nn)); + nn->id = eg_get_str_field(obj, "id"); + nn->content = eg_get_str_field(obj, "content"); + nn->node_type = eg_get_str_field(obj, "node_type"); + nn->label = eg_get_str_field(obj, "label"); + nn->tier = eg_get_str_field(obj, "tier"); + nn->tags = eg_get_str_field(obj, "tags"); + nn->metadata = eg_get_str_field(obj, "metadata"); + if (!nn->metadata || !*nn->metadata) { free(nn->metadata); nn->metadata = el_strdup("{}"); } + nn->salience = eg_get_num_field(obj, "salience"); + nn->importance = eg_get_num_field(obj, "importance"); + nn->confidence = eg_get_num_field(obj, "confidence"); + nn->temporal_decay_rate = eg_get_num_field(obj, "temporal_decay_rate"); + /* temporal_decay_rate defaults to 0 (use global) if absent in snapshot */ + nn->activation_count = eg_get_int_field(obj, "activation_count"); + nn->last_activated = eg_get_int_field(obj, "last_activated"); + nn->created_at = eg_get_int_field(obj, "created_at"); + nn->updated_at = eg_get_int_field(obj, "updated_at"); + nn->background_activation = eg_get_num_field(obj, "background_activation"); + nn->working_memory_weight = eg_get_num_field(obj, "working_memory_weight"); + nn->suppression_count = (int32_t)eg_get_int_field(obj, "suppression_count"); + g->node_count++; + free(obj); + nodes_p = end; + nodes_p = eg_skip_ws(nodes_p); + if (*nodes_p == ',') { nodes_p++; nodes_p = eg_skip_ws(nodes_p); } + } + } + } + /* Walk edges array */ + const char* edges_p = json_find_key(data, "edges"); + if (edges_p) { + edges_p = eg_skip_ws(edges_p); + if (*edges_p == '[') { + edges_p++; + edges_p = eg_skip_ws(edges_p); + while (*edges_p && *edges_p != ']') { + if (*edges_p != '{') { edges_p++; continue; } + const char* end = json_skip_value(edges_p); + size_t n = (size_t)(end - edges_p); + char* obj = malloc(n + 1); + memcpy(obj, edges_p, n); obj[n] = '\0'; + engram_grow_edges(); + EngramEdge* ee = &g->edges[g->edge_count]; + memset(ee, 0, sizeof(*ee)); + ee->id = eg_get_str_field(obj, "id"); + ee->from_id = eg_get_str_field(obj, "from_id"); + ee->to_id = eg_get_str_field(obj, "to_id"); + ee->relation = eg_get_str_field(obj, "relation"); + ee->metadata = eg_get_str_field(obj, "metadata"); + if (!ee->metadata || !*ee->metadata) { free(ee->metadata); ee->metadata = el_strdup("{}"); } + ee->weight = eg_get_num_field(obj, "weight"); + ee->confidence = eg_get_num_field(obj, "confidence"); + ee->created_at = eg_get_int_field(obj, "created_at"); + ee->updated_at = eg_get_int_field(obj, "updated_at"); + ee->last_fired = eg_get_int_field(obj, "last_fired"); + ee->inhibitory = (int)eg_get_int_field(obj, "inhibitory"); + g->edge_count++; + free(obj); + edges_p = end; + edges_p = eg_skip_ws(edges_p); + if (*edges_p == ',') { edges_p++; edges_p = eg_skip_ws(edges_p); } + } + } + } + free(data); + return 1; +} + +/* ── Engram JSON-string accessors ───────────────────────────────────────── + * These return pre-serialized JSON strings so callers (especially HTTP + * handlers) don't have to round-trip ElList/ElMap through json_stringify + * — which can't reliably distinguish those structures from raw pointers + * due to el_val_t's type erasure. The runtime knows the real C types and + * can serialize directly. */ + +el_val_t engram_get_node_json(el_val_t id) { + const char* sid = EL_CSTR(id); + EngramNode* n = engram_find_node(sid); + if (!n) return el_wrap_str(el_strdup("{}")); + JsonBuf b; jb_init(&b); + engram_emit_node_json(&b, n); + return el_wrap_str(b.buf); +} + +el_val_t engram_search_json(el_val_t query, el_val_t limit) { + EngramStore* g = engram_get(); + const char* q = EL_CSTR(query); + int64_t lim = (int64_t)limit; + if (lim <= 0) lim = 100; + JsonBuf b; jb_init(&b); + jb_putc(&b, '['); + int first = 1; + int64_t found = 0; + if (q && *q) { + for (int64_t i = 0; i < g->node_count && found < lim; i++) { + EngramNode* n = &g->nodes[i]; + if (istr_contains(n->content, q) || + istr_contains(n->label, q) || + istr_contains(n->tags, q)) { + if (!first) jb_putc(&b, ','); + engram_emit_node_json(&b, n); + first = 0; + found++; + } + } + } + jb_putc(&b, ']'); + return el_wrap_str(b.buf); +} + +el_val_t engram_scan_nodes_json(el_val_t limit, el_val_t offset) { + EngramStore* g = engram_get(); + int64_t lim = (int64_t)limit; if (lim <= 0) lim = 100; + int64_t off = (int64_t)offset; if (off < 0) off = 0; + JsonBuf b; jb_init(&b); + jb_putc(&b, '['); + if (g->node_count == 0) { jb_putc(&b, ']'); return el_wrap_str(b.buf); } + int64_t* idx = malloc((size_t)g->node_count * sizeof(int64_t)); + if (!idx) { jb_putc(&b, ']'); return el_wrap_str(b.buf); } + for (int64_t i = 0; i < g->node_count; i++) idx[i] = i; + engram_sort_indices_by_salience(idx, g->node_count, g->nodes); + int64_t end = off + lim; + if (end > g->node_count) end = g->node_count; + int first = 1; + for (int64_t i = off; i < end; i++) { + if (!first) jb_putc(&b, ','); + engram_emit_node_json(&b, &g->nodes[idx[i]]); + first = 0; + } + free(idx); + jb_putc(&b, ']'); + return el_wrap_str(b.buf); +} + +el_val_t engram_neighbors_json(el_val_t node_id, el_val_t max_depth, el_val_t direction) { + /* Re-implement here directly so we serialize without going through + * the ElList path. Walks BFS to max_depth, emits {node, edge, hops} + * triples. */ + EngramStore* g = engram_get(); + const char* sid = EL_CSTR(node_id); + int64_t depth = (int64_t)max_depth; if (depth <= 0) depth = 1; + const char* dir = EL_CSTR(direction); if (!dir) dir = "both"; + int allow_out = (strcmp(dir, "out") == 0) || (strcmp(dir, "both") == 0); + int allow_in = (strcmp(dir, "in") == 0) || (strcmp(dir, "both") == 0); + JsonBuf b; jb_init(&b); + jb_putc(&b, '['); + if (!sid || !*sid) { jb_putc(&b, ']'); return el_wrap_str(b.buf); } + + /* Frontier of (node_id, hops). Cap to a sane size. */ + char** frontier = calloc(1024, sizeof(char*)); + int64_t* frontier_h = calloc(1024, sizeof(int64_t)); + int64_t fc = 0; + char** visited = calloc(1024, sizeof(char*)); + int64_t vc = 0; + if (!frontier || !frontier_h || !visited) { + free(frontier); free(frontier_h); free(visited); + jb_putc(&b, ']'); return el_wrap_str(b.buf); + } + frontier[fc] = el_strdup(sid); frontier_h[fc] = 0; fc++; + visited[vc++] = el_strdup(sid); + + int first = 1; + while (fc > 0) { + char* cur = frontier[0]; int64_t h = frontier_h[0]; + for (int64_t k = 1; k < fc; k++) { frontier[k-1] = frontier[k]; frontier_h[k-1] = frontier_h[k]; } + fc--; + if (h >= depth) { free(cur); continue; } + for (int64_t i = 0; i < g->edge_count; i++) { + EngramEdge* e = &g->edges[i]; + const char* peer = NULL; + if (allow_out && e->from_id && strcmp(e->from_id, cur) == 0) peer = e->to_id; + else if (allow_in && e->to_id && strcmp(e->to_id, cur) == 0) peer = e->from_id; + if (!peer) continue; + int seen = 0; + for (int64_t v = 0; v < vc; v++) { + if (strcmp(visited[v], peer) == 0) { seen = 1; break; } + } + if (seen) continue; + EngramNode* n = engram_find_node(peer); + if (!n) continue; + if (!first) jb_putc(&b, ','); + jb_puts(&b, "{\"node\":"); + engram_emit_node_json(&b, n); + jb_puts(&b, ",\"edge\":"); + engram_emit_edge_json(&b, e); + char tmp[64]; snprintf(tmp, sizeof(tmp), ",\"hops\":%lld}", (long long)(h + 1)); + jb_puts(&b, tmp); + first = 0; + if (vc < 1024) visited[vc++] = el_strdup(peer); + if (fc < 1024 && h + 1 < depth) { frontier[fc] = el_strdup(peer); frontier_h[fc] = h + 1; fc++; } + } + free(cur); + } + for (int64_t i = 0; i < fc; i++) free(frontier[i]); + for (int64_t i = 0; i < vc; i++) free(visited[i]); + free(frontier); free(frontier_h); free(visited); + jb_putc(&b, ']'); + return el_wrap_str(b.buf); +} + +el_val_t engram_activate_json(el_val_t query, el_val_t depth) { + /* Run two-layer engram_activate and serialize the result list to JSON. + * Each entry includes both activation_strength (layer 1 background) and + * working_memory_weight (layer 2 executive filter), plus promoted flag. + * Callers performing context compilation should filter to promoted=1. */ + el_val_t lst = engram_activate(query, depth); + ElList* arr = (ElList*)(uintptr_t)lst; + JsonBuf b; jb_init(&b); + jb_putc(&b, '['); + if (arr) { + for (int64_t i = 0; i < arr->length; i++) { + if (!arr->elems[i]) continue; + el_val_t node_map = el_map_get(arr->elems[i], EL_STR("node")); + el_val_t strength_v = el_map_get(arr->elems[i], EL_STR("activation_strength")); + el_val_t wm_v = el_map_get(arr->elems[i], EL_STR("working_memory_weight")); + el_val_t epist_v = el_map_get(arr->elems[i], EL_STR("epistemic_confidence")); + el_val_t hops_v = el_map_get(arr->elems[i], EL_STR("hops")); + el_val_t promoted_v = el_map_get(arr->elems[i], EL_STR("promoted")); + /* Look up underlying EngramNode by id to emit canonical JSON. */ + el_val_t id_v = el_map_get(node_map, EL_STR("id")); + const char* id_s = EL_CSTR(id_v); + EngramNode* n = id_s ? engram_find_node(id_s) : NULL; + if (i > 0) jb_putc(&b, ','); + jb_puts(&b, "{\"node\":"); + if (n) { + engram_emit_node_json(&b, n); + } else { + jb_puts(&b, "{}"); + } + char tmp[80]; + snprintf(tmp, sizeof(tmp), ",\"activation_strength\":%g", el_to_float(strength_v)); jb_puts(&b, tmp); + snprintf(tmp, sizeof(tmp), ",\"working_memory_weight\":%g", el_to_float(wm_v)); jb_puts(&b, tmp); + snprintf(tmp, sizeof(tmp), ",\"epistemic_confidence\":%g", el_to_float(epist_v)); jb_puts(&b, tmp); + snprintf(tmp, sizeof(tmp), ",\"hops\":%lld", (long long)(int64_t)hops_v); jb_puts(&b, tmp); + snprintf(tmp, sizeof(tmp), ",\"promoted\":%d}", (int)(int64_t)promoted_v); jb_puts(&b, tmp); + } + } + jb_putc(&b, ']'); + return el_wrap_str(b.buf); +} + +el_val_t engram_stats_json(void) { + EngramStore* g = engram_get(); + char buf[128]; + snprintf(buf, sizeof(buf), + "{\"node_count\":%lld,\"edge_count\":%lld}", + (long long)g->node_count, (long long)g->edge_count); + return el_wrap_str(el_strdup(buf)); +} + +/* engram_query_range — temporal range query. + * Returns a JSON array of nodes whose created_at OR last_activated falls + * within [start_ms, end_ms], sorted by created_at ascending. + * Enables "what was I working on last Tuesday?" style queries by passing + * unix-millisecond timestamps for the start and end of the target interval. + * Both endpoints are inclusive. Pass 0 for start_ms to mean "beginning of + * time"; pass 0 for end_ms to mean "now". */ +el_val_t engram_query_range(el_val_t start_ms_v, el_val_t end_ms_v) { + EngramStore* g = engram_get(); + int64_t start_ms = (int64_t)start_ms_v; + int64_t end_ms = (int64_t)end_ms_v; + if (end_ms <= 0) end_ms = engram_now_ms(); + + /* Collect matching indices. */ + int64_t* idx = malloc((size_t)g->node_count * sizeof(int64_t)); + if (!idx) return el_wrap_str(el_strdup("[]")); + int64_t mc = 0; + for (int64_t i = 0; i < g->node_count; i++) { + EngramNode* n = &g->nodes[i]; + int in_created = (n->created_at >= start_ms && n->created_at <= end_ms); + int in_activated = (n->last_activated >= start_ms && n->last_activated <= end_ms); + if (in_created || in_activated) idx[mc++] = i; + } + /* Sort by created_at ascending (insertion sort — N is small in practice). */ + for (int64_t i = 1; i < mc; i++) { + int64_t key = idx[i]; + int64_t kts = g->nodes[key].created_at; + int64_t j = i - 1; + while (j >= 0 && g->nodes[idx[j]].created_at > kts) { + idx[j + 1] = idx[j]; + j--; + } + idx[j + 1] = key; + } + JsonBuf b; jb_init(&b); + jb_putc(&b, '['); + for (int64_t i = 0; i < mc; i++) { + if (i > 0) jb_putc(&b, ','); + engram_emit_node_json(&b, &g->nodes[idx[i]]); + } + jb_putc(&b, ']'); + free(idx); + return el_wrap_str(b.buf); +} + +/* ── DHARMA network ───────────────────────────────────────────────────────── + * Real implementation. Peers are addressed by `dharma_id` — either bare + * (e.g. "ntn-genesis", transport defaults to http://localhost:7770) or + * "@" where is the peer's Engram-exposed daemon. + * + * Channels are logical handles cached per-cgi: `dharma_connect` is + * idempotent and returns "ch:". The channel registry below tracks + * every cgi_id we've connected to and its resolved transport URL. + * + * Relationship weights live in the local Engram graph: edges of type + * "dharma-relation" between a synthetic local node ("dharma:self") and + * synthetic peer nodes ("dharma:peer:"). Hebbian increments + * accumulate in EngramEdge.weight, clamped to [0.0, 1.0]. + * + * Events arrive over HTTP via the application's request handler, which is + * expected to call el_runtime_dharma_event_arrive() when it sees a + * /dharma/event POST. dharma_field() blocks on a per-event-type queue. + */ + +#define DHARMA_DEFAULT_URL "http://localhost:7770" + +/* Channel registry — one entry per known peer. */ +typedef struct DharmaChannel { + char* cgi_id; /* full dharma_id including any @ suffix */ + char* base_id; /* registry-id portion (before @) for relationship lookup */ + char* url; /* resolved transport URL */ + char* channel_id; /* "ch:" */ +} DharmaChannel; + +static DharmaChannel* _dharma_channels = NULL; +static size_t _dharma_channel_count = 0; +static size_t _dharma_channel_cap = 0; +static pthread_mutex_t _dharma_channel_mu = PTHREAD_MUTEX_INITIALIZER; + +/* Event queue — per-type linked list. dharma_field blocks on _dharma_event_cv. */ +typedef struct DharmaEvent { + char* event_type; + char* payload; + char* source; + int64_t timestamp; + struct DharmaEvent* next; +} DharmaEvent; + +static DharmaEvent* _dharma_event_head = NULL; +static DharmaEvent* _dharma_event_tail = NULL; +static pthread_mutex_t _dharma_event_mu = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t _dharma_event_cv = PTHREAD_COND_INITIALIZER; + +/* Split "@" → (base_id, url). If no "@", base_id = full, url = default. + * Returned strings are heap-allocated; caller must free. */ +static void dharma_parse_id(const char* full, char** out_base, char** out_url) { + if (!full) full = ""; + const char* at = strchr(full, '@'); + if (at) { + size_t bn = (size_t)(at - full); + char* b = malloc(bn + 1); + memcpy(b, full, bn); b[bn] = '\0'; + *out_base = b; + *out_url = el_strdup(at + 1); + if (!**out_url) { free(*out_url); *out_url = el_strdup(DHARMA_DEFAULT_URL); } + } else { + *out_base = el_strdup(full); + *out_url = el_strdup(DHARMA_DEFAULT_URL); + } +} + +/* Find existing channel by full cgi_id. Caller must hold _dharma_channel_mu. */ +static DharmaChannel* dharma_find_channel_locked(const char* cgi_id) { + if (!cgi_id) return NULL; + for (size_t i = 0; i < _dharma_channel_count; i++) { + if (_dharma_channels[i].cgi_id && + strcmp(_dharma_channels[i].cgi_id, cgi_id) == 0) { + return &_dharma_channels[i]; + } + } + return NULL; +} + +/* Add a new channel entry. Caller must hold _dharma_channel_mu. */ +static DharmaChannel* dharma_add_channel_locked(const char* cgi_id) { + if (_dharma_channel_count >= _dharma_channel_cap) { + size_t nc = _dharma_channel_cap ? _dharma_channel_cap * 2 : 8; + _dharma_channels = realloc(_dharma_channels, nc * sizeof(DharmaChannel)); + if (!_dharma_channels) { fputs("el_runtime: out of memory\n", stderr); exit(1); } + memset(_dharma_channels + _dharma_channel_cap, 0, + (nc - _dharma_channel_cap) * sizeof(DharmaChannel)); + _dharma_channel_cap = nc; + } + DharmaChannel* ch = &_dharma_channels[_dharma_channel_count++]; + char* base = NULL; char* url = NULL; + dharma_parse_id(cgi_id, &base, &url); + ch->cgi_id = el_strdup(cgi_id ? cgi_id : ""); + ch->base_id = base; + ch->url = url; + size_t cn = strlen(ch->cgi_id) + 4; + ch->channel_id = malloc(cn); + snprintf(ch->channel_id, cn, "ch:%s", ch->cgi_id); + return ch; +} + +el_val_t dharma_connect(el_val_t cgi_id) { + const char* id = EL_CSTR(cgi_id); + if (!id || !*id) return el_wrap_str(el_strdup("")); + pthread_mutex_lock(&_dharma_channel_mu); + DharmaChannel* ch = dharma_find_channel_locked(id); + if (!ch) ch = dharma_add_channel_locked(id); + char* out = el_strdup(ch->channel_id); + pthread_mutex_unlock(&_dharma_channel_mu); + return el_wrap_str(out); +} + +/* Build an error JSON body — same shape http_error_json uses. */ +static el_val_t dharma_error_json(const char* msg) { + return http_error_json(msg); +} + +el_val_t dharma_send(el_val_t channel, el_val_t content) { + const char* ch_id = EL_CSTR(channel); + const char* msg = EL_CSTR(content); + if (!ch_id || strncmp(ch_id, "ch:", 3) != 0) { + return dharma_error_json("invalid channel"); + } + const char* peer_id = ch_id + 3; + /* Look up channel; if unknown (caller fabricated), auto-register. */ + pthread_mutex_lock(&_dharma_channel_mu); + DharmaChannel* ch = dharma_find_channel_locked(peer_id); + if (!ch) ch = dharma_add_channel_locked(peer_id); + char* url = el_strdup(ch->url); + pthread_mutex_unlock(&_dharma_channel_mu); + /* Build /dharma/recv body. */ + const char* from = _el_cgi_dharma_id ? _el_cgi_dharma_id : "(unknown)"; + char* esc_ch = json_escape_alloc(ch_id); + char* esc_from = json_escape_alloc(from); + char* esc_msg = json_escape_alloc(msg ? msg : ""); + JsonBuf b; jb_init(&b); + jb_puts(&b, "{\"channel\":\""); jb_puts(&b, esc_ch); + jb_puts(&b, "\",\"from\":\""); jb_puts(&b, esc_from); + jb_puts(&b, "\",\"content\":\""); jb_puts(&b, esc_msg); + jb_puts(&b, "\"}"); + free(esc_ch); free(esc_from); free(esc_msg); + size_t ul = strlen(url) + 16; + char* full_url = malloc(ul); + snprintf(full_url, ul, "%s/dharma/recv", url); + struct curl_slist* h = NULL; + h = curl_slist_append(h, "Content-Type: application/json"); + el_val_t resp = http_do("POST", full_url, b.buf, h); + curl_slist_free_all(h); + free(b.buf); free(full_url); free(url); + return resp; +} + +el_val_t dharma_activate(el_val_t query) { + const char* q = EL_CSTR(query); + if (!q) q = ""; + el_val_t out = el_list_empty(); + char* esc_q = json_escape_alloc(q); + JsonBuf body; jb_init(&body); + jb_puts(&body, "{\"query\":\""); jb_puts(&body, esc_q); jb_puts(&body, "\"}"); + free(esc_q); + + /* Snapshot the channel list under lock so we can iterate without + * holding the mutex during network I/O. */ + pthread_mutex_lock(&_dharma_channel_mu); + size_t n = _dharma_channel_count; + char** urls = calloc(n ? n : 1, sizeof(char*)); + char** ids = calloc(n ? n : 1, sizeof(char*)); + char** bases = calloc(n ? n : 1, sizeof(char*)); + for (size_t i = 0; i < n; i++) { + urls[i] = el_strdup(_dharma_channels[i].url); + ids[i] = el_strdup(_dharma_channels[i].cgi_id); + bases[i] = el_strdup(_dharma_channels[i].base_id); + } + pthread_mutex_unlock(&_dharma_channel_mu); + + for (size_t i = 0; i < n; i++) { + size_t ul = strlen(urls[i]) + 32; + char* full_url = malloc(ul); + snprintf(full_url, ul, "%s/api/activate", urls[i]); + struct curl_slist* h = NULL; + h = curl_slist_append(h, "Content-Type: application/json"); + el_val_t resp = http_do("POST", full_url, body.buf, h); + curl_slist_free_all(h); + free(full_url); + const char* rs = EL_CSTR(resp); + if (!rs || !*rs) continue; + if (rs[0] == '{' && strstr(rs, "\"error\"")) continue; + + /* Look up relationship weight (attenuation). */ + double rel_weight = 1.0; + { + const char* self_id = "dharma:self"; + char peer_node[512]; + snprintf(peer_node, sizeof(peer_node), "dharma:peer:%s", bases[i]); + EngramStore* g = engram_get(); + for (int64_t k = 0; k < g->edge_count; k++) { + EngramEdge* e = &g->edges[k]; + if (e->from_id && e->to_id && + strcmp(e->from_id, self_id) == 0 && + strcmp(e->to_id, peer_node) == 0 && + e->relation && strcmp(e->relation, "dharma-relation") == 0) { + rel_weight = e->weight; + break; + } + } + } + + /* Iterate the response array. Expect either a top-level array + * or an object whose "results" field is an array. */ + const char* arr = rs; + while (*arr == ' ' || *arr == '\t' || *arr == '\n' || *arr == '\r') arr++; + char* arr_owned = NULL; + if (*arr == '{') { + el_val_t r = json_get_raw(EL_STR(rs), EL_STR("results")); + const char* rr = EL_CSTR(r); + if (rr && *rr == '[') { + arr_owned = el_strdup(rr); + arr = arr_owned; + } else { + continue; + } + } + if (*arr != '[') { free(arr_owned); continue; } + const char* p = arr + 1; + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + while (*p && *p != ']') { + const char* end = json_skip_value(p); + size_t en = (size_t)(end - p); + char* obj = el_strbuf(en); + memcpy(obj, p, en); obj[en] = '\0'; + + /* Pull activation_strength if present, else 1.0. */ + el_val_t act_v = json_get_float(EL_STR(obj), EL_STR("activation_strength")); + double act = el_to_float(act_v); + if (!(act > 0.0 && act <= 100.0)) act = 1.0; + double final_act = act * rel_weight; + + el_val_t entry = el_map_new(0); + /* node = the inner JSON if present, else the entire obj. */ + el_val_t node_raw = json_get_raw(EL_STR(obj), EL_STR("node")); + const char* nr = EL_CSTR(node_raw); + entry = el_map_set(entry, EL_STR(el_strdup("node")), + (nr && *nr) ? node_raw : EL_STR(el_strdup(obj))); + entry = el_map_set(entry, EL_STR(el_strdup("source_cgi")), + EL_STR(el_strdup(ids[i]))); + entry = el_map_set(entry, EL_STR(el_strdup("activation_strength")), + el_from_float(final_act)); + out = el_list_append(out, entry); + free(obj); + p = end; + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',') p++; + } + free(arr_owned); + } + for (size_t i = 0; i < n; i++) { free(urls[i]); free(ids[i]); free(bases[i]); } + free(urls); free(ids); free(bases); + free(body.buf); + return out; +} + +void dharma_emit(el_val_t event_type, el_val_t payload) { + const char* et = EL_CSTR(event_type); + const char* pay = EL_CSTR(payload); + if (!et) et = ""; + if (!pay) pay = ""; + const char* src = _el_cgi_dharma_id ? _el_cgi_dharma_id : "(unknown)"; + int64_t ts = engram_now_ms(); + + char* esc_et = json_escape_alloc(et); + char* esc_pay = json_escape_alloc(pay); + char* esc_src = json_escape_alloc(src); + JsonBuf b; jb_init(&b); + jb_puts(&b, "{\"type\":\""); jb_puts(&b, esc_et); + jb_puts(&b, "\",\"payload\":\""); jb_puts(&b, esc_pay); + jb_puts(&b, "\",\"source\":\""); jb_puts(&b, esc_src); + jb_puts(&b, "\",\"timestamp\":"); jb_emit_int(&b, ts); + jb_putc(&b, '}'); + free(esc_et); free(esc_pay); free(esc_src); + + /* Snapshot URLs to avoid holding the channel mutex during I/O. */ + pthread_mutex_lock(&_dharma_channel_mu); + size_t n = _dharma_channel_count; + char** urls = calloc(n ? n : 1, sizeof(char*)); + for (size_t i = 0; i < n; i++) urls[i] = el_strdup(_dharma_channels[i].url); + pthread_mutex_unlock(&_dharma_channel_mu); + + for (size_t i = 0; i < n; i++) { + size_t ul = strlen(urls[i]) + 32; + char* full_url = malloc(ul); + snprintf(full_url, ul, "%s/dharma/event", urls[i]); + struct curl_slist* h = NULL; + h = curl_slist_append(h, "Content-Type: application/json"); + el_val_t r = http_do("POST", full_url, b.buf, h); + (void)r; /* fire-and-forget — emit is not synchronous */ + curl_slist_free_all(h); + free(full_url); + } + for (size_t i = 0; i < n; i++) free(urls[i]); + free(urls); + free(b.buf); +} + +void el_runtime_dharma_event_arrive(const char* event_type, const char* payload, + const char* source) { + DharmaEvent* ev = calloc(1, sizeof(DharmaEvent)); + if (!ev) return; + ev->event_type = el_strdup(event_type ? event_type : ""); + ev->payload = el_strdup(payload ? payload : ""); + ev->source = el_strdup(source ? source : ""); + ev->timestamp = engram_now_ms(); + ev->next = NULL; + pthread_mutex_lock(&_dharma_event_mu); + if (_dharma_event_tail) _dharma_event_tail->next = ev; + else _dharma_event_head = ev; + _dharma_event_tail = ev; + pthread_cond_broadcast(&_dharma_event_cv); + pthread_mutex_unlock(&_dharma_event_mu); +} + +el_val_t dharma_field(el_val_t event_type) { + const char* et = EL_CSTR(event_type); + if (!et) et = ""; + + /* Compute deadline: now + 30 seconds. */ + struct timespec deadline; + clock_gettime(CLOCK_REALTIME, &deadline); + deadline.tv_sec += 30; + + DharmaEvent* found = NULL; + pthread_mutex_lock(&_dharma_event_mu); + while (1) { + /* Scan queue for matching type; pop and return first match. */ + DharmaEvent* prev = NULL; + DharmaEvent* cur = _dharma_event_head; + while (cur) { + if (cur->event_type && strcmp(cur->event_type, et) == 0) { + if (prev) prev->next = cur->next; + else _dharma_event_head = cur->next; + if (_dharma_event_tail == cur) _dharma_event_tail = prev; + cur->next = NULL; + found = cur; + break; + } + prev = cur; cur = cur->next; + } + if (found) break; + int rc = pthread_cond_timedwait(&_dharma_event_cv, &_dharma_event_mu, &deadline); + if (rc == ETIMEDOUT) break; + } + pthread_mutex_unlock(&_dharma_event_mu); + + if (!found) return el_map_new(0); + el_val_t m = el_map_new(0); + m = el_map_set(m, EL_STR(el_strdup("type")), + EL_STR(el_strdup(found->event_type ? found->event_type : ""))); + m = el_map_set(m, EL_STR(el_strdup("payload")), + EL_STR(el_strdup(found->payload ? found->payload : ""))); + m = el_map_set(m, EL_STR(el_strdup("source_cgi")), + EL_STR(el_strdup(found->source ? found->source : ""))); + m = el_map_set(m, EL_STR(el_strdup("timestamp")), (el_val_t)found->timestamp); + free(found->event_type); free(found->payload); free(found->source); free(found); + return m; +} + +/* Locate (or create) the local "dharma:self" node and the synthetic peer + * node "dharma:peer:". Returns the index of the dharma-relation + * edge, or -1 if not found. If `create` is non-zero, ensure the nodes + * and edge exist (creating them as needed) and return the edge index. */ +static int64_t dharma_find_or_create_relation_edge(const char* peer_base, int create) { + if (!peer_base || !*peer_base) return -1; + EngramStore* g = engram_get(); + const char* self_id = "dharma:self"; + char peer_node[512]; + snprintf(peer_node, sizeof(peer_node), "dharma:peer:%s", peer_base); + + /* Look for the edge first. */ + for (int64_t i = 0; i < g->edge_count; i++) { + EngramEdge* e = &g->edges[i]; + if (e->from_id && e->to_id && + strcmp(e->from_id, self_id) == 0 && + strcmp(e->to_id, peer_node) == 0 && + e->relation && strcmp(e->relation, "dharma-relation") == 0) { + return i; + } + } + if (!create) return -1; + + /* Ensure self node exists. We use a fixed id (not engram_new_id) so + * subsequent calls reuse the same one. */ + if (!engram_find_node(self_id)) { + engram_grow_nodes(); + EngramNode* n = &g->nodes[g->node_count]; + memset(n, 0, sizeof(*n)); + n->id = el_strdup(self_id); + n->content = el_strdup(_el_cgi_dharma_id ? _el_cgi_dharma_id : "(self)"); + n->node_type = el_strdup("DharmaSelf"); + n->label = el_strdup("dharma:self"); + n->tier = el_strdup("Working"); + n->tags = el_strdup("dharma"); + n->metadata = el_strdup("{}"); + n->salience = 1.0; n->importance = 1.0; n->confidence = 1.0; + int64_t now = engram_now_ms(); + n->created_at = now; n->updated_at = now; n->last_activated = now; + g->node_count++; + } + if (!engram_find_node(peer_node)) { + engram_grow_nodes(); + EngramNode* n = &g->nodes[g->node_count]; + memset(n, 0, sizeof(*n)); + n->id = el_strdup(peer_node); + n->content = el_strdup(peer_base); + n->node_type = el_strdup("DharmaPeer"); + n->label = el_strdup(peer_node); + n->tier = el_strdup("Working"); + n->tags = el_strdup("dharma"); + n->metadata = el_strdup("{}"); + n->salience = 0.5; n->importance = 0.5; n->confidence = 1.0; + int64_t now = engram_now_ms(); + n->created_at = now; n->updated_at = now; n->last_activated = now; + g->node_count++; + } + /* Create the edge with weight 0.0 — caller will increment. */ + engram_grow_edges(); + EngramEdge* e = &g->edges[g->edge_count]; + memset(e, 0, sizeof(*e)); + e->id = engram_new_id(); + e->from_id = el_strdup(self_id); + e->to_id = el_strdup(peer_node); + e->relation = el_strdup("dharma-relation"); + e->metadata = el_strdup("{}"); + e->weight = 0.0; + e->confidence = 1.0; + int64_t now = engram_now_ms(); + e->created_at = now; e->updated_at = now; + int64_t idx = g->edge_count; + g->edge_count++; + return idx; +} + +void dharma_strengthen(el_val_t cgi_id, el_val_t weight) { + const char* id = EL_CSTR(cgi_id); + if (!id || !*id) return; + char* base = NULL; char* url = NULL; + dharma_parse_id(id, &base, &url); + free(url); + int64_t ei = dharma_find_or_create_relation_edge(base, 1); + free(base); + if (ei < 0) return; + EngramStore* g = engram_get(); + double inc = engram_decode_score(weight); + if (!(inc >= 0.0)) inc = 0.0; + double w = g->edges[ei].weight + inc; + if (w < 0.0) w = 0.0; + if (w > 1.0) w = 1.0; + g->edges[ei].weight = w; + g->edges[ei].updated_at = engram_now_ms(); + g->edges[ei].last_fired = g->edges[ei].updated_at; +} + +el_val_t dharma_relationship(el_val_t cgi_id) { + const char* id = EL_CSTR(cgi_id); + if (!id || !*id) return el_from_float(0.0); + char* base = NULL; char* url = NULL; + dharma_parse_id(id, &base, &url); + free(url); + int64_t ei = dharma_find_or_create_relation_edge(base, 0); + free(base); + if (ei < 0) return el_from_float(0.0); + EngramStore* g = engram_get(); + return el_from_float(g->edges[ei].weight); +} + +el_val_t dharma_peers(void) { + /* Walk dharma-relation edges out of "dharma:self", weight > 0, sort desc. */ + EngramStore* g = engram_get(); + const char* self_id = "dharma:self"; + typedef struct { char* peer_base; double weight; } PeerEntry; + PeerEntry* peers = malloc((size_t)(g->edge_count + 1) * sizeof(PeerEntry)); + int64_t pcount = 0; + if (!peers) return el_list_empty(); + for (int64_t i = 0; i < g->edge_count; i++) { + EngramEdge* e = &g->edges[i]; + if (!e->from_id || !e->to_id) continue; + if (strcmp(e->from_id, self_id) != 0) continue; + if (!e->relation || strcmp(e->relation, "dharma-relation") != 0) continue; + if (e->weight <= 0.0) continue; + const char* prefix = "dharma:peer:"; + size_t pl = strlen(prefix); + if (strncmp(e->to_id, prefix, pl) != 0) continue; + peers[pcount].peer_base = el_strdup(e->to_id + pl); + peers[pcount].weight = e->weight; + pcount++; + } + /* Sort desc by weight. */ + for (int64_t i = 1; i < pcount; i++) { + PeerEntry key = peers[i]; + int64_t j = i - 1; + while (j >= 0 && peers[j].weight < key.weight) { + peers[j + 1] = peers[j]; j--; + } + peers[j + 1] = key; + } + el_val_t out = el_list_empty(); + for (int64_t i = 0; i < pcount; i++) { + out = el_list_append(out, EL_STR(peers[i].peer_base)); + } + free(peers); + return out; +} + +/* ── Batch 4: LLM (Anthropic API client) ─────────────────────────────────── */ +/* + * All LLM builtins call https://api.anthropic.com/v1/messages with the API + * key from env ANTHROPIC_API_KEY. Default model is "claude-sonnet-4-5" + * when the supplied model is empty/null. + * + * `llm_call_agentic` runs a real multi-turn tool_use/tool_result loop. + * Tool handlers are registered with `llm_register_tool(name, fn_name)`, + * which dlsym()s the named symbol. Each tool handler has the C signature + * el_val_t handler(el_val_t input_json); + * and returns a JSON-string el_val_t result. Iteration is capped at 10. + */ + +static const char* LLM_DEFAULT_MODEL = "claude-sonnet-4-5"; +static const char* LLM_API_URL = "https://api.anthropic.com/v1/messages"; +static const char* LLM_VERSION = "2023-06-01"; + +static const char* llm_resolve_model(const char* m) { + if (!m || !*m) return LLM_DEFAULT_MODEL; + return m; +} + +/* + * ── Configurable LLM provider chain ────────────────────────────────────────── + * + * Providers are configured via indexed env vars. The runtime tries each in + * order (0, 1, 2, ...) and returns the first successful non-empty response. + * + * Per provider (N = 0, 1, 2, ...): + * NEURON_LLM_N_URL — endpoint URL (base URL; /v1/chat/completions appended + * if format is "openai" and not already in URL) + * NEURON_LLM_N_KEY — API key + * NEURON_LLM_N_FORMAT — "openai" (default) or "anthropic" + * NEURON_LLM_N_MODEL — model name override (optional) + * + * Example — Neuron inference primary, Anthropic fallback: + * NEURON_LLM_0_URL=https://soma.../v1/chat/completions + * NEURON_LLM_0_KEY=svc-key + * NEURON_LLM_0_FORMAT=openai + * NEURON_LLM_0_MODEL=neuron + * NEURON_LLM_1_URL=https://api.anthropic.com/v1/messages + * NEURON_LLM_1_KEY=sk-ant-... + * NEURON_LLM_1_FORMAT=anthropic + * + * If no NEURON_LLM_0_URL is set, falls back to legacy ANTHROPIC_API_KEY. + */ + +#define LLM_MAX_PROVIDERS 16 + +/* forward declarations */ +static el_val_t llm_extract_text(el_val_t resp_val); +static el_val_t llm_extract_text_openai(el_val_t resp_val); + +static el_val_t llm_extract_text_openai(el_val_t resp_val) { + const char* resp = EL_CSTR(resp_val); + if (!resp || !*resp) return el_wrap_str(el_strdup("")); + if (resp[0] == '{' && strstr(resp, "\"error\"")) return el_wrap_str(el_strdup("")); + const char* choices = json_find_key(resp, "choices"); + if (!choices || *choices != '[') return el_wrap_str(el_strdup("")); + choices++; + while (*choices == ' ' || *choices == '\t') choices++; + if (*choices != '{') return el_wrap_str(el_strdup("")); + const char* end = json_skip_value(choices); + size_t n = (size_t)(end - choices); + char* obj = malloc(n + 1); memcpy(obj, choices, n); obj[n] = '\0'; + const char* msg = json_find_key(obj, "message"); + if (!msg || *msg != '{') { free(obj); return el_wrap_str(el_strdup("")); } + const char* msg_end = json_skip_value(msg); + size_t mn = (size_t)(msg_end - msg); + char* msg_obj = malloc(mn + 1); memcpy(msg_obj, msg, mn); msg_obj[mn] = '\0'; + const char* content = json_find_key(msg_obj, "content"); + el_val_t result = el_wrap_str(el_strdup("")); + if (content && *content == '"') { + JsonParser jp = { .p = content, .end = content + strlen(content), .err = 0 }; + char* text = jp_parse_string_raw(&jp); + if (!jp.err && text) result = el_wrap_str(text); + } + free(msg_obj); free(obj); + return result; +} + +/* Send a request to one provider. Returns the raw response string. + * format: 0 = openai, 1 = anthropic */ +static el_val_t llm_provider_request(const char* url, const char* key, + int format, const char* model, + const char* system_str, + const char* user_str) { + char* esc_sys = system_str && *system_str ? json_escape_alloc(system_str) : NULL; + char* esc_user = json_escape_alloc(user_str ? user_str : ""); + JsonBuf b; jb_init(&b); + struct curl_slist* h = NULL; + h = curl_slist_append(h, "Content-Type: application/json"); + + if (format == 0) { /* OpenAI */ + char full_url[1024]; + if (strstr(url, "/chat/completions") || strstr(url, "/messages")) { + snprintf(full_url, sizeof(full_url), "%s", url); + } else { + snprintf(full_url, sizeof(full_url), "%s/v1/chat/completions", url); + } + { size_t n = strlen(key)+24; char* l=malloc(n); snprintf(l,n,"Authorization: Bearer %s",key); h=curl_slist_append(h,l); free(l); } + jb_putc(&b, '{'); + jb_puts(&b, "\"model\":"); jb_emit_escaped(&b, model ? model : "neuron"); + jb_puts(&b, ",\"max_tokens\":4096,\"messages\":["); + if (esc_sys && *esc_sys) { jb_puts(&b,"{\"role\":\"system\",\"content\":\""); jb_puts(&b,esc_sys); jb_puts(&b,"\"},"); } + jb_puts(&b, "{\"role\":\"user\",\"content\":\""); jb_puts(&b, esc_user); jb_puts(&b, "\"}]}"); + el_val_t resp = http_do("POST", full_url, b.buf, h); + curl_slist_free_all(h); free(b.buf); + if (esc_sys) free(esc_sys); free(esc_user); + return llm_extract_text_openai(resp); + } else { /* Anthropic */ + { size_t n = strlen(key)+16; char* l=malloc(n); snprintf(l,n,"x-api-key: %s",key); h=curl_slist_append(h,l); free(l); } + { size_t n = strlen(LLM_VERSION)+32; char* l=malloc(n); snprintf(l,n,"anthropic-version: %s",LLM_VERSION); h=curl_slist_append(h,l); free(l); } + jb_putc(&b, '{'); + jb_puts(&b, "\"model\":"); jb_emit_escaped(&b, model ? model : LLM_DEFAULT_MODEL); + jb_puts(&b, ",\"max_tokens\":4096"); + if (esc_sys && *esc_sys) { jb_puts(&b,",\"system\":\""); jb_puts(&b,esc_sys); jb_puts(&b,"\""); } + jb_puts(&b, ",\"messages\":[{\"role\":\"user\",\"content\":\""); jb_puts(&b, esc_user); jb_puts(&b, "\"}]}"); + el_val_t resp = http_do("POST", url, b.buf, h); + curl_slist_free_all(h); free(b.buf); + if (esc_sys) free(esc_sys); free(esc_user); + return llm_extract_text(resp); + } +} + +static el_val_t llm_chain_call(const char* system_str, const char* user_str) { + char url_key[64], key_key[64], fmt_key[64], model_key[64]; + for (int i = 0; i < LLM_MAX_PROVIDERS; i++) { + snprintf(url_key, sizeof(url_key), "NEURON_LLM_%d_URL", i); + snprintf(key_key, sizeof(key_key), "NEURON_LLM_%d_KEY", i); + snprintf(fmt_key, sizeof(fmt_key), "NEURON_LLM_%d_FORMAT", i); + snprintf(model_key, sizeof(model_key), "NEURON_LLM_%d_MODEL", i); + const char* url = getenv(url_key); + const char* key = getenv(key_key); + if (!url || !*url || !key || !*key) break; /* end of chain */ + const char* fmt_s = getenv(fmt_key); + int fmt = (fmt_s && strcmp(fmt_s, "anthropic") == 0) ? 1 : 0; + const char* model = getenv(model_key); + fprintf(stderr, "[llm] trying provider %d (%s)\n", i, url); + el_val_t result = llm_provider_request(url, key, fmt, model, system_str, user_str); + const char* t = EL_CSTR(result); + if (t && *t && t[0] != '{') return result; /* success */ + fprintf(stderr, "[llm] provider %d failed or empty, trying next\n", i); + } + /* Legacy fallback: ANTHROPIC_API_KEY */ + const char* api_key = getenv("ANTHROPIC_API_KEY"); + if (!api_key || !*api_key) return http_error_json("no LLM providers configured"); + fprintf(stderr, "[llm] using legacy ANTHROPIC_API_KEY fallback\n"); + return llm_provider_request(LLM_API_URL, api_key, 1, NULL, system_str, user_str); +} + +/* Legacy llm_request — kept for backward compat with agentic loop internals */ +static el_val_t llm_request(const char* json_body) { + const char* api_key = getenv("ANTHROPIC_API_KEY"); + if (!api_key || !*api_key) return http_error_json("ANTHROPIC_API_KEY not set"); + struct curl_slist* h = NULL; + h = curl_slist_append(h, "Content-Type: application/json"); + { size_t n=strlen(api_key)+16; char* l=malloc(n); snprintf(l,n,"x-api-key: %s",api_key); h=curl_slist_append(h,l); free(l); } + { size_t n=strlen(LLM_VERSION)+32; char* l=malloc(n); snprintf(l,n,"anthropic-version: %s",LLM_VERSION); h=curl_slist_append(h,l); free(l); } + el_val_t resp = http_do("POST", LLM_API_URL, json_body, h); + curl_slist_free_all(h); + return resp; +} + +/* Extract concatenated assistant text from an Anthropic /v1/messages + * response. The response shape is: + * {"content":[{"type":"text","text":"..."}, ...], ...} + * If parsing fails, returns the raw response so the caller can inspect. + */ +static el_val_t llm_extract_text(el_val_t resp_val) { + const char* resp = EL_CSTR(resp_val); + if (!resp || !*resp) return el_wrap_str(el_strdup("")); + /* If error JSON, propagate as-is. */ + if (resp[0] == '{' && strstr(resp, "\"error\"")) { + return el_wrap_str(el_strdup(resp)); + } + /* Find "content":[ ... ] */ + const char* p = json_find_key(resp, "content"); + if (!p) return el_wrap_str(el_strdup(resp)); + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + if (*p != '[') return el_wrap_str(el_strdup(resp)); + p++; + JsonBuf out; jb_init(&out); + while (*p && *p != ']') { + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',') p++; + if (*p != '{') break; + const char* end = json_skip_value(p); + size_t n = (size_t)(end - p); + char* obj = malloc(n + 1); + memcpy(obj, p, n); obj[n] = '\0'; + const char* type_p = json_find_key(obj, "type"); + if (type_p && *type_p == '"') { + JsonParser jp = { .p = type_p, .end = type_p + strlen(type_p), .err = 0 }; + char* type_s = jp_parse_string_raw(&jp); + if (!jp.err && type_s && strcmp(type_s, "text") == 0) { + const char* tp = json_find_key(obj, "text"); + if (tp && *tp == '"') { + JsonParser jp2 = { .p = tp, .end = tp + strlen(tp), .err = 0 }; + char* text_s = jp_parse_string_raw(&jp2); + if (!jp2.err && text_s) jb_puts(&out, text_s); + free(text_s); + } + } + free(type_s); + } + free(obj); + p = end; + } + return el_wrap_str(out.buf); +} + +el_val_t llm_call(el_val_t model, el_val_t prompt) { + const char* u = EL_CSTR(prompt); if (!u) u = ""; + return llm_chain_call(NULL, u); +} + +el_val_t llm_call_system(el_val_t model, el_val_t system_prompt, el_val_t user_prompt) { + const char* s = EL_CSTR(system_prompt); if (!s) s = ""; + const char* u = EL_CSTR(user_prompt); if (!u) u = ""; + return llm_chain_call(s, u); +} + +/* ── Tool registry for llm_call_agentic ─────────────────────────────────── */ + +typedef el_val_t (*llm_tool_fn)(el_val_t input); + +typedef struct LlmToolEntry { + char* name; + llm_tool_fn fn; +} LlmToolEntry; + +static LlmToolEntry _llm_tools[64]; +static size_t _llm_tool_count = 0; +static pthread_mutex_t _llm_tool_mu = PTHREAD_MUTEX_INITIALIZER; + +static llm_tool_fn llm_tool_lookup(const char* name) { + if (!name) return NULL; + llm_tool_fn fn = NULL; + pthread_mutex_lock(&_llm_tool_mu); + for (size_t i = 0; i < _llm_tool_count; i++) { + if (strcmp(_llm_tools[i].name, name) == 0) { fn = _llm_tools[i].fn; break; } + } + pthread_mutex_unlock(&_llm_tool_mu); + return fn; +} + +void llm_register_tool(el_val_t name, el_val_t handler_fn_name) { + const char* nm = EL_CSTR(name); + const char* sym = EL_CSTR(handler_fn_name); + if (!nm || !*nm || !sym || !*sym) return; + void* p = dlsym(RTLD_DEFAULT, sym); + if (!p) { + fprintf(stderr, "[llm_register_tool] symbol not found: %s\n", sym); + return; + } + pthread_mutex_lock(&_llm_tool_mu); + /* Replace existing entry by name. */ + for (size_t i = 0; i < _llm_tool_count; i++) { + if (strcmp(_llm_tools[i].name, nm) == 0) { + _llm_tools[i].fn = (llm_tool_fn)p; + pthread_mutex_unlock(&_llm_tool_mu); + return; + } + } + if (_llm_tool_count < sizeof(_llm_tools) / sizeof(_llm_tools[0])) { + _llm_tools[_llm_tool_count].name = el_strdup(nm); + _llm_tools[_llm_tool_count].fn = (llm_tool_fn)p; + _llm_tool_count++; + } + pthread_mutex_unlock(&_llm_tool_mu); +} + +/* Serialize the El `tools` list into the JSON `tools:[...]` field expected + * by the Anthropic API. Each tool is an ElMap with name/description/ + * input_schema. input_schema is treated as either a JSON-object string + * (passed through verbatim) or a missing field (substitute {}). */ +static void llm_emit_tools_json(JsonBuf* b, el_val_t tools_list) { + jb_putc(b, '['); + ElList* lst = (ElList*)(uintptr_t)tools_list; + int64_t n = lst ? lst->length : 0; + for (int64_t i = 0; i < n; i++) { + if (i > 0) jb_putc(b, ','); + ElMap* tm = as_map(lst->elems[i]); + const char* name = ""; + const char* desc = ""; + const char* schema = "{}"; + if (tm) { + for (int64_t k = 0; k < tm->count; k++) { + const char* key = EL_CSTR(tm->keys[k]); + const char* val = EL_CSTR(tm->values[k]); + if (!key || !val) continue; + if (strcmp(key, "name") == 0) name = val; + else if (strcmp(key, "description") == 0) desc = val; + else if (strcmp(key, "input_schema") == 0) schema = val; + } + } + char* esc_name = json_escape_alloc(name); + char* esc_desc = json_escape_alloc(desc); + jb_puts(b, "{\"name\":\""); jb_puts(b, esc_name); + jb_puts(b, "\",\"description\":\""); jb_puts(b, esc_desc); + jb_puts(b, "\",\"input_schema\":"); jb_puts(b, schema && *schema ? schema : "{}"); + jb_putc(b, '}'); + free(esc_name); free(esc_desc); + } + jb_putc(b, ']'); +} + +/* Walk the assistant `content` array and emit each block back into b, + * preserving the verbatim JSON of every block — used to re-include the + * assistant turn in the next request. */ +static void llm_emit_content_blocks(JsonBuf* b, const char* resp) { + const char* p = json_find_key(resp, "content"); + jb_putc(b, '['); + if (!p) { jb_putc(b, ']'); return; } + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + if (*p != '[') { jb_putc(b, ']'); return; } + p++; + int first = 1; + while (*p && *p != ']') { + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',') p++; + if (*p != '{') break; + const char* end = json_skip_value(p); + if (!first) jb_putc(b, ','); + first = 0; + size_t n = (size_t)(end - p); + jb_reserve(b, n); + memcpy(b->buf + b->len, p, n); + b->len += n; + b->buf[b->len] = '\0'; + p = end; + } + jb_putc(b, ']'); +} + +/* Concatenate all "text" blocks from a response. Returns owned string. */ +static char* llm_concat_text_blocks(const char* resp) { + JsonBuf out; jb_init(&out); + if (!resp) return out.buf; + const char* p = json_find_key(resp, "content"); + if (!p) return out.buf; + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + if (*p != '[') return out.buf; + p++; + while (*p && *p != ']') { + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',') p++; + if (*p != '{') break; + const char* end = json_skip_value(p); + size_t n = (size_t)(end - p); + char* obj = malloc(n + 1); + memcpy(obj, p, n); obj[n] = '\0'; + const char* tp = json_find_key(obj, "type"); + if (tp && *tp == '"') { + JsonParser jp = { .p = tp, .end = tp + strlen(tp), .err = 0 }; + char* tname = jp_parse_string_raw(&jp); + if (!jp.err && tname && strcmp(tname, "text") == 0) { + const char* xp = json_find_key(obj, "text"); + if (xp && *xp == '"') { + JsonParser jp2 = { .p = xp, .end = xp + strlen(xp), .err = 0 }; + char* txt = jp_parse_string_raw(&jp2); + if (!jp2.err && txt) jb_puts(&out, txt); + free(txt); + } + } + free(tname); + } + free(obj); + p = end; + } + return out.buf; +} + +/* Build tool_result message blocks for every tool_use in a response. + * Appends to `b` an array element for each tool_use; caller wraps. */ +static int llm_build_tool_results(JsonBuf* b, const char* resp) { + int any = 0; + const char* p = json_find_key(resp, "content"); + if (!p) return 0; + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; + if (*p != '[') return 0; + p++; + while (*p && *p != ']') { + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',') p++; + if (*p != '{') break; + const char* end = json_skip_value(p); + size_t n = (size_t)(end - p); + char* obj = malloc(n + 1); + memcpy(obj, p, n); obj[n] = '\0'; + + const char* tp = json_find_key(obj, "type"); + char* type_s = NULL; + if (tp && *tp == '"') { + JsonParser jp = { .p = tp, .end = tp + strlen(tp), .err = 0 }; + type_s = jp_parse_string_raw(&jp); + } + if (type_s && strcmp(type_s, "tool_use") == 0) { + /* Extract id, name, input. */ + char* id_s = NULL; char* name_s = NULL; + const char* idp = json_find_key(obj, "id"); + if (idp && *idp == '"') { + JsonParser jp = { .p = idp, .end = idp + strlen(idp), .err = 0 }; + id_s = jp_parse_string_raw(&jp); + } + const char* np = json_find_key(obj, "name"); + if (np && *np == '"') { + JsonParser jp = { .p = np, .end = np + strlen(np), .err = 0 }; + name_s = jp_parse_string_raw(&jp); + } + el_val_t input_raw = json_get_raw(EL_STR(obj), EL_STR("input")); + const char* input_s = EL_CSTR(input_raw); + if (!input_s || !*input_s) input_s = "{}"; + + llm_tool_fn fn = llm_tool_lookup(name_s ? name_s : ""); + char* result = NULL; + int is_error = 0; + if (!fn) { + size_t en = strlen(name_s ? name_s : "(null)") + 64; + result = malloc(en); + snprintf(result, en, "{\"error\":\"tool not registered: %s\"}", + name_s ? name_s : "(null)"); + is_error = 1; + } else { + el_val_t out = fn(EL_STR(input_s)); + const char* os = EL_CSTR(out); + result = el_strdup(os ? os : ""); + } + + if (any) jb_putc(b, ','); + char* esc_id = json_escape_alloc(id_s ? id_s : ""); + char* esc_res = json_escape_alloc(result ? result : ""); + jb_puts(b, "{\"type\":\"tool_result\",\"tool_use_id\":\""); + jb_puts(b, esc_id); + jb_puts(b, "\",\"content\":\""); + jb_puts(b, esc_res); + jb_puts(b, "\""); + if (is_error) jb_puts(b, ",\"is_error\":true"); + jb_putc(b, '}'); + free(esc_id); free(esc_res); free(result); + free(id_s); free(name_s); + any = 1; + } + free(type_s); + free(obj); + p = end; + } + return any; +} + +el_val_t llm_call_agentic(el_val_t model, el_val_t system, el_val_t user, el_val_t tools) { + /* Empty tools list → degrade to plain system call. */ + ElList* tl = (ElList*)(uintptr_t)tools; + if (!tl || tl->length == 0) { + return llm_call_system(model, system, user); + } + const char* m = llm_resolve_model(EL_CSTR(model)); + const char* sys_p = EL_CSTR(system); if (!sys_p) sys_p = ""; + const char* usr_p = EL_CSTR(user); if (!usr_p) usr_p = ""; + + /* Build the static parts: tools JSON and system prompt — these don't + * change across iterations. */ + JsonBuf tools_buf; jb_init(&tools_buf); + llm_emit_tools_json(&tools_buf, tools); + char* esc_sys = json_escape_alloc(sys_p); + + /* messages array, accumulated as a mutable JSON fragment (no surrounding + * brackets — emitted at request time). */ + JsonBuf msgs; jb_init(&msgs); + /* First user message. */ + char* esc_user = json_escape_alloc(usr_p); + jb_puts(&msgs, "{\"role\":\"user\",\"content\":\""); + jb_puts(&msgs, esc_user); + jb_puts(&msgs, "\"}"); + free(esc_user); + + char* last_text = el_strdup(""); + el_val_t final_out = 0; + int reached_cap = 1; + + for (int iter = 0; iter < 10; iter++) { + /* Build request body. */ + JsonBuf body; jb_init(&body); + jb_putc(&body, '{'); + jb_puts(&body, "\"model\":"); jb_emit_escaped(&body, m); + jb_puts(&body, ",\"max_tokens\":4096"); + if (*sys_p) { + jb_puts(&body, ",\"system\":\""); + jb_puts(&body, esc_sys); + jb_puts(&body, "\""); + } + jb_puts(&body, ",\"tools\":"); + jb_puts(&body, tools_buf.buf); + jb_puts(&body, ",\"messages\":["); + jb_puts(&body, msgs.buf); + jb_puts(&body, "]}"); + + el_val_t resp_v = llm_request(body.buf); + free(body.buf); + const char* resp = EL_CSTR(resp_v); + if (!resp || !*resp) { + final_out = http_error_json("empty response"); + reached_cap = 0; + break; + } + if (resp[0] == '{' && strstr(resp, "\"error\"") && + !json_find_key(resp, "content")) { + final_out = el_wrap_str(el_strdup(resp)); + reached_cap = 0; + break; + } + + /* Update last_text from this response. */ + free(last_text); + last_text = llm_concat_text_blocks(resp); + + /* Inspect stop_reason. */ + el_val_t sr_v = json_get_string(EL_STR(resp), EL_STR("stop_reason")); + const char* sr = EL_CSTR(sr_v); if (!sr) sr = ""; + + if (strcmp(sr, "end_turn") == 0) { + final_out = el_wrap_str(el_strdup(last_text)); + reached_cap = 0; + break; + } + if (strcmp(sr, "max_tokens") == 0) { + size_t ln = strlen(last_text) + 16; + char* out = malloc(ln); + snprintf(out, ln, "%s\n[truncated]", last_text); + final_out = el_wrap_str(out); + reached_cap = 0; + break; + } + if (strcmp(sr, "tool_use") != 0) { + /* Unexpected stop reason; return the text we have. */ + final_out = el_wrap_str(el_strdup(last_text)); + reached_cap = 0; + break; + } + + /* Append the assistant turn (raw content blocks) to messages. */ + JsonBuf ab; jb_init(&ab); + jb_puts(&ab, ",{\"role\":\"assistant\",\"content\":"); + llm_emit_content_blocks(&ab, resp); + jb_putc(&ab, '}'); + jb_puts(&msgs, ab.buf); + free(ab.buf); + + /* Build tool_result message. */ + JsonBuf tr; jb_init(&tr); + jb_puts(&tr, ",{\"role\":\"user\",\"content\":["); + int any = llm_build_tool_results(&tr, resp); + jb_puts(&tr, "]}"); + if (any) { + jb_puts(&msgs, tr.buf); + } + free(tr.buf); + } + + if (reached_cap) { + size_t ln = strlen(last_text) + 32; + char* out = malloc(ln); + snprintf(out, ln, "[loop_cap_reached]\n%s", last_text); + final_out = el_wrap_str(out); + } + free(last_text); + free(esc_sys); + free(tools_buf.buf); + free(msgs.buf); + return final_out; +} + +/* base64-encode arbitrary bytes (returns owned C string). + * Internal helper for llm_vision; the public crypto entry point that El + * programs call is `base64_encode(el_val_t)` defined in the crypto block + * at the end of this file. */ +static char* el_b64_encode_internal(const unsigned char* src, size_t n) { + static const char tbl[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + size_t out_len = 4 * ((n + 2) / 3); + char* out = malloc(out_len + 1); + if (!out) return NULL; + size_t o = 0; + for (size_t i = 0; i < n;) { + uint32_t v = 0; int got = 0; + v |= (uint32_t)src[i++] << 16; got++; + if (i < n) { v |= (uint32_t)src[i++] << 8; got++; } + if (i < n) { v |= (uint32_t)src[i++]; got++; } + out[o++] = tbl[(v >> 18) & 0x3f]; + out[o++] = tbl[(v >> 12) & 0x3f]; + out[o++] = (got > 1) ? tbl[(v >> 6) & 0x3f] : '='; + out[o++] = (got > 2) ? tbl[v & 0x3f] : '='; + } + out[o] = '\0'; + return out; +} + +el_val_t llm_vision(el_val_t model, el_val_t system, el_val_t prompt, el_val_t image_url_or_b64) { + const char* m = llm_resolve_model(EL_CSTR(model)); + const char* s = EL_CSTR(system); if (!s) s = ""; + const char* u = EL_CSTR(prompt); if (!u) u = ""; + const char* img = EL_CSTR(image_url_or_b64); if (!img) img = ""; + + /* Choose source mode */ + char* image_block = NULL; + if (strncasecmp(img, "http://", 7) == 0 || strncasecmp(img, "https://", 8) == 0) { + char* esc_url = json_escape_alloc(img); + size_t n = strlen(esc_url) + 128; + image_block = malloc(n); + snprintf(image_block, n, + "{\"type\":\"image\",\"source\":{\"type\":\"url\",\"url\":\"%s\"}}", + esc_url); + free(esc_url); + } else if (strncmp(img, "data:", 5) == 0) { + /* Inline data URL: split media-type and base64 */ + const char* semi = strchr(img + 5, ';'); + const char* comma = strchr(img + 5, ','); + char media[64] = "image/png"; + if (semi && comma && semi < comma) { + size_t ml = (size_t)(semi - (img + 5)); + if (ml >= sizeof(media)) ml = sizeof(media) - 1; + memcpy(media, img + 5, ml); media[ml] = '\0'; + } + const char* b64 = comma ? comma + 1 : ""; + char* esc_media = json_escape_alloc(media); + char* esc_b64 = json_escape_alloc(b64); + size_t n = strlen(esc_media) + strlen(esc_b64) + 192; + image_block = malloc(n); + snprintf(image_block, n, + "{\"type\":\"image\",\"source\":{\"type\":\"base64\"," + "\"media_type\":\"%s\",\"data\":\"%s\"}}", + esc_media, esc_b64); + free(esc_media); free(esc_b64); + } else if (*img) { + /* Treat as file path: read, base64-encode, attach. */ + FILE* f = fopen(img, "rb"); + if (!f) { + char err[256]; snprintf(err, sizeof(err), "cannot open image: %s", img); + return http_error_json(err); + } + fseek(f, 0, SEEK_END); + long sz = ftell(f); + rewind(f); + if (sz <= 0) { fclose(f); return http_error_json("empty image file"); } + unsigned char* buf = malloc((size_t)sz); + if (!buf) { fclose(f); return http_error_json("oom"); } + size_t got = fread(buf, 1, (size_t)sz, f); + fclose(f); + char* b64 = el_b64_encode_internal(buf, got); + free(buf); + if (!b64) return http_error_json("base64 encode failed"); + const char* media = "image/png"; + size_t ilen = strlen(img); + if (ilen >= 4) { + if (strcasecmp(img + ilen - 4, ".jpg") == 0 || + (ilen >= 5 && strcasecmp(img + ilen - 5, ".jpeg") == 0)) media = "image/jpeg"; + else if (strcasecmp(img + ilen - 4, ".gif") == 0) media = "image/gif"; + else if (strcasecmp(img + ilen - 4, ".webp") == 0) media = "image/webp"; + } + char* esc_b64 = json_escape_alloc(b64); free(b64); + size_t n = strlen(esc_b64) + 192; + image_block = malloc(n); + snprintf(image_block, n, + "{\"type\":\"image\",\"source\":{\"type\":\"base64\"," + "\"media_type\":\"%s\",\"data\":\"%s\"}}", + media, esc_b64); + free(esc_b64); + } + + char* esc_sys = json_escape_alloc(s); + char* esc_user = json_escape_alloc(u); + JsonBuf b; jb_init(&b); + jb_putc(&b, '{'); + jb_puts(&b, "\"model\":"); jb_emit_escaped(&b, m); + jb_puts(&b, ",\"max_tokens\":4096"); + if (*s) { + jb_puts(&b, ",\"system\":\""); + jb_puts(&b, esc_sys); + jb_puts(&b, "\""); + } + jb_puts(&b, ",\"messages\":[{\"role\":\"user\",\"content\":["); + if (image_block) { + jb_puts(&b, image_block); + jb_putc(&b, ','); + } + jb_puts(&b, "{\"type\":\"text\",\"text\":\""); + jb_puts(&b, esc_user); + jb_puts(&b, "\"}]}]}"); + free(esc_sys); free(esc_user); free(image_block); + el_val_t resp = llm_request(b.buf); + free(b.buf); + return llm_extract_text(resp); +} + +el_val_t llm_models(void) { + el_val_t lst = el_list_empty(); + lst = el_list_append(lst, el_wrap_str(el_strdup("claude-sonnet-4-5"))); + lst = el_list_append(lst, el_wrap_str(el_strdup("claude-opus-4-7"))); + lst = el_list_append(lst, el_wrap_str(el_strdup("claude-haiku-4-5"))); + return lst; +} + +/* ── Native VM builtin aliases ────────────────────────────────────────────── + * El source files use native_* names (El VM builtins). + * When compiled to C, these map directly to el_* runtime functions. */ + +el_val_t native_list_get(el_val_t list, el_val_t index) { + return el_list_get(list, index); +} + +el_val_t native_list_len(el_val_t list) { + return el_list_len(list); +} + +el_val_t native_list_append(el_val_t list, el_val_t elem) { + return el_list_append(list, elem); +} + +el_val_t native_list_empty(void) { + return el_list_empty(); +} + +el_val_t native_list_clone(el_val_t list) { + return el_list_clone(list); +} + +el_val_t native_string_chars(el_val_t sv) { + const char* s = EL_CSTR(sv); + el_val_t result = el_list_empty(); + if (!s) return result; + while (*s) { + char buf[2]; + buf[0] = *s; + buf[1] = '\0'; + result = el_list_append(result, EL_STR(strdup(buf))); + s++; + } + return result; +} + +el_val_t native_int_to_str(el_val_t n) { + return int_to_str(n); +} + +/* ── Method-call shorthand aliases ────────────────────────────────────────── + * Short names that result from the method-call convention: + * myList.append(x) → append(myList, x) + * myList.len() → len(myList) + * myList.get(i) → get(myList, i) + * myMap.map_get(k) → map_get(myMap, k) + * myMap.map_set(k,v) → map_set(myMap, k, v) */ + +el_val_t append(el_val_t list, el_val_t elem) { return el_list_append(list, elem); } +el_val_t len(el_val_t list) { return el_list_len(list); } +el_val_t get(el_val_t list, el_val_t index) { return el_list_get(list, index); } +el_val_t map_get(el_val_t map, el_val_t key) { return el_map_get(map, key); } +el_val_t map_set(el_val_t map, el_val_t key, el_val_t value) { return el_map_set(map, key, value); } + +/* ── Crypto primitives ────────────────────────────────────────────────────── + * + * SHA-256 implementation adapted from Brad Conte's public-domain reference + * (https://github.com/B-Con/crypto-algorithms/blob/master/sha256.c, public + * domain per the project's LICENSE). HMAC follows RFC 2104. Base64 encoding + * follows RFC 4648; the URL-safe variant uses the alphabet from §5 of the + * RFC and omits padding (per JWT/JWS convention). + * + * Self-contained: no OpenSSL/libcrypto dependency. The runtime keeps its + * existing `-lcurl -lpthread -ldl -lm` link line. + * + * Binary outputs (sha256_bytes, hmac_sha256_bytes) tag their buffer with a + * magic header so base64_encode/base64url_encode can recover the exact byte + * length even when the payload contains embedded NULs. Plain C strings + * (without the header) fall back to strlen(), preserving the existing API + * shape for normal text inputs. */ + +/* Magic-header for length-tagged binary buffers. Layout: + * [ uint32_t magic = EL_MAGIC_BIN ][ uint32_t length ][ data... ][ \0 ] + * The returned el_val_t points at `data`, so consumers that strlen() it still + * get a sensible (though possibly truncated) view. el_bin_len() recovers the + * true length by sniffing the 8 bytes preceding the pointer. + * + * Magic value chosen with high MSB so it cannot collide with printable ASCII + * (the same discriminator pattern used by EL_MAGIC_LIST / EL_MAGIC_MAP). */ +#define EL_MAGIC_BIN 0xE1B17EAFu + +typedef struct { + uint32_t magic; + uint32_t length; +} el_bin_hdr_t; + +/* Allocate a length-tagged binary buffer; returns pointer to the data area. */ +static unsigned char* el_bin_alloc(size_t len) { + el_bin_hdr_t* hdr = (el_bin_hdr_t*)malloc(sizeof(el_bin_hdr_t) + len + 1); + if (!hdr) { fputs("el_runtime: out of memory (bin)\n", stderr); exit(1); } + hdr->magic = EL_MAGIC_BIN; + hdr->length = (uint32_t)len; + unsigned char* data = (unsigned char*)(hdr + 1); + data[len] = '\0'; /* keep NUL-terminated for accidental strlen calls */ + return data; +} + +/* Recover length from a possibly-tagged buffer. Returns 1 if tagged. */ +static int el_bin_lookup(const void* p, size_t* out_len) { + if (!p) { *out_len = 0; return 0; } + /* Avoid reading off the front of a page on tiny pointers (e.g. NULs + * passed in as int-cast values). 4096 is a safe lower bound on any + * platform we target. */ + if ((uintptr_t)p < 4096) return 0; + const el_bin_hdr_t* hdr = (const el_bin_hdr_t*)((const char*)p - sizeof(el_bin_hdr_t)); + if (hdr->magic != EL_MAGIC_BIN) return 0; + *out_len = hdr->length; + return 1; +} + +/* Effective input length: tagged length if present, else strlen. */ +static size_t el_input_len(const char* s) { + size_t n; + if (el_bin_lookup(s, &n)) return n; + return s ? strlen(s) : 0; +} + +/* ─── SHA-256 (Brad Conte / public domain) ──────────────────────────────── */ + +typedef struct { + unsigned char data[64]; + uint32_t datalen; + uint64_t bitlen; + uint32_t state[8]; +} el_sha256_ctx_t; + +static const uint32_t el_sha256_k[64] = { + 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 +}; + +#define EL_ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) +#define EL_CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) +#define EL_MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define EL_EP0(x) (EL_ROTR(x,2) ^ EL_ROTR(x,13) ^ EL_ROTR(x,22)) +#define EL_EP1(x) (EL_ROTR(x,6) ^ EL_ROTR(x,11) ^ EL_ROTR(x,25)) +#define EL_SIG0(x) (EL_ROTR(x,7) ^ EL_ROTR(x,18) ^ ((x) >> 3)) +#define EL_SIG1(x) (EL_ROTR(x,17) ^ EL_ROTR(x,19) ^ ((x) >> 10)) + +static void el_sha256_transform(el_sha256_ctx_t* ctx, const unsigned char* data) { + uint32_t a, b, c, d, e, f, g, h, t1, t2, m[64]; + int i, j; + for (i = 0, j = 0; i < 16; ++i, j += 4) { + m[i] = ((uint32_t)data[j] << 24) | ((uint32_t)data[j + 1] << 16) + | ((uint32_t)data[j + 2] << 8) | (uint32_t)data[j + 3]; + } + for (; i < 64; ++i) { + m[i] = EL_SIG1(m[i-2]) + m[i-7] + EL_SIG0(m[i-15]) + m[i-16]; + } + a = ctx->state[0]; b = ctx->state[1]; c = ctx->state[2]; d = ctx->state[3]; + e = ctx->state[4]; f = ctx->state[5]; g = ctx->state[6]; h = ctx->state[7]; + for (i = 0; i < 64; ++i) { + t1 = h + EL_EP1(e) + EL_CH(e,f,g) + el_sha256_k[i] + m[i]; + t2 = EL_EP0(a) + EL_MAJ(a,b,c); + h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; + } + ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c; ctx->state[3] += d; + ctx->state[4] += e; ctx->state[5] += f; ctx->state[6] += g; ctx->state[7] += h; +} + +static void el_sha256_init(el_sha256_ctx_t* ctx) { + ctx->datalen = 0; + ctx->bitlen = 0; + ctx->state[0] = 0x6a09e667; ctx->state[1] = 0xbb67ae85; + ctx->state[2] = 0x3c6ef372; ctx->state[3] = 0xa54ff53a; + ctx->state[4] = 0x510e527f; ctx->state[5] = 0x9b05688c; + ctx->state[6] = 0x1f83d9ab; ctx->state[7] = 0x5be0cd19; +} + +static void el_sha256_update(el_sha256_ctx_t* ctx, const unsigned char* data, size_t len) { + for (size_t i = 0; i < len; ++i) { + ctx->data[ctx->datalen++] = data[i]; + if (ctx->datalen == 64) { + el_sha256_transform(ctx, ctx->data); + ctx->bitlen += 512; + ctx->datalen = 0; + } + } +} + +static void el_sha256_final(el_sha256_ctx_t* ctx, unsigned char hash[32]) { + uint32_t i = ctx->datalen; + if (ctx->datalen < 56) { + ctx->data[i++] = 0x80; + while (i < 56) ctx->data[i++] = 0x00; + } else { + ctx->data[i++] = 0x80; + while (i < 64) ctx->data[i++] = 0x00; + el_sha256_transform(ctx, ctx->data); + memset(ctx->data, 0, 56); + } + ctx->bitlen += (uint64_t)ctx->datalen * 8; + ctx->data[63] = (unsigned char)( ctx->bitlen & 0xff); + ctx->data[62] = (unsigned char)((ctx->bitlen >> 8) & 0xff); + ctx->data[61] = (unsigned char)((ctx->bitlen >> 16) & 0xff); + ctx->data[60] = (unsigned char)((ctx->bitlen >> 24) & 0xff); + ctx->data[59] = (unsigned char)((ctx->bitlen >> 32) & 0xff); + ctx->data[58] = (unsigned char)((ctx->bitlen >> 40) & 0xff); + ctx->data[57] = (unsigned char)((ctx->bitlen >> 48) & 0xff); + ctx->data[56] = (unsigned char)((ctx->bitlen >> 56) & 0xff); + el_sha256_transform(ctx, ctx->data); + for (i = 0; i < 4; ++i) { + hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0xff; + hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0xff; + hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0xff; + hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0xff; + hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0xff; + hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0xff; + hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0xff; + hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0xff; + } +} + +static void el_sha256_oneshot(const unsigned char* data, size_t len, unsigned char out[32]) { + el_sha256_ctx_t c; + el_sha256_init(&c); + el_sha256_update(&c, data, len); + el_sha256_final(&c, out); +} + +/* ─── HMAC-SHA-256 (RFC 2104) ───────────────────────────────────────────── */ + +static void el_hmac_sha256(const unsigned char* key, size_t key_len, + const unsigned char* msg, size_t msg_len, + unsigned char out[32]) { + unsigned char k[64]; + unsigned char k_ipad[64]; + unsigned char k_opad[64]; + unsigned char inner[32]; + + if (key_len > 64) { + el_sha256_oneshot(key, key_len, k); + memset(k + 32, 0, 32); + } else { + memcpy(k, key, key_len); + memset(k + key_len, 0, 64 - key_len); + } + for (int i = 0; i < 64; ++i) { + k_ipad[i] = k[i] ^ 0x36; + k_opad[i] = k[i] ^ 0x5c; + } + { + el_sha256_ctx_t c; + el_sha256_init(&c); + el_sha256_update(&c, k_ipad, 64); + el_sha256_update(&c, msg, msg_len); + el_sha256_final(&c, inner); + } + { + el_sha256_ctx_t c; + el_sha256_init(&c); + el_sha256_update(&c, k_opad, 64); + el_sha256_update(&c, inner, 32); + el_sha256_final(&c, out); + } +} + +/* ─── Hex helper ────────────────────────────────────────────────────────── */ + +static el_val_t el_hex_encode(const unsigned char* data, size_t len) { + static const char digits[] = "0123456789abcdef"; + char* out = el_strbuf(len * 2); + for (size_t i = 0; i < len; ++i) { + out[i * 2] = digits[(data[i] >> 4) & 0xf]; + out[i * 2 + 1] = digits[ data[i] & 0xf]; + } + out[len * 2] = '\0'; + return el_wrap_str(out); +} + +/* ─── Base64 (RFC 4648) ─────────────────────────────────────────────────── */ + +static const char el_b64_std_alphabet[64] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char el_b64_url_alphabet[64] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + +el_val_t el_base64_encode_n(const unsigned char* data, size_t len, int url_safe) { + const char* alphabet = url_safe ? el_b64_url_alphabet : el_b64_std_alphabet; + /* Standard form is padded to multiple of 4; URL-safe omits padding. */ + size_t out_cap = ((len + 2) / 3) * 4 + 1; + char* out = el_strbuf(out_cap); + size_t i = 0, j = 0; + while (i + 3 <= len) { + uint32_t v = ((uint32_t)data[i] << 16) | ((uint32_t)data[i+1] << 8) | (uint32_t)data[i+2]; + out[j++] = alphabet[(v >> 18) & 0x3f]; + out[j++] = alphabet[(v >> 12) & 0x3f]; + out[j++] = alphabet[(v >> 6) & 0x3f]; + out[j++] = alphabet[ v & 0x3f]; + i += 3; + } + size_t rem = len - i; + if (rem == 1) { + uint32_t v = (uint32_t)data[i] << 16; + out[j++] = alphabet[(v >> 18) & 0x3f]; + out[j++] = alphabet[(v >> 12) & 0x3f]; + if (!url_safe) { out[j++] = '='; out[j++] = '='; } + } else if (rem == 2) { + uint32_t v = ((uint32_t)data[i] << 16) | ((uint32_t)data[i+1] << 8); + out[j++] = alphabet[(v >> 18) & 0x3f]; + out[j++] = alphabet[(v >> 12) & 0x3f]; + out[j++] = alphabet[(v >> 6) & 0x3f]; + if (!url_safe) { out[j++] = '='; } + } + out[j] = '\0'; + return el_wrap_str(out); +} + +/* Decode either alphabet — accepts both '+/' and '-_' transparently, and + * tolerates missing padding (which JWTs typically omit). Whitespace is + * skipped for robustness. Invalid characters cause the decode to stop and + * the partial result so far is returned. */ +static el_val_t el_base64_decode_any(const char* in) { + if (!in) { + unsigned char* empty = el_bin_alloc(0); + return EL_STR((char*)empty); + } + size_t in_len = strlen(in); + /* Worst case: 3 output bytes per 4 input chars, +1 NUL slack. */ + unsigned char* out = el_bin_alloc(((in_len + 3) / 4) * 3 + 1); + + int8_t lut[256]; + for (int i = 0; i < 256; ++i) lut[i] = -1; + for (int i = 0; i < 64; ++i) lut[(unsigned char)el_b64_std_alphabet[i]] = (int8_t)i; + /* Allow URL-safe characters too (so one decoder handles both forms). */ + lut[(unsigned char)'-'] = 62; + lut[(unsigned char)'_'] = 63; + + uint32_t buf = 0; + int bits = 0; + size_t o = 0; + for (size_t i = 0; i < in_len; ++i) { + unsigned char c = (unsigned char)in[i]; + if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') continue; + int8_t v = lut[c]; + if (v < 0) break; /* invalid char — stop */ + buf = (buf << 6) | (uint32_t)v; + bits += 6; + if (bits >= 8) { + bits -= 8; + out[o++] = (unsigned char)((buf >> bits) & 0xff); + } + } + /* Patch the length header to the actual decoded length. */ + el_bin_hdr_t* hdr = (el_bin_hdr_t*)((char*)out - sizeof(el_bin_hdr_t)); + hdr->length = (uint32_t)o; + out[o] = '\0'; + return EL_STR((char*)out); +} + +/* ─── Public crypto entry points ────────────────────────────────────────── */ + +el_val_t el_sha256_bytes_n(const unsigned char* data, size_t len) { + unsigned char* out = el_bin_alloc(32); + el_sha256_oneshot(data, len, out); + return EL_STR((char*)out); +} + +el_val_t sha256_hex(el_val_t input) { + const char* s = EL_CSTR(input); + size_t n = el_input_len(s); + unsigned char digest[32]; + el_sha256_oneshot((const unsigned char*)(s ? s : ""), n, digest); + return el_hex_encode(digest, 32); +} + +el_val_t sha256_bytes(el_val_t input) { + const char* s = EL_CSTR(input); + size_t n = el_input_len(s); + return el_sha256_bytes_n((const unsigned char*)(s ? s : ""), n); +} + +el_val_t hmac_sha256_hex(el_val_t key, el_val_t message) { + const char* k = EL_CSTR(key); + const char* m = EL_CSTR(message); + size_t kn = el_input_len(k); + size_t mn = el_input_len(m); + unsigned char mac[32]; + el_hmac_sha256((const unsigned char*)(k ? k : ""), kn, + (const unsigned char*)(m ? m : ""), mn, + mac); + return el_hex_encode(mac, 32); +} + +el_val_t hmac_sha256_bytes(el_val_t key, el_val_t message) { + const char* k = EL_CSTR(key); + const char* m = EL_CSTR(message); + size_t kn = el_input_len(k); + size_t mn = el_input_len(m); + unsigned char* out = el_bin_alloc(32); + el_hmac_sha256((const unsigned char*)(k ? k : ""), kn, + (const unsigned char*)(m ? m : ""), mn, + out); + return EL_STR((char*)out); +} + +el_val_t base64_encode(el_val_t input) { + const char* s = EL_CSTR(input); + size_t n = el_input_len(s); + return el_base64_encode_n((const unsigned char*)(s ? s : ""), n, /*url_safe=*/0); +} + +el_val_t base64url_encode(el_val_t input) { + const char* s = EL_CSTR(input); + size_t n = el_input_len(s); + return el_base64_encode_n((const unsigned char*)(s ? s : ""), n, /*url_safe=*/1); +} + +el_val_t base64_decode(el_val_t input) { + return el_base64_decode_any(EL_CSTR(input)); +} + +el_val_t base64url_decode(el_val_t input) { + return el_base64_decode_any(EL_CSTR(input)); +} + +/* ── Post-quantum cryptography (liboqs + OpenSSL) ─────────────────────────── + * + * Algorithm choices (per CNSA 2.0 / NIST PQ guidance, as of 2024): + * Signatures: CRYSTALS-Dilithium-3 (NIST security level 3, balanced) + * KEM: CRYSTALS-Kyber-768 (NIST security level 3) + * Hash: SHA3-256 (Keccak) (PQ-aware protocols favour SHA3 over SHA2) + * Hybrid: X25519 || Kyber-768, combined via HKDF-SHA256 + * + * Why hybrid: Kyber is new. X25519 has 20+ years of analysis. Hybridizing + * preserves classical security if Kyber falls to a future cryptanalytic + * advance, and preserves PQ security if X25519 falls to a quantum adversary. + * "Recordable now, decryptable later" already threatens long-lived classical + * key exchange — the only safe move for keys protecting durable doctrine + * (CGI lineage, KindredGrants, Principal-CGI covenants) is to encapsulate + * with PQ today, even if the classical leg is what the wire shows. + * + * Compile-time detection: when is unavailable the pq_* functions + * compile to stubs that return a JSON error envelope. SHA3-256 stays + * available regardless (it's implemented inline, no liboqs dep). This lets + * the runtime build cleanly on dev machines without liboqs while production + * gets the full PQ stack. */ + +/* ─── SHA3-256 (Keccak, FIPS 202) ──────────────────────────────────────────── + * Inline reference implementation. ~120 LoC, no external dependency. + * rate=1088 bits, capacity=512 bits, output=256 bits, padding=0x06. */ + +static const uint64_t el_keccak_rc[24] = { + 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL, + 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL, + 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL, + 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL, + 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, + 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL, + 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL, + 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL +}; + +static const unsigned el_keccak_rho[24] = { + 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, + 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44 +}; + +static const unsigned el_keccak_pi[24] = { + 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, + 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 +}; + +#define EL_ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n)))) + +static void el_keccak_f1600(uint64_t s[25]) { + for (int round = 0; round < 24; ++round) { + uint64_t bc[5], t; + for (int i = 0; i < 5; ++i) + bc[i] = s[i] ^ s[i+5] ^ s[i+10] ^ s[i+15] ^ s[i+20]; + for (int i = 0; i < 5; ++i) { + t = bc[(i+4) % 5] ^ EL_ROTL64(bc[(i+1) % 5], 1); + for (int j = 0; j < 25; j += 5) s[j+i] ^= t; + } + t = s[1]; + for (int i = 0; i < 24; ++i) { + int j = el_keccak_pi[i]; + bc[0] = s[j]; + s[j] = EL_ROTL64(t, el_keccak_rho[i]); + t = bc[0]; + } + for (int j = 0; j < 25; j += 5) { + for (int i = 0; i < 5; ++i) bc[i] = s[j+i]; + for (int i = 0; i < 5; ++i) + s[j+i] = bc[i] ^ ((~bc[(i+1) % 5]) & bc[(i+2) % 5]); + } + s[0] ^= el_keccak_rc[round]; + } +} + +static void el_sha3_256_oneshot(const unsigned char* data, size_t len, + unsigned char out[32]) { + uint64_t st[25] = {0}; + unsigned char* sb = (unsigned char*)st; + const size_t rate = 136; /* 1088 bits / 8 */ + size_t i = 0; + while (len - i >= rate) { + for (size_t k = 0; k < rate; ++k) sb[k] ^= data[i + k]; + el_keccak_f1600(st); + i += rate; + } + size_t rem = len - i; + for (size_t k = 0; k < rem; ++k) sb[k] ^= data[i + k]; + sb[rem] ^= 0x06; /* SHA3 domain-separation byte */ + sb[rate - 1] ^= 0x80; /* final-block padding bit (high bit of last byte) */ + el_keccak_f1600(st); + memcpy(out, sb, 32); +} + +el_val_t sha3_256_hex(el_val_t input) { + const char* s = EL_CSTR(input); + size_t n = el_input_len(s); + unsigned char digest[32]; + el_sha3_256_oneshot((const unsigned char*)(s ? s : ""), n, digest); + return el_hex_encode(digest, 32); +} + +/* ─── Hex decode helper ───────────────────────────────────────────────────── + * Returns a length-tagged binary buffer (so embedded NULs survive); on + * odd-length / invalid input returns NULL with *out_len = 0. Caller is + * responsible for emitting the error envelope. */ + +static int el_hex_nibble(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +__attribute__((unused)) +static unsigned char* el_hex_decode(const char* s, size_t* out_len) { + *out_len = 0; + if (!s) return NULL; + size_t n = strlen(s); + if (n & 1) return NULL; + size_t blen = n / 2; + unsigned char* out = el_bin_alloc(blen); + for (size_t i = 0; i < blen; ++i) { + int hi = el_hex_nibble(s[i*2]); + int lo = el_hex_nibble(s[i*2 + 1]); + if (hi < 0 || lo < 0) return NULL; + out[i] = (unsigned char)((hi << 4) | lo); + } + *out_len = blen; + return out; +} + +/* JSON error envelope reused across all PQ entry points. */ +static el_val_t pq_error(const char* msg) { + return http_error_json(msg); +} + +#if __has_include() +#include +#define EL_HAVE_LIBOQS 1 +#else +#define EL_HAVE_LIBOQS 0 +#endif + +#if EL_HAVE_LIBOQS && __has_include() +#include +#define EL_HAVE_OPENSSL 1 +#else +#define EL_HAVE_OPENSSL 0 +#endif + +#if !EL_HAVE_LIBOQS + +/* ─── Stubs (liboqs unavailable) ─────────────────────────────────────────── + * Each entry point returns the same JSON error so callers can inspect a + * single canonical "missing primitive" string. pq_verify is the lone + * exception — verifying without liboqs simply means "not verified", so + * returning Bool false (0) keeps the type contract intact. */ + +#define EL_PQ_NO_LIB "liboqs not linked, post-quantum primitives unavailable" + +el_val_t pq_keygen_signature(void) { return pq_error(EL_PQ_NO_LIB); } +el_val_t pq_sign(el_val_t sk, el_val_t msg) { (void)sk; (void)msg; return pq_error(EL_PQ_NO_LIB); } +el_val_t pq_verify(el_val_t pk, el_val_t msg, el_val_t sig) { (void)pk; (void)msg; (void)sig; return EL_INT(0); } +el_val_t pq_kem_keygen(void) { return pq_error(EL_PQ_NO_LIB); } +el_val_t pq_kem_encaps(el_val_t pk) { (void)pk; return pq_error(EL_PQ_NO_LIB); } +el_val_t pq_kem_decaps(el_val_t sk, el_val_t ct) { (void)sk; (void)ct; return pq_error(EL_PQ_NO_LIB); } +el_val_t pq_hybrid_keygen(void) { return pq_error(EL_PQ_NO_LIB); } +el_val_t pq_hybrid_handshake(el_val_t pub) { (void)pub; return pq_error(EL_PQ_NO_LIB); } + +#else /* EL_HAVE_LIBOQS */ + +/* ─── Dilithium-3 / ML-DSA-65 signatures ──────────────────────────────── + * + * NIST FIPS 204 standardized CRYSTALS-Dilithium as ML-DSA. ML-DSA-65 is the + * FIPS form of what we historically called Dilithium-3 — same algorithm + * family, same security level, identical key/sig sizes, but with a couple + * of standardization-driven tweaks (e.g. domain separation in the message + * binding). liboqs 0.12+ exposes both names; 0.15+ retired the legacy + * "Dilithium" constants in favour of "ML-DSA". We prefer ML-DSA-65 if the + * header advertises it, fall back to Dilithium-3 otherwise. Anything + * already signed with the older constant remains verifiable against that + * same constant — callers should pin the algorithm via the OQS_SIG handle's + * method_name field if they need to interoperate with archival signatures. */ + +#if defined(OQS_SIG_alg_ml_dsa_65) +# define EL_DILITHIUM_ALG OQS_SIG_alg_ml_dsa_65 +#elif defined(OQS_SIG_alg_dilithium_3) +# define EL_DILITHIUM_ALG OQS_SIG_alg_dilithium_3 +#else +# define EL_DILITHIUM_ALG "ML-DSA-65" /* string fallback; runtime probe catches misconfig */ +#endif + +el_val_t pq_keygen_signature(void) { + OQS_SIG* sig = OQS_SIG_new(EL_DILITHIUM_ALG); + if (!sig) return pq_error("OQS_SIG_new(dilithium-3) failed"); + unsigned char* pk = (unsigned char*)malloc(sig->length_public_key); + unsigned char* sk = (unsigned char*)malloc(sig->length_secret_key); + if (!pk || !sk) { free(pk); free(sk); OQS_SIG_free(sig); return pq_error("oom"); } + if (OQS_SIG_keypair(sig, pk, sk) != OQS_SUCCESS) { + free(pk); free(sk); OQS_SIG_free(sig); + return pq_error("dilithium-3 keypair generation failed"); + } + el_val_t pk_hex = el_hex_encode(pk, sig->length_public_key); + el_val_t sk_hex = el_hex_encode(sk, sig->length_secret_key); + OQS_MEM_secure_free(sk, sig->length_secret_key); + free(pk); + + const char* pks = EL_CSTR(pk_hex); + const char* sks = EL_CSTR(sk_hex); + char* buf = el_strbuf(strlen(pks) + strlen(sks) + 64); + sprintf(buf, "{\"public_key\":\"%s\",\"secret_key\":\"%s\"}", pks, sks); + OQS_SIG_free(sig); + return el_wrap_str(buf); +} + +el_val_t pq_sign(el_val_t secret_key_hex, el_val_t message) { + size_t sk_len = 0; + unsigned char* sk = el_hex_decode(EL_CSTR(secret_key_hex), &sk_len); + if (!sk) return pq_error("invalid hex in secret_key"); + + OQS_SIG* sig = OQS_SIG_new(EL_DILITHIUM_ALG); + if (!sig) return pq_error("OQS_SIG_new(dilithium-3) failed"); + if (sk_len != sig->length_secret_key) { + OQS_SIG_free(sig); + return pq_error("secret_key length mismatch for dilithium-3"); + } + + const char* msg = EL_CSTR(message); + size_t msg_len = el_input_len(msg); + unsigned char* signature = (unsigned char*)malloc(sig->length_signature); + size_t signature_len = sig->length_signature; + if (!signature) { OQS_SIG_free(sig); return pq_error("oom"); } + + if (OQS_SIG_sign(sig, signature, &signature_len, + (const unsigned char*)(msg ? msg : ""), msg_len, sk) != OQS_SUCCESS) { + free(signature); OQS_SIG_free(sig); + return pq_error("dilithium-3 sign failed"); + } + el_val_t sig_hex = el_hex_encode(signature, signature_len); + free(signature); OQS_SIG_free(sig); + return sig_hex; +} + +el_val_t pq_verify(el_val_t public_key_hex, el_val_t message, el_val_t signature_hex) { + size_t pk_len = 0, sig_len = 0; + unsigned char* pk = el_hex_decode(EL_CSTR(public_key_hex), &pk_len); + unsigned char* signature = el_hex_decode(EL_CSTR(signature_hex), &sig_len); + if (!pk || !signature) return EL_INT(0); + + OQS_SIG* sig = OQS_SIG_new(EL_DILITHIUM_ALG); + if (!sig) return EL_INT(0); + if (pk_len != sig->length_public_key) { OQS_SIG_free(sig); return EL_INT(0); } + + const char* msg = EL_CSTR(message); + size_t msg_len = el_input_len(msg); + OQS_STATUS rc = OQS_SIG_verify(sig, + (const unsigned char*)(msg ? msg : ""), msg_len, + signature, sig_len, pk); + OQS_SIG_free(sig); + return (rc == OQS_SUCCESS) ? EL_INT(1) : EL_INT(0); +} + +/* ─── Kyber-768 / ML-KEM-768 KEM ──────────────────────────────────────── + * + * NIST FIPS 203 standardized CRYSTALS-Kyber as ML-KEM. ML-KEM-768 is the + * FIPS form of what we historically called Kyber-768. Same situation as + * Dilithium → ML-DSA: prefer the standardized constant, fall back to the + * legacy name. liboqs 0.15.0 still exposes OQS_KEM_alg_kyber_768; the + * algorithm is identical at the wire level to ML-KEM-768 except for FIPS + * domain-separation tweaks, so the two ciphertexts/keys are NOT + * cross-compatible. Pin the constant for archival material. */ + +#if defined(OQS_KEM_alg_ml_kem_768) +# define EL_KYBER_ALG OQS_KEM_alg_ml_kem_768 +#elif defined(OQS_KEM_alg_kyber_768) +# define EL_KYBER_ALG OQS_KEM_alg_kyber_768 +#else +# define EL_KYBER_ALG "ML-KEM-768" +#endif + +el_val_t pq_kem_keygen(void) { + OQS_KEM* kem = OQS_KEM_new(EL_KYBER_ALG); + if (!kem) return pq_error("OQS_KEM_new(kyber-768) failed"); + unsigned char* pk = (unsigned char*)malloc(kem->length_public_key); + unsigned char* sk = (unsigned char*)malloc(kem->length_secret_key); + if (!pk || !sk) { free(pk); free(sk); OQS_KEM_free(kem); return pq_error("oom"); } + if (OQS_KEM_keypair(kem, pk, sk) != OQS_SUCCESS) { + free(pk); free(sk); OQS_KEM_free(kem); + return pq_error("kyber-768 keypair generation failed"); + } + el_val_t pk_hex = el_hex_encode(pk, kem->length_public_key); + el_val_t sk_hex = el_hex_encode(sk, kem->length_secret_key); + OQS_MEM_secure_free(sk, kem->length_secret_key); + free(pk); + + const char* pks = EL_CSTR(pk_hex); + const char* sks = EL_CSTR(sk_hex); + char* buf = el_strbuf(strlen(pks) + strlen(sks) + 64); + sprintf(buf, "{\"public_key\":\"%s\",\"secret_key\":\"%s\"}", pks, sks); + OQS_KEM_free(kem); + return el_wrap_str(buf); +} + +el_val_t pq_kem_encaps(el_val_t public_key_hex) { + size_t pk_len = 0; + unsigned char* pk = el_hex_decode(EL_CSTR(public_key_hex), &pk_len); + if (!pk) return pq_error("invalid hex in public_key"); + + OQS_KEM* kem = OQS_KEM_new(EL_KYBER_ALG); + if (!kem) return pq_error("OQS_KEM_new(kyber-768) failed"); + if (pk_len != kem->length_public_key) { + OQS_KEM_free(kem); + return pq_error("public_key length mismatch for kyber-768"); + } + unsigned char* ct = (unsigned char*)malloc(kem->length_ciphertext); + unsigned char* ss = (unsigned char*)malloc(kem->length_shared_secret); + if (!ct || !ss) { free(ct); free(ss); OQS_KEM_free(kem); return pq_error("oom"); } + if (OQS_KEM_encaps(kem, ct, ss, pk) != OQS_SUCCESS) { + free(ct); free(ss); OQS_KEM_free(kem); + return pq_error("kyber-768 encapsulation failed"); + } + el_val_t ct_hex = el_hex_encode(ct, kem->length_ciphertext); + el_val_t ss_hex = el_hex_encode(ss, kem->length_shared_secret); + free(ct); + OQS_MEM_secure_free(ss, kem->length_shared_secret); + + const char* cts = EL_CSTR(ct_hex); + const char* sss = EL_CSTR(ss_hex); + char* buf = el_strbuf(strlen(cts) + strlen(sss) + 64); + sprintf(buf, "{\"ciphertext\":\"%s\",\"shared_secret\":\"%s\"}", cts, sss); + OQS_KEM_free(kem); + return el_wrap_str(buf); +} + +el_val_t pq_kem_decaps(el_val_t secret_key_hex, el_val_t ciphertext_hex) { + size_t sk_len = 0, ct_len = 0; + unsigned char* sk = el_hex_decode(EL_CSTR(secret_key_hex), &sk_len); + unsigned char* ct = el_hex_decode(EL_CSTR(ciphertext_hex), &ct_len); + if (!sk || !ct) return pq_error("invalid hex in inputs"); + + OQS_KEM* kem = OQS_KEM_new(EL_KYBER_ALG); + if (!kem) return pq_error("OQS_KEM_new(kyber-768) failed"); + if (sk_len != kem->length_secret_key || ct_len != kem->length_ciphertext) { + OQS_KEM_free(kem); + return pq_error("input length mismatch for kyber-768"); + } + unsigned char* ss = (unsigned char*)malloc(kem->length_shared_secret); + if (!ss) { OQS_KEM_free(kem); return pq_error("oom"); } + /* Kyber is IND-CCA via Fujisaki-Okamoto: decaps always returns *some* + * shared_secret even on tampered ciphertext (an implicit-rejection value + * derived from sk). Protocols MUST confirm the shared_secret matches via + * a subsequent step (e.g. AEAD tag, key-confirmation MAC) — do not + * assume decaps success implies authenticity. */ + if (OQS_KEM_decaps(kem, ss, ct, sk) != OQS_SUCCESS) { + free(ss); OQS_KEM_free(kem); + return pq_error("kyber-768 decapsulation failed"); + } + el_val_t ss_hex = el_hex_encode(ss, kem->length_shared_secret); + OQS_MEM_secure_free(ss, kem->length_shared_secret); + OQS_KEM_free(kem); + return ss_hex; +} + +/* ─── Hybrid handshake (X25519 + Kyber-768, HKDF-SHA256 combined) ─────── */ + +#if !EL_HAVE_OPENSSL + +el_val_t pq_hybrid_keygen(void) { + return pq_error("hybrid handshake requires OpenSSL (X25519); rebuild with -lcrypto"); +} +el_val_t pq_hybrid_handshake(el_val_t pub) { + (void)pub; + return pq_error("hybrid handshake requires OpenSSL (X25519); rebuild with -lcrypto"); +} + +#else /* EL_HAVE_OPENSSL */ + +/* HKDF-SHA256 (RFC 5869) — Extract+Expand. Reuses the inline HMAC-SHA256 + * already in this file. Empty salt → 32 zero bytes per the RFC. */ +static void el_hkdf_sha256(const unsigned char* salt, size_t salt_len, + const unsigned char* ikm, size_t ikm_len, + const unsigned char* info, size_t info_len, + unsigned char* out, size_t out_len) { + unsigned char zero_salt[32] = {0}; + if (salt_len == 0) { salt = zero_salt; salt_len = 32; } + unsigned char prk[32]; + el_hmac_sha256(salt, salt_len, ikm, ikm_len, prk); + + unsigned char t[32]; + size_t produced = 0; + unsigned char counter = 1; + unsigned char* buf = (unsigned char*)malloc(32 + info_len + 1); + if (!buf) { fputs("el_runtime: hkdf oom\n", stderr); return; } + while (produced < out_len) { + size_t off = 0; + if (counter > 1) { memcpy(buf, t, 32); off = 32; } + if (info && info_len) { memcpy(buf + off, info, info_len); off += info_len; } + buf[off++] = counter; + el_hmac_sha256(prk, 32, buf, off, t); + size_t chunk = (out_len - produced > 32) ? 32 : (out_len - produced); + memcpy(out + produced, t, chunk); + produced += chunk; + counter++; + } + free(buf); +} + +/* X25519 keygen via OpenSSL EVP. Returns 1 on success. + * Fills pk[32] and sk[32] (raw X25519 byte strings, no DER wrapper). */ +static int el_x25519_keygen(unsigned char pk[32], unsigned char sk[32]) { + EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_X25519, NULL); + if (!pctx) return 0; + if (EVP_PKEY_keygen_init(pctx) != 1) { EVP_PKEY_CTX_free(pctx); return 0; } + EVP_PKEY* key = NULL; + if (EVP_PKEY_keygen(pctx, &key) != 1) { EVP_PKEY_CTX_free(pctx); return 0; } + EVP_PKEY_CTX_free(pctx); + + size_t plen = 32, slen = 32; + if (EVP_PKEY_get_raw_public_key (key, pk, &plen) != 1 || plen != 32) { + EVP_PKEY_free(key); return 0; + } + if (EVP_PKEY_get_raw_private_key(key, sk, &slen) != 1 || slen != 32) { + EVP_PKEY_free(key); return 0; + } + EVP_PKEY_free(key); + return 1; +} + +/* X25519 ECDH: derive 32-byte shared secret from local sk and remote pk. */ +static int el_x25519_derive(const unsigned char sk[32], const unsigned char rpk[32], + unsigned char ss[32]) { + EVP_PKEY* my = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, sk, 32); + EVP_PKEY* rem = EVP_PKEY_new_raw_public_key (EVP_PKEY_X25519, NULL, rpk, 32); + if (!my || !rem) { EVP_PKEY_free(my); EVP_PKEY_free(rem); return 0; } + EVP_PKEY_CTX* dctx = EVP_PKEY_CTX_new(my, NULL); + if (!dctx) { EVP_PKEY_free(my); EVP_PKEY_free(rem); return 0; } + int ok = 0; + size_t out_len = 32; + if (EVP_PKEY_derive_init(dctx) == 1 && + EVP_PKEY_derive_set_peer(dctx, rem) == 1 && + EVP_PKEY_derive(dctx, ss, &out_len) == 1 && + out_len == 32) ok = 1; + EVP_PKEY_CTX_free(dctx); + EVP_PKEY_free(my); + EVP_PKEY_free(rem); + return ok; +} + +/* Hybrid wire layout (binary form, before hex encode): + * public_key = x25519_pub (32) || kyber_pub (1184) → 1216 bytes + * secret_key = x25519_sec (32) || kyber_sec (2400) → 2432 bytes + * ciphertext = ephem_x25519_pub (32) || kyber_ct (1088) → 1120 bytes + * shared_secret = HKDF-SHA256(x25519_ss || kyber_ss, info="el-pq-hybrid-v1", 32 bytes) + * The keygen result also exposes the four component hex fields for callers + * that prefer to handle the legs independently. */ + +el_val_t pq_hybrid_keygen(void) { + OQS_KEM* kem = OQS_KEM_new(EL_KYBER_ALG); + if (!kem) return pq_error("OQS_KEM_new(kyber-768) failed"); + + unsigned char xpk[32], xsk[32]; + if (!el_x25519_keygen(xpk, xsk)) { + OQS_KEM_free(kem); + return pq_error("X25519 keygen failed"); + } + + unsigned char* kpk = (unsigned char*)malloc(kem->length_public_key); + unsigned char* ksk = (unsigned char*)malloc(kem->length_secret_key); + if (!kpk || !ksk) { free(kpk); free(ksk); OQS_KEM_free(kem); return pq_error("oom"); } + if (OQS_KEM_keypair(kem, kpk, ksk) != OQS_SUCCESS) { + free(kpk); free(ksk); OQS_KEM_free(kem); + return pq_error("kyber-768 keypair generation failed"); + } + + size_t pub_len = 32 + kem->length_public_key; + size_t sec_len = 32 + kem->length_secret_key; + unsigned char* pub_buf = (unsigned char*)malloc(pub_len); + unsigned char* sec_buf = (unsigned char*)malloc(sec_len); + if (!pub_buf || !sec_buf) { + free(pub_buf); free(sec_buf); free(kpk); + OQS_MEM_secure_free(ksk, kem->length_secret_key); + OQS_KEM_free(kem); return pq_error("oom"); + } + memcpy(pub_buf, xpk, 32); memcpy(pub_buf + 32, kpk, kem->length_public_key); + memcpy(sec_buf, xsk, 32); memcpy(sec_buf + 32, ksk, kem->length_secret_key); + + el_val_t x_pub_hex = el_hex_encode(xpk, 32); + el_val_t x_sec_hex = el_hex_encode(xsk, 32); + el_val_t k_pub_hex = el_hex_encode(kpk, kem->length_public_key); + el_val_t k_sec_hex = el_hex_encode(ksk, kem->length_secret_key); + el_val_t pub_hex = el_hex_encode(pub_buf, pub_len); + el_val_t sec_hex = el_hex_encode(sec_buf, sec_len); + + OQS_MEM_secure_free(ksk, kem->length_secret_key); + free(kpk); free(pub_buf); free(sec_buf); + OQS_KEM_free(kem); + memset(xsk, 0, 32); /* best-effort wipe of stack copy */ + + const char* xph = EL_CSTR(x_pub_hex); + const char* xsh = EL_CSTR(x_sec_hex); + const char* kph = EL_CSTR(k_pub_hex); + const char* ksh = EL_CSTR(k_sec_hex); + const char* pubh = EL_CSTR(pub_hex); + const char* sech = EL_CSTR(sec_hex); + + char* buf = el_strbuf(strlen(xph) + strlen(xsh) + strlen(kph) + strlen(ksh) + + strlen(pubh) + strlen(sech) + 256); + sprintf(buf, + "{\"x25519_pub\":\"%s\",\"x25519_sec\":\"%s\"," + "\"kyber_pub\":\"%s\",\"kyber_sec\":\"%s\"," + "\"public_key\":\"%s\",\"secret_key\":\"%s\"}", + xph, xsh, kph, ksh, pubh, sech); + return el_wrap_str(buf); +} + +/* Initiator-side handshake. Caller supplies the responder's combined public + * key (x25519_pub || kyber_pub, hex-encoded). The runtime: + * 1. Generates an ephemeral X25519 keypair, runs ECDH against the + * responder's static x25519_pub. + * 2. Runs Kyber-768 encaps against the responder's kyber_pub → kyber_ct, + * kyber_ss. + * 3. Combined shared = HKDF-SHA256(salt="", ikm = x25519_ss || kyber_ss, + * info = "el-pq-hybrid-v1", L = 32). + * 4. Returns combined ciphertext (= ephemeral_x25519_pub || kyber_ct) and + * the derived shared_secret. + * + * Responder side composition (intentionally not a separate runtime fn — + * trivial to express in El given pq_kem_decaps + a future x25519_derive + * primitive): split the ciphertext into ephem_xpk (32) and kyber_ct, run + * X25519(static_xsk, ephem_xpk) and pq_kem_decaps(static_kyber_sk, kyber_ct), + * then HKDF-SHA256 with the same salt/info to recover the same shared_secret. + * If a separate x25519 entry point becomes valuable, add `pq_hybrid_open` + * here taking (secret_key_combined, ciphertext_combined). */ +el_val_t pq_hybrid_handshake(el_val_t remote_pub_combined) { + size_t pub_len = 0; + unsigned char* rpub = el_hex_decode(EL_CSTR(remote_pub_combined), &pub_len); + if (!rpub) return pq_error("invalid hex in remote_pub_combined"); + + OQS_KEM* kem = OQS_KEM_new(EL_KYBER_ALG); + if (!kem) return pq_error("OQS_KEM_new(kyber-768) failed"); + if (pub_len != 32 + kem->length_public_key) { + OQS_KEM_free(kem); + return pq_error("remote_pub_combined length mismatch (expected x25519_pub || kyber_pub)"); + } + + unsigned char e_xpk[32], e_xsk[32], x_ss[32]; + if (!el_x25519_keygen(e_xpk, e_xsk)) { + OQS_KEM_free(kem); + return pq_error("X25519 ephemeral keygen failed"); + } + if (!el_x25519_derive(e_xsk, rpub, x_ss)) { + memset(e_xsk, 0, 32); + OQS_KEM_free(kem); + return pq_error("X25519 derive failed"); + } + memset(e_xsk, 0, 32); /* ephemeral; not needed after derive */ + + unsigned char* k_ct = (unsigned char*)malloc(kem->length_ciphertext); + unsigned char* k_ss = (unsigned char*)malloc(kem->length_shared_secret); + if (!k_ct || !k_ss) { + free(k_ct); free(k_ss); OQS_KEM_free(kem); + return pq_error("oom"); + } + if (OQS_KEM_encaps(kem, k_ct, k_ss, rpub + 32) != OQS_SUCCESS) { + free(k_ct); free(k_ss); OQS_KEM_free(kem); + return pq_error("kyber-768 encapsulation failed"); + } + + /* HKDF combine: ikm = x_ss || k_ss. */ + size_t ikm_len = 32 + kem->length_shared_secret; + unsigned char* ikm = (unsigned char*)malloc(ikm_len); + if (!ikm) { + free(k_ct); OQS_MEM_secure_free(k_ss, kem->length_shared_secret); + OQS_KEM_free(kem); + return pq_error("oom"); + } + memcpy(ikm, x_ss, 32); + memcpy(ikm + 32, k_ss, kem->length_shared_secret); + unsigned char combined[32]; + static const char info_str[] = "el-pq-hybrid-v1"; + el_hkdf_sha256(NULL, 0, ikm, ikm_len, + (const unsigned char*)info_str, sizeof(info_str) - 1, + combined, 32); + + memset(x_ss, 0, 32); + OQS_MEM_secure_free(k_ss, kem->length_shared_secret); + OQS_MEM_secure_free(ikm, ikm_len); + + /* Combined ciphertext = ephemeral_x25519_pub || kyber_ct. */ + size_t ct_len = 32 + kem->length_ciphertext; + unsigned char* combined_ct = (unsigned char*)malloc(ct_len); + if (!combined_ct) { free(k_ct); OQS_KEM_free(kem); return pq_error("oom"); } + memcpy(combined_ct, e_xpk, 32); + memcpy(combined_ct + 32, k_ct, kem->length_ciphertext); + free(k_ct); + OQS_KEM_free(kem); + + el_val_t ct_hex = el_hex_encode(combined_ct, ct_len); + el_val_t ss_hex = el_hex_encode(combined, 32); + free(combined_ct); + memset(combined, 0, 32); + + const char* cts = EL_CSTR(ct_hex); + const char* sss = EL_CSTR(ss_hex); + char* buf = el_strbuf(strlen(cts) + strlen(sss) + 64); + sprintf(buf, "{\"ciphertext\":\"%s\",\"shared_secret\":\"%s\"}", cts, sss); + return el_wrap_str(buf); +} + +#endif /* EL_HAVE_OPENSSL */ +#endif /* EL_HAVE_LIBOQS */ diff --git a/releases/v1.0.0-20260501/el_runtime.h b/releases/v1.0.0-20260501/el_runtime.h new file mode 100644 index 0000000..693c210 --- /dev/null +++ b/releases/v1.0.0-20260501/el_runtime.h @@ -0,0 +1,507 @@ +/* + * el_runtime.h — El language C runtime header + * + * Declares all built-in functions available to compiled El programs. + * Include this in every generated .c file. + * + * Value model: + * All El values are represented as el_val_t (= int64_t). + * On 64-bit systems a pointer fits in int64_t. + * String values are cast: (el_val_t)(uintptr_t)"hello" + * Integer values are stored directly. + * This lets arithmetic work naturally while still passing strings around. + * + * Type conventions (El -> C): + * String -> el_val_t (holds const char* via uintptr_t cast) + * Int -> el_val_t + * Bool -> el_val_t (0 = false, nonzero = true) + * Any -> el_val_t + * Void -> void + * + * Macros for convenience: + * EL_STR(s) cast string literal to el_val_t + * EL_CSTR(v) cast el_val_t back to const char* + * EL_INT(v) identity — el_val_t is already int64_t + * + * Link requirements: + * -lcurl — required for the HTTP client (http_get, http_post, llm_*). + * -lpthread — required for the HTTP server (one detached thread per + * connection, capped at 64 concurrent). + * -loqs — optional; required only when liboqs is installed and the + * pq_* / sha3_256_hex entry points are needed. Detected at + * compile time via __has_include(). + * -lcrypto — optional; pulled in alongside -loqs. Used for X25519 in + * pq_hybrid_* and HKDF-SHA256 derivation. + * + * Canonical compile command: + * cc -std=c11 -I el-compiler/runtime -lcurl -lpthread \ + * -o .c el-compiler/runtime/el_runtime.c + * + * With liboqs (post-quantum stack): + * cc -std=c11 -I el-compiler/runtime -lcurl -lpthread -loqs -lcrypto \ + * -o .c el-compiler/runtime/el_runtime.c + */ + +#pragma once + +#include +#include + +typedef int64_t el_val_t; + +#define EL_STR(s) ((el_val_t)(uintptr_t)(s)) +#define EL_CSTR(v) ((const char*)(uintptr_t)(v)) +#define EL_INT(v) (v) +#define EL_NULL ((el_val_t)0) + +/* Float values share the el_val_t (int64) slot via a bit-cast. + * The codegen emits Float literals as `el_from_float()` so the + * underlying bits represent the IEEE 754 double. Float-aware builtins + * (math, format, json) round-trip via these helpers. */ +static inline double el_to_float(el_val_t v) { + union { int64_t i; double f; } u; + u.i = (int64_t)v; + return u.f; +} + +static inline el_val_t el_from_float(double f) { + union { double f; int64_t i; } u; + u.f = f; + return (el_val_t)u.i; +} + +#ifdef __cplusplus +extern "C" { +#endif + +/* ── I/O ──────────────────────────────────────────────────────────────────── */ + +void println(el_val_t s); +void print(el_val_t s); +el_val_t readline(void); + +/* ── String builtins ─────────────────────────────────────────────────────── */ + +el_val_t el_str_concat(el_val_t a, el_val_t b); +el_val_t str_eq(el_val_t a, el_val_t b); +el_val_t str_starts_with(el_val_t s, el_val_t prefix); +el_val_t str_ends_with(el_val_t s, el_val_t suffix); +el_val_t str_len(el_val_t s); +el_val_t str_concat(el_val_t a, el_val_t b); +el_val_t int_to_str(el_val_t n); +el_val_t str_to_int(el_val_t s); +el_val_t str_slice(el_val_t s, el_val_t start, el_val_t end); +el_val_t str_contains(el_val_t s, el_val_t sub); +el_val_t str_replace(el_val_t s, el_val_t from, el_val_t to); +el_val_t str_to_upper(el_val_t s); +el_val_t str_to_lower(el_val_t s); +el_val_t str_trim(el_val_t s); + +/* ── Math ────────────────────────────────────────────────────────────────── */ + +el_val_t el_abs(el_val_t n); +el_val_t el_max(el_val_t a, el_val_t b); +el_val_t el_min(el_val_t a, el_val_t b); + +/* ── Refcount (ARC) ────────────────────────────────────────────────────────── + * Lists and Maps carry a refcount. Strings and ints do not — el_retain and + * el_release are safe no-ops on non-refcounted values (they sniff a magic + * header at offset 0 and only act if the magic matches). + * + * Codegen emits these at let-binding shadowing, function entry (params), and + * function exit (locals other than the returned value). The refcount lets + * el_list_append and el_map_set mutate in place when uniquely owned (cheap) + * and copy-on-write when shared (preserves persistent semantics across + * accumulator patterns in the compiler itself). */ + +void el_retain(el_val_t v); +void el_release(el_val_t v); + +/* ── List ────────────────────────────────────────────────────────────────── */ + +el_val_t el_list_new(el_val_t count, ...); +el_val_t el_list_len(el_val_t list); +el_val_t el_list_get(el_val_t list, el_val_t index); +el_val_t el_list_append(el_val_t list, el_val_t elem); +el_val_t el_list_empty(void); +el_val_t el_list_clone(el_val_t list); + +/* ── Map ─────────────────────────────────────────────────────────────────── */ + +el_val_t el_map_new(el_val_t pair_count, ...); +el_val_t el_get_field(el_val_t map, el_val_t key); +el_val_t el_map_get(el_val_t map, el_val_t key); +el_val_t el_map_set(el_val_t map, el_val_t key, el_val_t value); + +/* ── HTTP ─────────────────────────────────────────────────────────────────── */ + +el_val_t http_get(el_val_t url); +el_val_t http_post(el_val_t url, el_val_t body); +el_val_t http_post_json(el_val_t url, el_val_t json_body); +el_val_t http_get_with_headers(el_val_t url, el_val_t headers_map); +el_val_t http_post_with_headers(el_val_t url, el_val_t body, el_val_t headers_map); +el_val_t http_post_form_auth(el_val_t url, el_val_t form_body, el_val_t auth_header); +el_val_t http_delete(el_val_t url); +void http_serve(el_val_t port, el_val_t handler); +void http_set_handler(el_val_t name); + +/* HTTP server v2 ───────────────────────────────────────────────────────────── + * Same dispatch model as http_serve, but the handler signature is widened: + * + * el_val_t handler(method, path, headers_map, body) + * + * `headers_map` is an ElMap from lowercased header name → header value (both + * Strings). Repeated headers are joined with ", " per RFC 7230. + * + * Response value: the handler may return either + * (a) a plain body string — same auto-content-type / 200-OK behaviour as + * http_serve (3-arg) — or + * (b) a response envelope built with `http_response(status, headers_json, + * body)`. The runtime detects the envelope discriminator + * `"el_http_response":1` at the start of the returned string and + * unpacks status / headers / body before sending. + * + * The 3-arg http_serve(port, handler) remains supported unchanged for + * existing handlers (e.g. products/web/server.el): it dispatches with + * (method, path, body), hardcodes 200 OK, and auto-detects content type. */ +void http_serve_v2(el_val_t port, el_val_t handler); +void http_set_handler_v2(el_val_t name); + +/* Build an HTTP response envelope. `headers_json` should be a JSON object + * literal like `{"WWW-Authenticate":"Basic"}` (or "" / "{}" for none). The + * returned string carries the discriminator `{"el_http_response":1,...}` + * which the runtime's send-path detects and unpacks. Detection happens + * uniformly inside http_send_response, so a 3-arg handler may also return + * an envelope. The 3-arg variant remains documented as a fixed 200-OK + * auto-content-type contract for legacy handlers that return plain bodies. */ +el_val_t http_response(el_val_t status, el_val_t headers_json, el_val_t body); + +/* HTTP timeout — every libcurl request honors EL_HTTP_TIMEOUT_MS (default + * 60000ms). Read lazily on first use, so setting the env var any time before + * the first http_* call is sufficient. */ + +/* Streaming variants — write the response body straight to a file via + * libcurl's CURLOPT_WRITEFUNCTION = fwrite. These bypass the el_val_t string + * wrapper entirely, so binary payloads (audio/mpeg, image/png, etc.) survive + * embedded NUL bytes that would truncate a strlen()-based code path. + * + * Both honor EL_HTTP_TIMEOUT_MS, follow redirects, and accept the same + * `headers_map` shape as http_post_with_headers (ElMap of String→String). + * + * Return value: 1 on success (file fully written), 0 on any failure + * (network, file open, partial write). On failure the output file is removed + * so callers cannot mistake a partially-written file for a valid one. */ +el_val_t http_post_to_file(el_val_t url, el_val_t body, el_val_t headers_map, el_val_t output_path); +el_val_t http_get_to_file(el_val_t url, el_val_t headers_map, el_val_t output_path); + +/* ── URL encoding ────────────────────────────────────────────────────────── */ + +el_val_t url_encode(el_val_t s); /* RFC 3986 unreserved set */ +el_val_t url_decode(el_val_t s); /* '+' → space, %XX → byte */ + +/* ── Filesystem ──────────────────────────────────────────────────────────── */ + +el_val_t fs_read(el_val_t path); +el_val_t fs_write(el_val_t path, el_val_t content); +el_val_t fs_list(el_val_t path); +el_val_t fs_exists(el_val_t path); +el_val_t fs_mkdir(el_val_t path); /* mkdir -p, mode 0755 */ + +/* Length-explicit binary write. `length` is an Int (el_val_t holding the + * byte count). The caller knows the length from context — typically because + * `bytes` came from base64_decode (which produces a magic-tagged binary + * buffer with embedded NULs possible) and the caller already tracks the + * decoded length, OR because the bytes came from a fixed-size source + * (sha256_bytes = 32, hmac_sha256_bytes = 32). Bypasses strlen entirely. + * + * Returns 1 on success, 0 on failure (invalid path, can't open, partial + * write, negative length). On partial-write failure, the file is removed + * so callers cannot read back a truncated artefact. */ +el_val_t fs_write_bytes(el_val_t path, el_val_t bytes, el_val_t length); + +/* ── JSON ────────────────────────────────────────────────────────────────── */ + +el_val_t json_get(el_val_t json, el_val_t key); +el_val_t json_parse(el_val_t s); +el_val_t json_stringify(el_val_t v); +el_val_t json_get_string(el_val_t json_str, el_val_t key); +el_val_t json_get_int(el_val_t json_str, el_val_t key); +el_val_t json_get_float(el_val_t json_str, el_val_t key); +el_val_t json_get_bool(el_val_t json_str, el_val_t key); +el_val_t json_get_raw(el_val_t json_str, el_val_t key); +el_val_t json_set(el_val_t json_str, el_val_t key, el_val_t value); +el_val_t json_array_len(el_val_t json_str); +el_val_t json_array_get(el_val_t json_str, el_val_t index); +el_val_t json_array_get_string(el_val_t json_str, el_val_t index); + +/* ── Time ────────────────────────────────────────────────────────────────── */ + +el_val_t time_now(void); +el_val_t time_now_utc(void); +el_val_t sleep_secs(el_val_t secs); +el_val_t sleep_ms(el_val_t ms); +el_val_t time_format(el_val_t ts, el_val_t fmt); +el_val_t time_to_parts(el_val_t ts); +el_val_t time_from_parts(el_val_t secs, el_val_t ns, el_val_t tz); +el_val_t time_add(el_val_t ts, el_val_t n, el_val_t unit); +el_val_t time_diff(el_val_t ts1, el_val_t ts2, el_val_t unit); + +/* ── UUID ────────────────────────────────────────────────────────────────── */ + +el_val_t uuid_new(void); +el_val_t uuid_v4(void); + +/* ── Environment ─────────────────────────────────────────────────────────── */ + +el_val_t env(el_val_t key); + +/* ── In-process state K/V ────────────────────────────────────────────────── */ + +el_val_t state_set(el_val_t key, el_val_t value); +el_val_t state_get(el_val_t key); +el_val_t state_del(el_val_t key); +el_val_t state_keys(void); + +/* ── Float formatting ────────────────────────────────────────────────────── */ + +el_val_t float_to_str(el_val_t f); +el_val_t int_to_float(el_val_t n); +el_val_t float_to_int(el_val_t f); +el_val_t format_float(el_val_t f, el_val_t decimals); +el_val_t decimal_round(el_val_t f, el_val_t decimals); +el_val_t str_to_float(el_val_t s); + +/* ── Math (Float-aware) ──────────────────────────────────────────────────── */ + +el_val_t math_sqrt(el_val_t f); +el_val_t math_log(el_val_t f); +el_val_t math_ln(el_val_t f); +el_val_t math_sin(el_val_t f); +el_val_t math_cos(el_val_t f); +el_val_t math_pi(void); + +/* ── String additions ────────────────────────────────────────────────────── */ + +el_val_t str_index_of(el_val_t s, el_val_t sub); +el_val_t str_split(el_val_t s, el_val_t sep); +el_val_t str_char_at(el_val_t s, el_val_t i); +el_val_t str_char_code(el_val_t s, el_val_t i); +el_val_t str_pad_left(el_val_t s, el_val_t width, el_val_t pad); +el_val_t str_pad_right(el_val_t s, el_val_t width, el_val_t pad); +el_val_t str_format(el_val_t template, el_val_t data); +el_val_t str_lower(el_val_t s); +el_val_t str_upper(el_val_t s); + +/* ── List additions ──────────────────────────────────────────────────────── */ + +el_val_t list_push(el_val_t list, el_val_t elem); +el_val_t list_push_front(el_val_t list, el_val_t elem); +el_val_t list_join(el_val_t list, el_val_t sep); +el_val_t list_range(el_val_t start, el_val_t end); + +/* ── Bool helpers ────────────────────────────────────────────────────────── */ + +el_val_t bool_to_str(el_val_t b); + +/* ── Numeric parsing ─────────────────────────────────────────────────────── */ + +el_val_t parse_int(el_val_t s, el_val_t default_val); + +/* ── Process ─────────────────────────────────────────────────────────────── */ + +void exit_program(el_val_t code); +el_val_t getpid_now(void); + +/* ── CGI identity ───────────────────────────────────────────────────────────── + * Called at the start of main() in CGI programs (those with a `cgi {}` block). + * Records the program's DHARMA identity before any other code executes. */ + +void el_cgi_init(el_val_t name, el_val_t dharma_id, el_val_t principal, + el_val_t network, el_val_t engram); + +/* ── DHARMA network builtins ───────────────────────────────────────────────── + * Available to CGI programs (declared with a `cgi {}` block). + * + * Peers are addressed by `dharma_id` of the form + * "@" e.g. "ntn-genesis@http://localhost:7770" + * If the @ portion is omitted, transport defaults to + * "http://localhost:7770" (the local CGI daemon assumption). + * + * Wire protocol (all peers expose): + * POST /dharma/recv { channel, from, content } → response body + * POST /dharma/event { type, payload, source, timestamp } + * POST /api/activate { query } → list of nodes + * + * Hosting application's responsibility: an El program with a `cgi {}` block + * runs http_serve() with its own request handler; that handler should route + * "/dharma/event" requests by calling el_runtime_dharma_event_arrive() so + * incoming events feed dharma_field() queues. The runtime itself does not + * intercept any /dharma path. */ + +el_val_t dharma_connect(el_val_t cgi_id); +el_val_t dharma_send(el_val_t channel, el_val_t content); +el_val_t dharma_activate(el_val_t query); +void dharma_emit(el_val_t event_type, el_val_t payload); +el_val_t dharma_field(el_val_t event_type); +void dharma_strengthen(el_val_t cgi_id, el_val_t weight); +el_val_t dharma_relationship(el_val_t cgi_id); +el_val_t dharma_peers(void); + +/* Public C API: called by an El program's HTTP handler when a /dharma/event + * request arrives. Pushes onto the per-event-type queue and signals any + * pending dharma_field() blockers. All three arguments must be NUL-terminated + * C strings (or NULL — then treated as empty). */ +void el_runtime_dharma_event_arrive(const char* event_type, + const char* payload, + const char* source); + +/* ── Engram local graph primitives ─────────────────────────────────────────── + * Operate on the CGI's local Engram knowledge graph. + * `engram_activate` queries the local graph only; `dharma_activate` is + * network-wide across all connected CGI graphs. */ + +el_val_t engram_node(el_val_t content, el_val_t node_type, el_val_t salience); +el_val_t engram_node_full(el_val_t content, el_val_t node_type, el_val_t label, + el_val_t salience, el_val_t importance, el_val_t confidence, + el_val_t tier, el_val_t tags); +el_val_t engram_get_node(el_val_t id); +void engram_strengthen(el_val_t node_id); +void engram_forget(el_val_t node_id); +el_val_t engram_node_count(void); +el_val_t engram_search(el_val_t query, el_val_t limit); +el_val_t engram_scan_nodes(el_val_t limit, el_val_t offset); +void engram_connect(el_val_t from_id, el_val_t to_id, el_val_t weight, el_val_t relation); +el_val_t engram_edge_between(el_val_t from_id, el_val_t to_id); +el_val_t engram_neighbors(el_val_t node_id); +el_val_t engram_neighbors_filtered(el_val_t node_id, el_val_t max_depth, el_val_t direction); +el_val_t engram_edge_count(void); +el_val_t engram_activate(el_val_t query, el_val_t depth); +el_val_t engram_save(el_val_t path); +el_val_t engram_load(el_val_t path); + +/* JSON-string accessors — return pre-serialized JSON so HTTP handlers + * can pass results straight through without round-tripping ElList/ElMap + * through json_stringify. */ +el_val_t engram_get_node_json(el_val_t id); +el_val_t engram_search_json(el_val_t query, el_val_t limit); +el_val_t engram_scan_nodes_json(el_val_t limit, el_val_t offset); +el_val_t engram_neighbors_json(el_val_t node_id, el_val_t max_depth, el_val_t direction); +el_val_t engram_activate_json(el_val_t query, el_val_t depth); +el_val_t engram_stats_json(void); + +/* ── LLM (Anthropic API client) ───────────────────────────────────────────── + * All functions call https://api.anthropic.com/v1/messages with the API key + * from env ANTHROPIC_API_KEY. Default model when empty: claude-sonnet-4-5. */ + +el_val_t llm_call(el_val_t model, el_val_t prompt); +el_val_t llm_call_system(el_val_t model, el_val_t system_prompt, el_val_t user_prompt); +el_val_t llm_call_agentic(el_val_t model, el_val_t system, el_val_t user, el_val_t tools); +el_val_t llm_vision(el_val_t model, el_val_t system, el_val_t prompt, el_val_t image_url_or_b64); +el_val_t llm_models(void); + +/* Register a tool handler by name. The handler is looked up via dlsym + * (mirroring http_set_handler), so any El `fn (input)` compiles to + * a global C symbol that this function can locate at runtime. + * Handler signature: `el_val_t handler(el_val_t input_json)` — receives + * the tool input as a JSON-string el_val_t and returns a JSON-string + * el_val_t result. Used by llm_call_agentic. */ +void llm_register_tool(el_val_t name, el_val_t handler_fn_name); + +/* ── args() ───────────────────────────────────────────────────────────────── + * Provides access to command-line arguments passed to the program. + * Populated by el_runtime_init_args() before main() runs. */ + +el_val_t args(void); +void el_runtime_init_args(int argc, char** argv); + +/* ── Crypto primitives ───────────────────────────────────────────────────── + * SHA-256, HMAC-SHA-256, and base64 (standard + URL-safe). + * Self-contained — no OpenSSL/libcrypto dependency. The implementations are + * adapted from public-domain reference code (Brad Conte / RFC 4648). + * + * Bytes-returning variants (sha256_bytes, hmac_sha256_bytes) return a string + * value whose contents are raw binary; callers usually feed these into + * base64_encode. Note that el_val_t strings are NUL-terminated by convention, + * so the binary payload may contain embedded NULs — pass it directly into + * base64_encode (which uses an explicit length) rather than treating it as + * a printable C string. + * + * The "base64" variants emit/accept RFC 4648 standard alphabet with padding. + * The "base64url" variants use URL-safe alphabet (`-`/`_`) with no padding, + * as used in JWTs. */ + +el_val_t sha256_hex(el_val_t input); +el_val_t sha256_bytes(el_val_t input); +el_val_t hmac_sha256_hex(el_val_t key, el_val_t message); +el_val_t hmac_sha256_bytes(el_val_t key, el_val_t message); +el_val_t base64_encode(el_val_t input); +el_val_t base64_decode(el_val_t input); +el_val_t base64url_encode(el_val_t input); +el_val_t base64url_decode(el_val_t input); + +/* Length-aware variants (internal — exposed for the rare caller that already + * has a known-length binary buffer and doesn't want to round-trip through + * a NUL-terminated el_val_t string). Sha256_bytes and hmac_sha256_bytes feed + * these implicitly. */ +el_val_t el_sha256_bytes_n(const unsigned char* data, size_t len); +el_val_t el_base64_encode_n(const unsigned char* data, size_t len, int url_safe); + +/* ── Post-quantum primitives (liboqs-backed) ──────────────────────────────── + * All inputs/outputs hex-encoded. Algorithm choices: + * Signature: CRYSTALS-Dilithium-3 (NIST level 3, balanced) + * KEM: CRYSTALS-Kyber-768 (NIST level 3) + * Hash: SHA3-256 (Keccak) (PQ-aware protocols favour SHA3 over SHA2) + * + * If liboqs is not linked (detected via __has_include() at compile + * time), the pq_* entry points return a JSON-shaped error string so callers + * fail loudly rather than silently fall back to classical schemes: + * {"error":"liboqs not linked, post-quantum primitives unavailable"} + * + * The hybrid handshake pairs X25519 with Kyber-768 per NIST PQ guidance and + * CNSA 2.0. Combined shared secret is HKDF-SHA256(x25519_ss || kyber_ss). + * Even if Kyber falls, X25519 holds; if X25519 falls under quantum attack, + * Kyber holds. SHA3-256 also remains usable independent of liboqs (the + * Keccak permutation is PQ-OK as a primitive). */ + +el_val_t pq_keygen_signature(void); +el_val_t pq_sign(el_val_t secret_key_hex, el_val_t message); +el_val_t pq_verify(el_val_t public_key_hex, el_val_t message, el_val_t signature_hex); + +el_val_t pq_kem_keygen(void); +el_val_t pq_kem_encaps(el_val_t public_key_hex); +el_val_t pq_kem_decaps(el_val_t secret_key_hex, el_val_t ciphertext_hex); + +el_val_t pq_hybrid_keygen(void); +el_val_t pq_hybrid_handshake(el_val_t remote_pub_combined); + +el_val_t sha3_256_hex(el_val_t input); + +/* ── Native VM builtin aliases (for compiled El source) ───────────────────── + * These match the El VM's native_* builtins so that El source compiled + * to C can call the same names without modification. */ + +el_val_t native_list_get(el_val_t list, el_val_t index); +el_val_t native_list_len(el_val_t list); +el_val_t native_list_append(el_val_t list, el_val_t elem); +el_val_t native_list_empty(void); +el_val_t native_list_clone(el_val_t list); +el_val_t native_string_chars(el_val_t s); +el_val_t native_int_to_str(el_val_t n); + +/* ── Method-call shorthand aliases ────────────────────────────────────────── + * The El method-call convention `obj.method(args)` compiles to + * `method(obj, args)`. These aliases expose the runtime functions under + * the short names that result from method calls in El source. + * + * Example: `myList.append(x)` → `append(myList, x)` (calls this alias) + * `myList.len()` → `len(myList)` (calls this alias) */ + +el_val_t append(el_val_t list, el_val_t elem); /* el_list_append */ +el_val_t len(el_val_t list); /* el_list_len */ +el_val_t get(el_val_t list, el_val_t index); /* el_list_get */ +el_val_t map_get(el_val_t map, el_val_t key); /* el_map_get */ +el_val_t map_set(el_val_t map, el_val_t key, el_val_t value); /* el_map_set */ + +#ifdef __cplusplus +} +#endif diff --git a/spec/codegen-js.md b/spec/codegen-js.md new file mode 100644 index 0000000..4920bc4 --- /dev/null +++ b/spec/codegen-js.md @@ -0,0 +1,236 @@ +# El JavaScript Backend (codegen-js) + +**Status:** scaffolded. Hello-world compiles and runs. ~50% language coverage. Core runtime (~30 builtins) implemented. CGI / DHARMA / LLM / Engram intentionally stubbed. + +**Authoritative files** + +| File | Role | +|---|---| +| `el-compiler/src/codegen-js.el` | El → JS code generator (mirrors `codegen.el`) | +| `el-compiler/runtime/el_runtime.js` | Browser/Node runtime that compiled programs link against | +| `el-compiler/src/compiler.el` | Adds `compile_js()` and `--target=js` CLI dispatch | +| `spec/codegen-js.md` | This document | + +--- + +## 1. Why a JS backend exists + +El compiles to C today. C is the right substrate for the agent runtime, the DHARMA daemon, and Engram. But three first-class consumers of El need to **run in a browser**, where C is not an option: + +1. **`el-ui/runtime/`** — the activation-based frontend framework written in JS. The long-term plan is to author components and the runtime itself in El and compile them down to JS. +2. **`cgi-studio`** — the web app for cultivating CGIs. Today it is hand-written JS. Once the JS backend is mature, the studio's UI logic can be authored in El and share types/identifier names with the CGI it cultivates. +3. **Marketplace plugin UIs** — third parties writing browser-side El that runs untrusted in a sandbox. They need a JS target. + +A secondary motivation: **El-on-Node**. CLI tooling, build scripts, and tests benefit from a tight `el → js → node` cycle without a `cc` step. + +--- + +## 2. Type representation strategy + +The C backend pretends every value is `int64_t`. That is a deliberate runtime trick to avoid dynamic dispatch in generated C. JavaScript already has tagged dynamic values, so the JS backend is **simpler**: every El value is a native JS value, and the tag of `el_val_t` collapses into the JS type system. + +| El type | C representation | JS representation | +|---|---|---| +| `Int` | `int64_t` (direct) | `number` (with `Number.isSafeInteger` caveat — see §6) | +| `Float` | `int64_t` bit-cast of `double` via `el_from_float` | `number` (no bit-cast — JS number IS a double) | +| `Bool` | `int64_t`, 0 = false, nonzero = true | `boolean` | +| `String` | `(int64_t)(uintptr_t)cstring` | `string` | +| `Void` | C `void` | `undefined` | +| `[T]` (List) | `el_val_t` pointer to refcounted struct | `Array` | +| `Map` | `el_val_t` pointer to refcounted struct | plain object `{[key]: any}` | +| `EL_NULL` (`0`) | `(el_val_t)0` | `null` | +| Any | `el_val_t` | `any` (no compile-time check) | + +**Key consequences:** + +- `+` on two strings is JS `+` (string concat) — no `el_str_concat()` runtime call needed for the common case. The runtime DOES export `el_str_concat` for the cases where codegen does not know the types. +- `==` on strings is `===` — not `str_eq()`. Same disambiguation logic as the C backend (look at left/right kind, fall back to `str_eq` for identifiers without int annotation). +- `Map` access `m["foo"]` compiles to JS `m["foo"]` (no `el_get_field`). For `Field` access (`m.foo`) we emit `m["foo"]` so it works on plain objects regardless of prototype shape. +- List access `arr[i]` is JS `arr[i]`. No bounds checking — same as C (which segfaults on bad index). Could add `el_list_get` wrapper later for safe access. +- `EL_NULL` becomes JS `null`, not `undefined`. The runtime checks for `=== null` consistently. This avoids the JS undefined/null fork and matches El's single null value. + +--- + +## 3. Builtin runtime layer (`el_runtime.js`) + +Same function names as `el_runtime.c` wherever possible, so codegen-js can emit the same call sites. The runtime is a single ES module that exposes every builtin as a named export AND attaches them to a `globalThis.__el` namespace (so generated code can do either `import * as el from './el_runtime.js'` or assume globals). + +**The codegen-js generated output uses the global-namespace style:** every emitted file starts with `import './el_runtime.js'` (which side-effects the globals) so call sites stay flat — `println(x)` not `el.println(x)`. This matches the C backend's flat call surface and keeps the generated code grep-compatible across targets. + +### Implemented today (~30 builtins) + +| Category | Functions | +|---|---| +| I/O | `println`, `print` | +| String | `el_str_concat`, `str_concat`, `str_eq`, `str_starts_with`, `str_ends_with`, `str_len`, `int_to_str`, `str_to_int`, `str_slice`, `str_contains`, `str_replace`, `str_to_upper`, `str_to_lower`, `str_trim`, `str_index_of`, `str_split`, `str_char_at`, `str_char_code`, `str_lower`, `str_upper` | +| Math | `el_abs`, `el_max`, `el_min` | +| List | `el_list_new`, `el_list_len`, `el_list_get`, `el_list_append`, `el_list_empty`, `el_list_clone`, `list_push`, `list_join`, `list_range` | +| Map | `el_map_new`, `el_get_field`, `el_map_get`, `el_map_set` | +| HTTP | `http_get`, `http_post`, `http_post_json` (via `fetch()`, returns `Promise` — see §5 async caveat) | +| FS | `fs_read`, `fs_write`, `fs_list` (Node-only, throw in browser) | +| JSON | `json_parse`, `json_stringify`, `json_get`, `json_get_string`, `json_get_int` | +| Time | `time_now`, `time_now_utc`, `sleep_secs` (Node), `sleep_ms` | +| Bool | `bool_to_str` | +| Process | `exit_program` (Node `process.exit`, throw in browser) | +| Refcount | `el_retain`, `el_release` (no-ops — JS has GC) | +| ARC method-call shortforms | `append`, `len`, `get`, `map_get`, `map_set` | +| Native VM aliases | `native_list_get`, `native_list_len`, `native_list_append`, `native_list_empty`, `native_list_clone`, `native_string_chars`, `native_int_to_str` | +| `args` | `args()` returns `process.argv.slice(2)` in Node, `[]` in browser | +| `state_*` | In-memory `Map` keyed by string | +| `env` | `process.env[k]` in Node, throws in browser | + +### Stubbed (throw at runtime) + +Every function in this list compiles successfully but throws `Error("not supported in JS target — needs server-side delegation: ")` when called. This is a **runtime** error, not a compile error, so it doesn't block compilation of code that has dead-code paths through these functions. + +- All `dharma_*` (membership in DHARMA network requires the daemon) +- All `engram_*` (needs the embedded SQLite + activation engine — could be reimplemented in JS later) +- All `llm_*` (CORS + API key handling — must go through a server-side proxy) +- `http_serve` (browsers don't host servers; Node could, but that's a separate runtime mode) +- `el_cgi_init` (CGI identity is a server-side concept) +- Crypto: `sha256_*`, `hmac_sha256_*`, `base64*` (deferred — can use `crypto.subtle` later) + +### Browser-side specific behavior + +When running in a browser: +- `println` / `print` map to `console.log` (no stdout in browsers) +- `http_get` / `http_post` use `fetch()` (CORS applies) +- `fs_*` throws (browsers have no fs) +- `args()` returns `[]` +- `env(k)` throws (or could read from a global config object — TBD) + +When running in Node: +- `println` / `print` map to `console.log` and `process.stdout.write` +- `fs_*` use `node:fs/promises` (sync versions for the simple cases) +- `args()` returns `process.argv.slice(2)` +- `env(k)` returns `process.env[k] ?? null` + +The runtime auto-detects via `typeof window === 'undefined'`. + +--- + +## 4. Tradeoffs vs the C backend + +| Concern | C backend | JS backend | +|---|---|---| +| **Static types** | El's `Int` becomes `int64_t`, real arithmetic | El's `Int` becomes `number` — loses precision past 2^53 | +| **Linking model** | Static link against `el_runtime.c` + libcurl + libpthread | ES module import of `el_runtime.js` | +| **Dynamic dispatch** | `dlsym` for `http_set_handler` / `llm_register_tool` (requires `-rdynamic`) | JS function value lookup via `globalThis[name]` — no compiler flag | +| **Tool registry** | dlsym walks symbol table; tool fns must be top-level C symbols | Tool fns live as exports of the generated module; trivially callable | +| **Memory model** | Refcounted lists/maps with `el_retain`/`el_release` to avoid leaks | JS GC handles all of it; `el_retain`/`el_release` are no-ops | +| **`+` overload** | Has to dispatch in codegen between `el_str_concat` and integer `+` because at C level both are `int64_t` | JS `+` is already overloaded: `"a" + "b"` → `"ab"`, `1 + 2` → `3`. Codegen still preserves the existing dispatch for safety, but the runtime fallback is correct | +| **Concurrency** | `pthread`-backed `http_serve` | Single-threaded event loop; `http_serve` not supported in this target | +| **HTTP client** | libcurl, blocking, returns body string | `fetch()` is async — see §5 | +| **CGI identity** | `el_cgi_init` runs at start of `main()` | Not supported; UI code is not a CGI principal | +| **DHARMA / LLM** | Native, blocking, libcurl-backed | Not supported — all such calls throw and the program is expected to delegate to a server-side El daemon via plain HTTP | +| **Compile speed** | El → C → cc → binary (cc is the slow step) | El → JS → done. Faster iteration | +| **Output size** | Static binary ~2MB | Source `.js` + ~10kb runtime | + +--- + +## 5. The async problem (the big deferred decision) + +`fetch()` is async. The C backend's `http_get(url)` is synchronous and returns the body string directly. El source was written assuming sync. Three options: + +1. **Pretend it's sync from El's POV; use synchronous XHR (browser) or `child_process.execSync('curl …')` (Node).** Bad: synchronous XHR is deprecated and frozen on the main thread; `execSync` is a hack. +2. **Make every `http_*` builtin in the JS runtime return a `Promise`, and rewrite codegen-js to insert `await` everywhere.** This requires turning every El function that transitively calls a network builtin into an `async fn` in JS. Doable, but invasive — the El AST does not currently mark async-ness. +3. **Compile El's call sites with implicit await; compile-time taint tracking marks every fn that transitively calls a network builtin as `async`. Generated JS uses `async function` and `await`.** This is the right answer long-term. + +**Decision for this scaffold:** option 3, but only the runtime side is implemented. `http_get` in `el_runtime.js` returns a `Promise`. `codegen-js.el` does NOT yet emit `async`/`await`. Calling `http_get` from compiled El will return a Promise that the El program will treat as a string (which produces `"[object Promise]"`). This is documented and accepted for the scaffold; the compile-time taint pass is a follow-up. + +For now, programs that don't touch HTTP work correctly. That covers `el-ui/runtime` (which only manipulates the DOM and a graph), most of cgi-studio's pure UI components, and all hello-world style programs. + +--- + +## 6. Number precision + +JS `number` is IEEE 754 double — only 53 bits of integer precision. El `Int` is `int64_t` and the runtime sometimes uses the full 64 bits (e.g. `time_now_utc` returns nanoseconds-since-epoch, which exceeds 2^53 in practice). + +**Decision for this scaffold:** accept the precision loss. Document it. UI code does not use 64-bit timestamps. If/when a use case demands it, `time_now_utc` can return a `BigInt` and we can introduce a `BigInt` sub-mode. That's a follow-up. + +--- + +## 7. What's NOT supported in JS target initially + +This is the canonical list. Programs that use any of these compile (no `#error`-style fail-fast like the C backend's capability check) but throw at runtime or behave as documented. + +| Feature | Status | Notes | +|---|---|---| +| `cgi {}` block | Compiled to a no-op + warning comment | CGI identity is server-side. UI code is not a CGI. | +| `service {}` block | Compiled to a no-op + warning comment | Same. | +| All `dharma_*` | Stub throws | Programs needing DHARMA must call a server-side daemon over HTTP | +| All `engram_*` | Stub throws | Could be ported to in-browser (IndexedDB-backed) later | +| All `llm_*` | Stub throws | Browser cannot hold API keys; route through server | +| `llm_register_tool` | Stub throws | Same | +| `http_serve` | Stub throws | Browsers cannot serve. Node-mode could, deferred | +| `http_set_handler` | Stub throws | Same | +| `match` expressions | Compiled (basic) | LitInt/LitStr/LitBool/Wildcard/Binding all work via `if/else` chain. Tagged-union match deferred | +| `type` (struct) defs | Skipped at codegen | Treated as documentation; structs are plain JS objects. `t["field"]` works | +| `enum` defs | Skipped at codegen | Same — enum values are bare strings or ints | +| `?` postfix (nil-prop) | No-op | Same as C backend's current state | +| `try` postfix | Stripped to inner | Same as C backend | +| Capability enforcement | Not enforced | The C backend uses `#error` directives; the JS backend lets the runtime stubs throw. Future: emit `throw new Error('capability violation')` at compile time | +| VBD role check | Not enforced | Same | +| Float bit-cast | Not needed | JS number is already a double | +| Crypto primitives | Stub throws | Easy to add via `crypto.subtle` later | +| `state_*` | In-memory only | No persistence; resets on page reload | +| `args()` | Node-only | Browser returns `[]` | +| `fs_*` | Node-only | Browser throws | + +--- + +## 8. CLI dispatch — `--target=js` + +The compiler entry point `compiler.el` adds a `compile_js(source: String) -> String` alongside the existing `compile()`. The CLI behavior: + +``` +elc # default — emit C +elc --target=c # explicit — emit C +elc --target=js # emit JS + +elc --target=js source.el # write JS to stdout (no out path) +``` + +The argv parser scans for a `--target=` token; remaining positional args are `` and optional ``. The dispatch logic stays in El: a `compile_dispatch(target, source) -> String` switch. + +--- + +## 9. The path to compiling el-ui/runtime through this backend + +This is the real-world test. `el-ui/runtime/src/` is currently 5 hand-written `.js` files. The path to authoring them in El: + +1. **Phase 1 — Hello-world** (this scaffold). Done. +2. **Phase 2 — language coverage.** Get codegen-js to ~95% parity with codegen.el for non-network features. Specifically: `match`, struct/enum field access, `?`-propagation, full `for`-over-list, complete unary/binary operators, lexical closures (the C backend doesn't have these but we'll need them for el-ui's component model). +3. **Phase 3 — DOM bridge.** Add `dom_*` builtins to el_runtime.js: `dom_create_element`, `dom_set_text`, `dom_append_child`, `dom_query`, `dom_listen`, etc. These are Node-as-El builtins for the browser; the C backend will add a stub set that errors. Source-shareable El UI code becomes possible. +4. **Phase 4 — Component class lowering.** El doesn't have classes; el-ui's `Component` is a JS class. Decide: extend El with a `component` keyword that compiles to JS class + C struct? Or have el-ui authors define components as `fn render_(state) -> String` and provide a small bootstrap. The latter is the lower-impact path. +5. **Phase 5 — Async taint pass.** Implement compile-time async tracking so `http_get` and friends produce `await fetch()` correctly. Required before authoring code that fetches data. +6. **Phase 6 — Port `el-ui/runtime/`.** Translate the 5 JS files to El, compile to JS, swap in. Run el-ui's existing tests. Iterate. +7. **Phase 7 — Port cgi-studio UI.** Larger surface area; same pattern. +8. **Phase 8 — Marketplace plugins.** Open the door for third-party UI El. + +The blocking item between phase 1 and phase 2 is incremental — every El construct used by el-ui's source needs codegen-js coverage. Phase 5 (async) is the architectural decision that needs explicit user buy-in, because it changes the language's effective semantics on the JS target. + +--- + +## 10. Test + +```bash +echo 'fn main() -> Void { println("hello from el-js") }' > /tmp/hello.el +elc --target=js /tmp/hello.el > /tmp/hello.js +node /tmp/hello.js +# → hello from el-js +``` + +This should pass after the bootstrap rebuild. See §11. + +--- + +## 11. Bootstrap status + +Adding `--target=js` to `compile()` requires regenerating the shipped `elc` binary at `dist/platform/elc`. The rebuild path is: + +1. Existing `elc` binary compiles updated `elc-combined.el` (which now includes `codegen-js.el` and the `--target=js` dispatch) → `elc.c`. +2. `cc` compiles `elc.c` → new `elc` binary. +3. New `elc` binary supports `--target=js`. + +The scaffold checks all four scaffold files in. The bootstrap rebuild happens as a follow-up step, gated on review of this design doc.