-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
200 lines (179 loc) · 4.28 KB
/
utils.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
200
#include "shirp.h"
// offset given by `>> ` repl prompt
#define PROMPT_OFFSET 3
void *shirp_malloc(size_t size) {
void *ptr = malloc(size);
if (!ptr) {
fprintf(stderr, "shirp: allocation error");
exit(EXIT_FAILURE);
}
return ptr;
}
void *shirp_calloc(size_t n, size_t size) {
void *ptr = calloc(n, size);
if (!ptr) {
fprintf(stderr, "shirp: allocation error");
exit(EXIT_FAILURE);
}
return ptr;
}
void *shirp_realloc(void *ptr, size_t size) {
void *new_ptr = realloc(ptr, size);
if (!new_ptr) {
fprintf(stderr, "shirp: allocation error");
exit(EXIT_FAILURE);
}
return new_ptr;
}
// reports error with its position
void verror_at(char *loc, size_t len, char *fmt, va_list ap) {
char *head = loc;
char *tail = loc;
while (*(head - 1) && *(head - 1) != '\n')
head--;
while (*tail && *(tail + 1) && *(tail + 1) != '\n')
tail++;
int line_len = (int)(tail - head + 1);
int indent = (int)(loc - head);
fprintf(stderr, "\x1b[1m\x1b[31merror\x1b[0m: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
fprintf(stderr, " %.*s\n", line_len, head);
fprintf(stderr, " %*s\x1b[1m\x1b[31m^\x1b[0m", indent, "");
for (size_t i = 0; i < len - 1; i++)
fprintf(stderr, "\x1b[1m\x1b[31m~\x1b[0m");
fprintf(stderr, "\n");
}
const char *type_name(ObjType typ) {
switch (typ) {
case UNDEF_TY:
return "undefined";
case BOOL_TY:
return "boolean";
case INT_TY:
return "integer";
case FLOAT_TY:
return "float";
case CHAR_TY:
return "character";
case STRING_TY:
return "string";
case SYMBOL_TY:
return "symbol";
case LAMBDA_TY:
case BUILTIN_TY:
return "closure";
case CONS_TY:
return "pair";
}
return NULL;
}
void debug_printf(char *fmt, ...) {
#ifndef DEBUG
return;
#endif
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void debug_log(char *fmt, ...) {
#ifndef DEBUG
return;
#endif
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
/*
Tokenizer Utility Functions
*/
bool match_str(char *str, char *keyword, size_t len) {
return memcmp(str, keyword, len) == 0;
}
bool match_tok(Token *tok, char *keyword) {
return tok->len == strlen(keyword) && match_str(tok->loc, keyword, tok->len);
}
bool match_anyof_tok(Token *tok, char *keywords[]) {
for (int i = 0; keywords[i]; i++)
if (match_tok(tok, keywords[i]))
return true;
return false;
}
void dump_tokens(Token *tokens) {
#ifndef DEBUG
return;
#endif
if (!tokens) {
printf("NULL\n");
return;
}
Token *tok = tokens;
do {
if (tok && tok->kind == TOKEN_IMMEDIATE) {
if (tok->typ == INT_TY)
fprintf(stderr, "`%.*s`[I'%" PRId64 "]-> ", (int)tok->len, tok->loc,
tok->val.int_val);
else if (tok->typ == FLOAT_TY)
fprintf(stderr, "`%.*s`[F'%lf]-> ", (int)tok->len, tok->loc,
tok->val.float_val);
else if (tok->typ == BOOL_TY)
fprintf(stderr, "`%.*s`[V'%s]-> ", (int)tok->len, tok->loc,
tok->val.bool_val ? "#t" : "#f");
} else
fprintf(stderr, "`%.*s` (%d)-> ", (int)tok->len, tok->loc, tok->kind);
tok = tok->next;
} while (tok);
fprintf(stderr, "NULL\n");
}
#define TOMBSTONE ((void *)-1)
void dump_hashtable(HashTable *ht) {
#ifndef DEBUG
return;
#endif
for (size_t i = 0; i < ht->capacity; i++) {
Entry *entry = ht->buckets[i];
if (entry && entry != TOMBSTONE &&
(*(Obj **)entry->val)->typ != BUILTIN_TY) {
fprintf(stderr, "`%.*s`: ", (int)entry->keylen, entry->key);
println_obj(*(Obj **)entry->val);
}
}
fprintf(stderr, "-----------------\n");
}
void dump_env(Frame *env) {
#ifndef DEBUG
return;
#endif
fprintf(stderr, "---Environment---\n");
while (env) {
dump_hashtable(env->table);
env = env->outer;
}
}
void free_tokens(Token *tok) {
if (!tok)
return;
free_tokens(tok->next);
free(tok);
}
void free_ast(ASTNode *ast) {
if (!ast)
return;
free_ast(ast->caller);
free_ast(ast->args);
free_ast(ast->listarg);
free_ast(ast->next);
free(ast);
}
void free_obj(Obj *obj) {
if (!obj)
return;
if (obj->typ == LAMBDA_TY)
pop_frame(obj->exclusive.saved_env);
else if (obj->typ == STRING_TY || obj->typ == SYMBOL_TY)
free(obj->exclusive.str_val);
free(obj);
}