Skip to content

Commit b17a149

Browse files
committed
Fixed some bugs about va_arg function.
1 parent 8f9ce4c commit b17a149

File tree

5 files changed

+156
-69
lines changed

5 files changed

+156
-69
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ cmake_minimum_required(VERSION 3.10)
66
project(AQ C CXX)
77
enable_testing()
88

9+
set(CMAKE_CXX_COMPILER "g++")
10+
set(CMAKE_C_COMPILER "gcc")
11+
912
include_directories(${PROJECT_SOURCE_DIR})
1013

1114
set(COMPILER_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/prototype/compiler.cc)

prototype/compiler.cc

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8375,6 +8375,8 @@ void BytecodeGenerator::HandleFuncDecl(FuncDeclNode* func_decl) {
83758375
nullptr, return_value_reference_index));
83768376
args_index.push_back(return_value_reference_index);
83778377

8378+
std::size_t va_array_index = 0;
8379+
83788380
for (std::size_t i = 0; i < args.size(); i++) {
83798381
if (args[i]->GetType() == StmtNode::StmtType::kVarDecl) {
83808382
args_index.push_back(
@@ -8387,7 +8389,8 @@ void BytecodeGenerator::HandleFuncDecl(FuncDeclNode* func_decl) {
83878389
"args is not VarDeclNode or ArrayDeclNode.");
83888390
}
83898391
if(i==args.size()-1&& func_decl->GetStat()->GetVaFlag()){
8390-
args_index.push_back(global_memory_.Add(1));
8392+
va_array_index = global_memory_.Add(1);
8393+
args_index.push_back(va_array_index);
83918394
}
83928395
}
83938396

@@ -8416,7 +8419,7 @@ void BytecodeGenerator::HandleFuncDecl(FuncDeclNode* func_decl) {
84168419
if(i == func_decl->GetStat()->GetArgs().size()-1&& func_decl->GetStat()->GetVaFlag()){
84178420
var_decl_map_.emplace(current_scope_.back() + "#args",
84188421
std::pair<VarDeclNode*, std::size_t>(
8419-
NULL, global_memory_.Add(1)));
8422+
NULL, va_array_index));
84208423
}
84218424
}
84228425

@@ -8542,6 +8545,8 @@ void BytecodeGenerator::HandleClassFuncDecl(FuncDeclNode* func_decl) {
85428545
nullptr, return_value_reference_index));
85438546
args_index.push_back(return_value_reference_index);
85448547

8548+
std::size_t va_array_index = 0;
8549+
85458550
for (std::size_t i = 0; i < args.size(); i++) {
85468551
if (args[i]->GetType() == StmtNode::StmtType::kVarDecl) {
85478552
args_index.push_back(
@@ -8554,7 +8559,8 @@ void BytecodeGenerator::HandleClassFuncDecl(FuncDeclNode* func_decl) {
85548559
"args is not VarDeclNode or ArrayDeclNode.");
85558560
}
85568561
if(i == args.size()-1&& func_decl->GetStat()->GetVaFlag()){
8557-
args_index.push_back(global_memory_.Add(1));
8562+
va_array_index = global_memory_.Add(1);
8563+
args_index.push_back(va_array_index);
85588564
}
85598565
}
85608566

@@ -8584,7 +8590,7 @@ void BytecodeGenerator::HandleClassFuncDecl(FuncDeclNode* func_decl) {
85848590
var_decl_map_.emplace(
85858591
current_scope_.back() + "#args",
85868592
std::pair<VarDeclNode*, std::size_t>(
8587-
NULL, global_memory_.Add(1)));
8593+
NULL, va_array_index));
85888594
}
85898595
}
85908596

@@ -8703,6 +8709,8 @@ void BytecodeGenerator::HandleClassConstructor(FuncDeclNode* func_decl) {
87038709
nullptr, return_value_reference_index));*/
87048710
args_index.push_back(return_value_index);
87058711

8712+
std::size_t va_array_index = 0;
8713+
87068714
for (std::size_t i = 0; i < args.size(); i++) {
87078715
if (args[i]->GetType() == StmtNode::StmtType::kVarDecl) {
87088716
args_index.push_back(
@@ -8715,7 +8723,8 @@ void BytecodeGenerator::HandleClassConstructor(FuncDeclNode* func_decl) {
87158723
"args is not VarDeclNode or ArrayDeclNode.");
87168724
}
87178725
if(i==args.size()-1&& func_decl->GetStat()->GetVaFlag()){
8718-
args_index.push_back(global_memory_.Add(1));
8726+
va_array_index = global_memory_.Add(1);
8727+
args_index.push_back(va_array_index);
87198728
}
87208729
}
87218730

@@ -8745,7 +8754,7 @@ void BytecodeGenerator::HandleClassConstructor(FuncDeclNode* func_decl) {
87458754
var_decl_map_.emplace(
87468755
current_scope_.back() + "#args",
87478756
std::pair<VarDeclNode*, std::size_t>(
8748-
NULL, global_memory_.Add(1)));
8757+
NULL, va_array_index));
87498758
}
87508759
}
87518760

prototype/hello-world.aq

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
int print(auto str);
2-
class TEST_CLASS{
2+
/*class TEST_CLASS{
33
void TEST_CLASS(){
44
print("TEST_CLASS Created.\n");
55
}
@@ -13,26 +13,32 @@ class TEST_CLASS{
1313
};
1414
void test_func(int abc){
1515
print("Int");
16+
}*/
17+
void test_func(char abc,...){
18+
print(args[0]);
19+
print("Char VA");
1620
}
17-
void test_func(char abc){
18-
print("Char");
19-
}
20-
void test_func(TEST_CLASS abc){
21+
/*void test_func(TEST_CLASS abc){
2122
print("TEST_CLASS");
2223
abc.data_=999;
2324
}
25+
26+
void test_func(char abc){
27+
// print(args[0]);
28+
print("Char INT NOT VA");
29+
}*/
2430

2531
auto main(){
26-
TEST_CLASS class_arr[10];
27-
class_arr[11].data_=100;
32+
//TEST_CLASS class_arr[10];
33+
//class_arr[11].data_=100;
2834

29-
auto result = print("Hello World!\n");
30-
print("1");
31-
auto tc = class_arr[8];
32-
tc.data_ = 666;
33-
print(tc.data_);
34-
print(1);
35-
test_func(tc);
36-
print( class_arr[8].data_);
37-
return result;
35+
//auto result = print("Hello World!\n");
36+
test_func('1',"ABC");
37+
//auto tc = class_arr[8];
38+
//tc.data_ = 666;
39+
//print(tc.data_);
40+
//print(1);
41+
//test_func(tc);
42+
//print( class_arr[8].data_);
43+
//return result;
3844
}

prototype/hello-world.aqbc

-1.09 KB
Binary file not shown.

0 commit comments

Comments
 (0)