-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9cc.h
91 lines (79 loc) · 2.23 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
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//----------------------------------------------------------------------
// 型宣言(Type declaration)
//----------------------------------------------------------------------
typedef struct Token Token;
typedef enum {
TK_RESERVED, // 記号
TK_IDENT, // 識別子
TK_NUM, // 整数トークン
TK_EOF, // 入力の終わりを表すトークン
} TokenKind;
// トークン型
struct Token {
TokenKind kind; // トークンの型
Token *next; // 次の入力トークン
int val; // kindがTK_NUMの場合、その数値
char *str; // トークン文字列
int len; // トークンの長さ
};
// 抽象構文木のノードの種類
typedef enum {
ND_ADD, // +
ND_SUB, // -
ND_MUL, // *
ND_DIV, // /
ND_ASSIGN, // =
ND_LVAR, // ローカル変数
ND_EQ, // ==
ND_NE, // !=
ND_LT, // <
ND_LE, // <=
ND_NUM, // integer
} NodeKind;
typedef struct Node Node;
struct Node {
NodeKind kind; // ノードの型
Node *lhs; // 左辺 left-hand side
Node *rhs; // 右辺 right-hand side
int val; // kindがND_NUMの場合のみ使う
int offset; // kindがND_LVARの場合のみ使う
// ローカル変数のRBPからのオフセット
};
// 現在着目しているトークン
Token *token;
char *user_input;
Node *code[100];
//----------------------------------------------------------------------
// プロトタイプ宣言(Prototype declaration)
//----------------------------------------------------------------------
void error(char *fmt, ...);
void error_at(char *loc, char *fmt, ...);
bool consume(char *op);
Token *consume_ident();
void expect(char *op);
int expect_number();
bool at_eof();
Token *new_token(TokenKind kind, Token *cur, char *str, int len);
bool startswith(char *p, char *q);
Node *new_node(NodeKind kind, Node *lhs, Node *rhs);
Node *new_node_num(int val);
void program();
Node *stmt();
Node *expr();
Node *assign();
Node *equality();
Node *equality();
Node *relational();
Node *add();
Node *mul();
Node *unary();
Node *primary();
Token *tokenize();
void gen_lval(Node *node);
void gen(Node *node);