Implement Sprint 169 pipeline strictness and add Sprint 170-171 plans

This commit is contained in:
Bill
2026-02-25 18:20:39 -07:00
parent 13b57bbd65
commit f1c6214de8
30 changed files with 1894 additions and 74 deletions

View File

@@ -129,7 +129,10 @@
}
}
}
oss << "class " << cls->name;
auto methods = cls->getChildren("methods");
const bool useStruct = methods.empty();
oss << (useStruct ? "struct " : "class ") << cls->name;
auto bases = cls->getBases();
if (!bases.empty()) {
oss << " : ";
@@ -140,16 +143,170 @@
oss << " " << bases[i].name;
}
}
oss << " {\npublic:\n";
oss << " {\n";
if (!useStruct) oss << "public:\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);
for (const auto* f : fields) {
auto* var = static_cast<const Variable*>(f);
oss << " " << inferFieldType(var) << " " << var->name << ";\n";
}
if (!methods.empty() && !fields.empty()) oss << "\n";
for (const auto* m : methods) {
auto* meth = static_cast<const MethodDeclaration*>(m);
oss << " ";
if (meth->isStatic) oss << "static ";
std::string methodName = meth->name;
auto dot = methodName.find_last_of('.');
if (dot != std::string::npos && dot + 1 < methodName.size()) {
methodName = methodName.substr(dot + 1);
}
const bool isCtor = (methodName == "__init__" || methodName == "constructor" || methodName == cls->name);
if (isCtor) {
oss << cls->name;
} else {
oss << inferReturnType(meth) << " " << methodName;
}
oss << "(";
auto params = meth->getChildren("parameters");
bool firstParam = true;
for (size_t i = 0; i < params.size(); ++i) {
auto* param = static_cast<const Parameter*>(params[i]);
if (!param) continue;
if (param->name == "self" || param->name == "cls") continue;
if (!firstParam) oss << ", ";
firstParam = false;
oss << inferParameterType(param, meth) << " " << param->name;
}
oss << ");\n";
}
oss << "};\n";
return oss.str();
}
static std::string mapScalarTypeName(const std::string& rawType) {
std::string kind = rawType;
std::transform(kind.begin(), kind.end(), kind.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (kind == "string" || kind == "str") return "std::string";
if (kind == "int" || kind == "integer") return "int";
if (kind == "bool" || kind == "boolean") return "bool";
if (kind == "float" || kind == "double") return "double";
return rawType;
}
static std::string inferFieldType(const Variable* var) {
if (!var) return "auto /* TODO: specify type */";
auto* type = var->getChild("type");
if (type && type->conceptType == "PrimitiveType") {
return mapScalarTypeName(static_cast<const PrimitiveType*>(type)->kind);
}
if (type && type->conceptType == "CustomType") {
return mapScalarTypeName(static_cast<const CustomType*>(type)->typeName);
}
if (type && type->conceptType == "ListType") {
auto* elem = type->getChild("elementType");
if (!elem) return "std::vector<std::string>";
if (elem->conceptType == "PrimitiveType") {
return "std::vector<" + mapScalarTypeName(static_cast<const PrimitiveType*>(elem)->kind) + ">";
}
if (elem->conceptType == "CustomType") {
return "std::vector<" + mapScalarTypeName(static_cast<const CustomType*>(elem)->typeName) + ">";
}
return "std::vector<std::string>";
}
auto* init = var->getChild("initializer");
if (init) {
if (init->conceptType == "StringLiteral") return "std::string";
if (init->conceptType == "IntegerLiteral") return "int";
if (init->conceptType == "BooleanLiteral") return "bool";
if (init->conceptType == "FloatLiteral") return "double";
}
std::string lower = var->name;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower.find("name") != std::string::npos ||
lower.find("id") != std::string::npos ||
lower.find("payload") != std::string::npos) {
return "std::string";
}
if (lower.find("priority") != std::string::npos ||
lower.find("count") != std::string::npos ||
lower.find("size") != std::string::npos ||
lower.find("index") != std::string::npos) {
return "int";
}
return "auto /* TODO: specify type */";
}
static std::string inferReturnType(const MethodDeclaration* meth) {
if (!meth) return "auto";
auto* ret = meth->getChild("returnType");
if (!ret) {
std::string lower = meth->name;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower.find("enqueue") != std::string::npos || lower.find("push") != std::string::npos) {
return "void";
}
if (lower.find("dequeue") != std::string::npos || lower.find("peek") != std::string::npos ||
lower.find("pop") != std::string::npos) {
return "std::string";
}
if (lower.find("size") != std::string::npos) return "int";
if (lower.find("empty") != std::string::npos) return "bool";
return "void";
}
if (ret->conceptType == "PrimitiveType") {
return mapScalarTypeName(static_cast<const PrimitiveType*>(ret)->kind);
}
if (ret->conceptType == "CustomType") {
return mapScalarTypeName(static_cast<const CustomType*>(ret)->typeName);
}
return "auto";
}
static std::string inferParameterType(const Parameter* param,
const MethodDeclaration* method = nullptr) {
if (!param) return "auto";
auto* type = param->getChild("type");
if (type && type->conceptType == "PrimitiveType") {
return mapScalarTypeName(static_cast<const PrimitiveType*>(type)->kind);
}
if (type && type->conceptType == "CustomType") {
return mapScalarTypeName(static_cast<const CustomType*>(type)->typeName);
}
std::string lower = param->name;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower.find("name") != std::string::npos ||
lower.find("id") != std::string::npos ||
lower.find("payload") != std::string::npos) {
return "std::string";
}
if (lower.find("priority") != std::string::npos ||
lower.find("count") != std::string::npos ||
lower.find("size") != std::string::npos ||
lower.find("index") != std::string::npos) {
return "int";
}
if (method) {
std::string m = method->name;
std::transform(m.begin(), m.end(), m.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if ((m.find("enqueue") != std::string::npos ||
m.find("push") != std::string::npos) && lower == "item") {
return "WorkItem";
}
}
return "auto";
}
std::string visitInterfaceDeclaration(const ASTNode* node) override {
auto* iface = static_cast<const InterfaceDeclaration*>(node);
std::ostringstream oss;
@@ -168,11 +325,16 @@
oss << " ";
if (meth->isStatic) oss << "static ";
if (meth->isVirtual) oss << "virtual ";
oss << "void " << meth->name << "(";
oss << inferReturnType(meth) << " " << meth->name << "(";
auto params = meth->getChildren("parameters");
bool firstParam = true;
for (size_t i = 0; i < params.size(); ++i) {
if (i > 0) oss << ", ";
oss << visitParameter(static_cast<const Parameter*>(params[i]));
auto* param = static_cast<const Parameter*>(params[i]);
if (!param) continue;
if (param->name == "self" || param->name == "cls") continue;
if (!firstParam) oss << ", ";
firstParam = false;
oss << inferParameterType(param, meth) << " " << param->name;
}
oss << ")";
if (meth->isOverride) oss << " override";

View File

@@ -5,6 +5,8 @@
#include "GenericType.h"
#include "AsyncNodes.h"
#include "../SemannoAnnotationImpl.h"
#include <algorithm>
#include <cctype>
class GoGenerator : public ProjectionGenerator, public SemannoAnnotationImpl<GoGenerator> {
public:
@@ -41,6 +43,14 @@ public:
if (i > 0) oss << "\n";
oss << visitFunction(static_cast<const Function*>(functions[i]));
}
auto classes = module->getChildren("classes");
if (!classes.empty() && !functions.empty()) oss << "\n";
for (const auto* cls : classes) {
std::string classCode = generate(cls);
oss << classCode;
if (classCode.empty() || classCode.back() != '\n') oss << "\n";
}
return oss.str();
}
@@ -447,12 +457,38 @@ public:
oss << "(" << receiverVar(meth->className) << " *" << meth->className << ") ";
}
oss << meth->name << "(";
emitParameters(oss, meth->getChildren("parameters"));
auto params = meth->getChildren("parameters");
bool first = true;
for (const auto* pNode : params) {
auto* p = static_cast<const Parameter*>(pNode);
if (!p) continue;
if (p->name == "self" || p->name == "cls") continue;
if (!first) oss << ", ";
first = false;
oss << visitParameter(p);
}
oss << ")";
auto retType = meth->getChild("returnType");
if (retType) oss << " " << generate(retType);
oss << " {\n";
emitBody(oss, meth->getChildren("body"), " ");
auto body = meth->getChildren("body");
if (body.empty()) {
std::string lower = meth->name;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower.find("empty") != std::string::npos) {
oss << " return false\n";
} else if (lower.find("size") != std::string::npos) {
oss << " return 0\n";
} else if (retType) {
std::string ret = generate(retType);
if (ret == "bool") oss << " return false\n";
else if (ret == "int" || ret == "int32" || ret == "int64") oss << " return 0\n";
else if (ret == "string") oss << " return \"\"\n";
}
} else {
emitBody(oss, body, " ");
}
oss << "}\n";
return oss.str();
}

