Started working on engine nodes

This commit is contained in:
2022-10-02 05:42:45 +01:00
parent 50aef00ec0
commit 7e1612d915
12 changed files with 365 additions and 19 deletions

View File

@@ -68,7 +68,9 @@ static void execStep(Engine* engine) {
case SDL_WINDOWEVENT: {
switch(event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
SDL_RenderSetLogicalSize(engine->renderer, event.window.data1, event.window.data2);
engine->screenWidth = event.window.data1;
engine->screenHeight = event.window.data2;
SDL_RenderSetLogicalSize(engine->renderer, engine->screenWidth, engine->screenHeight);
break;
}
}

View File

@@ -1,2 +1,120 @@
#include "engine_node.h"
#include "memory.h"
void initEngineNode(EngineNode* node, Interpreter* interpreter, void* tb, size_t size) {
//init
node->children = NULL;
node->capacity = 0;
node->count = 0;
node->functions = ALLOCATE(LiteralDictionary, 1);
initLiteralDictionary(node->functions);
//run bytecode
runInterpreter(interpreter, tb, size);
//grab all top-level function literals
LiteralDictionary* variablesPtr = &interpreter->scope->variables;
for (int i = 0; i < variablesPtr->capacity; i++) {
//skip empties and tombstones
if (IS_NULL(variablesPtr->entries[i].key)) {
continue;
}
//if this variable is a function
_entry* entry = &variablesPtr->entries[i];
if (IS_FUNCTION(entry->value)) {
//save a copy
setLiteralDictionary(node->functions, entry->key, entry->value);
}
}
}
void pushEngineNode(EngineNode* node, EngineNode* child) {
//push to the array (prune tombstones when expanding/copying)
if (node->count + 1 > node->capacity) {
int oldCapacity = node->capacity;
node->capacity = GROW_CAPACITY(oldCapacity);
node->children = GROW_ARRAY(EngineNode, node->children, oldCapacity, node->capacity);
}
//prune tombstones (experimental)
int counter = 0;
for (int i = 0; i < node->capacity; i++) {
if (i >= node->count) {
node->count = counter;
break;
}
//move down
if (node->children[i].functions != NULL) {
node->children[counter++] = node->children[i];
}
}
//zero the rest
while (counter < node->capacity) {
node->children[counter].children = NULL;
node->children[counter].capacity = 0;
node->children[counter].count = 0;
node->children[counter].functions = NULL;
counter++;
}
//assign
node->children[node->count++] = *child;
}
void freeEngineNode(EngineNode* node) {
//free and tombstone this node
for (int i = 0; i < node->capacity; i++) {
freeEngineNode(&node->children[i]);
}
FREE_ARRAY(EngineNode, node->children, node->capacity);
if (node->functions != NULL) {
freeLiteralDictionary(node->functions);
}
FREE(LiteralDictionary, node->functions);
node->children = NULL;
node->capacity = -1;
node->count = -1;
node->functions = NULL;
}
static void callEngineNodeLiteral(EngineNode* node, Interpreter* interpreter, Literal key) {
//if this fn exists
if (existsLiteralDictionary(node->functions, key)) {
Literal fn = getLiteralDictionary(node->functions, key);
LiteralArray dummyArray;
initLiteralArray(&dummyArray);
callLiteralFn(interpreter, fn, &dummyArray, &dummyArray);
freeLiteralArray(&dummyArray);
freeLiteral(fn);
}
//recurse to the (non-tomstone) children
for (int i = 0; i < node->count; i++) {
if (node->children[i].functions != NULL) {
callEngineNodeLiteral(&node->children[i], interpreter, key);
}
}
}
void callEngineNode(EngineNode* node, Interpreter* interpreter, char* fnName) {
//call "fnName" on this node, and all children, if it exists
Literal key = TO_IDENTIFIER_LITERAL(copyString(fnName, strlen(fnName)), strlen(fnName));
callEngineNodeLiteral(node, interpreter, key);
freeLiteral(key);
}

View File

@@ -2,22 +2,25 @@
#include "common.h"
#include "literal_dictionary.h"
#include "interpreter.h"
//forward declare
typedef struct _engineNode EngineNode;
typedef struct _engine Engine;
//the interface function
typedef void (*EngineNodeFn)(EngineNode* self, Engine* engine);
//the node object, which forms a tree
typedef struct _engineNode {
//use Toy's memory model
void* children;
EngineNode* children;
int capacity;
int count;
int count; //includes tombstones
EngineNodeFn onInit;
EngineNodeFn onStep;
EngineNodeFn onFree;
//toy functions, stored in a dict for flexibility
LiteralDictionary* functions;
} EngineNode;
CORE_API void initEngineNode(EngineNode* node, Interpreter* interpreter, void* tb, size_t size); //run bytecode, then grab all top-level function literals
CORE_API void pushEngineNode(EngineNode* node, EngineNode* child); //push to the array (prune tombstones when expanding/copying)
CORE_API void freeEngineNode(EngineNode* node); //free and tombstone this node
CORE_API void callEngineNode(EngineNode* node, Interpreter* interpreter, char* fnName); //call "fnName" on this node, and all children, if it exists

View File

@@ -1,8 +1,8 @@
CC=gcc
IDIR+=.
CFLAGS+=$(addprefix -I,$(IDIR)) -DSDL_MAIN_HANDLED -g -Wall -W -pedantic -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable
LIBS+=-lSDL2
IDIR+=. ../Toy/source
CFLAGS+=$(addprefix -I,$(IDIR)) -DSDL_MAIN_HANDLED -g -Wall -W -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable
LIBS+=-lSDL2 -ltoy
ODIR = obj
SRC = $(wildcard *.c)
@@ -24,10 +24,10 @@ else
endif
library: $(OBJ)
$(CC) -DCORE_EXPORT $(CFLAGS) -shared -o $(OUT) $(LIBLINE) $(LIBS)
$(CC) -DCORE_EXPORT $(CFLAGS) -shared -o $(OUT) $(LIBLINE) -L../$(LIBDIR) $(LIBS)
static: $(OBJ)
ar crs $(CORE_OUTDIR)/lib$(OUTNAME).a $(OBJ) $(LIBS)
ar crs $(CORE_OUTDIR)/lib$(OUTNAME).a $(OBJ) -L../$(LIBDIR) $(LIBS)
$(OBJ): | $(ODIR)