diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/puzzles/puzzle.c | 104 | ||||
| -rw-r--r-- | src/puzzles/puzzle.h | 1 | ||||
| -rw-r--r-- | src/resources/resources.h | 2 | ||||
| -rw-r--r-- | src/scenes/puzzleScene.c | 7 | ||||
| -rw-r--r-- | src/scenes/resultView.c | 5 | ||||
| -rw-r--r-- | src/ui_elements/TextEditor.c | 2 | ||||
| -rw-r--r-- | src/ui_elements/UI.h | 3 |
7 files changed, 113 insertions, 11 deletions
diff --git a/src/puzzles/puzzle.c b/src/puzzles/puzzle.c index 22f7a4d..286057b 100644 --- a/src/puzzles/puzzle.c +++ b/src/puzzles/puzzle.c @@ -4,6 +4,8 @@ #include <string.h> #include <wren.h> +static const char *USER_MODULE_NAME = "main"; + /// Returns 1 test puzzle for the GetTestPuzzle segment Test *getTestPuzzleTests() { Test *outp = malloc(sizeof(Test)); @@ -53,10 +55,108 @@ void FreePuzzle(Puzzle *self) { free(self); } +// We are currenttly +void PutIOIntoSlot(int slot, PuzzleIO *io, WrenVM *vm) { + switch (io->type) { + case (WREN_TYPE_BOOL): + wrenSetSlotBool(vm, slot, *(bool *)io->value); + break; + case (WREN_TYPE_NUM): + wrenSetSlotDouble(vm, slot, *(double *)io->value); + break; + case (WREN_TYPE_LIST): + wrenSetSlotDouble(vm, slot, *(double *)io->value); + break; + case (WREN_TYPE_MAP): + // TODO + return; + case (WREN_TYPE_NULL): + wrenSetSlotNull(vm, slot); + break; + case (WREN_TYPE_STRING): + wrenSetSlotString(vm, slot, io->value); + break; + default: + return; + } +} + +// The cleanest way to do it is move this from the CheckResult enum +bool checkResultString(PuzzleIO *io, WrenVM *vm) { + const char *res = wrenGetSlotString(vm, 0); + if (res == NULL || io->value == NULL) return false; + return strcmp(res, io->value) == 0; +} + +bool CheckResult(PuzzleIO *io, WrenVM *vm) { + WrenType actual_type = wrenGetSlotType(vm, 0); + + switch (io->type) { + case (WREN_TYPE_BOOL): + if (actual_type != WREN_TYPE_BOOL) return false; + if (io->value == NULL) return false; + return wrenGetSlotBool(vm, 0) == *((bool *)io->value); + + case (WREN_TYPE_NUM): + if (actual_type != WREN_TYPE_NUM) return false; + if (io->value == NULL) return false; + return wrenGetSlotDouble(vm, 0) == *((double *)io->value); + + case (WREN_TYPE_NULL): + return actual_type == WREN_TYPE_NULL; + + case (WREN_TYPE_STRING): + if (actual_type != WREN_TYPE_STRING) return false; + return checkResultString(io, vm); + + case (WREN_TYPE_LIST): + case (WREN_TYPE_MAP): + default: + return false; + } +} enum PUZZLE_TEST_RESULT RunTest(Test *test, WrenVM *vm) { - // TODO: finish this - return RESULT_OK; + wrenEnsureSlots(vm, test->argc + 1); // finction handle + arguments + wrenGetVariable(vm, USER_MODULE_NAME, test->RecieverName, 0); + + WrenHandle *obj_handle = wrenGetSlotHandle(vm, 0); + if (obj_handle == NULL) { + return RESULT_COMPTIME_NO_ENTRY; + } + + WrenHandle *fn_handle = wrenMakeCallHandle(vm, test->MainMethodSignature); + if (fn_handle == NULL) { + wrenReleaseHandle(vm, obj_handle); + return RESULT_COMPTIME_NO_ENTRY; + } + + wrenSetSlotHandle(vm, 0, obj_handle); + for (int i = 0; i < test->argc; ++i) { + PutIOIntoSlot(i+1, &test->argv[i], vm); + } + + WrenInterpretResult call_result = wrenCall(vm, fn_handle); + + + switch (call_result) { + case WREN_RESULT_COMPILE_ERROR: + return RESULT_COMPTIME_ERR; + case WREN_RESULT_RUNTIME_ERROR: + return RESULT_RUNTIME_ERR; + default: break; + } + + bool result = CheckResult(&test->ExpectedResult, vm); + + wrenReleaseHandle(vm, fn_handle); + wrenReleaseHandle(vm, obj_handle); + + if (result) { + return RESULT_OK; + } else { + return RESULT_WRONG_ANSWER; + } } // Returns the res ptr. Does not modify the address diff --git a/src/puzzles/puzzle.h b/src/puzzles/puzzle.h index 6c72a18..838b9d2 100644 --- a/src/puzzles/puzzle.h +++ b/src/puzzles/puzzle.h @@ -5,6 +5,7 @@ enum PUZZLE_TEST_RESULT : char { RESULT_OK = 0, + RESULT_WRONG_ANSWER, RESULT_COMPTIME_NO_ENTRY, RESULT_COMPTIME_ERR, RESULT_RUNTIME_ERR, diff --git a/src/resources/resources.h b/src/resources/resources.h index e6efc25..d3ad840 100644 --- a/src/resources/resources.h +++ b/src/resources/resources.h @@ -15,6 +15,8 @@ static const Color COLORSCHEME_ORANGE = { 240, 120, 28, 255 }; static const Color COLORSCHEME_YELLOW = { 247, 188, 83, 255 }; /// #E9E1B9 as RGBA static const Color COLORSCHEME_WHITE = { 233, 225, 185, 255 }; +/// #48A56A as RGBA +static const Color COLORSCHEME_GREEN = { 72, 165, 106, 255 }; void LoadResources(); Font *GetMainFont(); diff --git a/src/scenes/puzzleScene.c b/src/scenes/puzzleScene.c index 57ac15d..4bf5b60 100644 --- a/src/scenes/puzzleScene.c +++ b/src/scenes/puzzleScene.c @@ -3,7 +3,6 @@ #include "scenes.h" #include "../resources/resources.h" #include "../ui_elements/UI.h" -#include "../wren_inter/wren_inter.h" typedef struct { Puzzle *PuzzlePtr; @@ -93,11 +92,9 @@ bool updatePuzzleScene(void *self) { if (IsKeyPressed(KEY_F5)) { char *script = GetText(data->EditorInfo); - // char *outp = EvaluateScript(txt); - // free(txt); - // AddScene(CreateResultView(outp)); SubmitionResult *res = SubmitSolution(data->PuzzlePtr, script); - AddScene(CreateResultView(res)); + if (res != NULL) + AddScene(CreateResultView(res)); } return true; diff --git a/src/scenes/resultView.c b/src/scenes/resultView.c index 033d17e..c63967a 100644 --- a/src/scenes/resultView.c +++ b/src/scenes/resultView.c @@ -56,7 +56,8 @@ bool drawResultView(void *self) { ResultViewData *data = ((Scene *)self)->data; // DrawBackground(); Rectangle body = GetResultViewBodyRect(data->scroll_up); - DrawRecBordered(body, WHITE, COLORSCHEME_ORANGE, BORDER); + + DrawRecBordered(body, WHITE, data->value->result == RESULT_OK ? COLORSCHEME_GREEN : COLORSCHEME_ORANGE, BORDER); DrawTextInRec(body, data->value->stdout, GetMainFont(), 20, COLORSCHEME_BROWN, 3, &data->scroll); return true; @@ -75,7 +76,7 @@ Scene *CreateResultView(SubmitionResult *result) { Scene *outp = malloc(sizeof(Scene)); outp->Draw = drawResultView; outp->Update = updateResultView; - outp->Deint = deinitResultView; + outp->Deint = deinitResultView; outp->IsTransparent = true; ResultViewData *data = malloc(sizeof(ResultViewData)); diff --git a/src/ui_elements/TextEditor.c b/src/ui_elements/TextEditor.c index 3cf34ff..1d35612 100644 --- a/src/ui_elements/TextEditor.c +++ b/src/ui_elements/TextEditor.c @@ -500,13 +500,11 @@ void HandleControlComands(TextArea *ta) { } /// Assumes that control is pressed void HandleAltComands(TextArea *ta) { - // emacs-like stuff if (IsKeyPressedFix(KEY_F)) MoveCursorRightWord(ta); if (IsKeyPressedFix(KEY_B)) MoveCursorLeftWord(ta); } - void HandleKeyboard(TextArea *ta) { if (IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL)) { HandleControlComands(ta); diff --git a/src/ui_elements/UI.h b/src/ui_elements/UI.h index bab154c..06d127f 100644 --- a/src/ui_elements/UI.h +++ b/src/ui_elements/UI.h @@ -7,7 +7,9 @@ static const float SCROLL_SENC = 10; static const float TAB_WIDTH = 4; +// Sure about this value static const int MIN_FONT_SIZE = 10; +// Unsure about this value static const int MAX_FONT_SIZE = 100; void DrawRecBordered(Rectangle rec, Color bg, Color border_color, int border_width); @@ -22,6 +24,7 @@ static const size_t DEFAULT_TEXT_NODE_SIZE = 256; typedef struct { void *PrevNode; void *NextNode; + char *text; size_t len; size_t cap; |