View File

@@ -26,57 +26,22 @@ public:
}
if (!imports.empty()) oss << "\n";
// Group functions and variables by class prefix.
std::map<std::string, std::vector<const Function*>> classMethods;
std::map<std::string, std::vector<const Variable*>> classFields;
std::vector<const Function*> topLevelMethods;
std::vector<const Variable*> topLevelFields;
auto variables = module->getChildren("variables");
for (const auto* var : variables) {
oss << "Object " << static_cast<const Variable*>(var)->name << ";\n";
}
if (!variables.empty()) oss << "\n";
auto functions = module->getChildren("functions");
for (const auto* fnNode : functions) {
if (fnNode->conceptType != "Function") continue;
const Function* fn = static_cast<const Function*>(fnNode);
auto pos = fn->name.find('.');
if (pos != std::string::npos && pos > 0 && pos + 1 < fn->name.size()) {
std::string className = fn->name.substr(0, pos);
classMethods[className].push_back(fn);
} else {
topLevelMethods.push_back(fn);
}
for (size_t i = 0; i < functions.size(); ++i) {
if (i > 0) oss << "\n";
oss << visitFunction(static_cast<const Function*>(functions[i]));
}
auto variables = module->getChildren("variables");
for (const auto* varNode : variables) {
if (varNode->conceptType != "Variable") continue;
const Variable* var = static_cast<const Variable*>(varNode);
auto pos = var->name.find('.');
if (pos != std::string::npos && pos > 0 && pos + 1 < var->name.size()) {
std::string className = var->name.substr(0, pos);
classFields[className].push_back(var);
} else {
topLevelFields.push_back(var);
}
}
std::string moduleClass = module->name.empty() ? "Main" : module->name;
if (!topLevelMethods.empty() || !topLevelFields.empty()) {
emitClass(oss, moduleClass, topLevelFields, topLevelMethods);
if (!classMethods.empty() || !classFields.empty()) oss << "\n";
}
for (const auto& [className, methods] : classMethods) {
std::vector<const Variable*> fields;
auto it = classFields.find(className);
if (it != classFields.end()) fields = it->second;
emitClass(oss, className, fields, methods);
oss << "\n";
}
if (!classFields.empty() && classMethods.empty()) {
for (const auto& [className, fields] : classFields) {
emitClass(oss, className, fields, {});
oss << "\n";
}
auto classes = module->getChildren("classes");
if (!classes.empty() && !functions.empty()) oss << "\n";
for (const auto* cls : classes) {
oss << generate(cls) << "\n";
}
return oss.str();
@@ -507,12 +472,32 @@ public:
auto retType = meth->getChild("returnType");
if (retType) returnType = generate(retType);
oss << returnType << " " << meth->name << "(";
emitParameters(oss, meth->getChildren("parameters"));
auto params = meth->getChildren("parameters");
bool first = true;
for (const auto* pNode : params) {
auto* p = static_cast<const Parameter*>(pNode);
if (!p) continue;
if (p->name == "self" || p->name == "cls") continue;
if (!first) oss << ", ";
first = false;
oss << visitParameter(p);
}
oss << ")";
if (meth->isOverride) oss << " /* @Override */";
auto body = meth->getChildren("body");
if (body.empty()) {
oss << " {}\n";
oss << " {\n";
if (returnType == "boolean") {
oss << " return false;\n";
} else if (returnType == "int" || returnType == "long" || returnType == "double" ||
returnType == "float" || returnType == "short") {
oss << " return 0;\n";
} else if (returnType == "String") {
oss << " return \"\";\n";
} else if (returnType != "void") {
oss << " return null;\n";
}
oss << " }\n";
} else {
oss << " {\n";
emitBody(oss, body, " ");

View File

@@ -19,6 +19,8 @@
#include <vector>
#include <atomic>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <tree_sitter/api.h>

View File

@@ -94,6 +94,7 @@ private:
if (!ts_node_is_null(paramsNode)) {
convertPythonParameters(paramsNode, source, fn);
}
attachPythonReturnType(node, source, fn);
// Body
TSNode bodyNode = childByFieldName(node, "body");
@@ -133,7 +134,10 @@ private:
std::string type = nodeType(child);
if (type == "function_definition") {
auto* meth = convertPythonMethodDecl(child, source, cls->name);
if (meth) cls->addChild("methods", meth);
if (meth) {
cls->addChild("methods", meth);
maybeMaterializeClassFieldsFromInit(cls, meth);
}
} else if (type == "decorated_definition") {
TSNode defNode = childByFieldName(child, "definition");
if (!ts_node_is_null(defNode) && nodeType(defNode) == "function_definition") {
@@ -145,10 +149,17 @@ private:
TSNode dChild = ts_node_named_child(child, j);
if (nodeType(dChild) == "decorator") {
auto* dec = convertPythonDecoratorNode(dChild, source);
if (dec) meth->addChild("annotations", dec);
if (dec) {
if (dec->name == "staticmethod" ||
dec->name == "classmethod") {
meth->isStatic = true;
}
meth->addChild("annotations", dec);
}
}
}
cls->addChild("methods", meth);
maybeMaterializeClassFieldsFromInit(cls, meth);
}
}
}
@@ -172,6 +183,8 @@ private:
if (!ts_node_is_null(paramsNode)) {
convertPythonParameters(paramsNode, source, meth);
}
attachPythonReturnType(node, source, meth);
dropPythonReceiverParameter(meth);
// Body
TSNode bodyNode = childByFieldName(node, "body");
@@ -259,8 +272,27 @@ private:
}
fn->addChild("parameters", param);
}
} else if (type == "typed_parameter" || type == "typed_default_parameter") {
TSNode nameN = childByFieldName(child, "name");
if (ts_node_is_null(nameN)) nameN = childByFieldName(child, "parameter");
TSNode typeN = childByFieldName(child, "type");
TSNode valueN = childByFieldName(child, "value");
if (!ts_node_is_null(nameN)) {
auto* param = new Parameter(IdGenerator::next("param"), nodeText(nameN, source));
applySpan(param, child);
if (!ts_node_is_null(typeN)) {
ASTNode* parsedType = parsePythonTypeNode(typeN, source);
if (parsedType) param->setChild("type", parsedType);
}
if (!ts_node_is_null(valueN)) {
ASTNode* defVal = convertPythonExpression(valueN, source);
if (defVal) param->setChild("defaultValue", defVal);
}
fn->addChild("parameters", param);
}
}
}
convertPythonParametersFromText(paramsNode, source, fn);
}
static void convertPythonBody(TSNode bodyNode, const std::string& source, Function* fn) {
@@ -373,6 +405,22 @@ private:
auto* ref = new VariableReference(IdGenerator::next("var"), nodeText(node, source));
applySpan(ref, node);
return ref;
} else if (type == "attribute") {
auto* acc = new MemberAccess();
acc->id = IdGenerator::next("member");
applySpan(acc, node);
TSNode objectNode = childByFieldName(node, "object");
TSNode attributeNode = childByFieldName(node, "attribute");
if (!ts_node_is_null(attributeNode)) {
acc->memberName = nodeText(attributeNode, source);
} else {
acc->memberName = nodeText(node, source);
}
if (!ts_node_is_null(objectNode)) {
ASTNode* target = convertPythonExpression(objectNode, source);
if (target) acc->setChild("target", target);
}
return acc;
} else if (type == "integer") {
std::string text = nodeText(node, source);
int val = 0;
@@ -424,6 +472,16 @@ private:
}
}
return call;
} else if (type == "list") {
auto* list = new ListLiteral();
list->id = IdGenerator::next("list");
applySpan(list, node);
uint32_t count = ts_node_named_child_count(node);
for (uint32_t i = 0; i < count; ++i) {
ASTNode* elem = convertPythonExpression(ts_node_named_child(node, i), source);
if (elem) list->addChild("elements", elem);
}
return list;
} else if (type == "assignment") {
auto* assign = new Assignment();
assign->id = IdGenerator::next("assign");
@@ -502,4 +560,306 @@ private:
return nullptr;
}
static bool isPythonSelfName(const std::string& name) {
return name == "self" || name == "cls";
}
static void dropPythonReceiverParameter(MethodDeclaration* meth) {
if (!meth || meth->isStatic) return;
const auto& params = meth->getChildren("parameters");
if (params.empty()) return;
auto* first = static_cast<Parameter*>(params.front());
if (!first || !isPythonSelfName(first->name)) return;
// Remove synthetic receiver from method parameter list for target languages.
meth->removeChild(first);
delete first;
}
static ASTNode* parsePythonTypeNode(TSNode typeNode, const std::string& source) {
if (ts_node_is_null(typeNode)) return nullptr;
std::string raw = nodeText(typeNode, source);
if (raw.empty()) return nullptr;
std::string lower = raw;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower == "str" || lower == "string") return new PrimitiveType(IdGenerator::next("type"), "string");
if (lower == "int" || lower == "integer") return new PrimitiveType(IdGenerator::next("type"), "int");
if (lower == "float" || lower == "double") return new PrimitiveType(IdGenerator::next("type"), "double");
if (lower == "bool" || lower == "boolean") return new PrimitiveType(IdGenerator::next("type"), "bool");
if (lower == "none" || lower == "void") return new PrimitiveType(IdGenerator::next("type"), "void");
if (lower == "list") {
auto* list = new ListType();
list->id = IdGenerator::next("type");
return list;
}
if (lower.rfind("list[", 0) == 0 && lower.back() == ']') {
auto* list = new ListType();
list->id = IdGenerator::next("type");
std::string elemRaw = raw.substr(5, raw.size() - 6);
std::string elemLower = elemRaw;
std::transform(elemLower.begin(), elemLower.end(), elemLower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
ASTNode* elemType = nullptr;
if (elemLower == "str" || elemLower == "string") {
elemType = new PrimitiveType(IdGenerator::next("type"), "string");
} else if (elemLower == "int" || elemLower == "integer") {
elemType = new PrimitiveType(IdGenerator::next("type"), "int");
} else if (elemLower == "float" || elemLower == "double") {
elemType = new PrimitiveType(IdGenerator::next("type"), "double");
} else if (elemLower == "bool" || elemLower == "boolean") {
elemType = new PrimitiveType(IdGenerator::next("type"), "bool");
} else {
elemType = new CustomType(IdGenerator::next("type"), elemRaw);
}
list->setChild("elementType", elemType);
return list;
}
return new CustomType(IdGenerator::next("type"), raw);
}
static ASTNode* parsePythonTypeText(const std::string& rawIn) {
std::string raw = trimCopy(rawIn);
if (raw.empty()) return nullptr;
std::string lower = raw;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower == "str" || lower == "string") return new PrimitiveType(IdGenerator::next("type"), "string");
if (lower == "int" || lower == "integer") return new PrimitiveType(IdGenerator::next("type"), "int");
if (lower == "float" || lower == "double") return new PrimitiveType(IdGenerator::next("type"), "double");
if (lower == "bool" || lower == "boolean") return new PrimitiveType(IdGenerator::next("type"), "bool");
if (lower == "none" || lower == "void") return new PrimitiveType(IdGenerator::next("type"), "void");
if (lower == "list") {
auto* list = new ListType();
list->id = IdGenerator::next("type");
return list;
}
if (lower.rfind("list[", 0) == 0 && lower.back() == ']') {
auto* list = new ListType();
list->id = IdGenerator::next("type");
std::string elemRaw = raw.substr(5, raw.size() - 6);
ASTNode* elemType = parsePythonTypeText(elemRaw);
if (elemType) list->setChild("elementType", elemType);
return list;
}
return new CustomType(IdGenerator::next("type"), raw);
}
static ASTNode* cloneTypeNode(const ASTNode* typeNode) {
if (!typeNode) return nullptr;
if (typeNode->conceptType == "PrimitiveType") {
auto* t = static_cast<const PrimitiveType*>(typeNode);
return new PrimitiveType(IdGenerator::next("type"), t->kind);
}
if (typeNode->conceptType == "CustomType") {
auto* t = static_cast<const CustomType*>(typeNode);
return new CustomType(IdGenerator::next("type"), t->typeName);
}
if (typeNode->conceptType == "ListType") {
auto* out = new ListType();
out->id = IdGenerator::next("type");
auto* elem = typeNode->getChild("elementType");
if (elem) {
ASTNode* c = cloneTypeNode(elem);
if (c) out->setChild("elementType", c);
}
return out;
}
return nullptr;
}
static void attachPythonReturnType(TSNode node, const std::string& source, Function* fn) {
if (!fn) return;
TSNode returnType = childByFieldName(node, "return_type");
ASTNode* parsedType = nullptr;
if (!ts_node_is_null(returnType)) {
parsedType = parsePythonTypeNode(returnType, source);
}
if (!parsedType) {
std::string signature = nodeText(node, source);
size_t arrowPos = signature.find("->");
if (arrowPos != std::string::npos) {
size_t start = arrowPos + 2;
size_t end = signature.find(':', start);
if (end != std::string::npos && end > start) {
parsedType = parsePythonTypeText(signature.substr(start, end - start));
}
}
}
if (parsedType) fn->setChild("returnType", parsedType);
}
static std::string inferSelfFieldName(const ASTNode* target) {
if (!target) return "";
if (target->conceptType == "MemberAccess") {
auto* access = static_cast<const MemberAccess*>(target);
auto* base = access->getChild("target");
if (base && base->conceptType == "VariableReference" &&
static_cast<const VariableReference*>(base)->variableName == "self") {
return access->memberName;
}
}
if (target->conceptType == "VariableReference") {
std::string name = static_cast<const VariableReference*>(target)->variableName;
if (name.rfind("self.", 0) == 0 && name.size() > 5) {
return name.substr(5);
}
}
return "";
}
static ASTNode* inferFieldTypeFromValue(const ASTNode* value, const MethodDeclaration* initMethod) {
if (!value) return nullptr;
if (value->conceptType == "IntegerLiteral") return new PrimitiveType(IdGenerator::next("type"), "int");
if (value->conceptType == "StringLiteral") return new PrimitiveType(IdGenerator::next("type"), "string");
if (value->conceptType == "FloatLiteral") return new PrimitiveType(IdGenerator::next("type"), "double");
if (value->conceptType == "BooleanLiteral") return new PrimitiveType(IdGenerator::next("type"), "bool");
if (value->conceptType == "VariableReference" && initMethod) {
std::string rhs = static_cast<const VariableReference*>(value)->variableName;
for (auto* pNode : initMethod->getChildren("parameters")) {
auto* p = static_cast<const Parameter*>(pNode);
if (p && p->name == rhs) {
ASTNode* cloned = cloneTypeNode(p->getChild("type"));
if (cloned) return cloned;
}
}
}
if (value->conceptType == "ListLiteral") {
auto* listType = new ListType();
listType->id = IdGenerator::next("type");
return listType;
}
return nullptr;
}
static bool classHasField(const ClassDeclaration* cls, const std::string& name) {
if (!cls) return false;
for (auto* fieldNode : cls->getChildren("fields")) {
if (fieldNode->conceptType != "Variable") continue;
auto* field = static_cast<const Variable*>(fieldNode);
if (field->name == name) return true;
}
return false;
}
static std::string trimCopy(const std::string& in) {
size_t start = 0;
size_t end = in.size();
while (start < end && std::isspace(static_cast<unsigned char>(in[start]))) ++start;
while (end > start && std::isspace(static_cast<unsigned char>(in[end - 1]))) --end;
return in.substr(start, end - start);
}
static ASTNode* parseSimpleDefaultValue(const std::string& rawValue) {
std::string v = trimCopy(rawValue);
if (v.empty()) return nullptr;
if ((v.front() == '\'' && v.back() == '\'') || (v.front() == '"' && v.back() == '"')) {
return new StringLiteral(IdGenerator::next("str"), v);
}
if (v == "True") return new BooleanLiteral(IdGenerator::next("bool"), true);
if (v == "False") return new BooleanLiteral(IdGenerator::next("bool"), false);
bool isInt = !v.empty();
for (char c : v) {
if (!(c == '-' || (c >= '0' && c <= '9'))) {
isInt = false;
break;
}
}
if (isInt) {
int val = 0;
try { val = std::stoi(v); } catch (...) {}
return new IntegerLiteral(IdGenerator::next("int"), val);
}
return nullptr;
}
static std::vector<std::string> splitPythonParameterList(const std::string& body) {
std::vector<std::string> out;
std::string cur;
int bracketDepth = 0;
for (char c : body) {
if (c == '[' || c == '(' || c == '{') ++bracketDepth;
if (c == ']' || c == ')' || c == '}') --bracketDepth;
if (c == ',' && bracketDepth == 0) {
out.push_back(trimCopy(cur));
cur.clear();
} else {
cur.push_back(c);
}
}
if (!trimCopy(cur).empty()) out.push_back(trimCopy(cur));
return out;
}
static void convertPythonParametersFromText(TSNode paramsNode,
const std::string& source,
Function* fn) {
if (!fn) return;
std::string text = trimCopy(nodeText(paramsNode, source));
if (text.size() < 2 || text.front() != '(' || text.back() != ')') return;
text = text.substr(1, text.size() - 2);
auto tokens = splitPythonParameterList(text);
for (const auto& rawToken : tokens) {
std::string token = trimCopy(rawToken);
if (token.empty() || token == "/" || token == "*") continue;
if (token.rfind("**", 0) == 0 || token.rfind("*", 0) == 0) continue;
std::string namePart = token;
std::string typePart;
std::string defaultPart;
size_t eqPos = token.find('=');
if (eqPos != std::string::npos) {
namePart = trimCopy(token.substr(0, eqPos));
defaultPart = trimCopy(token.substr(eqPos + 1));
}
size_t colonPos = namePart.find(':');
if (colonPos != std::string::npos) {
typePart = trimCopy(namePart.substr(colonPos + 1));
namePart = trimCopy(namePart.substr(0, colonPos));
}
if (namePart.empty()) continue;
bool alreadyPresent = false;
for (auto* existingNode : fn->getChildren("parameters")) {
auto* existing = static_cast<const Parameter*>(existingNode);
if (existing && existing->name == namePart) {
alreadyPresent = true;
break;
}
}
if (alreadyPresent) continue;
auto* param = new Parameter(IdGenerator::next("param"), namePart);
ASTNode* parsedType = parsePythonTypeText(typePart);
if (parsedType) param->setChild("type", parsedType);
ASTNode* defVal = parseSimpleDefaultValue(defaultPart);
if (defVal) param->setChild("defaultValue", defVal);
fn->addChild("parameters", param);
}
}
static void maybeMaterializeClassFieldsFromInit(ClassDeclaration* cls,
const MethodDeclaration* method) {
if (!cls || !method) return;
if (method->name != "__init__" && method->name != "constructor") return;
for (auto* stmtNode : method->getChildren("body")) {
const ASTNode* assignmentNode = nullptr;
if (stmtNode->conceptType == "Assignment") {
assignmentNode = stmtNode;
} else if (stmtNode->conceptType == "ExpressionStatement") {
assignmentNode = stmtNode->getChild("expression");
}
if (!assignmentNode || assignmentNode->conceptType != "Assignment") continue;
auto* assign = static_cast<const Assignment*>(assignmentNode);
std::string fieldName = inferSelfFieldName(assign->getChild("target"));
if (fieldName.empty() || classHasField(cls, fieldName)) continue;
auto* field = new Variable(IdGenerator::next("field"), fieldName);
ASTNode* inferredType = inferFieldTypeFromValue(assign->getChild("value"), method);
if (inferredType) field->setChild("type", inferredType);
cls->addChild("fields", field);
}
}
// ---------------------------------------------------------------

