forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecl.c
81 lines (70 loc) · 2.12 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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_LONG)
return (P_LONG);
if (t == T_VOID)
return (P_VOID);
fatald("Illegal type, token", t);
return(0); // Keep -Wall happy
}
// variable_declaration: type 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, 0);
genglobsym(id);
// Get the trailing semicolon
semi();
}
//
// function_declaration: type identifier '(' ')' compound_statement ;
//
// Parse the declaration of a simplistic function
struct ASTnode *function_declaration(void) {
struct ASTnode *tree, *finalstmt;
int nameslot, type, endlabel;
// Get the type of the variable, then the identifier
type = parse_type(Token.token);
scan(&Token);
ident();
// Get a label-id for the end label, add the function
// to the symbol table, and set the Functionid global
// to the function's symbol-id
endlabel = genlabel();
nameslot = addglob(Text, type, S_FUNCTION, endlabel);
Functionid = nameslot;
// Scan in the parentheses
lparen();
rparen();
// Get the AST tree for the compound statement
tree = compound_statement();
// If the function type isn't P_VOID, check that
// the last AST operation in the compound statement
// was a return statement
if (type != P_VOID) {
finalstmt = (tree->op == A_GLUE) ? tree->right : tree;
if (finalstmt == NULL || finalstmt->op != A_RETURN)
fatal("No return for function with non-void type");
}
// Return an A_FUNCTION node which has the function's nameslot
// and the compound statement sub-tree
return (mkastunary(A_FUNCTION, type, tree, nameslot));
}