Skip to content

Commit 92d6d75

Browse files
tidy up variable setting a bit
1 parent 753594a commit 92d6d75

File tree

2 files changed

+31
-49
lines changed

2 files changed

+31
-49
lines changed

src/basic/main.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,6 @@ void library_statement(struct basic_ctx *ctx) {
350350
accept_or_return(LIBRARY, ctx);
351351
const char *lib_file = str_expr(ctx);
352352

353-
dprintf("START REHASH ALLOCATOR: %p\n", ctx->allocator);
354-
355353
/* Validate the file exists and is not a directory */
356354
fs_directory_entry_t *file_info = fs_get_file_info(lib_file);
357355
accept_or_return(NEWLINE, ctx);
@@ -369,7 +367,7 @@ void library_statement(struct basic_ctx *ctx) {
369367
* (we need to look ahead and take note of this because the entire program
370368
* pointer structure will be rebuilt and any old ctx->ptr will be invalid!)
371369
*/
372-
uint64_t next_line = tokenizer_num(ctx, NUMBER);
370+
int64_t next_line = tokenizer_num(ctx, NUMBER);
373371

374372
/* Load the library file from VFS */
375373
size_t library_len = file_info->size;
@@ -440,7 +438,6 @@ void library_statement(struct basic_ctx *ctx) {
440438
*/
441439
char error[MAX_STRINGLEN];
442440
hashmap_free(ctx->lines);
443-
basic_debug("PRE REHASH ALLOCATOR: %p\n", ctx->allocator);
444441
ctx->lines = hashmap_new_with_allocator(varmap_malloc, varmap_realloc, varmap_free, sizeof(ub_line_ref), 0, 5923530135432, 458397058, line_hash, line_compare, NULL, ctx->allocator);
445442
if (!basic_hash_lines(ctx, (char **) &error)) {
446443
tokenizer_error_print(ctx, error);
@@ -457,23 +454,15 @@ void library_statement(struct basic_ctx *ctx) {
457454
if (def) {
458455
basic_debug("Calling initialisation constructor '%s' on line %ld with return to line %ld\n", file_info->filename, def->line, next_line);
459456
if (ctx->call_stack_ptr < MAX_CALL_STACK_DEPTH) {
460-
basic_debug("1\n");
461457
ctx->call_stack[ctx->call_stack_ptr] = next_line;
462-
basic_debug("2\n");
463458
ctx->fn_type_stack[ctx->call_stack_ptr] = ctx->fn_type; // save caller’s type
464-
basic_debug("3\n");
465459
ctx->call_stack_ptr++;
466-
basic_debug("4\n");
467460
init_local_heap(ctx);
468-
basic_debug("5\n");
469461
ctx->fn_type = RT_NONE;
470-
basic_debug("6 -> %lu\n", def->line);
471462
jump_linenum(def->line, ctx);
472-
basic_debug("7 -> %p\n", ctx->allocator);
473463
} else {
474464
tokenizer_error_printf(ctx, "Call stack exhausted when calling constructor PROC for library '%s'", lib_file);
475465
}
476-
basic_debug("done\n");
477466
return;
478467
} else {
479468
dprintf("Library '%s' has no initialisation constructor, continue at line %ld\n", file_info->filename, next_line);

src/basic/variable.c

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ bool valid_int_var(const char* name)
188188
return valid_suffix_var(name, '\0');
189189
}
190190

191+
static void update_string(struct basic_ctx* ctx, ub_var_string* str, size_t len, bool propagate_global, const char* varname, const char* value) {
192+
str->name_length = len;
193+
str->global = propagate_global;
194+
str->varname = buddy_strdup(ctx->allocator, varname);
195+
str->value = buddy_strdup(ctx->allocator, value);
196+
}
197+
191198
void basic_set_string_variable(const char* var, const char* value, struct basic_ctx* ctx, bool local, bool propagate_global) {
192199
struct hashmap* locals = ctx->local_string_variables[ctx->call_stack_ptr];
193200
struct hashmap* globals = ctx->str_variables;
@@ -203,26 +210,17 @@ void basic_set_string_variable(const char* var, const char* value, struct basic_
203210
if (local && locals && (found = hashmap_get(locals, &(ub_var_string) { .varname = var }))) {
204211
buddy_free(ctx->allocator, found->varname);
205212
buddy_free(ctx->allocator, found->value);
206-
found->name_length = len;
207-
found->global = propagate_global;
208-
found->varname = buddy_strdup(ctx->allocator, var);
209-
found->value = buddy_strdup(ctx->allocator, value);
213+
update_string(ctx, found, len, propagate_global, var, value);
210214
oom = !hashmap_set(locals, found) && hashmap_oom(locals);
211215
} else if ((found = hashmap_get(globals, &(ub_var_string) { .varname = var }))) {
212216
buddy_free(ctx->allocator, found->varname);
213217
buddy_free(ctx->allocator, found->value);
214-
found->name_length = len;
215-
found->global = propagate_global;
216-
found->varname = buddy_strdup(ctx->allocator, var);
217-
found->value = buddy_strdup(ctx->allocator, value);
218+
update_string(ctx, found, len, propagate_global, var, value);
218219
oom = !hashmap_set(globals, found) && hashmap_oom(globals);
219220
} else {
220221
struct hashmap* target = local && locals ? locals : globals;
221222
ub_var_string new;
222-
new.name_length = len;
223-
new.global = propagate_global;
224-
new.varname = buddy_strdup(ctx->allocator, var);
225-
new.value = buddy_strdup(ctx->allocator, value);
223+
update_string(ctx, &new, len, propagate_global, var, value);
226224
oom = !hashmap_set(target, &new) && hashmap_oom(target);
227225
}
228226
if (oom) {
@@ -231,6 +229,13 @@ void basic_set_string_variable(const char* var, const char* value, struct basic_
231229
}
232230
}
233231

232+
static void update_int(struct basic_ctx* ctx, ub_var_int* integer, size_t len, bool propagate_global, const char* varname, int64_t value) {
233+
integer->name_length = len;
234+
integer->global = propagate_global;
235+
integer->varname = buddy_strdup(ctx->allocator, varname);
236+
integer->value = value;
237+
}
238+
234239
void basic_set_int_variable(const char* var, int64_t value, struct basic_ctx* ctx, bool local, bool propagate_global) {
235240
struct hashmap* locals = ctx->local_int_variables[ctx->call_stack_ptr];
236241
struct hashmap* globals = ctx->int_variables;
@@ -246,29 +251,19 @@ void basic_set_int_variable(const char* var, int64_t value, struct basic_ctx* ct
246251
if (local && locals && (found = hashmap_get(locals, &(ub_var_int) { .varname = var }))) {
247252
basic_debug("local set '%s' %p\n", var, ctx->allocator);
248253
buddy_free(ctx->allocator, found->varname);
249-
found->name_length = len;
250-
found->global = propagate_global;
251-
found->varname = buddy_strdup(ctx->allocator, var);
252-
found->value = value;
254+
update_int(ctx, found, len, propagate_global, var, value);
253255
oom = !hashmap_set(locals, found) && hashmap_oom(locals);
254256
} else if ((found = hashmap_get(globals, &(ub_var_int) { .varname = var }))) {
255257
basic_debug("global set '%s' %p %p\n", var, found->varname, ctx->allocator);
256258
buddy_free(ctx->allocator, found->varname);
257-
found->name_length = len;
258-
found->global = propagate_global;
259-
found->varname = buddy_strdup(ctx->allocator, var);
260-
basic_debug("strdup'd\n");
261-
found->value = value;
259+
update_int(ctx, found, len, propagate_global, var, value);
262260
oom = !hashmap_set(globals, found) && hashmap_oom(globals);
263261
basic_debug("set, oom=%d\n", oom);
264262
} else {
265263
struct hashmap* target = local && locals ? locals : globals;
266264
basic_debug("%s create '%s' %p\n", target == globals ? "global" : "local", var, ctx->allocator);
267265
ub_var_int new;
268-
new.name_length = len;
269-
new.global = propagate_global;
270-
new.varname = buddy_strdup(ctx->allocator, var);
271-
new.value = value;
266+
update_int(ctx, &new, len, propagate_global, var, value);
272267
oom = !hashmap_set(target, &new) && hashmap_oom(target);
273268
}
274269
if (oom) {
@@ -277,6 +272,13 @@ void basic_set_int_variable(const char* var, int64_t value, struct basic_ctx* ct
277272
}
278273
}
279274

275+
static void update_double(struct basic_ctx* ctx, ub_var_double* dbl, size_t len, bool propagate_global, const char* varname, double value) {
276+
dbl->name_length = len;
277+
dbl->global = propagate_global;
278+
dbl->varname = buddy_strdup(ctx->allocator, varname);
279+
dbl->value = value;
280+
}
281+
280282
void basic_set_double_variable(const char* var, double value, struct basic_ctx* ctx, bool local, bool propagate_global)
281283
{
282284
struct hashmap* locals = ctx->local_double_variables[ctx->call_stack_ptr];
@@ -292,25 +294,16 @@ void basic_set_double_variable(const char* var, double value, struct basic_ctx*
292294
bool oom = false;
293295
if (local && locals && (found = hashmap_get(locals, &(ub_var_double) { .varname = var }))) {
294296
buddy_free(ctx->allocator, found->varname);
295-
found->name_length = len;
296-
found->global = propagate_global;
297-
found->varname = buddy_strdup(ctx->allocator, var);
298-
found->value = value;
297+
update_double(ctx, found, len, propagate_global, var, value);
299298
oom = !hashmap_set(locals, found) && hashmap_oom(locals);
300299
} else if ((found = hashmap_get(globals, &(ub_var_double) { .varname = var }))) {
301300
buddy_free(ctx->allocator, found->varname);
302-
found->name_length = len;
303-
found->global = propagate_global;
304-
found->varname = buddy_strdup(ctx->allocator, var);
305-
found->value = value;
301+
update_double(ctx, found, len, propagate_global, var, value);
306302
oom = !hashmap_set(globals, found) && hashmap_oom(globals);
307303
} else {
308304
struct hashmap* target = local && locals ? locals : globals;
309305
ub_var_double new;
310-
new.name_length = len;
311-
new.global = propagate_global;
312-
new.varname = buddy_strdup(ctx->allocator, var);
313-
new.value = value;
306+
update_double(ctx, &new, len, propagate_global, var, value);
314307
oom = !hashmap_set(target, &new) && hashmap_oom(target);
315308
}
316309
if (oom) {

0 commit comments

Comments
 (0)