67 typedef typename compositor_t::function function_t;
69 const T width = T(40);
70 const T height = T(30);
77 std::vector<T> board (
static_cast<std::size_t
>(width * height), T(0));
78 std::vector<T> north_wall_board(
static_cast<std::size_t
>(width * height), T(0));
79 std::vector<T> west_wall_board (
static_cast<std::size_t
>(width * height), T(0));
85 symbol_table_t symbol_table;
87 symbol_table.add_constant(
"width" , width );
88 symbol_table.add_constant(
"height" , height );
89 symbol_table.add_constant(
"N" , N );
90 symbol_table.add_constant(
"E" , E );
91 symbol_table.add_constant(
"S" , S );
92 symbol_table.add_constant(
"W" , W );
94 symbol_table.add_vector (
"board" , board );
95 symbol_table.add_vector (
"north_wall_board" , north_wall_board);
96 symbol_table.add_vector (
"west_wall_board" , west_wall_board );
98 symbol_table.add_function(
"shuffle" ,
shuffle );
99 symbol_table.add_function(
"print_digit" ,
101 { printf(
" %d ",
static_cast<int>(v));
return 0; });
103 symbol_table.add_package (io_package);
105 compositor_t compositor(symbol_table);
107 compositor.load_variables(
true);
108 compositor.load_vectors (
true);
111 function_t(
"get_board")
114 (
" board[y * width + x]; " ));
117 function_t(
"set_board")
118 .vars(
"x",
"y",
"value")
120 (
" board[y * width + x] := value; " ));
123 function_t(
"get_nwall_board")
126 (
" north_wall_board[y * width + x]; " ));
129 function_t(
"get_wwall_board")
132 (
" west_wall_board[y * width + x]; " ));
135 function_t(
"set_nwall_board")
136 .vars(
"x",
"y",
"value")
138 (
" north_wall_board[y * width + x] := value; " ));
141 function_t(
"set_wwall_board")
142 .vars(
"x",
"y",
"value")
144 (
" west_wall_board[y * width + x] := value; " ));
147 function_t(
"display_maze")
150 " for (var x := 0; x < width; x += 1) "
157 " for (var y := 0; y < height; y += 1) "
159 " for (var x := 0; x < width; x += 1) "
161 " var segment := get_wwall_board(x,y) == W ? ' ' : '| '; "
167 " for (var x := 0; x < width; x += 1) "
169 " var segment := get_nwall_board(x,y) == N ? '+ ' : '+---'; "
180 function_t(
"generate_maze")
184 " set_board(x, y, 1); "
186 " var x_move [4] := { -1, 0, +1, 0 }; "
187 " var y_move [4] := { 0, +1, 0, -1 }; "
188 " var heading[4] := { W, N, E, S }; "
189 " var moves [4] := { 0, 1, 2, 3 }; "
193 " for (var i := 0; i < moves[]; i += 1) "
195 " var move := moves[i]; "
196 " var new_x := x + x_move[move]; "
197 " var new_y := y + y_move[move]; "
199 " if ((new_x < 0 ) or (new_y < 0 )) continue; "
200 " if ((new_x >= width) or (new_y >= height)) continue; "
202 " if (get_board(new_x, new_y) == 0) "
204 " if (heading[move] == N) { set_nwall_board(x, y, N); }; "
205 " if (heading[move] == W) { set_wwall_board(x, y, W); }; "
207 " if (heading[move] == S) { set_nwall_board(new_x, new_y, N); }; "
208 " if (heading[move] == E) { set_wwall_board(new_x, new_y, W); }; "
210 " generate_maze(new_x,new_y); "
215 const std::string maze_generator_program =
216 " generate_maze(0, 0); "
219 expression_t expression;
220 expression.register_symbol_table(symbol_table);
223 parser.compile(maze_generator_program,expression);