-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9cc.h
102 lines (84 loc) · 1.4 KB
/
9cc.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
#ifndef _9CC_H_INCLUDED
#define _9CC_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#define MX_NFUNC 100
// input
extern char *user_input;
// tokenize
typedef enum {
TK_RESERVED,
TK_IDENT,
TK_NUM,
TK_RETURN,
TK_IF,
TK_ELSE,
TK_WHILE,
TK_FOR,
TK_EOF,
TK_TYPE,
} TokenKind;
typedef struct Token Token;
struct Token {
TokenKind kind;
Token *next;
int val;
char *str;
int len;
};
typedef struct LVar LVar;
struct LVar {
LVar *next;
char *name;
int len;
int offset;
};
void tokenize();
extern Token *token;
extern char *user_input;
// construct AST
typedef enum {
ND_ADD,
ND_SUB,
ND_MUL,
ND_DIV,
ND_NUM,
ND_EQ, // ==
ND_NE, // !=
ND_LT, // <
ND_LE, // <=
ND_LVAR,
ND_ADDR,
ND_DEREF,
ND_FUNCVAR,
ND_FUNCDEF,
ND_ASSIGN,
ND_RETURN,
ND_IF,
ND_WHILE,
ND_FOR,
ND_BLOCK,
} NodeKind;
typedef struct Node Node;
struct Node {
NodeKind kind;
Node *lhs; // for args in FUNCDEF
Node *rhs;
Node *next; // for stmtlist in BLOCK and FUNCDEF and arguments in FUNCVAR
char *name; // for FUNCVAR
int len; // for FUNCVAR
int val; // This is used as an index for if, while and for.
int offset;
};
extern Node *code[MX_NFUNC];
extern LVar *locals[MX_NFUNC];
Node *stmt();
Node *expr();
void program();
// assembly code generation
void gen(Node *node);
#endif