Step 308: All 8 generators produce language-appropriate output for 9 new AST node types (8/8 tests)
Phase 11c complete (80/80 tests). Added visitor implementations for ClassDeclaration, InterfaceDeclaration, MethodDeclaration, GenericType, TypeParameter, AsyncFunction, AwaitExpression, LambdaExpression, and DecoratorAnnotation to Rust, Go, Java, JavaScript/TypeScript, and Elisp generators. Each generator emits idiomatic syntax: Rust struct+impl/trait/async fn/.await/|closure|, Go type struct/interface/func receiver/<-channel/func(), Java class extends/interface/CompletableFuture/(x)->/@annotation, JS class extends/async function/await/(x)=>/class, Elisp cl-defstruct/cl-defgeneric/cl-defmethod/(lambda). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include "ProjectionGenerator.h"
|
||||
#include "Import.h"
|
||||
#include "ClassDeclaration.h"
|
||||
#include "GenericType.h"
|
||||
#include "AsyncNodes.h"
|
||||
#include "../SemannoAnnotationImpl.h"
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
@@ -446,6 +449,133 @@ public:
|
||||
return type->typeName;
|
||||
}
|
||||
|
||||
// --- New AST node visitors (Step 308) ---
|
||||
|
||||
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";
|
||||
oss << "class " << cls->name;
|
||||
if (!cls->superClass.empty()) oss << " extends " << 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 << "class " << iface->name << " {\n";
|
||||
auto methods = iface->getChildren("methods");
|
||||
for (const auto* m : methods) {
|
||||
auto* meth = static_cast<const MethodDeclaration*>(m);
|
||||
oss << " " << meth->name << "() { throw new Error('not implemented'); }\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->isStatic) oss << "static ";
|
||||
oss << meth->name << "(";
|
||||
emitParameters(oss, meth->getChildren("parameters"));
|
||||
oss << ")";
|
||||
if (includeTypes_) {
|
||||
auto retType = meth->getChild("returnType");
|
||||
if (retType) oss << ": " << generate(retType);
|
||||
}
|
||||
auto body = meth->getChildren("body");
|
||||
if (body.empty()) {
|
||||
oss << " {}\n";
|
||||
} else {
|
||||
oss << " {\n";
|
||||
emitBody(oss, body, " ");
|
||||
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 << "async function " << af->name << "(";
|
||||
emitParameters(oss, af->getChildren("parameters"));
|
||||
oss << ")";
|
||||
if (includeTypes_) {
|
||||
auto retType = af->getChild("returnType");
|
||||
if (retType) oss << ": Promise<" << generate(retType) << ">";
|
||||
}
|
||||
oss << " {\n";
|
||||
emitBody(oss, af->getChildren("body"), " ");
|
||||
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) : "undefined");
|
||||
}
|
||||
|
||||
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);
|
||||
return "// @" + dec->name;
|
||||
}
|
||||
|
||||
std::string visitDerefStrategy(const DerefStrategy* annotation) override {
|
||||
return "// @deref(" + annotation->strategy + ")";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user