about summary refs log tree commit diff stats
path: root/029tools.cc
blob: faab1eb0e589b491ad13ea9157438612f3b152f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//: Allow mu programs to log facts just like we've been doing in C++ so far.

:(scenario trace)
def main [
  trace 1, [foo], [this is a trace in mu]
]
+foo: this is a trace in mu

:(before "End Primitive Recipe Declarations")
TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "trace", TRACE);
:(before "End Primitive Recipe Checks")
case TRACE: {
  if (SIZE(inst.ingredients) < 3) {
    raise << maybe(get(Recipe, r).name) << "'trace' takes three or more ingredients rather than '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    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();
    break;
  }
  if (!is_literal_string(inst.ingredients.at(1))) {
    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();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case TRACE: {
  int depth = ingredients.at(0).at(0);
  string label = current_instruction().ingredients.at(1).name;
  ostringstream out;
  for (int i = 2; i < SIZE(current_instruction().ingredients); ++i) {
    out << print_mu(current_instruction().ingredients.at(i), ingredients.at(i));
  }
  trace(depth, label) << out.str() << end();
  break;
}

//: simpler limited version of 'trace'

:(before "End Primitive Recipe Declarations")
STASH,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "stash", STASH);
:(before "End Primitive Recipe Checks")
case STASH: {
  break;
}
:(before "End Primitive Recipe Implementations")
case STASH: {
  ostringstream out;
  for (int i = 0; i < SIZE(current_instruction().ingredients); ++i) {
    out << print_mu(current_instruction().ingredients.at(i), ingredients.at(i));
  }
  trace(2, "app") << out.str() << end();
  break;
}

:(scenario stash_literal_string)
def main [
  stash [foo]
]
+app: foo

:(scenario stash_literal_number)
def main [
  stash [foo:], 4
]
+app: foo: 4

:(scenario stash_number)
def main [
  1:number <- copy 34
  stash [foo:], 1:number
]
+app: foo: 34

:(code)
string print_mu(const reagent& r, const vector<double>& data) {
  if (is_literal(r))
    return r.name+' ';
  // End print Special-cases(r, data)
  ostringstream out;
  for (long long i = 0; i < SIZE(data); ++i)
    out << no_scientific(data.at(i)) << ' ';
  return out.str();
}

:(before "End Primitive Recipe Declarations")
HIDE_ERRORS,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "hide-errors", HIDE_ERRORS);
:(before "End Primitive Recipe Checks")
case HIDE_ERRORS: {
  break;
}
:(before "End Primitive Recipe Implementations")
case HIDE_ERRORS: {
  Hide_errors = true;
  break;
}

:(before "End Primitive Recipe Declarations")
SHOW_ERRORS,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "show-errors", SHOW_ERRORS);
:(before "End Primitive Recipe Checks")
case SHOW_ERRORS: {
  break;
}
:(before "End Primitive Recipe Implementations")
case SHOW_ERRORS: {
  Hide_errors = false;
  break;
}

