62 typedef typename compositor_t::function function_t;
64 const std::size_t width = 80;
65 const std::size_t height = 40;
67 std::vector<T> world(width * height, T(0));
71 symbol_table_t symbol_table;
72 symbol_table.add_constant(
"width" ,
static_cast<T
>(width ));
73 symbol_table.add_constant(
"height",
static_cast<T
>(height));
74 symbol_table.add_vector (
"world" , world);
75 symbol_table.add_package (io_package);
76 symbol_table.add_function(
"random", rnd01);
82 std::this_thread::sleep_for(
83 std::chrono::milliseconds(
static_cast<std::size_t
>(time_ms)));
91 printf(
"%s\033[H", full == T(1) ?
"" :
"\033[2J");
96 compositor_t compositor(symbol_table);
98 compositor.load_variables(
true);
99 compositor.load_vectors (
true);
106 " world[y * width + x]; "
110 function_t(
"set_point")
111 .vars(
"x",
"y",
"value")
115 " (x >= 0) and (y >= 0) and "
116 " (x < width) and (y < height) "
119 " world[y * width + x] := value; "
124 function_t(
"set_glider")
128 " set_point(x + 1, y, 1); "
129 " set_point(x + 2, y + 1, 1); "
130 " set_point(x, y + 2, 1); "
131 " set_point(x + 1, y + 2, 1); "
132 " set_point(x + 2, y + 2, 1); "
140 " for (var x := 0; x < width; x += 1) { print('-'); } "
143 " for (var y := 0; y < height; y += 1) "
146 " for (var x := 0; x < width; x += 1) "
148 " print( point(x,y) ? '*' : ' ' ); "
154 " for (var x := 0; x < width; x += 1) { print('-'); } "
162 " var next_world[world[]] := [0]; "
164 " for (var y := 0; y < height; y += 1) "
166 " for (var x := 0; x < width; x += 1) "
168 " var alive_count := point(x,y) ? -1 : 0; "
170 " for (var y1 := y - 1; y1 <= y + 1; y1 += 1) "
172 " var curr_y := (y1 + height) % height; "
174 " for (var x1 := x - 1; x1 <= x + 1; x1 += 1) "
176 " var curr_x := (x1 + width) % width; "
178 " if (point(curr_x,curr_y)) "
180 " alive_count += 1; "
185 " next_world[y * width + x] := "
188 " case alive_count == 2 and point(x,y) : 1; "
189 " case alive_count == 3 : 1; "
195 " world := next_world; "
198 const std::string game_of_life_driver =
199 " /* Setup the initial state of the world */ "
200 " for (var x := 0; x < width; x += 1) "
202 " for (var y := 0; y < height; y += 1) "
206 " case (random() < 0.10) : set_glider(x,y); "
207 " case (random() < 0.10) : set_point (x,y,1); "
212 " var num_generations := 1000; "
214 " for (var i := 0; i < num_generations; i += 1) "
216 " clear(i % 50 == 0); "
217 " println('Generation: ', i); "
223 expression_t expression;
224 expression.register_symbol_table(symbol_table);
227 parser.compile(game_of_life_driver,expression);