forked from pda/dcpu16-asm-c
/
lexer.c
158 lines (138 loc) · 2.84 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "lexer.h"
static int is_name_char(char);
static int is_digit_char(char);
static int is_hex_char(char);
token_t * read_token(lexer_state * state)
{
token_t * t;
int type, value_size;
char * left = state->ptr;
char * right;
char c;
// Ignore spaces.
while ((c = *left) && (c == ' ')) left++;
right = left;
if (c == ';')
{
while (c != '\n') c = *(++right);
type = T_COMMENT;
}
else if (c == '\n')
{
right++;
type = T_NEWLINE;
}
else if (is_name_char(c))
{
while (is_name_char(c)) c = *(++right);
type = T_NAME;
}
else if (c == ':')
{
c = *(++right);
while (is_name_char(c)) c = *(++right);
type = T_LABEL;
}
else if (is_digit_char(c))
{
if (*(left + 1) == 'x') // look-ahead
{
while (is_hex_char(c)) c = *(++right);
type = T_INT_HEX;
}
else
{
while (is_digit_char(c)) c = *(++right);
type = T_INT_DEC;
}
}
else if (c == '[' || c == ']')
{
right++;
type = c == '[' ? T_BRACKET_L : T_BRACKET_R;
}
else if (c == ',')
{
right++;
type = T_COMMA;
}
else if (c == '+')
{
right++;
type = T_PLUS;
}
else if (c == 0)
{
return NULL;
}
else
{
printf("c: '%c' (0x%02x)\n", c, (int)c);
CRASH("unhandled token");
}
t = (token_t *)malloc(sizeof(token_t));
t->type = type;
t->size = value_size = right - left;
t->value = (char *)malloc(value_size + 1);
strlcpy(t->value, left, value_size + 1);
state->ptr = right;
return t;
}
token_t * next_token(lexer_state * state)
{
if (state->ahead > 0)
{
state->ahead--;
state->token_buffer = state->token_buffer->next;
return state->token_buffer->token;
}
else
{
return read_token(state);
}
}
token_t * peek_token(lexer_state * state, int offset)
{
if (offset > TOKEN_BUFFER_SIZE)
CRASH("offset > TOKEN_BUFFER_SIZE");
if (offset <= 0)
CRASH("offset <= 0");
int ahead = state->ahead;
token_list * b = state->token_buffer;
for (int i = 0; i < offset; i++)
{
b = b->next;
if (i >= ahead)
{
b->token = read_token(state);
state->ahead++;
}
}
return b->token;
}
void lexer_init(lexer_state * state, char * source)
{
// Circular linked list: 0 -> 1, 1 -> 2, 2 -> 3, 3 -> 0
token_list * b = (token_list *)malloc(TOKEN_BUFFER_SIZE * sizeof(token_list));
for (int i = 0; i < TOKEN_BUFFER_SIZE; i++)
(b + i)->next = (b + ((i + 1) % TOKEN_BUFFER_SIZE));
state->ptr = source;
state->token_buffer = b;
state->ahead = 0;
}
static int is_name_char(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static int is_digit_char(char c)
{
return c >= '0' && c <= '9';
}
static int is_hex_char(char c)
{
return is_digit_char(c) || (c >= 'A' && c <= 'F') || c == 'x';
}