-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.c
88 lines (73 loc) · 1.95 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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Parsing of statements
// Copyright (c) 2019 Warren Toomey, GPL3
// Modification Copyright (c) 2022 Emin, GPL3
// statements: statement
// | statement statements
// ;
//
// statement: 'print' expression ';'
// | 'int' identifier ';'
// | identifier '=' expression ';'
// ;
//
// identifier: T_IDENT
// ;
void print_statement(void) {
struct ASTnode* tree;
int reg;
// Match a 'print' as the first token
match(T_PRINT, "print");
// Parse the following expression and
// generate the assembly code
tree = parse_bin_expr(0);
reg = gen_AST(tree, -1);
gen_print_int(reg);
gen_free_regs();
// Match the following semicolon
semi();
}
void 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 = find_glob(Text)) == -1) {
fatals("Undeclared variable", Text);
}
right = create_ast_leaf(A_LVIDENT, id);
// Ensure we have an equals sign
match(T_ASSIGN, "=");
// Parse the following expression
left = parse_bin_expr(0);
// Make an assignment AST tree
tree = create_ast_node(A_ASSIGN, left, right, 0);
// Generate the assembly code for the assignment
gen_AST(tree, -1);
gen_free_regs();
// Match the following semicolon
semi();
}
// Parse one or more statements
void statements(void) {
while (1) {
switch (Token.token) {
case T_PRINT:
print_statement();
break;
case T_INT:
var_declaration();
break;
case T_IDENT:
assignment_statement();
break;
case T_EOF:
return;
default:
fatald("Syntax error, token", Token.token);
}
}
}