Skip to content

Commit 384fcd4

Browse files
author
ApsarasX
committed
refactor: rewrite all code
1 parent e8b3836 commit 384fcd4

45 files changed

Lines changed: 797 additions & 683 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,13 @@
11
cmake_minimum_required(VERSION 3.16)
22

3-
project(StaticScript)
3+
project(StaticScript CXX)
44

55
set(CMAKE_CXX_STANDARD 17)
66

7-
set(CMAKE_PREFIX_PATH /usr/local/lib/cmake/antlr4)
7+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
88

9-
if (CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
10-
set(ANTLR4_JAR_LOCATION ${PROJECT_SOURCE_DIR}/thirdparty/antlr/antlr-4.8-complete.jar)
11-
elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
12-
set(ANTLR4_JAR_LOCATION /usr/local/opt/antlr/antlr-4.8-complete.jar)
13-
endif ()
9+
include(AddANTLR)
1410

15-
find_package(antlr4-runtime REQUIRED)
11+
include_directories(${PROJECT_SOURCE_DIR}/include)
1612

17-
find_package(antlr4-generator REQUIRED)
18-
19-
antlr4_generate(
20-
StaticScriptLexer
21-
${PROJECT_SOURCE_DIR}/grammar/StaticScriptLexer.g4
22-
LEXER
23-
FALSE
24-
FALSE
25-
)
26-
27-
antlr4_generate(
28-
StaticScriptParser
29-
${PROJECT_SOURCE_DIR}/grammar/StaticScriptParser.g4
30-
PARSER
31-
FALSE
32-
TRUE
33-
""
34-
${ANTLR4_TOKEN_FILES_StaticScriptLexer}
35-
${ANTLR4_TOKEN_DIRECTORY_StaticScriptLexer}
36-
)
37-
38-
include_directories(
39-
${PROJECT_SOURCE_DIR}/include
40-
${ANTLR4_INCLUDE_DIR}
41-
${ANTLR4_INCLUDE_DIR_StaticScriptLexer}
42-
${ANTLR4_INCLUDE_DIR_StaticScriptParser}
43-
)
44-
45-
aux_source_directory(${PROJECT_SOURCE_DIR}/src STATICSCRIPT_SRC)
46-
47-
add_executable(
48-
StaticScript
49-
${STATICSCRIPT_SRC}
50-
${ANTLR4_SRC_FILES_StaticScriptLexer}
51-
${ANTLR4_SRC_FILES_StaticScriptParser}
52-
)
53-
54-
add_dependencies(StaticScript antlr4_static)
55-
56-
target_link_libraries(StaticScript PRIVATE antlr4_static)
13+
add_subdirectory(lib)

cmake/modules/AddANTLR.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
set(CMAKE_PREFIX_PATH /usr/local/lib/cmake/antlr4)
2+
3+
if (CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
4+
set(ANTLR4_JAR_LOCATION ${PROJECT_SOURCE_DIR}/thirdparty/antlr/antlr-4.8-complete.jar)
5+
elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
6+
set(ANTLR4_JAR_LOCATION /usr/local/opt/antlr/antlr-4.8-complete.jar)
7+
endif ()
8+
9+
find_package(antlr4-runtime REQUIRED)
10+
11+
find_package(antlr4-generator REQUIRED)
12+
13+
antlr4_generate(
14+
StaticScriptLexer
15+
${PROJECT_SOURCE_DIR}/grammar/StaticScriptLexer.g4
16+
LEXER
17+
FALSE
18+
FALSE
19+
)
20+
21+
antlr4_generate(
22+
StaticScriptParser
23+
${PROJECT_SOURCE_DIR}/grammar/StaticScriptParser.g4
24+
PARSER
25+
FALSE
26+
TRUE
27+
""
28+
${ANTLR4_TOKEN_FILES_StaticScriptLexer}
29+
${ANTLR4_TOKEN_DIRECTORY_StaticScriptLexer}
30+
)
31+
32+
include_directories(
33+
${ANTLR4_INCLUDE_DIR}
34+
${ANTLR4_INCLUDE_DIR_StaticScriptLexer}
35+
${ANTLR4_INCLUDE_DIR_StaticScriptParser}
36+
)
37+
38+
add_library(
39+
parser
40+
OBJECT
41+
${ANTLR4_SRC_FILES_StaticScriptLexer}
42+
${ANTLR4_SRC_FILES_StaticScriptParser}
43+
)

grammar/StaticScriptLexer.g4

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ options {
55
}
66

77
// 关键字
8-
Void: 'void';
98
Boolean: 'boolean';
109
Number: 'number';
1110
String: 'string';
@@ -44,10 +43,8 @@ NotEquals: '!=';
4443

4544
// 字面量
4645
BooleanLiteral: 'true' | 'false';
47-
DecimalIntegerLiteral: '0'
48-
| [1-9] [0-9]*
49-
;
50-
StringLiteral: '\'' SingleStringCharacter* '\'';
46+
IntegerLiteral: '0' | [1-9] [0-9]*;
47+
StringLiteral: '"' StringCharacter* '"';
5148
WhiteSpaces: [\t\u000B\u000C\u0020\u00A0]+ -> channel(HIDDEN);
5249
LineTerminator: [\r\n\u2028\u2029] -> channel(HIDDEN);
5350

@@ -59,7 +56,7 @@ MultiLineComment: '/*' .*? '*/' -> channel(HIDDEN);
5956
SingleLineComment: '//' ~[\r\n\u2028\u2029]* -> channel(HIDDEN);
6057

6158
// Fragment规则
62-
fragment SingleStringCharacter
59+
fragment StringCharacter
6360
: ~['\\\r\n]
6461
| '\\' EscapeSequence
6562
| LineContinuation
@@ -91,6 +88,4 @@ fragment LetterOrDigit
9188
| [0-9]
9289
;
9390

94-
fragment Letter
95-
: [a-zA-Z$_]
96-
;
91+
fragment Letter : [a-zA-Z_] ;

grammar/StaticScriptParser.g4

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ statements
1414
;
1515

1616
statement
17-
: declaration
17+
: emptyStatement
18+
| declarationStatement
1819
| compoundStatement
1920
| expressionStatement
2021
| selectionStatement
2122
| iterationStatement
2223
| jumpStatement
2324
;
2425

26+
emptyStatement
27+
: SemiColon
28+
;
29+
30+
declarationStatement
31+
: declaration
32+
;
33+
2534
declaration
2635
: variableDeclaration
2736
| functionDeclaration
@@ -45,20 +54,19 @@ variableDeclarator
4554
;
4655

4756
typeAnnotation
48-
: Colon predefinedType
49-
;
50-
51-
variableInitializer
52-
: Assign expression
57+
: Colon builtinType
5358
;
5459

55-
predefinedType
56-
: Void
57-
| Boolean
60+
builtinType
61+
: Boolean
5862
| Number
5963
| String
6064
;
6165

66+
variableInitializer
67+
: Assign expression
68+
;
69+
6270
functionDeclaration
6371
: Function Identifier callSignature functionBody
6472
;
@@ -75,7 +83,7 @@ functionBody
7583
: compoundStatement
7684
;
7785

78-
functionCallExpression
86+
callExpression
7987
: Identifier OpenParen argumentList? CloseParen
8088
;
8189

@@ -95,25 +103,26 @@ expression
95103
: literal
96104
| OpenParen expression CloseParen
97105
| Identifier
98-
| functionCallExpression
99-
| expression (Multiply | Divide) expression
100-
| expression (Plus | Minus) expression
101-
| expression (LessThan | MoreThan | LessThanEquals | GreaterThanEquals) expression
102-
| expression (Equals | NotEquals) expression
103-
| <assoc=right> expression Assign expression
106+
| callExpression
107+
| uop=Minus expression
108+
| expression bop=(Multiply | Divide) expression
109+
| expression bop=(Plus | Minus) expression
110+
| expression bop=(LessThan | MoreThan | LessThanEquals | GreaterThanEquals) expression
111+
| expression bop=(Equals | NotEquals) expression
112+
| <assoc=right> expression bop=Assign expression
104113
;
105114

106115
literal
107116
: BooleanLiteral
108-
| DecimalIntegerLiteral
117+
| IntegerLiteral
109118
| StringLiteral
110119
;
111120

112121
selectionStatement
113-
: ifElseStatement
122+
: ifStatement
114123
;
115124

116-
ifElseStatement
125+
ifStatement
117126
: If OpenParen expression CloseParen statement (Else statement)?
118127
;
119128

include/AST/AST.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef STATICSCRIPT_AST_AST_H
2+
#define STATICSCRIPT_AST_AST_H
3+
4+
#include "AST/Decl.h"
5+
#include "AST/Stmt.h"
6+
#include "AST/Type.h"
7+
#include "AST/Expr.h"
8+
#include "AST/Module.h"
9+
10+
#endif //STATICSCRIPT_AST_AST_H

include/AST/Base.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef STATICSCRIPT_AST_BASE_H
2+
#define STATICSCRIPT_AST_BASE_H
3+
4+
#include <memory>
5+
6+
template <typename T>
7+
using SharedPtr = std::shared_ptr<T>;
8+
9+
template <typename T>
10+
using SharedPtrVector = std::vector<SharedPtr<T>>;
11+
12+
template <typename T, typename ...Args>
13+
SharedPtr<T> makeShared(Args&& ...args) {
14+
return std::make_shared<T>(std::forward<Args>(args)...);
15+
}
16+
17+
class ASTNode {
18+
public:
19+
virtual ~ASTNode() = default;
20+
21+
virtual void codegen() = 0;
22+
};
23+
24+
25+
#endif //STATICSCRIPT_AST_BASE_H

include/AST/Decl.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef STATICSCRIPT_AST_DECL_H
2+
#define STATICSCRIPT_AST_DECL_H
3+
4+
#include <vector>
5+
#include <string>
6+
#include "AST/Base.h"
7+
#include "AST/Type.h"
8+
#include "AST/Stmt.h"
9+
#include "AST/Expr.h"
10+
11+
enum class VarModifier {
12+
Let, Const
13+
};
14+
15+
class Decl : public ASTNode {
16+
public:
17+
~Decl() override = default;
18+
};
19+
20+
// 变量声明
21+
class VarDecl : public Decl {
22+
public:
23+
~VarDecl() override = default;
24+
25+
void codegen() override;
26+
27+
VarModifier modifier;
28+
SharedPtr<BuiltinType> type;
29+
std::string name;
30+
SharedPtr<Expr> initVal;
31+
};
32+
33+
// 函数参数声明
34+
class ParmVarDecl : public VarDecl {
35+
public:
36+
~ParmVarDecl() override = default;
37+
};
38+
39+
// 函数声明
40+
class FunctionDecl : public Decl {
41+
public:
42+
~FunctionDecl() override = default;
43+
44+
SharedPtrVector<ParmVarDecl> params;
45+
SharedPtr<Type> returnType;
46+
SharedPtr<CompoundStmt> body;
47+
};
48+
49+
#endif //STATICSCRIPT_AST_DECL_H

0 commit comments

Comments
 (0)