Files
JetCore/jetc/JetCodegen.cpp
T
Brad ArantandClaude Sonnet 5 d52ca61250 Add jetc: a prototype JET-to-C++ compiler, plus the Tag API it needed
jetc (JetCore/jetc/) compiles a subset of JET template syntax - literal
text, $[...]/#[...] variable output, <set>, <if>/<else>, <for>, <while> -
into a standalone C++ program that links against CoreUtils and the same
JetCore tag-class library jet-2.0 itself uses. The static tag/text
structure becomes real C++ control flow (if/for/while), resolved once at
build time instead of being re-scanned character-by-character from raw
source on every request and, for loop bodies, on every single iteration
(confirmed by reading Tag::parseContainer - it re-tokenizes its container
text from scratch every time it runs, since a tag's parse and its
execution are the same pass in this codebase). Anything genuinely
dynamic - variable values, expr/value comparisons, loop bounds - still
calls into the existing runtime (Tag::getVariable/resolveKeyword,
jet::Operand) exactly as the interpreter does; compiling doesn't try to
eliminate that; only the repeated re-parsing of the template's own shape.

A <if type="..."> or <while type="..."> comparison type is resolved to
its C++ operator once at compile time (type is a literal attribute value
in practice, not runtime data) instead of being re-checked against six
string constants on every evaluation.

Any tag jetc doesn't have a codegen rule for (mysql, sql, call, include,
a <set> with a container body, etc.) is captured as its exact raw source
span and handed to the existing interpreter at compiled-program runtime
via the new Tag::interpretSpan(), so an unsupported construct degrades
gracefully instead of failing to compile - a real, if narrower, program
rather than requiring full tag coverage up front.

Tag.h/.cpp: made assignVariable public (parallel to the already-public
getVariable - generated code needs a write path without being a Tag
subclass) and added interpretSpan() as the fallback's entry point into
parseContainer.

Verified live: two representative templates (jetc/demo.jet, an if/for/set
walkthrough; jetc/demo2.jet, a nested <if> inside a <for>, a <while> that
mutates and rereads its own condition variable across iterations, and a
fallback-interpreted <comment> tag) both compile, link, run standalone,
and produce byte-identical output to jet-2.0 running the same source.
Building and running jetc/demo2.jet's <while> loop surfaced a real,
previously-unknown MString bug (fixed separately, see CoreUtils commit
6e4e463) along with a separate, already-existing bug in __while.cpp's
value1/value2/type form (unrelated to this compiler, not fixed here -
see project memory).

jetc/build-compiled.sh builds the compiler and a given .jet file's
compiled binary in one step; jetc/demo.jet and demo2.jet are kept as
worked examples. Known limitations: <set> with a container body isn't
modeled (falls back to interpretation); a fallback-interpreted tag
nested inside compiled control flow uses the template's single top-level
scope for parent/local context rather than a faithful per-level chain,
which only matters for legacy tags that rely on nested "local" scoping.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-23 19:18:41 -07:00

211 lines
9.6 KiB
C++
Executable File

