Step 151: add Go parser and generator

This commit is contained in:
Bill
2026-02-09 19:08:42 -07:00
parent 7ef0c4a8ad
commit b4cf14f3ae
12 changed files with 1159 additions and 3 deletions

View File

@@ -498,3 +498,4 @@ Sprint 5 in progress. Step 141 (Emacs daemon with user config) done. Next: Step
| 2026-02-10 | Codex | Step 148: JavaScript/TypeScript generator with imports, class method grouping, type annotations, and WeakRef/FinalizationRegistry mapping. 6/6 tests pass. | | 2026-02-10 | Codex | Step 148: JavaScript/TypeScript generator with imports, class method grouping, type annotations, and WeakRef/FinalizationRegistry mapping. 6/6 tests pass. |
| 2026-02-10 | Codex | Step 149: Java CST-to-AST parsing + generator with class/method/constructor handling, import support, and GC-aware annotations. 8/8 tests pass. | | 2026-02-10 | Codex | Step 149: Java CST-to-AST parsing + generator with class/method/constructor handling, import support, and GC-aware annotations. 8/8 tests pass. |
| 2026-02-10 | Codex | Step 150: Rust CST-to-AST parsing + generator with impl methods, imports, and RAII annotations. 7/7 tests pass. | | 2026-02-10 | Codex | Step 150: Rust CST-to-AST parsing + generator with impl methods, imports, and RAII annotations. 7/7 tests pass. |
| 2026-02-10 | Codex | Step 151: Go CST-to-AST parsing + generator with receivers, imports, and Go-style control flow. 8/8 tests pass. |

View File

@@ -849,6 +849,13 @@ target_link_libraries(step150_test PRIVATE
unofficial::tree-sitter::tree-sitter unofficial::tree-sitter::tree-sitter
tree_sitter_rust) tree_sitter_rust)
add_executable(step151_test tests/step151_test.cpp)
target_include_directories(step151_test PRIVATE src)
target_link_libraries(step151_test PRIVATE
nlohmann_json::nlohmann_json
unofficial::tree-sitter::tree-sitter
tree_sitter_go)
find_package(SDL2 CONFIG REQUIRED) find_package(SDL2 CONFIG REQUIRED)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(glad CONFIG REQUIRED) find_package(glad CONFIG REQUIRED)

View File

@@ -16,14 +16,15 @@ public:
return { return {
// Parsers // Parsers
{"TreeSitterParser", "parser", {"TreeSitterParser", "parser",
"Parses source code (Python, C++, Elisp, JavaScript, TypeScript, Java, Rust) into Whetstone AST using tree-sitter grammars", "Parses source code (Python, C++, Elisp, JavaScript, TypeScript, Java, Rust, Go) into Whetstone AST using tree-sitter grammars",
{"parsePython(source)", "parsePythonWithDiagnostics(source)", {"parsePython(source)", "parsePythonWithDiagnostics(source)",
"parseCpp(source)", "parseCppWithDiagnostics(source)", "parseCpp(source)", "parseCppWithDiagnostics(source)",
"parseElisp(source)", "parseElispWithDiagnostics(source)", "parseElisp(source)", "parseElispWithDiagnostics(source)",
"parseJavaScript(source)", "parseJavaScriptWithDiagnostics(source)", "parseJavaScript(source)", "parseJavaScriptWithDiagnostics(source)",
"parseTypeScript(source)", "parseTypeScriptWithDiagnostics(source)", "parseTypeScript(source)", "parseTypeScriptWithDiagnostics(source)",
"parseJava(source)", "parseJavaWithDiagnostics(source)", "parseJava(source)", "parseJavaWithDiagnostics(source)",
"parseRust(source)", "parseRustWithDiagnostics(source)"}}, "parseRust(source)", "parseRustWithDiagnostics(source)",
"parseGo(source)", "parseGoWithDiagnostics(source)"}},
// Generators // Generators
{"PythonGenerator", "generator", {"PythonGenerator", "generator",
@@ -54,6 +55,10 @@ public:
"Generates Rust source code from Whetstone AST", "Generates Rust source code from Whetstone AST",
{"generate(node)"}}, {"generate(node)"}},
{"GoGenerator", "generator",
"Generates Go source code from Whetstone AST",
{"generate(node)"}},
// Validators // Validators
{"AnnotationValidator", "validator", {"AnnotationValidator", "validator",
"Validates memory annotations for correctness: missing intent, aliasing, conflicts", "Validates memory annotations for correctness: missing intent, aliasing, conflicts",

View File

@@ -58,6 +58,10 @@ static std::string generateForLanguage(const Module* ast, const std::string& lan
RustGenerator gen; RustGenerator gen;
return gen.generate(ast); return gen.generate(ast);
} }
if (language == "go") {
GoGenerator gen;
return gen.generate(ast);
}
PythonGenerator gen; PythonGenerator gen;
return gen.generate(ast); return gen.generate(ast);
} }

View File

@@ -406,6 +406,8 @@ public:
targetLanguage = "java"; targetLanguage = "java";
} else if (path.substr(path.find_last_of(".") + 1) == "rs") { } else if (path.substr(path.find_last_of(".") + 1) == "rs") {
targetLanguage = "rust"; targetLanguage = "rust";
} else if (path.substr(path.find_last_of(".") + 1) == "go") {
targetLanguage = "go";
} }
// Parse the content using the appropriate tree-sitter parser // Parse the content using the appropriate tree-sitter parser
@@ -424,6 +426,8 @@ public:
module = TreeSitterParser::parseJava(content); module = TreeSitterParser::parseJava(content);
} else if (targetLanguage == "rust") { } else if (targetLanguage == "rust") {
module = TreeSitterParser::parseRust(content); module = TreeSitterParser::parseRust(content);
} else if (targetLanguage == "go") {
module = TreeSitterParser::parseGo(content);
} else { } else {
// For unknown languages, create a basic module with the content // For unknown languages, create a basic module with the content
module = std::make_unique<Module>(); module = std::make_unique<Module>();
@@ -463,6 +467,8 @@ public:
targetLanguage = "java"; targetLanguage = "java";
} else if (path.substr(path.find_last_of(".") + 1) == "rs") { } else if (path.substr(path.find_last_of(".") + 1) == "rs") {
targetLanguage = "rust"; targetLanguage = "rust";
} else if (path.substr(path.find_last_of(".") + 1) == "go") {
targetLanguage = "go";
} }
} }
@@ -489,6 +495,9 @@ public:
} else if (targetLanguage == "rust") { } else if (targetLanguage == "rust") {
RustGenerator gen; RustGenerator gen;
content = gen.generate(ast); content = gen.generate(ast);
} else if (targetLanguage == "go") {
GoGenerator gen;
content = gen.generate(ast);
} else { } else {
// Default to Python generator for unknown languages // Default to Python generator for unknown languages
PythonGenerator gen; PythonGenerator gen;

View File

@@ -106,6 +106,10 @@ public:
auto pr = TreeSitterParser::parseRustWithDiagnostics(source); auto pr = TreeSitterParser::parseRustWithDiagnostics(source);
diags = std::move(pr.diagnostics); diags = std::move(pr.diagnostics);
return std::move(pr.module); return std::move(pr.module);
} else if (language == "go") {
auto pr = TreeSitterParser::parseGoWithDiagnostics(source);
diags = std::move(pr.diagnostics);
return std::move(pr.module);
} }
return nullptr; return nullptr;
} }
@@ -134,6 +138,9 @@ public:
} else if (language == "rust") { } else if (language == "rust") {
RustGenerator gen; RustGenerator gen;
return gen.generate(ast); return gen.generate(ast);
} else if (language == "go") {
GoGenerator gen;
return gen.generate(ast);
} }
return ""; return "";
} }

