Step 307: Go + C++ + Elisp parser deepening — struct/interface, class, template, lambda (12/12 tests)
Go: struct → ClassDeclaration, interface → InterfaceDeclaration, method receiver → MethodDeclaration, func literal → LambdaExpression, expression_list unwrapping C++: class/struct → ClassDeclaration, template → TypeParameter, methods → MethodDeclaration, lambda_expression → LambdaExpression Elisp: (lambda ...) special_form → LambdaExpression Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1838,4 +1838,13 @@ target_link_libraries(step306_test PRIVATE
|
|||||||
tree_sitter_typescript
|
tree_sitter_typescript
|
||||||
tree_sitter_rust)
|
tree_sitter_rust)
|
||||||
|
|
||||||
|
# Step 307: Go + C++ + Elisp Parser Deepening
|
||||||
|
add_executable(step307_test tests/step307_test.cpp)
|
||||||
|
target_include_directories(step307_test PRIVATE src)
|
||||||
|
target_link_libraries(step307_test PRIVATE
|
||||||
|
unofficial::tree-sitter::tree-sitter
|
||||||
|
tree_sitter_go
|
||||||
|
tree_sitter_cpp
|
||||||
|
tree_sitter_elisp)
|
||||||
|
|
||||||
# Step 12: Dear ImGui shell scaffolding created (main.cpp exists but not built due to dependencies)
|
# Step 12: Dear ImGui shell scaffolding created (main.cpp exists but not built due to dependencies)
|
||||||
|
|||||||
@@ -55,6 +55,199 @@ private:
|
|||||||
if (type == "function_definition") {
|
if (type == "function_definition") {
|
||||||
auto* fn = convertCppFunction(child, source);
|
auto* fn = convertCppFunction(child, source);
|
||||||
if (fn) module->addChild("functions", fn);
|
if (fn) module->addChild("functions", fn);
|
||||||
|
} else if (type == "class_specifier" || type == "struct_specifier") {
|
||||||
|
convertCppClassOrStruct(child, source, module, type == "struct_specifier");
|
||||||
|
} else if (type == "template_declaration") {
|
||||||
|
convertCppTemplateDecl(child, source, module);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void convertCppClassOrStruct(TSNode node, const std::string& source,
|
||||||
|
Module* module, bool isStruct) {
|
||||||
|
TSNode nameNode = childByFieldName(node, "name");
|
||||||
|
std::string className;
|
||||||
|
if (!ts_node_is_null(nameNode)) {
|
||||||
|
className = nodeText(nameNode, source);
|
||||||
|
}
|
||||||
|
if (className.empty()) return;
|
||||||
|
|
||||||
|
auto* cls = new ClassDeclaration(IdGenerator::next("cls"), className);
|
||||||
|
applySpan(cls, node);
|
||||||
|
|
||||||
|
// Check for base class
|
||||||
|
TSNode baseClause = childByFieldName(node, "base_clause");
|
||||||
|
if (ts_node_is_null(baseClause)) {
|
||||||
|
// tree-sitter-cpp might use different field names — search children
|
||||||
|
uint32_t nc = ts_node_named_child_count(node);
|
||||||
|
for (uint32_t i = 0; i < nc; ++i) {
|
||||||
|
TSNode ch = ts_node_named_child(node, i);
|
||||||
|
if (nodeType(ch) == "base_class_clause") {
|
||||||
|
baseClause = ch;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!ts_node_is_null(baseClause)) {
|
||||||
|
uint32_t bc = ts_node_named_child_count(baseClause);
|
||||||
|
for (uint32_t i = 0; i < bc; ++i) {
|
||||||
|
TSNode base = ts_node_named_child(baseClause, i);
|
||||||
|
std::string baseText = nodeText(base, source);
|
||||||
|
// Strip access specifier prefix
|
||||||
|
if (baseText.find("public ") == 0) baseText = baseText.substr(7);
|
||||||
|
else if (baseText.find("private ") == 0) baseText = baseText.substr(8);
|
||||||
|
else if (baseText.find("protected ") == 0) baseText = baseText.substr(10);
|
||||||
|
if (!baseText.empty() && cls->superClass.empty()) {
|
||||||
|
cls->superClass = baseText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process class body
|
||||||
|
TSNode bodyNode = childByFieldName(node, "body");
|
||||||
|
if (!ts_node_is_null(bodyNode)) {
|
||||||
|
std::string currentVisibility = isStruct ? "public" : "private";
|
||||||
|
uint32_t bc = ts_node_named_child_count(bodyNode);
|
||||||
|
for (uint32_t i = 0; i < bc; ++i) {
|
||||||
|
TSNode member = ts_node_named_child(bodyNode, i);
|
||||||
|
std::string memberType = nodeType(member);
|
||||||
|
if (memberType == "access_specifier") {
|
||||||
|
std::string specText = nodeText(member, source);
|
||||||
|
if (specText.find("public") != std::string::npos) currentVisibility = "public";
|
||||||
|
else if (specText.find("private") != std::string::npos) currentVisibility = "private";
|
||||||
|
else if (specText.find("protected") != std::string::npos) currentVisibility = "protected";
|
||||||
|
} else if (memberType == "function_definition") {
|
||||||
|
auto* meth = convertCppMethodDecl(member, source, className, currentVisibility);
|
||||||
|
if (meth) cls->addChild("methods", meth);
|
||||||
|
} else if (memberType == "declaration") {
|
||||||
|
// Could be a field declaration
|
||||||
|
TSNode declNode = childByFieldName(member, "declarator");
|
||||||
|
if (!ts_node_is_null(declNode)) {
|
||||||
|
std::string fieldName = nodeText(declNode, source);
|
||||||
|
auto* var = new Variable(IdGenerator::next("var"), fieldName);
|
||||||
|
applySpan(var, member);
|
||||||
|
cls->addChild("fields", var);
|
||||||
|
} else {
|
||||||
|
// Try to find field names from named children
|
||||||
|
uint32_t dc = ts_node_named_child_count(member);
|
||||||
|
for (uint32_t d = 0; d < dc; ++d) {
|
||||||
|
TSNode dchild = ts_node_named_child(member, d);
|
||||||
|
std::string dtype = nodeType(dchild);
|
||||||
|
if (dtype == "field_identifier" || dtype == "identifier") {
|
||||||
|
// Skip type specifiers — just get the last identifier-like thing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (memberType == "field_declaration") {
|
||||||
|
TSNode declNode = childByFieldName(member, "declarator");
|
||||||
|
if (!ts_node_is_null(declNode)) {
|
||||||
|
std::string fieldName = nodeText(declNode, source);
|
||||||
|
auto* var = new Variable(IdGenerator::next("var"), fieldName);
|
||||||
|
applySpan(var, member);
|
||||||
|
cls->addChild("fields", var);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module->addChild("classes", cls);
|
||||||
|
}
|
||||||
|
|
||||||
|
static MethodDeclaration* convertCppMethodDecl(TSNode node, const std::string& source,
|
||||||
|
const std::string& className,
|
||||||
|
const std::string& visibility) {
|
||||||
|
auto* meth = new MethodDeclaration();
|
||||||
|
meth->id = IdGenerator::next("meth");
|
||||||
|
meth->className = className;
|
||||||
|
meth->visibility = visibility;
|
||||||
|
applySpan(meth, node);
|
||||||
|
|
||||||
|
// Check for virtual keyword
|
||||||
|
TSNode typeNode = childByFieldName(node, "type");
|
||||||
|
if (!ts_node_is_null(typeNode)) {
|
||||||
|
std::string typeText = nodeText(typeNode, source);
|
||||||
|
auto* retType = new PrimitiveType(IdGenerator::next("type"), typeText);
|
||||||
|
meth->setChild("returnType", retType);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check full text for virtual/static keywords
|
||||||
|
std::string fullText = nodeText(node, source);
|
||||||
|
if (fullText.find("virtual ") != std::string::npos) meth->isVirtual = true;
|
||||||
|
if (fullText.find("static ") != std::string::npos) meth->isStatic = true;
|
||||||
|
if (fullText.find("override") != std::string::npos) meth->isOverride = true;
|
||||||
|
|
||||||
|
// Declarator
|
||||||
|
TSNode declaratorNode = childByFieldName(node, "declarator");
|
||||||
|
if (!ts_node_is_null(declaratorNode)) {
|
||||||
|
extractCppFunctionName(declaratorNode, source, meth);
|
||||||
|
extractCppParameters(declaratorNode, source, meth);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body
|
||||||
|
TSNode bodyNode = childByFieldName(node, "body");
|
||||||
|
if (!ts_node_is_null(bodyNode)) {
|
||||||
|
convertCppBody(bodyNode, source, meth);
|
||||||
|
}
|
||||||
|
|
||||||
|
return meth;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void convertCppTemplateDecl(TSNode node, const std::string& source, Module* module) {
|
||||||
|
// template_declaration has template_parameter_list and a child declaration
|
||||||
|
TSNode paramsNode = childByFieldName(node, "parameters");
|
||||||
|
std::vector<std::pair<std::string, std::string>> typeParams; // name, constraint
|
||||||
|
|
||||||
|
if (!ts_node_is_null(paramsNode)) {
|
||||||
|
uint32_t pc = ts_node_named_child_count(paramsNode);
|
||||||
|
for (uint32_t p = 0; p < pc; ++p) {
|
||||||
|
TSNode param = ts_node_named_child(paramsNode, p);
|
||||||
|
std::string paramType = nodeType(param);
|
||||||
|
if (paramType == "type_parameter_declaration" ||
|
||||||
|
paramType == "template_type_parameter") {
|
||||||
|
TSNode pname = childByFieldName(param, "name");
|
||||||
|
if (!ts_node_is_null(pname)) {
|
||||||
|
typeParams.push_back({nodeText(pname, source), ""});
|
||||||
|
} else {
|
||||||
|
// Fallback: get all text
|
||||||
|
std::string txt = nodeText(param, source);
|
||||||
|
// Extract name from "typename T" or "class T"
|
||||||
|
size_t sp = txt.rfind(' ');
|
||||||
|
if (sp != std::string::npos) {
|
||||||
|
typeParams.push_back({txt.substr(sp + 1), ""});
|
||||||
|
} else {
|
||||||
|
typeParams.push_back({txt, ""});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the inner declaration (class_specifier, struct_specifier, function_definition)
|
||||||
|
uint32_t nc = ts_node_named_child_count(node);
|
||||||
|
for (uint32_t i = 0; i < nc; ++i) {
|
||||||
|
TSNode child = ts_node_named_child(node, i);
|
||||||
|
std::string childType = nodeType(child);
|
||||||
|
if (childType == "class_specifier" || childType == "struct_specifier") {
|
||||||
|
convertCppClassOrStruct(child, source, module, childType == "struct_specifier");
|
||||||
|
// Attach TypeParameters to the last class
|
||||||
|
auto& classes = module->getChildren("classes");
|
||||||
|
if (!classes.empty()) {
|
||||||
|
auto* cls = dynamic_cast<ClassDeclaration*>(classes.back());
|
||||||
|
if (cls) {
|
||||||
|
for (auto& [tpName, tpConstraint] : typeParams) {
|
||||||
|
auto* tp = new TypeParameter(IdGenerator::next("tp"), tpName);
|
||||||
|
tp->constraint = tpConstraint;
|
||||||
|
cls->addChild("typeParameters", tp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else if (childType == "function_definition") {
|
||||||
|
auto* fn = convertCppFunction(child, source);
|
||||||
|
if (fn) module->addChild("functions", fn);
|
||||||
|
return;
|
||||||
|
} else if (childType == "declaration") {
|
||||||
|
// Template function declaration without body — skip for now
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,6 +404,9 @@ private:
|
|||||||
auto* exprStmt = new ExpressionStatement();
|
auto* exprStmt = new ExpressionStatement();
|
||||||
exprStmt->id = IdGenerator::next("exprstmt");
|
exprStmt->id = IdGenerator::next("exprstmt");
|
||||||
applySpan(exprStmt, node);
|
applySpan(exprStmt, node);
|
||||||
|
// Scan for lambda expressions inside declarations
|
||||||
|
ASTNode* lambdaNode = findCppLambdaInSubtree(node, source);
|
||||||
|
if (lambdaNode) exprStmt->setChild("expression", lambdaNode);
|
||||||
return exprStmt;
|
return exprStmt;
|
||||||
} else if (type == "if_statement") {
|
} else if (type == "if_statement") {
|
||||||
auto* ifStmt = new IfStatement();
|
auto* ifStmt = new IfStatement();
|
||||||
@@ -221,6 +417,68 @@ private:
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recursively scan for lambda_expression nodes in a subtree
|
||||||
|
static ASTNode* findCppLambdaInSubtree(TSNode node, const std::string& source) {
|
||||||
|
std::string type = nodeType(node);
|
||||||
|
if (type == "lambda_expression") {
|
||||||
|
return convertCppLambdaExpression(node, source);
|
||||||
|
}
|
||||||
|
uint32_t count = ts_node_named_child_count(node);
|
||||||
|
for (uint32_t i = 0; i < count; ++i) {
|
||||||
|
ASTNode* found = findCppLambdaInSubtree(ts_node_named_child(node, i), source);
|
||||||
|
if (found) return found;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static LambdaExpression* convertCppLambdaExpression(TSNode node, const std::string& source) {
|
||||||
|
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
|
||||||
|
applySpan(lambda, node);
|
||||||
|
|
||||||
|
// Capture list
|
||||||
|
TSNode captureNode = childByFieldName(node, "captures");
|
||||||
|
if (!ts_node_is_null(captureNode)) {
|
||||||
|
uint32_t cc = ts_node_named_child_count(captureNode);
|
||||||
|
for (uint32_t c = 0; c < cc; ++c) {
|
||||||
|
TSNode cap = ts_node_named_child(captureNode, c);
|
||||||
|
std::string capText = nodeText(cap, source);
|
||||||
|
if (!capText.empty()) lambda->captureList.push_back(capText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
TSNode declNode = childByFieldName(node, "declarator");
|
||||||
|
if (!ts_node_is_null(declNode)) {
|
||||||
|
TSNode paramsNode = childByFieldName(declNode, "parameters");
|
||||||
|
if (!ts_node_is_null(paramsNode)) {
|
||||||
|
uint32_t pc = ts_node_named_child_count(paramsNode);
|
||||||
|
for (uint32_t p = 0; p < pc; ++p) {
|
||||||
|
TSNode paramChild = ts_node_named_child(paramsNode, p);
|
||||||
|
if (nodeType(paramChild) == "parameter_declaration") {
|
||||||
|
TSNode pnameNode = childByFieldName(paramChild, "declarator");
|
||||||
|
if (!ts_node_is_null(pnameNode)) {
|
||||||
|
auto* param = new Parameter(IdGenerator::next("param"),
|
||||||
|
nodeText(pnameNode, source));
|
||||||
|
applySpan(param, paramChild);
|
||||||
|
lambda->addChild("parameters", param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body
|
||||||
|
TSNode bodyNode = childByFieldName(node, "body");
|
||||||
|
if (!ts_node_is_null(bodyNode)) {
|
||||||
|
uint32_t bc = ts_node_named_child_count(bodyNode);
|
||||||
|
for (uint32_t b = 0; b < bc; ++b) {
|
||||||
|
ASTNode* stmt = convertCppStatement(ts_node_named_child(bodyNode, b), source);
|
||||||
|
if (stmt) lambda->addChild("body", stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lambda;
|
||||||
|
}
|
||||||
|
|
||||||
static ASTNode* convertCppExpression(TSNode node, const std::string& source) {
|
static ASTNode* convertCppExpression(TSNode node, const std::string& source) {
|
||||||
std::string type = nodeType(node);
|
std::string type = nodeType(node);
|
||||||
if (type == "binary_expression") {
|
if (type == "binary_expression") {
|
||||||
@@ -260,6 +518,8 @@ private:
|
|||||||
} else if (type == "parenthesized_expression") {
|
} else if (type == "parenthesized_expression") {
|
||||||
uint32_t count = ts_node_named_child_count(node);
|
uint32_t count = ts_node_named_child_count(node);
|
||||||
if (count > 0) return convertCppExpression(ts_node_named_child(node, 0), source);
|
if (count > 0) return convertCppExpression(ts_node_named_child(node, 0), source);
|
||||||
|
} else if (type == "lambda_expression") {
|
||||||
|
return convertCppLambdaExpression(node, source);
|
||||||
}
|
}
|
||||||
// Fallback
|
// Fallback
|
||||||
std::string text = nodeText(node, source);
|
std::string text = nodeText(node, source);
|
||||||
|
|||||||
@@ -314,8 +314,20 @@ private:
|
|||||||
if (right) binOp->setChild("right", right);
|
if (right) binOp->setChild("right", right);
|
||||||
return binOp;
|
return binOp;
|
||||||
}
|
}
|
||||||
|
// Check for (lambda (args) body)
|
||||||
|
if (firstText == "lambda") {
|
||||||
|
return convertElispLambda(node, source);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Generic list — could be a function call
|
// Check for (lambda (args) body) with 2 children
|
||||||
|
if (count >= 2) {
|
||||||
|
TSNode firstChild = ts_node_named_child(node, 0);
|
||||||
|
std::string firstText = nodeText(firstChild, source);
|
||||||
|
if (firstText == "lambda") {
|
||||||
|
return convertElispLambda(node, source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Generic list — could be a function call
|
||||||
if (count >= 1) {
|
if (count >= 1) {
|
||||||
auto* call = new FunctionCall();
|
auto* call = new FunctionCall();
|
||||||
call->id = IdGenerator::next("call");
|
call->id = IdGenerator::next("call");
|
||||||
@@ -362,8 +374,27 @@ private:
|
|||||||
uint32_t count = ts_node_named_child_count(node);
|
uint32_t count = ts_node_named_child_count(node);
|
||||||
if (count < 1) return nullptr;
|
if (count < 1) return nullptr;
|
||||||
|
|
||||||
TSNode firstChild = ts_node_named_child(node, 0);
|
// Check for lambda keyword — may be unnamed child
|
||||||
std::string formName = nodeText(firstChild, source);
|
// Scan all children (including unnamed) for the keyword
|
||||||
|
std::string formName;
|
||||||
|
uint32_t totalChildren = ts_node_child_count(node);
|
||||||
|
for (uint32_t c = 0; c < totalChildren; ++c) {
|
||||||
|
TSNode ch = ts_node_child(node, c);
|
||||||
|
std::string chType = ts_node_type(ch);
|
||||||
|
if (chType == "(" || chType == ")") continue;
|
||||||
|
// First non-paren child is the keyword
|
||||||
|
formName = chType;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Fallback: use first named child text
|
||||||
|
if (formName.empty()) {
|
||||||
|
TSNode firstChild = ts_node_named_child(node, 0);
|
||||||
|
formName = nodeText(firstChild, source);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formName == "lambda") {
|
||||||
|
return convertElispLambdaFromSpecialForm(node, source);
|
||||||
|
}
|
||||||
|
|
||||||
if (formName == "if" && count >= 3) {
|
if (formName == "if" && count >= 3) {
|
||||||
auto* ifStmt = new IfStatement();
|
auto* ifStmt = new IfStatement();
|
||||||
@@ -386,4 +417,79 @@ private:
|
|||||||
return call;
|
return call;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert (lambda (args) body...) to LambdaExpression
|
||||||
|
static LambdaExpression* convertElispLambda(TSNode node, const std::string& source) {
|
||||||
|
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
|
||||||
|
applySpan(lambda, node);
|
||||||
|
|
||||||
|
uint32_t count = ts_node_named_child_count(node);
|
||||||
|
// child 0 = "lambda" symbol, child 1 = arglist, rest = body
|
||||||
|
if (count >= 2) {
|
||||||
|
TSNode arglist = ts_node_named_child(node, 1);
|
||||||
|
if (nodeType(arglist) == "list") {
|
||||||
|
uint32_t ac = ts_node_named_child_count(arglist);
|
||||||
|
for (uint32_t a = 0; a < ac; ++a) {
|
||||||
|
TSNode arg = ts_node_named_child(arglist, a);
|
||||||
|
std::string argType = nodeType(arg);
|
||||||
|
if (argType == "symbol" || argType == "identifier") {
|
||||||
|
auto* param = new Parameter(IdGenerator::next("param"),
|
||||||
|
nodeText(arg, source));
|
||||||
|
applySpan(param, arg);
|
||||||
|
lambda->addChild("parameters", param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Body forms
|
||||||
|
for (uint32_t i = 2; i < count; ++i) {
|
||||||
|
ASTNode* expr = convertElispExpression(ts_node_named_child(node, i), source);
|
||||||
|
if (expr) {
|
||||||
|
auto* exprStmt = new ExpressionStatement();
|
||||||
|
exprStmt->id = IdGenerator::next("exprstmt");
|
||||||
|
applySpan(exprStmt, ts_node_named_child(node, i));
|
||||||
|
exprStmt->setChild("expression", expr);
|
||||||
|
lambda->addChild("body", exprStmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lambda;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert special_form lambda where "lambda" is an unnamed keyword child
|
||||||
|
// Named children: child 0 = arglist, child 1+ = body forms
|
||||||
|
static LambdaExpression* convertElispLambdaFromSpecialForm(TSNode node, const std::string& source) {
|
||||||
|
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
|
||||||
|
applySpan(lambda, node);
|
||||||
|
|
||||||
|
uint32_t count = ts_node_named_child_count(node);
|
||||||
|
// Named child 0 = arglist (list), rest = body
|
||||||
|
if (count >= 1) {
|
||||||
|
TSNode arglist = ts_node_named_child(node, 0);
|
||||||
|
if (nodeType(arglist) == "list") {
|
||||||
|
uint32_t ac = ts_node_named_child_count(arglist);
|
||||||
|
for (uint32_t a = 0; a < ac; ++a) {
|
||||||
|
TSNode arg = ts_node_named_child(arglist, a);
|
||||||
|
std::string argType = nodeType(arg);
|
||||||
|
if (argType == "symbol" || argType == "identifier") {
|
||||||
|
auto* param = new Parameter(IdGenerator::next("param"),
|
||||||
|
nodeText(arg, source));
|
||||||
|
applySpan(param, arg);
|
||||||
|
lambda->addChild("parameters", param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Body forms (named children after arglist)
|
||||||
|
for (uint32_t i = 1; i < count; ++i) {
|
||||||
|
ASTNode* expr = convertElispExpression(ts_node_named_child(node, i), source);
|
||||||
|
if (expr) {
|
||||||
|
auto* exprStmt = new ExpressionStatement();
|
||||||
|
exprStmt->id = IdGenerator::next("exprstmt");
|
||||||
|
applySpan(exprStmt, ts_node_named_child(node, i));
|
||||||
|
exprStmt->setChild("expression", expr);
|
||||||
|
lambda->addChild("body", exprStmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lambda;
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
|
|||||||
@@ -49,7 +49,16 @@ private:
|
|||||||
static void convertGoSourceFile(TSNode root,
|
static void convertGoSourceFile(TSNode root,
|
||||||
const std::string& source,
|
const std::string& source,
|
||||||
Module* module) {
|
Module* module) {
|
||||||
|
// First pass: collect type declarations (structs/interfaces)
|
||||||
uint32_t count = ts_node_named_child_count(root);
|
uint32_t count = ts_node_named_child_count(root);
|
||||||
|
for (uint32_t i = 0; i < count; ++i) {
|
||||||
|
TSNode child = ts_node_named_child(root, i);
|
||||||
|
std::string type = nodeType(child);
|
||||||
|
if (type == "type_declaration") {
|
||||||
|
convertGoTypeDeclaration(child, source, module);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Second pass: functions, methods, vars, imports
|
||||||
for (uint32_t i = 0; i < count; ++i) {
|
for (uint32_t i = 0; i < count; ++i) {
|
||||||
TSNode child = ts_node_named_child(root, i);
|
TSNode child = ts_node_named_child(root, i);
|
||||||
std::string type = nodeType(child);
|
std::string type = nodeType(child);
|
||||||
@@ -59,12 +68,9 @@ private:
|
|||||||
auto* fn = convertGoFunction(child, source, "");
|
auto* fn = convertGoFunction(child, source, "");
|
||||||
if (fn) module->addChild("functions", fn);
|
if (fn) module->addChild("functions", fn);
|
||||||
} else if (type == "method_declaration") {
|
} else if (type == "method_declaration") {
|
||||||
auto* fn = convertGoMethod(child, source);
|
convertGoMethodDecl(child, source, module);
|
||||||
if (fn) module->addChild("functions", fn);
|
|
||||||
} else if (type == "var_declaration") {
|
} else if (type == "var_declaration") {
|
||||||
convertGoVarDeclaration(child, source, module);
|
convertGoVarDeclaration(child, source, module);
|
||||||
} else if (type == "type_declaration") {
|
|
||||||
convertGoTypeDeclaration(child, source, module);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +124,57 @@ private:
|
|||||||
if (nodeType(spec) != "type_spec") continue;
|
if (nodeType(spec) != "type_spec") continue;
|
||||||
TSNode nameNode = childByFieldName(spec, "name");
|
TSNode nameNode = childByFieldName(spec, "name");
|
||||||
if (ts_node_is_null(nameNode)) continue;
|
if (ts_node_is_null(nameNode)) continue;
|
||||||
auto* var = new Variable(IdGenerator::next("var"), nodeText(nameNode, source));
|
std::string typeName = nodeText(nameNode, source);
|
||||||
|
|
||||||
|
TSNode typeNode = childByFieldName(spec, "type");
|
||||||
|
if (!ts_node_is_null(typeNode)) {
|
||||||
|
std::string typeKind = nodeType(typeNode);
|
||||||
|
if (typeKind == "struct_type") {
|
||||||
|
auto* cls = new ClassDeclaration(IdGenerator::next("cls"), typeName);
|
||||||
|
applySpan(cls, spec);
|
||||||
|
// Extract fields from struct body
|
||||||
|
uint32_t fc = ts_node_named_child_count(typeNode);
|
||||||
|
for (uint32_t f = 0; f < fc; ++f) {
|
||||||
|
TSNode field = ts_node_named_child(typeNode, f);
|
||||||
|
if (nodeType(field) == "field_declaration" ||
|
||||||
|
nodeType(field) == "field_declaration_list") {
|
||||||
|
TSNode fnameNode = childByFieldName(field, "name");
|
||||||
|
if (!ts_node_is_null(fnameNode)) {
|
||||||
|
auto* var = new Variable(IdGenerator::next("var"),
|
||||||
|
nodeText(fnameNode, source));
|
||||||
|
applySpan(var, field);
|
||||||
|
cls->addChild("fields", var);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module->addChild("classes", cls);
|
||||||
|
continue;
|
||||||
|
} else if (typeKind == "interface_type") {
|
||||||
|
auto* iface = new InterfaceDeclaration(IdGenerator::next("iface"), typeName);
|
||||||
|
applySpan(iface, spec);
|
||||||
|
// Extract method signatures
|
||||||
|
uint32_t mc = ts_node_named_child_count(typeNode);
|
||||||
|
for (uint32_t m = 0; m < mc; ++m) {
|
||||||
|
TSNode methSpec = ts_node_named_child(typeNode, m);
|
||||||
|
if (nodeType(methSpec) == "method_spec" ||
|
||||||
|
nodeType(methSpec) == "method_elem") {
|
||||||
|
TSNode methName = childByFieldName(methSpec, "name");
|
||||||
|
if (!ts_node_is_null(methName)) {
|
||||||
|
auto* meth = new MethodDeclaration(
|
||||||
|
IdGenerator::next("meth"), nodeText(methName, source));
|
||||||
|
meth->className = typeName;
|
||||||
|
meth->isVirtual = true;
|
||||||
|
applySpan(meth, methSpec);
|
||||||
|
iface->addChild("methods", meth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module->addChild("classes", iface);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Fallback: non-struct/interface type alias → Variable
|
||||||
|
auto* var = new Variable(IdGenerator::next("var"), typeName);
|
||||||
module->addChild("variables", var);
|
module->addChild("variables", var);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,6 +228,82 @@ private:
|
|||||||
return convertGoFunction(node, source, receiverType);
|
return convertGoFunction(node, source, receiverType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a MethodDeclaration and attach to existing ClassDeclaration
|
||||||
|
static void convertGoMethodDecl(TSNode node,
|
||||||
|
const std::string& source,
|
||||||
|
Module* module) {
|
||||||
|
TSNode recvNode = childByFieldName(node, "receiver");
|
||||||
|
std::string receiverType;
|
||||||
|
if (!ts_node_is_null(recvNode)) {
|
||||||
|
TSNode typeNode = findDescendantByField(recvNode, "type");
|
||||||
|
if (!ts_node_is_null(typeNode)) {
|
||||||
|
receiverType = nodeText(typeNode, source);
|
||||||
|
}
|
||||||
|
if (receiverType.empty()) {
|
||||||
|
TSNode nameNode = findDescendantByField(recvNode, "name");
|
||||||
|
if (!ts_node_is_null(nameNode)) {
|
||||||
|
receiverType = nodeText(nameNode, source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
receiverType = trimPointerPrefix(receiverType);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract method name
|
||||||
|
TSNode nameNode = childByFieldName(node, "name");
|
||||||
|
if (ts_node_is_null(nameNode)) return;
|
||||||
|
std::string methodName = nodeText(nameNode, source);
|
||||||
|
|
||||||
|
// Create MethodDeclaration
|
||||||
|
auto* meth = new MethodDeclaration(IdGenerator::next("meth"), methodName);
|
||||||
|
meth->className = receiverType;
|
||||||
|
applySpan(meth, node);
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
TSNode paramsNode = childByFieldName(node, "parameters");
|
||||||
|
if (!ts_node_is_null(paramsNode)) {
|
||||||
|
convertGoParameters(paramsNode, source, meth);
|
||||||
|
}
|
||||||
|
// Return type
|
||||||
|
TSNode resultNode = childByFieldName(node, "result");
|
||||||
|
if (!ts_node_is_null(resultNode)) {
|
||||||
|
if (auto* t = convertGoType(resultNode, source)) meth->setChild("returnType", t);
|
||||||
|
}
|
||||||
|
// Body
|
||||||
|
TSNode bodyNode = childByFieldName(node, "body");
|
||||||
|
if (!ts_node_is_null(bodyNode)) {
|
||||||
|
convertGoBlock(bodyNode, source, meth);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to attach to existing ClassDeclaration
|
||||||
|
bool attached = false;
|
||||||
|
if (!receiverType.empty()) {
|
||||||
|
auto& classes = module->getChildren("classes");
|
||||||
|
for (auto* entry : classes) {
|
||||||
|
auto* cls = dynamic_cast<ClassDeclaration*>(entry);
|
||||||
|
if (cls && cls->name == receiverType) {
|
||||||
|
cls->addChild("methods", meth);
|
||||||
|
attached = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Also add as backward-compat Function in functions list
|
||||||
|
auto* fn = convertGoFunction(node, source, receiverType);
|
||||||
|
if (fn) module->addChild("functions", fn);
|
||||||
|
|
||||||
|
// If not attached to any class, the MethodDeclaration is still owned by the class search
|
||||||
|
if (!attached) {
|
||||||
|
// Create a ClassDeclaration stub for the receiver type
|
||||||
|
if (!receiverType.empty()) {
|
||||||
|
auto* cls = new ClassDeclaration(IdGenerator::next("cls"), receiverType);
|
||||||
|
cls->addChild("methods", meth);
|
||||||
|
module->addChild("classes", cls);
|
||||||
|
} else {
|
||||||
|
delete meth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void convertGoParameters(TSNode paramsNode,
|
static void convertGoParameters(TSNode paramsNode,
|
||||||
const std::string& source,
|
const std::string& source,
|
||||||
Function* fn) {
|
Function* fn) {
|
||||||
@@ -421,6 +553,44 @@ private:
|
|||||||
if (ts_node_named_child_count(node) > 0) {
|
if (ts_node_named_child_count(node) > 0) {
|
||||||
return convertGoExpression(ts_node_named_child(node, 0), source);
|
return convertGoExpression(ts_node_named_child(node, 0), source);
|
||||||
}
|
}
|
||||||
|
} else if (type == "expression_list") {
|
||||||
|
// Unwrap single-element expression lists
|
||||||
|
uint32_t elc = ts_node_named_child_count(node);
|
||||||
|
if (elc == 1) {
|
||||||
|
return convertGoExpression(ts_node_named_child(node, 0), source);
|
||||||
|
}
|
||||||
|
if (elc > 0) {
|
||||||
|
return convertGoExpression(ts_node_named_child(node, 0), source);
|
||||||
|
}
|
||||||
|
} else if (type == "func_literal") {
|
||||||
|
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
|
||||||
|
applySpan(lambda, node);
|
||||||
|
TSNode paramsNode = childByFieldName(node, "parameters");
|
||||||
|
if (!ts_node_is_null(paramsNode)) {
|
||||||
|
uint32_t pc = ts_node_named_child_count(paramsNode);
|
||||||
|
for (uint32_t p = 0; p < pc; ++p) {
|
||||||
|
TSNode paramChild = ts_node_named_child(paramsNode, p);
|
||||||
|
if (nodeType(paramChild) == "parameter_declaration" ||
|
||||||
|
nodeType(paramChild) == "variadic_parameter_declaration") {
|
||||||
|
TSNode pnameNode = childByFieldName(paramChild, "name");
|
||||||
|
if (!ts_node_is_null(pnameNode)) {
|
||||||
|
auto* param = new Parameter(IdGenerator::next("param"),
|
||||||
|
nodeText(pnameNode, source));
|
||||||
|
applySpan(param, paramChild);
|
||||||
|
lambda->addChild("parameters", param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TSNode bodyNode = childByFieldName(node, "body");
|
||||||
|
if (!ts_node_is_null(bodyNode)) {
|
||||||
|
uint32_t bc = ts_node_named_child_count(bodyNode);
|
||||||
|
for (uint32_t b = 0; b < bc; ++b) {
|
||||||
|
ASTNode* stmt = convertGoStatement(ts_node_named_child(bodyNode, b), source);
|
||||||
|
if (stmt) lambda->addChild("body", stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lambda;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string text = nodeText(node, source);
|
std::string text = nodeText(node, source);
|
||||||
|
|||||||
315
editor/tests/step307_test.cpp
Normal file
315
editor/tests/step307_test.cpp
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
// Step 307: Go + C++ + Elisp Parser Deepening (12 tests)
|
||||||
|
// Tests that Go, C++, and Elisp parsers correctly produce the new AST node
|
||||||
|
// types: ClassDeclaration, InterfaceDeclaration, MethodDeclaration,
|
||||||
|
// GenericType, LambdaExpression via tree-sitter parsing.
|
||||||
|
|
||||||
|
#include "ast/Parser.h"
|
||||||
|
#include "ast/ClassDeclaration.h"
|
||||||
|
#include "ast/GenericType.h"
|
||||||
|
#include "ast/AsyncNodes.h"
|
||||||
|
#include "ast/Module.h"
|
||||||
|
#include "ast/Function.h"
|
||||||
|
#include "ast/Variable.h"
|
||||||
|
#include "ast/Parameter.h"
|
||||||
|
#include "ast/Statement.h"
|
||||||
|
#include "ast/Expression.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
static int passed = 0, failed = 0;
|
||||||
|
#define TEST(name) { std::cout << " " << #name << "... "; }
|
||||||
|
#define PASS() { std::cout << "PASS\n"; ++passed; }
|
||||||
|
#define FAIL(msg) { std::cout << "FAIL: " << msg << "\n"; ++failed; }
|
||||||
|
#define CHECK(cond, msg) if (!(cond)) { FAIL(msg); return; } else {}
|
||||||
|
|
||||||
|
// Helper: recursively find a node of a given conceptType
|
||||||
|
static bool findNodeOfType(ASTNode* node, const std::string& type) {
|
||||||
|
if (!node) return false;
|
||||||
|
if (node->conceptType == type) return true;
|
||||||
|
for (auto* child : node->allChildren()) {
|
||||||
|
if (findNodeOfType(child, type)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
// Go tests (1-5)
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
|
// 1. Go backward compat — simple function still yields Function
|
||||||
|
void test_go_backward_compat() {
|
||||||
|
TEST(go_backward_compat);
|
||||||
|
std::string src = "package main\n\nfunc greet(name string) string {\n return name\n}\n";
|
||||||
|
auto mod = TreeSitterParser::parseGo(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& fns = mod->getChildren("functions");
|
||||||
|
CHECK(!fns.empty(), "expected at least one function");
|
||||||
|
auto* fn0 = dynamic_cast<Function*>(fns[0]);
|
||||||
|
CHECK(fn0 != nullptr, "dynamic_cast to Function failed");
|
||||||
|
CHECK(fn0->name == "greet", "expected name 'greet', got " + fn0->name);
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Go struct → ClassDeclaration
|
||||||
|
void test_go_struct() {
|
||||||
|
TEST(go_struct);
|
||||||
|
std::string src =
|
||||||
|
"package main\n\n"
|
||||||
|
"type Point struct {\n"
|
||||||
|
" X float64\n"
|
||||||
|
" Y float64\n"
|
||||||
|
"}\n";
|
||||||
|
auto mod = TreeSitterParser::parseGo(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& classes = mod->getChildren("classes");
|
||||||
|
CHECK(!classes.empty(), "expected at least one class");
|
||||||
|
|
||||||
|
auto* cls = dynamic_cast<ClassDeclaration*>(classes[0]);
|
||||||
|
CHECK(cls != nullptr, "expected ClassDeclaration, got " + classes[0]->conceptType);
|
||||||
|
CHECK(cls->name == "Point", "expected name 'Point', got " + cls->name);
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Go interface → InterfaceDeclaration
|
||||||
|
void test_go_interface() {
|
||||||
|
TEST(go_interface);
|
||||||
|
std::string src =
|
||||||
|
"package main\n\n"
|
||||||
|
"type Stringer interface {\n"
|
||||||
|
" String() string\n"
|
||||||
|
"}\n";
|
||||||
|
auto mod = TreeSitterParser::parseGo(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& classes = mod->getChildren("classes");
|
||||||
|
bool foundInterface = false;
|
||||||
|
for (auto* entry : classes) {
|
||||||
|
if (entry->conceptType == "InterfaceDeclaration") {
|
||||||
|
auto* iface = dynamic_cast<InterfaceDeclaration*>(entry);
|
||||||
|
CHECK(iface != nullptr, "dynamic_cast to InterfaceDeclaration failed");
|
||||||
|
CHECK(iface->name == "Stringer", "expected name 'Stringer', got " + iface->name);
|
||||||
|
foundInterface = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CHECK(foundInterface, "expected InterfaceDeclaration in classes");
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Go method receiver → MethodDeclaration on ClassDeclaration
|
||||||
|
void test_go_method_receiver() {
|
||||||
|
TEST(go_method_receiver);
|
||||||
|
std::string src =
|
||||||
|
"package main\n\n"
|
||||||
|
"type Dog struct {\n"
|
||||||
|
" Name string\n"
|
||||||
|
"}\n\n"
|
||||||
|
"func (d *Dog) Bark() string {\n"
|
||||||
|
" return \"Woof!\"\n"
|
||||||
|
"}\n";
|
||||||
|
auto mod = TreeSitterParser::parseGo(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& classes = mod->getChildren("classes");
|
||||||
|
CHECK(!classes.empty(), "expected class from struct");
|
||||||
|
|
||||||
|
auto* cls = dynamic_cast<ClassDeclaration*>(classes[0]);
|
||||||
|
CHECK(cls != nullptr, "expected ClassDeclaration");
|
||||||
|
CHECK(cls->name == "Dog", "expected name 'Dog', got " + cls->name);
|
||||||
|
|
||||||
|
auto& methods = cls->getChildren("methods");
|
||||||
|
CHECK(!methods.empty(), "expected at least one method from receiver");
|
||||||
|
|
||||||
|
auto* meth = dynamic_cast<MethodDeclaration*>(methods[0]);
|
||||||
|
CHECK(meth != nullptr, "expected MethodDeclaration");
|
||||||
|
CHECK(meth->name == "Bark", "expected method 'Bark', got " + meth->name);
|
||||||
|
CHECK(meth->className == "Dog", "expected className 'Dog', got " + meth->className);
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Go func literal → LambdaExpression
|
||||||
|
void test_go_func_literal() {
|
||||||
|
TEST(go_func_literal);
|
||||||
|
std::string src =
|
||||||
|
"package main\n\n"
|
||||||
|
"func make() {\n"
|
||||||
|
" fn := func(x int) int { return x + 1 }\n"
|
||||||
|
"}\n";
|
||||||
|
auto mod = TreeSitterParser::parseGo(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& fns = mod->getChildren("functions");
|
||||||
|
CHECK(!fns.empty(), "expected function");
|
||||||
|
|
||||||
|
CHECK(findNodeOfType(fns[0], "LambdaExpression"),
|
||||||
|
"expected LambdaExpression from func literal in body");
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
// C++ tests (6-10)
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
|
// 6. C++ backward compat — simple function still yields Function
|
||||||
|
void test_cpp_backward_compat() {
|
||||||
|
TEST(cpp_backward_compat);
|
||||||
|
std::string src = "int add(int a, int b) {\n return a + b;\n}\n";
|
||||||
|
auto mod = TreeSitterParser::parseCpp(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& fns = mod->getChildren("functions");
|
||||||
|
CHECK(!fns.empty(), "expected at least one function");
|
||||||
|
auto* fn0 = dynamic_cast<Function*>(fns[0]);
|
||||||
|
CHECK(fn0 != nullptr, "dynamic_cast to Function failed");
|
||||||
|
CHECK(fn0->name == "add", "expected name 'add', got " + fn0->name);
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. C++ class → ClassDeclaration with MethodDeclaration
|
||||||
|
void test_cpp_class() {
|
||||||
|
TEST(cpp_class);
|
||||||
|
std::string src =
|
||||||
|
"class Animal {\n"
|
||||||
|
"public:\n"
|
||||||
|
" void speak() {\n"
|
||||||
|
" return;\n"
|
||||||
|
" }\n"
|
||||||
|
"};\n";
|
||||||
|
auto mod = TreeSitterParser::parseCpp(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& classes = mod->getChildren("classes");
|
||||||
|
CHECK(!classes.empty(), "expected at least one class");
|
||||||
|
|
||||||
|
auto* cls = dynamic_cast<ClassDeclaration*>(classes[0]);
|
||||||
|
CHECK(cls != nullptr, "expected ClassDeclaration, got " + classes[0]->conceptType);
|
||||||
|
CHECK(cls->name == "Animal", "expected name 'Animal', got " + cls->name);
|
||||||
|
|
||||||
|
auto& methods = cls->getChildren("methods");
|
||||||
|
CHECK(!methods.empty(), "expected at least one method");
|
||||||
|
auto* meth = dynamic_cast<MethodDeclaration*>(methods[0]);
|
||||||
|
CHECK(meth != nullptr, "expected MethodDeclaration");
|
||||||
|
CHECK(meth->name == "speak", "expected method 'speak', got " + meth->name);
|
||||||
|
CHECK(meth->className == "Animal", "expected className 'Animal', got " + meth->className);
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8. C++ struct → ClassDeclaration
|
||||||
|
void test_cpp_struct() {
|
||||||
|
TEST(cpp_struct);
|
||||||
|
std::string src =
|
||||||
|
"struct Point {\n"
|
||||||
|
" double x;\n"
|
||||||
|
" double y;\n"
|
||||||
|
"};\n";
|
||||||
|
auto mod = TreeSitterParser::parseCpp(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& classes = mod->getChildren("classes");
|
||||||
|
CHECK(!classes.empty(), "expected class from struct");
|
||||||
|
|
||||||
|
auto* cls = dynamic_cast<ClassDeclaration*>(classes[0]);
|
||||||
|
CHECK(cls != nullptr, "expected ClassDeclaration, got " + classes[0]->conceptType);
|
||||||
|
CHECK(cls->name == "Point", "expected name 'Point', got " + cls->name);
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 9. C++ template class → GenericType on ClassDeclaration
|
||||||
|
void test_cpp_template() {
|
||||||
|
TEST(cpp_template);
|
||||||
|
std::string src =
|
||||||
|
"template<typename T>\n"
|
||||||
|
"class Container {\n"
|
||||||
|
"public:\n"
|
||||||
|
" T value;\n"
|
||||||
|
"};\n";
|
||||||
|
auto mod = TreeSitterParser::parseCpp(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& classes = mod->getChildren("classes");
|
||||||
|
CHECK(!classes.empty(), "expected class from template");
|
||||||
|
|
||||||
|
auto* cls = dynamic_cast<ClassDeclaration*>(classes[0]);
|
||||||
|
CHECK(cls != nullptr, "expected ClassDeclaration");
|
||||||
|
CHECK(cls->name == "Container", "expected name 'Container', got " + cls->name);
|
||||||
|
|
||||||
|
// Should have a GenericType annotation or child
|
||||||
|
CHECK(findNodeOfType(cls, "TypeParameter"),
|
||||||
|
"expected TypeParameter from template parameter");
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10. C++ lambda → LambdaExpression
|
||||||
|
void test_cpp_lambda() {
|
||||||
|
TEST(cpp_lambda);
|
||||||
|
std::string src =
|
||||||
|
"void make() {\n"
|
||||||
|
" auto fn = [](int x) { return x + 1; };\n"
|
||||||
|
"}\n";
|
||||||
|
auto mod = TreeSitterParser::parseCpp(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& fns = mod->getChildren("functions");
|
||||||
|
CHECK(!fns.empty(), "expected function");
|
||||||
|
|
||||||
|
CHECK(findNodeOfType(fns[0], "LambdaExpression"),
|
||||||
|
"expected LambdaExpression from C++ lambda in body");
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
// Elisp tests (11-12)
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
|
// 11. Elisp backward compat — defun still yields Function
|
||||||
|
void test_elisp_backward_compat() {
|
||||||
|
TEST(elisp_backward_compat);
|
||||||
|
std::string src = "(defun greet (name)\n (message name))\n";
|
||||||
|
auto mod = TreeSitterParser::parseElisp(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& fns = mod->getChildren("functions");
|
||||||
|
CHECK(!fns.empty(), "expected at least one function");
|
||||||
|
auto* fn0 = dynamic_cast<Function*>(fns[0]);
|
||||||
|
CHECK(fn0 != nullptr, "dynamic_cast to Function failed");
|
||||||
|
CHECK(fn0->name == "greet", "expected name 'greet', got " + fn0->name);
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 12. Elisp lambda → LambdaExpression
|
||||||
|
void test_elisp_lambda() {
|
||||||
|
TEST(elisp_lambda);
|
||||||
|
std::string src =
|
||||||
|
"(defun make ()\n"
|
||||||
|
" (lambda (x) (+ x 1)))\n";
|
||||||
|
auto mod = TreeSitterParser::parseElisp(src);
|
||||||
|
CHECK(mod != nullptr, "module null");
|
||||||
|
|
||||||
|
auto& fns = mod->getChildren("functions");
|
||||||
|
CHECK(!fns.empty(), "expected function");
|
||||||
|
|
||||||
|
CHECK(findNodeOfType(fns[0], "LambdaExpression"),
|
||||||
|
"expected LambdaExpression from (lambda ...) in function body");
|
||||||
|
PASS();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << "=== Step 307: Go + C++ + Elisp Parser Deepening ===\n";
|
||||||
|
test_go_backward_compat();
|
||||||
|
test_go_struct();
|
||||||
|
test_go_interface();
|
||||||
|
test_go_method_receiver();
|
||||||
|
test_go_func_literal();
|
||||||
|
test_cpp_backward_compat();
|
||||||
|
test_cpp_class();
|
||||||
|
test_cpp_struct();
|
||||||
|
test_cpp_template();
|
||||||
|
test_cpp_lambda();
|
||||||
|
test_elisp_backward_compat();
|
||||||
|
test_elisp_lambda();
|
||||||
|
std::cout << "\nResults: " << passed << "/" << (passed + failed) << " passed\n";
|
||||||
|
return failed > 0 ? 1 : 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user