Merge branch 'main' of https://github.com/Open-Argon/Chloride
This commit is contained in:
@@ -378,7 +378,7 @@ Execution execute(char *path, Stack *stack) {
|
|||||||
RuntimeState state = init_runtime_state(translated, path);
|
RuntimeState state = init_runtime_state(translated, path);
|
||||||
Stack *main_scope = create_scope(stack);
|
Stack *main_scope = create_scope(stack);
|
||||||
ArErr err = runtime(translated, state, main_scope);
|
ArErr err = runtime(translated, state, main_scope);
|
||||||
free(state.registers);
|
free_runtime_state(state);
|
||||||
end = clock();
|
end = clock();
|
||||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
total_time_spent += time_spent;
|
total_time_spent += time_spent;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ void load_argon_function(Translated *translated, RuntimeState *state,
|
|||||||
object->type = TYPE_FUNCTION;
|
object->type = TYPE_FUNCTION;
|
||||||
uint64_t offset = pop_bytecode(translated, state);
|
uint64_t offset = pop_bytecode(translated, state);
|
||||||
uint64_t length = pop_bytecode(translated, state);
|
uint64_t length = pop_bytecode(translated, state);
|
||||||
add_field(object, "__class__", new_string_object(arena_get(&translated->constants, offset), length));
|
add_field(object, "__name__", new_string_object(arena_get(&translated->constants, offset), length));
|
||||||
object->value.argon_fn.number_of_parameters = pop_bytecode(translated, state);
|
object->value.argon_fn.number_of_parameters = pop_bytecode(translated, state);
|
||||||
object->value.argon_fn.parameters =
|
object->value.argon_fn.parameters =
|
||||||
ar_alloc(object->value.argon_fn.number_of_parameters * sizeof(struct string_struct));
|
ar_alloc(object->value.argon_fn.number_of_parameters * sizeof(struct string_struct));
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ ArgonObject *new_string_object(char*data, size_t length) {
|
|||||||
ArgonObject * object = new_object();
|
ArgonObject * object = new_object();
|
||||||
add_field(object, "__class__", ARGON_STRING_TYPE);
|
add_field(object, "__class__", ARGON_STRING_TYPE);
|
||||||
object->type = TYPE_STRING;
|
object->type = TYPE_STRING;
|
||||||
object->value.as_str.data = data;
|
object->value.as_str.data = ar_alloc_atomic(length);
|
||||||
|
memcpy(object->value.as_str.data, data, length);
|
||||||
object->value.as_str.length = length;
|
object->value.as_str.length = length;
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "../hash_data/hash_data.h"
|
#include "../hash_data/hash_data.h"
|
||||||
#include "../translator/translator.h"
|
#include "../translator/translator.h"
|
||||||
#include "declaration/declaration.h"
|
#include "declaration/declaration.h"
|
||||||
|
#include "internals/hashmap/hashmap.h"
|
||||||
#include "objects/functions/functions.h"
|
#include "objects/functions/functions.h"
|
||||||
#include "objects/literals/literals.h"
|
#include "objects/literals/literals.h"
|
||||||
#include "objects/object.h"
|
#include "objects/object.h"
|
||||||
@@ -39,22 +40,28 @@ void bootstrap_types() {
|
|||||||
ARGON_BOOL_TYPE = new_object();
|
ARGON_BOOL_TYPE = new_object();
|
||||||
add_field(ARGON_BOOL_TYPE, "__base__", BASE_CLASS);
|
add_field(ARGON_BOOL_TYPE, "__base__", BASE_CLASS);
|
||||||
ARGON_TRUE = new_object();
|
ARGON_TRUE = new_object();
|
||||||
add_field(ARGON_NULL, "__class__", ARGON_BOOL_TYPE);
|
add_field(ARGON_TRUE, "__class__", ARGON_BOOL_TYPE);
|
||||||
ARGON_FALSE = new_object();
|
ARGON_FALSE = new_object();
|
||||||
add_field(ARGON_NULL, "__class__", ARGON_BOOL_TYPE);
|
add_field(ARGON_FALSE, "__class__", ARGON_BOOL_TYPE);
|
||||||
|
|
||||||
ARGON_STRING_TYPE = new_object();
|
ARGON_STRING_TYPE = new_object();
|
||||||
add_field(ARGON_STRING_TYPE, "__base__", BASE_CLASS);
|
add_field(ARGON_STRING_TYPE, "__base__", BASE_CLASS);
|
||||||
|
|
||||||
|
add_field(ARGON_STRING_TYPE, "__name__",
|
||||||
add_field(BASE_CLASS, "__name__", new_string_object_null_terminated("object"));
|
new_string_object_null_terminated("string"));
|
||||||
add_field(ARGON_TYPE_TYPE, "__name__", new_string_object_null_terminated("type"));
|
add_field(BASE_CLASS, "__name__",
|
||||||
add_field(ARGON_NULL_TYPE, "__name__", new_string_object_null_terminated("NullType"));
|
new_string_object_null_terminated("object"));
|
||||||
add_field(ARGON_BOOL_TYPE, "__name__", new_string_object_null_terminated("boolean"));
|
add_field(ARGON_TYPE_TYPE, "__name__",
|
||||||
|
new_string_object_null_terminated("type"));
|
||||||
|
add_field(ARGON_NULL_TYPE, "__name__",
|
||||||
|
new_string_object_null_terminated("null_type"));
|
||||||
|
add_field(ARGON_BOOL_TYPE, "__name__",
|
||||||
|
new_string_object_null_terminated("boolean"));
|
||||||
|
|
||||||
ARGON_FUNCTION_TYPE = new_object();
|
ARGON_FUNCTION_TYPE = new_object();
|
||||||
add_field(ARGON_FUNCTION_TYPE, "__base__", BASE_CLASS);
|
add_field(ARGON_FUNCTION_TYPE, "__base__", BASE_CLASS);
|
||||||
add_field(ARGON_FUNCTION_TYPE, "__name__", new_string_object_null_terminated("function"));
|
add_field(ARGON_FUNCTION_TYPE, "__name__",
|
||||||
|
new_string_object_null_terminated("function"));
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare_by_order(const void *a, const void *b) {
|
int compare_by_order(const void *a, const void *b) {
|
||||||
@@ -192,6 +199,40 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
|||||||
// free(array);
|
// free(array);
|
||||||
*stack = (*stack)->prev;
|
*stack = (*stack)->prev;
|
||||||
break;
|
break;
|
||||||
|
case OP_INIT_ARGS:;
|
||||||
|
size_t size = pop_bytecode(translated, state) * sizeof(ArgonObject *);
|
||||||
|
if (state->call_args) {
|
||||||
|
state->call_args = realloc(state->call_args, size);
|
||||||
|
} else {
|
||||||
|
state->call_args = checked_malloc(size);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case OP_INSERT_ARG:;
|
||||||
|
to_register = pop_byte(translated, state);
|
||||||
|
state->call_args[pop_bytecode(translated, state)] =
|
||||||
|
state->registers[to_register];
|
||||||
|
break;
|
||||||
|
case OP_RESET_ARGS:;
|
||||||
|
free(state->call_args);
|
||||||
|
state->call_args = NULL;
|
||||||
|
break;
|
||||||
|
case OP_CALL:
|
||||||
|
from_register = pop_byte(translated, state);
|
||||||
|
ArgonObject *object = state->registers[from_register];
|
||||||
|
char *field = "__class__";
|
||||||
|
uint64_t hash = siphash64_bytes(field, strlen(field), siphash_key);
|
||||||
|
ArgonObject *class = hashmap_lookup_GC(object->dict, hash);
|
||||||
|
field = "__name__";
|
||||||
|
hash = siphash64_bytes(field, strlen(field), siphash_key);
|
||||||
|
ArgonObject *class_name = hashmap_lookup_GC(class->dict, hash);
|
||||||
|
hash = siphash64_bytes(field, strlen(field), siphash_key);
|
||||||
|
ArgonObject *object_name = hashmap_lookup_GC(object->dict, hash);
|
||||||
|
if (object_name) {
|
||||||
|
printf("call <%.*s %.*s at %p>\n", (int)class_name->value.as_str.length, class_name->value.as_str.data,(int)object_name->value.as_str.length, object_name->value.as_str.data, object);
|
||||||
|
} else {
|
||||||
|
printf("call <%.*s object at %p>\n", (int)class_name->value.as_str.length, class_name->value.as_str.data, object);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return create_err(0, 0, 0, NULL, "Runtime Error", "Invalid Opcode %#x",
|
return create_err(0, 0, 0, NULL, "Runtime Error", "Invalid Opcode %#x",
|
||||||
opcode);
|
opcode);
|
||||||
@@ -200,9 +241,21 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
RuntimeState init_runtime_state(Translated translated, char *path) {
|
RuntimeState init_runtime_state(Translated translated, char *path) {
|
||||||
return (RuntimeState){
|
RuntimeState runtime = (RuntimeState){
|
||||||
checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0, path,
|
checked_malloc(translated.registerCount * sizeof(ArgonObject *)),
|
||||||
ARGON_NULL, NULL, NULL};
|
0,
|
||||||
|
path,
|
||||||
|
ARGON_NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL};
|
||||||
|
return runtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_runtime_state(RuntimeState runtime_state) {
|
||||||
|
free(runtime_state.registers);
|
||||||
|
if (runtime_state.call_args)
|
||||||
|
free(runtime_state.call_args);
|
||||||
}
|
}
|
||||||
|
|
||||||
Stack *create_scope(Stack *prev) {
|
Stack *create_scope(Stack *prev) {
|
||||||
@@ -215,19 +268,22 @@ Stack *create_scope(Stack *prev) {
|
|||||||
ArErr runtime(Translated translated, RuntimeState state, Stack *stack) {
|
ArErr runtime(Translated translated, RuntimeState state, Stack *stack) {
|
||||||
state.head = 0;
|
state.head = 0;
|
||||||
StackFrame *currentStackFrame = checked_malloc(sizeof(StackFrame));
|
StackFrame *currentStackFrame = checked_malloc(sizeof(StackFrame));
|
||||||
*currentStackFrame = (StackFrame){translated, state, stack, NULL};
|
*currentStackFrame = (StackFrame){translated, state, stack, NULL, no_err};
|
||||||
state.currentStackFramePointer = ¤tStackFrame;
|
state.currentStackFramePointer = ¤tStackFrame;
|
||||||
ArErr err = no_err;
|
ArErr err = no_err;
|
||||||
while (currentStackFrame) {
|
while (currentStackFrame) {
|
||||||
while (currentStackFrame->state.head <
|
while (currentStackFrame->state.head <
|
||||||
currentStackFrame->translated.bytecode.size &&
|
currentStackFrame->translated.bytecode.size &&
|
||||||
!err.exists) {
|
!currentStackFrame->err.exists) {
|
||||||
err =
|
currentStackFrame->err =
|
||||||
run_instruction(¤tStackFrame->translated,
|
run_instruction(¤tStackFrame->translated,
|
||||||
¤tStackFrame->state, ¤tStackFrame->stack);
|
¤tStackFrame->state, ¤tStackFrame->stack);
|
||||||
}
|
}
|
||||||
StackFrame *tempStackFrame = currentStackFrame;
|
StackFrame *tempStackFrame = currentStackFrame;
|
||||||
|
err = currentStackFrame->err;
|
||||||
currentStackFrame = currentStackFrame->previousStackFrame;
|
currentStackFrame = currentStackFrame->previousStackFrame;
|
||||||
|
if (currentStackFrame)
|
||||||
|
free_runtime_state(tempStackFrame->state);
|
||||||
free(tempStackFrame);
|
free(tempStackFrame);
|
||||||
}
|
}
|
||||||
return err;
|
return err;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#define RUNTIME_H
|
#define RUNTIME_H
|
||||||
#include "../returnTypes.h"
|
#include "../returnTypes.h"
|
||||||
#include "../translator/translator.h"
|
#include "../translator/translator.h"
|
||||||
|
#include "internals/dynamic_array_armem/darray_armem.h"
|
||||||
#include "internals/hashmap/hashmap.h"
|
#include "internals/hashmap/hashmap.h"
|
||||||
|
|
||||||
typedef struct StackFrame StackFrame;
|
typedef struct StackFrame StackFrame;
|
||||||
@@ -23,6 +24,7 @@ typedef struct RuntimeState {
|
|||||||
ArgonObject *return_value;
|
ArgonObject *return_value;
|
||||||
StackFrame **currentStackFramePointer;
|
StackFrame **currentStackFramePointer;
|
||||||
error_result result;
|
error_result result;
|
||||||
|
ArgonObject** call_args;
|
||||||
} RuntimeState;
|
} RuntimeState;
|
||||||
|
|
||||||
typedef struct StackFrame {
|
typedef struct StackFrame {
|
||||||
@@ -30,6 +32,7 @@ typedef struct StackFrame {
|
|||||||
RuntimeState state;
|
RuntimeState state;
|
||||||
Stack *stack;
|
Stack *stack;
|
||||||
StackFrame *previousStackFrame;
|
StackFrame *previousStackFrame;
|
||||||
|
ArErr err;
|
||||||
} StackFrame;
|
} StackFrame;
|
||||||
|
|
||||||
void bootstrap_types();
|
void bootstrap_types();
|
||||||
@@ -47,6 +50,8 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
|||||||
|
|
||||||
RuntimeState init_runtime_state(Translated translated, char *path);
|
RuntimeState init_runtime_state(Translated translated, char *path);
|
||||||
|
|
||||||
|
void free_runtime_state(RuntimeState runtime_state);
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
@@ -88,3 +88,31 @@ creates a new stack
|
|||||||
## OP_POP_SCOPE
|
## OP_POP_SCOPE
|
||||||
|
|
||||||
pops the top scope off the current
|
pops the top scope off the current
|
||||||
|
|
||||||
|
## OP_INIT_ARGS
|
||||||
|
|
||||||
|
initialises the arguments buffer with a fixed number of objects on the current state
|
||||||
|
|
||||||
|
1. the number of objects for the arguments buffer
|
||||||
|
|
||||||
|
## OP_INSERT_ARG
|
||||||
|
|
||||||
|
1. the register to take the object from. (*)
|
||||||
|
1. index of the argument in the arguments buffer to write the object from the register into.
|
||||||
|
|
||||||
|
## OP_RESET_ARGS
|
||||||
|
|
||||||
|
resets the arguments buffer to NULL
|
||||||
|
|
||||||
|
## OP_CALL
|
||||||
|
|
||||||
|
call a function with args
|
||||||
|
|
||||||
|
1. the register containing the function to call. (*)
|
||||||
|
|
||||||
|
## OP_SWAP_REGISTERS
|
||||||
|
|
||||||
|
swap the contents in two registers
|
||||||
|
|
||||||
|
1. register a (*)
|
||||||
|
2. register b (*)
|
||||||
27
src/translator/call/call.c
Normal file
27
src/translator/call/call.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2025 William Bell
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
*/
|
||||||
|
#include "call.h"
|
||||||
|
|
||||||
|
size_t translate_parsed_call(Translated *translated, ParsedCall* call, ArErr *err) {
|
||||||
|
set_registers(translated, 1);
|
||||||
|
size_t first = push_instruction_byte(translated, OP_INIT_ARGS);
|
||||||
|
push_instruction_code(translated, call->args.size);
|
||||||
|
for (size_t i = 0; i < call->args.size; i++) {
|
||||||
|
translate_parsed(translated, darray_get(&call->args, i), err);
|
||||||
|
if (err->exists)
|
||||||
|
return first;
|
||||||
|
push_instruction_byte(translated, OP_INSERT_ARG);
|
||||||
|
push_instruction_byte(translated, 0);
|
||||||
|
push_instruction_code(translated, i);
|
||||||
|
}
|
||||||
|
translate_parsed(translated, call->to_call, err);
|
||||||
|
if (err->exists)
|
||||||
|
return first;
|
||||||
|
push_instruction_byte(translated, OP_CALL);
|
||||||
|
push_instruction_byte(translated, 0);
|
||||||
|
push_instruction_byte(translated, OP_RESET_ARGS);
|
||||||
|
return first;
|
||||||
|
}
|
||||||
15
src/translator/call/call.h
Normal file
15
src/translator/call/call.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2025 William Bell
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BYTECODE_CALL_H
|
||||||
|
#define BYTECODE_CALL_H
|
||||||
|
#include "../../parser/assignable/call/call.h"
|
||||||
|
#include "../translator.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
size_t translate_parsed_call(Translated *translated, ParsedCall* call, ArErr *err);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "../hash_data/hash_data.h"
|
#include "../hash_data/hash_data.h"
|
||||||
#include "../hashmap/hashmap.h"
|
#include "../hashmap/hashmap.h"
|
||||||
#include "declaration/declaration.h"
|
#include "declaration/declaration.h"
|
||||||
|
#include "call/call.h"
|
||||||
#include "dowrap/dowrap.h"
|
#include "dowrap/dowrap.h"
|
||||||
#include "function/function.h"
|
#include "function/function.h"
|
||||||
#include "identifier/identifier.h"
|
#include "identifier/identifier.h"
|
||||||
@@ -152,6 +153,8 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue, ArErr*
|
|||||||
return translate_parsed_dowrap(translated, (DArray *)parsedValue->data, err);
|
return translate_parsed_dowrap(translated, (DArray *)parsedValue->data, err);
|
||||||
case AST_RETURN:
|
case AST_RETURN:
|
||||||
return translate_parsed_return(translated, (ParsedReturn *)parsedValue->data, err);
|
return translate_parsed_return(translated, (ParsedReturn *)parsedValue->data, err);
|
||||||
|
case AST_CALL:
|
||||||
|
return translate_parsed_call(translated, (ParsedCall*)parsedValue->data, err);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER, OP_BOOL, OP_JUMP_IF_FALSE, OP_JUMP, OP_NEW_SCOPE, OP_POP_SCOPE } OperationType;
|
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER, OP_BOOL, OP_JUMP_IF_FALSE, OP_JUMP, OP_NEW_SCOPE, OP_POP_SCOPE, OP_INIT_ARGS, OP_INSERT_ARG, OP_RESET_ARGS, OP_CALL } OperationType;
|
||||||
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
|
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
1092514
testing.ar
1092514
testing.ar
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user