Skip to content
This repository was archived by the owner on Jan 8, 2023. It is now read-only.

Commit c03de6f

Browse files
author
ApsarasX
committed
optimize: change the inheritance structure of Expr
1 parent 10b389a commit c03de6f

5 files changed

Lines changed: 24 additions & 17 deletions

File tree

include/AST/Expr.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class FunctionDecl;
88

9-
class Expr : public ValueStmt {
9+
class Expr {
1010
public:
11-
~Expr() override = default;
11+
virtual ~Expr() = default;
1212
};
1313

1414
// 字面量表达式
@@ -29,7 +29,6 @@ class BooleanLiteralExpr : public LiteralExpr {
2929
~BooleanLiteralExpr() override = default;
3030

3131
bool literal;
32-
3332
};
3433

3534
// 整数字面量表达式
@@ -84,22 +83,22 @@ class CallExpr : public Expr {
8483
// 一元运算表达式
8584
class UnaryOperatorExpr : public Expr {
8685
public:
87-
UnaryOperatorExpr(size_t operatorCode, const SharedPtr<Expr> &subExpr);
86+
UnaryOperatorExpr(unsigned int opCode, const SharedPtr<Expr> &subExpr);
8887

8988
~UnaryOperatorExpr() override = default;
9089

91-
size_t operatorCode{};
90+
unsigned int opCode;
9291
SharedPtr<Expr> subExpr;
9392
};
9493

9594
// 二元运算表达式
9695
class BinaryOperatorExpr : public Expr {
9796
public:
98-
BinaryOperatorExpr(size_t operatorCode, const SharedPtr<Expr> &lhs, const SharedPtr<Expr> &rhs);
97+
BinaryOperatorExpr(unsigned int opCode, const SharedPtr<Expr> &lhs, const SharedPtr<Expr> &rhs);
9998

10099
~BinaryOperatorExpr() override = default;
101100

102-
size_t operatorCode;
101+
unsigned int opCode;
103102
SharedPtr<Expr> lhs, rhs;
104103
};
105104

include/AST/Stmt.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ class Stmt {
1515
};
1616

1717
// 值语句
18-
class ValueStmt : public Stmt {
18+
class ExprStmt : public Stmt {
1919
public:
20-
~ValueStmt() override = default;
20+
SharedPtr<Expr> expr;
21+
22+
explicit ExprStmt(const SharedPtr<Expr> &expr);
23+
24+
~ExprStmt() override = default;
2125
};
2226

2327
// 复合语句

lib/AST/ASTVisitor.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ antlrcpp::Any ASTVisitor::visitStatement(StaticScriptParser::StatementContext *c
3737
}
3838
} else if (ctx->expressionStatement()) {
3939
stmtAny = visitExpressionStatement(ctx->expressionStatement());
40-
if (stmtAny.is<SharedPtr<Expr>>()) {
41-
stmt = stmtAny.as<SharedPtr<Expr>>();
40+
if (stmtAny.is<SharedPtr<ExprStmt>>()) {
41+
stmt = stmtAny.as<SharedPtr<ExprStmt>>();
4242
}
4343
} else if (ctx->selectionStatement()) {
4444
stmtAny = visitSelectionStatement(ctx->selectionStatement());
@@ -177,7 +177,9 @@ antlrcpp::Any ASTVisitor::visitCompoundStatement(StaticScriptParser::CompoundSta
177177
}
178178

179179
antlrcpp::Any ASTVisitor::visitExpressionStatement(StaticScriptParser::ExpressionStatementContext *ctx) {
180-
return visitExpression(ctx->expression());
180+
SharedPtr<Expr> expr = visitExpression(ctx->expression());
181+
SharedPtr<ExprStmt> exprStmt = makeShared<ExprStmt>(expr);
182+
return exprStmt;
181183
}
182184

183185
antlrcpp::Any ASTVisitor::visitExpression(StaticScriptParser::ExpressionContext *ctx) {

lib/AST/Expr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ StringLiteralExpr::StringLiteralExpr(TypeKind type, String literal) : LiteralExp
1212

1313
IdentifierExpr::IdentifierExpr(String name) : name(std::move(name)) {}
1414

15-
UnaryOperatorExpr::UnaryOperatorExpr(size_t operatorCode, const SharedPtr<Expr> &subExpr) : operatorCode(operatorCode), subExpr(subExpr) {}
15+
UnaryOperatorExpr::UnaryOperatorExpr(unsigned int opCode, const SharedPtr<Expr> &subExpr) : opCode(opCode), subExpr(subExpr) {}
1616

17-
BinaryOperatorExpr::BinaryOperatorExpr(size_t operatorCode, const SharedPtr<Expr> &lhs, const SharedPtr<Expr> &rhs) : operatorCode(operatorCode),
17+
BinaryOperatorExpr::BinaryOperatorExpr(unsigned int opCode, const SharedPtr<Expr> &lhs, const SharedPtr<Expr> &rhs) : opCode(opCode),
1818
lhs(lhs), rhs(rhs) {}
1919

2020
CallExpr::CallExpr(String calleeName, const SharedPtrVector<Expr> &args) : calleeName(std::move(calleeName)), args(args) {}

lib/AST/Stmt.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#include "AST/Stmt.h"
22
#include "AST/Decl.h"
33

4-
void VarDeclStmt::pushVarDecl(const SharedPtr<VarDecl> &varDecl) {
5-
childVarDecls.push_back(varDecl);
6-
}
4+
ExprStmt::ExprStmt(const SharedPtr<Expr> &expr) : expr(expr) {}
75

86
CompoundStmt::CompoundStmt(const SharedPtrVector<Stmt> &childStmts) : childStmts(childStmts) {}
97

108
bool CompoundStmt::isEmpty() const {
119
return childStmts.empty();
1210
}
1311

12+
void VarDeclStmt::pushVarDecl(const SharedPtr<VarDecl> &varDecl) {
13+
childVarDecls.push_back(varDecl);
14+
}
15+
1416
FunctionDeclStmt::FunctionDeclStmt(const SharedPtr<FunctionDecl> &childFunctionDecl) : childFunctionDecl(childFunctionDecl) {}
1517

1618
IfStmt::IfStmt(const SharedPtr<Expr> &condition, const SharedPtr<Stmt> &thenStmt, const SharedPtr<Stmt> &elseStmt) : condition(condition),

0 commit comments

Comments
 (0)