View File

@@ -53,6 +53,9 @@ public:
} else if (language_ == "rust") { } else if (language_ == "rust") {
RustGenerator gen; RustGenerator gen;
return gen.generate(module_.get()); return gen.generate(module_.get());
} else if (language_ == "go") {
GoGenerator gen;
return gen.generate(module_.get());
} }
return text_; return text_;
} }
@@ -73,6 +76,8 @@ public:
module_ = TreeSitterParser::parseJava(text_); module_ = TreeSitterParser::parseJava(text_);
} else if (language_ == "rust") { } else if (language_ == "rust") {
module_ = TreeSitterParser::parseRust(text_); module_ = TreeSitterParser::parseRust(text_);
} else if (language_ == "go") {
module_ = TreeSitterParser::parseGo(text_);
} }
parsePending_ = false; parsePending_ = false;
} }

View File

@@ -8,3 +8,4 @@
#include "JavaScriptGenerator.h" #include "JavaScriptGenerator.h"
#include "JavaGenerator.h" #include "JavaGenerator.h"
#include "RustGenerator.h" #include "RustGenerator.h"
#include "GoGenerator.h"

View File

@@ -0,0 +1,539 @@
#pragma once
#include "ProjectionGenerator.h"
#include "Import.h"
class GoGenerator : public ProjectionGenerator {
public:
std::string generate(const ASTNode* node) override {
return dispatchGenerate(this, node, "// Unknown concept: ");
}
std::string visitModule(const Module* module) override {
std::ostringstream oss;
std::string pkgName = module->name.empty() ? "main" : module->name;
oss << "package " << pkgName << "\n\n";
auto imports = module->getChildren("imports");
if (!imports.empty()) {
oss << "import (\n";
for (const auto* impNode : imports) {
if (impNode->conceptType != "Import") continue;
oss << " " << emitImport(static_cast<const Import*>(impNode)) << "\n";
}
oss << ")\n\n";
}
auto variables = module->getChildren("variables");
for (const auto* var : variables) {
oss << visitVariable(static_cast<const Variable*>(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 << visitFunction(static_cast<const Function*>(functions[i]));
}
return oss.str();
}
std::string visitFunction(const Function* function) override {
std::ostringstream oss;
emitAnnotations(oss, function->getChildren("annotations"), "");
std::string name = function->name;
std::string receiver;
auto pos = name.find('.');
if (pos != std::string::npos && pos > 0 && pos + 1 < name.size()) {
receiver = name.substr(0, pos);
name = name.substr(pos + 1);
}
oss << "func ";
if (!receiver.empty()) {
oss << "(" << receiverVar(receiver) << " *" << receiver << ") ";
}
oss << name << "(";
emitParameters(oss, function->getChildren("parameters"));
oss << ")";
auto retType = function->getChild("returnType");
if (retType) {
oss << " " << generate(retType);
}
oss << " {\n";
emitBody(oss, function->getChildren("body"), " ");
oss << "}\n";
return oss.str();
}
std::string visitVariable(const Variable* variable) override {
std::ostringstream oss;
emitAnnotations(oss, variable->getChildren("annotations"), "");
std::string typeStr;
auto type = variable->getChild("type");
if (type) typeStr = generate(type);
oss << "var " << variable->name;
if (!typeStr.empty()) oss << " " << typeStr;
auto initializer = variable->getChild("initializer");
if (initializer) {
oss << " = " << generate(initializer);
}
oss << "";
return oss.str();
}
std::string visitParameter(const Parameter* parameter) override {
std::ostringstream oss;
std::string typeStr;
auto type = parameter->getChild("type");
if (type) typeStr = generate(type);
oss << parameter->name;
if (!typeStr.empty()) {
oss << " " << typeStr;
}
return oss.str();
}
std::string visitAssignment(const Assignment* assignment) override {
std::ostringstream oss;
auto target = assignment->getChild("target");
auto value = assignment->getChild("value");
if (target) {
oss << generate(target);
} else {
oss << "/* missing target */";
}
oss << " = ";
if (value) {
oss << generate(value);
} else {
oss << "nil";
}
return oss.str();
}
std::string visitReturn(const Return* ret) override {
std::ostringstream oss;
oss << "return";
auto value = ret->getChild("value");
if (value) {
oss << " " << generate(value);
}
return oss.str();
}
std::string visitBinaryOperation(const BinaryOperation* binOp) override {
std::ostringstream oss;
auto left = binOp->getChild("left");
auto right = binOp->getChild("right");
if (left) {
oss << generate(left);
} else {
oss << "/* missing left */";
}
oss << " " << binOp->op << " ";
if (right) {
oss << generate(right);
} else {
oss << "/* missing right */";
}
return oss.str();
}
std::string visitVariableReference(const VariableReference* varRef) override {
return varRef->variableName;
}
std::string visitIntegerLiteral(const IntegerLiteral* lit) override {
return std::to_string(lit->value);
}
std::string visitFloatLiteral(const FloatLiteral* lit) override {
return lit->value;
}
std::string visitStringLiteral(const StringLiteral* lit) override {
if (isQuotedLiteral(lit->value)) return lit->value;
return "\"" + lit->value + "\"";
}
std::string visitBooleanLiteral(const BooleanLiteral* lit) override {
return lit->value ? "true" : "false";
}
std::string visitNullLiteral(const NullLiteral*) override {
return "nil";
}
std::string visitIfStatement(const IfStatement* stmt) override {
std::ostringstream oss;
auto condition = stmt->getChild("condition");
auto thenBranch = stmt->getChildren("thenBranch");
auto elseBranch = stmt->getChildren("elseBranch");
oss << "if ";
if (condition) {
oss << generate(condition);
} else {
oss << "true";
}
oss << " {\n";
emitBody(oss, thenBranch, " ");
oss << "}";
if (!elseBranch.empty()) {
oss << " else {\n";
emitBody(oss, elseBranch, " ");
oss << "}";
}
return oss.str();
}
std::string visitWhileLoop(const WhileLoop* loop) override {
std::ostringstream oss;
auto condition = loop->getChild("condition");
auto body = loop->getChildren("body");
oss << "for ";
if (condition) {
oss << generate(condition);
}
oss << " {\n";
emitBody(oss, body, " ");
oss << "}";
return oss.str();
}
std::string visitForLoop(const ForLoop* loop) override {
std::ostringstream oss;
auto iterable = loop->getChild("iterable");
auto body = loop->getChildren("body");
std::string iterName = loop->iteratorName.empty() ? "item" : loop->iteratorName;
if (iterable) {
oss << "for " << iterName << " := range " << generate(iterable) << " {\n";
} else {
oss << "for {\n";
}
emitBody(oss, body, " ");
oss << "}";
return oss.str();
}
std::string visitExpressionStatement(const ExpressionStatement* stmt) override {
std::ostringstream oss;
auto expr = stmt->getChild("expression");
if (expr) {
oss << generate(expr);
}
return oss.str();
}
std::string visitUnaryOperation(const UnaryOperation* unOp) override {
std::ostringstream oss;
std::string op = unOp->op;
bool wordOp = !op.empty() && std::isalpha(static_cast<unsigned char>(op[0]));
oss << op;
if (wordOp) oss << " ";
auto operand = unOp->getChild("operand");
if (operand) {
oss << generate(operand);
} else {
oss << "nil";
}
return oss.str();
}
std::string visitFunctionCall(const FunctionCall* call) override {
std::ostringstream oss;
oss << call->functionName << "(";
auto arguments = call->getChildren("arguments");
for (size_t i = 0; i < arguments.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(arguments[i]);
}
oss << ")";
return oss.str();
}
std::string visitBlock(const Block* block) override {
std::ostringstream oss;
auto statements = block->getChildren("statements");
oss << "{\n";
emitBody(oss, statements, " ");
oss << "}";
return oss.str();
}
std::string visitListLiteral(const ListLiteral* lit) override {
std::ostringstream oss;
oss << "[]interface{}{";
auto elements = lit->getChildren("elements");
for (size_t i = 0; i < elements.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(elements[i]);
}
oss << "}";
return oss.str();
}
std::string visitIndexAccess(const IndexAccess* access) override {
std::ostringstream oss;
auto target = access->getChild("target");
auto index = access->getChild("index");
if (target) {
oss << generate(target);
} else {
oss << "/* missing target */";
}
oss << "[";
if (index) {
oss << generate(index);
} else {
oss << "0";
}
oss << "]";
return oss.str();
}
std::string visitMemberAccess(const MemberAccess* access) override {
std::ostringstream oss;
auto target = access->getChild("target");
if (target) {
oss << generate(target);
} else {
oss << "/* missing target */";
}
oss << "." << access->memberName;
return oss.str();
}
std::string visitPrimitiveType(const PrimitiveType* type) override {
std::string kind = type->kind;
if (kind == "int" || kind == "long" || kind == "short") return "int";
if (kind == "byte") return "byte";
if (kind == "float" || kind == "double") return "float64";
if (kind == "string" || kind == "char") return "string";
if (kind == "bool") return "bool";
if (kind == "void") return "";
return kind;
}
std::string visitListType(const ListType* type) override {
std::ostringstream oss;
oss << "[]";
auto elementType = type->getChild("elementType");
if (elementType) {
oss << generate(elementType);
} else {
oss << "interface{}";
}
return oss.str();
}
std::string visitSetType(const SetType* type) override {
std::ostringstream oss;
oss << "map[";
auto elementType = type->getChild("elementType");
if (elementType) {
oss << generate(elementType);
} else {
oss << "interface{}";
}
oss << "]struct{}";
return oss.str();
}
std::string visitMapType(const MapType* type) override {
std::ostringstream oss;
oss << "map[";
auto keyType = type->getChild("keyType");
auto valueType = type->getChild("valueType");
if (keyType) {
oss << generate(keyType);
} else {
oss << "interface{}";
}
oss << "]";
if (valueType) {
oss << generate(valueType);
} else {
oss << "interface{}";
}
return oss.str();
}
std::string visitTupleType(const TupleType* type) override {
std::ostringstream oss;
oss << "[]interface{}{ ";
auto elementTypes = type->getChildren("elementTypes");
for (size_t i = 0; i < elementTypes.size(); ++i) {
if (i > 0) oss << ", ";
oss << generate(elementTypes[i]);
}
oss << " }";
return oss.str();
}
std::string visitArrayType(const ArrayType* type) override {
std::ostringstream oss;
oss << "[]";
auto elementType = type->getChild("elementType");
if (elementType) {
oss << generate(elementType);
} else {
oss << "interface{}";
}
return oss.str();
}
std::string visitOptionalType(const OptionalType* type) override {
std::ostringstream oss;
oss << "*";
auto inner = type->getChild("innerType");
if (inner) {
oss << generate(inner);
} else {
oss << "interface{}";
}
return oss.str();
}
std::string visitCustomType(const CustomType* type) override {
return type->typeName;
}
std::string visitDerefStrategy(const DerefStrategy* annotation) override {
return "// @deref(" + annotation->strategy + ")";
}
std::string visitOptimizationLock(const OptimizationLock* annotation) override {
return "// @lock(" + annotation->lockedBy + ") - " + annotation->lockReason;
}
std::string visitLangSpecific(const LangSpecific* annotation) override {
return "// @lang_specific(" + annotation->language + ", " + annotation->idiomType + ")";
}
std::string visitDeallocateAnnotation(const DeallocateAnnotation* annotation) override {
if (annotation->strategy == "Explicit") {
return "// @dealloc(Explicit) - Close/Release";
}
return "// @dealloc(" + annotation->strategy + ")";
}
std::string visitLifetimeAnnotation(const LifetimeAnnotation* annotation) override {
return "// @lifetime(" + annotation->strategy + ")";
}
std::string visitReclaimAnnotation(const ReclaimAnnotation* annotation) override {
if (annotation->strategy == "Escape") {
return "// @reclaim(Escape) - escape analysis";
}
return "// @reclaim(" + annotation->strategy + ")";
}
std::string visitOwnerAnnotation(const OwnerAnnotation* annotation) override {
return "// @owner(" + annotation->strategy + ")";
}
std::string visitAllocateAnnotation(const AllocateAnnotation* annotation) override {
return "// @allocate(" + annotation->strategy + ")";
}
std::string visitHotColdAnnotation(const HotColdAnnotation* annotation) override {
return "// @" + annotation->hint;
}
std::string visitInlineAnnotation(const InlineAnnotation* annotation) override {
return "// @inline(" + annotation->mode + ")";
}
std::string visitPureAnnotation(const PureAnnotation*) override {
return "// @pure";
}
std::string visitConstExprAnnotation(const ConstExprAnnotation*) override {
return "// @constexpr";
}
private:
static std::string emitImport(const Import* imp) {
std::string moduleName = imp->moduleName.empty() ? "" : imp->moduleName;
if (!moduleName.empty() && (moduleName.front() != '"' || moduleName.back() != '"')) {
moduleName = "\"" + moduleName + "\"";
}
return moduleName.empty() ? "\"\"" : moduleName;
}
static void emitAnnotations(std::ostringstream& oss,
const std::vector<ASTNode*>& annotations,
const std::string& indent) {
for (const auto* annotation : annotations) {
std::string code;
if (annotation) {
GoGenerator gen;
code = gen.generate(annotation);
}
if (!code.empty()) {
oss << indent << code << "\n";
}
}
}
void emitParameters(std::ostringstream& oss,
const std::vector<ASTNode*>& params) {
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << visitParameter(static_cast<const Parameter*>(params[i]));
}
}
void emitBody(std::ostringstream& oss,
const std::vector<ASTNode*>& body,
const std::string& indent) {
if (body.empty()) {
oss << indent << "// TODO: implement\n";
return;
}
for (const auto* stmt : body) {
std::string stmtCode = generate(stmt);
appendIndented(oss, stmtCode, indent);
oss << "\n";
}
}
static void appendIndented(std::ostringstream& oss,
const std::string& code,
const std::string& indent) {
size_t pos = 0;
while (pos < code.length()) {
size_t newlinePos = code.find('\n', pos);
if (newlinePos == std::string::npos) {
oss << indent << code.substr(pos);
break;
}
oss << indent << code.substr(pos, newlinePos - pos) << "\n";
pos = newlinePos + 1;
if (pos >= code.length()) break;
}
}
static std::string receiverVar(const std::string& receiver) {
if (receiver.empty()) return "r";
char c = receiver[0];
if (std::isalpha(static_cast<unsigned char>(c))) {
return std::string(1, (char)std::tolower(c));
}
return "r";
}
static bool isQuotedLiteral(const std::string& text) {
if (text.size() < 2) return false;
char first = text.front();
char last = text.back();
if ((first == '\'' && last == '\'') || (first == '"' && last == '"') ||
(first == '`' && last == '`')) {
return true;
}
return false;
}
};

View File

@@ -26,6 +26,7 @@ extern "C" {
const TSLanguage* tree_sitter_typescript(); const TSLanguage* tree_sitter_typescript();
const TSLanguage* tree_sitter_java(); const TSLanguage* tree_sitter_java();
const TSLanguage* tree_sitter_rust(); const TSLanguage* tree_sitter_rust();
const TSLanguage* tree_sitter_go();
} }
// Unique ID generator for AST nodes // Unique ID generator for AST nodes
@@ -360,6 +361,49 @@ public:
return result; return result;
} }
// ---------------------------------------------------------------
// Go
// ---------------------------------------------------------------
static std::unique_ptr<Module> parseGo(const std::string& source) {
TSParser* parser = ts_parser_new();
ts_parser_set_language(parser, tree_sitter_go());
TSTree* tree = ts_parser_parse_string(parser, nullptr, source.c_str(), (uint32_t)source.size());
TSNode root = ts_tree_root_node(tree);
auto module = std::make_unique<Module>();
module->id = IdGenerator::next("mod");
module->name = "parsed_go_module";
module->targetLanguage = "go";
applySpan(module.get(), root);
convertGoSourceFile(root, source, module.get());
ts_tree_delete(tree);
ts_parser_delete(parser);
return module;
}
static ParseResult parseGoWithDiagnostics(const std::string& source) {
ParseResult result;
TSParser* parser = ts_parser_new();
ts_parser_set_language(parser, tree_sitter_go());
TSTree* tree = ts_parser_parse_string(parser, nullptr, source.c_str(), (uint32_t)source.size());
TSNode root = ts_tree_root_node(tree);
result.module = std::make_unique<Module>();
result.module->id = IdGenerator::next("mod");
result.module->name = "parsed_go_module";
result.module->targetLanguage = "go";
applySpan(result.module.get(), root);
convertGoSourceFile(root, source, result.module.get());
collectDiagnostics(root, source, result.diagnostics);
ts_tree_delete(tree);
ts_parser_delete(parser);
return result;
}
private: private:
// --------------------------------------------------------------- // ---------------------------------------------------------------
// Helpers // Helpers
@@ -2655,4 +2699,469 @@ private:
custom->typeName = nodeText(node, source); custom->typeName = nodeText(node, source);
return custom; return custom;
} }
// ---------------------------------------------------------------
// Go CST -> AST
// ---------------------------------------------------------------
static void convertGoSourceFile(TSNode root,
const std::string& source,
Module* module) {
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 == "import_declaration") {
convertGoImport(child, source, module);
} else if (type == "function_declaration") {
auto* fn = convertGoFunction(child, source, "");
if (fn) module->addChild("functions", fn);
} else if (type == "method_declaration") {
auto* fn = convertGoMethod(child, source);
if (fn) module->addChild("functions", fn);
} else if (type == "var_declaration") {
convertGoVarDeclaration(child, source, module);
} else if (type == "type_declaration") {
convertGoTypeDeclaration(child, source, module);
}
}
}
static void convertGoImport(TSNode node,
const std::string& source,
Module* module) {
uint32_t count = ts_node_named_child_count(node);
for (uint32_t i = 0; i < count; ++i) {
TSNode spec = ts_node_named_child(node, i);
if (nodeType(spec) != "import_spec") continue;
TSNode pathNode = childByFieldName(spec, "path");
std::string path = nodeText(pathNode, source);
if (!path.empty()) {
auto* imp = new Import(IdGenerator::next("imp"), path, "module");
module->addChild("imports", imp);
}
}
}
static void convertGoVarDeclaration(TSNode node,
const std::string& source,
Module* module) {
uint32_t count = ts_node_named_child_count(node);
for (uint32_t i = 0; i < count; ++i) {
TSNode spec = ts_node_named_child(node, i);
if (nodeType(spec) != "var_spec") continue;
TSNode nameNode = childByFieldName(spec, "name");
TSNode typeNode = childByFieldName(spec, "type");
TSNode valueNode = childByFieldName(spec, "value");
if (ts_node_is_null(nameNode)) continue;
auto* var = new Variable(IdGenerator::next("var"), nodeText(nameNode, source));
applySpan(var, spec);
if (!ts_node_is_null(typeNode)) {
if (auto* t = convertGoType(typeNode, source)) var->setChild("type", t);
}
if (!ts_node_is_null(valueNode)) {
ASTNode* init = convertGoExpression(valueNode, source);
if (init) var->setChild("initializer", init);
}
module->addChild("variables", var);
}
}
static void convertGoTypeDeclaration(TSNode node,
const std::string& source,
Module* module) {
uint32_t count = ts_node_named_child_count(node);
for (uint32_t i = 0; i < count; ++i) {
TSNode spec = ts_node_named_child(node, i);
if (nodeType(spec) != "type_spec") continue;
TSNode nameNode = childByFieldName(spec, "name");
if (ts_node_is_null(nameNode)) continue;
auto* var = new Variable(IdGenerator::next("var"), nodeText(nameNode, source));
module->addChild("variables", var);
}
}
static Function* convertGoFunction(TSNode node,
const std::string& source,
const std::string& receiverType) {
TSNode nameNode = childByFieldName(node, "name");
if (ts_node_is_null(nameNode)) return nullptr;
auto* fn = new Function();
fn->id = IdGenerator::next("fn");
applySpan(fn, node);
std::string name = nodeText(nameNode, source);
fn->name = receiverType.empty() ? name : (receiverType + "." + name);
TSNode paramsNode = childByFieldName(node, "parameters");
if (!ts_node_is_null(paramsNode)) {
convertGoParameters(paramsNode, source, fn);
}
TSNode resultNode = childByFieldName(node, "result");
if (!ts_node_is_null(resultNode)) {
if (auto* t = convertGoType(resultNode, source)) fn->setChild("returnType", t);
}
TSNode bodyNode = childByFieldName(node, "body");
if (!ts_node_is_null(bodyNode)) {
convertGoBlock(bodyNode, source, fn);
}
auto* reclaim = new ReclaimAnnotation(IdGenerator::next("anno"), "Escape");
fn->addChild("annotations", reclaim);
return fn;
}
static Function* convertGoMethod(TSNode node,
const std::string& source) {
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);
}
return convertGoFunction(node, source, receiverType);
}
static void convertGoParameters(TSNode paramsNode,
const std::string& source,
Function* fn) {
uint32_t count = ts_node_named_child_count(paramsNode);
for (uint32_t i = 0; i < count; ++i) {
TSNode child = ts_node_named_child(paramsNode, i);
if (nodeType(child) != "parameter_declaration" &&
nodeType(child) != "variadic_parameter_declaration") {
continue;
}
TSNode nameNode = childByFieldName(child, "name");
TSNode typeNode = childByFieldName(child, "type");
if (ts_node_is_null(nameNode)) continue;
auto* param = new Parameter(IdGenerator::next("param"), nodeText(nameNode, source));
applySpan(param, child);
if (!ts_node_is_null(typeNode)) {
if (auto* t = convertGoType(typeNode, source)) param->setChild("type", t);
}
fn->addChild("parameters", param);
}
}
static void convertGoBlock(TSNode blockNode,
const std::string& source,
Function* fn) {
uint32_t count = ts_node_named_child_count(blockNode);
for (uint32_t i = 0; i < count; ++i) {
ASTNode* stmt = convertGoStatement(ts_node_named_child(blockNode, i), source);
if (stmt) fn->addChild("body", stmt);
}
}
static ASTNode* convertGoStatement(TSNode node,
const std::string& source) {
std::string type = nodeType(node);
if (type == "return_statement") {
auto* ret = new Return();
ret->id = IdGenerator::next("ret");
applySpan(ret, node);
TSNode valueNode = childByFieldName(node, "value");
if (!ts_node_is_null(valueNode)) {
ASTNode* val = convertGoExpression(valueNode, source);
if (val) ret->setChild("value", val);
}
return ret;
} else if (type == "short_var_declaration") {
auto* assign = new Assignment();
assign->id = IdGenerator::next("assign");
applySpan(assign, node);
TSNode leftNode = childByFieldName(node, "left");
TSNode rightNode = childByFieldName(node, "right");
if (!ts_node_is_null(leftNode)) {
ASTNode* target = convertGoExpression(leftNode, source);
if (target) assign->setChild("target", target);
}
if (!ts_node_is_null(rightNode)) {
ASTNode* value = convertGoExpression(rightNode, source);
if (value) assign->setChild("value", value);
}
return assign;
} else if (type == "assignment_statement") {
auto* assign = new Assignment();
assign->id = IdGenerator::next("assign");
applySpan(assign, node);
TSNode leftNode = childByFieldName(node, "left");
TSNode rightNode = childByFieldName(node, "right");
if (!ts_node_is_null(leftNode)) {
ASTNode* target = convertGoExpression(leftNode, source);
if (target) assign->setChild("target", target);
}
if (!ts_node_is_null(rightNode)) {
ASTNode* value = convertGoExpression(rightNode, source);
if (value) assign->setChild("value", value);
}
return assign;
} else if (type == "expression_statement") {
auto* exprStmt = new ExpressionStatement();
exprStmt->id = IdGenerator::next("exprstmt");
applySpan(exprStmt, node);
if (ts_node_named_child_count(node) > 0) {
ASTNode* expr = convertGoExpression(ts_node_named_child(node, 0), source);
if (expr) exprStmt->setChild("expression", expr);
}
return exprStmt;
} else if (type == "if_statement") {
auto* ifStmt = new IfStatement();
ifStmt->id = IdGenerator::next("if");
applySpan(ifStmt, node);
TSNode condNode = childByFieldName(node, "condition");
if (!ts_node_is_null(condNode)) {
ASTNode* cond = convertGoExpression(condNode, source);
if (cond) ifStmt->setChild("condition", cond);
}
TSNode consNode = childByFieldName(node, "consequence");
if (!ts_node_is_null(consNode)) {
uint32_t cc = ts_node_named_child_count(consNode);
for (uint32_t i = 0; i < cc; ++i) {
ASTNode* s = convertGoStatement(ts_node_named_child(consNode, i), source);
if (s) ifStmt->addChild("thenBranch", s);
}
}
TSNode altNode = childByFieldName(node, "alternative");
if (!ts_node_is_null(altNode)) {
uint32_t ac = ts_node_named_child_count(altNode);
for (uint32_t i = 0; i < ac; ++i) {
ASTNode* s = convertGoStatement(ts_node_named_child(altNode, i), source);
if (s) ifStmt->addChild("elseBranch", s);
}
}
return ifStmt;
} else if (type == "for_statement") {
auto* loop = new ForLoop();
loop->id = IdGenerator::next("for");
applySpan(loop, node);
TSNode rangeNode = childByFieldName(node, "right");
if (!ts_node_is_null(rangeNode)) {
ASTNode* iter = convertGoExpression(rangeNode, source);
if (iter) loop->setChild("iterable", iter);
}
TSNode bodyNode = childByFieldName(node, "body");
if (!ts_node_is_null(bodyNode)) {
uint32_t bc = ts_node_named_child_count(bodyNode);
for (uint32_t i = 0; i < bc; ++i) {
ASTNode* s = convertGoStatement(ts_node_named_child(bodyNode, i), source);
if (s) loop->addChild("body", s);
}
}
return loop;
} else if (type == "block") {
auto* block = new Block();
block->id = IdGenerator::next("block");
applySpan(block, node);
uint32_t bc = ts_node_named_child_count(node);
for (uint32_t i = 0; i < bc; ++i) {
ASTNode* s = convertGoStatement(ts_node_named_child(node, i), source);
if (s) block->addChild("statements", s);
}
return block;
}
ASTNode* expr = convertGoExpression(node, source);
if (expr) {
auto* exprStmt = new ExpressionStatement();
exprStmt->id = IdGenerator::next("exprstmt");
applySpan(exprStmt, node);
exprStmt->setChild("expression", expr);
return exprStmt;
}
return nullptr;
}
static ASTNode* convertGoExpression(TSNode node,
const std::string& source) {
std::string type = nodeType(node);
if (type == "binary_expression") {
auto* binOp = new BinaryOperation();
binOp->id = IdGenerator::next("binop");
applySpan(binOp, node);
TSNode leftNode = childByFieldName(node, "left");
TSNode rightNode = childByFieldName(node, "right");
TSNode opNode = childByFieldName(node, "operator");
if (!ts_node_is_null(opNode)) binOp->op = nodeText(opNode, source);
if (!ts_node_is_null(leftNode)) {
ASTNode* left = convertGoExpression(leftNode, source);
if (left) binOp->setChild("left", left);
}
if (!ts_node_is_null(rightNode)) {
ASTNode* right = convertGoExpression(rightNode, source);
if (right) binOp->setChild("right", right);
}
return binOp;
} else if (type == "call_expression") {
auto* call = new FunctionCall();
call->id = IdGenerator::next("call");
applySpan(call, node);
TSNode funcNode = childByFieldName(node, "function");
if (!ts_node_is_null(funcNode)) {
call->functionName = nodeText(funcNode, source);
}
TSNode argsNode = childByFieldName(node, "arguments");
if (!ts_node_is_null(argsNode)) {
uint32_t count = ts_node_named_child_count(argsNode);
for (uint32_t i = 0; i < count; ++i) {
ASTNode* arg = convertGoExpression(ts_node_named_child(argsNode, i), source);
if (arg) call->addChild("arguments", arg);
}
}
return call;
} else if (type == "selector_expression") {
auto* mem = new MemberAccess();
mem->id = IdGenerator::next("member");
applySpan(mem, node);
TSNode operandNode = childByFieldName(node, "operand");
TSNode fieldNode = childByFieldName(node, "field");
if (!ts_node_is_null(fieldNode)) {
mem->memberName = nodeText(fieldNode, source);
}
if (!ts_node_is_null(operandNode)) {
ASTNode* target = convertGoExpression(operandNode, source);
if (target) mem->setChild("target", target);
}
return mem;
} else if (type == "index_expression") {
auto* access = new IndexAccess();
access->id = IdGenerator::next("index");
applySpan(access, node);
TSNode operandNode = childByFieldName(node, "operand");
TSNode indexNode = childByFieldName(node, "index");
if (!ts_node_is_null(operandNode)) {
ASTNode* target = convertGoExpression(operandNode, source);
if (target) access->setChild("target", target);
}
if (!ts_node_is_null(indexNode)) {
ASTNode* idx = convertGoExpression(indexNode, source);
if (idx) access->setChild("index", idx);
}
return access;
} else if (type == "identifier") {
auto* ref = new VariableReference(IdGenerator::next("var"), nodeText(node, source));
applySpan(ref, node);
return ref;
} else if (type == "int_literal") {
std::string text = nodeText(node, source);
int val = 0;
try { val = std::stoi(text); } catch (...) {}
auto* lit = new IntegerLiteral(IdGenerator::next("int"), val);
applySpan(lit, node);
return lit;
} else if (type == "float_literal") {
auto* lit = new FloatLiteral(IdGenerator::next("float"), nodeText(node, source));
applySpan(lit, node);
return lit;
} else if (type == "raw_string_literal" || type == "interpreted_string_literal") {
auto* lit = new StringLiteral(IdGenerator::next("str"), nodeText(node, source));
applySpan(lit, node);
return lit;
} else if (type == "true" || type == "false") {
auto* lit = new BooleanLiteral(IdGenerator::next("bool"), type == "true");
applySpan(lit, node);
return lit;
} else if (type == "nil") {
auto* lit = new NullLiteral();
lit->id = IdGenerator::next("null");
applySpan(lit, node);
return lit;
} else if (type == "parenthesized_expression") {
if (ts_node_named_child_count(node) > 0) {
return convertGoExpression(ts_node_named_child(node, 0), source);
}
}
std::string text = nodeText(node, source);
if (!text.empty()) {
auto* ref = new VariableReference(IdGenerator::next("var"), text);
applySpan(ref, node);
return ref;
}
return nullptr;
}
static Type* convertGoType(TSNode node, const std::string& source) {
std::string type = nodeType(node);
if (type == "qualified_type") {
auto* custom = new CustomType();
custom->id = IdGenerator::next("type");
custom->typeName = nodeText(node, source);
return custom;
} else if (type == "pointer_type") {
TSNode elemNode = childByFieldName(node, "type");
if (!ts_node_is_null(elemNode)) {
if (auto* inner = convertGoType(elemNode, source)) {
auto* opt = new OptionalType();
opt->id = IdGenerator::next("type");
opt->setChild("innerType", inner);
return opt;
}
}
} else if (type == "array_type" || type == "slice_type") {
auto* arr = new ArrayType();
arr->id = IdGenerator::next("type");
TSNode elemNode = childByFieldName(node, "element");
if (!ts_node_is_null(elemNode)) {
if (auto* et = convertGoType(elemNode, source)) arr->setChild("elementType", et);
}
return arr;
} else if (type == "map_type") {
auto* map = new MapType();
map->id = IdGenerator::next("type");
TSNode keyNode = childByFieldName(node, "key");
TSNode valNode = childByFieldName(node, "value");
if (!ts_node_is_null(keyNode)) {
if (auto* kt = convertGoType(keyNode, source)) map->setChild("keyType", kt);
}
if (!ts_node_is_null(valNode)) {
if (auto* vt = convertGoType(valNode, source)) map->setChild("valueType", vt);
}
return map;
} else if (type == "struct_type") {
auto* custom = new CustomType();
custom->id = IdGenerator::next("type");
custom->typeName = "struct";
return custom;
} else if (type == "interface_type") {
auto* custom = new CustomType();
custom->id = IdGenerator::next("type");
custom->typeName = "interface{}";
return custom;
}
auto* custom = new CustomType();
custom->id = IdGenerator::next("type");
custom->typeName = nodeText(node, source);
return custom;
}
static TSNode findDescendantByField(TSNode node, const std::string& fieldName) {
TSNode child = childByFieldName(node, fieldName.c_str());
if (!ts_node_is_null(child)) return child;
uint32_t count = ts_node_named_child_count(node);
for (uint32_t i = 0; i < count; ++i) {
TSNode inner = ts_node_named_child(node, i);
TSNode found = findDescendantByField(inner, fieldName);
if (!ts_node_is_null(found)) return found;
}
return TSNode{};
}
static std::string trimPointerPrefix(const std::string& text) {
if (text.empty()) return text;
std::string out = text;
size_t pos = out.find('*');
if (pos != std::string::npos) {
out.erase(pos, 1);
}
return out;
}
}; };

