// =========================================================================== // sleela_emit.cpp -- Transpile a Sleela Program into Java / Sleela / C. // =========================================================================== #include "sleela_emit.h" #include "lowering.h" #include #include namespace nordshrift { namespace { using namespace sleela; std::string esc(const std::string& s) { std::string o; for (char c : s) { switch (c) { case '"': o += "\\\""; break; case '\\': o += "\\\\"; break; case '\n': o += "\\n"; break; case '\t': o += "\\t"; break; case '\r': o += "\\r"; break; default: o += c; } } return o; } // --------------------------------------------------------------------------- // Java emitter: Sleela is Java-like, so this is close to a pretty-printer. // Each Sleela class -> a Java class; fields -> static fields; methods -> static // methods; print(x) -> System.out.println(x). Java's own + already concatenates. // --------------------------------------------------------------------------- struct JavaGen { std::ostringstream out; std::string pkg; std::string mapType(const std::string& t) { if (t == "String") return "String"; if (t == "int") return "long"; // Sleela ints are 64-bit if (t == "double") return "double"; if (t == "boolean") return "boolean"; if (t == "void") return "void"; return "Object"; } void run(const Program& p) { out << "// Generated by Nordshrift (Java target)\n"; for (const auto& a : p.annotations.all()) out << "// @" << a.name << " " << a.value << "\n"; if (!pkg.empty()) out << "package " << pkg << ";\n\n"; for (auto& c : p.classes) emitClass(c); } void ind(int n) { for (int i = 0; i < n; i++) out << " "; } void emitClass(const ClassDecl& c) { out << "public final class " << c.name << " {\n"; for (auto& f : c.fields) { ind(1); out << "static " << mapType(f.type) << " " << f.name; if (f.init) { out << " = "; emitExpr(f.init.get()); } out << ";\n"; } if (!c.fields.empty()) out << "\n"; for (auto& m : c.methods) emitMethod(m); // a Java entry point that calls Sleela main() if present for (auto& m : c.methods) if (m.name == "main" && m.params.empty()) { ind(1); out << "public static void main(String[] args) { " << c.name << ".main(); }\n"; break; } out << "}\n\n"; } void emitMethod(const Method& m) { ind(1); out << "static " << mapType(m.retType) << " " << m.name << "("; for (size_t i = 0; i < m.params.size(); i++) { if (i) out << ", "; out << mapType(m.params[i].type) << " " << m.params[i].name; } out << ") {\n"; emitBlock(*m.body, 2); ind(1); out << "}\n"; } void emitBlock(const Block& b, int n) { for (auto& s : b.stmts) emitStmt(s.get(), n); } void emitStmt(const Stmt* s, int n) { if (auto b = dynamic_cast(s)) { ind(n); out << "{\n"; emitBlock(*b, n+1); ind(n); out << "}\n"; return; } if (auto d = dynamic_cast(s)) { ind(n); out << mapType(d->type) << " " << d->name; if (d->init) { out << " = "; emitExpr(d->init.get()); } out << ";\n"; return; } if (auto a = dynamic_cast(s)) { ind(n); out << a->name << " = "; emitExpr(a->value.get()); out << ";\n"; return; } if (auto e = dynamic_cast(s)) { ind(n); emitExpr(e->expr.get()); out << ";\n"; return; } if (auto p = dynamic_cast(s)) { ind(n); out << "System.out.println("; emitExpr(p->expr.get()); out << ");\n"; return; } if (auto r = dynamic_cast(s)) { ind(n); out << "return"; if (r->value) { out << " "; emitExpr(r->value.get()); } out << ";\n"; return; } if (auto i = dynamic_cast(s)) { ind(n); out << "if ("; emitExpr(i->cond.get()); out << ") {\n"; emitStmt(i->thenS.get(), n+1); ind(n); out << "}"; if (i->elseS) { out << " else {\n"; emitStmt(i->elseS.get(), n+1); ind(n); out << "}"; } out << "\n"; return; } if (auto w = dynamic_cast(s)) { ind(n); out << "while ("; emitExpr(w->cond.get()); out << ") {\n"; emitStmt(w->body.get(), n+1); ind(n); out << "}\n"; return; } if (auto f = dynamic_cast(s)) { ind(n); out << "for ("; if (f->init) emitInline(f->init.get()); out << "; "; if (f->cond) emitExpr(f->cond.get()); out << "; "; if (f->update) emitInline(f->update.get()); out << ") {\n"; emitStmt(f->body.get(), n+1); ind(n); out << "}\n"; return; } } // for-header init/update without indentation or trailing ';' void emitInline(const Stmt* s) { if (auto d = dynamic_cast(s)) { out << mapType(d->type) << " " << d->name; if (d->init) { out << " = "; emitExpr(d->init.get()); } return; } if (auto a = dynamic_cast(s)) { out << a->name << " = "; emitExpr(a->value.get()); return; } if (auto e = dynamic_cast(s)) { emitExpr(e->expr.get()); return; } } void emitExpr(const Expr* e) { if (auto x = dynamic_cast(e)) { out << x->value << "L"; return; } if (auto x = dynamic_cast(e)) { out << x->value; return; } if (auto x = dynamic_cast(e)) { out << (x->value ? "true" : "false"); return; } if (auto x = dynamic_cast(e)) { out << "\"" << esc(x->value) << "\""; return; } if (dynamic_cast(e)) { out << "null"; return; } if (auto x = dynamic_cast(e)) { out << x->name; return; } if (auto x = dynamic_cast(e)) { out << x->op << "("; emitExpr(x->operand.get()); out << ")"; return; } if (auto x = dynamic_cast(e)) { out << "("; emitExpr(x->lhs.get()); out << " " << x->op << " "; emitExpr(x->rhs.get()); out << ")"; return; } if (auto x = dynamic_cast(e)) { out << x->callee << "("; for (size_t i = 0; i < x->args.size(); i++) { if (i) out << ", "; emitExpr(x->args[i].get()); } out << ")"; return; } } }; // --------------------------------------------------------------------------- // Sleela emitter: canonical pretty-print of the same AST (round-trip target). // --------------------------------------------------------------------------- struct SleelaGen { std::ostringstream out; void run(const Program& p) { out << "// Generated by Nordshrift (Sleela target)\n"; for (const auto& a : p.annotations.all()) out << "@" << a.name << " " << a.value << "\n"; for (auto& c : p.classes) { out << "class " << c.name << " {\n"; for (auto& f : c.fields) { out << " " << f.type << " " << f.name; if (f.init) { out << " = "; emitExpr(f.init.get()); } out << ";\n"; } if (!c.fields.empty()) out << "\n"; for (auto& m : c.methods) emitMethod(m); out << "}\n\n"; } } void ind(int n) { for (int i = 0; i < n; i++) out << " "; } void emitMethod(const Method& m) { ind(1); out << m.retType << " " << m.name << "("; for (size_t i = 0; i < m.params.size(); i++) { if (i) out << ", "; out << m.params[i].type << " " << m.params[i].name; } out << ") {\n"; emitBlock(*m.body, 2); ind(1); out << "}\n"; } void emitBlock(const Block& b, int n) { for (auto& s : b.stmts) emitStmt(s.get(), n); } void emitStmt(const Stmt* s, int n) { if (auto b = dynamic_cast(s)) { ind(n); out << "{\n"; emitBlock(*b, n+1); ind(n); out << "}\n"; return; } if (auto d = dynamic_cast(s)) { ind(n); out << d->type << " " << d->name; if (d->init) { out << " = "; emitExpr(d->init.get()); } out << ";\n"; return; } if (auto a = dynamic_cast(s)) { ind(n); out << a->name << " = "; emitExpr(a->value.get()); out << ";\n"; return; } if (auto e = dynamic_cast(s)) { ind(n); emitExpr(e->expr.get()); out << ";\n"; return; } if (auto p = dynamic_cast(s)) { ind(n); out << "print("; emitExpr(p->expr.get()); out << ");\n"; return; } if (auto r = dynamic_cast(s)) { ind(n); out << "return"; if (r->value) { out << " "; emitExpr(r->value.get()); } out << ";\n"; return; } if (auto i = dynamic_cast(s)) { ind(n); out << "if ("; emitExpr(i->cond.get()); out << ") {\n"; emitStmt(i->thenS.get(), n+1); ind(n); out << "}"; if (i->elseS) { out << " else {\n"; emitStmt(i->elseS.get(), n+1); ind(n); out << "}"; } out << "\n"; return; } if (auto w = dynamic_cast(s)) { ind(n); out << "while ("; emitExpr(w->cond.get()); out << ") {\n"; emitStmt(w->body.get(), n+1); ind(n); out << "}\n"; return; } if (auto f = dynamic_cast(s)) { ind(n); out << "for ("; if (f->init) emitInline(f->init.get()); out << "; "; if (f->cond) emitExpr(f->cond.get()); out << "; "; if (f->update) emitInline(f->update.get()); out << ") {\n"; emitStmt(f->body.get(), n+1); ind(n); out << "}\n"; return; } } void emitInline(const Stmt* s) { if (auto d = dynamic_cast(s)) { out << d->type << " " << d->name; if (d->init) { out << " = "; emitExpr(d->init.get()); } return; } if (auto a = dynamic_cast(s)) { out << a->name << " = "; emitExpr(a->value.get()); return; } if (auto e = dynamic_cast(s)) { emitExpr(e->expr.get()); return; } } void emitExpr(const Expr* e) { if (auto x = dynamic_cast(e)) { out << x->value; return; } if (auto x = dynamic_cast(e)) { out << x->value; return; } if (auto x = dynamic_cast(e)) { out << (x->value ? "true" : "false"); return; } if (auto x = dynamic_cast(e)) { out << "\"" << esc(x->value) << "\""; return; } if (dynamic_cast(e)) { out << "null"; return; } if (auto x = dynamic_cast(e)) { out << x->name; return; } if (auto x = dynamic_cast(e)) { out << x->op << "("; emitExpr(x->operand.get()); out << ")"; return; } if (auto x = dynamic_cast(e)) { out << "("; emitExpr(x->lhs.get()); out << " " << x->op << " "; emitExpr(x->rhs.get()); out << ")"; return; } if (auto x = dynamic_cast(e)) { out << x->callee << "("; for (size_t i=0;iargs.size();i++){ if(i) out<<", "; emitExpr(x->args[i].get()); } out << ")"; return; } } }; // --------------------------------------------------------------------------- // C emitter: a tiny tagged-value runtime (nsv) carries int/double/bool/string, // mirroring the Sleela core's value model so + concatenates like Sleela. // --------------------------------------------------------------------------- const char* kCPrelude = R"PRE(/* Generated by Nordshrift (C target) */ #define _POSIX_C_SOURCE 200809L #include #include #include typedef enum { NSV_INT, NSV_DBL, NSV_BOOL, NSV_STR } nsv_tag; typedef struct { nsv_tag t; long long i; double d; int b; char* s; } nsv; static nsv nsv_int(long long v){ nsv x; x.t=NSV_INT; x.i=v; return x; } static nsv nsv_dbl(double v){ nsv x; x.t=NSV_DBL; x.d=v; return x; } static nsv nsv_bool(int v){ nsv x; x.t=NSV_BOOL; x.b=v?1:0; return x; } static nsv nsv_str(const char* v){ nsv x; x.t=NSV_STR; x.s=strdup(v); return x; } static double nsv_num(nsv v){ return v.t==NSV_INT?(double)v.i:v.t==NSV_DBL?v.d:v.t==NSV_BOOL?v.b:0; } static int nsv_truth(nsv v){ return v.t==NSV_INT?v.i!=0:v.t==NSV_DBL?v.d!=0:v.t==NSV_BOOL?v.b:v.t==NSV_STR?1:0; } static void nsv_render(nsv v,char*b,size_t n){ switch(v.t){case NSV_INT:snprintf(b,n,"%lld",v.i);break;case NSV_DBL:snprintf(b,n,"%g",v.d);break;case NSV_BOOL:snprintf(b,n,"%s",v.b?"true":"false");break;case NSV_STR:snprintf(b,n,"%s",v.s?v.s:"");break;} } static nsv nsv_add(nsv a,nsv b){ if(a.t==NSV_STR||b.t==NSV_STR){char x[256],y[256],o[512];nsv_render(a,x,sizeof x);nsv_render(b,y,sizeof y);snprintf(o,sizeof o,"%s%s",x,y);return nsv_str(o);} if(a.t==NSV_INT&&b.t==NSV_INT)return nsv_int(a.i+b.i); return nsv_dbl(nsv_num(a)+nsv_num(b)); } static nsv nsv_sub(nsv a,nsv b){ if(a.t==NSV_INT&&b.t==NSV_INT)return nsv_int(a.i-b.i); return nsv_dbl(nsv_num(a)-nsv_num(b)); } static nsv nsv_mul(nsv a,nsv b){ if(a.t==NSV_INT&&b.t==NSV_INT)return nsv_int(a.i*b.i); return nsv_dbl(nsv_num(a)*nsv_num(b)); } static nsv nsv_div(nsv a,nsv b){ if(a.t==NSV_INT&&b.t==NSV_INT)return nsv_int(a.i/b.i); return nsv_dbl(nsv_num(a)/nsv_num(b)); } static nsv nsv_mod(nsv a,nsv b){ return nsv_int(a.i%b.i); } static nsv nsv_neg(nsv a){ return a.t==NSV_INT?nsv_int(-a.i):nsv_dbl(-nsv_num(a)); } static nsv nsv_lt(nsv a,nsv b){ return nsv_bool(nsv_num(a)nsv_num(b)); } static nsv nsv_ge(nsv a,nsv b){ return nsv_bool(nsv_num(a)>=nsv_num(b)); } static nsv nsv_eq(nsv a,nsv b){ if(a.t==NSV_STR&&b.t==NSV_STR)return nsv_bool(strcmp(a.s,b.s)==0); return nsv_bool(nsv_num(a)==nsv_num(b)); } static nsv nsv_ne(nsv a,nsv b){ nsv e=nsv_eq(a,b); return nsv_bool(!e.b); } static nsv nsv_and(nsv a,nsv b){ return nsv_bool(nsv_truth(a)&&nsv_truth(b)); } static nsv nsv_or(nsv a,nsv b){ return nsv_bool(nsv_truth(a)||nsv_truth(b)); } static nsv nsv_not(nsv a){ return nsv_bool(!nsv_truth(a)); } static void nsv_print(nsv v){ char b[512]; nsv_render(v,b,sizeof b); printf("%s\n",b); } )PRE"; struct CGen { std::ostringstream out; std::string curClass; // class currently being emitted (to qualify calls) void run(const Program& p) { out << kCPrelude << "\n"; // forward declarations (with real parameter arity) for (auto& c : p.classes) for (auto& m : c.methods) { out << "static nsv " << c.name << "_" << m.name << "("; if (m.params.empty()) out << "void"; else for (size_t i = 0; i < m.params.size(); i++) { if (i) out << ", "; out << "nsv"; } out << ");\n"; } // fields as globals for (auto& c : p.classes) for (auto& f : c.fields) out << "static nsv " << f.name << ";\n"; out << "\n"; for (auto& c : p.classes) { curClass = c.name; for (auto& m : c.methods) emitMethod(c.name, m); } // main() out << "int main(void) {\n"; for (auto& c : p.classes) for (auto& f : c.fields) { out << " " << f.name << " = "; if (f.init) emitExpr(f.init.get()); else out << "nsv_int(0)"; out << ";\n"; } for (auto& c : p.classes) for (auto& m : c.methods) if (m.name == "main") { out << " " << c.name << "_main();\n"; } out << " return 0;\n}\n"; } void ind(int n) { for (int i = 0; i < n; i++) out << " "; } void emitMethod(const std::string& cls, const Method& m) { out << "static nsv " << cls << "_" << m.name << "("; if (m.params.empty()) out << "void"; else for (size_t i = 0; i < m.params.size(); i++) { if (i) out << ", "; out << "nsv " << m.params[i].name; } out << ") {\n"; emitBlock(*m.body, 1); ind(1); out << "return nsv_int(0);\n"; out << "}\n\n"; } void emitBlock(const Block& b, int n) { for (auto& s : b.stmts) emitStmt(s.get(), n); } void emitStmt(const Stmt* s, int n) { if (auto b = dynamic_cast(s)) { ind(n); out << "{\n"; emitBlock(*b, n+1); ind(n); out << "}\n"; return; } if (auto d = dynamic_cast(s)) { ind(n); out << "nsv " << d->name << " = "; if (d->init) emitExpr(d->init.get()); else out << "nsv_int(0)"; out << ";\n"; return; } if (auto a = dynamic_cast(s)) { ind(n); out << a->name << " = "; emitExpr(a->value.get()); out << ";\n"; return; } if (auto e = dynamic_cast(s)) { ind(n); emitExpr(e->expr.get()); out << ";\n"; return; } if (auto p = dynamic_cast(s)) { ind(n); out << "nsv_print("; emitExpr(p->expr.get()); out << ");\n"; return; } if (auto r = dynamic_cast(s)) { ind(n); out << "return "; if (r->value) emitExpr(r->value.get()); else out << "nsv_int(0)"; out << ";\n"; return; } if (auto i = dynamic_cast(s)) { ind(n); out << "if (nsv_truth("; emitExpr(i->cond.get()); out << ")) {\n"; emitStmt(i->thenS.get(), n+1); ind(n); out << "}"; if (i->elseS) { out << " else {\n"; emitStmt(i->elseS.get(), n+1); ind(n); out << "}"; } out << "\n"; return; } if (auto w = dynamic_cast(s)) { ind(n); out << "while (nsv_truth("; emitExpr(w->cond.get()); out << ")) {\n"; emitStmt(w->body.get(), n+1); ind(n); out << "}\n"; return; } if (auto f = dynamic_cast(s)) { ind(n); out << "{\n"; if (f->init) { ind(n+1); emitInlineDecl(f->init.get()); out << ";\n"; } ind(n+1); out << "while ("; if (f->cond) { out << "nsv_truth("; emitExpr(f->cond.get()); out << ")"; } else out << "1"; out << ") {\n"; emitStmt(f->body.get(), n+2); if (f->update) { ind(n+2); emitInlineDecl(f->update.get()); out << ";\n"; } ind(n+1); out << "}\n"; ind(n); out << "}\n"; return; } } void emitInlineDecl(const Stmt* s) { if (auto d = dynamic_cast(s)) { out << "nsv " << d->name << " = "; if (d->init) emitExpr(d->init.get()); else out << "nsv_int(0)"; return; } if (auto a = dynamic_cast(s)) { out << a->name << " = "; emitExpr(a->value.get()); return; } if (auto e = dynamic_cast(s)) { emitExpr(e->expr.get()); return; } } void emitExpr(const Expr* e) { if (auto x = dynamic_cast(e)) { out << "nsv_int(" << x->value << ")"; return; } if (auto x = dynamic_cast(e)) { out << "nsv_dbl(" << x->value << ")"; return; } if (auto x = dynamic_cast(e)) { out << "nsv_bool(" << (x->value?1:0) << ")"; return; } if (auto x = dynamic_cast(e)) { out << "nsv_str(\"" << esc(x->value) << "\")"; return; } if (dynamic_cast(e)) { out << "nsv_int(0)"; return; } if (auto x = dynamic_cast(e)) { out << x->name; return; } if (auto x = dynamic_cast(e)) { out << (x->op=="-"?"nsv_neg(":"nsv_not("); emitExpr(x->operand.get()); out << ")"; return; } if (auto x = dynamic_cast(e)) { const std::string& o = x->op; std::string fn = o=="+"?"nsv_add":o=="-"?"nsv_sub":o=="*"?"nsv_mul":o=="/"?"nsv_div":o=="%"?"nsv_mod": o=="=="?"nsv_eq":o=="!="?"nsv_ne":o=="<"?"nsv_lt":o=="<="?"nsv_le":o==">"?"nsv_gt": o==">="?"nsv_ge":o=="&&"?"nsv_and":"nsv_or"; out << fn << "("; emitExpr(x->lhs.get()); out << ", "; emitExpr(x->rhs.get()); out << ")"; return; } if (auto x = dynamic_cast(e)) { // Sleela calls are unqualified within a class; C functions are // named Class_method, so qualify with the current class. out << curClass << "_" << x->callee << "("; for (size_t i = 0; i < x->args.size(); i++) { if (i) out << ", "; emitExpr(x->args[i].get()); } out << ")"; return; } } }; } // namespace std::string emitProgram(const sleela::Program& prog, TargetLang lang, const std::string& packageRoot) { // All target backends share one target-neutral lowering gate. This keeps // unsupported AST nodes from silently diverging between Java/Sleela/C. LoweredProgram lowered = lowerProgram(prog); if (!lowered.ok()) throw std::runtime_error("Nordshrift lowering failed: " + lowered.error); if (lang == TargetLang::Java) { JavaGen g; g.pkg = packageRoot; g.run(prog); return g.out.str(); } if (lang == TargetLang::C) { CGen g; g.run(prog); return g.out.str(); } SleelaGen g; g.run(prog); return g.out.str(); } } // namespace nordshrift