Skip to content

Commit 9cd84c9

Browse files
committed
refactor(ast): change the ast-nodes
1. change the method of gettype 2. delete ast-left-postfix-node please refer to the work log for details
1 parent ce07b7f commit 9cd84c9

File tree

15 files changed

+381
-435
lines changed

15 files changed

+381
-435
lines changed

demos/demo.stvc

0 Bytes
Binary file not shown.

demos/demo1.stvc

0 Bytes
Binary file not shown.

demos/demo2.stvc

0 Bytes
Binary file not shown.

doc/AST-IR编码文件文档.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ AST-IR(以下简称``AstIr``)是一种将抽象语法树(以下简称``Ast
3838
|:-|:-|
3939
|ast::AstAnonClass|isHaveFather|
4040
|ast::AstExpression|ass_type|
41-
|ast::AstLeftPostfix|getPostfixType()|
4241
|ast::AstBinary|getOperatorType()|
4342
|ast::AstUnary|getOperatorType()|
4443
|ast::AstPostfix|getPostfixType()|

doc/工作日志/20250607.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 2025/06/07 工作日志
2+
3+
本次更新不会修改版本。
4+
5+
### 修改了AstNode的getType()的底层运作方式
6+
7+
无论是``DataType``,还是``AstNode``,抑或是``Token``,都需要调用方法来获取他们的具体类型,以进行详细的辨认。
8+
9+
但是,用现在的眼光看,``AstNode``的getType()在设计时是欠缺更多思考的——利用虚函数重载的方式来实现。
10+
11+
这样做有两点问题:
12+
1. 调用虚函数需要一定的开销
13+
2. 在初始化语法树时需要一个一个类型判断过去并new出对应的节点,即使对应的节点没有任何特有的数据。即每个节点都需要特判
14+
15+
理想的解决方法应该是:在父类设立一个整数变量``id``,子类在创建时初始化这个id。在初始化语法树时,对于没有任何特有数据的节点,可以直接创建父类,然后修改id。
16+
17+
目前,我已经把这种理想的解决方法应用到了Ast中。
18+
19+
### 删除了AstLeftPostfix节点
20+
21+
用现在的眼光看,该节点是设计之初过度思考的产物。``AstLeftPostfix````AstPostfix``别无二致,因此我删去了他。

doc/抽象语法树文件编码文档.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ AST文件里存储着一系列AST节点,一些应用程序可以将自己的
3434
* AstString:字符串节点,其AST数据由**四字节的字符长度****字节长度为字符串长度的字符串内容**组成。
3535
* AstAnonClass:匿名类节点,AST数据由**一字节的布尔值(即isHaveFather)组成**
3636
* AstExpression:表达式节点,AST数据由**四字节的数值(即ass_type)组成**
37-
* AstLeftPostfix:左值后缀节点,AST数据由**四字节的数值(即postfix_type)组成**
3837
* AstBinary:双目运算节点,AST数据由**四字节的数值(即operator_type)组成**
3938
* AstUnary:单目运算节点,AST数据由**四字节的数值(即operator_type)组成**
4039
* AstPostfix:右值后缀节点,AST数据由**四字节的数值(即postfix_type)组成**

src/action/AstFileReader.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ class AstFileReader {
238238
}
239239

240240
CHECK_SPECIAL_AST(ast::AstExpression, ass_type);
241-
CHECK_SPECIAL_AST(ast::AstLeftPostfix, postfix_type);
242241
CHECK_SPECIAL_AST(ast::AstBinary, operator_type);
243242
CHECK_SPECIAL_AST(ast::AstUnary, operator_type);
244243
CHECK_SPECIAL_AST(ast::AstPostfix, postfix_type);

src/action/AstFileWriter.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,6 @@ class AstFileWriter {
186186
break;
187187
}
188188

189-
case ast::AstLeftPostfixType: {
190-
// 匿名类
191-
WRITE_I(((ast::AstLeftPostfix *) top)->postfix_type);
192-
break;
193-
}
194-
195189
case ast::AstBinaryType: {
196190
// 匿名类
197191
WRITE_I(((ast::AstBinary *) top)->operator_type);

src/ast/Ast.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ namespace stamon {
3636
AstSFNType,
3737
AstExpressionType,
3838
AstLeftValueType,
39-
AstLeftPostfixType,
4039
AstBinaryType,
4140
AstUnaryType,
4241
AstPostfixType,
@@ -68,7 +67,6 @@ namespace stamon {
6867
class AstSFN;
6968
class AstExpression;
7069
class AstLeftValue;
71-
class AstLeftPostfix;
7270
class AstBinary;
7371
class AstUnary;
7472
class AstPostfix;
@@ -85,18 +83,18 @@ namespace stamon {
8583
ArrayList<AstNode*>* children;
8684

8785
public:
86+
int id; //语法树的节点类型
8887
int lineNo; //语法分析时用于显示行号
8988
String filename; //语义分析时用于显示文件名
9089

9190
AstNode() {
92-
//构造函数
91+
id = AstNodeType;
9392
children = new ArrayList<AstNode*>();
9493
}
9594

96-
AstNode(int line) {
97-
//构造函数
95+
AstNode(int type_id) {
96+
id = type_id;
9897
children = new ArrayList<AstNode*>();
99-
lineNo = line;
10098
}
10199

102100
virtual ArrayList<AstNode*> *Children() {
@@ -106,7 +104,7 @@ namespace stamon {
106104

107105
virtual int getType() {
108106
//获得节点类型
109-
return AstNodeType;
107+
return id;
110108
}
111109

112110
virtual ~AstNode() {

0 commit comments

Comments
 (0)