cleanups and more test cases

This commit is contained in:
cozis
2022-08-12 05:30:41 +02:00
parent f630830b3c
commit 18e936f0ad
145 changed files with 1445 additions and 234 deletions
+189
View File
@@ -0,0 +1,189 @@
/* +--------------------------------------------------------------------------+
** | _ _ _ |
** | | \ | | (_) |
** | | \| | ___ _ __ _ |
** | | . ` |/ _ \| |/ _` | |
** | | |\ | (_) | | (_| | |
** | |_| \_|\___/| |\__,_| |
** | _/ | |
** | |__/ |
** +--------------------------------------------------------------------------+
** | Copyright (c) 2022 Francesco Cozzuto <francesco.cozzuto@gmail.com> |
** +--------------------------------------------------------------------------+
** | This file is part of The Noja Interpreter. |
** | |
** | The Noja Interpreter is free software: you can redistribute it and/or |
** | modify it under the terms of the GNU General Public License as published |
** | by the Free Software Foundation, either version 3 of the License, or (at |
** | your option) any later version. |
** | |
** | The Noja Interpreter is distributed in the hope that it will be useful, |
** | but WITHOUT ANY WARRANTY; without even the implied warranty of |
** | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
** | Public License for more details. |
** | |
** | You should have received a copy of the GNU General Public License along |
** | with The Noja Interpreter. If not, see <http://www.gnu.org/licenses/>. |
** +--------------------------------------------------------------------------+
*/
#include <assert.h>
#include <stdlib.h>
#include "../utils/defs.h"
#include "../objects/objects.h"
#include "runtime.h"
typedef struct {
Object base;
Runtime *runtime;
Executable *exe;
int index, argc;
Object *closure;
} FunctionObject;
static _Bool free_(Object *self, Error *error)
{
(void) error;
FunctionObject *func = (FunctionObject*) self;
Executable_Free(func->exe);
return 1;
}
static void walk(Object *self, void (*callback)(Object **referer, void *userp), void *userp)
{
FunctionObject *func = (FunctionObject*) self;
callback(&func->closure, userp);
}
static int call(Object *self, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Heap *heap, Error *error)
{
assert(self != NULL && heap != NULL && error != NULL);
FunctionObject *func = (FunctionObject*) self;
assert(func->exe != NULL);
assert(func->argc >= 0);
assert(func->index >= 0);
// Make sure the right amount of arguments is provided.
Object **argv2;
int expected_argc = func->argc;
if(expected_argc < (int) argc)
{
// Nothing to be done. By using
// the right argc the additional
// arguments are ignored implicitly.
argv2 = argv;
}
else if(expected_argc > (int) argc)
{
// Some arguments are missing.
argv2 = malloc(sizeof(Object*) * expected_argc);
if(argv2 == NULL)
{
Error_Report(error, 1, "No memory");
return -1;
}
// Copy the provided arguments.
for(int i = 0; i < (int) argc; i += 1)
argv2[i] = argv[i];
// Set the unspecified arguments to none.
for(int i = argc; i < expected_argc; i += 1)
{
argv2[i] = Object_NewNone(heap, error);
if(argv2[i] == NULL)
return -1;
}
}
else
// The right amount of arguments was provided.
argv2 = argv;
int retc = run(func->runtime, error, func->exe, func->index, func->closure, argv2, expected_argc, rets, maxretc);
// NOTE: Every object reference is invalidated from here.
if(argv2 != argv)
free(argv2);
return retc;
}
static TypeObject t_func = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "function",
.size = sizeof (FunctionObject),
.call = call,
.walk = walk,
.free = free_,
};
/* Symbol: Object_FromNojaFunction
*
* Creates an object from a noja executable structure.
*
* Args:
* - runtime: The reference to an instanciated Runtime.
*
* - exe: A noja executable.
*
* - index: The index of the first bytecode instruction
* of the noja function within the executable.
*
* - argc: The number of arguments the function expects.
* It must be positive (unlike [Object_FromNativeFunction],
* where -1 means variadic).
*
* - closure: An object containing variables that will be
* accessible from the noja function other than
* the ones that will be defined inside it.
*
* - heap: The heap that will be used to allocate the object.
* It can't be NULL.
*
* - error: Output parameter where error information is stored.
* It can't be NULL.
*
* Returns:
* The newly created object. If an error occurred, NULL is returned
* and information about the error is stored in the [error] argument.
*/
Object *Object_FromNojaFunction(Runtime *runtime, Executable *exe, int index, int argc, Object *closure, Heap *heap, Error *error)
{
assert(runtime != NULL);
assert(exe != NULL);
assert(index >= 0);
assert(argc >= 0);
assert(heap != NULL);
assert(error != NULL);
FunctionObject *func = (FunctionObject*) Heap_Malloc(heap, &t_func, error);
if(func == NULL)
return NULL;
Executable *exe_copy = Executable_Copy(exe);
if(exe_copy == NULL)
{
Error_Report(error, 1, "Failed to copy executable");
return NULL;
}
func->runtime = runtime;
func->exe = exe_copy;
func->index = index;
func->argc = argc;
func->closure = closure;
return (Object*) func;
}
+170
View File
@@ -0,0 +1,170 @@
/* +--------------------------------------------------------------------------+
** | _ _ _ |
** | | \ | | (_) |
** | | \| | ___ _ __ _ |
** | | . ` |/ _ \| |/ _` | |
** | | |\ | (_) | | (_| | |
** | |_| \_|\___/| |\__,_| |
** | _/ | |
** | |__/ |
** +--------------------------------------------------------------------------+
** | Copyright (c) 2022 Francesco Cozzuto <francesco.cozzuto@gmail.com> |
** +--------------------------------------------------------------------------+
** | This file is part of The Noja Interpreter. |
** | |
** | The Noja Interpreter is free software: you can redistribute it and/or |
** | modify it under the terms of the GNU General Public License as published |
** | by the Free Software Foundation, either version 3 of the License, or (at |
** | your option) any later version. |
** | |
** | The Noja Interpreter is distributed in the hope that it will be useful, |
** | but WITHOUT ANY WARRANTY; without even the implied warranty of |
** | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
** | Public License for more details. |
** | |
** | You should have received a copy of the GNU General Public License along |
** | with The Noja Interpreter. If not, see <http://www.gnu.org/licenses/>. |
** +--------------------------------------------------------------------------+
*/
/*
* WHAT IS THIS FILE?
* This file implements an object that makes it possible
* to call native functions from within noja code.
*
*/
#include <assert.h>
#include <stdlib.h>
#include "../utils/defs.h"
#include "../objects/objects.h"
#include "runtime.h"
typedef struct {
Object base;
Runtime *runtime;
int (*callback)(Runtime *runtime, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Error *error);
int argc;
} NativeFunctionObject;
static int call(Object *self, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Heap *heap, Error *error)
{
assert(self != NULL);
assert(heap != NULL);
assert(error != NULL);
NativeFunctionObject *func = (NativeFunctionObject*) self;
// If the function isn't variadic, make sure
// the right amount of arguments is provided.
Object **argv2;
int argc2;
int expected_argc = func->argc;
if(expected_argc < 0 || expected_argc == (int) argc)
{
// The function is variadic or the right
// amount of arguments was provided.
argv2 = argv;
argc2 = argc;
}
else if(expected_argc < (int) argc)
{
// Nothing to be done. By using
// the right argc the additional
// arguments are ignored implicitly.
argv2 = argv;
argc2 = expected_argc;
}
else if(expected_argc > (int) argc)
{
// Some arguments are missing.
argv2 = malloc(sizeof(Object*) * expected_argc);
argc2 = expected_argc;
if(argv2 == NULL)
{
Error_Report(error, 1, "No memory");
return -1;
}
// Copy the provided arguments.
for(int i = 0; i < (int) argc; i += 1)
argv2[i] = argv[i];
// Set the unspecified arguments to none.
for(int i = argc; i < expected_argc; i += 1)
{
argv2[i] = Object_NewNone(heap, error);
if(argv2[i] == NULL)
{
free(argv2);
return -1;
}
}
}
else UNREACHABLE;
assert(func->callback != NULL);
int retc = func->callback(func->runtime, argv2, argc2, rets, maxretc, error);
// NOTE: Since the callback may have executed some bytecode, a GC
// cycle may have been triggered, therefore we must assume
// every object reference that was locally saved is invalidated
// from here (the returned object is good tho).
if(argv2 != argv)
free(argv2);
return retc;
}
static TypeObject t_nfunc = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "native function",
.size = sizeof (NativeFunctionObject),
.call = call,
};
/* Symbol: Object_FromNativeFunction
*
* Creates an object from a function pointer.
*
* Args:
* - runtime: The reference to an instanciated Runtime. This must be
* provided so that the callback can also access it.
*
* - callback: The native function to be executed when this object
* is called.
*
* - argc: The number of arguments the function expects. If -1 is
* provided, then the function is considered to be variadic.
*
* - heap: The heap that will be used to allocate the object.
* It can't be NULL.
*
* - error: Output parameter where error information is stored.
* It can't be NULL.
*
* Returns:
* The newly created object. If an error occurred, NULL is returned
* and information about the error is stored in the [error] argument.
*/
Object *Object_FromNativeFunction(Runtime *runtime, int (*callback)(Runtime*, Object**, unsigned int, Object**, unsigned int, Error*), int argc, Heap *heap, Error *error)
{
assert(callback != NULL);
NativeFunctionObject *func = (NativeFunctionObject*) Heap_Malloc(heap, &t_nfunc, error);
if(func == NULL)
return NULL;
func->runtime = runtime;
func->callback = callback;
func->argc = argc;
return (Object*) func;
}
+141
View File
@@ -0,0 +1,141 @@
/* +--------------------------------------------------------------------------+
** | _ _ _ |
** | | \ | | (_) |
** | | \| | ___ _ __ _ |
** | | . ` |/ _ \| |/ _` | |
** | | |\ | (_) | | (_| | |
** | |_| \_|\___/| |\__,_| |
** | _/ | |
** | |__/ |
** +--------------------------------------------------------------------------+
** | Copyright (c) 2022 Francesco Cozzuto <francesco.cozzuto@gmail.com> |
** +--------------------------------------------------------------------------+
** | This file is part of The Noja Interpreter. |
** | |
** | The Noja Interpreter is free software: you can redistribute it and/or |
** | modify it under the terms of the GNU General Public License as published |
** | by the Free Software Foundation, either version 3 of the License, or (at |
** | your option) any later version. |
** | |
** | The Noja Interpreter is distributed in the hope that it will be useful, |
** | but WITHOUT ANY WARRANTY; without even the implied warranty of |
** | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
** | Public License for more details. |
** | |
** | You should have received a copy of the GNU General Public License along |
** | with The Noja Interpreter. If not, see <http://www.gnu.org/licenses/>. |
** +--------------------------------------------------------------------------+
** | WHAT IS THIS FILE? |
** | This file implements the "static map" object. The static map object |
** | behaves like a read-only "map". (Note that "implementing an object" means|
** | a very specific thing in this interpreter. If you didn't know, check the |
** | src/objects folder.) |
** | |
** | THE STATIC MAP OBJECT |
** | The statis map is a read-only collection of objects. You can see it as |
** | an interface for static arrays. You can define an array of |
** | `StaticMapSlot`s and then wrap it in this object. When the map is |
** | accessed, a lookup is performed into the array. Something to note is that|
** | the array is converted to noja objects lazily when they are accessed, |
** | which makes the start-up times lower than a general purpose map. |
** +--------------------------------------------------------------------------+
** | NOTES: |
** | - Only strings can be keys. There is no intrinsic reason why |
** | it should be like that, it's just simpler. |
** +--------------------------------------------------------------------------+
*/
#include <string.h>
#include <assert.h>
#include "../runtime/runtime.h"
#include "../utils/defs.h"
#include "../objects/objects.h"
typedef struct {
Object base;
Runtime *runt;
const StaticMapSlot *slots;
} StaticMapObject;
static Object *select(Object *self, Object *key, Heap *heap, Error *err);
static Object *copy(Object *self, Heap *heap, Error *err);
static int hash(Object *self);
static TypeObject t_staticmap = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "static map",
.size = sizeof (StaticMapObject),
.copy = copy,
.hash = hash,
.select = select,
};
static Object *copy(Object *self, Heap *heap, Error *err)
{
(void) heap;
(void) err;
return self;
}
static int hash(Object *self)
{
(void) self;
return 0;
}
Object *Object_NewStaticMap(const StaticMapSlot *slots, Runtime *runt, Error *error)
{
Heap *heap = Runtime_GetHeap(runt);
// Make the thing.
StaticMapObject *obj = (StaticMapObject*) Heap_Malloc(heap, &t_staticmap, error);
{
if(obj == 0)
return 0;
obj->runt = runt;
obj->slots = slots;
}
return (Object*) obj;
}
static Object *select(Object *self, Object *key, Heap *heap, Error *error)
{
assert(self != NULL);
assert(self->type == &t_staticmap);
assert(key != NULL);
assert(heap != NULL);
assert(error != NULL);
StaticMapObject *map = (StaticMapObject*) self;
if(!Object_IsString(key))
return NULL;
const char *name = Object_ToString(key, NULL, heap, error);
if(map->slots == NULL)
return NULL;
for(int i = 0; map->slots[i].name != NULL; i += 1)
if(!strcmp(name, map->slots[i].name))
{
StaticMapSlot slot = map->slots[i];
Object *obj;
switch(slot.kind)
{
case SM_BOOL: return Object_FromBool(slot.as_bool, heap, error);
case SM_INT: return Object_FromInt(slot.as_int, heap, error);
case SM_FLOAT: return Object_FromFloat(slot.as_float, heap, error);
case SM_FUNCT: return Object_FromNativeFunction(map->runt, slot.as_funct, slot.argc, heap, error);
case SM_STRING: return Object_FromString(slot.as_string, slot.length, heap, error);
case SM_SMAP: return Object_NewStaticMap(slot.as_smap, map->runt, error);
case SM_NONE: return Object_NewNone(heap, error);
case SM_TYPE: return (Object*) slot.as_type;
default: assert(0); break;
}
return obj;
}
return NULL;
}
File diff suppressed because it is too large Load Diff
+100
View File
@@ -0,0 +1,100 @@
/* +--------------------------------------------------------------------------+
** | _ _ _ |
** | | \ | | (_) |
** | | \| | ___ _ __ _ |
** | | . ` |/ _ \| |/ _` | |
** | | |\ | (_) | | (_| | |
** | |_| \_|\___/| |\__,_| |
** | _/ | |
** | |__/ |
** +--------------------------------------------------------------------------+
** | Copyright (c) 2022 Francesco Cozzuto <francesco.cozzuto@gmail.com> |
** +--------------------------------------------------------------------------+
** | This file is part of The Noja Interpreter. |
** | |
** | The Noja Interpreter is free software: you can redistribute it and/or |
** | modify it under the terms of the GNU General Public License as published |
** | by the Free Software Foundation, either version 3 of the License, or (at |
** | your option) any later version. |
** | |
** | The Noja Interpreter is distributed in the hope that it will be useful, |
** | but WITHOUT ANY WARRANTY; without even the implied warranty of |
** | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
** | Public License for more details. |
** | |
** | You should have received a copy of the GNU General Public License along |
** | with The Noja Interpreter. If not, see <http://www.gnu.org/licenses/>. |
** +--------------------------------------------------------------------------+
*/
#ifndef RUNTIME_H
#define RUNTIME_H
#include <stdio.h> // meh.. just for the definition of FILE.
#include "../utils/error.h"
#include "../utils/stack.h"
#include "../objects/objects.h"
#include "../common/executable.h"
typedef struct xRuntime Runtime;
typedef struct xSnapshot Snapshot;
Runtime* Runtime_New(int stack_size, int heap_size, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*));
Runtime* Runtime_New2(int stack_size, Heap *heap, _Bool free_heap, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*));
void Runtime_Free(Runtime *runtime);
_Bool Runtime_Pop(Runtime *runtime, Error *error, unsigned int n);
_Bool Runtime_Push(Runtime *runtime, Error *error, Object *obj);
Heap* Runtime_GetHeap(Runtime *runtime);
Stack* Runtime_GetStack(Runtime *runtime);
Object* Runtime_GetBuiltins(Runtime *runtime);
void Runtime_SetBuiltins(Runtime *runtime, Object *builtins);
int Runtime_GetCurrentIndex(Runtime *runtime);
Executable *Runtime_GetCurrentExecutable(Runtime *runtime);
Snapshot *Snapshot_New(Runtime *runtime);
void Snapshot_Free(Snapshot *snapshot);
void Snapshot_Print(Snapshot *snapshot, FILE *fp);
int run(Runtime *runtime, Error *error, Executable *exe, int index, Object *closure, Object **argv, int argc, Object **rets, int maxretc);
typedef enum {
SM_END,
SM_BOOL,
SM_INT,
SM_FLOAT,
SM_FUNCT,
SM_STRING,
SM_SMAP,
SM_NONE,
SM_TYPE,
} StaticMapSlotKind;
typedef struct StaticMapSlot StaticMapSlot;
struct StaticMapSlot {
const char *name;
StaticMapSlotKind kind;
union {
const StaticMapSlot *as_smap;
const char *as_string;
_Bool as_bool;
long long int as_int;
double as_float;
int (*as_funct)(Runtime*, Object**, unsigned int, Object**, unsigned int, Error*);
TypeObject *as_type;
};
union { int argc; int length; };
};
Object *Object_NewStaticMap(const StaticMapSlot *slots, Runtime *runt, Error *error);
Object *Object_FromNojaFunction(Runtime *runtime, Executable *exe, int index, int argc, Object *closure, Heap *heap, Error *error);
Object *Object_FromNativeFunction(Runtime *runtime, int (*callback)(Runtime*, Object**, unsigned int, Object**, unsigned int, Error*), int argc, Heap *heap, Error *error);
typedef struct {
Error base;
Runtime *runtime;
Snapshot *snapshot;
} RuntimeError;
void RuntimeError_Init(RuntimeError *error, Runtime *runtime);
void RuntimeError_Free(RuntimeError *error);
#endif
+59
View File
@@ -0,0 +1,59 @@
/* +--------------------------------------------------------------------------+
** | _ _ _ |
** | | \ | | (_) |
** | | \| | ___ _ __ _ |
** | | . ` |/ _ \| |/ _` | |
** | | |\ | (_) | | (_| | |
** | |_| \_|\___/| |\__,_| |
** | _/ | |
** | |__/ |
** +--------------------------------------------------------------------------+
** | Copyright (c) 2022 Francesco Cozzuto <francesco.cozzuto@gmail.com> |
** +--------------------------------------------------------------------------+
** | This file is part of The Noja Interpreter. |
** | |
** | The Noja Interpreter is free software: you can redistribute it and/or |
** | modify it under the terms of the GNU General Public License as published |
** | by the Free Software Foundation, either version 3 of the License, or (at |
** | your option) any later version. |
** | |
** | The Noja Interpreter is distributed in the hope that it will be useful, |
** | but WITHOUT ANY WARRANTY; without even the implied warranty of |
** | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
** | Public License for more details. |
** | |
** | You should have received a copy of the GNU General Public License along |
** | with The Noja Interpreter. If not, see <http://www.gnu.org/licenses/>. |
** +--------------------------------------------------------------------------+
*/
#include <assert.h>
#include "runtime.h"
static void on_report(Error *error)
{
assert(error != NULL);
RuntimeError *error2 = (RuntimeError*) error;
if(error2->runtime != NULL)
error2->snapshot = Snapshot_New(error2->runtime);
}
void RuntimeError_Init(RuntimeError *error, Runtime *runtime)
{
assert(error != NULL);
Error_Init2(&error->base, on_report);
error->runtime = runtime;
error->snapshot = NULL;
}
void RuntimeError_Free(RuntimeError *error)
{
if(error->snapshot)
Snapshot_Free(error->snapshot);
Error_Free(&error->base);
}