-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
199 lines (171 loc) · 5.68 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "lib/builtin.h"
#include "lib/error.h"
#include "lib/parsing.h"
#include "lib/bit_set.h"
#include "lib/format.h"
#include "lib/string.h"
#include <assert.h>
#include <stdarg.h>
const char *whitespace = " \t\n";
typedef enum {
Empty,
Float,
Operator,
} Token_Type;
typedef struct {
Token_Type type;
union {
String text;
f64 value;
};
Location loc;
bool is_valid;
} Token;
static Token_Type classify(Parser_State *parser) {
if (parser_is_empty(parser)) return Empty;
const char first = peek(parser);
if ('0' <= first && first <= '9') return Float;
if (first == '-' || first == '+' || first == '.') {
Parser_State tmp = *parser;
next(&tmp);
const char second = peek(&tmp);
if ('0' <= second && second <= '9') {
return Float;
}
}
return Operator;
}
#define CONSOLE_COLORS
#ifdef CONSOLE_COLORS
#define RESET "\x1b[0m"
#define COL_RED_F "\x1b[31m"
#define COL_GRN_F "\x1b[32m"
#define COL_BLU_F "\x1b[34m"
#else
#define RESET ""
#define COL_RED_F ""
#define COL_GRN_F ""
#define COL_BLU_F ""
#endif
static i64 fmt_token_va(Byte_Slice dest, va_list va, Fmt_Info info) {
info = (Fmt_Info){0};
Token tok = va_arg(va, Token);
char *const begin = dest.begin;
dest.begin += fmt_location(dest, tok.loc, info);
dest.begin += fmt_cstr(dest, ": ", info);
dest.begin += fmt_cstr(dest, "Token{ ", info);
if (!tok.is_valid) {
dest.begin += fmt_cstr(dest, "[" COL_RED_F "INVALID" RESET "] ", info);
}
switch (tok.type) {
case Float:
dest.begin += fmt_cstr(dest, "f64: " COL_GRN_F, info);
dest.begin += fmt_i64(dest, (i64)tok.value, info);
dest.begin += fmt_cstr(dest, RESET " ", info);
break;
case Operator:
dest.begin += fmt_cstr(dest, "op: " COL_BLU_F "`", info);
dest.begin += fmt_str(dest, tok.text, info);
dest.begin += fmt_cstr(dest, "`" RESET " ", info);
break;
case Empty:
dest.begin += fmt_cstr(dest, "nil ", info);
break;
}
dest.begin += fmt_cstr(dest, "}", info);
return dest.begin - begin;
}
static String parse_operator(Parser_State *parser, Bit_Set whitespace, Bit_Set specials) {
u64 begin = parsed_bytes(parser);
rune c = peek(parser);
parser->error = None;
// empty operator
if (c == '\0' || get_bit(whitespace, c)) {
parser->error = Invalid_Parse;
return slice(parser->source, begin, parsed_bytes(parser));
}
// consume a special rune
if (get_bit(specials, c)) {
next(parser);
return slice(parser->source, begin, parsed_bytes(parser));
}
next(parser);
for(; (c = peek(parser)) != '\0'; next(parser)) {
if (get_bit(whitespace, c) || get_bit(specials, c)) { break; }
}
return slice(parser->source, begin, parsed_bytes(parser));
}
static void ensure_total_parse(Parser_State *parser, Bit_Set whitespace, Bit_Set specials) {
if (parser->error == None) {
rune r = peek(parser);
if (!get_bit(whitespace, r) && !get_bit(specials, r)) {
parser->error = Invalid_Parse;
}
}
}
static Token next_token(Parser_State *parser) {
static u64 whitespace_data[256 / BITS];
static Bit_Set whitespace;
if (whitespace.data == NULL)
whitespace = bit_set_from_runes(whitespace_data, U"\n\t ");
static u64 specials_data[256 / BITS];
static Bit_Set specials;
if (specials.data == NULL)
specials = bit_set_from_runes(specials_data, U",.()");
while(get_bit(whitespace, peek(parser))) next(parser);
Location loc = parser->location;
switch(classify(parser)) {
case Empty: {
return (Token){ .type = Empty, .loc = loc, .is_valid = false };
} break;
case Float: {
f64 value = parse_f64(parser);
ensure_total_parse(parser, whitespace, specials);
if (parser->error == None) {
return (Token){Float, .value = value, .loc = loc, .is_valid = true};
} else {
report_state(parser, stderr);
for (char c; (c = peek(parser)) != '\0'; next(parser))
if (get_bit(whitespace, c) || get_bit(specials, c))
break;
String text = slice(parser->source, loc.byte, parsed_bytes(parser));
parser->error = None;
return (Token){Float, .text = text, loc, false};
}
} break;
case Operator: {
String tok_text = parse_operator(parser, whitespace, specials);
return (Token){Operator, .text = tok_text, loc, true};
} break;
default: assert(false);
}
}
static Parser_State parser_from_filename(const char *filename) {
if (!filename) {
println(string_from_cstring("[ERROR]: no filename was provided"));
return (Parser_State){0};
}
FILE *file = fopen(filename, "rb");
if (!file) {
format_println("[ERROR]: unable to open file `{cstr}`", filename);
return (Parser_State){0};
}
Parser_State result = {
.source = string_from_stream(file),
.location = {.fname = filename},
.error = None,
};
fclose(file);
return result;
}
i32 main(i32 argc, char **argv) {
register_format_directive((Fmt_Directive){"Token", fmt_token_va});
Parser_State parser = parser_from_filename(argv[1]);
for (; !parser_is_empty(&parser); ) {
Token tok = next_token(&parser);
if (tok.type == Empty) continue;
if (!tok.is_valid) format_println("{Token}", tok);
}
delete_str(&parser.source);
return 0;
}