diff options
author | Bad Diode <bd@badd10de.dev> | 2022-02-01 18:43:43 +0100 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2022-02-01 18:43:43 +0100 |
commit | 5af70ecc0c8eafa7d6d31e16576cb4cf0713f5cc (patch) | |
tree | bb5ad458f19f4369c6c1c11c4fe4b137d358fc5a | |
parent | ee1a5de91c875fb66724dc21c02333bfebe2a812 (diff) | |
download | bdl-5af70ecc0c8eafa7d6d31e16576cb4cf0713f5cc.tar.gz bdl-5af70ecc0c8eafa7d6d31e16576cb4cf0713f5cc.zip |
Ensure EOF is handled properly
-rw-r--r-- | src/lexer.c | 13 | ||||
-rw-r--r-- | tests/constants/numbers.bdl | 91 |
2 files changed, 42 insertions, 62 deletions
diff --git a/src/lexer.c b/src/lexer.c index 56b670b..80ef3ad 100644 --- a/src/lexer.c +++ b/src/lexer.c | |||
@@ -262,6 +262,10 @@ tokenize(const StringView *sv) { | |||
262 | case '.': { token.type = TOKEN_DOT; } break; | 262 | case '.': { token.type = TOKEN_DOT; } break; |
263 | case '@': { token.type = TOKEN_AT; } break; | 263 | case '@': { token.type = TOKEN_AT; } break; |
264 | default: { | 264 | default: { |
265 | if (c == EOF || c == '\0') { | ||
266 | token.type = TOKEN_EOF; | ||
267 | break; | ||
268 | } | ||
265 | size_t n = 1; | 269 | size_t n = 1; |
266 | if (c == '-' && !is_delimiter(scan_peek(&scanner))) { | 270 | if (c == '-' && !is_delimiter(scan_peek(&scanner))) { |
267 | n += scan_number_token(&scanner); | 271 | n += scan_number_token(&scanner); |
@@ -304,14 +308,5 @@ tokenize(const StringView *sv) { | |||
304 | } | 308 | } |
305 | array_push(tokens, token); | 309 | array_push(tokens, token); |
306 | } | 310 | } |
307 | |||
308 | // Push EOF token. | ||
309 | Token token = (Token){ | ||
310 | .type = TOKEN_EOF, | ||
311 | .line = scanner.line_number, | ||
312 | .col = 1, | ||
313 | }; | ||
314 | array_push(tokens, token); | ||
315 | |||
316 | return tokens; | 311 | return tokens; |
317 | } | 312 | } |
diff --git a/tests/constants/numbers.bdl b/tests/constants/numbers.bdl index 0412af8..136b5fb 100644 --- a/tests/constants/numbers.bdl +++ b/tests/constants/numbers.bdl | |||
@@ -1,56 +1,41 @@ | |||
1 | ; Signed integers. | 1 | ; Signed integers. |
2 | 3 | 2 | (print 1:s8) |
3 | ; -1 | 3 | (print 52:s8) |
4 | 0.1 | 4 | (print -1:s8) |
5 | 43 | 5 | (print 0:s8) |
6 | 0 | 6 | (print 1:s16) |
7 | -123 | 7 | (print 52:s16) |
8 | 0x123 | 8 | (print -1:s16) |
9 | 0xabCd | 9 | (print 0:s16) |
10 | 0xABCDEF | 10 | (print 1:s32) |
11 | -0x12 | 11 | (print 52:s32) |
12 | ; a | 12 | (print -1:s32) |
13 | ; fun | 13 | (print 0:s32) |
14 | ; a.a | 14 | (print 1:s64) |
15 | ; a.1 ; should err | 15 | (print 52:s64) |
16 | ; 1.a ; should err | 16 | (print -1:s64) |
17 | ; (print 1:s8) | 17 | (print 0:s64) |
18 | ; (print 52:s8) | ||
19 | ; (print -1:s8) | ||
20 | ; (print 0:s8) | ||
21 | ; (print 1:s16) | ||
22 | ; (print 52:s16) | ||
23 | ; (print -1:s16) | ||
24 | ; (print 0:s16) | ||
25 | ; (print 1:s32) | ||
26 | ; (print 52:s32) | ||
27 | ; (print -1:s32) | ||
28 | ; (print 0:s32) | ||
29 | ; (print 1:s64) | ||
30 | ; (print 52:s64) | ||
31 | ; (print -1:s64) | ||
32 | ; (print 0:s64) | ||
33 | 18 | ||
34 | ; ; Unsigned integers. | 19 | ; Unsigned integers. |
35 | ; (print 1:u8) | 20 | (print 1:u8) |
36 | ; (print 52:u8) | 21 | (print 52:u8) |
37 | ; (print -1:u8) | 22 | (print -1:u8) |
38 | ; (print 0:u8) | 23 | (print 0:u8) |
39 | ; (print 1:u16) | 24 | (print 1:u16) |
40 | ; (print 52:u16) | 25 | (print 52:u16) |
41 | ; (print -1:u16) | 26 | (print -1:u16) |
42 | ; (print 0:u16) | 27 | (print 0:u16) |
43 | ; (print 1:u32) | 28 | (print 1:u32) |
44 | ; (print 52:u32) | 29 | (print 52:u32) |
45 | ; (print -1:u32) | 30 | (print -1:u32) |
46 | ; (print 0:u32) | 31 | (print 0:u32) |
47 | ; (print 1:u64) | 32 | (print 1:u64) |
48 | ; (print 52:u64) | 33 | (print 52:u64) |
49 | ; (print -1:u64) | 34 | (print -1:u64) |
50 | ; (print 0:u64) | 35 | (print 0:u64) |
51 | 36 | ||
52 | ; ; Type inference. | 37 | ; Type inference. |
53 | ; (print 1) | 38 | (print 1) |
54 | ; (print 52) | 39 | (print 52) |
55 | ; (print -1) | 40 | (print -1) |
56 | ; (print 0) | 41 | (print 0) |