add if statement and declaration
This commit is contained in:
18
src/runtime/declaration/declaration.c
Normal file
18
src/runtime/declaration/declaration.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "declaration.h"
|
||||
|
||||
ArErr runtime_declaration(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);
|
||||
int64_t source_location_index = pop_bytecode(translated, state);
|
||||
uint64_t hash = runtime_hash(arena_get(&translated->constants, offset), length, prehash);
|
||||
ArgonObject * exists = hashmap_lookup_GC(stack->scope, hash);
|
||||
if (exists) {
|
||||
SourceLocation *source_location = darray_get(&translated->source_locations, source_location_index);
|
||||
return create_err(source_location->line, source_location->column, source_location->length, state->path, "Runtime Error", "Identifier '%.*s' has already been declared in the current scope", length, arena_get(&translated->constants, offset));
|
||||
}
|
||||
hashmap_insert_GC(stack->scope, hash, arena_get(&translated->constants, offset), state->registers[from_register], 0);
|
||||
return no_err;
|
||||
}
|
||||
8
src/runtime/declaration/declaration.h
Normal file
8
src/runtime/declaration/declaration.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef runtime_declaration_H
|
||||
#define runtime_declaration_H
|
||||
#include "../runtime.h"
|
||||
|
||||
ArErr runtime_declaration(Translated *translated, RuntimeState *state,
|
||||
struct Stack *stack);
|
||||
|
||||
#endif // runtime_declaration_H
|
||||
@@ -12,7 +12,7 @@ void init_function_type() {
|
||||
}
|
||||
|
||||
void load_argon_function(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack) {
|
||||
struct Stack *stack) {
|
||||
ArgonObject *object = init_child_argon_object(ARGON_FUNCTION_TYPE);
|
||||
object->type = TYPE_FUNCTION;
|
||||
uint64_t offset = pop_bytecode(translated, state);
|
||||
@@ -35,6 +35,6 @@ void load_argon_function(Translated *translated, RuntimeState *state,
|
||||
darray_armem_init(&object->value.argon_fn.bytecode, sizeof(uint64_t));
|
||||
darray_armem_resize(&object->value.argon_fn.bytecode, length/object->value.argon_fn.bytecode.element_size);
|
||||
memcpy(object->value.argon_fn.bytecode.data, arena_get(&translated->constants, offset), length);
|
||||
object->value.argon_fn.stack = stack;
|
||||
object->value.argon_fn.stack = *stack;
|
||||
state->registers[0]=object;
|
||||
}
|
||||
@@ -4,6 +4,6 @@
|
||||
|
||||
void init_function_type();
|
||||
|
||||
ArgonObject *load_argon_function(Translated *translated, RuntimeState *state, struct Stack stack);
|
||||
ArgonObject *load_argon_function(Translated *translated, RuntimeState *state, struct Stack *stack);
|
||||
|
||||
#endif // FUNCTION_H
|
||||
25
src/runtime/objects/literals/literals.c
Normal file
25
src/runtime/objects/literals/literals.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "../object.h"
|
||||
#include <string.h>
|
||||
#include "literals.h"
|
||||
|
||||
ArgonObject *ARGON_NULL_TYPE = NULL;
|
||||
ArgonObject *ARGON_NULL = NULL;
|
||||
|
||||
ArgonObject *ARGON_BOOL_TYPE = NULL;
|
||||
ArgonObject *ARGON_TRUE = NULL;
|
||||
ArgonObject *ARGON_FALSE = NULL;
|
||||
|
||||
void init_literals() {
|
||||
ARGON_NULL_TYPE = init_argon_class("NULL_TYPE");
|
||||
|
||||
ARGON_NULL = init_child_argon_object(ARGON_NULL_TYPE);
|
||||
ARGON_NULL->type=TYPE_NULL;
|
||||
|
||||
ARGON_BOOL_TYPE = init_argon_class("Bool");
|
||||
|
||||
ARGON_FALSE = init_child_argon_object(ARGON_BOOL_TYPE);
|
||||
ARGON_FALSE->type=TYPE_BOOL;
|
||||
|
||||
ARGON_TRUE = init_child_argon_object(ARGON_BOOL_TYPE);
|
||||
ARGON_TRUE->type=TYPE_BOOL;
|
||||
}
|
||||
12
src/runtime/objects/literals/literals.h
Normal file
12
src/runtime/objects/literals/literals.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef RUNTIME_LITERALS_H
|
||||
#define RUNTIME_LITERALS_H
|
||||
#include "../object.h"
|
||||
|
||||
extern ArgonObject *ARGON_NULL;
|
||||
extern ArgonObject *ARGON_FALSE;
|
||||
extern ArgonObject *ARGON_TRUE;
|
||||
|
||||
void init_literals();
|
||||
|
||||
|
||||
#endif // RUNTIME_LITERALS_H
|
||||
@@ -1,14 +0,0 @@
|
||||
#include "../../internals/hashmap/hashmap.h"
|
||||
#include "../object.h"
|
||||
#include <string.h>
|
||||
#include "null.h"
|
||||
|
||||
ArgonObject *ARGON_NULL_TYPE = NULL;
|
||||
ArgonObject *ARGON_NULL = NULL;
|
||||
|
||||
void init_null() {
|
||||
ARGON_NULL_TYPE = init_argon_class("NULL_TYPE");
|
||||
|
||||
ARGON_NULL = init_child_argon_object(ARGON_NULL_TYPE);
|
||||
ARGON_NULL->type=TYPE_NULL;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#ifndef NULL_H
|
||||
#define NULL_H
|
||||
#include "../object.h"
|
||||
|
||||
extern ArgonObject *ARGON_NULL;
|
||||
|
||||
void init_null();
|
||||
|
||||
|
||||
#endif // NULL_H
|
||||
@@ -2,8 +2,9 @@
|
||||
#include "../err.h"
|
||||
#include "../hash_data/hash_data.h"
|
||||
#include "../translator/translator.h"
|
||||
#include "declaration/declaration.h"
|
||||
#include "objects/functions/functions.h"
|
||||
#include "objects/null/null.h"
|
||||
#include "objects/literals/literals.h"
|
||||
#include "objects/object.h"
|
||||
#include "objects/string/string.h"
|
||||
#include "objects/type/type.h"
|
||||
@@ -29,7 +30,7 @@ void init_types() {
|
||||
|
||||
init_type();
|
||||
init_function_type();
|
||||
init_null();
|
||||
init_literals();
|
||||
init_string_type();
|
||||
|
||||
init_base_field();
|
||||
@@ -64,33 +65,50 @@ void load_const(Translated *translated, RuntimeState *state) {
|
||||
state->registers[to_register] = object;
|
||||
}
|
||||
|
||||
struct hashmap *runtime_hash_table = NULL;
|
||||
|
||||
uint64_t runtime_hash(const void *data, size_t len, uint64_t prehash) {
|
||||
if (!runtime_hash_table) {
|
||||
runtime_hash_table = createHashmap();
|
||||
} else {
|
||||
void *result = hashmap_lookup(runtime_hash_table, prehash);
|
||||
if (result) {
|
||||
return (uint64_t)result;
|
||||
}
|
||||
}
|
||||
uint64_t hash = siphash64_bytes(data, len, siphash_key);
|
||||
hashmap_insert(runtime_hash_table, prehash, 0, (void *)hash, 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
ArErr load_variable(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack) {
|
||||
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 source_location_index = pop_bytecode(translated, state);
|
||||
char *name = checked_malloc(length);
|
||||
memcpy(name, arena_get(&translated->constants, offset), length);
|
||||
uint64_t hash = siphash64_bytes(name, length, siphash_key);
|
||||
struct Stack *current_stack = &stack;
|
||||
uint64_t hash =
|
||||
runtime_hash(arena_get(&translated->constants, offset), length, prehash);
|
||||
struct Stack *current_stack = stack;
|
||||
while (current_stack) {
|
||||
ArgonObject *result = hashmap_lookup_GC(current_stack->scope, hash);
|
||||
if (result) {
|
||||
state->registers[0] = result;
|
||||
free(name);
|
||||
return no_err;
|
||||
}
|
||||
current_stack = current_stack->prev;
|
||||
}
|
||||
SourceLocation *source_location = darray_get(&translated->source_locations, source_location_index);
|
||||
ArErr err = create_err(source_location->line, source_location->column, length, state->path, "Name Error", "name '%.*s' is not defined",
|
||||
(int)length, name);
|
||||
free(name);
|
||||
SourceLocation *source_location =
|
||||
darray_get(&translated->source_locations, source_location_index);
|
||||
ArErr err = create_err(source_location->line, source_location->column,
|
||||
source_location->length, state->path, "Name Error",
|
||||
"name '%.*s' is not defined", (int)length,
|
||||
arena_get(&translated->constants, offset));
|
||||
return err;
|
||||
}
|
||||
|
||||
ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack) {
|
||||
struct Stack **stack) {
|
||||
OperationType opcode = pop_byte(translated, state);
|
||||
switch (opcode) {
|
||||
case OP_LOAD_NULL:
|
||||
@@ -100,10 +118,32 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
load_const(translated, state);
|
||||
break;
|
||||
case OP_LOAD_FUNCTION:
|
||||
load_argon_function(translated, state, stack);
|
||||
load_argon_function(translated, state, *stack);
|
||||
break;
|
||||
case OP_IDENTIFIER:
|
||||
return load_variable(translated, state, stack);
|
||||
return load_variable(translated, state, *stack);
|
||||
case OP_DECLARE:
|
||||
return runtime_declaration(translated, state, *stack);
|
||||
case OP_BOOL:;
|
||||
uint8_t to_register = pop_byte(translated, state);
|
||||
state->registers[to_register] = ARGON_TRUE;
|
||||
break;
|
||||
case OP_JUMP_IF_FALSE:;
|
||||
uint8_t from_register = pop_byte(translated, state);
|
||||
uint64_t to = pop_bytecode(translated, state);
|
||||
if (state->registers[from_register] == ARGON_FALSE) {
|
||||
state->head = to;
|
||||
}
|
||||
break;
|
||||
case OP_JUMP:
|
||||
state->head = pop_bytecode(translated, state);
|
||||
break;
|
||||
case OP_NEW_SCOPE:
|
||||
*stack = create_scope(*stack);
|
||||
break;
|
||||
case OP_POP_SCOPE:
|
||||
*stack = (*stack)->prev;
|
||||
break;
|
||||
default:
|
||||
return create_err(0, 0, 0, NULL, "Runtime Error", "Invalid Opcode %#x",
|
||||
opcode);
|
||||
@@ -113,19 +153,21 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
|
||||
RuntimeState init_runtime_state(Translated translated, char *path) {
|
||||
return (RuntimeState){
|
||||
checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0,
|
||||
path};
|
||||
checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0, path,
|
||||
ARGON_NULL};
|
||||
}
|
||||
|
||||
Stack create_scope(Stack *prev) {
|
||||
struct hashmap_GC *scope = createHashmap_GC();
|
||||
return (Stack){scope, prev};
|
||||
Stack *create_scope(Stack *prev) {
|
||||
Stack *stack = ar_alloc(sizeof(Stack));
|
||||
stack->scope = createHashmap_GC();
|
||||
stack->prev = prev;
|
||||
return stack;
|
||||
}
|
||||
|
||||
ArErr runtime(Translated translated, RuntimeState state, Stack stack) {
|
||||
ArErr runtime(Translated translated, RuntimeState state, Stack *stack) {
|
||||
state.head = 0;
|
||||
while (state.head < translated.bytecode.size) {
|
||||
ArErr err = run_instruction(&translated, &state, stack);
|
||||
ArErr err = run_instruction(&translated, &state, &stack);
|
||||
if (err.exists) {
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -8,19 +8,26 @@ typedef struct {
|
||||
ArgonObject **registers;
|
||||
size_t head;
|
||||
char*path;
|
||||
ArgonObject * return_value;
|
||||
} RuntimeState;
|
||||
|
||||
void init_types();
|
||||
|
||||
extern struct hashmap * runtime_hash_table;
|
||||
|
||||
uint64_t runtime_hash(const void *data, size_t len, uint64_t prehash);
|
||||
|
||||
uint8_t pop_byte(Translated *translated, RuntimeState *state);
|
||||
|
||||
uint64_t pop_bytecode(Translated *translated, RuntimeState *state);
|
||||
|
||||
ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack);
|
||||
struct Stack **stack);
|
||||
|
||||
RuntimeState init_runtime_state(Translated translated, char *path);
|
||||
|
||||
Stack create_scope(Stack *prev);
|
||||
Stack *create_scope(Stack *prev);
|
||||
|
||||
ArErr runtime(Translated translated, RuntimeState state, Stack stack);
|
||||
ArErr runtime(Translated translated, RuntimeState state, Stack *stack);
|
||||
|
||||
#endif // RUNTIME_H
|
||||
Reference in New Issue
Block a user