forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.c
182 lines (152 loc) · 4.11 KB
/
stmt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "defs.h"
#include "data.h"
#include "decl.h"
// Parsing of statements
// Copyright (c) 2019 Warren Toomey, GPL3
// compound_statement: // empty, i.e. no statement
// | statement
// | statement statements
// ;
//
// statement: print_statement
// | declaration
// | assignment_statement
// | if_statement
// | while_statement
// ;
// print_statement: 'print' expression ';' ;
//
static struct ASTnode *print_statement(void) {
struct ASTnode *tree;
int reg;
// Match a 'print' as the first token
match(T_PRINT, "print");
// Parse the following expression
tree = binexpr(0);
// Make an print AST tree
tree = mkastunary(A_PRINT, tree, 0);
// Match the following semicolon
// and return the AST
semi();
return (tree);
}
// assignment_statement: identifier '=' expression ';' ;
//
static struct ASTnode *assignment_statement(void) {
struct ASTnode *left, *right, *tree;
int id;
// Ensure we have an identifier
ident();
// Check it's been defined then make a leaf node for it
if ((id = findglob(Text)) == -1) {
fatals("Undeclared variable", Text);
}
right = mkastleaf(A_LVIDENT, id);
// Ensure we have an equals sign
match(T_ASSIGN, "=");
// Parse the following expression
left = binexpr(0);
// Make an assignment AST tree
tree = mkastnode(A_ASSIGN, left, NULL, right, 0);
// Match the following semicolon
// and return the AST
semi();
return (tree);
}
// if_statement: if_head
// | if_head 'else' compound_statement
// ;
//
// if_head: 'if' '(' true_false_expression ')' compound_statement ;
//
// Parse an IF statement including
// any optional ELSE clause
// and return its AST
struct ASTnode *if_statement(void) {
struct ASTnode *condAST, *trueAST, *falseAST = NULL;
// Ensure we have 'if' '('
match(T_IF, "if");
lparen();
// Parse the following expression
// and the ')' following. Ensure
// the tree's operation is a comparison.
condAST = binexpr(0);
if (condAST->op < A_EQ || condAST->op > A_GE)
fatal("Bad comparison operator");
rparen();
// Get the AST for the compound statement
trueAST = compound_statement();
// If we have an 'else', skip it
// and get the AST for the compound statement
if (Token.token == T_ELSE) {
scan(&Token);
falseAST = compound_statement();
}
// Build and return the AST for this statement
return (mkastnode(A_IF, condAST, trueAST, falseAST, 0));
}
// while_statement: 'while' '(' true_false_expression ')' compound_statement ;
//
// Parse a WHILE statement
// and return its AST
struct ASTnode *while_statement(void) {
struct ASTnode *condAST, *bodyAST;
// Ensure we have 'while' '('
match(T_WHILE, "while");
lparen();
// Parse the following expression
// and the ')' following. Ensure
// the tree's operation is a comparison.
condAST = binexpr(0);
if (condAST->op < A_EQ || condAST->op > A_GE)
fatal("Bad comparison operator");
rparen();
// Get the AST for the compound statement
bodyAST = compound_statement();
// Build and return the AST for this statement
return (mkastnode(A_WHILE, condAST, NULL, bodyAST, 0));
}
// Parse a compound statement
// and return its AST
struct ASTnode *compound_statement(void) {
struct ASTnode *left = NULL;
struct ASTnode *tree;
// Require a left curly bracket
lbrace();
while (1) {
switch (Token.token) {
case T_PRINT:
tree = print_statement();
break;
case T_INT:
var_declaration();
tree = NULL; // No AST generated here
break;
case T_IDENT:
tree = assignment_statement();
break;
case T_IF:
tree = if_statement();
break;
case T_WHILE:
tree = while_statement();
break;
case T_RBRACE:
// When we hit a right curly bracket,
// skip past it and return the AST
rbrace();
return (left);
default:
fatald("Syntax error, token", Token.token);
}
// For each new tree, either save it in left
// if left is empty, or glue the left and the
// new tree together
if (tree) {
if (left == NULL)
left = tree;
else
left = mkastnode(A_GLUE, left, NULL, tree, 0);
}
}
}