1 //: Allow Mu programs to log facts just like we've been doing in C++ so far.
  2 
  3 :(scenario trace)
  4 def main [
  5   trace 1, [foo], [this is a trace in Mu]
  6 ]
  7 +foo: this is a trace in Mu
  8 
  9 :(before "End Primitive Recipe Declarations")
 10 TRACE,
 11 :(before "End Primitive Recipe Numbers")
 12 put(Recipe_ordinal, "trace", TRACE);
 13 :(before "End Primitive Recipe Checks")
 14 case TRACE: {
 15   if (SIZE(inst.ingredients) < 3) {
 16   ¦ raise << maybe(get(Recipe, r).name) << "'trace' takes three or more ingredients rather than '" << inst.original_string << "'\n" << end();
 17   ¦ break;
 18   }
 19   if (!is_mu_number(inst.ingredients.at(0))) {
 20   ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'trace' should be a number (depth), but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
 21   ¦ break;
 22   }
 23   if (!is_literal_text(inst.ingredients.at(1))) {
 24   ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'trace' should be a literal string (label), but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
 25   ¦ break;
 26   }
 27   break;
 28 }
 29 :(before "End Primitive Recipe Implementations")
 30 case TRACE: {
 31   int depth = ingredients.at(0).at(0);
 32   string label = current_instruction().ingredients.at(1).name;
 33   ostringstream out;
 34   for (int i = 2;  i < SIZE(current_instruction().ingredients);  ++i) {
 35   ¦ if (i > 2) out << ' ';
 36   ¦ out << inspect(current_instruction().ingredients.at(i), ingredients.at(i));
 37   }
 38   trace(depth, label) << out.str() << end();
 39   break;
 40 }
 41 
 42 //: simpler limited version of 'trace'
 43 
 44 :(before "End Primitive Recipe Declarations")
 45 STASH,
 46 :(before "End Primitive Recipe Numbers")
 47 put(Recipe_ordinal, "stash", STASH);
 48 :(before "End Primitive Recipe Checks")
 49 case STASH: {
 50   break;
 51 }
 52 :(before "End Primitive Recipe Implementations")
 53 case STASH: {
 54   ostringstream out;
 55   for (int i = 0;  i < SIZE(current_instruction().ingredients);  ++i) {
 56   ¦ if (i) out << ' ';
 57   ¦ out << inspect(current_instruction().ingredients.at(i), ingredients.at(i));
 58   }
 59   trace(2, "app") << out.str() << end();
 60   break;
 61 }
 62 
 63 :(scenario stash_literal_string)
 64 def main [
 65   stash [foo]
 66 ]
 67 +app: foo
 68 
 69 :(scenario stash_literal_number)
 70 def main [
 71   stash [foo:], 4
 72 ]
 73 +app: foo: 4
 74 
 75 :(scenario stash_number)
 76 def main [
 77   1:num <- copy 34
 78   stash [foo:], 1:num
 79 ]
 80 +app: foo: 34
 81 
 82 :(code)
 83 string inspect(const reagent& r, const vector<double>& data) {
 84   if (is_literal(r))
 85   ¦ return r.name;
 86   // End inspect Special-cases(r, data)
 87   ostringstream out;
 88   for (long long i = 0;  i < SIZE(data);  ++i) {
 89   ¦ if (i) out << ' ';
 90   ¦ out << no_scientific(data.at(i));
 91   }
 92   return out.str();
 93 }
 94 
 95 :(before "End Primitive Recipe Declarations")
 96 HIDE_ERRORS,
 97 :(before "End Primitive Recipe Numbers")
 98 put(Recipe_ordinal, "hide-errors", HIDE_ERRORS);
 99 :(before "End Primitive Recipe Checks")
