-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEST.txt
403 lines (356 loc) · 6.99 KB
/
TEST.txt
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# Tests
An introduction and general documentation about the project structure is available in `README.md`.
This file was auto generated by the `test.py` script.
Snapshot tests can be found in the `tests` folder.
The output of a test combines `stdin`, `stdout` and `stderr` into one string.
## Command line arguments
You can pass arguments to set some configuration flags.
- Command: tiny-calc --print-tokens --print-chunks
- Inputs: ["+ 1 2\n", "/ pi 2\n"]
- Output:
```
Welcome to tiny-calc!
Type ':help' if you are lost =)
>> + 1 2
Tokens:
Plus[+] 0..1
Number[1] 2..3
Number[2] 4..5
OpCodes:
[0] Literal
[1] Literal
[2] Add
Literals:
[0] 2
[1] 1
3
>> / pi 2
Tokens:
Slash[/] 0..1
Identifier[pi] 2..4
Number[2] 5..6
OpCodes:
[0] Literal
[1] Literal
[2] Div
Literals:
[0] 2
[1] 3.141592653589793
1.570796326794897
>> CTRL+D
```
## Wrong command line arguments
A help message is provided if the command line arguments are incorrect.
- Command: tiny-calc --test -- -9 -10
- Inputs: []
- Output:
```
Error: Invalid argument '--test'
Usage:
tiny-calc [OPTIONS]
Options:
-h, --help, -? Print this help message
--plain Only print the results of the calculation,
or error messages (useful for piping)
--print-tokens Print token streams
--print-chunks Print compiled chunks
```
## Command line help
The help message may also be printed by using the help argument.
- Command: tiny-calc --help
- Inputs: []
- Output:
```
A very minimal terminal calculator with a bit of an unusual syntax.
Usage:
tiny-calc [OPTIONS]
Options:
-h, --help, -? Print this help message
--plain Only print the results of the calculation,
or error messages (useful for piping)
--print-tokens Print token streams
--print-chunks Print compiled chunks
```
## REPL commands
The repl used to evaluate expressions supports a few commands too.
- Command: tiny-calc
- Inputs: [":help\n", ":?\n", ":invalid\n", ":chunks\n", ":tokens\n", "sin 0\n", ":quit\n"]
- Output:
```
Welcome to tiny-calc!
Type ':help' if you are lost =)
>> :help
:help, :? Print command help
:examples Print expression examples
:quit, :exit Exit calculator (or press CTRL+C)
:tokens Toggle printing token streams
:chunks Toggle printing compiled chunks
>> :?
:help, :? Print command help
:examples Print expression examples
:quit, :exit Exit calculator (or press CTRL+C)
:tokens Toggle printing token streams
:chunks Toggle printing compiled chunks
>> :invalid
Error: Unkown command ':invalid'
Note: Type ':help' for a list of valid commands
>> :chunks
>> :tokens
>> sin 0
Tokens:
Identifier[sin] 0..3
Number[0] 4..5
OpCodes:
[0] Literal
[1] Sin
Literals:
[0] 0
0
>> :quit
```
## Empty lines
Lines that contain only whitespace are skipped.
- Command: tiny-calc --print-tokens --print-chunks
- Inputs: ["\n", "\t\t\n", " \n"]
- Output:
```
Welcome to tiny-calc!
Type ':help' if you are lost =)
>>
>>
>>
>> CTRL+D
```
## Addition
- Command: tiny-calc --print-tokens --print-chunks
- Inputs: ["+ 1 + 2 + 3 + 4 5_000\n"]
- Output:
```
Welcome to tiny-calc!
Type ':help' if you are lost =)
>> + 1 + 2 + 3 + 4 5_000
Tokens:
Plus[+] 0..1
Number[1] 2..3
Plus[+] 4..5
Number[2] 6..7
Plus[+] 8..9
Number[3] 10..11
Plus[+] 12..13
Number[4] 14..15
Number[5_000] 16..21
OpCodes:
[0] Literal
[1] Literal
[2] Add
[3] Literal
[4] Add
[5] Literal
[6] Add
[7] Literal
[8] Add
Literals:
[0] 5000
[1] 4
[2] 3
[3] 2
[4] 1
5010
>> CTRL+D
```
## Constants
Currently only pi exists as a constant.
- Command: tiny-calc --plain
- Inputs: ["pi\n"]
- Output:
```
3.141592653589793
```
## Functions
Cosine and sine are currently the only functions implemented.
- Command: tiny-calc --print-tokens --print-chunks
- Inputs: ["s 0\n", "sin * 2 pi\n", "c * 2 pi\n", "cos 0\n", "cos * 1.5 pi\n", "cos * / 1 4 pi\n"]
- Output:
```
Welcome to tiny-calc!
Type ':help' if you are lost =)
>> s 0
Tokens:
Identifier[s] 0..1
Number[0] 2..3
OpCodes:
[0] Literal
[1] Sin
Literals:
[0] 0
0
>> sin * 2 pi
Tokens:
Identifier[sin] 0..3
Star[*] 4..5
Number[2] 6..7
Identifier[pi] 8..10
OpCodes:
[0] Literal
[1] Literal
[2] Mul
[3] Sin
Literals:
[0] 3.141592653589793
[1] 2
-2.449293598294706e-16
>> c * 2 pi
Tokens:
Identifier[c] 0..1
Star[*] 2..3
Number[2] 4..5
Identifier[pi] 6..8
OpCodes:
[0] Literal
[1] Literal
[2] Mul
[3] Cos
Literals:
[0] 3.141592653589793
[1] 2
1
>> cos 0
Tokens:
Identifier[cos] 0..3
Number[0] 4..5
OpCodes:
[0] Literal
[1] Cos
Literals:
[0] 0
1
>> cos * 1.5 pi
Tokens:
Identifier[cos] 0..3
Star[*] 4..5
Number[1.5] 6..9
Identifier[pi] 10..12
OpCodes:
[0] Literal
[1] Literal
[2] Mul
[3] Cos
Literals:
[0] 3.141592653589793
[1] 1.5
-1.83697019872103e-16
>> cos * / 1 4 pi
Tokens:
Identifier[cos] 0..3
Star[*] 4..5
Slash[/] 6..7
Number[1] 8..9
Number[4] 10..11
Identifier[pi] 12..14
OpCodes:
[0] Literal
[1] Literal
[2] Literal
[3] Div
[4] Mul
[5] Cos
Literals:
[0] 3.141592653589793
[1] 4
[2] 1
0.7071067811865476
>> CTRL+D
```
## Combined
This is how the expression used to explain the task looks like:
- Command: tiny-calc --print-tokens --print-chunks
- Inputs: ["c + * 3.1 4 + 7 8\n"]
- Output:
```
Welcome to tiny-calc!
Type ':help' if you are lost =)
>> c + * 3.1 4 + 7 8
Tokens:
Identifier[c] 0..1
Plus[+] 2..3
Star[*] 4..5
Number[3.1] 6..9
Number[4] 10..11
Plus[+] 12..13
Number[7] 14..15
Number[8] 16..17
OpCodes:
[0] Literal
[1] Literal
[2] Add
[3] Literal
[4] Literal
[5] Mul
[6] Add
[7] Cos
Literals:
[0] 8
[1] 7
[2] 4
[3] 3.1
-0.6415079902223829
>> CTRL+D
```
## Examples
The example command prints a list of example expressions and their results.
The results are currently precalculated and do not respect configuration flags.
- Command: tiny-calc
- Inputs: [":examples\n"]
- Output:
```
Welcome to tiny-calc!
Type ':help' if you are lost =)
>> :examples
╭── Addition
│ >> + 1 2
│ 3
│ >> + 10.2 - 0 0.2
│ 10
─╯
╭── Subtraction
│ >> - 2 1
│ 1
─╯
╭── Multiplication
│ >> * 5 0.4
│ 2
─╯
╭── Division
│ >> / 1 2
│ 0.5
─╯
╭── Functions
│ >> sin 0
│ 0
│ >> cos 0
│ 1
─╯
╭── Constants
│ >> pi
│ 3.141592653589793
│ >> π
│ 3.141592653589793
│ >> * 2 pi
│ 6.283185307179586
│ >> cos * 2 π
│ 1
─╯
>> CTRL+D
```
## Plain output
Useful when piping input to tiny-calc and processing its output.
- Command: tiny-calc --plain
- Inputs: ["+ 1 2\n", "+ 20 + 19 + 18 + 17 + 16 + 15 + 14 + 13 + 12 + 11 + 10 9\n", "* 20 0.11\n", "/ 5 2\n", "/ 18 2.2\n"]
- Output:
```
3
174
2.2
2.5
8.181818181818182
```