#include "JetCodegen.h"
#include <stdexcept>
namespace jetc {
std::string Codegen::ind(int indent) {
return std::string(indent * 3, ' ');
}
std::string Codegen::cstr(const std::string &raw) {
std::string r;
r.reserve(raw.size() + 8);
r += '"';
for (unsigned char c : raw) {
switch (c) {
case '\\': r += "\\\\"; break;
case '"': r += "\\\""; break;
case '\n': r += "\\n"; break;
case '\r': r += "\\r"; break;
case '\t': r += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
snprintf(buf, sizeof(buf), "\\x%02x\" \"", c);
r += buf;
} else {
r += (char)c;
}
}
}
r += '"';
return r;
}
const char *Codegen::staticTypeOp(const std::string &type, const char *rc) {
// type is known at compile time (a literal attribute value, not a
// runtime expression) so the comparison operator is picked once here
// instead of being re-checked against six string constants on every
// request the way Tag::__if/__while do it at runtime.
static thread_local std::string expr;
if (type == "eq") expr = std::string(rc) + " == 0";
else if (type == "ne") expr = std::string(rc) + " != 0";
else if (type == "lt") expr = std::string(rc) + " == -1";
else if (type == "le") expr = std::string(rc) + " != 1";
else if (type == "gt") expr = std::string(rc) + " == 1";
else if (type == "ge") expr = std::string(rc) + " != -1";
else throw std::runtime_error("jetc: type=\"" + type + "\" is not one of eq/ne/lt/le/gt/ge "
"(and jetc only supports a literal type= value - use expr= for a dynamic comparison).");
return expr.c_str();
}
void Codegen::emitBody(const std::vector<NodePtr> &nodes, int indent) {
for (auto &n : nodes) emitNode(n, indent);
}
void Codegen::emitNode(const NodePtr &n, int indent) {
std::string I = ind(indent);
switch (n->kind) {
case NodeKind::TEXT: {
if (n->text.empty()) break;
out << I << "out << " << cstr(n->text) << ";\n";
break;
}
case NodeKind::VAR: {
out << I << "{ coreutils::ZString v(" << cstr(n->varText) << "); out << scope.getVariable(v, true); }\n";
break;
}
case NodeKind::SET: {
std::string k = id("set_");
out << I << "{\n";
out << I << " scope.keywords[\"" << k << "name\"] = " << cstr(n->setName) << ";\n";
out << I << " scope.keywords[\"" << k << "scope\"] = " << cstr(n->setScope) << ";\n";
out << I << " coreutils::MString val;\n";
if (n->setHasExpr) {
out << I << " scope.keywords[\"" << k << "expr\"] = " << cstr(n->setExpr) << ";\n";
out << I << " val = jet::Operand(scope.keywords[\"" << k << "expr\"], scope).string;\n";
} else {
out << I << " scope.keywords[\"" << k << "value\"] = " << cstr(n->setHasValue ? n->setValue : std::string()) << ";\n";
out << I << " val = scope.resolveKeyword(\"" << k << "value\");\n";
}
out << I << " scope.assignVariable(scope.resolveKeyword(\"" << k << "name\"), val, scope.resolveKeyword(\"" << k << "scope\"));\n";
out << I << "}\n";
break;
}
case NodeKind::IF: {
std::string k = id("if_");
out << I << "{\n";
out << I << " bool cond;\n";
if (n->cond.mode == CondMode::EXPR) {
out << I << " scope.keywords[\"" << k << "expr\"] = " << cstr(n->cond.expr) << ";\n";
out << I << " cond = jet::Operand(scope.keywords[\"" << k << "expr\"], scope).boolean;\n";
} else {
out << I << " scope.keywords[\"" << k << "value1\"] = " << cstr(n->cond.value1) << ";\n";
out << I << " scope.keywords[\"" << k << "value2\"] = " << cstr(n->cond.value2) << ";\n";
out << I << " int rc = scope.resolveKeyword(\"" << k << "value1\").compare(scope.resolveKeyword(\"" << k << "value2\"));\n";
out << I << " cond = (" << staticTypeOp(n->cond.type, "rc") << ");\n";
}
out << I << " if (cond) {\n";
emitBody(n->thenBody, indent + 2);
if (n->hasElse) {
out << I << " } else {\n";
emitBody(n->elseBody, indent + 2);
}
out << I << " }\n";
out << I << "}\n";
break;
}
case NodeKind::FOR: {
std::string k = id("for_");
out << I << "{\n";
out << I << " scope.keywords[\"" << k << "start\"] = " << cstr(n->forHasStart ? n->forStart : std::string("0")) << ";\n";
out << I << " scope.keywords[\"" << k << "end\"] = " << cstr(n->forEnd) << ";\n";
out << I << " scope.keywords[\"" << k << "step\"] = " << cstr(n->forStep) << ";\n";
out << I << " double ix = scope.resolveKeyword(\"" << k << "start\").asDouble();\n";
if (n->forHasName) {
out << I << " scope.keywords[\"" << k << "name\"] = " << cstr(n->forName) << ";\n";
out << I << " scope.keywords[\"" << k << "scope\"] = " << cstr(n->forScope) << ";\n";
}
out << I << " for (; ix <= scope.resolveKeyword(\"" << k << "end\").asDouble(); ix += scope.resolveKeyword(\"" << k << "step\").asDouble()) {\n";
if (n->forHasName) {
out << I << " coreutils::MString nameSel = scope.resolveKeyword(\"" << k << "name\");\n";
out << I << " coreutils::MString scopeSel = scope.resolveKeyword(\"" << k << "scope\");\n";
out << I << " if ((scopeSel == \"\") || (scopeSel == \"global\")) global.variables[nameSel] = ix;\n";
out << I << " else if (scopeSel == \"local\") scope.local->variables[nameSel] = ix;\n";
out << I << " else if (scopeSel == \"parent\") scope.parent->local->variables[nameSel] = ix;\n";
out << I << " else throw coreutils::Exception(\"scope value is not valid.\");\n";
}
emitBody(n->body, indent + 2);
out << I << " }\n";
out << I << "}\n";
break;
}
case NodeKind::WHILE: {
std::string k = id("while_");
out << I << "{\n";
out << I << " bool cond;\n";
if (n->cond.mode == CondMode::EXPR) {
out << I << " scope.keywords[\"" << k << "expr\"] = " << cstr(n->cond.expr) << ";\n";
out << I << " coreutils::MString exprSaved = scope.keywords[\"" << k << "expr\"];\n";
out << I << " cond = jet::Operand(scope.keywords[\"" << k << "expr\"], scope).boolean;\n";
out << I << " while (cond) {\n";
emitBody(n->body, indent + 2);
out << I << " scope.keywords[\"" << k << "expr\"].reset();\n";
out << I << " scope.keywords[\"" << k << "expr\"] = exprSaved;\n";
out << I << " cond = jet::Operand(scope.keywords[\"" << k << "expr\"], scope).boolean;\n";
out << I << " }\n";
} else {
out << I << " scope.keywords[\"" << k << "value1\"] = " << cstr(n->cond.value1) << ";\n";
out << I << " scope.keywords[\"" << k << "value2\"] = " << cstr(n->cond.value2) << ";\n";
out << I << " int rc = scope.resolveKeyword(\"" << k << "value1\").compare(scope.resolveKeyword(\"" << k << "value2\"));\n";
out << I << " cond = (" << staticTypeOp(n->cond.type, "rc") << ");\n";
out << I << " while (cond) {\n";
emitBody(n->body, indent + 2);
out << I << " int rc2 = scope.resolveKeyword(\"" << k << "value1\").compare(scope.resolveKeyword(\"" << k << "value2\"));\n";
out << I << " cond = (" << staticTypeOp(n->cond.type, "rc2") << ");\n";
out << I << " }\n";
}
out << I << "}\n";
break;
}
case NodeKind::FALLBACK: {
out << I << "{ coreutils::ZString span(" << cstr(n->rawSpan) << "); scope.interpretSpan(span, out); }\n";
break;
}
}
}
std::string Codegen::generate(const std::vector<NodePtr> &top, const std::string &sourceFileComment) {
out.str("");
out.clear();
out << "// " << sourceFileComment << "\n";
out << "// Generated by jetc - DO NOT EDIT BY HAND.\n";
out << "#include \"Global.h\"\n";
out << "#include \"Tag.h\"\n";
out << "#include \"Operand.h\"\n";
out << "#include \"ZString.h\"\n";
out << "#include \"MString.h\"\n";
out << "#include \"Exception.h\"\n";
out << "#include <iostream>\n\n";
out << "static void jetc_render(jet::Global &global, coreutils::MString &out) {\n";
out << " coreutils::ZString jetc_empty(\"\");\n";
out << " coreutils::MString jetc_scopeOut;\n";
out << " jet::Tag scope(jetc_empty, jetc_scopeOut, global, NULL, NULL);\n";
emitBody(top, 1);
out << "}\n\n";
out << "int main(int argc, char **argv, char **envp) {\n";
out << " jet::Global global(envp);\n";
out << " coreutils::MString out;\n";
out << " try {\n";
out << " jetc_render(global, out);\n";
out << " global.outputHeaders();\n";
out << " std::cout << out;\n";
out << " } catch(coreutils::Exception e) {\n";
out << " std::cout << out;\n";
out << " std::cout << \"\\n----- Error: \" << e.text << std::endl;\n";
out << " return 1;\n";
out << " }\n";
out << " return 0;\n";
out << "}\n";
return out.str();
}
}