-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.c
117 lines (97 loc) · 2.12 KB
/
lexer.c
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
#include "lcc.h"
char const* TOKEN_KIND[] = {
"Plus",
"Minus",
"Slash",
"Star",
"Literal"
};
char const* HEX_NUMERIC_DIGS = "0123456789abcdef";
int ClawBack = 0;
static void preprocess_file() {
// TODO: perform input file pre-processing
}
// returns position of the character c in str
// else returns -1
static int charpos(const char* str, int c) {
char *t;
int idx = 0;
for (idx = 0; idx < strlen(str); ++idx) {
printf("c: %c, idx: %d, str[idx]: %c\n", c, idx, str[idx]);
if (str[idx] == c) {
return idx;
}
}
return -1;
}
static int scan_numeric(int c, FILE* file) {
int val = 0, dig;
while ((dig = charpos(HEX_NUMERIC_DIGS, c)) != -1) {
val = 10 * val + dig;
printf("c is %c\n", c);
printf("val is %d\n", val);
c = fgetc(file);
}
ClawBack = c;
return val;
}
static int next(const FILE* file) {
int c;
if (ClawBack) {
c = ClawBack;
ClawBack = 0;
return c;
}
return fgetc(file);
}
static bool is_skippable(int c) {
return (' ' == c || '\n' == c || '\t' == c || '\r' == c);
}
Token* new_token() {
Token* tok =
tok->next = NULL;
return tok;
}
Token* append_token(Token* tok, TokenKind tokenKind, int value) {
Token* new_tok = calloc(1, sizeof(Token));
new_tok->tokenKind = tokenKind;
if (tokenKind == TK_LITERAL) {
new_tok->value = value;
}
tok->next = new_tok;
return new_tok;
}
static void scan(const FILE* file) {
Token* cur = &init_token;
int c;
while((c = next(file)) != EOF) {
printf("c is %c\n", c);
switch(c) {
case '+':
cur = append_token(&cur, TK_PLUS, NULL);
break;
case '-':
cur = append_token(&cur, TK_MINUS, NULL);
break;
case '/':
cur = append_token(&cur, TK_SLASH, NULL);
break;
case '*':
cur = append_token(&cur, TK_STAR, NULL);
break;
default:
if (is_skippable(c)) {
break;
}
if (isdigit(c)) {
cur = append_token(&cur, TK_LITERAL, scan_numeric(c, file));
break;
}
}
}
return init_token;
}
void scan_file() {
preprocess_file();
scan(InputFile);
}