-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchibicc.h
90 lines (68 loc) · 1.38 KB
/
chibicc.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
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
// tokenize.c
//
typedef enum {
TK_RESERVED, //keywords or punctuators
TK_NUM, //integer literals
TK_IDENT, //identifiers
TK_EOF, //end-of-file markers
} TokenKind;
// Token type
typedef struct Token Token;
struct Token {
TokenKind kind; //token kind
Token *next; //next token
int val; //if kind is TK_NUM, its value
char *str; //token string
char *loc; //token location
int len; //token length
};
//Reports an error and exit.
void error(char *fmt, ...);
void error_tok(Token *tok, char *fmt, ...);
bool equal(Token *tok, char *op);
Token *skip(Token *tok, char *op);
Token *tokenize(char *input);
//
// parse.c
//
typedef enum {
ND_ADD, //+
ND_SUB, //-
ND_MUL, //*
ND_DIV, ///
ND_EQ, //==
ND_NE, //!=
ND_LT, //<
ND_LE, //<=
ND_RT, //>
ND_RE, //>=
ND_NUM, //integer
ND_ASSIGN, //=
ND_VAR, //variable
ND_EXPR_STMT, //Expression statment
ND_RETURN, //"return"
} NodeKind;
// Node type
typedef struct Node Node;
struct Node {
NodeKind kind; //node kind
Node *next; //next node
Node *lhs; //left-hand side
Node *rhs; //right-hand side
int val; //when kind is ND_NUM
char name; //when kind is ND_VAR
};
Node *parse(Token *tok);
//
//codegen.c
//
//void gen(Node *node);
void codegen(Node *node);