Skip to content

Commit 5fc25d6

Browse files
committed
maze solver
1 parent 224d010 commit 5fc25d6

File tree

9 files changed

+158
-164
lines changed

9 files changed

+158
-164
lines changed

makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,32 @@ TARGET = bins
55
J != nproc
66
EMCC = emcc
77

8+
RAYLIB_CC ?= $(CC)
9+
RAYLIB_DIR = vendor/raylib/src
10+
811
linux: .dummy
912
$(PRE) make -Bj$(J) -f tool/core.mak $(TARGET) OS=LINUX CC="$(CC)" EXE= TEST_LUA="$(TEST_LUA)"
1013

11-
RAYLIB_DIR = vendor/raylib/src
1214
MAC_RAYLIB_OBJS = $(RAYLIB_DIR)/rcore.o $(RAYLIB_DIR)/rglfw.o $(RAYLIB_DIR)/rshapes.o $(RAYLIB_DIR)/rtextures.o $(RAYLIB_DIR)/rtext.o $(RAYLIB_DIR)/rmodels.o $(RAYLIB_DIR)/raudio.o $(RAYLIB_DIR)/utils.o
1315

1416
mac: .dummy
15-
$(PRE) make -Bj$(J) -C vendor/raylib/src CC="$(CC)" LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -DPLATFORM_DESKTOP" PLATFORM=PLATFORM_DESKTOP
17+
$(PRE) make -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -DPLATFORM_DESKTOP" PLATFORM=PLATFORM_DESKTOP
1618
$(PRE) make -Bj$(J) -f tool/core.mak $(TARGET) OS=MAC CC="$(CC)" EXE= TEST_LUA="$(TEST_LUA)" CFLAGS="-DVM_USE_RAYLIB -DVM_NO_GC $(CFLAGS)" LDFLAGS="$(MAC_RAYLIB_OBJS) -framework Cocoa -framework OpenGL -framework IOKit $(LDFLAGS)"
1719

1820
windows: .dummy
1921
$(PRE) make -Bj$(J) -f tool/core.mak $(TARGET) OS=WINDOWS CC="$(CC)" EXE=.exe TEST_LUA="$(TEST_LUA)"
2022

2123
freebsd: .dummy
22-
$(PRE) gmake -Bj$(J) -C vendor/raylib/src CC="$(CC)" LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -DPLATFORM_DESKTOP" PLATFORM=PLATFORM_DESKTOP
24+
$(PRE) gmake -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -DPLATFORM_DESKTOP" PLATFORM=PLATFORM_DESKTOP
2325
$(PRE) gmake -Bj$(J) -f tool/core.mak $(TARGET) OS=FREEBSD CC="$(CC)" EXE= TEST_LUA="$(TEST_LUA)" CFLAGS="-DVM_USE_RAYLIB $(CFLAGS)" LDFLAGS="-L/usr/local/lib vendor/raylib/src/libraylib.a -lOpenGL -lm -lpthread $(LDFLAGS)"
2426

27+
WEB_RAYLIB_OBJS = $(RAYLIB_DIR)/rcore.o $(RAYLIB_DIR)/rshapes.o $(RAYLIB_DIR)/rtextures.o $(RAYLIB_DIR)/rtext.o $(RAYLIB_DIR)/rmodels.o $(RAYLIB_DIR)/raudio.o $(RAYLIB_DIR)/utils.o
28+
2529
web: .dummy
26-
$(PRE) make -Bj$(J) CC=$(EMCC) -C vendor/raylib/src LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -D_DEFAULT_SOURCE -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2" PLATFORM=PLATFORM_WEB
30+
$(PRE) make -Bj$(J) CC=$(EMCC) -C vendor/raylib/src LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -D_DEFAULT_SOURCE -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2" PLATFORM=PLATFORM_WEB $(WEB_RAYLIB_OBJS:$(RAYLIB_DIR)/%=%)
2731
$(PRE) make -Bj$(J) CC=$(EMCC) -f tool/core.mak build/bin/minivm.mjs OS=LINUX EXE=.mjs \
28-
CFLAGS='-fPIC -DVM_USE_RAYLIB -DNDEBUG' \
29-
LDFLAGS='--embed-file test@test -s ASYNCIFY=1 -s BINARYEN_ASYNC_COMPILATION=0 -s EXPORTED_RUNTIME_METHODS=['FS'] -s STACK_SIZE=16mb -s USE_GLFW=3 -s SINGLE_FILE=1 vendor/raylib/src/libraylib.a -s MALLOC=mimalloc -s ENVIRONMENT=web -s ALLOW_MEMORY_GROWTH=1'
32+
CFLAGS='-fPIC -DVM_USE_RAYLIB -DNDEBUG -DVM_NO_GC' \
33+
LDFLAGS='--embed-file test@test -lglfw -lGL -s ASYNCIFY=1 -s BINARYEN_ASYNC_COMPILATION=0 -s EXPORTED_RUNTIME_METHODS=['FS'] -s STACK_SIZE=16mb -s USE_GLFW=3 -s SINGLE_FILE=1 $(WEB_RAYLIB_OBJS) -s ENVIRONMENT=web -s ALLOW_MEMORY_GROWTH=1'
3034
mv build/bin/minivm.mjs web/minivm.js
3135

