-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
140 lines (131 loc) · 2.25 KB
/
parser.h
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
// parser.h
// create abstract syntax tree
// Copyright (C) 2018: see LICENSE
#ifndef _YOWAIC_PARSER_H_
#define _YOWAIC_PARSER_H_
#include "util.h"
#include <stddef.h>
// kind of ast
enum {
AST_ADDRESS = 257,
AST_ARRAY_INIT,
AST_COMPOUND,
AST_DECLARATION,
AST_DEREFERENCE,
AST_FOR,
AST_FUN_CALL,
AST_FUN_DEFINE,
AST_GID,
AST_GREF,
AST_IF,
AST_LID,
AST_LREF,
AST_LITERAL,
AST_RETURN,
AST_STRING,
};
// kind of run time type
enum {
RT_ARRAY,
RT_CHAR,
RT_INT,
RT_PTR,
RT_VOID,
};
typedef struct rt_t {
int type;
struct rt_t* ptr;
int size;
} rt_t;
typedef struct Ast {
int kind;
rt_t* rt_type;
union {
// Char
char cval;
// Integer
int ival;
// String
struct {
char* sval;
char* slabel;
};
// Local variable
struct {
char* lname;
int loffset;
};
// Global variable
struct {
char* gname;
char* glabel;
};
// Local reference
struct {
struct Ast* lref;
int lref_offset;
};
// Global reference
struct {
struct Ast* gref;
int gref_offset;
};
// Unary Operator
struct {
struct Ast *operand;
};
// Binary Operator
struct {
struct Ast *left;
struct Ast *right;
};
// Function call or declaration
struct {
char *fun_name;
struct {
ywlist* args;
struct {
ywlist* params;
ywlist* locals;
struct Ast* body;
};
};
};
// Declaration
struct {
struct Ast* decl_var;
struct Ast* decl_init;
};
// Array initializer
ywlist* array_init;
// If statement
struct {
struct Ast* s_cond;
struct Ast* s_then;
struct Ast* s_else;
};
// For statement
struct {
struct Ast* forinit;
struct Ast* forcond;
struct Ast* forstep;
struct Ast* forbody;
};
// Return statement
struct {
struct Ast* ret;
};
// Compound statement
ywlist* compound;
};
} Ast;
extern rt_t* rt_type_char;
extern rt_t* rt_type_int;
extern rt_t* rt_type_void;
extern ywlist* globals;
extern ywlist* locals;
ywlist* parseFunList();
char* createNextLabel();
// print abstract syntax tree
char* astToS(Ast *ast);
#endif