Step 138: type-aware code generation
This commit is contained in:
28
editor/src/TypeAwareMappings.h
Normal file
28
editor/src/TypeAwareMappings.h
Normal 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 "";
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user