1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#include "puzzle.h"
#include "../wren_inter/wren_inter.h"
#include <stdlib.h>
#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));
outp->RecieverName = strdup("main");
outp->MainMethodSignature = strdup("call(_)");
outp->argc = 1;
outp->argv = malloc(sizeof(PuzzleIO));
outp->argv->type = WREN_TYPE_STRING;
outp->argv->value = strdup("Hello");
outp->ExpectedResult.type = WREN_TYPE_STRING;
outp->ExpectedResult.value = strdup("Hello");
return outp;
}
Puzzle *GetTestPuzzle() {
Puzzle *outp = malloc(sizeof(Puzzle));
if (outp == NULL) return NULL;
outp->Name = strdup("Test puzzle");
outp->Description = strdup("In this puzzle you will have to return the input string");
outp->InitialCode = strdup( "var main = Fn.new { |name| \n System.print(\"Hello, %(name)!\")\n}\n\nmain.call(\"world\")");
outp->TestC = 1;
outp->Tests = getTestPuzzleTests();
return outp;
}
void FreeTests(Test *Tests, size_t TestC) {
if (TestC == 0 || Tests == NULL) return;
for (size_t i = 0; i < TestC; i++) {
Test *t = &Tests[i];
free(t->RecieverName);
free(t->MainMethodSignature);
free(t->argv);
}
free(Tests);
}
void FreePuzzle(Puzzle *self) {
if (self == NULL) return;
if (self->Name != NULL) free(self->Name);
if (self->Description != NULL) free(self->Description);
if (self->InitialCode != NULL) free(self->InitialCode);
FreeTests(self->Tests, self->TestC);
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) {
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
SubmitionResult *TryEvaluateUserCode(WrenVM *vm, char *script, SubmitionResult *res) {
WrenInterpretResult result = wrenInterpret(vm, "main", script);
res->result = RESULT_OK;
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
res->result = RESULT_COMPTIME_ERR;
return res;
case WREN_RESULT_RUNTIME_ERROR:
res->result = RESULT_RUNTIME_ERR;
return res;
default: break;
}
return res;
}
// Frees the vm. Does not modify the self address.
SubmitionResult *PrepareResult(SubmitionResult *self,
WrenVM *vm,
enum PUZZLE_TEST_RESULT result) {
if (vm == NULL || self == NULL) return self;
self->result = result;
self->stdout = GetMachineOutput(vm);
InterpreterFreeMachine(vm);
return self;
}
SubmitionResult *SubmitSolution(Puzzle *self, char *script) {
if (self == NULL || script == NULL) return NULL;
SubmitionResult *res = malloc(sizeof(SubmitionResult));
WrenVM *vm = InterpreterPrepareMachine();
if (vm == NULL) {
res->result = RESULT_INIT_ERROR;
return res;
}
res = TryEvaluateUserCode(vm, script, res);
if (res->result != RESULT_OK) {
return PrepareResult(res, vm, res->result);
}
for (size_t i = 0; i < self->TestC; i++) {
enum PUZZLE_TEST_RESULT test_res = RunTest(&self->Tests[i], vm);
switch (test_res) {
case WREN_RESULT_COMPILE_ERROR:
return PrepareResult(res, vm, RESULT_COMPTIME_ERR);
case WREN_RESULT_RUNTIME_ERROR:
return PrepareResult(res, vm, RESULT_RUNTIME_ERR);
default: break;
}
}
return PrepareResult(res, vm, RESULT_OK);
}
void FreeSunmitionResult(SubmitionResult *res) {
if (res->stdout != NULL) free(res->stdout);
free(res);
}
|