3236
web-raw: .dummy

test/app/maze.lua

Lines changed: 114 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@ local gui = vm.import("test/app/gui.lua")
33

44
local debug = false
55

6-
local s = 20
6+
local s = 200
77

88
local xs = 2 * s + 3
99
local ys = 2 * s + 3
1010

1111
local grid = gui.Grid.new(xs, ys)
1212

13+
local solved = {}
14+
15+
for i=1, s do
16+
solved[i] = {}
17+
for j=1, s do
18+
solved[i][j] = false
19+
end
20+
end
21+
22+
local colors = {
23+
solved = gui.Rectangle.GREEN,
24+
unsolved = gui.Rectangle.WHITE,
25+
}
26+
1327
local dir = {
1428
root = 0
1529
nx = 1,
@@ -33,16 +47,41 @@ for x=1, s do
3347
end
3448
end
3549

36-
local seed = {
37-
seed1 = 0
50+
local vars = {
51+
cur = 1,
52+
last = 0,
53+
seed = 0
3854
}
3955

40-
local function random(n)
41-
seed.seed1 = (3301 * seed.seed1 + 4993) % 6121
42-
return (seed.seed1) % n
56+
local function random4()
57+
return math.randint() % 4
58+
end
59+
60+
local function dir_x(d)
61+
if d == dir.px then
62+
return 1
63+
elseif d == dir.nx then
64+
return -1
65+
else
66+
return 0
67+
end
68+
end
69+
70+
local function dir_y(d)
71+
if d == dir.py then
72+
return 1
73+
elseif d == dir.ny then
74+
return -1
75+
else
76+
return 0
77+
end
4378
end
4479

4580
local function frame()
81+
if vars.cur == vars.last then
82+
return nil
83+
end
84+
vars.last = vars.cur
4685
for x=1, xs do
4786
for y=1, ys do
4887
grid[x][y] = gui.Rectangle.BLACK
@@ -54,77 +93,122 @@ local function frame()
5493
local px = x*2 + 1
5594
local py = y*2 + 1
5695
local ent = maze[x][y]
57-
grid[px][py] = gui.Rectangle.ORANGE
96+
local c = if not solved[x] or not solved[x][y] then
97+
colors.unsolved
98+
else
99+
colors.solved
100+
end
101+
grid[px][py] = c
58102
if ent == dir.root then
59-
grid[px][py] = gui.Rectangle.RED
103+
grid[px][py] = c
60104
end
61105
if ent == dir.nx then
62-
grid[px-1][py] = gui.Rectangle.YELLOW
106+
grid[px-1][py] = c
63107
end
64108
if ent == dir.px then
65-
grid[px+1][py] = gui.Rectangle.YELLOW
109+
grid[px+1][py] = c
66110
end
67111
if ent == dir.ny then
68-
grid[px][py-1] = gui.Rectangle.YELLOW
112+
grid[px][py-1] = c
69113
end
70114
if ent == dir.py then
71-
grid[px][py+1] = gui.Rectangle.YELLOW
115+
grid[px][py+1] = c
72116
end
73117
end
74118
end
75119
end
76120

77-
local function maze_iter()
78-
for x=1, #maze do
79-
for y=1, #maze[x] do
121+
local function walk(x, y)
122+
local v = true
123+
solved[x][y] = v
124+
local run = true
125+
while run do
126+
local val = solved[x][y]
127+
local ent = maze[x][y]
128+
if ent == dir.root or ent == v then
129+
run = false
130+
end
131+
x = x + dir_x(ent)
132+
y = y + dir_y(ent)
133+
local n = solved[x][y]
134+
solved[x][y] = v
135+
if n then
136+
v = false
137+
end
138+
end
139+
end
140+
141+
local points = {
142+
start = {1, 1},
143+
stop = {s, s},
144+
}
145+
146+
local function rep(n)
147+
return function()
148+
vars.cur = vars.cur + 1
149+
local x = 0
150+
local y = 0
151+
for xp=1, #maze do
152+
for yp=1, #maze[xp] do
153+
if maze[xp][yp] == dir.root then
154+
x = xp
155+
y = yp
156+
end
157+
end
158+
end
159+
for i=1, n do
80160
local ent = maze[x][y]
81161
if ent == dir.root then
82162
local more = true
83163
while more do
84-
local v = random(4) + 1
164+
local v = random4() + 1
85165
maze[x][y] = v
86166
if v == dir.nx and x ~= 1 then
87167
maze[x-1][y] = dir.root
168+
x = x - 1
88169
more = false
89170
end
90171
if v == dir.px and x ~= #maze then
91172
maze[x+1][y] = dir.root
173+
x = x + 1
92174
more = false
93175
end
94176
if v == dir.ny and y ~= 1 then
95177
maze[x][y-1] = dir.root
178+
y = y - 1
96179
more = false
97180
end
98181
if v == dir.py and y ~= #maze[x] then
99182
maze[x][y+1] = dir.root
183+
y = y + 1
100184
more = false
101185
end
102186
end
103187
end
104188
end
105-
end
106-
end
107-
108-
local function rep(n)
109-
return function()
110-
for i=1, n do
111-
maze_iter()
189+
190+
for i=1, s do
191+
for j=1, s do
192+
solved[i][j] = nil
193+
end
112194
end
195+
196+
walk(points.start[1], points.start[2])
197+
walk(points.stop[1], points.stop[2])
113198
end
114199
end
115200

116201
local code = gui.Code.new(frame)
117202

203+
local redo = rep(s * s * 50)
204+
205+
redo()
206+
118207
draw = {}
119208
draw.code = code
120209
draw.grid = grid
121210
draw.keys = {}
122-
draw.keys.one = gui.Key.pressed("ONE", rep(1))
123-
draw.keys.two = gui.Key.pressed("TWO", rep(8))
124-
draw.keys.three = gui.Key.pressed("THREE", rep(64))
125-
draw.keys.four = gui.Key.pressed("FOUR", rep(512))
126-
draw.keys.five = gui.Key.pressed("FIVE", rep(2048))
127-
draw.keys.six = gui.Key.pressed("SIX", rep(16386))
128-
draw.window = gui.Window.new(600, 600)
211+
draw.keys.n = gui.Key.pressed("N", redo)
212+
draw.window = gui.Window.new(800, 800)
129213

130214
app()

vendor/bdwgc

Submodule bdwgc updated from 6992ccf to 09fc015

vm/backend/backend.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99
#define CONCAT3_2(x, y, z) x ## _ ## y ## _ ## z
1010
#define CONCAT3(x, y) CONCAT3_2(x, y)
1111

12-
#define VM_MATH_SWITCH 1
13-
14-
#if VM_MATH_SWITCH
1512
#define VM_INLINE inline __attribute__((always_inline))
16-
#else
17-
#define VM_INLINE inline
18-
#endif
1913

2014
#if 0
2115
#define VM_OPCODE_DEBUG(s) printf("%s\n", #s);
@@ -781,7 +775,7 @@ new_block_no_print:;
781775
if (v1.tag != VM_TAG_TAB) {
782776
return (vm_std_value_t) {
783777
.tag = VM_TAG_ERROR,
784-
.value.str = "can only index tables",
778+
.value.str = "can only set index on tables",
785779
};
786780
}
787781
vm_table_set(v1.value.table, v2.value, v3.value, v2.tag, v3.tag);
@@ -1109,7 +1103,6 @@ new_block_no_print:;
11091103
.key_val = v2.value,
11101104
};
11111105
if (v1.tag != VM_TAG_TAB) {
1112-
__builtin_trap();
11131106
return (vm_std_value_t) {
11141107
.tag = VM_TAG_ERROR,
11151108
.value.str = "can only index tables",

vm/backend/binop.inc

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,30 @@
1515
#define OP_F(x, y) OP(x, y)
1616
#endif
1717

18-
#if VM_MATH_SWITCH
1918
#define LABEL(X, Y) case COMBINE(X, Y):;
20-
#else
21-
#define LABEL(X, Y) LABEL(X, Y):;
22-
#endif
2319

2420
#define COMBINE(x, y) ((x)*VM_TAG_MAX + (y))
2521

26-
#if !VM_MATH_SWITCH
27-
static void *binop_table[COMBINE(VM_TAG_MAX, VM_TAG_MAX)] = {
28-
[COMBINE(VM_TAG_I8, VM_TAG_I8)] = &&LABEL(VM_TAG_I8, VM_TAG_I8),
29-
[COMBINE(VM_TAG_I8, VM_TAG_I16)] = &&LABEL(VM_TAG_I8, VM_TAG_I16),
30-
[COMBINE(VM_TAG_I8, VM_TAG_I32)] = &&LABEL(VM_TAG_I8, VM_TAG_I32),
31-
[COMBINE(VM_TAG_I8, VM_TAG_I64)] = &&LABEL(VM_TAG_I8, VM_TAG_I64),
32-
[COMBINE(VM_TAG_I8, VM_TAG_F32)] = &&LABEL(VM_TAG_I8, VM_TAG_F32),
33-
[COMBINE(VM_TAG_I8, VM_TAG_F64)] = &&LABEL(VM_TAG_I8, VM_TAG_F64),
34-
35-
[COMBINE(VM_TAG_I16, VM_TAG_I8)] = &&LABEL(VM_TAG_I16, VM_TAG_I8),
36-
[COMBINE(VM_TAG_I16, VM_TAG_I16)] = &&LABEL(VM_TAG_I16, VM_TAG_I16),
37-
[COMBINE(VM_TAG_I16, VM_TAG_I32)] = &&LABEL(VM_TAG_I16, VM_TAG_I32),
38-
[COMBINE(VM_TAG_I16, VM_TAG_I64)] = &&LABEL(VM_TAG_I16, VM_TAG_I64),
39-
[COMBINE(VM_TAG_I16, VM_TAG_F32)] = &&LABEL(VM_TAG_I16, VM_TAG_F32),
40-
[COMBINE(VM_TAG_I16, VM_TAG_F64)] = &&LABEL(VM_TAG_I16, VM_TAG_F64),
41-
42-
[COMBINE(VM_TAG_I32, VM_TAG_I8)] = &&LABEL(VM_TAG_I32, VM_TAG_I8),
43-
[COMBINE(VM_TAG_I32, VM_TAG_I16)] = &&LABEL(VM_TAG_I32, VM_TAG_I16),
44-
[COMBINE(VM_TAG_I32, VM_TAG_I32)] = &&LABEL(VM_TAG_I32, VM_TAG_I32),
45-
[COMBINE(VM_TAG_I32, VM_TAG_I64)] = &&LABEL(VM_TAG_I32, VM_TAG_I64),
46-
[COMBINE(VM_TAG_I32, VM_TAG_F32)] = &&LABEL(VM_TAG_I32, VM_TAG_F32),
47-
[COMBINE(VM_TAG_I32, VM_TAG_F64)] = &&LABEL(VM_TAG_I32, VM_TAG_F64),
48-
49-
[COMBINE(VM_TAG_I64, VM_TAG_I8)] = &&LABEL(VM_TAG_I64, VM_TAG_I8),
50-
[COMBINE(VM_TAG_I64, VM_TAG_I16)] = &&LABEL(VM_TAG_I64, VM_TAG_I16),
51-
[COMBINE(VM_TAG_I64, VM_TAG_I32)] = &&LABEL(VM_TAG_I64, VM_TAG_I32),
52-
[COMBINE(VM_TAG_I64, VM_TAG_I64)] = &&LABEL(VM_TAG_I64, VM_TAG_I64),
53-
[COMBINE(VM_TAG_I64, VM_TAG_F32)] = &&LABEL(VM_TAG_I64, VM_TAG_F32),
54-
[COMBINE(VM_TAG_I64, VM_TAG_F64)] = &&LABEL(VM_TAG_I64, VM_TAG_F64),
55-
56-
[COMBINE(VM_TAG_F32, VM_TAG_I8)] = &&LABEL(VM_TAG_F32, VM_TAG_I8),
57-
[COMBINE(VM_TAG_F32, VM_TAG_I16)] = &&LABEL(VM_TAG_F32, VM_TAG_I16),
58-
[COMBINE(VM_TAG_F32, VM_TAG_I32)] = &&LABEL(VM_TAG_F32, VM_TAG_I32),
59-
[COMBINE(VM_TAG_F32, VM_TAG_I64)] = &&LABEL(VM_TAG_F32, VM_TAG_I64),
60-
[COMBINE(VM_TAG_F32, VM_TAG_F32)] = &&LABEL(VM_TAG_F32, VM_TAG_F32),
61-
[COMBINE(VM_TAG_F32, VM_TAG_F64)] = &&LABEL(VM_TAG_F32, VM_TAG_F64),
62-
63-
[COMBINE(VM_TAG_F64, VM_TAG_I8)] = &&LABEL(VM_TAG_F64, VM_TAG_I8),
64-
[COMBINE(VM_TAG_F64, VM_TAG_I16)] = &&LABEL(VM_TAG_F64, VM_TAG_I16),
65-
[COMBINE(VM_TAG_F64, VM_TAG_I32)] = &&LABEL(VM_TAG_F64, VM_TAG_I32),
66-
[COMBINE(VM_TAG_F64, VM_TAG_I64)] = &&LABEL(VM_TAG_F64, VM_TAG_I64),
67-
[COMBINE(VM_TAG_F64, VM_TAG_F32)] = &&LABEL(VM_TAG_F64, VM_TAG_F32),
68-
[COMBINE(VM_TAG_F64, VM_TAG_F64)] = &&LABEL(VM_TAG_F64, VM_TAG_F64),
69-
};
70-
#endif
71-
72-
#if VM_MATH_SWITCH
7322
switch (COMBINE(v1.tag, v2.tag)) {
7423
default: {
7524
__builtin_trap();
7625
}
77-
#else
78-
goto *binop_table[COMBINE(v1.tag, v2.tag)];
79-
#endif
26+
LABEL(VM_TAG_TAB, VM_TAG_NIL)
27+
LABEL(VM_TAG_TAB, VM_TAG_BOOL)
28+
LABEL(VM_TAG_TAB, VM_TAG_I8)
29+
LABEL(VM_TAG_TAB, VM_TAG_I16)
30+
LABEL(VM_TAG_TAB, VM_TAG_I32)
31+
LABEL(VM_TAG_TAB, VM_TAG_I64)
32+
LABEL(VM_TAG_TAB, VM_TAG_F32)
33+
LABEL(VM_TAG_TAB, VM_TAG_F64)
34+
LABEL(VM_TAG_TAB, VM_TAG_STR)
35+
LABEL(VM_TAG_TAB, VM_TAG_CLOSURE)
36+
LABEL(VM_TAG_TAB, VM_TAG_FUN)
37+
LABEL(VM_TAG_TAB, VM_TAG_TAB)
38+
LABEL(VM_TAG_TAB, VM_TAG_FFI)
39+
{
40+
__builtin_trap();
41+
}
8042
LABEL(VM_TAG_I8, VM_TAG_I8) {
8143
WRITE (vm_std_value_t) {
8244
.tag = VM_TAG_I8,
@@ -329,9 +291,7 @@ LABEL(VM_TAG_F64, VM_TAG_F64) {
329291
};
330292
goto CONCAT(NAME, END);
331293
}
332-
#if VM_MATH_SWITCH
333294
}
334-
#endif
335295
CONCAT(NAME, END):;
336296

337297
#undef COMBINE

0 commit comments

Comments
 (0)