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
|
#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "lex.h"
String collect_identifier(char* input, size_t* pos) {
String id_buf = make_String(256);
while (input[*pos] == '_' || isalnum(input[*pos])) {
push_to_String(&id_buf, input[(*pos)++]);
}
return id_buf;
}
String collect_single_quoted_section(char* input, size_t* pos) {
(*pos)++; // Consume quote mark
String chunk_str = make_String(256);
while (input[*pos] != '\'') {
if (input[*pos] == '\0') exit(42);
push_to_String(&chunk_str, input[(*pos)++]);
}
(*pos)++; // Consume closing quote
return chunk_str;
}
TokenVec lex_region(char* input, size_t* pos, char until);
void expect_char(char* input, size_t* pos, char expected) {
if (input[*pos] != expected) exit(42); // TODO: nicer handling
(*pos)++;
}
void lex_double_quoted_section(char* input, size_t* pos, ChunkVec* coll, RegionVec* reg_arena) {
(*pos)++;
String accum = make_String(256);
while (input[*pos] != '\"') {
if (input[*pos] == '\0') exit(42);
if (input[*pos] == '$') {
(*pos)++;
if (input[*pos] == '(') {
if (input[*pos] == '(') {
TokenVec inner_toks = lex_region(input, pos, ')');
expect_char(input, pos, ')');
expect_char(input, pos, ')');
push_to_RegionVec(reg_arena, (Region){.})
push_to_ChunkVec(coll, (Chunk){.mode = DOUBLE_Q, .kind = ARITHM, .as.arithm_expr_region = inner_toks});
}
} else {
String id_name = collect_identifier(input, pos);
if (id_name.count > 0) {
push_to_ChunkVec(coll, (Chunk){.mode = DOUBLE_Q, .kind = VAR_EXP, .as.param_string = inner(id_name)});
}
continue;
}
} else {
}
}
}
typedef struct {
Region root_reg;
RegionVec reg_arena;
} LexResult;
// TODO: maybe make an input+pos struct
TokenVec lex_region(char* input, size_t* pos, char until) {
TokenVec tokens = make_TokenVec(256);
RegionVec reg_arena;
while (input[*pos] != until) {
// Getting a token
if (input[*pos] == ' ') {
(*pos)++;
continue;
}
ChunkVec chunks = make_ChunkVec(256);
switch (input[*pos]) {
case '\'':
String qstring = get_single_quoted_chunk(input, pos);
push_to_ChunkVec(&chunks, (Chunk){.mode=SINGLE_Q, .kind = LITERAL, .as.lit_str = qstring.items});
break;
}
}
return tokens;
}
TokenVec lex(char* input) {
size_t pos = 0;
return lex_region(input, &pos, '\0');
}
|