1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 :(scenario copy_literal)
16 def main [
17 1:num <- copy 23
18 ]
19 +run: {1: "number"} <- copy {23: "literal"}
20 +mem: storing 23 in location 1
21
22 :(scenario copy)
23 def main [
24 1:num <- copy 23
25 2:num <- copy 1:num
26 ]
27 +run: {2: "number"} <- copy {1: "number"}
28 +mem: location 1 is 23
29 +mem: storing 23 in location 2
30
31 :(scenario copy_multiple)
32 def main [
33 1:num, 2:num <- copy 23, 24
34 ]
35 +mem: storing 23 in location 1
36 +mem: storing 24 in location 2
37
38 :(before "End Types")
39
40
41 struct routine {
42 recipe_ordinal running_recipe;
43 int running_step_index;
44 routine(recipe_ordinal r) :running_recipe(r), running_step_index(0) {}
45 bool completed() const;
46 const vector<instruction>& steps() const;
47 };
48
49 :(before "End Globals")
50 routine* Current_routine = NULL;
51 :(before "End Reset")
52 Current_routine = NULL;
53
54 :(code)
55 void run(const recipe_ordinal r) {
56 routine rr(r);
57 Current_routine = &rr;
58 run_current_routine();
59 Current_routine = NULL;
60 }
61
62 void run_current_routine() {
63 while (should_continue_running(Current_routine)) {
64 ¦
65 ¦ if (current_instruction().is_label) { ++current_step_index(); continue; }
66 ¦ trace(Initial_callstack_depth + Trace_stream->callstack_depth, "run") << to_string(current_instruction()) << end();
67 ¦ if (get_or_insert(Memory, 0) != 0) {
68 ¦ ¦ raise << "something wrote to location 0; this should never happen\n" << end();
69 ¦ ¦ put(Memory, 0, 0);
70 ¦ }
71 ¦
72 ¦ vector<vector<double> > ingredients;
73 ¦ if (should_copy_ingredients()) {
74 ¦ ¦ for (int i = 0; i < SIZE(current_instruction().ingredients); ++i)
75 ¦ ¦ ¦ ingredients.push_back(read_memory(current_instruction().ingredients.at(i)));
76 ¦ }
77 ¦
78 ¦ vector<vector<double> > products;
79 ¦
80 ¦
81 ¦
82 ¦
83 ¦ bool write_products = true;
84 ¦ bool fall_through_to_next_instruction = true;
85 ¦ switch (current_instruction().operation) {
86 ¦ ¦
87 ¦ ¦ case COPY: {
88 ¦ ¦ ¦ copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
89 ¦ ¦ ¦ break;
90 ¦ ¦ }
91 ¦ ¦
92 ¦ ¦ default: {
93 ¦ ¦ ¦ cout << "not a primitive op: " << current_instruction().operation << '\n';
94 ¦ ¦ }
95 ¦ }
96 ¦
97 ¦ if (write_products) {
98 ¦ ¦ if (SIZE(products) < SIZE(current_instruction().products)) {
99 ¦ ¦ ¦ raise << SIZE(products) << " vs " << SIZE(current_instruction().products) << ": failed to write to all products in '" << to_original_string(current_instruction()) << "'\n" << end();
100 ¦ ¦ }
101 ¦ ¦ else {
102 ¦ ¦ ¦ for (int i = 0; i < SIZE(current_instruction().products); ++i) {
103 ¦ ¦ ¦ ¦
104 ¦ ¦ ¦ ¦ write_memory(current_instruction().products.at(i), products.at(i));
105 ¦ ¦ ¦ }
106 ¦ ¦ }
107 ¦ }
108 ¦
109 ¦ if (fall_through_to_next_instruction)
110 ¦ ¦ ++current_step_index();
111 }
112 stop_running_current_routine:;
113 }
114
115 :(code)
116
117 bool should_continue_running(const routine* current_routine) {
118 assert(current_routine == Current_routine);
119 return !Current_routine->completed();
120 }
121
122 bool should_copy_ingredients() {
123
124 return true;
125 }
126
127
128
129
130
131 int& current_step_index() {
132 return Current_routine->running_step_index;
133 }
134
135
136 recipe_ordinal currently_running_recipe() {
137 return Current_routine->running_recipe;
138 }
139
140
141 const string& current_recipe_name() {
142 return get(Recipe, Current_routine->running_recipe).name;
143 }
144
145
146 const recipe& current_recipe() {
147 return get(Recipe, Current_routine->running_recipe);
148 }
149
150
151 const instruction& current_instruction() {
152 return get(Recipe, Current_routine->running_recipe).steps.at(Current_routine->running_step_index);
153 }
154
155
156 bool routine::completed() const {
157 return running_step_index >= SIZE(get(Recipe, running_recipe).steps);
158 }
159
160
161 const vector<instruction>& routine::steps() const {
162 return get(Recipe, running_recipe).steps;
163 }
164
165
166
167
168 :(before "End Load Recipes")
169
170
171
172 load_file_or_directory("core.mu");
173
174
175
176
177 :(before "End Commandline Parsing")
178
179
180
181 if (argc > 1) {
182
183 ++argv;
184 --argc;
185 while (argc > 0) {
186 ¦
187 ¦ if (string(*argv) == "--") break;
188 ¦ if (starts_with(*argv, "--"))
189 ¦ ¦ cerr << "treating " << *argv << " as a file rather than an option\n";
190 ¦ load_file_or_directory(*argv);
191 ¦ --argc;
192 ¦ ++argv;
193 }
194 if (Run_tests) Recipe.erase(get(Recipe_ordinal, "main"));
195 }
196 transform_all();
197
198
199
200
201 if (trace_contains_errors()) return 1;
202 save_snapshots();
203
204
205
206 :(before "End Main")
207 if (!Run_tests && contains_key(Recipe_ordinal, "main") && contains_key(Recipe, get(Recipe_ordinal, "main"))) {
208
209 reset();
210 if (Start_tracing) {
211 ¦ Trace_stream = new trace_stream;
212 ¦ Save_trace = true;
213 }
214 trace(2, "run") << "=== Starting to run" << end();
215 assert(Num_calls_to_transform_all == 1);
216 run_main(argc, argv);
217 }
218 :(code)
219 void run_main(int argc, char* argv[]) {
220 recipe_ordinal r = get(Recipe_ordinal, "main");
221 if (r) run(r);
222 }
223
224
225
226 :(before "End Globals")
227 bool Start_tracing = false;
228 :(before "End Commandline Options(*arg)")
229 else if (is_equal(*arg, "--trace")) {
230 Start_tracing = true;
231 }
232
233 :(code)
234 void cleanup_main() {
235 if (Save_trace && Trace_stream) {
236 ¦ cerr << "writing trace to 'last_run'\n";
237 ¦ ofstream fout("last_run");
238 ¦ fout << Trace_stream->readable_contents("");
239 ¦ fout.close();
240 }
241 if (Trace_stream) delete Trace_stream, Trace_stream = NULL;
242 }
243 :(before "End One-time Setup")
244 atexit(cleanup_main);
245
246 :(code)
247 void load_file_or_directory(string filename) {
248 if (is_directory(filename)) {
249 ¦ load_all(filename);
250 ¦ return;
251 }
252 ifstream fin(filename.c_str());
253 if (!fin) {
254 ¦ cerr << "no such file '" << filename << "'\n" << end();
255 ¦ return;
256 }
257 trace(9990, "load") << "=== " << filename << end();
258 load(fin);
259 fin.close();
260 }
261
262 bool is_directory(string path) {
263 struct stat info;
264 if (stat(path.c_str(), &info)) return false;
265 return info.st_mode & S_IFDIR;
266 }
267
268 void load_all(string dir) {
269 dirent** files;
270 int num_files = scandir(dir.c_str(), &files, NULL, alphasort);
271 for (int i = 0; i < num_files; ++i) {
272 ¦ string curr_file = files[i]->d_name;
273 ¦ if (isdigit(curr_file.at(0)))
274 ¦ ¦ load_file_or_directory(dir+'/'+curr_file);
275 ¦ free(files[i]);
276 ¦ files[i] = NULL;
277 }
278 free(files);
279 }
280 :(before "End Includes")
281 #include <dirent.h>
282 #include <sys/stat.h>
283
284
285
286 :(code)
287 vector<double> read_memory(reagent x) {
288
289 vector<double> result;
290 if (is_literal(x)) {
291 ¦ result.push_back(x.value);
292 ¦ return result;
293 }
294
295 int size = size_of(x);
296 for (int offset = 0; offset < size; ++offset) {
297 ¦ double val = get_or_insert(Memory, x.value+offset);
298 ¦ trace(9999, "mem") << "location " << x.value+offset << " is " << no_scientific(val) << end();
299 ¦ result.push_back(val);
300 }
301 return result;
302 }
303
304 void write_memory(reagent x, const vector<double>& data) {
305 assert(Current_routine);
306
307 if (!x.type) {
308 ¦ raise << "can't write to '" << to_string(x) << "'; no type\n" << end();
309 ¦ return;
310 }
311 if (is_dummy(x)) return;
312 if (is_literal(x)) return;
313
314 if (x.value == 0) {
315 ¦ raise << "can't write to location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
316 ¦ return;
317 }
318 if (size_mismatch(x, data)) {
319 ¦ raise << maybe(current_recipe_name()) << "size mismatch in storing to '" << x.original_string << "' (" << size_of(x) << " vs " << SIZE(data) << ") at '" << to_original_string(current_instruction()) << "'\n" << end();
320 ¦ return;
321 }
322
323 for (int offset = 0; offset < SIZE(data); ++offset) {
324 ¦ assert(x.value+offset > 0);
325 ¦ trace(9999, "mem") << "storing " << no_scientific(data.at(offset)) << " in location " << x.value+offset << end();
326 ¦ put(Memory, x.value+offset, data.at(offset));
327 }
328 }
329
330 :(code)
331 int size_of(const reagent& r) {
332 if (!r.type) return 0;
333
334 return size_of(r.type);
335 }
336 int size_of(const type_tree* type) {
337 if (!type) return 0;
338 if (type->atom) {
339 ¦ if (type->value == -1) return 1;
340 ¦ if (type->value == 0) return 1;
341 ¦
342 }
343 else {
344 ¦ if (!type->left->atom) {
345 ¦ ¦ raise << "invalid type " << to_string(type) << '\n' << end();
346 ¦ ¦ return 0;
347 ¦ }
348 ¦ if (type->left->value == get(Type_ordinal, "address")) return 1;
349 ¦
350 }
351
352 return 1;
353 }
354
355 bool size_mismatch(const reagent& x, const vector<double>& data) {
356 if (!x.type) return true;
357
358
359 return size_of(x) != SIZE(data);
360 }
361
362 bool is_literal(const reagent& r) {
363 return is_literal(r.type);
364 }
365 bool is_literal(const type_tree* type) {
366 if (!type) return false;
367 if (!type->atom) return false;
368 return type->value == 0;
369 }
370
371 bool scalar(const vector<int>& x) {
372 return SIZE(x) == 1;
373 }
374 bool scalar(const vector<double>& x) {
375 return SIZE(x) == 1;
376 }
377
378
379 void run(const string& form) {
380 vector<recipe_ordinal> tmp = load(form);
381 transform_all();
382 if (tmp.empty()) return;
383 if (trace_contains_errors()) return;
384
385
386 if (contains_key(Recipe, get(Recipe_ordinal, "main")))
387 ¦ run(get(Recipe_ordinal, "main"));
388 else
389 ¦ run(tmp.front());
390 }
391
392 :(scenario run_label)
393 def main [
394 +foo
395 1:num <- copy 23
396 2:num <- copy 1:num
397 ]
398 +run: {1: "number"} <- copy {23: "literal"}
399 +run: {2: "number"} <- copy {1: "number"}
400 -run: +foo
401
402 :(scenario run_dummy)
403 def main [
404 _ <- copy 0
405 ]
406 +run: _ <- copy {0: "literal"}
407
408 :(scenario write_to_0_disallowed)
409 % Hide_errors = true;
410 def main [
411 0:num <- copy 34
412 ]
413 -mem: storing 34 in location 0
414
415
416
417
418 :(scenario comma_without_space)
419 def main [
420 1:num, 2:num <- copy 2,2
421 ]
422 +mem: storing 2 in location 1
423
424 :(scenario space_without_comma)
425 def main [
426 1:num, 2:num <- copy 2 2
427 ]
428 +mem: storing 2 in location 1
429
430 :(scenario comma_before_space)
431 def main [
432 1:num, 2:num <- copy 2, 2
433 ]
434 +mem: storing 2 in location 1
435
436 :(scenario comma_after_space)
437 def main [
438 1:num, 2:num <- copy 2 ,2
439 ]
440 +mem: storing 2 in location 1
441
442
443
444
445 :(before "End Globals")
446 bool Run_profiler = false;
447
448
449
450
451
452
453 map<recipe_ordinal, int> Instructions_running;
454 :(before "End Commandline Options(*arg)")
455 else if (is_equal(*arg, "--profile")) {
456 Run_profiler = true;
457 }
458 :(after "Running One Instruction")
459 if (Run_profiler) Instructions_running[currently_running_recipe()]++;
460 :(before "End One-time Setup")
461 atexit(dump_profile);
462 :(code)
463 void dump_profile() {
464 if (!Run_profiler) return;
465 if (Run_tests) {
466 ¦ cerr << "It's not a good idea to profile a run with tests, since tests can create conflicting recipes and mislead you. To try it anyway, comment out this check in the code.\n";
467 ¦ return;
468 }
469 ofstream fout;
470 fout.open("profile.instructions");
471 if (fout) {
472 ¦ for (map<recipe_ordinal, int>::iterator p = Instructions_running.begin(); p != Instructions_running.end(); ++p) {
473 ¦ ¦ fout << std::setw(9) << p->second << ' ' << header_label(p->first) << '\n';
474 ¦ }
475 }
476 fout.close();
477
478 }
479
480
481 string header_label(const recipe_ordinal r) {
482 return get(Recipe, r).name;
483 }