62 typedef typename compositor_t::function function_t;
64 const std::size_t width = 60;
65 const std::size_t height = 30;
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);
77 symbol_table.add_function(
"sleep" ,
80 std::this_thread::sleep_for(
81 std::chrono::milliseconds(
static_cast<std::size_t
>(time)));
85 compositor_t compositor(symbol_table);
87 compositor.load_variables(
true);
88 compositor.load_vectors (
true);
96 " world[y * width + x]; "
100 function_t(
"set_point")
101 .vars(
"x",
"y",
"value")
104 " world[y * width + x] := value; "
112 " for (var x := 0; x < width; x += 1) { print('-'); } "
115 " for (var y := 0; y < height; y += 1) "
118 " for (var x := 0; x < width; x += 1) "
120 " print( point(x,y) ? '*' : ' ' ); "
126 " for (var x := 0; x < width; x += 1) { print('-'); } "
134 " var next_world[world[]] := [0]; "
136 " for (var y := 0; y < height; y += 1) "
138 " for (var x := 0; x < width; x += 1) "
140 " var alive_count := point(x,y) ? -1 : 0; "
142 " for (var y1 := y - 1; y1 <= y + 1; y1 += 1) "
144 " var curr_y := (y1 + height) % height; "
146 " for (var x1 := x - 1; x1 <= x + 1; x1 += 1) "
148 " var curr_x := (x1 + width) % width; "
150 " if (point(curr_x,curr_y)) "
152 " alive_count += 1; "
157 " next_world[y * width + x] := "
160 " case alive_count == 2 and point(x,y) : 1; "
161 " case alive_count == 3 : 1; "
167 " world := next_world; "
170 const std::string game_of_life_driver =
171 " /* Randomly setup the initial state of the world */ "
172 " for (var x := 0; x < width; x += 1) "
174 " for (var y := 0; y < height; y += 1) "
176 " set_point(x,y, (random() < 0.15) ? 1 : 0); "
180 " var num_generations := 200; "
182 " for (var i := 0; i < num_generations; i += 1) "
184 " println('Generation: ', i); "
190 expression_t expression;
191 expression.register_symbol_table(symbol_table);
194 parser.compile(game_of_life_driver,expression);