Updated Toy, added a couple util functions

This commit is contained in:
2022-10-15 00:04:58 +01:00
parent dfcdaf3b92
commit 2eb67d9bca
9 changed files with 107 additions and 9 deletions

2
Toy

Submodule Toy updated: 168369d897...bd348abf32

View File

@@ -1,3 +1,4 @@
import engine;
import render; import render;
fn onInit(node: opaque) { fn onInit(node: opaque) {
@@ -5,7 +6,8 @@ fn onInit(node: opaque) {
} }
fn onStep(node: opaque) { fn onStep(node: opaque) {
// print node.getNodeTag();
print node.getNodeParent().getNodeTag();
} }
fn onFree(node: opaque) { fn onFree(node: opaque) {
@@ -13,5 +15,5 @@ fn onFree(node: opaque) {
} }
fn onDraw(node: opaque) { fn onDraw(node: opaque) {
node.drawRenderNode(50, 50); node.drawRenderNode(50, 50, 100, 100);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 166 B

After

Width:  |  Height:  |  Size: 700 B

View File

@@ -2,7 +2,7 @@
#include "memory.h" #include "memory.h"
STATIC_ASSERT(sizeof(EngineNode) == 32); STATIC_ASSERT(sizeof(EngineNode) == 48);
STATIC_ASSERT(sizeof(EngineNodeCallback) == 8); STATIC_ASSERT(sizeof(EngineNodeCallback) == 8);
STATIC_ASSERT(sizeof(LiteralDictionary*) == 8); STATIC_ASSERT(sizeof(LiteralDictionary*) == 8);
STATIC_ASSERT(sizeof(EngineNode*) == 8); STATIC_ASSERT(sizeof(EngineNode*) == 8);
@@ -17,6 +17,8 @@ void initEngineNode(EngineNode* node, Interpreter* interpreter, void* tb, size_t
//init //init
node->freeMemory = freeMemory; node->freeMemory = freeMemory;
node->functions = ALLOCATE(LiteralDictionary, 1); node->functions = ALLOCATE(LiteralDictionary, 1);
node->parent = NULL;
node->tag = OPAQUE_TAG_ENGINE_NODE;
node->children = NULL; node->children = NULL;
node->capacity = 0; node->capacity = 0;
node->count = 0; node->count = 0;
@@ -69,6 +71,9 @@ void pushEngineNode(EngineNode* node, EngineNode* child) {
//assign //assign
node->children[node->count++] = child; node->children[node->count++] = child;
//reverse-assign
child->parent = node;
} }
void freeEngineNode(EngineNode* node) { void freeEngineNode(EngineNode* node) {
@@ -97,7 +102,7 @@ static void callEngineNodeLiteral(EngineNode* node, Interpreter* interpreter, Li
//if this fn exists //if this fn exists
if (existsLiteralDictionary(node->functions, key)) { if (existsLiteralDictionary(node->functions, key)) {
Literal fn = getLiteralDictionary(node->functions, key); Literal fn = getLiteralDictionary(node->functions, key);
Literal n = TO_OPAQUE_LITERAL(node, -1); Literal n = TO_OPAQUE_LITERAL(node, node->tag);
LiteralArray arguments; LiteralArray arguments;
LiteralArray returns; LiteralArray returns;

View File

@@ -5,9 +5,10 @@
#include "literal_dictionary.h" #include "literal_dictionary.h"
#include "interpreter.h" #include "interpreter.h"
#define OPAQUE_TAG_ENGINE_NODE 1
//forward declare //forward declare
typedef struct _engineNode EngineNode; typedef struct _engineNode EngineNode;
typedef void (*EngineNodeCallback)(void*); typedef void (*EngineNodeCallback)(void*);
//the node object, which forms a tree //the node object, which forms a tree
@@ -18,6 +19,13 @@ typedef struct _engineNode {
//toy functions, stored in a dict for flexibility //toy functions, stored in a dict for flexibility
LiteralDictionary* functions; LiteralDictionary* functions;
//point to the parent
EngineNode* parent;
//my opaque type tag
int tag;
int _unused;
//use Toy's memory model //use Toy's memory model
EngineNode** children; EngineNode** children;
int capacity; int capacity;

View File

@@ -201,7 +201,7 @@ static int nativeLoadNode(Interpreter* interpreter, LiteralArray* arguments) {
//NOTE: initNode() must be called manually //NOTE: initNode() must be called manually
// return the node // return the node
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, -1); Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
pushLiteralArray(&interpreter->stack, nodeLiteral); pushLiteralArray(&interpreter->stack, nodeLiteral);
//cleanup //cleanup
@@ -397,7 +397,7 @@ static int nativeGetNode(Interpreter* interpreter, LiteralArray* arguments) {
} }
EngineNode* childNode = parentNode->children[intIndex]; EngineNode* childNode = parentNode->children[intIndex];
Literal child = TO_OPAQUE_LITERAL(childNode, -1); Literal child = TO_OPAQUE_LITERAL(childNode, childNode->tag);
pushLiteralArray(&interpreter->stack, child); pushLiteralArray(&interpreter->stack, child);
@@ -409,6 +409,76 @@ static int nativeGetNode(Interpreter* interpreter, LiteralArray* arguments) {
return 1; return 1;
} }
static int nativeGetNodeParent(Interpreter* interpreter, LiteralArray* arguments) {
//checks
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments passed to getNodeParent\n");
return -1;
}
Literal nodeLiteral = popLiteralArray(arguments);
Literal nodeIdn = nodeLiteral;
if (IS_IDENTIFIER(nodeLiteral) && parseIdentifierToValue(interpreter, &nodeLiteral)) {
freeLiteral(nodeIdn);
}
if (!IS_OPAQUE(nodeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to getNodeParent\n");
freeLiteral(nodeLiteral);
return -1;
}
//push the node
EngineNode* node = AS_OPAQUE(nodeLiteral);
EngineNode* parent = node->parent;
Literal parentLiteral = TO_NULL_LITERAL;
if (parent != NULL) {
parentLiteral = TO_OPAQUE_LITERAL(parent, parent->tag);
}
pushLiteralArray(&interpreter->stack, parentLiteral);
//cleanup
freeLiteral(parentLiteral);
freeLiteral(nodeLiteral);
return 1;
}
static int nativeGetNodeTag(Interpreter* interpreter, LiteralArray* arguments) {
//checks
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments passed to getNodeTag\n");
return -1;
}
Literal nodeLiteral = popLiteralArray(arguments);
Literal nodeIdn = nodeLiteral;
if (IS_IDENTIFIER(nodeLiteral) && parseIdentifierToValue(interpreter, &nodeLiteral)) {
freeLiteral(nodeIdn);
}
if (!IS_OPAQUE(nodeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to getNodeTag\n");
freeLiteral(nodeLiteral);
return -1;
}
//push the tag
Literal tagLiteral = TO_INTEGER_LITERAL( ((EngineNode*)AS_OPAQUE(nodeLiteral))->tag );
pushLiteralArray(&interpreter->stack, tagLiteral);
//cleanup
freeLiteral(nodeLiteral);
freeLiteral(tagLiteral);
return 1;
}
//call the hook //call the hook
typedef struct Natives { typedef struct Natives {
char* name; char* name;
@@ -426,6 +496,8 @@ int hookEngine(Interpreter* interpreter, Literal identifier, Literal alias) {
{"_freeChildNode", nativeFreeChildNode}, {"_freeChildNode", nativeFreeChildNode},
{"_pushNode", nativePushNode}, {"_pushNode", nativePushNode},
{"_getNode", nativeGetNode}, {"_getNode", nativeGetNode},
{"_getNodeParent", nativeGetNodeParent},
{"_getNodeTag", nativeGetNodeTag},
{NULL, NULL} {NULL, NULL}
}; };

View File

@@ -61,7 +61,7 @@ static int nativeLoadRenderNode(Interpreter* interpreter, LiteralArray* argument
//NOTE: initNode() must be called manually //NOTE: initNode() must be called manually
// return the node // return the node
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, -1); Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
pushLiteralArray(&interpreter->stack, nodeLiteral); pushLiteralArray(&interpreter->stack, nodeLiteral);
//cleanup //cleanup

View File

@@ -19,6 +19,8 @@ void initRenderNode(RenderNode* node, Interpreter* interpreter, void* tb, size_t
//init //init
node->freeMemory = freeMemory; node->freeMemory = freeMemory;
node->functions = ALLOCATE(LiteralDictionary, 1); node->functions = ALLOCATE(LiteralDictionary, 1);
node->parent = NULL;
node->tag = OPAQUE_TAG_RENDER_NODE;
node->children = NULL; node->children = NULL;
node->capacity = 0; node->capacity = 0;
node->count = 0; node->count = 0;

View File

@@ -7,6 +7,8 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#define OPAQUE_TAG_RENDER_NODE 2
typedef struct _renderNode { typedef struct _renderNode {
//function for releasing memory //function for releasing memory
EngineNodeCallback freeMemory; EngineNodeCallback freeMemory;
@@ -14,6 +16,13 @@ typedef struct _renderNode {
//toy functions, stored in a dict for flexibility //toy functions, stored in a dict for flexibility
LiteralDictionary* functions; LiteralDictionary* functions;
//point to the parent
EngineNode* parent;
//my opaque type tag
int tag;
int _unused;
//use Toy's memory model //use Toy's memory model
EngineNode** children; EngineNode** children;
int capacity; int capacity;