100 case HIDE_ERRORS: {
101   break;
102 }
103 :(before "End Primitive Recipe Implementations")
104 case HIDE_ERRORS: {
105   Hide_errors = true;
106   break;
107 }
108 
109 :(before "End Primitive Recipe Declarations")
110 SHOW_ERRORS,
111 :(before "End Primitive Recipe Numbers")
112 put(Recipe_ordinal, "show-errors", SHOW_ERRORS);
113 :(before "End Primitive Recipe Checks")
114 case SHOW_ERRORS: {
115   break;
116 }
117 :(before "End Primitive Recipe Implementations")
118 case SHOW_ERRORS: {
119   Hide_errors = false;
120   break;
121 }
122 
123 :(before "End Primitive Recipe Declarations")
124 TRACE_UNTIL,
125 :(before "End Primitive Recipe Numbers")
126 put(Recipe_ordinal, "trace-until", TRACE_UNTIL);
127 :(before "End Primitive Recipe Checks")
128 case TRACE_UNTIL: {
129   break;
130 }
131 :(before "End Primitive Recipe Implementations")
132 case TRACE_UNTIL: {
133   if (Trace_stream) {
134   ¦ Trace_stream->collect_depth = ingredients.at(0).at(0);
135   }
136   break;
137 }
138 
139 :(before "End Primitive Recipe Declarations")
140 _DUMP_TRACE,
141 :(before "End Primitive Recipe Numbers")
142 put(Recipe_ordinal, "$dump-trace", _DUMP_TRACE);
143 :(before "End Primitive Recipe Checks")
144 case _DUMP_TRACE: {
145   break;
146 }
147 :(before "End Primitive Recipe Implementations")
148 case _DUMP_TRACE: {
149   if (ingredients.empty()) {
150   ¦ DUMP("");
151   }
152   else {
153   ¦ DUMP(current_instruction().ingredients.at(0).name);
154   }
155   break;
156 }
157 
158 :(before "End Primitive Recipe Declarations")
159 _CLEAR_TRACE,
160 :(before "End Primitive Recipe Numbers")
161 put(Recipe_ordinal, "$clear-trace", _CLEAR_TRACE);
162 :(before "End Primitive Recipe Checks")
163 case _CLEAR_TRACE: {
164   break;
165 }
166 :(before "End Primitive Recipe Implementations")
167 case _CLEAR_TRACE: {
168   if (Trace_stream) Trace_stream->past_lines.clear();
169   break;
170 }
171 
172 :(before "End Primitive Recipe Declarations")
173 _SAVE_TRACE,
174 :(before "End Primitive Recipe Numbers")
175 put(Recipe_ordinal, "$save-trace", _SAVE_TRACE);
176 :(before "End Primitive Recipe Checks")
177 case _SAVE_TRACE: {
178   break;
179 }
180 :(before "End Primitive Recipe Implementations")
181 case _SAVE_TRACE: {
182   if (Save_trace) {
183   ¦ ofstream fout("last_trace");
184   ¦ fout << Trace_stream->readable_contents("");
185   ¦ fout.close();
186   }
187   break;
188 }
189 
190 //:: 'cheating' by using the host system
191 
192 :(before "End Primitive Recipe Declarations")
193 _PRINT,
194 :(before "End Primitive Recipe Numbers")
195 put(Recipe_ordinal, "$print", _PRINT);
196 :(before "End Primitive Recipe Checks")
197 case _PRINT: {
198   break;
199 }
200 :(before "End Primitive Recipe Implementations")
201 case _PRINT: {
202   for (int i = 0;  i < SIZE(ingredients);  ++i) {
203   ¦ if (is_literal(current_instruction().ingredients.at(i))) {
204   ¦ ¦ trace(9998, "run") << "$print: " << current_instruction().ingredients.at(i).name << end();
205   ¦ ¦ if (!has_property(current_instruction().ingredients.at(i), "newline")) {
206   ¦ ¦ ¦ cout << current_instruction().ingredients.at(i).name;
207   ¦ ¦ }
208   ¦ ¦ // hack: '$print 10' prints '10', but '$print 10/newline' prints '\n'
209   ¦ ¦ // End $print 10/newline Special-cases
210   ¦ ¦ else {
211   ¦ ¦ ¦ cout << '\n';
212   ¦ ¦ }
213   ¦ }
214   ¦ // End $print Special-cases
215   ¦ else {
216   ¦ ¦ for (int j = 0;  j < SIZE(ingredients.at(i));  ++j) {
217   ¦ ¦ ¦ trace(9998, "run") << "$print: " << ingredients.at(i).at(j) << end();
218   ¦ ¦ ¦ if (j > 0) cout << " ";
219   ¦ ¦ ¦ cout << no_scientific(ingredients.at(i).at(j));
220   ¦ ¦ }
221   ¦ }
222   }
223   cout.flush();
224   break;
225 }
226 
227 :(before "End Primitive Recipe Declarations")
228 _EXIT,
229 :(before "End Primitive Recipe Numbers")
230 put(Recipe_ordinal, "$exit", _EXIT);
231 :(before "End Primitive Recipe Checks")
232 case _EXIT: {
233   break;
234 }
235 :(before "End Primitive Recipe Implementations")
236 case _EXIT: {
237   exit(0);
238   break;
239 }
240 
241 :(before "End Primitive Recipe Declarations")
242 _SYSTEM,
243 :(before "End Primitive Recipe Numbers")
244 put(Recipe_ordinal, "$system", _SYSTEM);
245 :(before "End Primitive Recipe Checks")
246 case _SYSTEM: {
247   if (SIZE(inst.ingredients) != 1) {
248   ¦ raise << maybe(get(Recipe, r).name) << "'$system' requires exactly one ingredient, but got '" << to_string(inst) << "'\n" << end();
249   ¦ break;
250   }
251   if (!is_literal_text(inst.ingredients.at(0))) {
252   ¦ raise << maybe(get(Recipe, r).name) << "ingredient to '$system' must be a literal text, but got '" << to_string(inst) << "'\n" << end();
253   }
254   break;
255 }
256 :(before "End Primitive Recipe Implementations")
257 case _SYSTEM: {
258   int status = system(current_instruction().ingredients.at(0).name.c_str());
259   products.resize(1);
260   products.at(0).push_back(status);
261   break;
262 }
263 
264 :(before "End Primitive Recipe Declarations")
265 _DUMP_MEMORY,
266 :(before "End Primitive Recipe Numbers")
267 put(Recipe_ordinal, "$dump-memory", _DUMP_MEMORY);
268 :(before "End Primitive Recipe Checks")
269 case _DUMP_MEMORY: {
270   break;
271 }
272 :(before "End Primitive Recipe Implementations")
273 case _DUMP_MEMORY: {
274   dump_memory();
275   break;
276 }
277 
278 //: In times of real extremis we need to create a whole new modality for debug
279 //: logs, independent of other changes to the screen or Trace_stream.
280 
281 :(before "End Globals")
282 ofstream LOG;
283 :(before "End One-time Setup")
284 //? LOG.open("log");
285 
286 :(before "End Primitive Recipe Declarations")
287 _LOG,
288 :(before "End Primitive Recipe Numbers")
289 put(Recipe_ordinal, "$log", _LOG);
290 :(before "End Primitive Recipe Checks")
291 case _LOG: {
292   break;
293 }
294 :(before "End Primitive Recipe Implementations")
295 case _LOG: {
296   ostringstream out;
297   for (int i = 0;  i < SIZE(current_instruction().ingredients);  ++i) {
298   ¦ out << inspect(current_instruction().ingredients.at(i), ingredients.at(i));
299   }
300   LOG << out.str() << '\n';
301   break;
302 }
303 
304 //: set a variable from within Mu code
305 //: useful for selectively tracing or printing after some point
306 :(before "End Globals")
307 bool Foo = false;
308 :(before "End Primitive Recipe Declarations")
309 _FOO,
310 :(before "End Primitive Recipe Numbers")
311 put(Recipe_ordinal, "$foo", _FOO);
312 :(before "End Primitive Recipe Checks")
313 case _FOO: {
314   break;
315 }
316 :(before "End Primitive Recipe Implementations")
317 case _FOO: {
318   Foo = true;
319   break;
320 }