Step 138: type-aware code generation

This commit is contained in:
Bill
2026-02-09 17:35:17 -07:00
parent 4bfaaa6cce
commit dcd44d26ed
6 changed files with 89 additions and 4 deletions

View File

@@ -0,0 +1,28 @@
#pragma once
#include <string>
#include <unordered_map>
static inline std::string mapTypeForFunctionCall(const std::string& targetLanguage,
const std::string& functionName) {
if (targetLanguage == "cpp") {
static const std::unordered_map<std::string, std::string> cppMap = {
{"numpy.array", "std::vector<double>"},
{"numpy.zeros", "std::vector<double>"},
{"numpy.ones", "std::vector<double>"},
{"pandas.DataFrame", "std::vector<std::vector<double>>"},
{"torch.tensor", "std::vector<float>"}
};
auto it = cppMap.find(functionName);
if (it != cppMap.end()) return it->second;
}
if (targetLanguage == "rust") {
static const std::unordered_map<std::string, std::string> rustMap = {
{"numpy.array", "Vec<f64>"},
{"numpy.zeros", "Vec<f64>"},
{"numpy.ones", "Vec<f64>"}
};
auto it = rustMap.find(functionName);
if (it != rustMap.end()) return it->second;
}
return "";
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include "ProjectionGenerator.h"
#include "../TypeAwareMappings.h"
class CppGenerator : public ProjectionGenerator {
public:
@@ -106,6 +107,13 @@ public:
typeStr = generate(type);
}
auto initializer = variable->getChild("initializer");
if (!type && initializer && initializer->conceptType == "FunctionCall") {
auto* call = static_cast<const FunctionCall*>(initializer);
std::string mapped = mapTypeForFunctionCall("cpp", call->functionName);
if (!mapped.empty()) typeStr = mapped;
}
// Check enclosing function for memory annotations that affect variable types
std::string wrapper = getMemoryTypeWrapper(variable);
if (!wrapper.empty()) {
@@ -113,8 +121,6 @@ public:
} else {
oss << typeStr << " " << variable->name;
}
auto initializer = variable->getChild("initializer");
if (initializer) {
oss << " = " << generate(initializer);
}