Skip to content

Commit e3f0d70

Browse files
committed
Fixed some bugs about array and added some new features.
1 parent 653f292 commit e3f0d70

8 files changed

Lines changed: 35 additions & 54 deletions

File tree

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "cppdbg",
77
"request": "launch",
88
"program": "${workspaceFolder}/build/aq", // 替换为实际可执行文件路径
9-
"args": ["${workspaceFolder}/build/test.aq"],
9+
"args": ["${workspaceFolder}/build/test1.aq"],
1010
"stopAtEntry": false,
1111
"cwd": "${workspaceFolder}",
1212
"environment": [],

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project(AQ CXX)
88
set(CMAKE_CXX_COMPILER "g++")
99
set(CMAKE_CXX_STANDARD 20)
1010
SET(CMAKE_BUILD_TYPE "Debug")
11-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O3")
11+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
1212

1313

1414

src/ast/ast.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -420,32 +420,25 @@ class Variable : public Declaration, public Expression {
420420

421421
class ArrayDeclaration : public Variable {
422422
public:
423-
ArrayDeclaration(Type* type, Expression* name, Expression* size) {
423+
ArrayDeclaration(Type* type, Expression* name) {
424424
Declaration::statement_type_ = StatementType::kArrayDeclaration;
425425
variable_type_ = type;
426426
variable_name_ = name;
427-
array_size_ = size;
428427
variable_value_.clear();
429428
}
430429

431-
ArrayDeclaration(Type* type, Expression* name, Expression* size,
432-
std::vector<Expression*> value) {
430+
ArrayDeclaration(Type* type, Expression* name,
431+
std::vector<Expression*>&& value) {
433432
Declaration::statement_type_ = StatementType::kArrayDeclaration;
434433
variable_type_ = type;
435434
variable_name_ = name;
436-
array_size_ = size;
437435
variable_value_ = value;
438436
}
439437

440438
virtual ~ArrayDeclaration() = default;
441439

442-
Expression* GetArraySizeExpression() { return array_size_; }
443-
444440
ArrayDeclaration(const ArrayDeclaration&) = default;
445441
ArrayDeclaration& operator=(const ArrayDeclaration&) = default;
446-
447-
private:
448-
Expression* array_size_ = nullptr;
449442
};
450443

451444
class FunctionDeclaration : public Declaration {

src/ast/type.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,8 @@ Type* Type::CreateConstDerivedType(Type* sub_type, Token* token,
171171
// If it is not a post const type, it is a pre const type.
172172
if (sub_type == nullptr) sub_type = CreateType(token, length, ++index);
173173

174-
// ConstType* const_type = new ConstType();
175-
// if (const_type == nullptr) INTERNAL_ERROR("const_type is nullptr.");
176-
177174
if (sub_type == nullptr) INTERNAL_ERROR("type is nullptr.");
178175

179-
// const_type->SetSubType(sub_type);
180-
// return const_type;
181-
182176
LOGGING_WARNING("The C-style const declaration method is now deprecated.");
183177

184178
return sub_type;
@@ -198,6 +192,18 @@ Type* Type::CreateOperatorDerivedType(Type* sub_type, Token* token,
198192
break;
199193
}
200194

195+
case Token::OperatorType::l_square: {
196+
if (token[index + 1].type != Token::Type::OPERATOR &&
197+
token[index + 1].value.oper != Token::OperatorType::r_square)
198+
LOGGING_WARNING("Unsupported array size yet.");
199+
200+
index++;
201+
202+
ArrayType* array_type = new ArrayType();
203+
array_type->SetSubType(type);
204+
type = array_type;
205+
}
206+
201207
default:
202208
return type;
203209
}

src/ast/type.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
#ifndef AQ_AST_TYPE_H_
66
#define AQ_AST_TYPE_H_
77

8+
#include <cstdint>
89
#include <string>
910
#include <vector>
10-
#include <cstdint>
1111

1212
#include "logging/logging.h"
1313
#include "token/token.h"
1414

15-
1615
namespace Aq {
1716
namespace Ast {
1817

@@ -145,27 +144,16 @@ class ConstType : public Type {
145144

146145
class ArrayType : public Type {
147146
public:
148-
ArrayType() {
149-
type_category_ = TypeCategory::kArray;
150-
size_ = nullptr;
151-
}
152-
void SetSubType(Type* type, Expression* size) {
153-
type_data_ = type;
154-
size_ = size;
155-
}
147+
ArrayType() { type_category_ = TypeCategory::kArray; }
148+
void SetSubType(Type* type) { type_data_ = type; }
156149
virtual ~ArrayType() = default;
157150

158151
Type* GetSubType() { return type_data_; }
159152

160-
Expression* GetArraySize() { return size_; }
161-
162153
operator std::string() override { return std::string(*type_data_) + "*"; }
163154

164155
ArrayType(const ArrayType&) = default;
165156
ArrayType& operator=(const ArrayType&) = default;
166-
167-
private:
168-
Expression* size_;
169157
};
170158

171159
class ReferenceType : public Type {

src/interpreter/declaration_interpreter.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void HandleClassDeclaration(Interpreter& interpreter, Ast::Class* declaration) {
206206

207207
// Check if there are any errors in the preprocessing.
208208
if (classes.find(class_name) == classes.end())
209-
INTERNAL_ERROR("Not found class declaration.");
209+
INTERNAL_ERROR("Not found class declaration: " + class_name);
210210
current_class = &classes[class_name];
211211

212212
// Adds the special variable into class memory.
@@ -528,9 +528,9 @@ std::size_t HandleArrayDeclaration(Interpreter& interpreter,
528528
// initialized when the ARRAY operator is called.
529529
if (sub_type_category == Ast::Type::TypeCategory::kClass) {
530530
std::size_t current_index = global_memory->Add(1);
531-
code.push_back(Bytecode{
532-
_AQVM_OPERATOR_ARRAY,
533-
{current_index, array_index, global_memory->GetUint64tData(0)}});
531+
code.push_back(
532+
Bytecode{_AQVM_OPERATOR_ARRAY,
533+
{current_index, array_index, global_memory->AddUint64t(0)}});
534534
code.push_back(
535535
Bytecode{_AQVM_OPERATOR_INVOKE_METHOD,
536536
{current_index, global_memory->AddString("@constructor"),
@@ -542,9 +542,9 @@ std::size_t HandleArrayDeclaration(Interpreter& interpreter,
542542
for (std::size_t i = 0; i < declaration->GetVariableValue().size(); i++) {
543543
// Gets the corresponding array index reference.
544544
std::size_t current_index = global_memory->Add(1);
545-
code.push_back(Bytecode{
546-
_AQVM_OPERATOR_ARRAY,
547-
{current_index, array_index, global_memory->GetUint64tData(i)}});
545+
code.push_back(
546+
Bytecode{_AQVM_OPERATOR_ARRAY,
547+
{current_index, array_index, global_memory->AddUint64t(i)}});
548548

549549
// Gets the value of the initialization list and assigns value to
550550
// corresponding index.

src/interpreter/preprocesser.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ void PreProcessClassDeclaration(Interpreter& interpreter,
9090

9191
classes[full_name] = current_class;
9292

93+
LOGGING_INFO(full_name);
94+
9395
for (std::size_t i = 0; i < statement->GetSubClasses().size(); i++) {
9496
if (Ast::IsOfType<Ast::Class>(statement->GetSubClasses()[i])) {
9597
PreProcessClassDeclaration(

src/parser/declaration_parser.cc

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "parser/declaration_parser.h"
66

77
#include <cstddef>
8-
#include <string>
98

109
#include "ast/ast.h"
1110
#include "ast/type.h"
@@ -158,7 +157,7 @@ Ast::Variable* Parser::DeclarationParser::ParseVariableDeclaration(
158157
if (name == nullptr) INTERNAL_ERROR("name is nullptr.");
159158

160159
// Checks if the variable is an array declaration.
161-
if (*name == Ast::Statement::StatementType::kArray)
160+
if (type->GetTypeCategory() == Ast::Type::TypeCategory::kArray)
162161
return ParseArrayDeclaration(type, name, token, length, index);
163162

164163
// Declaration with initialization value.
@@ -276,24 +275,19 @@ bool Parser::DeclarationParser::HasCustomTypeBeforeExpression(
276275
Ast::ArrayDeclaration* Parser::DeclarationParser::ParseArrayDeclaration(
277276
Ast::Type* type, Ast::Expression* name, Token* token, std::size_t length,
278277
std::size_t& index) {
279-
280-
281278
if (token == nullptr) INTERNAL_ERROR("token is nullptr.");
282279
if (index >= length) INTERNAL_ERROR("index is out of range.");
283-
Ast::Array* array = Ast::Cast<Ast::Array>(name);
284-
if (array == nullptr) INTERNAL_ERROR("name is not an array.");
285280

286281
if (token[index].value.oper == Token::OperatorType::equal) {
287282
index++;
288283
if (token[index].type == Token::Type::OPERATOR &&
289-
token[index].value.oper == Token::OperatorType::l_brace) {
284+
token[index].value.oper == Token::OperatorType::l_square) {
290285
std::vector<Ast::Expression*> values;
291286
while (true) {
292-
293-
// Skip the l_brace or comma.
287+
// Skip the l_square or comma.
294288
values.push_back(ExpressionParser::ParseExpressionWithoutComma(
295289
token, length, ++index));
296-
if (token[index] == Token::OperatorType::r_brace) {
290+
if (token[index] == Token::OperatorType::r_square) {
297291
index++;
298292
break;
299293
}
@@ -303,15 +297,13 @@ Ast::ArrayDeclaration* Parser::DeclarationParser::ParseArrayDeclaration(
303297
"found.");
304298
}
305299
}
306-
return new Ast::ArrayDeclaration(type, array->GetExpression(),
307-
array->GetIndexExpression(), values);
300+
return new Ast::ArrayDeclaration(type, name, std::move(values));
308301
} else {
309302
LOGGING_ERROR(
310303
"Expected '{' or ',' after '=' in array declaration, but not found.");
311304
}
312305
}
313-
return new Ast::ArrayDeclaration(type, array->GetExpression(),
314-
array->GetIndexExpression());
306+
return new Ast::ArrayDeclaration(type, name);
315307
}
316308

317309
} // namespace Aq

0 commit comments

Comments
 (0)