start adding assignment (currently only identifier assignment works)

This commit is contained in:
William Bell
2025-08-29 01:41:53 +01:00
parent fff4f6bcb5
commit f598c215e7
9 changed files with 134 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2025 William Bell
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "assignment.h"
ArErr runtime_assignment(Translated *translated, RuntimeState *state,
struct Stack *stack) {
int64_t length = pop_bytecode(translated, state);
int64_t offset = pop_bytecode(translated, state);
int64_t prehash = pop_bytecode(translated, state);
int64_t from_register = pop_byte(translated, state);
uint64_t hash =
runtime_hash(arena_get(&translated->constants, offset), length, prehash);
ArgonObject *key =
new_string_object(arena_get(&translated->constants, offset), length);
for (Stack *current_stack = stack; current_stack;
current_stack = current_stack->prev) {
ArgonObject *exists = hashmap_lookup_GC(current_stack->scope, hash);
if (exists) {
hashmap_insert_GC(current_stack->scope, hash, key,
state->registers[from_register], 0);
}
}
hashmap_insert_GC(stack->scope, hash, key, state->registers[from_register],
0);
return no_err;
}

View File

@@ -0,0 +1,15 @@
/*
* SPDX-FileCopyrightText: 2025 William Bell
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#ifndef runtime_assignment_H
#define runtime_assignment_H
#include "../runtime.h"
#include "../objects/string/string.h"
ArErr runtime_assignment(Translated *translated, RuntimeState *state,
struct Stack *stack);
#endif // runtime_assignment_H

View File

@@ -12,6 +12,7 @@
#include "access/access.h"
#include "call/call.h"
#include "declaration/declaration.h"
#include "assignment/assignment.h"
#include "internals/hashmap/hashmap.h"
#include "objects/functions/functions.h"
#include "objects/literals/literals.h"
@@ -679,6 +680,8 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
return load_variable(translated, state, *stack);
case OP_DECLARE:
return runtime_declaration(translated, state, *stack);
case OP_ASSIGN:
return runtime_assignment(translated, state, *stack);
case OP_BOOL:;
ArErr err = no_err;
uint8_t to_register = pop_byte(translated, state);