Skip to content

Commit 3d00fe6

Browse files
committed
Merge remote-tracking branch 'origin/integration' into integration
2 parents 4360627 + c61eefb commit 3d00fe6

3 files changed

Lines changed: 122 additions & 20 deletions

File tree

lib/parser/ast/visitors/BytecodeVisitor.cpp

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,22 @@ void BytecodeVisitor::Visit(ExprStmt& node) {
10441044
}
10451045
}
10461046

1047+
// Check if this is a builtin type method that returns void
1048+
if (!object_type.empty() && kBuiltinTypeNames.contains(object_type)) {
1049+
if (object_type == "File") {
1050+
if (method_name == "Open" || method_name == "Close" || method_name == "WriteLine" ||
1051+
method_name == "Seek") {
1052+
should_pop = false;
1053+
}
1054+
} else if (object_type.find("Array") != std::string::npos) {
1055+
if (method_name == "Add" || method_name == "RemoveAt" || method_name == "InsertAt" ||
1056+
method_name == "SetAt" || method_name == "Clear" || method_name == "Reserve" ||
1057+
method_name == "ShrinkToFit") {
1058+
should_pop = false;
1059+
}
1060+
}
1061+
}
1062+
10471063
std::string method_key;
10481064
if (!object_type.empty() && !kBuiltinTypeNames.contains(object_type)) {
10491065
method_key = object_type + "::" + method_name;
@@ -1470,6 +1486,14 @@ void BytecodeVisitor::Visit(Assign& node) {
14701486
EmitTypeConversionIfNeeded(elem_type, value_type_name);
14711487

14721488
index_access->MutableIndexExpr().Accept(*this);
1489+
1490+
// Unwrap the index if it's a wrapper type (e.g., Int -> int)
1491+
// Array SetAt methods expect int, not Int wrapper
1492+
std::string index_type = GetTypeNameForExpr(&index_access->MutableIndexExpr());
1493+
if (IsPrimitiveWrapper(index_type)) {
1494+
EmitCommand("Unwrap");
1495+
}
1496+
14731497
index_access->MutableObject().Accept(*this);
14741498

14751499
std::string method_name = GenerateArraySetAtMethodName(array_type);
@@ -2114,6 +2138,14 @@ void BytecodeVisitor::Visit(FieldAccess& node) {
21142138

21152139
void BytecodeVisitor::Visit(IndexAccess& node) {
21162140
node.MutableIndexExpr().Accept(*this);
2141+
2142+
// Unwrap the index if it's a wrapper type (e.g., Int -> int)
2143+
// Array GetAt methods expect int, not Int wrapper
2144+
std::string index_type = GetTypeNameForExpr(&node.MutableIndexExpr());
2145+
if (IsPrimitiveWrapper(index_type)) {
2146+
EmitCommand("Unwrap");
2147+
}
2148+
21172149
node.MutableObject().Accept(*this);
21182150

21192151
std::string array_type = GetTypeNameForExpr(&node.MutableObject());
@@ -2675,6 +2707,31 @@ BytecodeVisitor::OperandType BytecodeVisitor::DetermineOperandType(Expr* expr) {
26752707
}
26762708
}
26772709

2710+
if (auto* index_access = dynamic_cast<IndexAccess*>(expr)) {
2711+
// Get the element type of the array
2712+
std::string array_type = GetTypeNameForExpr(&index_access->MutableObject());
2713+
std::string elem_type = GetElementTypeForArray(array_type);
2714+
2715+
if (elem_type == "int") {
2716+
return OperandType::kInt;
2717+
}
2718+
if (elem_type == "float") {
2719+
return OperandType::kFloat;
2720+
}
2721+
if (elem_type == "byte") {
2722+
return OperandType::kByte;
2723+
}
2724+
if (elem_type == "bool") {
2725+
return OperandType::kBool;
2726+
}
2727+
if (elem_type == "char") {
2728+
return OperandType::kChar;
2729+
}
2730+
if (elem_type == "String") {
2731+
return OperandType::kString;
2732+
}
2733+
}
2734+
26782735
if (auto* binary = dynamic_cast<Binary*>(expr)) {
26792736
OperandType lhs_type = DetermineOperandType(&binary->MutableLhs());
26802737
OperandType rhs_type = DetermineOperandType(&binary->MutableRhs());
@@ -2904,7 +2961,7 @@ std::string BytecodeVisitor::GetTypeNameForExpr(Expr* expr) {
29042961
return "String";
29052962
}
29062963
if (ns_name == "ReadChar" && call->Args().size() == 0) {
2907-
return "Char";
2964+
return "char";
29082965
}
29092966
// Time and Date Operations
29102967
if (ns_name == "FormatDateTime" && call->Args().size() == 2) {
@@ -2924,16 +2981,16 @@ std::string BytecodeVisitor::GetTypeNameForExpr(Expr* expr) {
29242981
return "Bool";
29252982
}
29262983
if (ns_name == "DeleteFile" && call->Args().size() == 1) {
2927-
return "Bool";
2984+
return "bool";
29282985
}
29292986
if (ns_name == "DeleteDirectory" && call->Args().size() == 1) {
2930-
return "Bool";
2987+
return "bool";
29312988
}
29322989
if (ns_name == "MoveFile" && call->Args().size() == 2) {
2933-
return "Bool";
2990+
return "bool";
29342991
}
29352992
if (ns_name == "CopyFile" && call->Args().size() == 2) {
2936-
return "Bool";
2993+
return "bool";
29372994
}
29382995
if (ns_name == "ListDirectory" && call->Args().size() == 1) {
29392996
return "StringArray";
@@ -2942,7 +2999,7 @@ std::string BytecodeVisitor::GetTypeNameForExpr(Expr* expr) {
29422999
return "String";
29433000
}
29443001
if (ns_name == "ChangeDirectory" && call->Args().size() == 1) {
2945-
return "Bool";
3002+
return "bool";
29463003
}
29473004
// Process Control
29483005
if (ns_name == "Sleep" && call->Args().size() == 1) {
@@ -2961,7 +3018,7 @@ std::string BytecodeVisitor::GetTypeNameForExpr(Expr* expr) {
29613018
return "String?"; // Returns Nullable<String>
29623019
}
29633020
if (ns_name == "SetEnvironmentVar" && call->Args().size() == 2) {
2964-
return "Bool";
3021+
return "bool";
29653022
}
29663023
// Random Number Generation
29673024
if (ns_name == "SeedRandom" && call->Args().size() == 1) {
@@ -3019,6 +3076,17 @@ std::string BytecodeVisitor::GetTypeNameForExpr(Expr* expr) {
30193076
if (method_name == "GetHash") {
30203077
return "int";
30213078
}
3079+
if (method_name == "Equals") {
3080+
return "bool";
3081+
}
3082+
if (method_name == "IsLess") {
3083+
return "bool";
3084+
}
3085+
if (method_name == "Add" || method_name == "RemoveAt" || method_name == "InsertAt" ||
3086+
method_name == "SetAt" || method_name == "Clear" || method_name == "Reserve" ||
3087+
method_name == "ShrinkToFit") {
3088+
return "void";
3089+
}
30223090
}
30233091
// Handle String methods
30243092
if (object_type == "String") {
@@ -3034,6 +3102,12 @@ std::string BytecodeVisitor::GetTypeNameForExpr(Expr* expr) {
30343102
if (method_name == "ToUtf8Bytes") {
30353103
return "ByteArray";
30363104
}
3105+
if (method_name == "Equals") {
3106+
return "bool";
3107+
}
3108+
if (method_name == "IsLess") {
3109+
return "bool";
3110+
}
30373111
}
30383112
// Handle wrapper type methods (Int, Float, etc.)
30393113
if (method_name == "ToString") {
@@ -3042,6 +3116,30 @@ std::string BytecodeVisitor::GetTypeNameForExpr(Expr* expr) {
30423116
if (method_name == "GetHash") {
30433117
return "int";
30443118
}
3119+
if (method_name == "Equals") {
3120+
return "bool";
3121+
}
3122+
if (method_name == "IsLess") {
3123+
return "bool";
3124+
}
3125+
// Handle File methods
3126+
if (object_type == "File") {
3127+
if (method_name == "Open" || method_name == "Close" || method_name == "WriteLine" || method_name == "Seek") {
3128+
return "void";
3129+
}
3130+
if (method_name == "IsOpen" || method_name == "Eof") {
3131+
return "bool";
3132+
}
3133+
if (method_name == "Read") {
3134+
return "ByteArray";
3135+
}
3136+
if (method_name == "ReadLine") {
3137+
return "String";
3138+
}
3139+
if (method_name == "Tell" || method_name == "Write") {
3140+
return "int";
3141+
}
3142+
}
30453143
}
30463144

30473145
// Handle method calls on user-defined types

lib/parser/ast/visitors/TypeChecker.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,29 @@ const std::unordered_map<std::string, std::string> kBuiltinReturnTypes = {
126126
{"ParseDateTime", "Int"},
127127
{"FormatDateTime", "String"},
128128
// File operations
129-
{"FileExists", "Bool"},
130-
{"DirectoryExists", "Bool"},
131-
{"CreateDirectory", "Bool"},
132-
{"DeleteFile", "Bool"},
133-
{"DeleteDirectory", "Bool"},
134-
{"MoveFile", "Bool"},
135-
{"CopyFile", "Bool"},
129+
{"FileExists", "bool"},
130+
{"DirectoryExists", "bool"},
131+
{"CreateDirectory", "bool"},
132+
{"DeleteFile", "bool"},
133+
{"DeleteDirectory", "bool"},
134+
{"MoveFile", "bool"},
135+
{"CopyFile", "bool"},
136136
{"ListDirectory", "StringArray"},
137137
{"GetCurrentDirectory", "String"},
138-
{"ChangeDirectory", "Bool"},
138+
{"ChangeDirectory", "bool"},
139139
// I/O functions
140140
{"ReadLine", "String"},
141-
{"ReadChar", "Char"},
142-
{"ReadInt", "Int"},
143-
{"ReadFloat", "Float"},
141+
{"ReadChar", "char"},
142+
{"ReadInt", "int"},
143+
{"ReadFloat", "float"},
144144
// System information
145145
{"GetOsName", "String"},
146146
{"GetOsVersion", "String"},
147147
{"GetArchitecture", "String"},
148148
{"GetUserName", "String"},
149149
{"GetHomeDirectory", "String"},
150150
// Environment
151-
{"SetEnvironmentVar", "Bool"},
151+
{"SetEnvironmentVar", "bool"},
152152
// Math functions
153153
{"Sqrt", "float"},
154154
{"ToString", "String"},
@@ -2628,7 +2628,7 @@ void TypeChecker::InitializeBuiltinMethods() {
26282628
{
26292629
BuiltinMethodSignature sig;
26302630
sig.param_types = {TypeReference("ByteArray")};
2631-
sig.return_type = nullptr; // void
2631+
sig.return_type = std::make_unique<TypeReference>("int"); // int
26322632
builtin_methods_["File"]["Write"] = std::move(sig);
26332633
}
26342634

tests/main_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ TEST_F(ProjectIntegrationTestSuite, ExampleFileNBody) {
116116
CompileAndCompareExample("nbody");
117117
}
118118

119+
TEST_F(ProjectIntegrationTestSuite, ExampleFileFile) {
120+
CompileAndCompareExample("file");
121+
}
122+
119123
// Integrational files compilation tests
120124
TEST_F(ProjectIntegrationTestSuite, IntegrationalFileQuadratic) {
121125
CompileAndCompareIntegrational("quadratic");

0 commit comments

Comments
 (0)