forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecl.c
63 lines (54 loc) · 1.54 KB
/
decl.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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Parsing of declarations
// Copyright (c) 2019 Warren Toomey, GPL3
// Parse the current token and
// return a primitive type enum value
int parse_type(int t) {
if (t == T_CHAR)
return (P_CHAR);
if (t == T_INT)
return (P_INT);
if (t == T_VOID)
return (P_VOID);
fatald("Illegal type, token", t);
}
// variable_declaration: 'int' identifier ';' ;
//
// Parse the declaration of a variable
void var_declaration(void) {
int id, type;
// Get the type of the variable, then the identifier
type = parse_type(Token.token);
scan(&Token);
ident();
// Text now has the identifier's name.
// Add it as a known identifier
// and generate its space in assembly
id = addglob(Text, type, S_VARIABLE);
genglobsym(id);
// Get the trailing semicolon
semi();
}
// For now we have a very simplistic function definition grammar
//
// function_declaration: 'void' identifier '(' ')' compound_statement ;
//
// Parse the declaration of a simplistic function
struct ASTnode *function_declaration(void) {
struct ASTnode *tree;
int nameslot;
// Find the 'void', the identifier, and the '(' ')'.
// For now, do nothing with them
match(T_VOID, "void");
ident();
nameslot = addglob(Text, P_VOID, S_FUNCTION);
lparen();
rparen();
// Get the AST tree for the compound statement
tree = compound_statement();
// Return an A_FUNCTION node which has the function's nameslot
// and the compound statement sub-tree
return (mkastunary(A_FUNCTION, P_VOID, tree, nameslot));
}