forked from rui314/chibicc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
124 lines (104 loc) · 2.62 KB
/
main.c
File metadata and controls
124 lines (104 loc) · 2.62 KB
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
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
TK_PUNCT, // Punctuators
TK_NUM, // Numeric literals
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 *loc; // Token location
int len; // Token length
};
// Reports an error and exit.
static void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// Consumes the current token if it matches `op`.
static bool equal(Token *tok, char *op) {
return memcmp(tok->loc, op, tok->len) == 0 && op[tok->len] == '\0';
}
// Ensure that the current token is `s`.
static Token *skip(Token *tok, char *s) {
if (!equal(tok, s))
error("expected '%s'", s);
return tok->next;
}
// Ensure that the current token is TK_NUM.
static int get_number(Token *tok) {
if (tok->kind != TK_NUM)
error("expected a number");
return tok->val;
}
// Create a new token.
static Token *new_token(TokenKind kind, char *start, char *end) {
Token *tok = calloc(1, sizeof(Token));
tok->kind = kind;
tok->loc = start;
tok->len = end - start;
return tok;
}
// Tokenize `p` and returns new tokens.
static Token *tokenize(char *p) {
Token head = {};
Token *cur = &head;
while (*p) {
// Skip whitespace characters.
if (isspace(*p)) {
p++;
continue;
}
// Numeric literal
if (isdigit(*p)) {
cur = cur->next = new_token(TK_NUM, p, p);
char *q = p;
cur->val = strtoul(p, &p, 10);
cur->len = p - q;
continue;
}
// Punctuator
if (*p == '+' || *p == '-') {
cur = cur->next = new_token(TK_PUNCT, p, p + 1);
p++;
continue;
}
error("invalid token");
}
cur = cur->next = new_token(TK_EOF, p, p);
return head.next;
}
int main(int argc, char **argv) {
if (argc != 2)
error("%s: invalid number of arguments", argv[0]);
Token *tok = tokenize(argv[1]);
printf(" .globl main\n");
printf("main:\n");
// The first token must be a number
printf(" mov $%d, %%rax\n", get_number(tok));
tok = tok->next;
// ... followed by either `+ <number>` or `- <number>`.
while (tok->kind != TK_EOF) {
if (equal(tok, "+")) {
printf(" add $%d, %%rax\n", get_number(tok->next));
tok = tok->next->next;
continue;
}
tok = skip(tok, "-");
printf(" sub $%d, %%rax\n", get_number(tok));
tok = tok->next;
}
printf(" ret\n");
return 0;
}