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
|
//: Everything this project/binary supports.
//: This should give you a sense for what to look forward to in later layers.
:(before "End Commandline Parsing")
if (argc <= 1 || is_equal(argv[1], "--help")) {
//: this is the functionality later layers will provide
// currently no automated tests for commandline arg parsing
if (argc <= 1) {
cerr << "Please provide a Mu program to run.\n"
<< "\n";
}
cerr << "Usage:\n"
<< " mu [options] [test] [files]\n"
<< "or:\n"
<< " mu [options] [test] [files] -- [ingredients for function/recipe 'main']\n"
<< "Square brackets surround optional arguments.\n"
<< "\n"
<< "Examples:\n"
<< " To load files and run 'main':\n"
<< " mu file1.mu file2.mu ...\n"
<< " To run 'main' and dump a trace of all operations at the end:\n"
<< " mu --trace file1.mu file2.mu ...\n"
<< " To run all tests:\n"
<< " mu test\n"
<< " To load files and then run all tests:\n"
<< " mu test file1.mu file2.mu ...\n"
<< " To run a single Mu scenario:\n"
<< " mu test file1.mu file2.mu ... scenario\n"
<< " To run a single Mu scenario and dump a trace at the end:\n"
<< " mu --trace test file1.mu file2.mu ... scenario\n"
<< " To load files and run only the tests in explicitly loaded files (for apps):\n"
<< " mu --test-only-app test file1.mu file2.mu ...\n"
<< " To load all files with a numeric prefix in a directory:\n"
<< " mu directory1 directory2 ...\n"
<< " You can test directories just like files.\n"
<< " mu test directory1 directory2 ...\n"
<< " To pass ingredients to a mu program, provide them after '--':\n"
<< " mu file_or_dir1 file_or_dir2 ... -- ingredient1 ingredient2 ...\n"
<< " To see where a mu program is spending its time:\n"
<< " mu --profile file_or_dir1 file_or_dir2 ...\n"
<< " this slices and dices time spent in various profile.* output files\n"
<< " To print out the trace to stderr:\n"
<< " mu --dump file1.mu file2.mu ...\n"
<< " this is handy when you want to see sandboxed traces alongside the main one\n"
<< "\n"
<< " To browse a trace generated by a previous run:\n"
<< " mu browse-trace file\n"
;
return 0;
}
//: Support for option parsing.
//: Options always begin with '--' and are always the first arguments. An
//: option will never follow a non-option.
:(before "End Commandline Parsing")
char** arg = &argv[1];
while (argc > 1 && starts_with(*arg, "--")) {
if (false)
; // no-op branch just so any further additions can consistently always start with 'else'
// End Commandline Options(*arg)
else
cerr << "skipping unknown option " << *arg << '\n';
--argc; ++argv; ++arg;
}
//:: Helper function used by the above fragment of code (and later layers too,
//:: who knows?).
//: The :(code) directive appends function definitions to the end of the
//: project. Regardless of where functions are defined, we can call them
//: anywhere we like as long as we format the function header in a specific
//: way: put it all on a single line without indent, end the line with ') {'
//: and no trailing whitespace. As long as functions uniformly start this
//: way, our 'build*' scripts contain a little command to automatically
//: generate declarations for them.
:(code)
bool is_equal(char* s, const char* lit) {
return strncmp(s, lit, strlen(lit)) == 0;
}
bool starts_with(const string& s, const string& pat) {
string::const_iterator a=s.begin(), b=pat.begin();
for (/*nada*/; a!=s.end() && b!=pat.end(); ++a, ++b)
if (*a != *b) return false;
return b == pat.end();
}
//: I'll throw some style conventions here for want of a better place for them.
//: As a rule I hate style guides. Do what you want, that's my motto. But since
//: we're dealing with C/C++, the one big thing we want to avoid is undefined
//: behavior. If a compiler ever encounters undefined behavior it can make
//: your program do anything it wants.
//:
//: For reference, my checklist of undefined behaviors to watch out for:
//: out-of-bounds access
//: uninitialized variables
//: use after free
//: dereferencing invalid pointers: null, a new of size 0, others
//:
//: casting a large number to a type t
|