-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy3c.h
executable file
·158 lines (130 loc) · 2.96 KB
/
y3c.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Type Type;
//
// tokenize.c
//
// Token
typedef enum {
TOKEN_SYMBOL,
TOKEN_IDENTIFIER,
TOKEN_NUM,
TOKEN_EOF,
} TokenKind;
typedef struct Token Token;
struct Token {
TokenKind kind;
Token *next;
int val; // If kind is TOKEN_NUM, it's assigned
char *token_string;
size_t token_length;
};
void error(char *fmt, ...);
void error_tok(Token *tok, char *fmt, ...);
bool equal(Token *tok, char *s);
Token *skip(Token *tok, char *s);
bool consume(Token **rest, Token *tok, char *str);
Token *tokenize(char *p);
void print_all_token(Token *head);
//
// parse.c
//
// Local variable
typedef struct Var Var;
struct Var {
Var *next;
char *name; // Variable name
Type *ty; // Type
int offset; // Offset from RBP
};
// AST node
typedef enum {
NODE_ADD, // +
NODE_SUB, // -
NODE_MUL, // *
NODE_DIV, // /
NODE_EQ, // ==
NODE_NE, // !=
NODE_LT, // <
NODE_LE, // <=
NODE_GT, // >
NODE_GE, // >=
NODE_ASSIGN, // =
NODE_ADDRESS, // unary &
NODE_DEREFERENCE, // unary *
NODE_RETURN, // return
NODE_IF, // if
NODE_FOR, // for or while
NODE_BLOCK, // { ... }
NODE_FUNCTION_CALL, // Function call
NODE_EXPR_STATEMENT, // Expression statement
NODE_VAR, // Variable
NODE_NUM, // Integer
} NodeKind;
// AST node type
typedef struct Node Node;
struct Node {
NodeKind kind;
Node *next; // Divided by semicolon
Type *ty; // Type, e.g. (pointer to)* int
Token *tok; // Representative token
Node *lhs;
Node *rhs;
// "if or for statement"
Node *cond;
Node *then;
Node *els;
Node *init;
Node *inc;
// Code block
Node *body;
// Function call
char *funcname;
Node *args;
Var *var; // Used if kind == NODE_VAR
int val; // Used if kind == NODE_NUM
};
typedef struct Function Function;
struct Function {
Function *next;
char *name;
Var *params;
Node *node;
Var *locals;
int stack_size;
};
Function *parse(Token *tok);
//
// type.c
//
typedef enum { TY_INT, TY_PTR, TY_FUNC, TY_ARRAY } TypeKind;
struct Type {
TypeKind kind;
int size; // sizeof() value
// Pointer or array
Type *base;
// Declaration
Token *name;
// Array
int array_length;
// Function type
Type *return_ty;
Type *params;
Type *next;
};
extern Type *ty_int;
bool is_integer(Type *ty);
Type *copy_type(Type *ty);
Type *pointer_to(Type *base);
Type *func_type(Type *return_ty);
Type *array_of(Type *base, int size);
void add_type(Node *node);
//
// codegen.c
//
void codegen(Function *prog);