├── .gitignore
├── README.md
├── Spec
├── .DS_Store
├── arm.md
├── ir.md
└── teapl.md
├── doc
├── Compiler_Assignment2.md
├── Compiler_Assignment2.pptx
└── Lex&Yacc_More.md
└── src
├── .gitignore
├── Makefile
├── PrintTeaplaAst.cpp
├── PrintTeaplaAst.h
├── TeaplAst.cpp
├── TeaplAst.h
├── TeaplaAst.cpp
├── TeaplaAst.h
├── compiler.cpp
├── lexer.lex
├── parser.yacc
└── tests
├── test01.refast
├── test01.tea
├── test02.refast
├── test02.tea
├── test03.refast
└── test03.tea
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # compiler_project
2 | This is the compiler project release repo for the SOFT130061 @ Fudan University.
3 |
--------------------------------------------------------------------------------
/Spec/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hxuhack/compiler_project/6baf3220a22e56b54bf0f62d5f73aafd86e1eb1f/Spec/.DS_Store
--------------------------------------------------------------------------------
/Spec/arm.md:
--------------------------------------------------------------------------------
1 | ## Quickguide of ARM ISA
2 |
3 | The document is based on ARM v8-A (e.g., Cortex-A72), which supports AArch64.
4 |
5 | ### Registers
6 |
7 | #### 31 General-purpose Registers:
8 | X0 - X30: 64-bit
9 | W0 - W30: lower 32-bit only
10 |
11 | ```
12 | ADD W0, W1, W2
13 | ```
14 |
15 | X29 is generally used as stack frame pointer (base address of the current stack frame)
16 | X30 is generally used as link register (ret address)
17 |
18 | #### 32 Floating-point/Vector Registers
19 | Floating-point mode:
20 | Q0-Q31: 128-bit
21 | D0-D31: 64-bit
22 | S0-S31: 32-bit
23 | H0-H31: 16-bit
24 | B0-B31: 8-bit
25 |
26 | ```
27 | FADD S0, S1, S2
28 | ```
29 |
30 | Vector mode (multiple independent values in one register):
31 | V0-V31: 128-bit
32 |
33 | ```
34 | FADD V0.2D, V1.2D, V2.2D
35 | ADD V0.1D, V1.1D, V2.1D
36 | ```
37 |
38 | #### More
39 | ZXR (64-bit)/WZR (32-bit): zero register
40 | SP: stack pointer
41 | SPSR: saved process status register (similar to EFLAGs in X86-64)
42 |
43 | #### Calling Convention
44 |
45 | Parameter: X0-X7
46 | Result: X0-X1
47 | Caller-saved Registers: X9-X15
48 | Callee-saved Registers: X19-X28
49 |
50 | ### Instructions
51 |
52 | #### Instruction Format
53 |
54 | Each instruction is 32-bit.
55 |
56 | Valid instant numbers: 12 bit (4 bit for right rotation, and 8 bit for value), e.g., 0x00ab0000 is valid; 0x001ab0000 is invalid.
57 |
58 | #### Load/Store
59 |
60 | Load the 32bit value at address X1 into W0
61 | ```
62 | LDR W0, [X1]
63 | ```
64 |
65 | Load the 32bit value at address X1+12 into W0
66 | ```
67 | LDR W0, [X1, #12]
68 | ```
69 |
70 | Pre-index Load: Load the 32bit value at address X1+12 into W0, update X1 as X1+12
71 | ```
72 | LDR W0, [X1, #12]!
73 | ```
74 |
75 | Post-index Load: Load the 32bit value at address X1 into W0, update X1 as X1+12
76 | ```
77 | LDR W0, [X1], #12
78 | ```
79 |
80 | #### Integer Arithmatic
81 |
82 | #### Floating-point Arithmatic
83 |
84 | #### Branch
85 |
86 | #### Function Call
87 | BL: branch with linkage (call)
88 | RET
89 |
90 | #### More
91 |
92 |
93 | ### Reference:
94 | [Armv8-A Instruction Set Architecture](https%3A%2F%2Fdeveloper.arm.com%2F-%2Fmedia%2FArm%2520Developer%2520Community%2FPDF%2FLearn%2520the%2520Architecture%2FArmv8-A%2520Instruction%2520Set%2520Architecture.pdf%3Frevision%3Debf53406-04fd-4c67-a485-1b329febfb3e&usg=AOvVaw3bCQfc3kXAgqyMYzE8ZbY5&opi=89978449)
95 |
--------------------------------------------------------------------------------
/Spec/ir.md:
--------------------------------------------------------------------------------
1 | ## Intermidiate Code of TeaPL
2 |
3 | ### Basics
4 | **Identifiers**
5 | There are three types of identifiers:
6 | - temporal variables: %1, %2, ...
7 | - local variables: %a, %b, ...
8 | - global variables: @a, %b
9 |
10 | **Types**
11 |
12 | **Type Conversion**
13 |
14 | Data truncatation
15 | ```
16 | %1 = trunc i64 %0, i32
17 | %8 = fptrunc f64 %7, f32
18 | ```
19 |
20 | Data extension
21 | ```
22 | %4 = sext i32 %0, i64
23 | %5 = zext i32 %0, i64
24 | ```
25 |
26 | Integer to float/Float to integer
27 | ```
28 | %2 = sitofp i32 %0, f32
29 | %3 = uitofp i32 %0, f32
30 | %6 = fptoui f32 %2, i32
31 | %7 = fpext f32 %2, f64
32 | ```
33 |
34 | Integer to pointer/Pointer to integer
35 | ```
36 | %9 = inttoptr i64 %4, i8*
37 | %10 = ptrtoint i8* %9, i32
38 | ```
39 |
40 | ### Overall Structures of IR
41 | An IR file is composed of global data and function definitions.
42 | **Global Data**
43 |
44 | **Function Definition**
45 | ```
46 | @define fn i32 @bar(i32 %y){
47 | ...
48 | ret i32 %1;
49 | }
50 | ```
51 |
52 | ### Data Load/Store
53 | We require the memory of all local variables should be explicitly allocated on stack.
54 |
55 | ```
56 | @define fn i32 @bar(i32 %y){
57 | %a = stack i32; //allocate a memory unit of i32 for a local variable a on stack
58 | store i32 1, %a;
59 | %1 = load i32, %a;
60 | ...
61 | }
62 | ```
63 |
64 | Load data from local variables on stack, perform an add operation and store the results back.
65 | ```
66 |
67 | %2 = add i32 %1, 1;
68 | store i32 %2, %a;
69 | ```
70 |
71 | ### Binary Operations
72 | **Integer arithmatic**: The IR supports five types of integer operators, add/sub/mul/div/rem. The operands should be the same type as the return value. For example,
73 | ```
74 | %3 = add i32, %2, %1;
75 | %2 = mul i32, %1, 2;
76 | ```
77 | **Floating-point arithmatic**: Similarly, the IR supports four types of floating-point arithmatic operations, fadd/fsub/fmul/fdiv.
78 | ```
79 | %3 = fadd f32, %1, %2;
80 | %3 = fmul f32, %1, 2.1;
81 | ```
82 |
83 | **Bitwise operations**: and/or/xor/shl/ashr/lshr
84 |
85 | ### Addressing
86 |
87 | ```
88 | %a = stalloc [2 x i32];
89 | %1 = getptr i32*, %a, 0, 1;
90 | store i32 99, %1;
91 | ```
92 |
93 | ```
94 | %struct.st = type { i32, f32 };
95 | %s = salloc %struct.st;
96 | %1 = getptr %struct.st.i*, %s, 0, 0;
97 | store i32 1, %1;
98 | ```
99 |
100 | ### Comparison
101 | ```
102 | %0 = cmp eq i32 4, 5;
103 | %1 = cmp ne f32 0.1, 0.2;
104 | %2 = cmp ult i32 4, 5;
105 | %3 = cmp sgt i32 4, 5;
106 | %4 = cmp ule i32 -4, 5;
107 | %5 = cmp sge i32 4, 5;
108 | ```
109 |
110 | ### Control
111 | **Direct jump**
112 | ```
113 | jmp %BB1;
114 | ```
115 |
116 | **Indirect jump**
117 | ```
118 | %1 = getptr void*, %a, 0, 1;
119 | jmp %1;
120 | ```
121 |
122 | **Conditional jump**
123 | ```
124 | cjmp %0, %BB1, %BB2
125 | ```
126 |
127 | **Multiple branches**
128 | ```
129 | match i32 %0, %BBdefault [
130 | i32 0, %BB1
131 | i32 1, %BB2
132 | i32 2, %BB3 ]
133 | ```
134 |
135 | ### Function Call
136 | ```
137 | call void @foo(1)
138 | %1 = load %a
139 | %2 = call i32 @test(i32 %1)
140 | ```
141 |
142 |
143 |
144 |
--------------------------------------------------------------------------------
/Spec/teapl.md:
--------------------------------------------------------------------------------
1 | ## Grammar of TeaPL
2 |
3 | Each program is composed of variable declarations, function declarations, function definitions, and comments.
4 |
5 | ```
6 | program := (varDeclStmt | structDef | fnDeclStmt | fnDef | comment | < ; >)*
7 | ```
8 |
9 | ### Basic Identifiers, Values, Expressions, and Assignments
10 |
11 | Each identifier begins with an alphabat and contains only alphabats and digits, e.g., alice, a0.
12 |
13 | ```
14 | id := [a-zA-Z][a-zA-Z0-9]*
15 | ```
16 |
17 | TeaPL allows integers, e.g., 123
18 | ```
19 | num := [1-9][0-9]* | 0
20 | ```
21 |
22 | **Arithmatic Expressions**
23 | An expression is a composd of identifiers, values, and operators, e.g., 1+2, a*(b+c). For simplicity, we donot support unary operators, such as ++, +=.
24 |
25 | ```
26 | arithExpr := arithExpr arithBiOp arithExpr | exprUnit
27 | exprUnit := num | id | < ( > arithExpr < ) > | fnCall | id < [ > id | num < ] > | id < . > id | arithUOp exprUnit
28 | arithBiOp := < + > | < - > | < * > | < / >
29 | arithUOp := < - >
30 | ```
31 |
32 | 主要可能是优先级的问题
33 |
34 | **Condition Expressions**
35 |
36 | ```
37 | boolExpr := boolExpr boolBiOp boolUnit | boolUnit
38 | boolUnit := < ( > exprUnit comOp exprUnit < ) > | < ( > boolExpr < ) > | boolUOp boolUnit // we restrict the operands of comparison operators to be exprUnit instead of rightVal to avoid confusing the precedence.
39 | boolBiOp := < && > | < || >
40 | boolUOp := < ! >
41 | comOp := < > > | < < > | < >= > | < <= > | < == > | < != >
42 | ```
43 |
44 | **Assignment**
45 | We restrict neither the left value nor right value can be assignments.
46 |
47 | ```
48 | assignStmt := leftVal < = > rightVal < ; >
49 | leftVal := id | id < [ > id | num < ] > | id < . > id
50 | rightVal := arithExpr | boolExpr
51 | ```
52 |
53 | **Function Call**
54 |
55 | ```
56 | fnCall := id < ( > rightVal (< , > rightVal)* | ϵ < ) >
57 | ```
58 |
59 | ### Variable Declarations
60 |
61 | TeaPL allows declaring one variable each time, which can be either a primitive or array type. Developers can initializate the variable during declaration. For example, it supports the following variable declaration samples.
62 |
63 | **Primitive Types**
64 |
65 | ```
66 | let a:int; // declare a variable of type int; the type field can be ignored.
67 | let b:int = 0; // declare a variable of int and init it with value 0.
68 | ```
69 | **One-level Array**
70 |
71 | ```
72 | let c[10]:int; // declear a variable of integer array.
73 | let d[10]:int = {0}; // declear a variable of integer array and initialize it with zero.
74 | ```
75 |
76 | The grammar is defined as follows.
77 | ```
78 | varDeclStmt := < let > (varDecl | varDef) < ; >
79 | varDecl := id < : > type | id < [ > num < ] >< : > type
80 | varDef := id < : > type < = > rightVal //primitive type
81 | | id < [ > num < ] >< : > type < = > < { > rightVal (< , > rightVal)* | ϵ < } > //array
82 | type := nativeType | structType | ϵ
83 | nativeType := < int >
84 | structType := id
85 | ```
86 |
87 | ### Define A New Structure
88 |
89 | Developers can define new customized types with the preserved keyword struct, e.g.,
90 | ```
91 | struct MyStruct {
92 | node:int,
93 | len:int
94 | }
95 | ```
96 |
97 | The grammar is defined as follows.
98 | ```
99 | structDef := < struct > id < { > (varDecl) (< , > varDecl)* < } >
100 | ```
101 |
102 | ### Function Declaration and Definition
103 |
104 | Each function declaration starts with the keyword fn.
105 | ```
106 | fn foo(a:int, b:int)->int;
107 | fn foo();
108 | ```
109 |
110 | The grammar is defined as follows.
111 | ```
112 | fnDeclStmt := fnDecl < ; >
113 | fnDecl := < fn > id < ( > paramDecl < ) > //without return value
114 | | < fn > id < ( > paramDecl < ) > < -> > type //with return value
115 | paramDecl := varDecl (< , > varDecl)* | ϵ
116 | ```
117 |
118 | **Function Definition**
119 | We can also define a function while declaring it.
120 | ```
121 | fn foo(a:int, b:int)->int {
122 | return a + b;
123 | }
124 | ```
125 |
126 | The grammar is specified as follows.
127 | ```
128 | fnDef := fnDecl codeBlock
129 | codeBlock := < { > (varDeclStmt | assignStmt | callStmt | ifStmt | whileStmt | returnStmt | continueStmt | breakStmt | < ; > )* < } >
130 | returnStmt := < ret > rightVal < ; >
131 | continueStmt := < continue > < ; >
132 | breakStmt := < break > < ; >
133 | ```
134 |
135 | We have already defined the grammar of varDeclStmt and assignStmt. The callStmt is simply a function call terminated with an colon.
136 |
137 | ```
138 | callStmt := fnCall < ; >
139 | ```
140 | Next, we define the grammar of each rest statement type.
141 |
142 | ### Control Flows
143 |
144 | **If-Else Statement**
145 | The condition should be surrounded with a paired parenthesis, and we further restrict the body should be within a paired bracket. The following shows an example.
146 |
147 | ```
148 | if (x >0) {
149 | if (y >0) {
150 | x++;
151 | }
152 | else {
153 | x--;
154 | }
155 | } else {
156 |
157 | }
158 |
159 | ```
160 |
161 | Besides, we restrict the condition expression to be explicit logical operations, e.g., x >0; we donot allow implicit expressions like x, which means. We define the grammar as follows.
162 | ```
163 | ifStmt := < if > < ( > boolExpr < ) > codeBlock ( < else > codeBlock | ϵ )
164 | ```
165 |
166 | **While Statemet**
167 |
168 | Used for the representability of complicated loops.
169 |
170 | Example:
171 | ```
172 | while (x > 0) {
173 | x--;
174 | }
175 | ```
176 |
177 | Definition:
178 |
179 | ```
180 | whileStmt := < while > < ( > boolExpr < ) > codeBlock
181 | ```
182 |
183 | ### Code Comments
184 |
185 | Similar to most programming languages, TeaPL allows line comments with "//" and scope comments with "/* ... */".
186 | ```
187 | int a = 0; // this is a line comment.
188 |
189 | /*
190 | Feature: this is a scope comment
191 | */
192 | fn foo(){
193 | ...
194 | }
195 | ```
196 |
197 | ```
198 | comment := < // > _* | < /* > _* < */ >
199 | ```
200 |
--------------------------------------------------------------------------------
/doc/Compiler_Assignment2.md:
--------------------------------------------------------------------------------
1 |
编译原理实验:Assignment 2
2 |
3 | 指导老师:徐辉
4 | 助教:倪雯倩 陈实立 董方
5 | 崔漠寒 王兆瀚 柏露 张业鸿
6 | 2023 年秋季学期
7 |
8 | ## 实验介绍
9 |
10 | Assignment 2 的目标是实现 TeaPL 的语法分析最终得到抽象语法树。在本实验中,输入是 TeaPL 的源代码,输出是 TeaPL 的抽象语法树。我们将得到抽象语法树打印出来以检查程序的正确性。
11 |
12 | ### 实验工作流
13 |
14 | 本次实验的工作流在 `compiler.cpp` 中可以清楚的看到:我们首先调用 `yyparse` 来生成我们的抽象语法树(在 `TeaplAst.h` 中定义,这是一个 C 风格的定义(方便接入 lex 和 yacc)),然后我们调用 `aA_Program` 将抽象语法树转换为使用 STL 的定义(为了方便后续的实验),最后调用 `print_aA_Program` 将转换后的语法树打印出来。
15 |
16 | ### 文件介绍
17 |
18 | - `compiler.cpp`
19 |
20 | main 函数所在的文件,能够体现实验的总体流程
21 |
22 | - `lexer.lex`
23 |
24 | lexer,需要补全其中的代码以完成本次实验
25 |
26 | - `Makefile`
27 |
28 | 输入 make 进行测试,输入 make clean 清除生成的文件
29 |
30 | - `parser.yacc`
31 |
32 | parser,需要补全其中的代码以完成本次实验
33 |
34 | - `compiler.cpp`
35 |
36 | main 函数所在的文件,能够体现实验的总体流程
37 |
38 | - `PrintTeaplaAst.h`
39 |
40 | 输出语法树的代码
41 |
42 | - `PrintTeaplaAst.cpp`
43 |
44 | 用于输出语法树
45 |
46 | - `TeaplaAst.h`
47 |
48 | 使用 STL 的语法树定义
49 |
50 | - `TeaplaAst.cpp`
51 |
52 | 用于将 `TeaplAst.h` 中定义的语法树转换为 `TeaplaAst.h` 中定义的语法树
53 |
54 | - `TeaplAst.h`
55 |
56 | C 风格的语法树定义
57 |
58 | - `TeaplAst.cpp`
59 |
60 | 用于构造 `TeaplAst.h` 中定义的语法树
61 |
62 | ## 实验要求
63 |
64 | 需要补全 `lexer.lex` 和 `parser.yacc` 中的代码以完成本次实验(补全代码后可以直接 make 进行测试)。
65 |
66 | ### 实验提交
67 |
68 | 提交格式:提交命名如`20307110078.zip`的压缩包(直接将本次实验的文件夹打包即可,不要随意更改实验代码的目录结构)
69 |
70 | ### 评分说明
71 |
72 | 评分时会替换掉 `tests` 文件夹,使用隐藏测试检查代码的正确性,按照通过的测试点数目进行给分。
73 |
--------------------------------------------------------------------------------
/doc/Compiler_Assignment2.pptx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hxuhack/compiler_project/6baf3220a22e56b54bf0f62d5f73aafd86e1eb1f/doc/Compiler_Assignment2.pptx
--------------------------------------------------------------------------------
/doc/Lex&Yacc_More.md:
--------------------------------------------------------------------------------
1 | # Lex + Yacc进阶
2 |
3 | ## 1. Lex的状态
4 |
5 | 单纯使用正则表达式处理注释会非常复杂,因此 Lex 提供了状态机制。
6 |
7 | Lex规则的完整形式为:`<状态>正则表达式 { c代码 }`。初始状态为 `INITIAL`,未标注状态的规则默认在初始状态下使用。
8 |
9 | 状态在声明部分声明,格式为`%start 状态名`,例如声明两个状态用于处理两类注释:
10 |
11 | ```lex
12 | %s COMMENT1
13 | %s COMMENT2
14 | ```
15 |
16 | 转为某个状态的指令为`BEGIN 状态名`,例如遇到`//`,转入`COMMENT1` 状态处理注释(INITIAL 是默认的初始状态):
17 |
18 | ```lex
19 | "//" { BEGIN COMMENT1; }
20 | ```
21 |
22 | 注释结束后,返回`INITIAL`状态继续处理源代码:
23 |
24 | ```lex
25 | "\n" { BEGIN INITIAL; }
26 | ```
27 |
28 | Lex 中特殊符号的处理:代码中难免会包含空格、`\n`、`\t`、`\r` 等特殊符号(为了保证 Parser 的健壮性,最好也处理 ),我们编写的 Lex 规则应接收这些符号,但不需要返回 token 给 Yacc,只更新位置信息即可。
29 |
30 | ## 2. Yacc的优先级和结合性
31 |
32 | 为了消除文法中的冲突,需定义规则的优先级与结合性。
33 |
34 | 在 `parser.yacc` 的声明部分添加:
35 |
36 | ```parser.yacc
37 | %left '+' '-'
38 | %left '*' '/'
39 | %left UMINUS
40 | ```
41 |
42 | `%`后面的结合性可以是: left 左结合、right 右结合、nonassoc 不结合;我们使用的一般都是左结合。
43 |
44 | 声明越靠下的符号优先级越高。没有运算符或运算符无法区分的规则,也可以通过在规则后增加`%prec`规定其优先级,例如负号和减法可如下处理:
45 |
46 | ```parser
47 | exp '-' exp
48 | {
49 | $$ = A_OpExp($1, A_minus, $3);
50 | }
51 | |
52 | '-' exp %prec UMINUS
53 | {
54 | $$ = A_OpExp(A_NumExp(0), A_minus, $2);
55 | }
56 | ```
57 |
58 | 使用`yacc -v parser.yacc`指令编译,可以获得`y.output`文件,包含 Yacc 的状态信息。
59 |
--------------------------------------------------------------------------------
/src/.gitignore:
--------------------------------------------------------------------------------
1 | *.o
2 | *.output
3 | tests/*.ast
--------------------------------------------------------------------------------
/src/Makefile:
--------------------------------------------------------------------------------
1 | CXX = clang++
2 | CXXFLAGS = -std=c++17 -g
3 |
4 | TESTCASE_DIR := tests
5 | TESTCASES = $(wildcard $(TESTCASE_DIR)/*.tea)
6 | LLFILES = $(patsubst $(TESTCASE_DIR)/%.tea,$(TESTCASE_DIR)/%.ast,$(TESTCASES))
7 |
8 | .SECONDARY: $(LLFILES)
9 |
10 | run: $(patsubst $(TESTCASE_DIR)/%.tea,$(TESTCASE_DIR)/%.ast,$(TESTCASES))
11 |
12 | $(TESTCASE_DIR)/%.ast: $(TESTCASE_DIR)/%.tea $(TESTCASE_DIR)/%.refast compiler
13 | @./compiler $< $@
14 | @diff $@ $(word 2,$^)
15 | @echo PASS $*
16 | @echo
17 |
18 | compiler: y.tab.o lex.yy.o TeaplAst.o TeaplaAst.o PrintTeaplaAst.o compiler.o
19 | $(CXX) $(CXXFLAGS) -o compiler $^
20 |
21 | y.tab.cpp: parser.yacc TeaplAst.h
22 | # yacc --verbose --debug -d $< -o y.tab.cpp
23 | yacc -o y.tab.cpp -d $< -v --debug
24 |
25 | y.tab.hpp: parser.yacc
26 | # yacc --verbose --debug -d $< -o y.tab.cpp
27 | yacc -o y.tab.cpp -d $< -v --debug
28 |
29 | lex.yy.cpp: lexer.lex y.tab.hpp y.tab.cpp
30 | lex -o lex.yy.cpp $<
31 |
32 | y.tab.o: y.tab.cpp y.tab.hpp
33 | $(CXX) $(CXXFLAGS) -c $<
34 |
35 | lex.yy.o: lex.yy.cpp y.tab.hpp
36 | $(CXX) $(CXXFLAGS) -c $<
37 |
38 | TeaplAst.o: TeaplAst.cpp TeaplAst.h
39 | $(CXX) $(CXXFLAGS) -c $<
40 |
41 | TeaplaAst.o: TeaplaAst.cpp TeaplaAst.h
42 | $(CXX) $(CXXFLAGS) -c $<
43 |
44 | PrintTeaplaAst.o: PrintTeaplaAst.cpp PrintTeaplaAst.h
45 | $(CXX) $(CXXFLAGS) -c $<
46 |
47 | compiler.o: compiler.cpp TeaplAst.o TeaplaAst.o PrintTeaplaAst.o y.tab.o lex.yy.o
48 | $(CXX) $(CXXFLAGS) -c $<
49 |
50 | clean:
51 | rm *.o *.output y.tab.cpp y.tab.hpp lex.yy.cpp compiler tests/*.ast
52 |
--------------------------------------------------------------------------------
/src/PrintTeaplaAst.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include "PrintTeaplaAst.h"
5 |
6 | using namespace std;
7 |
8 | void print_aA_Type(aA_type type, ostream& os){
9 | if(!type) return;
10 | switch(type->type){
11 | case A_nativeTypeKind:{
12 | switch(type->u.nativeType){
13 | case A_intTypeKind:{
14 | os << "int";
15 | break;
16 | }
17 | }
18 | break;
19 | }
20 | case A_structTypeKind:{
21 | os << *(type->u.structType);
22 | break;
23 | }
24 | }
25 | }
26 |
27 | void print_aA_FnCall(aA_fnCall fnCall, ostream& os){
28 | if(!fnCall) return;
29 | os << *(fnCall->fn);
30 | os << "(";
31 | if(!fnCall->vals.empty()){
32 | print_aA_RightVal(fnCall->vals[0], os);
33 | for(int i=1; ivals.size(); ++i){
34 | os << ", ";
35 | print_aA_RightVal(fnCall->vals[i], os);
36 | }
37 | }
38 | os << ")";
39 | }
40 |
41 | void print_aA_IndexExpr(aA_indexExpr indexExpr, ostream& os){
42 | if(!indexExpr) return;
43 | switch(indexExpr->kind){
44 | case A_numIndexKind:{
45 | os << indexExpr->u.num;
46 | break;
47 | }
48 | case A_idIndexKind:{
49 | os << *(indexExpr->u.id);
50 | break;
51 | }
52 | }
53 | }
54 |
55 | void print_aA_ArrayExpr(aA_arrayExpr arrayExpr, ostream& os){
56 | if(!arrayExpr) return;
57 | os << *(arrayExpr->arr) << "[";
58 | print_aA_IndexExpr(arrayExpr->idx, os);
59 | os << "]";
60 | }
61 |
62 | void print_aA_MemberExpr(aA_memberExpr memberExpr, ostream& os){
63 | if(!memberExpr) return;
64 | os << *(memberExpr->structId) << ".";
65 | os << *(memberExpr->memberId);
66 | }
67 |
68 | void print_aA_ExprUnit(aA_exprUnit exprUnit, ostream& os){
69 | if(!exprUnit) return;
70 | switch(exprUnit->kind){
71 | case A_numExprKind:{
72 | os << exprUnit->u.num;
73 | break;
74 | }
75 | case A_idExprKind:{
76 | os << *(exprUnit->u.id);
77 | break;
78 | }
79 | case A_arithExprKind:{
80 | os << "(";
81 | print_aA_ArithExpr(exprUnit->u.arithExpr, os);
82 | os << ")";
83 | break;
84 | }
85 | case A_fnCallKind:{
86 | print_aA_FnCall(exprUnit->u.callExpr, os);
87 | break;
88 | }
89 | case A_arrayExprKind:{
90 | print_aA_ArrayExpr(exprUnit->u.arrayExpr, os);
91 | break;
92 | }
93 | case A_memberExprKind:{
94 | print_aA_MemberExpr(exprUnit->u.memberExpr, os);
95 | break;
96 | }
97 | case A_arithUExprKind:{
98 | print_aA_ArithUExpr(exprUnit->u.arithUExpr, os);
99 | break;
100 | }
101 | }
102 | }
103 |
104 | static const string arithBiOps[] = {"+", "-", "*", "/"};
105 |
106 | void print_aA_ArithBiOpExpr(aA_arithBiOpExpr arithBiOpExpr, ostream& os){
107 | if(!arithBiOpExpr) return;
108 | print_aA_ArithExpr(arithBiOpExpr->left, os);
109 | os << arithBiOps[arithBiOpExpr->op];
110 | print_aA_ArithExpr(arithBiOpExpr->right, os);
111 | }
112 |
113 | static const string arithUOps[] = {"-"};
114 |
115 | void print_aA_ArithUExpr(aA_arithUExpr arithUExpr, ostream& os){
116 | if(!arithUExpr) return;
117 | os << arithUOps[arithUExpr->op];
118 | print_aA_ExprUnit(arithUExpr->expr, os);
119 | }
120 |
121 | void print_aA_ArithExpr(aA_arithExpr arithExpr, ostream& os){
122 | if(!arithExpr) return;
123 | switch(arithExpr->kind){
124 | case A_arithBiOpExprKind:{
125 | print_aA_ArithBiOpExpr(arithExpr->u.arithBiOpExpr, os);
126 | break;
127 | }
128 | case A_exprUnitKind:{
129 | print_aA_ExprUnit(arithExpr->u.exprUnit, os);
130 | break;
131 | }
132 | }
133 | }
134 |
135 | static const string boolBiOps[] = {"&&", "||"};
136 |
137 | void print_aA_BoolBiOpExpr(aA_boolBiOpExpr boolBiOpExpr, ostream& os){
138 | if(!boolBiOpExpr) return;
139 | print_aA_BoolExpr(boolBiOpExpr->left, os);
140 | os << boolBiOps[boolBiOpExpr->op];
141 | print_aA_BoolUnit(boolBiOpExpr->right, os);
142 | }
143 |
144 | static const string boolUOps[] = {"!"};
145 |
146 | void print_aA_BoolUOpExpr(aA_boolUOpExpr boolUOpExpr, ostream& os){
147 | if(!boolUOpExpr) return;
148 | os << boolUOps[boolUOpExpr->op];
149 | print_aA_BoolUnit(boolUOpExpr->cond, os);
150 | }
151 |
152 | void print_aA_BoolExpr(aA_boolExpr boolExpr, ostream& os){
153 | if(!boolExpr) return;
154 | switch(boolExpr->kind){
155 | case A_boolBiOpExprKind:{
156 | print_aA_BoolBiOpExpr(boolExpr->u.boolBiOpExpr, os);
157 | break;
158 | }
159 | case A_boolUnitKind:{
160 | print_aA_BoolUnit(boolExpr->u.boolUnit, os);
161 | break;
162 | }
163 | }
164 | }
165 |
166 | static const string comOps[] = {"<", "<=", ">", ">=", "==", "!="};
167 |
168 | void print_aA_ComExpr(aA_comExpr comExpr, ostream& os){
169 | if(!comExpr) return;
170 | print_aA_ExprUnit(comExpr->left, os);
171 | os << comOps[comExpr->op];
172 | print_aA_ExprUnit(comExpr->right, os);
173 | }
174 |
175 | void print_aA_BoolUnit(aA_boolUnit boolUnit, ostream& os){
176 | if(!boolUnit) return;
177 | switch(boolUnit->kind){
178 | case A_comOpExprKind:{
179 | os << "(";
180 | print_aA_ComExpr(boolUnit->u.comExpr, os);
181 | os << ")";
182 | break;
183 | }
184 | case A_boolExprKind:{
185 | os << "(";
186 | print_aA_BoolExpr(boolUnit->u.boolExpr, os);
187 | os << ")";
188 | break;
189 | }
190 | case A_boolUOpExprKind:{
191 | print_aA_BoolUOpExpr(boolUnit->u.boolUOpExpr, os);
192 | break;
193 | }
194 | }
195 | }
196 |
197 | void print_aA_RightVal(aA_rightVal rightVal, ostream& os){
198 | if(!rightVal) return;
199 | switch(rightVal->kind){
200 | case A_arithExprValKind:{
201 | print_aA_ArithExpr(rightVal->u.arithExpr, os);
202 | break;
203 | }
204 | case A_boolExprValKind:{
205 | print_aA_BoolExpr(rightVal->u.boolExpr, os);
206 | break;
207 | }
208 | }
209 | }
210 |
211 | void print_aA_LeftVal(aA_leftVal leftVal, ostream& os){
212 | if(!leftVal) return;
213 | switch(leftVal->kind){
214 | case A_varValKind:{
215 | os << *(leftVal->u.id);
216 | break;
217 | }
218 | case A_arrValKind:{
219 | print_aA_ArrayExpr(leftVal->u.arrExpr, os);
220 | break;
221 | }
222 | case A_memberValKind:{
223 | print_aA_MemberExpr(leftVal->u.memberExpr, os);
224 | break;
225 | }
226 | }
227 | }
228 |
229 | void print_aA_AssignStmt(aA_assignStmt assignStmt, ostream& os){
230 | if(!assignStmt) return;
231 | print_aA_LeftVal(assignStmt->leftVal, os);
232 | os << " = ";
233 | print_aA_RightVal(assignStmt->rightVal, os);
234 | os << ";";
235 | }
236 |
237 | void print_aA_VarDeclScalar(aA_varDeclScalar varDeclScalar, ostream& os){
238 | if(!varDeclScalar) return;
239 | os << *(varDeclScalar->id) << ":";
240 | print_aA_Type(varDeclScalar->type, os);
241 | }
242 |
243 | void print_aA_VarDeclArray(aA_varDeclArray varDeclArray, ostream& os){
244 | if(!varDeclArray) return;
245 | os << *(varDeclArray->id) << "[";
246 | os << varDeclArray->len << "]:";
247 | print_aA_Type(varDeclArray->type, os);
248 | }
249 |
250 | void print_aA_VarDecl(aA_varDecl varDecl, ostream& os){
251 | if(!varDecl) return;
252 | switch(varDecl->kind){
253 | case A_varDeclScalarKind:{
254 | print_aA_VarDeclScalar(varDecl->u.declScalar, os);
255 | break;
256 | }
257 | case A_varDeclArrayKind:{
258 | print_aA_VarDeclArray(varDecl->u.declArray, os);
259 | break;
260 | }
261 | }
262 | }
263 |
264 | void print_aA_VarDefScalar(aA_varDefScalar varDefScalar, ostream& os){
265 | if(!varDefScalar) return;
266 | os << *(varDefScalar->id) << ":";
267 | print_aA_Type(varDefScalar->type, os);
268 | os << " = ";
269 | print_aA_RightVal(varDefScalar->val, os);
270 | }
271 |
272 | void print_aA_VarDefArray(aA_varDefArray varDefArray, ostream& os){
273 | if(!varDefArray) return;
274 | os << *(varDefArray->id) << "[";
275 | os << varDefArray->len << "]:";
276 | print_aA_Type(varDefArray->type, os);
277 | os << " = {";
278 | if(!varDefArray->vals.empty()){
279 | print_aA_RightVal(varDefArray->vals[0], os);
280 | for(int i=1; ivals.size(); ++i){
281 | os << ", ";
282 | print_aA_RightVal(varDefArray->vals[i], os);
283 | }
284 | }
285 | os << "}";
286 | }
287 |
288 | void print_aA_VarDef(aA_varDef varDef, ostream& os){
289 | if(!varDef) return;
290 | switch(varDef->kind){
291 | case A_varDefScalarKind:{
292 | print_aA_VarDefScalar(varDef->u.defScalar, os);
293 | break;
294 | }
295 | case A_varDefArrayKind:{
296 | print_aA_VarDefArray(varDef->u.defArray, os);
297 | break;
298 | }
299 | }
300 | }
301 |
302 | void print_aA_VarDeclStmt(aA_varDeclStmt varDeclStmt, ostream& os){
303 | if(!varDeclStmt) return;
304 | os << "let ";
305 | switch(varDeclStmt->kind){
306 | case A_varDeclKind:{
307 | print_aA_VarDecl(varDeclStmt->u.varDecl, os);
308 | break;
309 | }
310 | case A_varDefKind:{
311 | print_aA_VarDef(varDeclStmt->u.varDef, os);
312 | break;
313 | }
314 | }
315 | os << ";";
316 | }
317 |
318 | void print_aA_StructDef(aA_structDef structDef, ostream& os){
319 | if(!structDef) return;
320 | os << "struct " << *(structDef->id) << " {";
321 | if(!structDef->varDecls.empty()){
322 | os << "\n";
323 | print_aA_VarDecl(structDef->varDecls[0], os);
324 | for(int i=1; ivarDecls.size(); ++i){
325 | os << ",\n";
326 | print_aA_VarDecl(structDef->varDecls[i], os);
327 | }
328 | }
329 | os << "\n}";
330 | }
331 |
332 | void print_aA_ParamDecl(aA_paramDecl paramDecl, ostream& os){
333 | if(!paramDecl) return;
334 | if(!paramDecl->varDecls.empty()){
335 | print_aA_VarDecl(paramDecl->varDecls[0], os);
336 | for(int i=1; ivarDecls.size(); ++i){
337 | os << ", ";
338 | print_aA_VarDecl(paramDecl->varDecls[i], os);
339 | }
340 | }
341 | }
342 |
343 | void print_aA_FnDecl(aA_fnDecl fnDecl, ostream& os){
344 | if(!fnDecl) return;
345 | os << "fn " << *(fnDecl->id) << "(";
346 | print_aA_ParamDecl(fnDecl->paramDecl, os);
347 | os << ")";
348 | if(fnDecl->type){
349 | os << "->";
350 | print_aA_Type(fnDecl->type, os);
351 | }
352 | }
353 |
354 | void print_aA_FnDef(aA_fnDef fnDef, ostream& os){
355 | if(!fnDef) return;
356 | print_aA_FnDecl(fnDef->fnDecl, os);
357 | os << "{";
358 | for(auto &stmt : fnDef->stmts){
359 | os << "\n";
360 | print_aA_CodeBlockStmt(stmt, os);
361 | }
362 | os << "\n}";
363 | }
364 |
365 | void print_aA_IfStmt(aA_ifStmt ifStmt, ostream& os){
366 | if(!ifStmt) return;
367 | os << "if(";
368 | print_aA_BoolExpr(ifStmt->boolExpr, os);
369 | os << "){";
370 | for(auto &stmt : ifStmt->ifStmts){
371 | os << "\n";
372 | print_aA_CodeBlockStmt(stmt, os);
373 | }
374 | os << "\n}";
375 | if(!ifStmt->elseStmts.empty()){
376 | os << "else{";
377 | for(auto &stmt : ifStmt->elseStmts){
378 | os << "\n";
379 | print_aA_CodeBlockStmt(stmt, os);
380 | }
381 | os << "\n}";
382 | }
383 | }
384 |
385 | void print_aA_WhileStmt(aA_whileStmt whileStmt, ostream& os){
386 | if(!whileStmt) return;
387 | os << "while(";
388 | print_aA_BoolExpr(whileStmt->boolExpr, os);
389 | os << "){";
390 | for(auto &stmt : whileStmt->whileStmts){
391 | os << "\n";
392 | print_aA_CodeBlockStmt(stmt, os);
393 | }
394 | os << "\n}";
395 | }
396 |
397 | void print_aA_CallStmt(aA_callStmt callStmt, ostream& os){
398 | if(!callStmt) return;
399 | print_aA_FnCall(callStmt->fnCall, os);
400 | os << ";";
401 | }
402 |
403 | void print_aA_ReturnStmt(aA_returnStmt returnStmt, ostream& os){
404 | if(!returnStmt) return;
405 | os << "ret ";
406 | print_aA_RightVal(returnStmt->retVal, os);
407 | os << ";";
408 | }
409 |
410 | void print_aA_CodeBlockStmt(aA_codeBlockStmt codeBlockStmt, ostream& os){
411 | if(!codeBlockStmt) return;
412 | switch(codeBlockStmt->kind){
413 | case A_nullStmtKind:{
414 | os << ";";
415 | break;
416 | }
417 | case A_varDeclStmtKind:{
418 | print_aA_VarDeclStmt(codeBlockStmt->u.varDeclStmt, os);
419 | break;
420 | }
421 | case A_assignStmtKind:{
422 | print_aA_AssignStmt(codeBlockStmt->u.assignStmt, os);
423 | break;
424 | }
425 | case A_callStmtKind:{
426 | print_aA_CallStmt(codeBlockStmt->u.callStmt, os);
427 | break;
428 | }
429 | case A_ifStmtKind:{
430 | print_aA_IfStmt(codeBlockStmt->u.ifStmt, os);
431 | break;
432 | }
433 | case A_whileStmtKind:{
434 | print_aA_WhileStmt(codeBlockStmt->u.whileStmt, os);
435 | break;
436 | }
437 | case A_returnStmtKind:{
438 | print_aA_ReturnStmt(codeBlockStmt->u.returnStmt, os);
439 | break;
440 | }
441 | case A_continueStmtKind:{
442 | os << "continue;";
443 | break;
444 | }
445 | case A_breakStmtKind:{
446 | os << "break;";
447 | break;
448 | }
449 | }
450 | }
451 |
452 | void print_aA_FnDeclStmt(aA_fnDeclStmt fnDeclStmt, ostream& os){
453 | if(!fnDeclStmt) return;
454 | print_aA_FnDecl(fnDeclStmt->fnDecl, os);
455 | os << ";";
456 | }
457 |
458 | void print_aA_ProgramElement(aA_programElement programElement, ostream& os){
459 | if(!programElement) return;
460 | switch(programElement->kind){
461 | case A_programNullStmtKind:{
462 | os << ";";
463 | break;
464 | }
465 | case A_programVarDeclStmtKind:{
466 | print_aA_VarDeclStmt(programElement->u.varDeclStmt, os);
467 | break;
468 | }
469 | case A_programStructDefKind:{
470 | print_aA_StructDef(programElement->u.structDef, os);
471 | break;
472 | }
473 | case A_programFnDeclStmtKind:{
474 | print_aA_FnDeclStmt(programElement->u.fnDeclStmt, os);
475 | break;
476 | }
477 | case A_programFnDefKind:{
478 | print_aA_FnDef(programElement->u.fnDef, os);
479 | break;
480 | }
481 | }
482 | }
483 |
484 | void print_aA_Program(aA_program program, ostream& os){
485 | if(!program) return;
486 | for(auto &programElement : program->programElements){
487 | print_aA_ProgramElement(programElement, os);
488 | os << "\n\n";
489 | }
490 | }
--------------------------------------------------------------------------------
/src/PrintTeaplaAst.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include "TeaplaAst.h"
6 |
7 | using std::ostream;
8 |
9 | void print_aA_Type(aA_type type, ostream& os);
10 | void print_aA_FnCall(aA_fnCall fnCall, ostream& os);
11 | void print_aA_IndexExpr(aA_indexExpr indexExpr, ostream& os);
12 | void print_aA_ArrayExpr(aA_arrayExpr arrayExpr, ostream& os);
13 | void print_aA_MemberExpr(aA_memberExpr memberExpr, ostream& os);
14 | void print_aA_ExprUnit(aA_exprUnit exprUnit, ostream& os);
15 | void print_aA_ArithBiOpExpr(aA_arithBiOpExpr arithBiOpExpr, ostream& os);
16 | void print_aA_ArithUExpr(aA_arithUExpr arithUExpr, ostream& os);
17 | void print_aA_ArithExpr(aA_arithExpr arithExpr, ostream& os);
18 | void print_aA_BoolBiOpExpr(aA_boolBiOpExpr boolBiOpExpr, ostream& os);
19 | void print_aA_BoolUOpExpr(aA_boolUOpExpr boolUOpExpr, ostream& os);
20 | void print_aA_BoolExpr(aA_boolExpr boolExpr, ostream& os);
21 | void print_aA_ComExpr(aA_comExpr comExpr, ostream& os);
22 | void print_aA_BoolUnit(aA_boolUnit boolUnit, ostream& os);
23 | void print_aA_RightVal(aA_rightVal rightVal, ostream& os);
24 | void print_aA_LeftVal(aA_leftVal leftVal, ostream& os);
25 | void print_aA_AssignStmt(aA_assignStmt assignStmt, ostream& os);
26 | void print_aA_VarDeclScalar(aA_varDeclScalar varDeclScalar, ostream& os);
27 | void print_aA_VarDeclArray(aA_varDeclArray varDeclArray, ostream& os);
28 | void print_aA_VarDecl(aA_varDecl varDecl, ostream& os);
29 | void print_aA_VarDefScalar(aA_varDefScalar varDefScalar, ostream& os);
30 | void print_aA_VarDefArray(aA_varDefArray varDefArray, ostream& os);
31 | void print_aA_VarDef(aA_varDef varDef, ostream& os);
32 | void print_aA_VarDeclStmt(aA_varDeclStmt varDeclStmt, ostream& os);
33 | void print_aA_StructDef(aA_structDef structDef, ostream& os);
34 | void print_aA_ParamDecl(aA_paramDecl paramDecl, ostream& os);
35 | void print_aA_FnDecl(aA_fnDecl fnDecl, ostream& os);
36 | void print_aA_FnDef(aA_fnDef fnDef, ostream& os);
37 | void print_aA_IfStmt(aA_ifStmt ifStmt, ostream& os);
38 | void print_aA_WhileStmt(aA_whileStmt whileStmt, ostream& os);
39 | void print_aA_CallStmt(aA_callStmt callStmt, ostream& os);
40 | void print_aA_ReturnStmt(aA_returnStmt returnStmt, ostream& os);
41 | void print_aA_CodeBlockStmt(aA_codeBlockStmt codeBlockStmt, ostream& os);
42 | void print_aA_FnDeclStmt(aA_fnDeclStmt fnDeclStmt, ostream& os);
43 | void print_aA_ProgramElement(aA_programElement programElement, ostream& os);
44 | void print_aA_Program(aA_program program, ostream& os);
45 |
--------------------------------------------------------------------------------
/src/TeaplAst.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "TeaplAst.h"
3 |
4 | A_pos A_Pos(int line, int col){
5 | A_pos p = (A_pos)malloc(sizeof(*p));
6 | p->line = line;
7 | p->col = col;
8 | return p;
9 | }
10 |
11 | A_tokenId A_TokenId(A_pos pos, char* id){
12 | A_tokenId p = (A_tokenId)malloc(sizeof(*p));
13 | p->pos = pos;
14 | p->id = id;
15 | return p;
16 | }
17 |
18 | A_tokenNum A_TokenNum(A_pos pos, int num){
19 | A_tokenNum p = (A_tokenNum)malloc(sizeof(*p));
20 | p->pos = pos;
21 | p->num = num;
22 | return p;
23 | }
24 |
25 | A_type A_NativeType(A_pos pos, A_nativeType ntype){
26 | A_type p = (A_type)malloc(sizeof(*p));
27 | p->pos = pos;
28 | p->type = A_nativeTypeKind;
29 | p->u.nativeType = ntype;
30 | return p;
31 | }
32 |
33 | A_type A_StructType(A_pos pos, char* stype){
34 | A_type p = (A_type)malloc(sizeof(*p));
35 | p->pos = pos;
36 | p->type = A_structTypeKind;
37 | p->u.structType = stype;
38 | return p;
39 | }
40 |
41 | A_rightValList A_RightValList(A_rightVal head, A_rightValList tail){
42 | A_rightValList p = (A_rightValList)malloc(sizeof(*p));
43 | p->head= head;
44 | p->tail = tail;
45 | return p;
46 | }
47 |
48 | A_fnCall A_FnCall(A_pos pos, char* fn, A_rightValList vals){
49 | A_fnCall p = (A_fnCall)malloc(sizeof(*p));
50 | p->pos= pos;
51 | p->fn= fn;
52 | p->vals = vals;
53 | return p;
54 | }
55 |
56 | A_indexExpr A_NumIndexExpr(A_pos pos, int num){
57 | A_indexExpr p = (A_indexExpr)malloc(sizeof(*p));
58 | p->pos = pos;
59 | p->kind= A_numIndexKind;
60 | p->u.num = num;
61 | return p;
62 | }
63 |
64 | A_indexExpr A_IdIndexExpr(A_pos pos, char* id){
65 | A_indexExpr p = (A_indexExpr)malloc(sizeof(*p));
66 | p->pos = pos;
67 | p->kind= A_idIndexKind;
68 | p->u.id = id;
69 | return p;
70 | }
71 |
72 | A_arrayExpr A_ArrayExpr(A_pos pos, char* arr, A_indexExpr idx){
73 | A_arrayExpr p = (A_arrayExpr)malloc(sizeof(*p));
74 | p->pos = pos;
75 | p->arr= arr;
76 | p->idx = idx;
77 | return p;
78 | }
79 |
80 | A_memberExpr A_MemberExpr(A_pos pos, char* structId, char* memberId){
81 | A_memberExpr p = (A_memberExpr)malloc(sizeof(*p));
82 | p->pos = pos;
83 | p->structId= structId;
84 | p->memberId = memberId;
85 | return p;
86 | }
87 |
88 | A_exprUnit A_NumExprUnit(A_pos pos, int num){
89 | A_exprUnit p = (A_exprUnit)malloc(sizeof(*p));
90 | p->pos = pos;
91 | p->kind= A_numExprKind;
92 | p->u.num = num;
93 | return p;
94 | }
95 |
96 | A_exprUnit A_IdExprUnit(A_pos pos, char* id){
97 | A_exprUnit p = (A_exprUnit)malloc(sizeof(*p));
98 | p->pos = pos;
99 | p->kind= A_idExprKind;
100 | p->u.id = id;
101 | return p;
102 | }
103 |
104 | A_exprUnit A_ArithExprUnit(A_pos pos, A_arithExpr arithExpr){
105 | A_exprUnit p = (A_exprUnit)malloc(sizeof(*p));
106 | p->pos = pos;
107 | p->kind= A_arithExprKind;
108 | p->u.arithExpr = arithExpr;
109 | return p;
110 | }
111 |
112 | A_exprUnit A_CallExprUnit(A_pos pos, A_fnCall callExpr){
113 | A_exprUnit p = (A_exprUnit)malloc(sizeof(*p));
114 | p->pos = pos;
115 | p->kind= A_fnCallKind;
116 | p->u.callExpr = callExpr;
117 | return p;
118 | }
119 |
120 | A_exprUnit A_ArrayExprUnit(A_pos pos, A_arrayExpr arrayExpr){
121 | A_exprUnit p = (A_exprUnit)malloc(sizeof(*p));
122 | p->pos = pos;
123 | p->kind= A_arrayExprKind;
124 | p->u.arrayExpr = arrayExpr;
125 | return p;
126 | }
127 |
128 | A_exprUnit A_MemberExprUnit(A_pos pos, A_memberExpr memberExpr){
129 | A_exprUnit p = (A_exprUnit)malloc(sizeof(*p));
130 | p->pos = pos;
131 | p->kind= A_memberExprKind;
132 | p->u.memberExpr = memberExpr;
133 | return p;
134 | }
135 |
136 | A_exprUnit A_ArithUExprUnit(A_pos pos, A_arithUExpr arithUExpr){
137 | A_exprUnit p = (A_exprUnit)malloc(sizeof(*p));
138 | p->pos = pos;
139 | p->kind= A_arithUExprKind;
140 | p->u.arithUExpr = arithUExpr;
141 | return p;
142 | }
143 |
144 | A_arithBiOpExpr A_ArithBiOpExpr(A_pos pos, A_arithBiOp op, A_arithExpr left, A_arithExpr right){
145 | A_arithBiOpExpr p = (A_arithBiOpExpr)malloc(sizeof(*p));
146 | p->pos = pos;
147 | p->op= op;
148 | p->left = left;
149 | p->right = right;
150 | return p;
151 | }
152 |
153 | A_arithUExpr A_ArithUExpr(A_pos pos, A_arithUOp op, A_exprUnit expr){
154 | A_arithUExpr p = (A_arithUExpr)malloc(sizeof(*p));
155 | p->pos = pos;
156 | p->op= op;
157 | p->expr = expr;
158 | return p;
159 | }
160 |
161 | A_arithExpr A_ArithBiOp_Expr(A_pos pos, A_arithBiOpExpr arithBiOpExpr){
162 | A_arithExpr p = (A_arithExpr)malloc(sizeof(*p));
163 | p->pos = pos;
164 | p->kind= A_arithBiOpExprKind;
165 | p->u.arithBiOpExpr = arithBiOpExpr;
166 | return p;
167 | }
168 |
169 | A_arithExpr A_ExprUnit(A_pos pos, A_exprUnit exprUnit){
170 | A_arithExpr p = (A_arithExpr)malloc(sizeof(*p));
171 | p->pos = pos;
172 | p->kind= A_exprUnitKind;
173 | p->u.exprUnit = exprUnit;
174 | return p;
175 | }
176 |
177 | A_boolBiOpExpr A_BoolBiOpExpr(A_pos pos, A_boolBiOp op, A_boolExpr left, A_boolUnit right){
178 | A_boolBiOpExpr p = (A_boolBiOpExpr)malloc(sizeof(*p));
179 | p->pos = pos;
180 | p->op= op;
181 | p->left = left;
182 | p->right = right;
183 | return p;
184 | }
185 |
186 | A_boolUOpExpr A_BoolUOpExpr(A_pos pos, A_boolUOp op, A_boolUnit cond){
187 | A_boolUOpExpr p = (A_boolUOpExpr)malloc(sizeof(*p));
188 | p->pos = pos;
189 | p->op= op;
190 | p->cond = cond;
191 | return p;
192 | }
193 |
194 | A_boolExpr A_BoolBiOp_Expr(A_pos pos, A_boolBiOpExpr boolBiOpExpr){
195 | A_boolExpr p = (A_boolExpr)malloc(sizeof(*p));
196 | p->pos = pos;
197 | p->kind= A_boolBiOpExprKind;
198 | p->u.boolBiOpExpr = boolBiOpExpr;
199 | return p;
200 | }
201 |
202 | A_boolExpr A_BoolExpr(A_pos pos, A_boolUnit boolUnit){
203 | A_boolExpr p = (A_boolExpr)malloc(sizeof(*p));
204 | p->pos = pos;
205 | p->kind= A_boolUnitKind;
206 | p->u.boolUnit = boolUnit;
207 | return p;
208 | }
209 |
210 | A_comExpr A_ComExpr(A_pos pos, A_comOp op, A_exprUnit left, A_exprUnit right){
211 | A_comExpr p = (A_comExpr)malloc(sizeof(*p));
212 | p->pos= pos;
213 | p->op = op;
214 | p->left = left;
215 | p->right = right;
216 | return p;
217 | }
218 |
219 | A_boolUnit A_ComExprUnit(A_pos pos, A_comExpr comExpr){
220 | A_boolUnit p = (A_boolUnit)malloc(sizeof(*p));
221 | p->pos = pos;
222 | p->kind= A_comOpExprKind;
223 | p->u.comExpr = comExpr;
224 | return p;
225 | }
226 |
227 | A_boolUnit A_BoolExprUnit(A_pos pos, A_boolExpr boolExpr){
228 | A_boolUnit p = (A_boolUnit)malloc(sizeof(*p));
229 | p->pos = pos;
230 | p->kind= A_boolExprKind;
231 | p->u.boolExpr = boolExpr;
232 | return p;
233 | }
234 |
235 | A_boolUnit A_BoolUOpExprUnit(A_pos pos, A_boolUOpExpr boolUOpExpr){
236 | A_boolUnit p = (A_boolUnit)malloc(sizeof(*p));
237 | p->pos = pos;
238 | p->kind= A_boolUOpExprKind;
239 | p->u.boolUOpExpr = boolUOpExpr;
240 | return p;
241 | }
242 |
243 | A_rightVal A_ArithExprRVal(A_pos pos, A_arithExpr arithExpr){
244 | A_rightVal p = (A_rightVal)malloc(sizeof(*p));
245 | p->pos = pos;
246 | p->kind= A_arithExprValKind;
247 | p->u.arithExpr = arithExpr;
248 | return p;
249 | }
250 |
251 | A_rightVal A_BoolExprRVal(A_pos pos, A_boolExpr boolExpr){
252 | A_rightVal p = (A_rightVal)malloc(sizeof(*p));
253 | p->pos = pos;
254 | p->kind= A_boolExprValKind;
255 | p->u.boolExpr = boolExpr;
256 | return p;
257 | }
258 |
259 | A_leftVal A_IdExprLVal(A_pos pos, char* id){
260 | A_leftVal p = (A_leftVal)malloc(sizeof(*p));
261 | p->pos = pos;
262 | p->kind= A_varValKind;
263 | p->u.id = id;
264 | return p;
265 | }
266 |
267 | A_leftVal A_ArrExprLVal(A_pos pos, A_arrayExpr arrExpr){
268 | A_leftVal p = (A_leftVal)malloc(sizeof(*p));
269 | p->pos = pos;
270 | p->kind= A_arrValKind;
271 | p->u.arrExpr = arrExpr;
272 | return p;
273 | }
274 |
275 | A_leftVal A_MemberExprLVal(A_pos pos, A_memberExpr memberExpr){
276 | A_leftVal p = (A_leftVal)malloc(sizeof(*p));
277 | p->pos = pos;
278 | p->kind= A_memberValKind;
279 | p->u.memberExpr = memberExpr;
280 | return p;
281 | }
282 |
283 | A_assignStmt A_AssignStmt(A_pos pos, A_leftVal leftVal, A_rightVal rightVal){
284 | A_assignStmt p = (A_assignStmt)malloc(sizeof(*p));
285 | p->pos = pos;
286 | p->leftVal= leftVal;
287 | p->rightVal = rightVal;
288 | return p;
289 | }
290 |
291 | A_varDeclScalar A_VarDeclScalar(A_pos pos, char* id, A_type type){
292 | A_varDeclScalar p = (A_varDeclScalar)malloc(sizeof(*p));
293 | p->pos = pos;
294 | p->id= id;
295 | p->type = type;
296 | return p;
297 | }
298 |
299 | A_varDeclArray A_VarDeclArray(A_pos pos, char* id, int len, A_type type){
300 | A_varDeclArray p = (A_varDeclArray)malloc(sizeof(*p));
301 | p->pos = pos;
302 | p->id= id;
303 | p->len = len;
304 | p->type = type;
305 | return p;
306 | }
307 |
308 | A_varDecl A_VarDecl_Scalar(A_pos pos, A_varDeclScalar declScalar){
309 | A_varDecl p = (A_varDecl)malloc(sizeof(*p));
310 | p->pos = pos;
311 | p->kind = A_varDeclScalarKind;
312 | p->u.declScalar = declScalar;
313 | return p;
314 | }
315 |
316 | A_varDecl A_VarDecl_Array(A_pos pos, A_varDeclArray declArray){
317 | A_varDecl p = (A_varDecl)malloc(sizeof(*p));
318 | p->pos = pos;
319 | p->kind = A_varDeclArrayKind;
320 | p->u.declArray = declArray;
321 | return p;
322 | }
323 |
324 | A_varDefScalar A_VarDefScalar(A_pos pos, char* id, A_type type, A_rightVal val){
325 | A_varDefScalar p = (A_varDefScalar)malloc(sizeof(*p));
326 | p->pos = pos;
327 | p->id= id;
328 | p->type = type;
329 | p->val = val;
330 | return p;
331 | }
332 |
333 | A_varDefArray A_VarDefArray(A_pos pos, char* id, int len, A_type type, A_rightValList vals){
334 | A_varDefArray p = (A_varDefArray)malloc(sizeof(*p));
335 | p->pos = pos;
336 | p->id= id;
337 | p->len = len;
338 | p->type = type;
339 | p->vals = vals;
340 | return p;
341 | }
342 |
343 | A_varDef A_VarDef_Scalar(A_pos pos, A_varDefScalar defScalar){
344 | A_varDef p = (A_varDef)malloc(sizeof(*p));
345 | p->pos = pos;
346 | p->kind = A_varDefScalarKind;
347 | p->u.defScalar = defScalar;
348 | return p;
349 | }
350 |
351 | A_varDef A_VarDef_Array(A_pos pos, A_varDefArray defArray){
352 | A_varDef p = (A_varDef)malloc(sizeof(*p));
353 | p->pos = pos;
354 | p->kind = A_varDefArrayKind;
355 | p->u.defArray = defArray;
356 | return p;
357 | }
358 |
359 | A_varDeclStmt A_VarDeclStmt(A_pos pos, A_varDecl varDecl){
360 | A_varDeclStmt p = (A_varDeclStmt)malloc(sizeof(*p));
361 | p->pos = pos;
362 | p->kind = A_varDeclKind;
363 | p->u.varDecl = varDecl;
364 | return p;
365 |
366 | }
367 |
368 | A_varDeclStmt A_VarDefStmt(A_pos pos, A_varDef varDef){
369 | A_varDeclStmt p = (A_varDeclStmt)malloc(sizeof(*p));
370 | p->pos = pos;
371 | p->kind = A_varDefKind;
372 | p->u.varDef = varDef;
373 | return p;
374 | }
375 |
376 | A_varDeclList A_VarDeclList(A_varDecl head, A_varDeclList tail){
377 | A_varDeclList p = (A_varDeclList)malloc(sizeof(*p));
378 | p->head = head;
379 | p->tail = tail;
380 | return p;
381 | }
382 |
383 | A_structDef A_StructDef(A_pos pos, char* id, A_varDeclList varDecls){
384 | A_structDef p = (A_structDef)malloc(sizeof(*p));
385 | p->pos = pos;
386 | p->id = id;
387 | p->varDecls = varDecls;
388 | return p;
389 | }
390 |
391 | A_fnDecl A_FnDecl(A_pos pos, char* id, A_paramDecl paramDecl, A_type type){
392 | A_fnDecl p = (A_fnDecl)malloc(sizeof(*p));
393 | p->pos = pos;
394 | p->id = id;
395 | p->paramDecl = paramDecl;
396 | p->type = type;
397 | return p;
398 | }
399 |
400 | A_paramDecl A_ParamDecl(A_varDeclList varDecls){
401 | A_paramDecl p = (A_paramDecl)malloc(sizeof(*p));
402 | p->varDecls = varDecls;
403 | return p;
404 | }
405 |
406 | A_codeBlockStmtList A_CodeBlockStmtList(A_codeBlockStmt head, A_codeBlockStmtList tail){
407 | A_codeBlockStmtList p = (A_codeBlockStmtList)malloc(sizeof(*p));
408 | p->head = head;
409 | p->tail = tail;
410 | return p;
411 | }
412 |
413 | A_fnDef A_FnDef(A_pos pos, A_fnDecl fnDecl, A_codeBlockStmtList stmts){
414 | A_fnDef p = (A_fnDef)malloc(sizeof(*p));
415 | p->pos = pos;
416 | p->fnDecl = fnDecl;
417 | p->stmts = stmts;
418 | return p;
419 | }
420 |
421 | A_ifStmt A_IfStmt(A_pos pos, A_boolExpr boolExpr, A_codeBlockStmtList ifStmts, A_codeBlockStmtList elseStmts){
422 | A_ifStmt p = (A_ifStmt)malloc(sizeof(*p));
423 | p->pos = pos;
424 | p->boolExpr = boolExpr;
425 | p->ifStmts = ifStmts;
426 | p->elseStmts = elseStmts;
427 | return p;
428 | }
429 |
430 | A_whileStmt A_WhileStmt(A_pos pos, A_boolExpr boolExpr, A_codeBlockStmtList whileStmts){
431 | A_whileStmt p = (A_whileStmt)malloc(sizeof(*p));
432 | p->pos = pos;
433 | p->boolExpr = boolExpr;
434 | p->whileStmts = whileStmts;
435 | return p;
436 | }
437 |
438 | A_callStmt A_CallStmt(A_pos pos, A_fnCall fnCall){
439 | A_callStmt p = (A_callStmt)malloc(sizeof(*p));
440 | p->pos = pos;
441 | p->fnCall = fnCall;
442 | return p;
443 | }
444 |
445 | A_returnStmt A_ReturnStmt(A_pos pos, A_rightVal retVal){
446 | A_returnStmt p = (A_returnStmt)malloc(sizeof(*p));
447 | p->pos = pos;
448 | p->retVal = retVal;
449 | return p;
450 | }
451 |
452 | A_codeBlockStmt A_BlockNullStmt(A_pos pos){
453 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
454 | p->pos = pos;
455 | p->kind = A_nullStmtKind;
456 | return p;
457 | }
458 |
459 | A_codeBlockStmt A_BlockVarDeclStmt(A_pos pos, A_varDeclStmt varDeclStmt){
460 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
461 | p->pos = pos;
462 | p->kind = A_varDeclStmtKind;
463 | p->u.varDeclStmt = varDeclStmt;
464 | return p;
465 | }
466 |
467 | A_codeBlockStmt A_BlockAssignStmt(A_pos pos, A_assignStmt assignStmt){
468 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
469 | p->pos = pos;
470 | p->kind = A_assignStmtKind;
471 | p->u.assignStmt = assignStmt;
472 | return p;
473 | }
474 |
475 | A_codeBlockStmt A_BlockCallStmt(A_pos pos, A_callStmt callStmt){
476 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
477 | p->pos = pos;
478 | p->kind = A_callStmtKind;
479 | p->u.callStmt = callStmt;
480 | return p;
481 | }
482 |
483 | A_codeBlockStmt A_BlockIfStmt(A_pos pos, A_ifStmt ifStmt){
484 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
485 | p->pos = pos;
486 | p->kind = A_ifStmtKind;
487 | p->u.ifStmt = ifStmt;
488 | return p;
489 | }
490 |
491 | A_codeBlockStmt A_BlockWhileStmt(A_pos pos, A_whileStmt whileStmt){
492 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
493 | p->pos = pos;
494 | p->kind = A_whileStmtKind;
495 | p->u.whileStmt = whileStmt;
496 | return p;
497 | }
498 |
499 | A_codeBlockStmt A_BlockReturnStmt(A_pos pos, A_returnStmt returnStmt){
500 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
501 | p->pos = pos;
502 | p->kind = A_returnStmtKind;
503 | p->u.returnStmt = returnStmt;
504 | return p;
505 | }
506 |
507 | A_codeBlockStmt A_BlockContinueStmt(A_pos pos){
508 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
509 | p->pos = pos;
510 | p->kind = A_continueStmtKind;
511 | return p;
512 | }
513 |
514 | A_codeBlockStmt A_BlockBreakStmt(A_pos pos){
515 | A_codeBlockStmt p = (A_codeBlockStmt)malloc(sizeof(*p));
516 | p->pos = pos;
517 | p->kind = A_breakStmtKind;
518 | return p;
519 | }
520 |
521 | A_fnDeclStmt A_FnDeclStmt(A_pos pos, A_fnDecl fnDecl){
522 | A_fnDeclStmt p = (A_fnDeclStmt)malloc(sizeof(*p));
523 | p->pos = pos;
524 | p->fnDecl = fnDecl;
525 | return p;
526 | }
527 |
528 | A_programElement A_ProgramNullStmt(A_pos pos){
529 | A_programElement p = (A_programElement)malloc(sizeof(*p));
530 | p->pos = pos;
531 | p->kind = A_programNullStmtKind;
532 | return p;
533 | }
534 |
535 | A_programElement A_ProgramVarDeclStmt(A_pos pos, A_varDeclStmt varDeclStmt){
536 | A_programElement p = (A_programElement)malloc(sizeof(*p));
537 | p->pos = pos;
538 | p->kind = A_programVarDeclStmtKind;
539 | p->u.varDeclStmt = varDeclStmt;
540 | return p;
541 | }
542 |
543 | A_programElement A_ProgramStructDef(A_pos pos, A_structDef structDef){
544 | A_programElement p = (A_programElement)malloc(sizeof(*p));
545 | p->pos = pos;
546 | p->kind = A_programStructDefKind;
547 | p->u.structDef = structDef;
548 | return p;
549 | }
550 |
551 | A_programElement A_ProgramFnDeclStmt(A_pos pos, A_fnDeclStmt fnDeclStmt){
552 | A_programElement p = (A_programElement)malloc(sizeof(*p));
553 | p->pos = pos;
554 | p->kind = A_programFnDeclStmtKind;
555 | p->u.fnDeclStmt = fnDeclStmt;
556 | return p;
557 | }
558 |
559 | A_programElement A_ProgramFnDef(A_pos pos, A_fnDef fnDef){
560 | A_programElement p = (A_programElement)malloc(sizeof(*p));
561 | p->pos = pos;
562 | p->kind = A_programFnDefKind;
563 | p->u.fnDef = fnDef;
564 | return p;
565 | }
566 |
567 | A_programElementList A_ProgramElementList(A_programElement head, A_programElementList tail){
568 | A_programElementList p = (A_programElementList)malloc(sizeof(*p));
569 | p->head = head;
570 | p->tail = tail;
571 | return p;
572 | }
573 |
574 | A_program A_Program(A_programElementList programElements){
575 | A_program p = (A_program)malloc(sizeof(*p));
576 | p->programElements = programElements;
577 | return p;
578 | }
--------------------------------------------------------------------------------
/src/TeaplAst.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | typedef struct A_pos_* A_pos; //position information
4 | typedef struct A_type_* A_type;
5 | typedef struct A_varDecl_* A_varDecl;
6 | typedef struct A_varDef_* A_varDef;
7 | typedef struct A_rightVal_* A_rightVal;
8 | typedef struct A_arithExpr_* A_arithExpr;
9 | typedef struct A_boolExpr_* A_boolExpr;
10 | typedef struct A_arithBiOpExpr_* A_arithBiOpExpr;
11 | typedef struct A_arithUExpr_* A_arithUExpr;
12 | typedef struct A_exprUnit_* A_exprUnit;
13 | typedef struct A_fnCall_* A_fnCall;
14 | typedef struct A_indexExpr_* A_indexExpr;
15 | typedef struct A_arrayExpr_* A_arrayExpr;
16 | typedef struct A_memberExpr_* A_memberExpr;
17 | typedef struct A_boolUnit_* A_boolUnit;
18 | typedef struct A_boolBiOpExpr_* A_boolBiOpExpr;
19 | typedef struct A_boolUOpExpr_* A_boolUOpExpr;
20 | typedef struct A_comExpr_* A_comExpr;
21 | typedef struct A_rightVal_* A_rightVal;
22 | typedef struct A_leftVal_* A_leftVal;
23 | typedef struct A_assignStmt_* A_assignStmt;
24 | typedef struct A_rightValList_* A_rightValList;
25 | typedef struct A_varDefScalar_* A_varDefScalar;
26 | typedef struct A_varDefArray_* A_varDefArray;
27 | typedef struct A_varDeclScalar_* A_varDeclScalar;
28 | typedef struct A_varDeclArray_* A_varDeclArray;
29 | typedef struct A_varDecl_* A_varDecl;
30 | typedef struct A_varDef_* A_varDef;
31 | typedef struct A_varDeclStmt_* A_varDeclStmt;
32 | typedef struct A_varDeclList_* A_varDeclList;
33 | typedef struct A_structDef_* A_structDef;
34 | typedef struct A_paramDecl_* A_paramDecl;
35 | typedef struct A_fnDecl_* A_fnDecl;
36 | typedef struct A_fnDef_* A_fnDef;
37 | typedef struct A_codeBlockStmt_* A_codeBlockStmt;
38 | typedef struct A_ifStmt_* A_ifStmt;
39 | typedef struct A_whileStmt_* A_whileStmt;
40 | typedef struct A_fnDeclStmt_* A_fnDeclStmt;
41 | typedef struct A_callStmt_* A_callStmt;
42 | typedef struct A_returnStmt_* A_returnStmt;
43 | typedef struct A_programElement_* A_programElement;
44 | typedef struct A_codeBlockStmtList_* A_codeBlockStmtList;
45 | typedef struct A_programElementList_* A_programElementList;
46 | typedef struct A_program_* A_program;
47 | typedef struct A_tokenId_* A_tokenId;
48 | typedef struct A_tokenNum_* A_tokenNum;
49 |
50 | struct A_pos_ {
51 | int line, col;
52 | };
53 |
54 | typedef enum {
55 | A_nativeTypeKind,
56 | A_structTypeKind
57 | } A_dataType;
58 |
59 | typedef enum {
60 | A_intTypeKind
61 | } A_nativeType;
62 |
63 | struct A_type_ {
64 | A_pos pos;
65 | A_dataType type;
66 | union {
67 | A_nativeType nativeType;
68 | char* structType;
69 | } u;
70 | };
71 |
72 | struct A_rightValList_ {
73 | A_rightVal head;
74 | A_rightValList tail;
75 | };
76 |
77 | struct A_fnCall_ {
78 | A_pos pos;
79 | char* fn;
80 | A_rightValList vals;
81 | };
82 |
83 | typedef enum {
84 | A_numIndexKind,
85 | A_idIndexKind
86 | } A_indexExprKind;
87 |
88 | // [id|num]
89 | struct A_indexExpr_ {
90 | A_pos pos;
91 | A_indexExprKind kind;
92 | union {
93 | int num;
94 | char* id;
95 | } u;
96 | };
97 |
98 | // arr[idx]
99 | struct A_arrayExpr_ {
100 | A_pos pos;
101 | char* arr;
102 | A_indexExpr idx;
103 | };
104 |
105 | // structId.memberId
106 | struct A_memberExpr_ {
107 | A_pos pos;
108 | char* structId;
109 | char* memberId;
110 | };
111 |
112 | typedef enum {
113 | A_numExprKind,
114 | A_idExprKind,
115 | A_arithExprKind,
116 | A_fnCallKind,
117 | A_arrayExprKind,
118 | A_memberExprKind,
119 | A_arithUExprKind
120 | } A_exprUnitType;
121 |
122 | struct A_exprUnit_ {
123 | A_pos pos;
124 | A_exprUnitType kind;
125 | union {
126 | int num;
127 | char* id;
128 | A_arithExpr arithExpr;
129 | A_fnCall callExpr;
130 | A_arrayExpr arrayExpr;
131 | A_memberExpr memberExpr;
132 | A_arithUExpr arithUExpr;
133 | } u;
134 | };
135 |
136 | typedef enum {
137 | A_neg
138 | } A_arithUOp;
139 |
140 | typedef enum {
141 | A_add,
142 | A_sub,
143 | A_mul,
144 | A_div
145 | } A_arithBiOp;
146 |
147 | typedef enum {
148 | A_not
149 | } A_boolUOp;
150 |
151 | typedef enum {
152 | A_and,
153 | A_or
154 | } A_boolBiOp;
155 |
156 | typedef enum {
157 | A_lt, // less than
158 | A_le, // less equal
159 | A_gt,
160 | A_ge,
161 | A_eq,
162 | A_ne
163 | } A_comOp;
164 |
165 | typedef enum {
166 | A_arithBiOpExprKind,
167 | A_exprUnitKind
168 | } A_arithExprType;
169 |
170 | typedef enum {
171 | A_boolBiOpExprKind,
172 | A_boolUnitKind
173 | } A_boolExprType;
174 |
175 | typedef enum {
176 | A_comOpExprKind,
177 | A_boolExprKind,
178 | A_boolUOpExprKind
179 | } A_boolUnitType;
180 |
181 | typedef enum {
182 | A_arithExprValKind,
183 | A_boolExprValKind
184 | } A_rightValType;
185 |
186 | typedef enum {
187 | A_varValKind,
188 | A_arrValKind,
189 | A_memberValKind
190 | } A_leftValType;
191 |
192 | typedef enum {
193 | A_varDeclKind,
194 | A_varDefKind
195 | } A_varDeclStmtType;
196 |
197 | typedef enum {
198 | A_varDeclScalarKind,
199 | A_varDeclArrayKind
200 | } A_varDeclType;
201 |
202 | typedef enum {
203 | A_varDefScalarKind,
204 | A_varDefArrayKind
205 | } A_varDefType;
206 |
207 | typedef enum {
208 | A_programNullStmtKind,
209 | A_programVarDeclStmtKind,
210 | A_programStructDefKind,
211 | A_programFnDeclStmtKind,
212 | A_programFnDefKind
213 | } A_programElementType;
214 |
215 | // left op right (eg: 1+2)
216 | struct A_arithBiOpExpr_ {
217 | A_pos pos;
218 | A_arithBiOp op;
219 | A_arithExpr left, right;
220 | };
221 |
222 | // op expr (eg: -1)
223 | struct A_arithUExpr_ {
224 | A_pos pos;
225 | A_arithUOp op;
226 | A_exprUnit expr;
227 | };
228 |
229 | struct A_arithExpr_ {
230 | A_pos pos;
231 | A_arithExprType kind;
232 | union {
233 | A_arithBiOpExpr arithBiOpExpr;
234 | A_exprUnit exprUnit;
235 | } u;
236 | };
237 |
238 | // left op right (eg: a && b)
239 | struct A_boolBiOpExpr_ {
240 | A_pos pos;
241 | A_boolBiOp op;
242 | A_boolExpr left;
243 | A_boolUnit right;
244 | };
245 |
246 | // op cond (eg: !a)
247 | struct A_boolUOpExpr_ {
248 | A_pos pos;
249 | A_boolUOp op;
250 | A_boolUnit cond;
251 | };
252 |
253 | struct A_boolExpr_ {
254 | A_pos pos;
255 | A_boolExprType kind;
256 | union {
257 | A_boolBiOpExpr boolBiOpExpr;
258 | A_boolUnit boolUnit;
259 | } u;
260 | };
261 |
262 | // left op right (eg: 1 < 2)
263 | struct A_comExpr_ {
264 | A_pos pos;
265 | A_comOp op;
266 | A_exprUnit left, right;
267 | };
268 |
269 | struct A_boolUnit_ {
270 | A_pos pos;
271 | A_boolUnitType kind;
272 | union {
273 | A_comExpr comExpr;
274 | A_boolExpr boolExpr;
275 | A_boolUOpExpr boolUOpExpr;
276 | } u;
277 | };
278 |
279 | struct A_rightVal_ {
280 | A_pos pos;
281 | A_rightValType kind;
282 | union {
283 | A_arithExpr arithExpr;
284 | A_boolExpr boolExpr;
285 | } u;
286 | };
287 |
288 | struct A_leftVal_ {
289 | A_pos pos;
290 | A_leftValType kind;
291 | union {
292 | char* id;
293 | A_arrayExpr arrExpr;
294 | A_memberExpr memberExpr;
295 | } u;
296 | };
297 |
298 | struct A_assignStmt_ {
299 | A_pos pos;
300 | A_leftVal leftVal;
301 | A_rightVal rightVal;
302 | };
303 |
304 | // id:type (eg: a:int)
305 | struct A_varDeclScalar_ {
306 | A_pos pos;
307 | char* id;
308 | A_type type;
309 | };
310 |
311 | // id[len]:type (eg: a[10]:int)
312 | struct A_varDeclArray_ {
313 | A_pos pos;
314 | char* id;
315 | int len;
316 | A_type type;
317 | };
318 |
319 | struct A_varDecl_ {
320 | A_pos pos;
321 | A_varDeclType kind;
322 | union {
323 | A_varDeclScalar declScalar;
324 | A_varDeclArray declArray;
325 | } u;
326 | };
327 |
328 | // id:type = val (eg: a:int = 10)
329 | struct A_varDefScalar_ {
330 | A_pos pos;
331 | char* id;
332 | A_type type;
333 | A_rightVal val;
334 | };
335 |
336 | // id[len]:type = {vals} (eg: a[10]:int = {1, 2})
337 | struct A_varDefArray_ {
338 | A_pos pos;
339 | char* id;
340 | int len;
341 | A_type type;
342 | A_rightValList vals;
343 | };
344 |
345 | struct A_varDef_ {
346 | A_pos pos;
347 | A_varDefType kind;
348 | union {
349 | A_varDefScalar defScalar;
350 | A_varDefArray defArray;
351 | } u;
352 | };
353 |
354 | struct A_varDeclStmt_ {
355 | A_pos pos;
356 | A_varDeclStmtType kind;
357 | union {
358 | A_varDecl varDecl;
359 | A_varDef varDef;
360 | } u;
361 | };
362 |
363 | struct A_varDeclList_ {
364 | A_varDecl head;
365 | A_varDeclList tail;
366 | };
367 |
368 | struct A_structDef_ {
369 | A_pos pos;
370 | char* id;
371 | A_varDeclList varDecls;
372 | };
373 |
374 | struct A_fnDecl_ {
375 | A_pos pos;
376 | char* id;
377 | A_paramDecl paramDecl;
378 | A_type type;
379 | };
380 |
381 | struct A_paramDecl_ {
382 | A_varDeclList varDecls;
383 | };
384 |
385 | struct A_fnDef_ {
386 | A_pos pos;
387 | A_fnDecl fnDecl;
388 | A_codeBlockStmtList stmts;
389 | };
390 |
391 | // if(boolExpr){
392 | // ifStmts
393 | // }else{
394 | // elseStmts
395 | // }
396 | struct A_ifStmt_ {
397 | A_pos pos;
398 | A_boolExpr boolExpr;
399 | A_codeBlockStmtList ifStmts, elseStmts;
400 | };
401 |
402 | // while(boolExpr){
403 | // whileStmts
404 | // }
405 | struct A_whileStmt_ {
406 | A_pos pos;
407 | A_boolExpr boolExpr;
408 | A_codeBlockStmtList whileStmts;
409 | };
410 |
411 | struct A_callStmt_ {
412 | A_pos pos;
413 | A_fnCall fnCall;
414 | };
415 |
416 | struct A_returnStmt_ {
417 | A_pos pos;
418 | A_rightVal retVal;
419 | };
420 |
421 | typedef enum {
422 | A_nullStmtKind,
423 | A_varDeclStmtKind,
424 | A_assignStmtKind,
425 | A_callStmtKind,
426 | A_ifStmtKind,
427 | A_whileStmtKind,
428 | A_returnStmtKind,
429 | A_continueStmtKind,
430 | A_breakStmtKind
431 | } A_codeBlockStmtType;
432 |
433 | struct A_codeBlockStmt_ {
434 | A_pos pos;
435 | A_codeBlockStmtType kind;
436 | union {
437 | A_varDeclStmt varDeclStmt;
438 | A_assignStmt assignStmt;
439 | A_callStmt callStmt;
440 | A_ifStmt ifStmt;
441 | A_whileStmt whileStmt;
442 | A_returnStmt returnStmt;
443 | // continue and break do not need other info
444 | } u;
445 | };
446 |
447 | struct A_codeBlockStmtList_ {
448 | A_codeBlockStmt head;
449 | A_codeBlockStmtList tail;
450 | };
451 |
452 | struct A_fnDeclStmt_ {
453 | A_pos pos;
454 | A_fnDecl fnDecl;
455 | };
456 |
457 | // programElement = varDeclStmt | structDef | fnDeclStmt | fnDef | comment | < ; >
458 | struct A_programElement_ {
459 | A_pos pos;
460 | A_programElementType kind;
461 | union {
462 | A_varDeclStmt varDeclStmt;
463 | A_structDef structDef;
464 | A_fnDeclStmt fnDeclStmt;
465 | A_fnDef fnDef;
466 | } u;
467 | };
468 |
469 | // programElementList := programElement*
470 | struct A_programElementList_ {
471 | A_programElement head;
472 | A_programElementList tail;
473 | };
474 |
475 | // program := programElementList
476 | struct A_program_ {
477 | A_programElementList programElements;
478 | };
479 |
480 | struct A_tokenId_ {
481 | A_pos pos;
482 | char* id;
483 | };
484 |
485 | struct A_tokenNum_ {
486 | A_pos pos;
487 | int num;
488 | };
489 |
490 | A_pos A_Pos(int, int);
491 | A_tokenId A_TokenId(A_pos, char*);
492 | A_tokenNum A_TokenNum(A_pos, int);
493 | A_type A_NativeType(A_pos pos, A_nativeType ntype);
494 | A_type A_StructType(A_pos pos, char* stype);
495 | A_rightValList A_RightValList(A_rightVal head, A_rightValList tail);
496 | A_fnCall A_FnCall(A_pos pos, char* fn, A_rightValList vals);
497 | A_indexExpr A_NumIndexExpr(A_pos pos, int num);
498 | A_indexExpr A_IdIndexExpr(A_pos pos, char* id);
499 | A_arrayExpr A_ArrayExpr(A_pos pos, char* arr, A_indexExpr idx);
500 | A_memberExpr A_MemberExpr(A_pos pos, char* structId, char* memberId);
501 | A_exprUnit A_NumExprUnit(A_pos pos, int num);
502 | A_exprUnit A_IdExprUnit(A_pos pos, char* id);
503 | A_exprUnit A_ArithExprUnit(A_pos pos, A_arithExpr arithExpr);
504 | A_exprUnit A_CallExprUnit(A_pos pos, A_fnCall callExpr);
505 | A_exprUnit A_ArrayExprUnit(A_pos pos, A_arrayExpr arrayExpr);
506 | A_exprUnit A_MemberExprUnit(A_pos pos, A_memberExpr memberExpr);
507 | A_exprUnit A_ArithUExprUnit(A_pos pos, A_arithUExpr arithUExpr);
508 | A_arithBiOpExpr A_ArithBiOpExpr(A_pos pos, A_arithBiOp op, A_arithExpr left, A_arithExpr right);
509 | A_arithUExpr A_ArithUExpr(A_pos pos, A_arithUOp op, A_exprUnit expr);
510 | A_arithExpr A_ArithBiOp_Expr(A_pos pos, A_arithBiOpExpr arithBiOpExpr);
511 | A_arithExpr A_ExprUnit(A_pos pos, A_exprUnit exprUnit);
512 | A_boolBiOpExpr A_BoolBiOpExpr(A_pos pos, A_boolBiOp op, A_boolExpr left, A_boolUnit right);
513 | A_boolUOpExpr A_BoolUOpExpr(A_pos pos, A_boolUOp op, A_boolUnit cond);
514 | A_boolExpr A_BoolBiOp_Expr(A_pos pos, A_boolBiOpExpr boolBiOpExpr);
515 | A_boolExpr A_BoolExpr(A_pos pos, A_boolUnit boolUnit);
516 | A_comExpr A_ComExpr(A_pos pos, A_comOp op, A_exprUnit left, A_exprUnit right);
517 | A_boolUnit A_ComExprUnit(A_pos pos, A_comExpr comExpr);
518 | A_boolUnit A_BoolExprUnit(A_pos pos, A_boolExpr boolExpr);
519 | A_boolUnit A_BoolUOpExprUnit(A_pos pos, A_boolUOpExpr boolUOpExpr);
520 | A_rightVal A_ArithExprRVal(A_pos pos, A_arithExpr arithExpr);
521 | A_rightVal A_BoolExprRVal(A_pos pos, A_boolExpr boolExpr);
522 | A_leftVal A_IdExprLVal(A_pos pos, char* id);
523 | A_leftVal A_ArrExprLVal(A_pos pos, A_arrayExpr arrExpr);
524 | A_leftVal A_MemberExprLVal(A_pos pos, A_memberExpr memberExpr);
525 | A_assignStmt A_AssignStmt(A_pos pos, A_leftVal leftVal, A_rightVal rightVal);
526 | A_varDeclScalar A_VarDeclScalar(A_pos pos, char* id, A_type type);
527 | A_varDeclArray A_VarDeclArray(A_pos pos, char* id, int len, A_type type);
528 | A_varDecl A_VarDecl_Scalar(A_pos pos, A_varDeclScalar declScalar);
529 | A_varDecl A_VarDecl_Array(A_pos pos, A_varDeclArray declArray);
530 | A_varDefScalar A_VarDefScalar(A_pos pos, char* id, A_type type, A_rightVal val);
531 | A_varDefArray A_VarDefArray(A_pos pos, char* id, int len, A_type type, A_rightValList vals);
532 | A_varDef A_VarDef_Scalar(A_pos pos, A_varDefScalar defScalar);
533 | A_varDef A_VarDef_Array(A_pos pos, A_varDefArray defArray);
534 | A_varDeclStmt A_VarDeclStmt(A_pos pos, A_varDecl varDecl);
535 | A_varDeclStmt A_VarDefStmt(A_pos pos, A_varDef varDef);
536 | A_varDeclList A_VarDeclList(A_varDecl head, A_varDeclList tail);
537 | A_structDef A_StructDef(A_pos pos, char* id, A_varDeclList varDecls);
538 | A_fnDecl A_FnDecl(A_pos pos, char* id, A_paramDecl paramDecl, A_type type);
539 | A_paramDecl A_ParamDecl(A_varDeclList varDecls);
540 | A_codeBlockStmtList A_CodeBlockStmtList(A_codeBlockStmt head, A_codeBlockStmtList tail);
541 | A_fnDef A_FnDef(A_pos pos, A_fnDecl fnDecl, A_codeBlockStmtList stmts);
542 | A_ifStmt A_IfStmt(A_pos pos, A_boolExpr boolExpr, A_codeBlockStmtList ifStmts, A_codeBlockStmtList elseStmts);
543 | A_whileStmt A_WhileStmt(A_pos pos, A_boolExpr boolExpr, A_codeBlockStmtList whileStmts);
544 | A_callStmt A_CallStmt(A_pos pos, A_fnCall fnCall);
545 | A_returnStmt A_ReturnStmt(A_pos pos, A_rightVal retVal);
546 | A_codeBlockStmt A_BlockNullStmt(A_pos pos);
547 | A_codeBlockStmt A_BlockVarDeclStmt(A_pos pos, A_varDeclStmt varDeclStmt);
548 | A_codeBlockStmt A_BlockAssignStmt(A_pos pos, A_assignStmt assignStmt);
549 | A_codeBlockStmt A_BlockCallStmt(A_pos pos, A_callStmt callStmt);
550 | A_codeBlockStmt A_BlockIfStmt(A_pos pos, A_ifStmt ifStmt);
551 | A_codeBlockStmt A_BlockWhileStmt(A_pos pos, A_whileStmt whileStmt);
552 | A_codeBlockStmt A_BlockReturnStmt(A_pos pos, A_returnStmt returnStmt);
553 | A_codeBlockStmt A_BlockContinueStmt(A_pos pos);
554 | A_codeBlockStmt A_BlockBreakStmt(A_pos pos);
555 | A_fnDeclStmt A_FnDeclStmt(A_pos pos, A_fnDecl fnDecl);
556 | A_programElement A_ProgramNullStmt(A_pos pos);
557 | A_programElement A_ProgramVarDeclStmt(A_pos pos, A_varDeclStmt varDeclStmt);
558 | A_programElement A_ProgramStructDef(A_pos pos, A_structDef structDef);
559 | A_programElement A_ProgramFnDeclStmt(A_pos pos, A_fnDeclStmt fnDecl);
560 | A_programElement A_ProgramFnDef(A_pos pos, A_fnDef fnDef);
561 | A_programElementList A_ProgramElementList(A_programElement head, A_programElementList tail);
562 | A_program A_Program(A_programElementList programElements);
563 |
564 |
--------------------------------------------------------------------------------
/src/TeaplaAst.cpp:
--------------------------------------------------------------------------------
1 | #include "TeaplAst.h"
2 | #include "TeaplaAst.h"
3 |
4 | aA_type aA_Type(A_type type){
5 | aA_type p = new aA_type_;
6 | p->pos = type->pos;
7 | p->type = type->type;
8 | switch(type->type){
9 | case A_nativeTypeKind:{
10 | p->u.nativeType = type->u.nativeType;
11 | break;
12 | }
13 | case A_structTypeKind:{
14 | p->u.structType = new string(type->u.structType);
15 | break;
16 | }
17 | }
18 | return p;
19 | }
20 |
21 | aA_fnCall aA_FnCall(A_fnCall fnCall){
22 | aA_fnCall p = new aA_fnCall_;
23 | p->pos = fnCall->pos;
24 | p->fn= new string(fnCall->fn);
25 | for(A_rightValList l=fnCall->vals; l; l=l->tail){
26 | p->vals.emplace_back(aA_RightVal(l->head));
27 | }
28 | return p;
29 | }
30 |
31 | aA_indexExpr aA_IndexExpr(A_indexExpr indexExpr){
32 | aA_indexExpr p = new aA_indexExpr_;
33 | p->pos = indexExpr->pos;
34 | p->kind= indexExpr->kind;
35 | switch(indexExpr->kind){
36 | case A_numIndexKind:{
37 | p->u.num = indexExpr->u.num;
38 | break;
39 | }
40 | case A_idIndexKind:{
41 | p->u.id = new string(indexExpr->u.id);
42 | break;
43 | }
44 | }
45 | return p;
46 | }
47 |
48 | aA_arrayExpr aA_ArrayExpr(A_arrayExpr arrayExpr){
49 | aA_arrayExpr p = new aA_arrayExpr_;
50 | p->pos = arrayExpr->pos;
51 | p->arr= new string(arrayExpr->arr);
52 | p->idx = aA_IndexExpr(arrayExpr->idx);
53 | return p;
54 | }
55 |
56 | aA_memberExpr aA_MemberExpr(A_memberExpr memberExpr){
57 | aA_memberExpr p = new aA_memberExpr_;
58 | p->pos = memberExpr->pos;
59 | p->structId= new string(memberExpr->structId);
60 | p->memberId = new string(memberExpr->memberId);
61 | return p;
62 | }
63 |
64 | aA_exprUnit aA_ExprUnit(A_exprUnit exprUnit){
65 | aA_exprUnit p = new aA_exprUnit_;
66 | p->pos = exprUnit->pos;
67 | p->kind= exprUnit->kind;
68 | switch(exprUnit->kind){
69 | case A_numExprKind:{
70 | p->u.num = exprUnit->u.num;
71 | break;
72 | }
73 | case A_idExprKind:{
74 | p->u.id = new string(exprUnit->u.id);
75 | break;
76 | }
77 | case A_arithExprKind:{
78 | p->u.arithExpr = aA_ArithExpr(exprUnit->u.arithExpr);
79 | break;
80 | }
81 | case A_fnCallKind:{
82 | p->u.callExpr = aA_FnCall(exprUnit->u.callExpr);
83 | break;
84 | }
85 | case A_arrayExprKind:{
86 | p->u.arrayExpr = aA_ArrayExpr(exprUnit->u.arrayExpr);
87 | break;
88 | }
89 | case A_memberExprKind:{
90 | p->u.memberExpr = aA_MemberExpr(exprUnit->u.memberExpr);
91 | break;
92 | }
93 | case A_arithUExprKind:{
94 | p->u.arithUExpr = aA_ArithUExpr(exprUnit->u.arithUExpr);
95 | break;
96 | }
97 | }
98 | return p;
99 | }
100 |
101 | aA_arithBiOpExpr aA_ArithBiOpExpr(A_arithBiOpExpr arithBiOpExpr){
102 | aA_arithBiOpExpr p = new aA_arithBiOpExpr_;
103 | p->pos = arithBiOpExpr->pos;
104 | p->op= arithBiOpExpr->op;
105 | p->left = aA_ArithExpr(arithBiOpExpr->left);
106 | p->right = aA_ArithExpr(arithBiOpExpr->right);
107 | return p;
108 | }
109 |
110 | aA_arithUExpr aA_ArithUExpr(A_arithUExpr arithUExpr){
111 | aA_arithUExpr p = new aA_arithUExpr_;
112 | p->pos = arithUExpr->pos;
113 | p->op= arithUExpr->op;
114 | p->expr = aA_ExprUnit(arithUExpr->expr);
115 | return p;
116 | }
117 |
118 | aA_arithExpr aA_ArithExpr(A_arithExpr arithExpr){
119 | aA_arithExpr p = new aA_arithExpr_;
120 | p->pos = arithExpr->pos;
121 | p->kind= arithExpr->kind;
122 | switch(arithExpr->kind){
123 | case A_arithBiOpExprKind:{
124 | p->u.arithBiOpExpr = aA_ArithBiOpExpr(arithExpr->u.arithBiOpExpr);
125 | break;
126 | }
127 | case A_exprUnitKind:{
128 | p->u.exprUnit = aA_ExprUnit(arithExpr->u.exprUnit);
129 | break;
130 | }
131 | }
132 | return p;
133 | }
134 |
135 | aA_boolBiOpExpr aA_BoolBiOpExpr(A_boolBiOpExpr boolBiOpExpr){
136 | aA_boolBiOpExpr p = new aA_boolBiOpExpr_;
137 | p->pos = boolBiOpExpr->pos;
138 | p->op= boolBiOpExpr->op;
139 | p->left = aA_BoolExpr(boolBiOpExpr->left);
140 | p->right = aA_BoolUnit(boolBiOpExpr->right);
141 | return p;
142 | }
143 |
144 | aA_boolUOpExpr aA_BoolUOpExpr(A_boolUOpExpr boolUOpExpr){
145 | aA_boolUOpExpr p = new aA_boolUOpExpr_;
146 | p->pos = boolUOpExpr->pos;
147 | p->op= boolUOpExpr->op;
148 | p->cond = aA_BoolUnit(boolUOpExpr->cond);
149 | return p;
150 | }
151 |
152 | aA_boolExpr aA_BoolExpr(A_boolExpr boolExpr){
153 | aA_boolExpr p = new aA_boolExpr_;
154 | p->pos = boolExpr->pos;
155 | p->kind= boolExpr->kind;
156 | switch(boolExpr->kind){
157 | case A_boolBiOpExprKind:{
158 | p->u.boolBiOpExpr = aA_BoolBiOpExpr(boolExpr->u.boolBiOpExpr);
159 | break;
160 | }
161 | case A_boolUnitKind:{
162 | p->u.boolUnit = aA_BoolUnit(boolExpr->u.boolUnit);
163 | break;
164 | }
165 | }
166 | return p;
167 | }
168 |
169 | aA_comExpr aA_ComExpr(A_comExpr comExpr){
170 | aA_comExpr p = new aA_comExpr_;
171 | p->pos= comExpr->pos;
172 | p->op = comExpr->op;
173 | p->left = aA_ExprUnit(comExpr->left);
174 | p->right = aA_ExprUnit(comExpr->right);
175 | return p;
176 | }
177 |
178 | aA_boolUnit aA_BoolUnit(A_boolUnit boolUnit){
179 | aA_boolUnit p = new aA_boolUnit_;
180 | p->pos = boolUnit->pos;
181 | p->kind= boolUnit->kind;
182 | switch(boolUnit->kind){
183 | case A_comOpExprKind:{
184 | p->u.comExpr = aA_ComExpr(boolUnit->u.comExpr);
185 | break;
186 | }
187 | case A_boolExprKind:{
188 | p->u.boolExpr = aA_BoolExpr(boolUnit->u.boolExpr);
189 | break;
190 | }
191 | case A_boolUOpExprKind:{
192 | p->u.boolUOpExpr = aA_BoolUOpExpr(boolUnit->u.boolUOpExpr);
193 | break;
194 | }
195 | }
196 | return p;
197 | }
198 |
199 | aA_rightVal aA_RightVal(A_rightVal rightVal){
200 | aA_rightVal p = new aA_rightVal_;
201 | p->pos = rightVal->pos;
202 | p->kind= rightVal->kind;
203 | switch(rightVal->kind){
204 | case A_arithExprValKind:{
205 | p->u.arithExpr = aA_ArithExpr(rightVal->u.arithExpr);
206 | break;
207 | }
208 | case A_boolExprValKind:{
209 | p->u.boolExpr = aA_BoolExpr(rightVal->u.boolExpr);
210 | break;
211 | }
212 | }
213 | return p;
214 | }
215 |
216 | aA_leftVal aA_LeftVal(A_leftVal leftVal){
217 | aA_leftVal p = new aA_leftVal_;
218 | p->pos = leftVal->pos;
219 | p->kind= leftVal->kind;
220 | switch(leftVal->kind){
221 | case A_varValKind:{
222 | p->u.id = new string(leftVal->u.id);
223 | break;
224 | }
225 | case A_arrValKind:{
226 | p->u.arrExpr = aA_ArrayExpr(leftVal->u.arrExpr);
227 | break;
228 | }
229 | case A_memberValKind:{
230 | p->u.memberExpr = aA_MemberExpr(leftVal->u.memberExpr);
231 | break;
232 | }
233 | }
234 | return p;
235 | }
236 |
237 | aA_assignStmt aA_AssignStmt(A_assignStmt assignStmt){
238 | aA_assignStmt p = new aA_assignStmt_;
239 | p->pos = assignStmt->pos;
240 | p->leftVal= aA_LeftVal(assignStmt->leftVal);
241 | p->rightVal = aA_RightVal(assignStmt->rightVal);
242 | return p;
243 | }
244 |
245 | aA_varDeclScalar aA_VarDeclScalar(A_varDeclScalar varDeclScalar){
246 | aA_varDeclScalar p = new aA_varDeclScalar_;
247 | p->pos = varDeclScalar->pos;
248 | p->id= new string(varDeclScalar->id);
249 | p->type = aA_Type(varDeclScalar->type);
250 | return p;
251 | }
252 |
253 | aA_varDeclArray aA_VarDeclArray(A_varDeclArray varDeclArray){
254 | aA_varDeclArray p = new aA_varDeclArray_;
255 | p->pos = varDeclArray->pos;
256 | p->id= new string(varDeclArray->id);
257 | p->len = varDeclArray->len;
258 | p->type = aA_Type(varDeclArray->type);
259 | return p;
260 | }
261 |
262 | aA_varDecl aA_VarDecl(A_varDecl varDecl){
263 | aA_varDecl p = new aA_varDecl_;
264 | p->pos = varDecl->pos;
265 | p->kind = varDecl->kind;
266 | switch(varDecl->kind){
267 | case A_varDeclScalarKind:{
268 | p->u.declScalar = aA_VarDeclScalar(varDecl->u.declScalar);
269 | break;
270 | }
271 | case A_varDeclArrayKind:{
272 | p->u.declArray = aA_VarDeclArray(varDecl->u.declArray);
273 | break;
274 | }
275 | }
276 | return p;
277 | }
278 |
279 | aA_varDefScalar aA_VarDefScalar(A_varDefScalar varDefScalar){
280 | aA_varDefScalar p = new aA_varDefScalar_;
281 | p->pos = varDefScalar->pos;
282 | p->id= new string(varDefScalar->id);
283 | p->type = aA_Type(varDefScalar->type);
284 | p->val = aA_RightVal(varDefScalar->val);
285 | return p;
286 | }
287 |
288 | aA_varDefArray aA_VarDefArray(A_varDefArray varDefArray){
289 | aA_varDefArray p = new aA_varDefArray_;
290 | p->pos = varDefArray->pos;
291 | p->id= new string(varDefArray->id);
292 | p->len = varDefArray->len;
293 | p->type = aA_Type(varDefArray->type);
294 | for(A_rightValList l=varDefArray->vals; l; l=l->tail){
295 | p->vals.emplace_back(aA_RightVal(l->head));
296 | }
297 | return p;
298 | }
299 |
300 | aA_varDef aA_VarDef(A_varDef varDef){
301 | aA_varDef p = new aA_varDef_;
302 | p->pos = varDef->pos;
303 | p->kind = varDef->kind;
304 | switch(varDef->kind){
305 | case A_varDefScalarKind:{
306 | p->u.defScalar = aA_VarDefScalar(varDef->u.defScalar);
307 | break;
308 | }
309 | case A_varDefArrayKind:{
310 | p->u.defArray = aA_VarDefArray(varDef->u.defArray);
311 | break;
312 | }
313 | }
314 | return p;
315 | }
316 |
317 | aA_varDeclStmt aA_VarDeclStmt(A_varDeclStmt varDeclStmt){
318 | aA_varDeclStmt p = new aA_varDeclStmt_;
319 | p->pos = varDeclStmt->pos;
320 | p->kind = varDeclStmt->kind;
321 | switch(varDeclStmt->kind){
322 | case A_varDeclKind:{
323 | p->u.varDecl = aA_VarDecl(varDeclStmt->u.varDecl);
324 | break;
325 | }
326 | case A_varDefKind:{
327 | p->u.varDef = aA_VarDef(varDeclStmt->u.varDef);
328 | break;
329 | }
330 | }
331 | return p;
332 |
333 | }
334 |
335 | aA_structDef aA_StructDef(A_structDef structDef){
336 | aA_structDef p = new aA_structDef_;
337 | p->pos = structDef->pos;
338 | p->id = new string(structDef->id);
339 | for(A_varDeclList l=structDef->varDecls; l; l=l->tail){
340 | p->varDecls.emplace_back(aA_VarDecl(l->head));
341 | }
342 | return p;
343 | }
344 |
345 | aA_paramDecl aA_ParamDecl(A_paramDecl paramDecl){
346 | aA_paramDecl p = new aA_paramDecl_;
347 | for(A_varDeclList l=paramDecl->varDecls; l; l=l->tail){
348 | p->varDecls.emplace_back(aA_VarDecl(l->head));
349 | }
350 | return p;
351 | }
352 |
353 | aA_fnDecl aA_FnDecl(A_fnDecl fnDecl){
354 | aA_fnDecl p = new aA_fnDecl_;
355 | p->pos = fnDecl->pos;
356 | p->id = new string(fnDecl->id);
357 | p->paramDecl = aA_ParamDecl(fnDecl->paramDecl);
358 | p->type = aA_type(fnDecl->type);
359 | return p;
360 | }
361 |
362 | aA_fnDef aA_FnDef(A_fnDef fnDef){
363 | aA_fnDef p = new aA_fnDef_;
364 | p->pos = fnDef->pos;
365 | p->fnDecl = aA_FnDecl(fnDef->fnDecl);
366 | for(A_codeBlockStmtList l=fnDef->stmts; l; l=l->tail){
367 | p->stmts.emplace_back(aA_CodeBlockStmt(l->head));
368 | }
369 | return p;
370 | }
371 |
372 | aA_ifStmt aA_IfStmt(A_ifStmt ifStmt){
373 | aA_ifStmt p = new aA_ifStmt_;
374 | p->pos = ifStmt->pos;
375 | p->boolExpr = aA_BoolExpr(ifStmt->boolExpr);
376 | for(A_codeBlockStmtList l=ifStmt->ifStmts; l; l=l->tail){
377 | p->ifStmts.emplace_back(aA_CodeBlockStmt(l->head));
378 | }
379 | for(A_codeBlockStmtList l=ifStmt->elseStmts; l; l=l->tail){
380 | p->elseStmts.emplace_back(aA_CodeBlockStmt(l->head));
381 | }
382 | return p;
383 | }
384 |
385 | aA_whileStmt aA_WhileStmt(A_whileStmt whileStmt){
386 | aA_whileStmt p = new aA_whileStmt_;
387 | p->pos = whileStmt->pos;
388 | p->boolExpr = aA_BoolExpr(whileStmt->boolExpr);
389 | for(A_codeBlockStmtList l=whileStmt->whileStmts; l; l=l->tail){
390 | p->whileStmts.emplace_back(aA_CodeBlockStmt(l->head));
391 | }
392 | return p;
393 | }
394 |
395 | aA_callStmt aA_CallStmt(A_callStmt callStmt){
396 | aA_callStmt p = new aA_callStmt_;
397 | p->pos = callStmt->pos;
398 | p->fnCall = aA_FnCall(callStmt->fnCall);
399 | return p;
400 | }
401 |
402 | aA_returnStmt aA_ReturnStmt(A_returnStmt returnStmt){
403 | aA_returnStmt p = new aA_returnStmt_;
404 | p->pos = returnStmt->pos;
405 | p->retVal = aA_RightVal(returnStmt->retVal);
406 | return p;
407 | }
408 |
409 | aA_codeBlockStmt aA_CodeBlockStmt(A_codeBlockStmt codeBlockStmt){
410 | aA_codeBlockStmt p = new aA_codeBlockStmt_;
411 | p->pos = codeBlockStmt->pos;
412 | p->kind = codeBlockStmt->kind;
413 | switch(codeBlockStmt->kind){
414 | case A_nullStmtKind:{
415 | break;
416 | }
417 | case A_varDeclStmtKind:{
418 | p->u.varDeclStmt = aA_VarDeclStmt(codeBlockStmt->u.varDeclStmt);
419 | break;
420 | }
421 | case A_assignStmtKind:{
422 | p->u.assignStmt = aA_AssignStmt(codeBlockStmt->u.assignStmt);
423 | break;
424 | }
425 | case A_callStmtKind:{
426 | p->u.callStmt = aA_CallStmt(codeBlockStmt->u.callStmt);
427 | break;
428 | }
429 | case A_ifStmtKind:{
430 | p->u.ifStmt = aA_IfStmt(codeBlockStmt->u.ifStmt);
431 | break;
432 | }
433 | case A_whileStmtKind:{
434 | p->u.whileStmt = aA_WhileStmt(codeBlockStmt->u.whileStmt);
435 | break;
436 | }
437 | case A_returnStmtKind:{
438 | p->u.returnStmt = aA_ReturnStmt(codeBlockStmt->u.returnStmt);
439 | break;
440 | }
441 | case A_continueStmtKind:{
442 | break;
443 | }
444 | case A_breakStmtKind:{
445 | break;
446 | }
447 | }
448 | return p;
449 | }
450 |
451 | aA_fnDeclStmt aA_FnDeclStmt(A_fnDeclStmt fnDeclStmt){
452 | aA_fnDeclStmt p = new aA_fnDeclStmt_;
453 | p->pos = fnDeclStmt->pos;
454 | p->fnDecl = aA_FnDecl(fnDeclStmt->fnDecl);
455 | return p;
456 | }
457 |
458 | aA_programElement aA_ProgramElement(A_programElement programElement){
459 | aA_programElement p = new aA_programElement_;
460 | p->pos = programElement->pos;
461 | p->kind = programElement->kind;
462 | switch(programElement->kind){
463 | case A_programNullStmtKind:{
464 | break;
465 | }
466 | case A_programVarDeclStmtKind:{
467 | p->u.varDeclStmt = aA_VarDeclStmt(programElement->u.varDeclStmt);
468 | break;
469 | }
470 | case A_programStructDefKind:{
471 | p->u.structDef = aA_StructDef(programElement->u.structDef);
472 | break;
473 | }
474 | case A_programFnDeclStmtKind:{
475 | p->u.fnDeclStmt = aA_FnDeclStmt(programElement->u.fnDeclStmt);
476 | break;
477 | }
478 | case A_programFnDefKind:{
479 | p->u.fnDef = aA_FnDef(programElement->u.fnDef);
480 | break;
481 | }
482 | }
483 | return p;
484 | }
485 |
486 | aA_program aA_Program(A_program program){
487 | aA_program p = new aA_program_;
488 | for(A_programElementList l=program->programElements; l; l=l->tail){
489 | p->programElements.emplace_back(aA_ProgramElement(l->head));
490 | }
491 | return p;
492 | }
--------------------------------------------------------------------------------
/src/TeaplaAst.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "TeaplAst.h"
4 | #include
5 | #include
6 |
7 | using std::string;
8 | using std::vector;
9 |
10 | typedef struct aA_type_* aA_type;
11 | typedef struct aA_varDecl_* aA_varDecl;
12 | typedef struct aA_varDef_* aA_varDef;
13 | typedef struct aA_rightVal_* aA_rightVal;
14 | typedef struct aA_arithExpr_* aA_arithExpr;
15 | typedef struct aA_boolExpr_* aA_boolExpr;
16 | typedef struct aA_arithBiOpExpr_* aA_arithBiOpExpr;
17 | typedef struct aA_arithUExpr_* aA_arithUExpr;
18 | typedef struct aA_exprUnit_* aA_exprUnit;
19 | typedef struct aA_fnCall_* aA_fnCall;
20 | typedef struct aA_indexExpr_* aA_indexExpr;
21 | typedef struct aA_arrayExpr_* aA_arrayExpr;
22 | typedef struct aA_memberExpr_* aA_memberExpr;
23 | typedef struct aA_boolUnit_* aA_boolUnit;
24 | typedef struct aA_boolBiOpExpr_* aA_boolBiOpExpr;
25 | typedef struct aA_boolUOpExpr_* aA_boolUOpExpr;
26 | typedef struct aA_comExpr_* aA_comExpr;
27 | typedef struct aA_rightVal_* aA_rightVal;
28 | typedef struct aA_leftVal_* aA_leftVal;
29 | typedef struct aA_assignStmt_* aA_assignStmt;
30 | typedef struct aA_varDefScalar_* aA_varDefScalar;
31 | typedef struct aA_varDefArray_* aA_varDefArray;
32 | typedef struct aA_varDeclScalar_* aA_varDeclScalar;
33 | typedef struct aA_varDeclArray_* aA_varDeclArray;
34 | typedef struct aA_varDecl_* aA_varDecl;
35 | typedef struct aA_varDef_* aA_varDef;
36 | typedef struct aA_varDeclStmt_* aA_varDeclStmt;
37 | typedef struct aA_structDef_* aA_structDef;
38 | typedef struct aA_paramDecl_* aA_paramDecl;
39 | typedef struct aA_fnDecl_* aA_fnDecl;
40 | typedef struct aA_fnDef_* aA_fnDef;
41 | typedef struct aA_codeBlockStmt_* aA_codeBlockStmt;
42 | typedef struct aA_ifStmt_* aA_ifStmt;
43 | typedef struct aA_whileStmt_* aA_whileStmt;
44 | typedef struct aA_fnDeclStmt_* aA_fnDeclStmt;
45 | typedef struct aA_callStmt_* aA_callStmt;
46 | typedef struct aA_returnStmt_* aA_returnStmt;
47 | typedef struct aA_programElement_* aA_programElement;
48 | typedef struct aA_program_* aA_program;
49 |
50 | struct aA_type_ {
51 | A_pos pos;
52 | A_dataType type;
53 | union {
54 | A_nativeType nativeType;
55 | string* structType;
56 | } u;
57 | };
58 |
59 | struct aA_fnCall_ {
60 | A_pos pos;
61 | string* fn;
62 | vector vals;
63 | };
64 |
65 | struct aA_indexExpr_ {
66 | A_pos pos;
67 | A_indexExprKind kind;
68 | union {
69 | int num;
70 | string* id;
71 | } u;
72 | };
73 |
74 | struct aA_arrayExpr_ {
75 | A_pos pos;
76 | string* arr;
77 | aA_indexExpr idx;
78 | };
79 |
80 | struct aA_memberExpr_ {
81 | A_pos pos;
82 | string* structId;
83 | string* memberId;
84 | };
85 |
86 | struct aA_exprUnit_ {
87 | A_pos pos;
88 | A_exprUnitType kind;
89 | union {
90 | int num;
91 | string* id;
92 | aA_arithExpr arithExpr;
93 | aA_fnCall callExpr;
94 | aA_arrayExpr arrayExpr;
95 | aA_memberExpr memberExpr;
96 | aA_arithUExpr arithUExpr;
97 | } u;
98 | };
99 |
100 | struct aA_arithBiOpExpr_ {
101 | A_pos pos;
102 | A_arithBiOp op;
103 | aA_arithExpr left, right;
104 | };
105 |
106 | struct aA_arithUExpr_ {
107 | A_pos pos;
108 | A_arithUOp op;
109 | aA_exprUnit expr;
110 | };
111 |
112 | struct aA_arithExpr_ {
113 | A_pos pos;
114 | A_arithExprType kind;
115 | union {
116 | aA_arithBiOpExpr arithBiOpExpr;
117 | aA_exprUnit exprUnit;
118 | } u;
119 | };
120 |
121 | struct aA_boolBiOpExpr_ {
122 | A_pos pos;
123 | A_boolBiOp op;
124 | aA_boolExpr left;
125 | aA_boolUnit right;
126 | };
127 |
128 | struct aA_boolUOpExpr_ {
129 | A_pos pos;
130 | A_boolUOp op;
131 | aA_boolUnit cond;
132 | };
133 |
134 | struct aA_boolExpr_ {
135 | A_pos pos;
136 | A_boolExprType kind;
137 | union {
138 | aA_boolBiOpExpr boolBiOpExpr;
139 | aA_boolUnit boolUnit;
140 | } u;
141 | };
142 |
143 | struct aA_comExpr_ {
144 | A_pos pos;
145 | A_comOp op;
146 | aA_exprUnit left, right;
147 | };
148 |
149 | struct aA_boolUnit_ {
150 | A_pos pos;
151 | A_boolUnitType kind;
152 | union {
153 | aA_comExpr comExpr;
154 | aA_boolExpr boolExpr;
155 | aA_boolUOpExpr boolUOpExpr;
156 | } u;
157 | };
158 |
159 | struct aA_rightVal_ {
160 | A_pos pos;
161 | A_rightValType kind;
162 | union {
163 | aA_arithExpr arithExpr;
164 | aA_boolExpr boolExpr;
165 | } u;
166 | };
167 |
168 | struct aA_leftVal_ {
169 | A_pos pos;
170 | A_leftValType kind;
171 | union {
172 | string* id;
173 | aA_arrayExpr arrExpr;
174 | aA_memberExpr memberExpr;
175 | } u;
176 | };
177 |
178 | struct aA_assignStmt_ {
179 | A_pos pos;
180 | aA_leftVal leftVal;
181 | aA_rightVal rightVal;
182 | };
183 |
184 | struct aA_varDeclScalar_ {
185 | A_pos pos;
186 | string* id;
187 | aA_type type;
188 | };
189 |
190 | struct aA_varDeclArray_ {
191 | A_pos pos;
192 | string* id;
193 | int len;
194 | aA_type type;
195 | };
196 |
197 | struct aA_varDecl_ {
198 | A_pos pos;
199 | A_varDeclType kind;
200 | union {
201 | aA_varDeclScalar declScalar;
202 | aA_varDeclArray declArray;
203 | } u;
204 | };
205 |
206 | struct aA_varDefScalar_ {
207 | A_pos pos;
208 | string* id;
209 | aA_type type;
210 | aA_rightVal val;
211 | };
212 |
213 | struct aA_varDefArray_ {
214 | A_pos pos;
215 | string* id;
216 | int len;
217 | aA_type type;
218 | vector vals;
219 | };
220 |
221 | struct aA_varDef_ {
222 | A_pos pos;
223 | A_varDefType kind;
224 | union {
225 | aA_varDefScalar defScalar;
226 | aA_varDefArray defArray;
227 | } u;
228 | };
229 |
230 | struct aA_varDeclStmt_ {
231 | A_pos pos;
232 | A_varDeclStmtType kind;
233 | union {
234 | aA_varDecl varDecl;
235 | aA_varDef varDef;
236 | } u;
237 | };
238 |
239 | struct aA_structDef_ {
240 | A_pos pos;
241 | string* id;
242 | vector varDecls;
243 | };
244 |
245 | struct aA_fnDecl_ {
246 | A_pos pos;
247 | string* id;
248 | aA_paramDecl paramDecl;
249 | aA_type type;
250 | };
251 |
252 | struct aA_paramDecl_ {
253 | vector varDecls;
254 | };
255 |
256 | struct aA_fnDef_ {
257 | A_pos pos;
258 | aA_fnDecl fnDecl;
259 | vector stmts;
260 | };
261 |
262 | struct aA_ifStmt_ {
263 | A_pos pos;
264 | aA_boolExpr boolExpr;
265 | vector ifStmts, elseStmts;
266 | };
267 |
268 | struct aA_whileStmt_ {
269 | A_pos pos;
270 | aA_boolExpr boolExpr;
271 | vector whileStmts;
272 | };
273 |
274 | struct aA_callStmt_ {
275 | A_pos pos;
276 | aA_fnCall fnCall;
277 | };
278 |
279 | struct aA_returnStmt_ {
280 | A_pos pos;
281 | aA_rightVal retVal;
282 | };
283 |
284 | struct aA_codeBlockStmt_ {
285 | A_pos pos;
286 | A_codeBlockStmtType kind;
287 | union {
288 | aA_varDeclStmt varDeclStmt;
289 | aA_assignStmt assignStmt;
290 | aA_callStmt callStmt;
291 | aA_ifStmt ifStmt;
292 | aA_whileStmt whileStmt;
293 | aA_returnStmt returnStmt;
294 | // continue and break do not need other info
295 | } u;
296 | };
297 |
298 | struct aA_fnDeclStmt_ {
299 | A_pos pos;
300 | aA_fnDecl fnDecl;
301 | };
302 |
303 | struct aA_programElement_ {
304 | A_pos pos;
305 | A_programElementType kind;
306 | union {
307 | aA_varDeclStmt varDeclStmt;
308 | aA_structDef structDef;
309 | aA_fnDeclStmt fnDeclStmt;
310 | aA_fnDef fnDef;
311 | } u;
312 | };
313 |
314 | struct aA_program_ {
315 | vector programElements;
316 | };
317 |
318 |
319 | aA_type aA_Type(A_type type);
320 | aA_fnCall aA_FnCall(A_fnCall fnCall);
321 | aA_indexExpr aA_IndexExpr(A_indexExpr indexExpr);
322 | aA_arrayExpr aA_ArrayExpr(A_arrayExpr arrayExpr);
323 | aA_memberExpr aA_MemberExpr(A_memberExpr memberExpr);
324 | aA_exprUnit aA_ExprUnit(A_exprUnit exprUnit);
325 | aA_arithBiOpExpr aA_ArithBiOpExpr(A_arithBiOpExpr arithBiOpExpr);
326 | aA_arithUExpr aA_ArithUExpr(A_arithUExpr arithUExpr);
327 | aA_arithExpr aA_ArithExpr(A_arithExpr arithExpr);
328 | aA_boolBiOpExpr aA_BoolBiOpExpr(A_boolBiOpExpr boolBiOpExpr);
329 | aA_boolUOpExpr aA_BoolUOpExpr(A_boolUOpExpr boolUOpExpr);
330 | aA_boolExpr aA_BoolExpr(A_boolExpr boolExpr);
331 | aA_comExpr aA_ComExpr(A_comExpr comExpr);
332 | aA_boolUnit aA_BoolUnit(A_boolUnit boolUnit);
333 | aA_rightVal aA_RightVal(A_rightVal rightVal);
334 | aA_leftVal aA_LeftVal(A_leftVal leftVal);
335 | aA_assignStmt aA_AssignStmt(A_assignStmt assignStmt);
336 | aA_varDeclScalar aA_VarDeclScalar(A_varDeclScalar varDeclScalar);
337 | aA_varDeclArray aA_VarDeclArray(A_varDeclArray varDeclArray);
338 | aA_varDecl aA_VarDecl(A_varDecl varDecl);
339 | aA_varDefScalar aA_VarDefScalar(A_varDefScalar varDefScalar);
340 | aA_varDefArray aA_VarDefArray(A_varDefArray varDefArray);
341 | aA_varDef aA_VarDef(A_varDef varDef);
342 | aA_varDeclStmt aA_VarDeclStmt(A_varDeclStmt varDeclStmt);
343 | aA_structDef aA_StructDef(A_structDef structDef);
344 | aA_paramDecl aA_ParamDecl(A_paramDecl paramDecl);
345 | aA_fnDecl aA_FnDecl(A_fnDecl fnDecl);
346 | aA_fnDef aA_FnDef(A_fnDef fnDef);
347 | aA_ifStmt aA_IfStmt(A_ifStmt ifStmt);
348 | aA_whileStmt aA_WhileStmt(A_whileStmt whileStmt);
349 | aA_callStmt aA_CallStmt(A_callStmt callStmt);
350 | aA_returnStmt aA_ReturnStmt(A_returnStmt returnStmt);
351 | aA_codeBlockStmt aA_CodeBlockStmt(A_codeBlockStmt codeBlockStmt);
352 | aA_fnDeclStmt aA_FnDeclStmt(A_fnDeclStmt fnDeclStmt);
353 | aA_programElement aA_ProgramElement(A_programElement programElement);
354 | aA_program aA_Program(A_program program);
355 |
--------------------------------------------------------------------------------
/src/compiler.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "TeaplAst.h"
3 | #include "TeaplaAst.h"
4 | #include "PrintTeaplaAst.h"
5 | #include "y.tab.hpp"
6 |
7 | // set YACCDEBUG 1 to turn on yydebug
8 | #define YACCDEBUG 0
9 |
10 | using namespace std;
11 |
12 | extern int yyparse();
13 | extern YYSTYPE yylval;
14 | extern int yydebug;
15 |
16 | int line, col;
17 |
18 | A_program root;
19 | aA_program aroot;
20 |
21 | int main(int argc, char * argv[]) {
22 |
23 | #if YACCDEBUG
24 | yydebug = 1;
25 | #endif
26 |
27 | line = 1;
28 | col = 1;
29 |
30 | freopen(argv[1], "r", stdin);
31 | ofstream ASTStream;
32 | ASTStream.open(argv[2]);
33 |
34 | yyparse();
35 | aroot = aA_Program(root);
36 | print_aA_Program(aroot, ASTStream);
37 |
38 | ASTStream.close();
39 | return 0;
40 | }
--------------------------------------------------------------------------------
/src/lexer.lex:
--------------------------------------------------------------------------------
1 | %{
2 | #include
3 | #include
4 | #include "TeaplAst.h"
5 | #include "y.tab.hpp"
6 | extern int line, col;
7 | %}
8 |
9 | // TODO:
10 | // your lexer
--------------------------------------------------------------------------------
/src/parser.yacc:
--------------------------------------------------------------------------------
1 | %{
2 | #include
3 | #include "TeaplAst.h"
4 |
5 | extern A_pos pos;
6 | extern A_program root;
7 |
8 | extern int yylex(void);
9 | extern "C"{
10 | extern void yyerror(char *s);
11 | extern int yywrap();
12 | }
13 |
14 | %}
15 |
16 | // TODO:
17 | // your parser
18 |
19 |
20 |
21 | extern "C"{
22 | void yyerror(char * s)
23 | {
24 | fprintf(stderr, "%s\n",s);
25 | }
26 | int yywrap()
27 | {
28 | return(1);
29 | }
30 | }
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/tests/test01.refast:
--------------------------------------------------------------------------------
1 | fn main(a:int, b:int)->int{
2 | let a:int;
3 | let b:int = 0;
4 | ret a+b;
5 | }
6 |
7 |
--------------------------------------------------------------------------------
/src/tests/test01.tea:
--------------------------------------------------------------------------------
1 | fn main(a:int, b:int)->int{
2 | let a:int;
3 | let b:int = 0;
4 | ret a + b;
5 | }
--------------------------------------------------------------------------------
/src/tests/test02.refast:
--------------------------------------------------------------------------------
1 | fn main(a:int, b:int)->int{
2 | let a:int;
3 | let b:int = 0;
4 | let c:MyStruct;
5 | ;
6 | c.node = 1;
7 | c.len = 3;
8 | ret foo(c.node, c.len);
9 | }
10 |
11 | ;
12 |
13 | struct MyStruct {
14 | node:int,
15 | len:int
16 | }
17 |
18 | fn foo(a:int, b:int)->int{
19 | ret a+1+b+2;
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/src/tests/test02.tea:
--------------------------------------------------------------------------------
1 | fn main(a:int, b:int)->int{
2 | let a:int;
3 | let b:int = 0;
4 | let c:MyStruct;
5 | ;
6 | c.node = 1;
7 | c.len = 3;
8 | ret foo(c.node, c.len);
9 | }
10 |
11 | ;
12 |
13 | struct MyStruct {
14 | node:int,
15 | len:int
16 | }
17 |
18 | fn foo(a:int, b:int)->int{
19 | ret a+1+b+2;
20 | }
--------------------------------------------------------------------------------
/src/tests/test03.refast:
--------------------------------------------------------------------------------
1 | fn main(a:int, b:int)->int{
2 | let a:int;
3 | let b:int = 0;
4 | let c:MyStruct;
5 | c.node = 1;
6 | c.len = 3;
7 | ret foo(c.node, c.len);
8 | }
9 |
10 | struct MyStruct {
11 | node:int,
12 | len:int
13 | }
14 |
15 | fn foo(a:int, b:int)->int{
16 | ret a+1+b+2;
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/src/tests/test03.tea:
--------------------------------------------------------------------------------
1 | // test comment
2 | fn main(a:int, b:int)->int{
3 | let a:int;
4 | let b:int = 0;
5 | let c:MyStruct;
6 | c.node = 1;
7 | c.len = 3; // comment1
8 | ret foo(c.node, c.len);
9 | }
10 |
11 | struct MyStruct { // comment1
12 | node:int,
13 | len:int /* comment 2 */
14 | }
15 |
16 | /* comment 2 */
17 |
18 | fn foo(a:int, b:int)->int{
19 | /* comment 2 */
20 | ret a+1+b+2;
21 | }
--------------------------------------------------------------------------------