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

Commit 95ba3a3

Browse files
author
ApsarasX
committed
feat: optimize include guard and add scope class
1 parent d9a8e9c commit 95ba3a3

20 files changed

Lines changed: 167 additions & 97 deletions

include/AST/AST.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
#ifndef STATICSCRIPT_AST_AST_H
2-
#define STATICSCRIPT_AST_AST_H
1+
#pragma once
32

43
#include "AST/TypeNode.h"
54
#include "AST/DeclNode.h"
65
#include "AST/StmtNode.h"
76
#include "AST/ExprNode.h"
8-
#include "AST/Module.h"
7+
#include "AST/ModuleNode.h"
98
#include "AST/ASTBuilder.h"
10-
11-
#endif // STATICSCRIPT_AST_AST_H

include/AST/ASTBuilder.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
#ifndef STATICSCRIPT_AST_ASTBUILDER_H
2-
#define STATICSCRIPT_AST_ASTBUILDER_H
1+
#pragma once
32

43
#include <iostream>
54
#include "StaticScriptParserBaseVisitor.h"
65
#include "AST/DeclNode.h"
76
#include "AST/StmtNode.h"
87
#include "AST/TypeNode.h"
98
#include "AST/ExprNode.h"
10-
#include "AST/Module.h"
9+
#include "AST/ModuleNode.h"
1110
#include "Util/Alias.h"
1211

