start adding error message support
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct ArgonObject ArgonObject;
|
||||
|
||||
struct node_GC {
|
||||
uint64_t hash;
|
||||
void *key;
|
||||
|
||||
@@ -1,53 +1,15 @@
|
||||
#ifndef OBJECT_H
|
||||
#define OBJECT_H
|
||||
#include "../internals/hashmap/hashmap.h"
|
||||
#include "../internals/dynamic_array_armem/darray_armem.h"
|
||||
#include <gmp.h>
|
||||
#include <stdbool.h>
|
||||
#include "../runtime.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
extern ArgonObject *BASE_CLASS;
|
||||
|
||||
struct string_struct {
|
||||
char *data;
|
||||
size_t length;
|
||||
};
|
||||
struct argon_function_struct {
|
||||
darray_armem bytecode;
|
||||
struct Stack stack;
|
||||
size_t number_of_parameters;
|
||||
char** parameters;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
TYPE_NULL,
|
||||
TYPE_BOOL,
|
||||
TYPE_NUMBER,
|
||||
TYPE_STRING,
|
||||
TYPE_FUNCTION,
|
||||
TYPE_NATIVE_FUNCTION,
|
||||
TYPE_OBJECT, // generic user object
|
||||
} ArgonType;
|
||||
|
||||
struct ArgonObject {
|
||||
ArgonType type;
|
||||
char* name;
|
||||
ArgonObject *self;
|
||||
ArgonObject *baseObject;
|
||||
struct hashmap_GC *fields; // dynamic fields/methods
|
||||
union {
|
||||
mpq_t as_number;
|
||||
bool as_bool;
|
||||
struct string_struct as_str;
|
||||
void *native_fn;
|
||||
struct argon_function_struct argon_fn;
|
||||
// others as needed
|
||||
} value;
|
||||
};
|
||||
typedef struct ArgonObject ArgonObject;
|
||||
void init_base_field();
|
||||
ArgonObject* init_child_argon_object(ArgonObject *cls);
|
||||
ArgonObject* init_argon_class(char*name);
|
||||
ArgonObject *init_child_argon_object(ArgonObject *cls);
|
||||
ArgonObject *init_argon_class(char *name);
|
||||
|
||||
void add_field(ArgonObject*target, char* name, ArgonObject *object);
|
||||
void add_field(ArgonObject *target, char *name, ArgonObject *object);
|
||||
#endif // OBJECT_H
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "runtime.h"
|
||||
#include "../err.h"
|
||||
#include "../translator/translator.h"
|
||||
#include "objects/functions/functions.h"
|
||||
#include "objects/null/null.h"
|
||||
@@ -62,20 +63,8 @@ void load_const(Translated *translated, RuntimeState *state) {
|
||||
state->registers[to_register] = object;
|
||||
}
|
||||
|
||||
const ArErr no_err = (ArErr){false};
|
||||
|
||||
ArErr create_err(char *path, int64_t line, char *type, char *message) {
|
||||
return (ArErr){
|
||||
false,
|
||||
path,
|
||||
line,
|
||||
type,
|
||||
message
|
||||
};
|
||||
}
|
||||
|
||||
ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack) {
|
||||
ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack) {
|
||||
OperationType opcode = pop_byte(translated, state);
|
||||
switch (opcode) {
|
||||
case OP_LOAD_NULL:
|
||||
@@ -88,24 +77,26 @@ ArErr create_err(char *path, int64_t line, char *type, char *message) {
|
||||
load_argon_function(translated, state, stack);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "bytecode invalid\n");
|
||||
exit(EXIT_FAILURE);
|
||||
return create_err(0, 0, 0, NULL, "Runtime Error", "Invalid Opcode %#x", opcode);
|
||||
}
|
||||
return no_err;
|
||||
}
|
||||
|
||||
RuntimeState init_runtime_state(Translated translated) {
|
||||
RuntimeState state = {
|
||||
return (RuntimeState){
|
||||
checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0};
|
||||
return state;
|
||||
}
|
||||
|
||||
ArgonObject *runtime(Translated translated, RuntimeState state) {
|
||||
struct Stack stack = {NULL, NULL};
|
||||
Stack create_scope(Stack *prev) { return (Stack){NULL, prev}; }
|
||||
|
||||
ArErr runtime(Translated translated, RuntimeState state, Stack stack) {
|
||||
state.head = 0;
|
||||
while (state.head < translated.bytecode.size) {
|
||||
run_instruction(&translated, &state, stack);
|
||||
ArErr err = run_instruction(&translated, &state, stack);
|
||||
if (err.exists) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
free(state.registers);
|
||||
return stack.scope;
|
||||
return no_err;
|
||||
}
|
||||
@@ -2,25 +2,13 @@
|
||||
#define RUNTIME_H
|
||||
#include "../translator/translator.h"
|
||||
#include "internals/hashmap/hashmap.h"
|
||||
#include "../returnTypes.h"
|
||||
|
||||
typedef struct {
|
||||
ArgonObject **registers;
|
||||
size_t head;
|
||||
} RuntimeState;
|
||||
|
||||
typedef struct {
|
||||
bool exists;
|
||||
char *path;
|
||||
int64_t line;
|
||||
char *type;
|
||||
char *message;
|
||||
} ArErr;
|
||||
|
||||
typedef struct Stack {
|
||||
ArgonObject *scope;
|
||||
struct Stack *prev;
|
||||
} Stack;
|
||||
|
||||
void init_types();
|
||||
|
||||
uint64_t pop_bytecode(Translated *translated, RuntimeState *state);
|
||||
@@ -30,6 +18,8 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
|
||||
RuntimeState init_runtime_state(Translated translated);
|
||||
|
||||
ArgonObject *runtime(Translated translated, RuntimeState state);
|
||||
Stack create_scope(Stack *prev);
|
||||
|
||||
ArErr runtime(Translated translated, RuntimeState state, Stack stack);
|
||||
|
||||
#endif // RUNTIME_H
|
||||
Reference in New Issue
Block a user