Steps 309-319: Sprint 11 Phases 11d-e — Kotlin/C# languages + workflow annotation foundation

Phase 11d (Steps 309-313): Kotlin + C# parsers and generators
- KotlinParser (regex-based): fun, suspend fun, class, data class, val/var
- KotlinGenerator: idiomatic Kotlin output with type mappings
- CSharpParser (regex-based): methods, async, class, interface
- CSharpGenerator: Allman braces, foreach, Task async, LINQ types
- Pipeline integration for both languages, 10 parsers + 10 generators

Phase 11e (Steps 314-319): Workflow annotation foundation
- AnnotationInference: generalized multi-subject inference engine
- Subject 9 routing annotations: ContextWidth, Review, Ambiguity,
  Automatability, Priority, ImplementationStatus
- SkeletonAST: project specification before code exists
- Architect tooling: createSkeleton, addSkeletonNode, getProjectModel,
  inferAnnotations RPCs + 4 MCP tools (42+ total)
- Inference-to-routing bridge: complexity→ambiguity, getter→deterministic
- TrainingDataExporter + TrainingDataGenerator scaffolding

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill
2026-02-15 15:50:06 -07:00
parent a2a9fe6f97
commit 0d51a6fe4c
34 changed files with 5801 additions and 4 deletions

View File

@@ -506,6 +506,9 @@ class AmbiguityAnnotation : public Annotation {
public:
std::string intent;
std::vector<std::string> options;
// Workflow routing fields (Subject 9)
std::string level; // "none" | "low" | "medium" | "high"
std::string description;
AmbiguityAnnotation() { conceptType = "AmbiguityAnnotation"; }
};
@@ -538,3 +541,40 @@ public:
std::string reason;
DecisionAnnotation() { conceptType = "DecisionAnnotation"; }
};
// ── Subject 9: Workflow Routing Annotations ──────────────────────
class ContextWidthAnnotation : public Annotation {
public:
std::string width; // "local" | "file" | "project" | "cross-project"
ContextWidthAnnotation() { conceptType = "ContextWidthAnnotation"; }
};
class ReviewAnnotation : public Annotation {
public:
bool required = false;
std::string reviewer; // "human" | "agent" | "either"
std::string reason;
ReviewAnnotation() { conceptType = "ReviewAnnotation"; }
};
class AutomatabilityAnnotation : public Annotation {
public:
std::string strategy; // "deterministic" | "template" | "slm" | "llm" | "human"
double confidence = 0.0;
AutomatabilityAnnotation() { conceptType = "AutomatabilityAnnotation"; }
};
class PriorityAnnotation : public Annotation {
public:
std::string level; // "critical" | "high" | "medium" | "low"
std::vector<std::string> blockedBy;
PriorityAnnotation() { conceptType = "PriorityAnnotation"; }
};
class ImplementationStatusAnnotation : public Annotation {
public:
std::string status; // "skeleton" | "partial" | "complete" | "needs-review"
std::string assignee;
ImplementationStatusAnnotation() { conceptType = "ImplementationStatusAnnotation"; }
};

View File

@@ -75,6 +75,13 @@ public:
virtual std::string visitChoiceAnnotation(const ChoiceAnnotation*) = 0;
virtual std::string visitDecisionAnnotation(const DecisionAnnotation*) = 0;
// Subject 9: Workflow Routing
virtual std::string visitContextWidthAnnotation(const ContextWidthAnnotation*) = 0;
virtual std::string visitReviewAnnotation(const ReviewAnnotation*) = 0;
virtual std::string visitAutomatabilityAnnotation(const AutomatabilityAnnotation*) = 0;
virtual std::string visitPriorityAnnotation(const PriorityAnnotation*) = 0;
virtual std::string visitImplementationStatusAnnotation(const ImplementationStatusAnnotation*) = 0;
// Semantic Core
virtual std::string visitIntentAnnotation(const IntentAnnotation*) = 0;
virtual std::string visitComplexityAnnotation(const ComplexityAnnotation*) = 0;

View File