:(before "End Primitive Recipe Declarations")
TRACE_UNTIL,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "trace-until", TRACE_UNTIL);
:(before "End Primitive Recipe Checks")
case TRACE_UNTIL: {
  break;
}
:(before "End Primitive Recipe Implementations")
case TRACE_UNTIL: {
  if (Trace_stream) {
    Trace_stream->collect_depth = ingredients.at(0).at(0);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
_DUMP_TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$dump-trace", _DUMP_TRACE);
:(before "End Primitive Recipe Checks")
case _DUMP_TRACE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _DUMP_TRACE: {
  if (ingredients.empty()) {
    DUMP("");
  }
  else {
    DUMP(current_instruction().ingredients.at(0).name);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
_CLEAR_TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$clear-trace", _CLEAR_TRACE);
:(before "End Primitive Recipe Checks")
case _CLEAR_TRACE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _CLEAR_TRACE: {
  if (Trace_stream) Trace_stream->past_lines.clear();
  break;
}

:(before "End Primitive Recipe Declarations")
_SAVE_TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$save-trace", _SAVE_TRACE);
:(before "End Primitive Recipe Checks")
case _SAVE_TRACE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _SAVE_TRACE: {
  if (!Trace_file.empty()) {
    ofstream fout((Trace_dir+Trace_file).c_str());
    fout << Trace_stream->readable_contents("");
    fout.close();
  }
  break;
}

//: assert: perform sanity checks at runtime

:(scenario assert)
% Hide_errors = true;  // '%' lines insert arbitrary C code into tests before calling 'run' with the lines below. Must be immediately after :(scenario) line.
def main [
  assert 0, [this is an assert in mu]
]
+error: this is an assert in mu

:(before "End Primitive Recipe Declarations")
ASSERT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "assert", ASSERT);
:(before "End Primitive Recipe Checks")
case ASSERT: {
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(get(Recipe, r).name) << "'assert' takes exactly two ingredients rather than '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_scalar(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "'assert' requires a boolean for its first ingredient, but got " << inst.ingredients.at(0).original_string << '\n' << end();
    break;
  }
  if (!is_literal_string(inst.ingredients.at(1))) {
    raise << maybe(get(Recipe, r).name) << "'assert' requires a literal string for its second ingredient, but got " << inst.ingredients.at(1).original_string << '\n' << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case ASSERT: {
  if (!ingredients.at(0).at(0)) {
    raise << current_instruction().ingredients.at(1).name << '\n' << end();
  }
  break;
}

//:: 'cheating' by using the host system

:(before "End Primitive Recipe Declarations")
_PRINT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$print", _PRINT);
:(before "End Primitive Recipe Checks")
case _PRINT: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _PRINT: {
  for (int i = 0; i < SIZE(ingredients); ++i) {
    if (is_literal(current_instruction().ingredients.at(i))) {
      trace(9998, "run") << "$print: " << current_instruction().ingredients.at(i).name << end();
      if (has_property(current_instruction().ingredients.at(i), "newline"))
        cout << '\n';
      else
        cout << current_instruction().ingredients.at(i).name;
    }
    else {
      for (int j = 0; j < SIZE(ingredients.at(i)); ++j) {
        trace(9998, "run") << "$print: " << ingredients.at(i).at(j) << end();
        if (j > 0) cout << " ";
        cout << no_scientific(ingredients.at(i).at(j));
      }
    }
  }
  cout.flush();
  break;
}

:(before "End Primitive Recipe Declarations")
_EXIT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$exit", _EXIT);
:(before "End Primitive Recipe Checks")
case _EXIT: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _EXIT: {
  exit(0);
  break;
}

:(before "End Primitive Recipe Declarations")
_SYSTEM,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$system", _SYSTEM);
:(before "End Primitive Recipe Checks")
case _SYSTEM: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'$system' requires exactly one ingredient, but got none\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case _SYSTEM: {
  int status = system(current_instruction().ingredients.at(0).name.c_str());
  products.resize(1);
  products.at(0).push_back(status);
  break;
}

:(before "End Primitive Recipe Declarations")
_DUMP_MEMORY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$dump-memory", _DUMP_MEMORY);
:(before "End Primitive Recipe Checks")
case _DUMP_MEMORY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _DUMP_MEMORY: {
  dump_memory();
  break;
}

//: In times of real extremis we need to create a whole new modality for debug
//: logs, independent of other changes to the screen or Trace_stream.

:(before "End Globals")
ofstream LOG;
:(before "End One-time Setup")
//? LOG.open("log");

:(before "End Primitive Recipe Declarations")
_LOG,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$log", _LOG);
:(before "End Primitive Recipe Checks")
case _LOG: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _LOG: {
  ostringstream out;
  for (int i = 0; i < SIZE(current_instruction().ingredients); ++i) {
    out << print_mu(current_instruction().ingredients.at(i), ingredients.at(i));
  }
  LOG << out.str() << '\n';
  break;
}

//: set a variable from within mu code
//: useful for selectively tracing or printing after some point
:(before "End Globals")
bool Foo = false;
:(before "End Primitive Recipe Declarations")
_FOO,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$foo", _FOO);
:(before "End Primitive Recipe Checks")
case _FOO: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _FOO: {
  Foo = true;
  break;
}
lass="w"> const status) {} void ui_room_member_nick_change(const char * const roomjid, const char * const old_nick, const char * const nick) {} void ui_room_nick_change(const char * const roomjid, const char * const nick) {} void ui_room_member_presence(const char * const roomjid, const char * const nick, const char * const show, const char * const status) {} void ui_room_show_occupants(const char * const roomjid) {} void ui_room_hide_occupants(const char * const roomjid) {} void ui_show_roster(void) {} void ui_hide_roster(void) {} void ui_roster_add(const char * const barejid, const char * const name) {} void ui_roster_remove(const char * const barejid) {} void ui_contact_already_in_group(const char * const contact, const char * const group) {} void ui_contact_not_in_group(const char * const contact, const char * const group) {} void ui_group_added(const char * const contact, const char * const group) {} void ui_group_removed(const char * const contact, const char * const group) {} void ui_chat_win_contact_online(PContact contact, Resource *resource, GDateTime *last_activity) {} void ui_chat_win_contact_offline(PContact contact, char *resource, char *status) {} gboolean ui_chat_win_exists(const char * const barejid) { return TRUE; } void ui_contact_offline(char *barejid, char *resource, char *status) {} void ui_handle_recipient_not_found(const char * const recipient, const char * const err_msg) { check_expected(recipient); check_expected(err_msg); } void ui_handle_recipient_error(const char * const recipient, const char * const err_msg) { check_expected(recipient); check_expected(err_msg); } void ui_handle_error(const char * const err_msg) { check_expected(err_msg); } void ui_clear_win_title(void) {} void ui_goodbye_title(void) {} void ui_handle_room_join_error(const char * const roomjid, const char * const err) {} void ui_handle_room_configuration(const char * const roomjid, DataForm *form) {} void ui_handle_room_configuration_form_error(const char * const roomjid, const char * const message) {} void ui_handle_room_config_submit_result(const char * const roomjid) {} void ui_handle_room_config_submit_result_error(const char * const roomjid, const char * const message) {} void ui_handle_room_affiliation_list_error(const char * const roomjid, const char * const affiliation, const char * const error) {} void ui_handle_room_affiliation_list(const char * const roomjid, const char * const affiliation, GSList *jids) {} void ui_handle_room_affiliation_set_error(const char * const roomjid, const char * const jid, const char * const affiliation, const char * const error) {} void ui_handle_room_role_set_error(const char * const roomjid, const char * const nick, const char * const role, const char * const error) {} void ui_handle_room_role_list_error(const char * const roomjid, const char * const role, const char * const error) {} void ui_handle_room_role_list(const char * const roomjid, const char * const role, GSList *nicks) {} void ui_handle_room_kick_error(const char * const roomjid, const char * const nick, const char * const error) {} void ui_show_form(ProfMucConfWin *confwin) {} void ui_show_form_field(ProfWin *window, DataForm *form, char *tag) {} void ui_show_form_help(ProfMucConfWin *confwin) {} void ui_show_form_field_help(ProfMucConfWin *confwin, char *tag) {} void ui_show_lines(ProfWin *window, const gchar** lines) {} void ui_redraw_all_room_rosters(void) {} void ui_show_all_room_rosters(void) {} void ui_hide_all_room_rosters(void) {} void ui_tidy_wins(void) {} void ui_prune_wins(void) {} gboolean ui_swap_wins(int source_win, int target_win) { return FALSE; } void ui_auto_away(void) {} void ui_end_auto_away(void) {} void ui_titlebar_presence(contact_presence_t presence) {} void ui_handle_login_account_success(ProfAccount *account) {} void ui_update_presence(const resource_presence_t resource_presence, const char * const message, const char * const show) {} void ui_about(void) {} void ui_statusbar_new(const int win) {} wint_t ui_get_char(char *input, int *size, int *result) { return 0; } void ui_input_clear(void) {} void ui_input_nonblocking(gboolean reset) {} void ui_replace_input(char *input, const char * const new_input, int *size) {} void ui_invalid_command_usage(const char * const usage, void (*setting_func)(void)) {} void ui_create_xmlconsole_win(void) {} gboolean ui_xmlconsole_exists(void) { return FALSE; } void ui_open_xmlconsole_win(void) {} gboolean ui_win_has_unsaved_form(int num) { return FALSE; } // console window actions void cons_show(const char * const msg, ...) { va_list args; va_start(args, msg); vsnprintf(output, sizeof(output), msg, args); check_expected(output); va_end(args); } void cons_about(void) {} void cons_help(void) {} void cons_navigation_help(void) {} void cons_prefs(void) {} void cons_show_ui_prefs(void) {} void cons_show_desktop_prefs(void) {} void cons_show_chat_prefs(void) {} void cons_show_log_prefs(void) {} void cons_show_presence_prefs(void) {} void cons_show_connection_prefs(void) {} void cons_show_otr_prefs(void) {} void cons_show_account(ProfAccount *account) { check_expected(account); } void cons_debug(const char * const msg, ...) {} void cons_show_time(void) {} void cons_show_word(const char * const word) {} void cons_show_error(const char * const cmd, ...) { va_list args; va_start(args, cmd); vsnprintf(output, sizeof(output), cmd, args); check_expected(output); va_end(args); } void cons_show_contacts(GSList * list) {} void cons_show_roster(GSList * list) { check_expected(list); } void cons_show_roster_group(const char * const group, GSList * list) {} void cons_show_wins(void) {} void cons_show_status(const char * const barejid) {} void cons_show_info(PContact pcontact) {} void cons_show_caps(const char * const fulljid, resource_presence_t presence) {} void cons_show_themes(GSList *themes) {} void cons_show_aliases(GList *aliases) { check_expected(aliases); } void cons_show_login_success(ProfAccount *account) {} void cons_show_software_version(const char * const jid, const char * const presence, const char * const name, const char * const version, const char * const os) {} void cons_show_account_list(gchar **accounts) { check_expected(accounts); } void cons_show_room_list(GSList *room, const char * const conference_node) {} void cons_show_bookmarks(const GList *list) { check_expected(list); } void cons_show_disco_items(GSList *items, const char * const jid) {} void cons_show_disco_info(const char *from, GSList *identities, GSList *features) {} void cons_show_room_invite(const char * const invitor, const char * const room, const char * const reason) {} void cons_check_version(gboolean not_available_msg) {} void cons_show_typing(const char * const barejid) {} void cons_show_incoming_message(const char * const short_from, const int win_index) {} void cons_show_room_invites(GSList *invites) {} void cons_show_received_subs(void) {} void cons_show_sent_subs(void) {} void cons_alert(void) {} void cons_theme_setting(void) {} void cons_privileges_setting(void) {} void cons_beep_setting(void) {} void cons_flash_setting(void) {} void cons_splash_setting(void) {} void cons_vercheck_setting(void) {} void cons_resource_setting(void) {} void cons_occupants_setting(void) {} void cons_roster_setting(void) {} void cons_presence_setting(void) {} void cons_wrap_setting(void) {} void cons_time_setting(void) {} void cons_mouse_setting(void) {} void cons_statuses_setting(void) {} void cons_titlebar_setting(void) {} void cons_notify_setting(void) {} void cons_states_setting(void) {} void cons_outtype_setting(void) {} void cons_intype_setting(void) {} void cons_gone_setting(void) {} void cons_history_setting(void) {} void cons_log_setting(void) {} void cons_chlog_setting(void) {} void cons_grlog_setting(void) {} void cons_autoaway_setting(void) {} void cons_reconnect_setting(void) {} void cons_autoping_setting(void) {} void cons_priority_setting(void) {} void cons_autoconnect_setting(void) {} void cons_inpblock_setting(void) {} void cons_show_contact_online(PContact contact, Resource *resource, GDateTime *last_activity) { check_expected(contact); check_expected(resource); check_expected(last_activity); } void cons_show_contact_offline(PContact contact, char *resource, char *status) {} void cons_theme_colours(void) {} // roster window void rosterwin_roster(void) {} // occupants window void occupantswin_occupants(const char * const room) {} // desktop notifier actions void notifier_uninit(void) {} void notify_typing(const char * const handle) {} void notify_message(const char * const handle, int win, const char * const text) {} void notify_room_message(const char * const handle, const char * const room, int win, const char * const text) {} void notify_remind(void) {} void notify_invite(const char * const from, const char * const room, const char * const reason) {} void notify_subscription(const char * const from) {}