View File

@@ -0,0 +1,69 @@
// Step 151 TDD Test: Go CST-to-AST + generator
#include "ast/Parser.h"
#include "ast/Generator.h"
#include <iostream>
static void expect(bool cond, const std::string& name, int& passed, int& failed) {
if (cond) {
std::cout << "Test " << (passed + failed + 1) << " PASS: " << name << "\n";
++passed;
} else {
std::cout << "Test " << (passed + failed + 1) << " FAIL: " << name << "\n";
++failed;
}
}
int main() {
int passed = 0;
int failed = 0;
std::string go = R"(
package main
import "fmt"
type Box struct {
value int
}
func (b *Box) Size(n int) int {
return n + 1
}
func Add(a int, b int) int {
c := a + b
return c
}
)";
auto mod = TreeSitterParser::parseGo(go);
expect(mod != nullptr, "go module created", passed, failed);
auto funcs = mod->getChildren("functions");
expect(funcs.size() >= 2, "go functions discovered", passed, failed);
bool foundAdd = false;
bool foundMethod = false;
for (const auto* fnNode : funcs) {
if (fnNode->conceptType != "Function") continue;
auto* fn = static_cast<const Function*>(fnNode);
if (fn->name == "Add") foundAdd = true;
if (fn->name.find("Box.") == 0) foundMethod = true;
}
expect(foundAdd, "go top-level function name", passed, failed);
expect(foundMethod, "go method name with receiver", passed, failed);
GoGenerator gen;
std::string out = gen.generate(mod.get());
expect(out.find("package") != std::string::npos,
"go package generated", passed, failed);
expect(out.find("import") != std::string::npos,
"go imports generated", passed, failed);
expect(out.find("func Add") != std::string::npos,
"go function signature generated", passed, failed);
expect(out.find("func (b *Box)") != std::string::npos,
"go method receiver generated", passed, failed);
std::cout << "\n=== Step 151 Results: " << passed << " passed, "
<< failed << " failed ===\n";
return failed == 0 ? 0 : 1;
}

View File

@@ -259,7 +259,7 @@ adds full semantic support.
@Reclaim(Tracing) → Rc<T> with caution note. @Reclaim(Tracing) → Rc<T> with caution note.
*Modifies:* `Parser.h`, `Generator.h` *Modifies:* `Parser.h`, `Generator.h`
- [ ] **Step 151: Go CST-to-AST and generator** - [x] **Step 151: Go CST-to-AST and generator**
Full tree-sitter Go parsing: functions, structs, interfaces, goroutines, Full tree-sitter Go parsing: functions, structs, interfaces, goroutines,
channels, defer, multiple return values. Generator produces Go with channels, defer, multiple return values. Generator produces Go with
proper package/import structure. Memory annotations map to: @Reclaim(Escape) proper package/import structure. Memory annotations map to: @Reclaim(Escape)