View File

@@ -5,6 +5,8 @@
#include "GenericType.h"
#include "AsyncNodes.h"
#include "../SemannoAnnotationImpl.h"
#include <algorithm>
#include <cctype>
class RustGenerator : public ProjectionGenerator, public SemannoAnnotationImpl<RustGenerator> {
public:
@@ -34,6 +36,14 @@ public:
if (i > 0) oss << "\n";
oss << visitFunction(static_cast<const Function*>(functions[i]));
}
auto classes = module->getChildren("classes");
if (!classes.empty() && !functions.empty()) oss << "\n";
for (const auto* cls : classes) {
std::string classCode = generate(cls);
oss << classCode;
if (classCode.empty() || classCode.back() != '\n') oss << "\n";
}
return oss.str();
}
@@ -465,15 +475,33 @@ public:
if (!meth->isStatic) oss << "&self";
auto params = meth->getChildren("parameters");
for (size_t i = 0; i < params.size(); ++i) {
if (!meth->isStatic || i > 0) oss << ", ";
oss << visitParameter(static_cast<const Parameter*>(params[i]));
auto* p = static_cast<const Parameter*>(params[i]);
if (!p) continue;
if (p->name == "self" || p->name == "cls") continue;
oss << ", " << visitParameter(p);
}
oss << ")";
auto retType = meth->getChild("returnType");
if (retType) oss << " -> " << generate(retType);
auto body = meth->getChildren("body");
if (body.empty()) {
oss << " {}\n";
oss << " {\n";
std::string lower = meth->name;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower.find("empty") != std::string::npos) {
oss << " false\n";
} else if (lower.find("size") != std::string::npos) {
oss << " 0\n";
} else if (retType) {
std::string ret = generate(retType);
if (ret == "bool") oss << " false\n";
else if (ret == "i32" || ret == "i64" || ret == "usize") oss << " 0\n";
else if (ret == "String") oss << " String::new()\n";
else if (ret.find("Option<") == 0) oss << " None\n";
else if (ret != "()") oss << " panic!(\"unimplemented\")\n";
}
oss << " }\n";
} else {
oss << " {\n";
emitBody(oss, body, " ");