From 1513ba98783c837464f5777851d26ae2c894f35d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 23 Feb 2023 20:23:10 +1100 Subject: [PATCH 1/2] tweaked scripts folder --- scripts/example-entity.toy | 125 ------------------------------------ scripts/example.toy | 89 ------------------------- scripts/level.toy | 6 +- scripts/roguelike-seeds.toy | 36 +++++++++++ scripts/small.toy | 12 ---- 5 files changed, 39 insertions(+), 229 deletions(-) delete mode 100644 scripts/example-entity.toy delete mode 100644 scripts/example.toy create mode 100644 scripts/roguelike-seeds.toy delete mode 100644 scripts/small.toy diff --git a/scripts/example-entity.toy b/scripts/example-entity.toy deleted file mode 100644 index 4206550..0000000 --- a/scripts/example-entity.toy +++ /dev/null @@ -1,125 +0,0 @@ -import node; - -//constants -var SPEED: int const = 10; - -//variables -var parent: opaque = null; -var posX: int = 50; -var posY: int = 50; -var WIDTH: int const = 100; -var HEIGHT: int const = 100; - -var xspeed: int = 0; -var yspeed: int = 0; - -//accessors - variables are private, functions are public -fn getX(node: opaque) { - return posX; -} - -fn getY(node: opaque) { - return posY; -} - -//lifecycle functions -fn onInit(node: opaque) { - print "render.toy:onInit() called\n"; - - node.loadTexture("sprites:/character.png"); - parent = node.getNodeParent(); -} - -fn onStep(node: opaque) { - posX += xspeed; - posY += yspeed; -} - -fn onFree(node: opaque) { - print "render.toy:onFree() called\n"; - - node.freeTexture(); -} - -fn onDraw(node: opaque) { -// print "render.toy:onDraw() called\n"; - - var px = parent.callNode("getX"); - var py = parent.callNode("getY"); - - if (px == null) { - px = 0; - } - - if (py == null) { - py = 0; - } - - node.drawNode(posX + px, posY + py, WIDTH, HEIGHT); -} - -//event functions -fn onKeyDown(node: opaque, event: string) { - if (event == "character_up") { - yspeed -= SPEED; - return; - } - - if (event == "character_down") { - yspeed += SPEED; - return; - } - - if (event == "character_left") { - xspeed -= SPEED; - return; - } - - if (event == "character_right") { - xspeed += SPEED; - return; - } -} - -fn onKeyUp(node: opaque, event: string) { - if (event == "character_up" && yspeed < 0) { - yspeed = 0; - return; - } - - if (event == "character_down" && yspeed > 0) { - yspeed = 0; - return; - } - - if (event == "character_left" && xspeed < 0) { - xspeed = 0; - return; - } - - if (event == "character_right" && xspeed > 0) { - xspeed = 0; - return; - } -} - -fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) { - // print "entity.toy:onMouseMotion(" + string x + ", " + string y + ", " + string xrel + ", " + string yrel + ")\n"; -} - -fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) { - // print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")\n"; - - //jump to pos - posX = x - WIDTH / 2; - posY = y - HEIGHT / 2; -} - -fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) { - // print "entity.toy:onMouseButtonUp(" + string x + ", " + string y + ", " + button + ")\n"; -} - -fn onMouseWheel(node: opaque, xrel: int, yrel: int) { - // print "entity.toy:onMouseWheel(" + string xrel + ", " + string yrel + ")\n"; -} - diff --git a/scripts/example.toy b/scripts/example.toy deleted file mode 100644 index 1489908..0000000 --- a/scripts/example.toy +++ /dev/null @@ -1,89 +0,0 @@ -//single line comment - -/* -multi line comment -*/ - -//test primitive literals -print "hello world"; -print null; -print true; -print false; -print 42; -print 3.14; -print -69; -print -4.20; -print 2 + (3 * 3); - -//test operators (integers) -print 1 + 1; -print 1 - 1; -print 2 * 2; -print 1 / 2; -print 4 % 2; - -//test operators (floats) -print 1.0 + 1.0; -print 1.0 - 1.0; -print 2.0 * 2.0; -print 1.0 / 2.0; - -//test scopes -{ - print "This statement is within a scope."; - { - print "This is a deeper scope."; - } -} -print "Back to the outer scope."; - -//test scope will delegate to higher scope -var a = 1; -{ - a = 2; - print a; -} -print a; - -//test scope will shadow higher scope on redefine -var b: int = 3; -{ - var b = 4; - print b; -} -print b; - -//test compounds, repeatedly -print [1, 2, 3]; -print [4, 5]; -print ["key":"value"]; -print [1, 2, 3]; -print [4, 5]; -print ["key":"value"]; - -//test empties -print []; -print [:]; - -//test nested compounds -print [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; - -//var declarations -var x = 31; -var y : int = 42; -var arr : [int] = [1, 2, 3, 42]; -var dict : [string:int] = ["hello": 1, "world":2]; - -//printing expressions -print x; -print x + y; -print arr; -print dict; - -//test asserts at the end of the file -assert x, "This won't be seen"; -assert true, "This won't be seen"; -assert false, "This is a failed assert, and will end execution"; - -print "This will not be printed because of the above assert"; - diff --git a/scripts/level.toy b/scripts/level.toy index 0bf5362..1cf9ac3 100644 --- a/scripts/level.toy +++ b/scripts/level.toy @@ -34,8 +34,8 @@ var tiles: [[int]] const = [ ]; var tileset: [int: string] const = [ - 0: " ", - 1: " X " + 0: " ", + 1: "X " ]; //variables @@ -48,7 +48,7 @@ fn draw() { for (var i: int = 0; i < WIDTH; i++) { //draw the player pos if (i == posX && j == posY) { - print " O "; + print "O "; continue; } diff --git a/scripts/roguelike-seeds.toy b/scripts/roguelike-seeds.toy new file mode 100644 index 0000000..130efe4 --- /dev/null +++ b/scripts/roguelike-seeds.toy @@ -0,0 +1,36 @@ +/* + +Since this is a pseudo-random generator, and there's no internal state to the algorithm other +than the generator opaque, there needs to be a "call counter" (current depth) to shuffle the +initial seeds, otherwise generators created from other generators will resemble their parents, +but one call greater. + +*/ + +import standard; +import random; + +var DEPTH: int const = 20; +var levels = []; + +//generate the level seeds +var generator: opaque = createRandomGenerator(clock().hash()); + +for (var i: int = 0; i < DEPTH; i++) { + levels.push(generator.generateRandomNumber()); +} + +generator.freeRandomGenerator(); + +//generate "levels" of a roguelike +for (var i = 0; i < DEPTH; i++) { + var rng: opaque = createRandomGenerator(levels[i] + i); + + print "---"; + print levels[i]; + print rng.generateRandomNumber(); + print rng.generateRandomNumber(); + print rng.generateRandomNumber(); + + rng.freeRandomGenerator(); +} \ No newline at end of file diff --git a/scripts/small.toy b/scripts/small.toy deleted file mode 100644 index a9afb07..0000000 --- a/scripts/small.toy +++ /dev/null @@ -1,12 +0,0 @@ -import standard; -import random; - -for (var i: int = 0; i < 1_000_000; i++) { - var generator: opaque = createRandomGenerator(clock().hash()); - - print generator.generateRandomNumber(); - print generator.generateRandomNumber(); - print generator.generateRandomNumber(); - - generator.freeRandomGenerator(); -} \ No newline at end of file From 3aeddff736945f953bb7865678405091f06567ef Mon Sep 17 00:00:00 2001 From: Ratstail91 Date: Fri, 24 Feb 2023 22:13:50 +1100 Subject: [PATCH 2/2] Tweaks to dictionary for performance --- source/toy_literal.h | 3 ++- source/toy_literal_dictionary.c | 10 +++++++--- source/toy_scope.c | 4 ++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/source/toy_literal.h b/source/toy_literal.h index f205fbf..64df483 100644 --- a/source/toy_literal.h +++ b/source/toy_literal.h @@ -8,6 +8,7 @@ struct Toy_Literal; struct Toy_Interpreter; struct Toy_LiteralArray; +struct Toy_Scope; typedef int (*Toy_NativeFn)(struct Toy_Interpreter* interpreter, struct Toy_LiteralArray* arguments); typedef int (*Toy_HookFn)(struct Toy_Interpreter* interpreter, struct Toy_Literal identifier, struct Toy_Literal alias); typedef void (*Toy_PrintFn)(const char*); @@ -57,7 +58,7 @@ typedef struct Toy_Literal { Toy_NativeFn native; //8 Toy_HookFn hook; //8 } inner; //8 - void* scope; //8 + struct Toy_Scope* scope; //8 } function; //16 struct { //for variable names diff --git a/source/toy_literal_dictionary.c b/source/toy_literal_dictionary.c index 46b5de7..0d89545 100644 --- a/source/toy_literal_dictionary.c +++ b/source/toy_literal_dictionary.c @@ -17,6 +17,10 @@ static void setEntryValues(Toy_private_dictionary_entry* entry, Toy_Literal key, } static Toy_private_dictionary_entry* getEntryArray(Toy_private_dictionary_entry* array, int capacity, Toy_Literal key, unsigned int hash, bool mustExist) { + if (!capacity) { + return NULL; + } + //find "key", starting at index unsigned int index = hash % capacity; unsigned int start = index; @@ -122,10 +126,10 @@ static void freeEntryArray(Toy_private_dictionary_entry* array, int capacity) { void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary) { //HACK: because modulo by 0 is undefined, set the capacity to a non-zero value (and allocate the arrays) dictionary->entries = NULL; - dictionary->capacity = TOY_GROW_CAPACITY(0); + dictionary->capacity = 0; dictionary->contains = 0; dictionary->count = 0; - adjustEntryCapacity(&dictionary->entries, 0, dictionary->capacity); + dictionary->capacity = 0; } void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary) { @@ -215,5 +219,5 @@ void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal bool Toy_existsLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key) { //null & not tombstoned Toy_private_dictionary_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, Toy_hashLiteral(key), false); - return !(TOY_IS_NULL(entry->key) && TOY_IS_NULL(entry->value)); + return entry != NULL && !(TOY_IS_NULL(entry->key) && TOY_IS_NULL(entry->value)); } diff --git a/source/toy_scope.c b/source/toy_scope.c index 9c2e979..61647ef 100644 --- a/source/toy_scope.c +++ b/source/toy_scope.c @@ -209,6 +209,10 @@ Toy_Scope* Toy_popScope(Toy_Scope* scope) { } Toy_Scope* Toy_copyScope(Toy_Scope* original) { + if (original == NULL) { + return NULL; + } + Toy_Scope* scope = TOY_ALLOCATE(Toy_Scope, 1); scope->ancestor = original->ancestor; Toy_initLiteralDictionary(&scope->variables);