diff options
| author | physick <mynameisgennadiy@vk.com> | 2026-08-10 15:12:10 +0500 |
|---|---|---|
| committer | physick <mynameisgennadiy@vk.com> | 2026-08-10 15:12:10 +0500 |
| commit | 40d500949686c3b2ea229eb3c7fe4e2f252785af (patch) | |
| tree | 0b0575c32fdd76a3dc83c3007886a0b83eeb1973 | |
| parent | 7ec77a546f8aaa0a77c1cd1cff23f929a9fdca87 (diff) | |
list support
| -rw-r--r-- | src/puzzles/puzzle.c | 90 | ||||
| -rw-r--r-- | src/puzzles/puzzle.h | 6 |
2 files changed, 76 insertions, 20 deletions
diff --git a/src/puzzles/puzzle.c b/src/puzzles/puzzle.c index e14708f..441c274 100644 --- a/src/puzzles/puzzle.c +++ b/src/puzzles/puzzle.c @@ -1,6 +1,7 @@ #include "../resources/resources.h" #include "puzzle.h" #include "../wren_inter/wren_inter.h" +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -9,6 +10,10 @@ static const char *USER_MODULE_NAME = "main"; +void PutIOIntoSlot(int slot, PuzzleIO *io, WrenVM *vm); +// We have to declare this funttion on this level because it is used recursively +// in its internal calls + /// Returns 1 test puzzle for the GetTestPuzzle segment Test *getTestPuzzleTests() { Test *outp = malloc(sizeof(Test)); @@ -17,8 +22,29 @@ Test *getTestPuzzleTests() { outp->argc = 1; outp->argv = malloc(sizeof(PuzzleIO)); - outp->argv->type = WREN_TYPE_STRING; - outp->argv->value = strdup("Hello"); + outp->argv->type = WREN_TYPE_LIST; + + PuzzleListIO *list = malloc(sizeof(PuzzleListIO)); + list->length = 2; + list->elements = malloc(sizeof(PuzzleIO) * 2); + + list->elements[0].type = WREN_TYPE_STRING; + list->elements[0].value = strdup("Hello"); + + list->elements[1].type = WREN_TYPE_LIST; + + PuzzleListIO *list1 = malloc(sizeof(PuzzleListIO)); + list1->length = 2; + list1->elements = malloc(sizeof(PuzzleIO) * 2); + + list1->elements[0].type = WREN_TYPE_STRING; + list1->elements[0].value = strdup("Hello"); + + list1->elements[1].type = WREN_TYPE_STRING; + list1->elements[1].value = strdup("World"); + + list->elements[1].value = list1; + outp->argv->value = list; outp->ExpectedResult.type = WREN_TYPE_STRING; outp->ExpectedResult.value = strdup("Hello"); @@ -39,6 +65,12 @@ Puzzle *GetTestPuzzle() { } void FreePuzzleIO(PuzzleIO *io) { + if (io->type == WREN_TYPE_LIST) { + PuzzleListIO *list = io->value; + for (int i = 0; i < list->length; i++) { + FreePuzzleIO(&list->elements[i]); + } + } if (io->value != NULL) free(io->value); } @@ -66,6 +98,20 @@ void FreePuzzle(Puzzle *self) { free(self); } +const char *GetTypeName(WrenType type) { + switch (type) { + case WREN_TYPE_BOOL: return "bool"; + case WREN_TYPE_NUM: return "num"; + case WREN_TYPE_FOREIGN: return "foreign"; + case WREN_TYPE_LIST: return "list"; + case WREN_TYPE_MAP: return "map"; + case WREN_TYPE_NULL: return "null"; + case WREN_TYPE_STRING: return "string"; + case WREN_TYPE_UNKNOWN: return "unknown"; + } +} + + void PrintArgumentToSB(PuzzleIO *io, StringBuilder *sb) { if (sb == NULL || io == NULL) return; char buf[128]; @@ -124,7 +170,25 @@ bool PrintTestCallToSB(Test *test, StringBuilder *sb) { return true; } -// We are currenttly +void PutListIOIntoSlot(WrenVM *vm, int slot, PuzzleListIO *io) { + if (io == NULL || vm == NULL) return; + + // The last slots will be used as a buffer for list elements. + // The slot count grows linearly so if we inpad lists there will be + // no overlap. The internal stack grows in powers of 2 so it is fine + // to re-ensure slots in small bursts + int slot_start = wrenGetSlotCount(vm); + wrenEnsureSlots(vm, slot_start + io->length); + + wrenSetSlotNewList(vm, slot); + for (int i = 0; i < io->length; i++) { + int idx = slot_start + i; + + PutIOIntoSlot(idx, &io->elements[i], vm); + wrenInsertInList(vm, slot, -1, idx); + } +} + void PutIOIntoSlot(int slot, PuzzleIO *io, WrenVM *vm) { switch (io->type) { case (WREN_TYPE_BOOL): @@ -134,7 +198,7 @@ void PutIOIntoSlot(int slot, PuzzleIO *io, WrenVM *vm) { wrenSetSlotDouble(vm, slot, *(double *)io->value); break; case (WREN_TYPE_LIST): - wrenSetSlotDouble(vm, slot, *(double *)io->value); + PutListIOIntoSlot(vm, slot, (PuzzleListIO *)io->value); break; case (WREN_TYPE_MAP): // TODO @@ -150,19 +214,6 @@ void PutIOIntoSlot(int slot, PuzzleIO *io, WrenVM *vm) { } } -const char *GetTypeName(WrenType type) { - switch (type) { - case WREN_TYPE_BOOL: return "bool"; - case WREN_TYPE_NUM: return "num"; - case WREN_TYPE_FOREIGN: return "foreign"; - case WREN_TYPE_LIST: return "list"; - case WREN_TYPE_MAP: return "map"; - case WREN_TYPE_NULL: return "null"; - case WREN_TYPE_STRING: return "string"; - case WREN_TYPE_UNKNOWN: return "unknown"; - } -} - bool CheckResult(PuzzleIO *io, WrenVM *vm, char *outp) { WrenType actual_type = wrenGetSlotType(vm, 0); if (io->type != actual_type) { @@ -221,10 +272,9 @@ bool CheckResult(PuzzleIO *io, WrenVM *vm, char *outp) { } } - - TestResult RunTest(Test *test, WrenVM *vm, size_t max_op) { - wrenEnsureSlots(vm, test->argc + 1); // finction handle + arguments + // function handle + arguments + wrenEnsureSlots(vm, test->argc + 1); wrenGetVariable(vm, USER_MODULE_NAME, test->RecieverName, 0); char outp_buf[512]; diff --git a/src/puzzles/puzzle.h b/src/puzzles/puzzle.h index 70c6cad..acf5a17 100644 --- a/src/puzzles/puzzle.h +++ b/src/puzzles/puzzle.h @@ -14,6 +14,12 @@ typedef struct { void *value; } PuzzleIO; +// Represents a WREN_TYPE_LIST in PuzzleIO +typedef struct { + size_t length; + PuzzleIO *elements; +} PuzzleListIO; + typedef struct { // https://wren.io/embedding/calling-wren-from-c.html#setting-up-a-receiver char *RecieverName; |