1312
class ASTBuilder final : public StaticScriptParserVisitor {
@@ -83,6 +82,3 @@ class ASTBuilder final : public StaticScriptParserVisitor {
8382
private:
8483
const String filename;
8584
};
86-
87-
88-
#endif // STATICSCRIPT_AST_ASTBUILDER_H

include/AST/ASTVisitor.h

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
#ifndef STATICSCRIPT_AST_ASTVISITOR_H
2-
#define STATICSCRIPT_AST_ASTVISITOR_H
1+
#pragma once
32

43
#include "AST/AST.h"
54

65
template<typename S, typename E>
76
class ASTVisitor {
87
public:
9-
virtual SharedPtr<S> visit(SharedPtr<CompoundStmt> stmt) = 0;
10-
virtual SharedPtr<S> visit(SharedPtr<ExprStmt> stmt) = 0;
11-
virtual SharedPtr<S> visit(SharedPtr<VarDeclStmt> stmt) = 0;
12-
virtual SharedPtr<S> visit(SharedPtr<FunctionDeclStmt> stmt) = 0;
13-
virtual SharedPtr<S> visit(SharedPtr<IfStmt> stmt) = 0;
14-
virtual SharedPtr<S> visit(SharedPtr<WhileStmt> stmt) = 0;
15-
virtual SharedPtr<S> visit(SharedPtr<ForStmt> stmt) = 0;
16-
virtual SharedPtr<S> visit(SharedPtr<ContinueStmt> stmt) = 0;
17-
virtual SharedPtr<S> visit(SharedPtr<BreakStmt> stmt) = 0;
18-
virtual SharedPtr<S> visit(SharedPtr<ReturnStmt> stmt) = 0;
8+
virtual SharedPtr<S> visit(SharedPtr<CompoundStmtNode> stmt) = 0;
9+
virtual SharedPtr<S> visit(SharedPtr<ExprStmtNode> stmt) = 0;
10+
virtual SharedPtr<S> visit(SharedPtr<VarDeclStmtNode> stmt) = 0;
11+
virtual SharedPtr<S> visit(SharedPtr<FunctionDeclStmtNode> stmt) = 0;
12+
virtual SharedPtr<S> visit(SharedPtr<IfStmtNode> stmt) = 0;
13+
virtual SharedPtr<S> visit(SharedPtr<WhileStmtNode> stmt) = 0;
14+
virtual SharedPtr<S> visit(SharedPtr<ForStmtNode> stmt) = 0;
15+
virtual SharedPtr<S> visit(SharedPtr<ContinueStmtNode> stmt) = 0;
16+
virtual SharedPtr<S> visit(SharedPtr<BreakStmtNode> stmt) = 0;
17+
virtual SharedPtr<S> visit(SharedPtr<ReturnStmtNode> stmt) = 0;
1918

20-
virtual SharedPtr<E> visit(SharedPtr<BooleanLiteralExpr> expr) = 0;
21-
virtual SharedPtr<E> visit(SharedPtr<IntegerLiteralExpr> expr) = 0;
22-
virtual SharedPtr<E> visit(SharedPtr<StringLiteralExpr> expr) = 0;
23-
virtual SharedPtr<E> visit(SharedPtr<IdentifierExpr> expr) = 0;
24-
virtual SharedPtr<E> visit(SharedPtr<CallExpr> expr) = 0;
25-
virtual SharedPtr<E> visit(SharedPtr<UnaryOperatorExpr> expr) = 0;
26-
virtual SharedPtr<E> visit(SharedPtr<BinaryOperatorExpr> expr) = 0;
19+
virtual SharedPtr<E> visit(SharedPtr<BooleanLiteralExprNode> expr) = 0;
20+
virtual SharedPtr<E> visit(SharedPtr<IntegerLiteralExprNode> expr) = 0;
21+
virtual SharedPtr<E> visit(SharedPtr<StringLiteralExprNode> expr) = 0;
22+
virtual SharedPtr<E> visit(SharedPtr<IdentifierExprNode> expr) = 0;
23+
virtual SharedPtr<E> visit(SharedPtr<CallExprNode> expr) = 0;
24+
virtual SharedPtr<E> visit(SharedPtr<UnaryOperatorExprNode> expr) = 0;
25+
virtual SharedPtr<E> visit(SharedPtr<BinaryOperatorExprNode> expr) = 0;
2726
};
28-
29-
#endif // STATICSCRIPT_AST_ASTVISITOR_H

include/AST/DeclNode.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#ifndef STATICSCRIPT_AST_DECLNODE_H
2-
#define STATICSCRIPT_AST_DECLNODE_H
1+
#pragma once
32

3+
#include "AST/Node.h"
44
#include "AST/TypeNode.h"
55
#include "AST/StmtNode.h"
66
#include "AST/ExprNode.h"
@@ -10,9 +10,9 @@ enum class VarModifier {
1010
Let, Const, Param
1111
};
1212

13-
class DeclNode {
13+
class DeclNode : public Node {
1414
public:
15-
virtual ~DeclNode() = default;
15+
~DeclNode() override = default;
1616
};
1717

1818
// 变量声明
@@ -53,5 +53,3 @@ class FunctionDeclNode : public DeclNode {
5353
SharedPtr<BuiltinTypeNode> returnType;
5454
SharedPtr<CompoundStmtNode> body;
5555
};
56-
57-
#endif // STATICSCRIPT_AST_DECLNODE_H

include/AST/ExprNode.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
#ifndef STATICSCRIPT_AST_EXPRNODE_H
2-
#define STATICSCRIPT_AST_EXPRNODE_H
1+
#pragma once
32

3+
#include "AST/Node.h"
44
#include "AST/TypeNode.h"
55
#include "AST/StmtNode.h"
66

77
class FunctionDeclNode;
88

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

1414
// 字面量表达式
@@ -101,5 +101,3 @@ class BinaryOperatorExprNode : public ExprNode {
101101
unsigned int opCode;
102102
SharedPtr<ExprNode> lhs, rhs;
103103
};
104-
105-
#endif // STATICSCRIPT_AST_EXPRNODE_H
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
#ifndef STATICSCRIPT_AST_MODULE_H
2-
#define STATICSCRIPT_AST_MODULE_H
1+
#pragma once
32

3+
#include "AST/Node.h"
44
#include "AST/StmtNode.h"
55

66
/// 一个文件就是一个模块, 即一个翻译单元
7-
class Module final {
7+
class ModuleNode final: public Node {
88
public:
9-
virtual ~Module() = default;
9+
~ModuleNode() override = default;
1010

11-
explicit Module(String filename, SharedPtrVector<StmtNode> childStmts);
11+
explicit ModuleNode(String filename, SharedPtrVector<StmtNode> childStmts);
1212

1313
[[nodiscard]] virtual const String &getFilename() const;
1414

@@ -17,6 +17,3 @@ class Module final {
1717
String filename;
1818
SharedPtrVector<StmtNode> childStmts;
1919
};
20-
21-
22-
#endif // STATICSCRIPT_AST_MODULE_H

include/AST/Node.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#ifndef STATICSCRIPT_AST_NODE_H
2-
#define STATICSCRIPT_AST_NODE_H
1+
#pragma once
32

43
class Node {
54
public:
6-
7-
};
8-
9-
#endif // STATICSCRIPT_AST_NODE_H
5+
virtual ~Node() = default;
6+
};

include/AST/StmtNode.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#ifndef STATICSCRIPT_AST_STMTNODE_H
2-
#define STATICSCRIPT_AST_STMTNODE_H
1+
#pragma once
32

3+
#include "AST/Node.h"
44
#include "Util/Alias.h"
55

66
class VarDeclNode;
@@ -9,9 +9,9 @@ class FunctionDeclNode;
99

1010
class ExprNode;
1111

12-
class StmtNode {
12+
class StmtNode: public Node {
1313
public:
14-
virtual ~StmtNode() = default;
14+
~StmtNode() override = default;
1515
};
1616

1717
// 值语句
@@ -114,7 +114,4 @@ class ReturnStmtNode : public StmtNode {
114114
~ReturnStmtNode() override = default;
115115

116116
SharedPtr<ExprNode> argument;
117-
};
118-
119-
120-
#endif // STATICSCRIPT_AST_STMTNODE_H
117+
};

include/AST/TypeNode.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
#ifndef STATICSCRIPT_AST_TYPENODE_H
2-
#define STATICSCRIPT_AST_TYPENODE_H
1+
#pragma once
32

3+
#include "AST/Node.h"
44
#include "Util/Alias.h"
55

66
enum class TypeKind {
77
Unknown, Boolean, Integer, String
88
};
99

10-
class TypeNode {
10+
class TypeNode : public Node {
1111
public:
1212
explicit TypeNode(TypeKind kind);
1313

14-
virtual ~TypeNode() = default;
14+
~TypeNode() override = default;
1515

1616
TypeKind kind;
1717
};
@@ -28,5 +28,3 @@ class BuiltinTypeNode : public TypeNode {
2828
static inline const SharedPtr<BuiltinTypeNode> STRING_TYPE = makeShared<BuiltinTypeNode>(TypeKind::String);
2929
static inline const SharedPtr<BuiltinTypeNode> UNKNOWN_TYPE = makeShared<BuiltinTypeNode>(TypeKind::Unknown);
3030
};
31-
32-
#endif // STATICSCRIPT_AST_TYPENODE_H

include/Entity/Scope.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include "Util/Alias.h"
5+
6+
class TopLevelScope;
7+
8+
class LocalScope;
9+
10+
class Scope {
11+
public:
12+
Scope() = default;
13+
14+
virtual ~Scope() = default;
15+
16+
virtual void addChild(const SharedPtr<LocalScope> &scope);
17+
18+
virtual bool isTopLevel() = 0;
19+
20+
virtual SharedPtr<TopLevelScope> getTopLevel() = 0;
21+
22+
virtual SharedPtr<Scope> getParent() = 0;
23+
24+
protected:
25+
SharedPtrVector<LocalScope> children;
26+
};
27+
28+
class TopLevelScope : public Scope, public std::enable_shared_from_this<TopLevelScope> {
29+
public:
30+
static SharedPtr<TopLevelScope> create() {
31+
return makeShared<TopLevelScope>();
32+
}
33+
34+
TopLevelScope() = default;
35+
36+
~TopLevelScope() override = default;
37+
38+
bool isTopLevel() override {
39+
return true;
40+
}
41+
42+
SharedPtr<TopLevelScope> getTopLevel() override;
43+
44+
SharedPtr<Scope> getParent() override;
45+
};
46+
47+
class LocalScope : public Scope, public std::enable_shared_from_this<LocalScope> {
48+
public:
49+
static SharedPtr<LocalScope> create(const SharedPtr<Scope> &parent);
50+
51+
explicit LocalScope(const SharedPtr<Scope> &parent);
52+
53+
~LocalScope() override = default;
54+
55+
bool isTopLevel() override;
56+
57+
SharedPtr<TopLevelScope> getTopLevel() override;
58+
59+
SharedPtr<Scope> getParent() override;
60+
61+
protected:
62+
SharedPtr<Scope> parent;
63+
};

0 commit comments

Comments
 (0)