-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.c
153 lines (126 loc) · 3.69 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
#include "lexer.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
static so_token so_lexer_string(so_lexer *);
static so_token so_lexer_integer(so_lexer *);
static so_token so_lexer_bareword(so_lexer *);
void so_token_deinit(so_token *t) {
free(t->lexeme);
t->lexeme = NULL;
t->type = SO_TT_INVALID;
}
void so_token_type_to_string(so_token_type tt, char buffer[], int size) {
switch (tt) {
case SO_TT_USE:
strncpy(buffer, "USE", size);
break;
case SO_TT_CALL:
strncpy(buffer, "CALL", size);
break;
case SO_TT_BARE:
strncpy(buffer, "BAREWORD", size);
break;
case SO_TT_STRING:
strncpy(buffer, "STRING", size);
break;
case SO_TT_INTEGER:
strncpy(buffer, "INTEGER", size);
break;
case SO_TT_EOL:
strncpy(buffer, "EOL", size);
break;
case SO_TT_EOF:
strncpy(buffer, "EOF", size);
break;
case SO_TT_INVALID:
strncpy(buffer, "INVALID", size);
break;
}
}
static char so_lexer_advance(so_lexer *l) {
if (l->current >= l->source_length)
return '\0';
return l->source[l->current++];
}
static char so_lexer_peek(so_lexer *l) {
if (l->current >= l->source_length)
return '\0';
return l->source[l->current];
}
static int so_lexer_eof(so_lexer *l) {
return so_lexer_peek(l) == '\0';
}
void so_lexer_init(so_lexer *l, const char *source) {
l->current = 0;
l->start = 0;
l->source_length = strlen(source);
l->source = source;
}
void so_lexer_deinit(so_lexer *l) {
l->current = 0;
l->start = 0;
l->source_length = 0;
l->source = NULL;
}
so_token so_lexer_next_token(so_lexer *l) {
l->start = l->current;
char c = so_lexer_advance(l);
if (c == '\0')
return (so_token){.lexeme = strdup("eof"), .type = SO_TT_EOF};
switch (c) {
case '"':
return so_lexer_string(l);
case '\n':
return (so_token){.lexeme = strdup("eol"), .type = SO_TT_EOL};
case ' ':
return so_lexer_next_token(l);
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return so_lexer_integer(l);
default:
return so_lexer_bareword(l);
}
}
so_token so_lexer_string(so_lexer *l) {
while (!so_lexer_eof(l) && so_lexer_peek(l) != '"')
so_lexer_advance(l);
if (so_lexer_peek(l) != '"') {
fprintf(stderr, "warning: unterminated string literal at %d\n", l->current);
return (so_token){.lexeme = strdup("invalid"), .type = SO_TT_INVALID};
}
char *lexeme = strndup(
&l->source[l->start + 1],
l->current - l->start - 1);
so_lexer_advance(l);
return (so_token){.lexeme = lexeme, .type = SO_TT_STRING};
}
so_token so_lexer_integer(so_lexer *l) {
while (!so_lexer_eof(l) && so_lexer_peek(l) >= '0' && so_lexer_peek(l) <= '9')
so_lexer_advance(l);
char *lexeme = strndup(
&l->source[l->start],
l->current - l->start);
return (so_token){.lexeme = lexeme, .type = SO_TT_INTEGER};
}
so_token so_lexer_bareword(so_lexer *l) {
while (!so_lexer_eof(l) && !isspace(so_lexer_peek(l)))
so_lexer_advance(l);
char *lexeme = strndup(
&l->source[l->start],
l->current - l->start);
so_token_type type = SO_TT_BARE;
if (strcmp(lexeme, "use") == 0)
type = SO_TT_USE;
if (strcmp(lexeme, "call") == 0)
type = SO_TT_CALL;
return (so_token){.lexeme = lexeme, .type = type};
}