set up so reusing scope when in a loop

This commit is contained in:
William Bell
2025-09-01 23:09:09 +01:00
parent 322716af0c
commit fd5b237dfe
10 changed files with 25 additions and 6 deletions

View File

@@ -1,3 +1,3 @@
let i = 10000000
let i = 1e7
while (i) do
i=i-1

View File

@@ -21,9 +21,16 @@ struct hashmap_GC *createHashmap_GC() {
t->order = 1;
t->list = (struct node_GC **)ar_alloc(sizeof(struct node_GC *) * size);
memset(t->list, 0, sizeof(struct node_GC *) * size);
t->count = 0;
return t;
}
void clear_hashmap_GC(struct hashmap_GC *t) {
t->order = 1;
t->count = 0;
memset(t->list, 0, sizeof(struct node_GC *) * t->size);
}
void resize_hashmap_GC(struct hashmap_GC *t) {
int old_size = t->size;
int new_size = old_size * 2;

View File

@@ -25,6 +25,8 @@ struct hashmap_GC {
struct hashmap_GC *createHashmap_GC();
void clear_hashmap_GC(struct hashmap_GC *t);
int hashCode_GC(struct hashmap_GC *t, uint64_t hash);
int hashmap_remove_GC(struct hashmap_GC *t, uint64_t hash);

View File

@@ -20,7 +20,7 @@ const char *built_in_field_names[BUILT_IN_FIELDS_COUNT] = {
"__string__", "__subtract__", "__multiply__", "__division__",
"__new__", "__init__", "__boolean__", "__get_attr__",
"__binding__", "__function__", "address", "__call__",
"__number__", "log"};
"__number__", "log", "length"};
uint64_t built_in_field_hashes[BUILT_IN_FIELDS_COUNT];

View File

@@ -30,6 +30,7 @@ typedef enum {
__call__,
__number__,
field_log,
field_length,
BUILT_IN_FIELDS_COUNT
} built_in_fields;

View File

@@ -17,7 +17,7 @@ ArgonObject *new_string_object(char *data, size_t length, uint64_t prehash,
uint64_t hash) {
ArgonObject *object = new_object();
add_builtin_field(object, __class__, ARGON_STRING_TYPE);
add_builtin_field(object, length,
add_builtin_field(object, field_length,
new_number_object_from_num_and_den(length, 1));
object->type = TYPE_STRING;
object->value.as_str.data = ar_alloc_atomic(length);

View File

@@ -683,6 +683,7 @@ static inline void run_instructions(Translated *translated, RuntimeState *state,
[OP_JUMP_IF_FALSE] = &&DO_JUMP_IF_FALSE,
[OP_JUMP] = &&DO_JUMP,
[OP_NEW_SCOPE] = &&DO_NEW_SCOPE,
[OP_EMPTY_SCOPE] = &&DO_EMPTY_SCOPE,
[OP_POP_SCOPE] = &&DO_POP_SCOPE,
[OP_INIT_CALL] = &&DO_INIT_CALL,
[OP_INSERT_ARG] = &&DO_INSERT_ARG,
@@ -745,6 +746,9 @@ DO_JUMP:
DO_NEW_SCOPE:
*stack = create_scope(*stack);
goto START;
DO_EMPTY_SCOPE:
clear_hashmap_GC((*stack)->scope);
goto START;
DO_POP_SCOPE:
*stack = (*stack)->prev;
goto START;

View File

@@ -93,6 +93,10 @@ jumps unconditionally to an index.
creates a new stack
## OP_EMPTY_SCOPE
empties the current scope so the same memory can be reused.
## OP_POP_SCOPE
pops the top scope off the current

View File

@@ -24,6 +24,7 @@ typedef enum {
OP_JUMP_IF_FALSE,
OP_JUMP,
OP_NEW_SCOPE,
OP_EMPTY_SCOPE,
OP_POP_SCOPE,
OP_INIT_CALL,
OP_INSERT_ARG,

View File

@@ -18,7 +18,7 @@ size_t translate_parsed_while(Translated *translated, ParsedWhile *parsedWhile,
translated->return_jumps = &return_jumps;
}
size_t first = push_instruction_byte(translated, OP_NEW_SCOPE);
translate_parsed(translated, parsedWhile->condition, err);
size_t start_of_loop = translate_parsed(translated, parsedWhile->condition, err);
if (err->exists) {
return 0;
}
@@ -28,9 +28,9 @@ size_t translate_parsed_while(Translated *translated, ParsedWhile *parsedWhile,
push_instruction_byte(translated, 0);
uint64_t jump_index = push_instruction_code(translated, 0);
translate_parsed(translated, parsedWhile->content, err);
push_instruction_byte(translated, OP_POP_SCOPE);
push_instruction_byte(translated, OP_EMPTY_SCOPE);
push_instruction_byte(translated, OP_JUMP);
push_instruction_code(translated, first);
push_instruction_code(translated, start_of_loop);