@@ -0,0 +1,357 @@
#pragma once
#include "ProjectionGenerator.h"
#include "ClassDeclaration.h"
#include "GenericType.h"
#include "AsyncNodes.h"
#include "../SemannoAnnotationImpl.h"
class CSharpGenerator : public ProjectionGenerator, public SemannoAnnotationImpl<CSharpGenerator> {
public:
std::string commentPrefix() const { return "// "; }
std::string generate(const ASTNode* node) override {
return dispatchGenerate(this, node, "// Unknown concept: ");
}
std::string visitModule(const Module* module) override {
std::ostringstream oss;
oss << "// Module: " << module->name << "\n\n";
auto variables = module->getChildren("variables");
for (const auto* var : variables) oss << generate(var) << "\n";
if (!variables.empty()) oss << "\n";
auto functions = module->getChildren("functions");
for (size_t i = 0; i < functions.size(); ++i) {
if (i > 0) oss << "\n";
oss << generate(functions[i]);
}
return oss.str();
}
std::string visitFunction(const Function* function) override {
std::ostringstream oss;
auto annotations = function->getChildren("annotations");
for (const auto* anno : annotations) oss << generate(anno) << "\n";
oss << "public void " << function->name << "(";
auto params = function->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ")\n{\n";
auto body = function->getChildren("body");
for (const auto* stmt : body)
oss << " " << generate(stmt) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitVariable(const Variable* v) override {
std::ostringstream oss;
auto type = v->getChild("type");
oss << (type ? generate(type) : "var") << " " << v->name;
auto init = v->getChild("initializer");
if (init) oss << " = " << generate(init);
oss << ";";
return oss.str();
}
std::string visitParameter(const Parameter* p) override {
auto type = p->getChild("type");
return (type ? generate(type) : "object") + " " + p->name;
}
std::string visitAssignment(const Assignment* a) override {
auto t = a->getChild("target"); auto v = a->getChild("value");
return (t ? generate(t) : "") + " = " + (v ? generate(v) : "null") + ";";
}
std::string visitReturn(const Return* r) override {
auto v = r->getChild("value");
return v ? "return " + generate(v) + ";" : "return;";
}
std::string visitBinaryOperation(const BinaryOperation* b) override {
auto l = b->getChild("left"); auto r = b->getChild("right");
return (l ? generate(l) : "") + " " + b->op + " " + (r ? generate(r) : "");
}
std::string visitVariableReference(const VariableReference* v) override { return v->variableName; }
std::string visitIntegerLiteral(const IntegerLiteral* l) override { return std::to_string(l->value); }
std::string visitFloatLiteral(const FloatLiteral* l) override { return l->value; }
std::string visitStringLiteral(const StringLiteral* l) override { return "\"" + l->value + "\""; }
std::string visitBooleanLiteral(const BooleanLiteral* l) override { return l->value ? "true" : "false"; }
std::string visitNullLiteral(const NullLiteral*) override { return "null"; }
std::string visitIfStatement(const IfStatement* s) override {
std::ostringstream oss;
auto c = s->getChild("condition");
oss << "if (" << (c ? generate(c) : "true") << ")\n{\n";
for (const auto* st : s->getChildren("thenBranch"))
oss << " " << generate(st) << "\n";
oss << "}";
auto el = s->getChildren("elseBranch");
if (!el.empty()) {
oss << "\nelse\n{\n";
for (const auto* st : el) oss << " " << generate(st) << "\n";
oss << "}";
}
oss << "\n";
return oss.str();
}
std::string visitWhileLoop(const WhileLoop* l) override {
std::ostringstream oss;
auto c = l->getChild("condition");
oss << "while (" << (c ? generate(c) : "true") << ")\n{\n";
for (const auto* s : l->getChildren("body")) oss << " " << generate(s) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitForLoop(const ForLoop* l) override {
std::ostringstream oss;
auto iter = l->getChild("iterable");
oss << "foreach (var " << (l->iteratorName.empty() ? "item" : l->iteratorName);
oss << " in " << (iter ? generate(iter) : "Array.Empty<object>()") << ")\n{\n";
for (const auto* s : l->getChildren("body")) oss << " " << generate(s) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitExpressionStatement(const ExpressionStatement* s) override {
auto e = s->getChild("expression");
return e ? generate(e) + ";" : "";
}
std::string visitUnaryOperation(const UnaryOperation* u) override {
auto o = u->getChild("operand");
return u->op + (o ? generate(o) : "");
}
std::string visitFunctionCall(const FunctionCall* c) override {
std::ostringstream oss;
oss << c->functionName << "(";
auto args = c->getChildren("arguments");
for (size_t i = 0; i < args.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(args[i]);
}
oss << ")";
return oss.str();
}
std::string visitBlock(const Block* b) override {
std::ostringstream oss;
for (const auto* s : b->getChildren("statements")) oss << generate(s) << "\n";
return oss.str();
}
std::string visitListLiteral(const ListLiteral* l) override {
std::ostringstream oss;
oss << "new List<object> { ";
auto e = l->getChildren("elements");
for (size_t i = 0; i < e.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(e[i]);
}
oss << " }";
return oss.str();
}
std::string visitIndexAccess(const IndexAccess* a) override {
auto t = a->getChild("target"); auto i = a->getChild("index");
return (t ? generate(t) : "") + "[" + (i ? generate(i) : "") + "]";
}
std::string visitMemberAccess(const MemberAccess* a) override {
auto t = a->getChild("target");
return (t ? generate(t) : "") + "." + a->memberName;
}
std::string visitPrimitiveType(const PrimitiveType* t) override {
if (t->kind == "int") return "int";
if (t->kind == "float") return "float";
if (t->kind == "double") return "double";
if (t->kind == "string") return "string";
if (t->kind == "bool") return "bool";
if (t->kind == "void") return "void";
return t->kind;
}
std::string visitListType(const ListType* t) override {
auto e = t->getChild("elementType");
return "List<" + (e ? generate(e) : "object") + ">";
}
std::string visitSetType(const SetType* t) override {
auto e = t->getChild("elementType");
return "HashSet<" + (e ? generate(e) : "object") + ">";
}
std::string visitMapType(const MapType* t) override {
auto k = t->getChild("keyType"); auto v = t->getChild("valueType");
return "Dictionary<" + (k ? generate(k) : "object") + ", " + (v ? generate(v) : "object") + ">";
}
std::string visitTupleType(const TupleType* t) override {
std::ostringstream oss;
oss << "(";
auto types = t->getChildren("elementTypes");
for (size_t i = 0; i < types.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(types[i]);
}
oss << ")";
return oss.str();
}
std::string visitArrayType(const ArrayType* t) override {
auto e = t->getChild("elementType");
return (e ? generate(e) : "object") + "[]";
}
std::string visitOptionalType(const OptionalType* t) override {
auto i = t->getChild("innerType");
return (i ? generate(i) : "object") + "?";
}
std::string visitCustomType(const CustomType* t) override { return t->typeName; }
// --- New AST node visitors (Step 312) ---
std::string visitClassDeclaration(const ASTNode* node) override {
auto* cls = static_cast<const ClassDeclaration*>(node);
std::ostringstream oss;
auto annotations = cls->getChildren("annotations");
for (const auto* a : annotations) oss << generate(a) << "\n";
if (cls->isAbstract) oss << "abstract ";
oss << "class " << cls->name;
if (!cls->superClass.empty()) oss << " : " << cls->superClass;
oss << "\n{\n";
auto fields = cls->getChildren("fields");
for (const auto* f : fields) oss << " " << generate(f) << "\n";
if (!fields.empty() && !cls->getChildren("methods").empty()) oss << "\n";
auto methods = cls->getChildren("methods");
for (const auto* m : methods) oss << generate(m) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitInterfaceDeclaration(const ASTNode* node) override {
auto* iface = static_cast<const InterfaceDeclaration*>(node);
std::ostringstream oss;
oss << "interface " << iface->name << "\n{\n";
auto methods = iface->getChildren("methods");
for (const auto* m : methods) {
auto* meth = static_cast<const MethodDeclaration*>(m);
oss << " void " << meth->name << "();\n";
}
oss << "}\n";
return oss.str();
}
std::string visitMethodDeclaration(const ASTNode* node) override {
auto* meth = static_cast<const MethodDeclaration*>(node);
std::ostringstream oss;
auto annotations = meth->getChildren("annotations");
for (const auto* a : annotations) oss << " " << generate(a) << "\n";
oss << " ";
if (!meth->visibility.empty()) oss << meth->visibility << " ";
if (meth->isStatic) oss << "static ";
if (meth->isVirtual) oss << "virtual ";
if (meth->isOverride) oss << "override ";
std::string returnType = "void";
auto retType = meth->getChild("returnType");
if (retType) returnType = generate(retType);
oss << returnType << " " << meth->name << "(";
auto params = meth->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ")\n";
auto body = meth->getChildren("body");
if (body.empty()) {
oss << " {\n }\n";
} else {
oss << " {\n";
for (const auto* s : body) oss << " " << generate(s) << "\n";
oss << " }\n";
}
return oss.str();
}
std::string visitGenericType(const ASTNode* node) override {
auto* gen = static_cast<const GenericType*>(node);
std::ostringstream oss;
oss << gen->baseName << "<";
auto params = gen->getChildren("typeParameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ">";
return oss.str();
}
std::string visitTypeParameter(const ASTNode* node) override {
auto* tp = static_cast<const TypeParameter*>(node);
return tp->name;
}
std::string visitAsyncFunction(const ASTNode* node) override {
auto* af = static_cast<const AsyncFunction*>(node);
std::ostringstream oss;
auto annotations = af->getChildren("annotations");
for (const auto* a : annotations) oss << generate(a) << "\n";
oss << "public async Task " << af->name << "(";
auto params = af->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ")\n{\n";
auto body = af->getChildren("body");
for (const auto* s : body) oss << " " << generate(s) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitAwaitExpression(const ASTNode* node) override {
auto* aw = static_cast<const AwaitExpression*>(node);
auto* expr = aw->getChild("expression");
return "await " + (expr ? generate(expr) : "Task.CompletedTask");
}
std::string visitLambdaExpression(const ASTNode* node) override {
auto* lam = static_cast<const LambdaExpression*>(node);
std::ostringstream oss;
oss << "(";
auto params = lam->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << static_cast<const Parameter*>(params[i])->name;
}
oss << ") => ";
auto body = lam->getChildren("body");
if (body.size() == 1) {
oss << generate(body[0]);
} else if (body.empty()) {
oss << "{ }";
} else {
oss << "{ ";
for (const auto* s : body) oss << generate(s) << "; ";
oss << "}";
}
return oss.str();
}
std::string visitDecoratorAnnotation(const ASTNode* node) override {
auto* dec = static_cast<const DecoratorAnnotation*>(node);
std::ostringstream oss;
oss << "[" << dec->name;
auto args = dec->getChildren("arguments");
if (!args.empty()) {
oss << "(";
for (size_t i = 0; i < args.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(args[i]);
}
oss << ")";
}
oss << "]";
return oss.str();
}
std::string visitDerefStrategy(const DerefStrategy* a) override { return "// @deref(" + a->strategy + ")"; }
std::string visitOptimizationLock(const OptimizationLock* a) override { return "// @lock(" + a->lockedBy + ")"; }
std::string visitLangSpecific(const LangSpecific* a) override { return "// @lang_specific(" + a->language + ")"; }
std::string visitDeallocateAnnotation(const DeallocateAnnotation* a) override { return semanno(a); }
std::string visitLifetimeAnnotation(const LifetimeAnnotation* a) override { return semanno(a); }
std::string visitReclaimAnnotation(const ReclaimAnnotation* a) override { return semanno(a); }
std::string visitOwnerAnnotation(const OwnerAnnotation* a) override { return semanno(a); }
std::string visitAllocateAnnotation(const AllocateAnnotation* a) override { return semanno(a); }
std::string visitHotColdAnnotation(const HotColdAnnotation* a) override { return semanno(a); }
std::string visitInlineAnnotation(const InlineAnnotation* a) override { return semanno(a); }
std::string visitPureAnnotation(const PureAnnotation* a) override { return semanno(a); }
std::string visitConstExprAnnotation(const ConstExprAnnotation* a) override { return semanno(a); }
};

View File

@@ -0,0 +1,144 @@
#pragma once
#include "ASTNode.h"
#include "Module.h"
#include "Function.h"
#include "Variable.h"
#include "Parameter.h"
#include "Statement.h"
#include "Expression.h"
#include "Type.h"
#include "Annotation.h"
#include "ClassDeclaration.h"
#include "GenericType.h"
#include "AsyncNodes.h"
#include "../ast/Parser.h"
#include <string>
#include <sstream>
class CSharpParser {
public:
static std::unique_ptr<Module> parseCSharp(const std::string& source) {
auto module = std::make_unique<Module>();
module->id = IdGenerator::next("mod");
module->name = "parsed_csharp_module";
module->targetLanguage = "csharp";
parseTopLevel(source, module.get());
return module;
}
static ParseResult parseCSharpWithDiagnostics(const std::string& source) {
ParseResult result;
result.module = parseCSharp(source);
return result;
}
private:
static void parseTopLevel(const std::string& source, Module* module) {
std::istringstream stream(source);
std::string line;
int braceDepth = 0;
bool inFunction = false;
bool inClass = false;
std::string currentName;
bool isAsync = false;
while (std::getline(stream, line)) {
std::string trimmed = trim(line);
if (trimmed.empty() || trimmed.substr(0, 2) == "//") continue;
if (trimmed.find("using ") == 0) continue; // skip using directives
if (trimmed.find("namespace ") == 0) continue; // skip namespace
if (braceDepth <= 1) {
if (hasMethodSignature(trimmed)) {
isAsync = trimmed.find("async ") != std::string::npos;
currentName = extractMethodName(trimmed);
inFunction = true;
}
else if (trimmed.find("class ") != std::string::npos) {
currentName = extractAfterKeyword(trimmed, "class ");
inClass = true;
}
else if (trimmed.find("interface ") != std::string::npos) {
currentName = extractAfterKeyword(trimmed, "interface ");
auto* iface = new InterfaceDeclaration();
iface->id = IdGenerator::next("iface");
iface->name = currentName;
module->addChild("interfaces", iface);
}
}
for (char c : trimmed) {
if (c == '{') braceDepth++;
else if (c == '}') braceDepth--;
}
if (braceDepth <= 1 && inFunction) {
if (isAsync) {
auto* fn = new AsyncFunction();
fn->id = IdGenerator::next("fn");
fn->name = currentName;
module->addChild("functions", fn);
} else {
auto* fn = new Function();
fn->id = IdGenerator::next("fn");
fn->name = currentName;
module->addChild("functions", fn);
}
inFunction = false;
isAsync = false;
}
if (braceDepth == 0 && inClass) {
auto* cls = new ClassDeclaration();
cls->id = IdGenerator::next("cls");
cls->name = currentName;
module->addChild("classes", cls);
inClass = false;
}
}
}
static bool hasMethodSignature(const std::string& line) {
// Look for patterns like "public void Method(" or "static async Task<int> Method("
if (line.find('(') == std::string::npos) return false;
if (line.find("class ") != std::string::npos) return false;
if (line.find("if ") != std::string::npos || line.find("if(") != std::string::npos) return false;
if (line.find("while ") != std::string::npos) return false;
if (line.find("for ") != std::string::npos || line.find("foreach ") != std::string::npos) return false;
// Has visibility or return type keyword
return line.find("void ") != std::string::npos ||
line.find("int ") != std::string::npos ||
line.find("string ") != std::string::npos ||
line.find("Task") != std::string::npos ||
line.find("public ") != std::string::npos ||
line.find("private ") != std::string::npos ||
line.find("static ") != std::string::npos;
}
static std::string extractMethodName(const std::string& line) {
auto parenPos = line.find('(');
if (parenPos == std::string::npos) return "unknown";
// Walk back from '(' to find the method name
auto nameEnd = parenPos;
auto nameStart = line.rfind(' ', nameEnd - 1);
if (nameStart == std::string::npos) nameStart = 0;
else nameStart++;
return trim(line.substr(nameStart, nameEnd - nameStart));
}
static std::string extractAfterKeyword(const std::string& line, const std::string& keyword) {
auto pos = line.find(keyword);
if (pos == std::string::npos) return "unknown";
pos += keyword.size();
auto end = line.find_first_of("{: <(", pos);
if (end == std::string::npos) end = line.size();
return trim(line.substr(pos, end - pos));
}
static std::string trim(const std::string& s) {
auto start = s.find_first_not_of(" \t\r\n");
if (start == std::string::npos) return "";
auto end = s.find_last_not_of(" \t\r\n");
return s.substr(start, end - start + 1);
}
};

View File

@@ -0,0 +1,377 @@
#pragma once
#include "ProjectionGenerator.h"
#include "ClassDeclaration.h"
#include "GenericType.h"
#include "AsyncNodes.h"
#include "../SemannoAnnotationImpl.h"
class KotlinGenerator : public ProjectionGenerator, public SemannoAnnotationImpl<KotlinGenerator> {
public:
std::string commentPrefix() const { return "// "; }
std::string generate(const ASTNode* node) override {
return dispatchGenerate(this, node, "// Unknown concept: ");
}
std::string visitModule(const Module* module) override {
std::ostringstream oss;
oss << "// Module: " << module->name << "\n\n";
auto variables = module->getChildren("variables");
for (const auto* var : variables) {
oss << generate(var) << "\n";
}
if (!variables.empty()) oss << "\n";
auto functions = module->getChildren("functions");
for (size_t i = 0; i < functions.size(); ++i) {
if (i > 0) oss << "\n";
oss << generate(functions[i]);
}
return oss.str();
}
std::string visitFunction(const Function* function) override {
std::ostringstream oss;
auto annotations = function->getChildren("annotations");
for (const auto* anno : annotations) oss << generate(anno) << "\n";
oss << "fun " << function->name << "(";
auto params = function->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ")";
auto retType = function->getChild("returnType");
if (retType) oss << ": " << generate(retType);
oss << " {\n";
auto body = function->getChildren("body");
for (const auto* stmt : body) {
std::string code = generate(stmt);
oss << " " << code << "\n";
}
oss << "}\n";
return oss.str();
}
std::string visitVariable(const Variable* variable) override {
std::ostringstream oss;
oss << "val " << variable->name;
auto type = variable->getChild("type");
if (type) oss << ": " << generate(type);
auto init = variable->getChild("initializer");
if (init) oss << " = " << generate(init);
return oss.str();
}
std::string visitParameter(const Parameter* parameter) override {
std::ostringstream oss;
oss << parameter->name;
auto type = parameter->getChild("type");
if (type) oss << ": " << generate(type);
auto def = parameter->getChild("defaultValue");
if (def) oss << " = " << generate(def);
return oss.str();
}
std::string visitAssignment(const Assignment* a) override {
std::ostringstream oss;
auto target = a->getChild("target");
auto value = a->getChild("value");
if (target) oss << generate(target);
oss << " = ";
if (value) oss << generate(value);
return oss.str();
}
std::string visitReturn(const Return* r) override {
auto value = r->getChild("value");
return value ? "return " + generate(value) : "return";
}
std::string visitBinaryOperation(const BinaryOperation* b) override {
std::ostringstream oss;
auto left = b->getChild("left");
auto right = b->getChild("right");
if (left) oss << generate(left);
oss << " " << b->op << " ";
if (right) oss << generate(right);
return oss.str();
}
std::string visitVariableReference(const VariableReference* v) override { return v->variableName; }
std::string visitIntegerLiteral(const IntegerLiteral* l) override { return std::to_string(l->value); }
std::string visitFloatLiteral(const FloatLiteral* l) override { return l->value; }
std::string visitStringLiteral(const StringLiteral* l) override { return "\"" + l->value + "\""; }
std::string visitBooleanLiteral(const BooleanLiteral* l) override { return l->value ? "true" : "false"; }
std::string visitNullLiteral(const NullLiteral*) override { return "null"; }
std::string visitIfStatement(const IfStatement* s) override {
std::ostringstream oss;
auto cond = s->getChild("condition");
oss << "if (" << (cond ? generate(cond) : "true") << ") {\n";
for (const auto* stmt : s->getChildren("thenBranch"))
oss << " " << generate(stmt) << "\n";
oss << "}";
auto elseBranch = s->getChildren("elseBranch");
if (!elseBranch.empty()) {
oss << " else {\n";
for (const auto* stmt : elseBranch)
oss << " " << generate(stmt) << "\n";
oss << "}";
}
oss << "\n";
return oss.str();
}
std::string visitWhileLoop(const WhileLoop* l) override {
std::ostringstream oss;
auto cond = l->getChild("condition");
oss << "while (" << (cond ? generate(cond) : "true") << ") {\n";
for (const auto* stmt : l->getChildren("body"))
oss << " " << generate(stmt) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitForLoop(const ForLoop* l) override {
std::ostringstream oss;
auto iter = l->getChild("iterable");
oss << "for (" << (l->iteratorName.empty() ? "item" : l->iteratorName);
oss << " in " << (iter ? generate(iter) : "listOf()") << ") {\n";
for (const auto* stmt : l->getChildren("body"))
oss << " " << generate(stmt) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitExpressionStatement(const ExpressionStatement* s) override {
auto expr = s->getChild("expression");
return expr ? generate(expr) : "";
}
std::string visitUnaryOperation(const UnaryOperation* u) override {
auto op = u->getChild("operand");
return u->op + (op ? generate(op) : "");
}
std::string visitFunctionCall(const FunctionCall* c) override {
std::ostringstream oss;
oss << c->functionName << "(";
auto args = c->getChildren("arguments");
for (size_t i = 0; i < args.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(args[i]);
}
oss << ")";
return oss.str();
}
std::string visitBlock(const Block* b) override {
std::ostringstream oss;
for (const auto* s : b->getChildren("statements"))
oss << generate(s) << "\n";
return oss.str();
}
std::string visitListLiteral(const ListLiteral* l) override {
std::ostringstream oss;
oss << "listOf(";
auto elems = l->getChildren("elements");
for (size_t i = 0; i < elems.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(elems[i]);
}
oss << ")";
return oss.str();
}
std::string visitIndexAccess(const IndexAccess* a) override {
auto t = a->getChild("target");
auto i = a->getChild("index");
return (t ? generate(t) : "") + "[" + (i ? generate(i) : "") + "]";
}
std::string visitMemberAccess(const MemberAccess* a) override {
auto t = a->getChild("target");
return (t ? generate(t) : "") + "." + a->memberName;
}
std::string visitPrimitiveType(const PrimitiveType* t) override {
if (t->kind == "int") return "Int";
if (t->kind == "float" || t->kind == "double") return "Double";
if (t->kind == "string") return "String";
if (t->kind == "bool") return "Boolean";
if (t->kind == "void") return "Unit";
return t->kind;
}
std::string visitListType(const ListType* t) override {
auto el = t->getChild("elementType");
return "List<" + (el ? generate(el) : "Any") + ">";
}
std::string visitSetType(const SetType* t) override {
auto el = t->getChild("elementType");
return "Set<" + (el ? generate(el) : "Any") + ">";
}
std::string visitMapType(const MapType* t) override {
auto k = t->getChild("keyType");
auto v = t->getChild("valueType");
return "Map<" + (k ? generate(k) : "Any") + ", " + (v ? generate(v) : "Any") + ">";
}
std::string visitTupleType(const TupleType* t) override {
std::ostringstream oss;
oss << "Pair<";
auto types = t->getChildren("elementTypes");
for (size_t i = 0; i < types.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(types[i]);
}
oss << ">";
return oss.str();
}
std::string visitArrayType(const ArrayType* t) override {
auto el = t->getChild("elementType");
return "Array<" + (el ? generate(el) : "Any") + ">";
}
std::string visitOptionalType(const OptionalType* t) override {
auto inner = t->getChild("innerType");
return (inner ? generate(inner) : "Any") + "?";
}
std::string visitCustomType(const CustomType* t) override { return t->typeName; }
// --- New AST node visitors (Step 310) ---
std::string visitClassDeclaration(const ASTNode* node) override {
auto* cls = static_cast<const ClassDeclaration*>(node);
std::ostringstream oss;
auto annotations = cls->getChildren("annotations");
for (const auto* a : annotations) oss << generate(a) << "\n";
if (cls->isAbstract) oss << "abstract ";
oss << "class " << cls->name;
if (!cls->superClass.empty()) oss << " : " << cls->superClass << "()";
oss << " {\n";
auto fields = cls->getChildren("fields");
for (const auto* f : fields) oss << " " << generate(f) << "\n";
auto methods = cls->getChildren("methods");
for (const auto* m : methods) oss << generate(m);
oss << "}\n";
return oss.str();
}
std::string visitInterfaceDeclaration(const ASTNode* node) override {
auto* iface = static_cast<const InterfaceDeclaration*>(node);
std::ostringstream oss;
oss << "interface " << iface->name << " {\n";
auto methods = iface->getChildren("methods");
for (const auto* m : methods) {
auto* meth = static_cast<const MethodDeclaration*>(m);
oss << " fun " << meth->name << "()\n";
}
oss << "}\n";
return oss.str();
}
std::string visitMethodDeclaration(const ASTNode* node) override {
auto* meth = static_cast<const MethodDeclaration*>(node);
std::ostringstream oss;
auto annotations = meth->getChildren("annotations");
for (const auto* a : annotations) oss << " " << generate(a) << "\n";
oss << " ";
if (meth->isOverride) oss << "override ";
oss << "fun " << meth->name << "(";
auto params = meth->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ")";
auto retType = meth->getChild("returnType");
if (retType) oss << ": " << generate(retType);
auto body = meth->getChildren("body");
if (body.empty()) {
oss << " {}\n";
} else {
oss << " {\n";
for (const auto* s : body) oss << " " << generate(s) << "\n";
oss << " }\n";
}
return oss.str();
}
std::string visitGenericType(const ASTNode* node) override {
auto* gen = static_cast<const GenericType*>(node);
std::ostringstream oss;
oss << gen->baseName << "<";
auto params = gen->getChildren("typeParameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ">";
return oss.str();
}
std::string visitTypeParameter(const ASTNode* node) override {
auto* tp = static_cast<const TypeParameter*>(node);
if (!tp->constraint.empty()) return tp->name + " : " + tp->constraint;
return tp->name;
}
std::string visitAsyncFunction(const ASTNode* node) override {
auto* af = static_cast<const AsyncFunction*>(node);
std::ostringstream oss;
auto annotations = af->getChildren("annotations");
for (const auto* a : annotations) oss << generate(a) << "\n";
oss << "suspend fun " << af->name << "(";
auto params = af->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(params[i]);
}
oss << ")";
auto retType = af->getChild("returnType");
if (retType) oss << ": " << generate(retType);
oss << " {\n";
auto body = af->getChildren("body");
for (const auto* s : body) oss << " " << generate(s) << "\n";
oss << "}\n";
return oss.str();
}
std::string visitAwaitExpression(const ASTNode* node) override {
auto* aw = static_cast<const AwaitExpression*>(node);
auto* expr = aw->getChild("expression");
return expr ? generate(expr) : "/* missing */";
}
std::string visitLambdaExpression(const ASTNode* node) override {
auto* lam = static_cast<const LambdaExpression*>(node);
std::ostringstream oss;
oss << "{ ";
auto params = lam->getChildren("parameters");
if (!params.empty()) {
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << static_cast<const Parameter*>(params[i])->name;
}
oss << " -> ";
}
auto body = lam->getChildren("body");
for (size_t i = 0; i < body.size(); ++i) {
if (i > 0) oss << "; ";
oss << generate(body[i]);
}
oss << " }";
return oss.str();
}
std::string visitDecoratorAnnotation(const ASTNode* node) override {
auto* dec = static_cast<const DecoratorAnnotation*>(node);
return "@" + dec->name;
}
// Legacy annotation visitors
std::string visitDerefStrategy(const DerefStrategy* a) override { return "// @deref(" + a->strategy + ")"; }
std::string visitOptimizationLock(const OptimizationLock* a) override { return "// @lock(" + a->lockedBy + ")"; }
std::string visitLangSpecific(const LangSpecific* a) override { return "// @lang_specific(" + a->language + ")"; }
std::string visitDeallocateAnnotation(const DeallocateAnnotation* a) override { return semanno(a); }
std::string visitLifetimeAnnotation(const LifetimeAnnotation* a) override { return semanno(a); }
std::string visitReclaimAnnotation(const ReclaimAnnotation* a) override { return semanno(a); }
std::string visitOwnerAnnotation(const OwnerAnnotation* a) override { return semanno(a); }
std::string visitAllocateAnnotation(const AllocateAnnotation* a) override { return semanno(a); }
std::string visitHotColdAnnotation(const HotColdAnnotation* a) override { return semanno(a); }
std::string visitInlineAnnotation(const InlineAnnotation* a) override { return semanno(a); }
std::string visitPureAnnotation(const PureAnnotation* a) override { return semanno(a); }
std::string visitConstExprAnnotation(const ConstExprAnnotation* a) override { return semanno(a); }
};

View File

@@ -0,0 +1,154 @@
#pragma once
#include "ASTNode.h"
#include "Module.h"
#include "Function.h"
#include "Variable.h"
#include "Parameter.h"
#include "Statement.h"
#include "Expression.h"
#include "Type.h"
#include "Annotation.h"
#include "ClassDeclaration.h"
#include "GenericType.h"
#include "AsyncNodes.h"
#include "../ast/Parser.h"
#include <string>
#include <sstream>
#include <regex>
class KotlinParser {
public:
static std::unique_ptr<Module> parseKotlin(const std::string& source) {
auto module = std::make_unique<Module>();
module->id = IdGenerator::next("mod");
module->name = "parsed_kotlin_module";
module->targetLanguage = "kotlin";
parseTopLevel(source, module.get());
return module;
}
static ParseResult parseKotlinWithDiagnostics(const std::string& source) {
ParseResult result;
result.module = parseKotlin(source);
return result;
}
private:
static void parseTopLevel(const std::string& source, Module* module) {
std::istringstream stream(source);
std::string line;
std::string buffer;
int braceDepth = 0;
bool inFunction = false;
bool inClass = false;
std::string currentName;
bool isSuspend = false;
bool isDataClass = false;
while (std::getline(stream, line)) {
std::string trimmed = trim(line);
// Skip empty lines and comments
if (trimmed.empty() || trimmed.substr(0, 2) == "//") continue;
if (braceDepth == 0) {
// Top-level declarations
if (trimmed.find("fun ") != std::string::npos ||
trimmed.find("suspend fun ") != std::string::npos) {
isSuspend = trimmed.find("suspend ") != std::string::npos;
currentName = extractFunName(trimmed);
inFunction = true;
buffer = trimmed;
}
else if (trimmed.find("class ") != std::string::npos ||
trimmed.find("data class ") != std::string::npos) {
isDataClass = trimmed.find("data class ") != std::string::npos;
currentName = extractClassName(trimmed);
inClass = true;
buffer = trimmed;
}
else if (trimmed.find("val ") == 0 || trimmed.find("var ") == 0) {
auto* var = new Variable();
var->id = IdGenerator::next("var");
var->name = extractValName(trimmed);
module->addChild("variables", var);
continue;
}
}
// Count braces
for (char c : trimmed) {
if (c == '{') braceDepth++;
else if (c == '}') braceDepth--;
}
if (braceDepth > 0 || (inFunction || inClass)) {
if (buffer != trimmed) buffer += "\n" + trimmed;
}
if (braceDepth == 0 && (inFunction || inClass)) {
if (inFunction) {
if (isSuspend) {
auto* fn = new AsyncFunction();
fn->id = IdGenerator::next("fn");
fn->name = currentName;
fn->isAsync = true;
module->addChild("functions", fn);
} else {
auto* fn = new Function();
fn->id = IdGenerator::next("fn");
fn->name = currentName;
module->addChild("functions", fn);
}
inFunction = false;
isSuspend = false;
}
if (inClass) {
auto* cls = new ClassDeclaration();
cls->id = IdGenerator::next("cls");
cls->name = currentName;
module->addChild("classes", cls);
inClass = false;
isDataClass = false;
}
buffer.clear();
}
}
}
static std::string extractFunName(const std::string& line) {
auto pos = line.find("fun ");
if (pos == std::string::npos) return "unknown";
pos += 4;
auto end = line.find('(', pos);
if (end == std::string::npos) end = line.size();
return trim(line.substr(pos, end - pos));
}
static std::string extractClassName(const std::string& line) {
std::string search = "class ";
auto pos = line.find(search);
if (pos == std::string::npos) return "unknown";
pos += search.size();
auto end = line.find_first_of("({: ", pos);
if (end == std::string::npos) end = line.size();
return trim(line.substr(pos, end - pos));
}
static std::string extractValName(const std::string& line) {
auto pos = line.find(' ');
if (pos == std::string::npos) return "unknown";
pos++;
auto end = line.find_first_of(":= ", pos);
if (end == std::string::npos) end = line.size();
return trim(line.substr(pos, end - pos));
}
static std::string trim(const std::string& s) {
auto start = s.find_first_not_of(" \t\r\n");
if (start == std::string::npos) return "";
auto end = s.find_last_not_of(" \t\r\n");
return s.substr(start, end - start + 1);
}
};

View File

@@ -294,6 +294,18 @@ std::string dispatchGenerate(Gen* gen, const ASTNode* node, const std::string& u
} else if (node->conceptType == "DecisionAnnotation") {
return gen->visitDecisionAnnotation(static_cast<const DecisionAnnotation*>(node));
}
// Subject 9: Workflow Routing
else if (node->conceptType == "ContextWidthAnnotation") {
return gen->visitContextWidthAnnotation(static_cast<const ContextWidthAnnotation*>(node));
} else if (node->conceptType == "ReviewAnnotation") {
return gen->visitReviewAnnotation(static_cast<const ReviewAnnotation*>(node));
} else if (node->conceptType == "AutomatabilityAnnotation") {
return gen->visitAutomatabilityAnnotation(static_cast<const AutomatabilityAnnotation*>(node));
} else if (node->conceptType == "PriorityAnnotation") {
return gen->visitPriorityAnnotation(static_cast<const PriorityAnnotation*>(node));
} else if (node->conceptType == "ImplementationStatusAnnotation") {
return gen->visitImplementationStatusAnnotation(static_cast<const ImplementationStatusAnnotation*>(node));
}
// Semantic Core
else if (node->conceptType == "IntentAnnotation") {
return gen->visitIntentAnnotation(static_cast<const IntentAnnotation*>(node));

View File

@@ -377,6 +377,8 @@ inline json propertiesToJson(const ASTNode* node) {
auto* n = static_cast<const AmbiguityAnnotation*>(node);
if (!n->intent.empty()) props["intent"] = n->intent;
if (!n->options.empty()) props["options"] = n->options;
if (!n->level.empty()) props["level"] = n->level;
if (!n->description.empty()) props["description"] = n->description;
}
else if (ct == "CandidateAnnotation") {
auto* n = static_cast<const CandidateAnnotation*>(node);
@@ -400,6 +402,32 @@ inline json propertiesToJson(const ASTNode* node) {
if (!n->author.empty()) props["author"] = n->author;
if (!n->reason.empty()) props["reason"] = n->reason;
}
// Subject 9: Workflow Routing (Step 315)
else if (ct == "ContextWidthAnnotation") {
auto* n = static_cast<const ContextWidthAnnotation*>(node);
if (!n->width.empty()) props["width"] = n->width;
}
else if (ct == "ReviewAnnotation") {
auto* n = static_cast<const ReviewAnnotation*>(node);
props["required"] = n->required;
if (!n->reviewer.empty()) props["reviewer"] = n->reviewer;
if (!n->reason.empty()) props["reason"] = n->reason;
}
else if (ct == "AutomatabilityAnnotation") {
auto* n = static_cast<const AutomatabilityAnnotation*>(node);
if (!n->strategy.empty()) props["strategy"] = n->strategy;
if (n->confidence > 0.0) props["confidence"] = n->confidence;
}
else if (ct == "PriorityAnnotation") {
auto* n = static_cast<const PriorityAnnotation*>(node);
if (!n->level.empty()) props["level"] = n->level;
if (!n->blockedBy.empty()) props["blockedBy"] = n->blockedBy;
}
else if (ct == "ImplementationStatusAnnotation") {
auto* n = static_cast<const ImplementationStatusAnnotation*>(node);
if (!n->status.empty()) props["status"] = n->status;
if (!n->assignee.empty()) props["assignee"] = n->assignee;
}
// Host Boundary (Step 288)
else if (ct == "HostCall") {
auto* n = static_cast<const HostCall*>(node);
@@ -604,6 +632,12 @@ inline ASTNode* createNode(const std::string& conceptName) {
if (conceptName == "TradeoffAnnotation") return new TradeoffAnnotation();
if (conceptName == "ChoiceAnnotation") return new ChoiceAnnotation();
if (conceptName == "DecisionAnnotation") return new DecisionAnnotation();
// Subject 9: Workflow Routing (Step 315)
if (conceptName == "ContextWidthAnnotation") return new ContextWidthAnnotation();
if (conceptName == "ReviewAnnotation") return new ReviewAnnotation();
if (conceptName == "AutomatabilityAnnotation") return new AutomatabilityAnnotation();
if (conceptName == "PriorityAnnotation") return new PriorityAnnotation();
if (conceptName == "ImplementationStatusAnnotation") return new ImplementationStatusAnnotation();
// Host Boundary (Step 288)
if (conceptName == "HostCall") return new HostCall();
if (conceptName == "ScheduleTask") return new ScheduleTask();
@@ -994,6 +1028,8 @@ inline void setPropertiesFromJson(ASTNode* node, const json& props) {
for (const auto& o : props["options"])
if (o.is_string()) n->options.push_back(o.get<std::string>());
}
if (props.contains("level")) n->level = props["level"].get<std::string>();
if (props.contains("description")) n->description = props["description"].get<std::string>();
}
else if (ct == "CandidateAnnotation") {
auto* n = static_cast<CandidateAnnotation*>(node);
@@ -1025,6 +1061,36 @@ inline void setPropertiesFromJson(ASTNode* node, const json& props) {
if (props.contains("author")) n->author = props["author"].get<std::string>();
if (props.contains("reason")) n->reason = props["reason"].get<std::string>();
}
// Subject 9: Workflow Routing (Step 315)
else if (ct == "ContextWidthAnnotation") {
auto* n = static_cast<ContextWidthAnnotation*>(node);
if (props.contains("width")) n->width = props["width"].get<std::string>();
}
else if (ct == "ReviewAnnotation") {
auto* n = static_cast<ReviewAnnotation*>(node);
if (props.contains("required")) n->required = props["required"].get<bool>();
if (props.contains("reviewer")) n->reviewer = props["reviewer"].get<std::string>();
if (props.contains("reason")) n->reason = props["reason"].get<std::string>();
}
else if (ct == "AutomatabilityAnnotation") {
auto* n = static_cast<AutomatabilityAnnotation*>(node);
if (props.contains("strategy")) n->strategy = props["strategy"].get<std::string>();
if (props.contains("confidence")) n->confidence = props["confidence"].get<double>();
}
else if (ct == "PriorityAnnotation") {
auto* n = static_cast<PriorityAnnotation*>(node);
if (props.contains("level")) n->level = props["level"].get<std::string>();
if (props.contains("blockedBy") && props["blockedBy"].is_array()) {
n->blockedBy.clear();
for (const auto& b : props["blockedBy"])
if (b.is_string()) n->blockedBy.push_back(b.get<std::string>());
}
}
else if (ct == "ImplementationStatusAnnotation") {
auto* n = static_cast<ImplementationStatusAnnotation*>(node);
if (props.contains("status")) n->status = props["status"].get<std::string>();
if (props.contains("assignee")) n->assignee = props["assignee"].get<std::string>();
}
// Host Boundary (Step 288)
else if (ct == "HostCall") {
auto* n = static_cast<HostCall*>(node);