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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow
package repl
import (
"fmt"
"strings"
)
type HelpTopic struct {
Category string // e.g. "Arithmetic", "Stack", "Comparison", "Variables", "Hyper", "REPL"
Operator string // the operator or command name (e.g. "+", "help", "rat")
Aliases []string
Description string
Usage string // short usage hint
Examples []string
// Render, when non-nil, overrides the default formatTopic rendering.
// Used for special topics like "categories" that need custom output.
Render func() string
}
// helpTopics is the single source of truth for all inline help entries.
// Each entry documents one operator, function, or REPL command with examples.
var helpTopics = []HelpTopic{
// ── REPL commands ──
{
Category: "REPL",
Operator: "help",
Description: "Show help information",
Usage: "help [topic]",
Examples: []string{
"help Show general help overview",
"help + Show help for the + operator",
"help dup Show help for dup",
},
},
{
Category: "REPL",
Operator: "clear",
Description: "Clear the terminal screen",
Usage: "clear",
Examples: []string{
"clear",
},
},
{
Category: "REPL",
Operator: "quit",
Aliases: []string{"exit"},
Description: "Exit the REPL",
Usage: "quit or exit",
Examples: []string{
"quit",
},
},
{
Category: "REPL",
Operator: "rpn",
Aliases: []string{"calc"},
Description: "Evaluate an RPN (Reverse Polish Notation) expression",
Usage: "rpn <expression>",
Examples: []string{
"rpn 3 4 + Evaluate 3 + 4 = 7",
"rpn 10 2 / Evaluate 10 / 2 = 5",
"calc 5 3 * Same as 'rpn 5 3 *'",
},
},
{
Category: "REPL",
Operator: "rat",
Description: "Switch between float64 and rational number modes",
Usage: "rat on|off|toggle",
Examples: []string{
"rat on Enable rational mode",
"rat off Disable rational mode (float64)",
"rat toggle Toggle between modes",
},
},
{
Category: "REPL",
Operator: "stack",
Description: "Show current RPN stack state",
Usage: "stack",
Examples: []string{
"1 2 3 stack Show stack: 1 2 3",
},
},
// ── Arithmetic operators ──
{
Category: "Arithmetic",
Operator: "+",
Description: "Add two numbers (metric-aware)",
Usage: "a b +",
Examples: []string{
"3 4 + 3 + 4 = 7",
"100Mbps 50 + 100Mbps + 50Mbps = 150Mbps (Cool absorbs metric)",
},
},
{
Category: "Arithmetic",
Operator: "-",
Description: "Subtract two numbers (metric-aware)",
Usage: "a b -",
Examples: []string{
"10 3 - 10 - 3 = 7",
"200Mbps 50 - 200Mbps - 50Mbps = 150Mbps",
},
},
{
Category: "Arithmetic",
Operator: "*",
Description: "Multiply two numbers",
Usage: "a b *",
Examples: []string{
"3 4 * 3 * 4 = 12",
"100Mbps 2 * 100Mbps * 2 = 200Mbps (Cool preserves metric)",
},
},
{
Category: "Arithmetic",
Operator: "/",
Description: "Divide two numbers (a / b)",
Usage: "a b /",
Examples: []string{
"10 3 / 10 / 3 = 3.333...",
},
},
{
Category: "Arithmetic",
Operator: "^",
Description: "Raise to power (a ^ b), result is always unitless",
Usage: "a b ^",
Examples: []string{
"2 3 ^ 2 ^ 3 = 8",
"10 0.5 ^ 10 ^ 0.5 = sqrt(10) = 3.162...",
},
},
{
Category: "Arithmetic",
Operator: "**",
Description: "Raise to integer power (a ** b) using fast binary exponentiation",
Usage: "a b **",
Examples: []string{
"2 10 ** 2 ^ 10 = 1024",
"3 6 ** 3 ^ 6 = 729",
},
},
{
Category: "Arithmetic",
Operator: "%",
Description: "Modulo (remainder of a / b)",
Usage: "a b %",
Examples: []string{
"10 3 % 10 mod 3 = 1",
"7 2 % 7 mod 2 = 1",
},
},
{
Category: "Arithmetic",
Operator: "lg",
Description: "Logarithm base 2",
Usage: "a lg",
Examples: []string{
"8 lg log2(8) = 3",
"1024 lg log2(1024) = 10",
},
},
{
Category: "Arithmetic",
Operator: "log",
Description: "Logarithm base 10",
Usage: "a log",
Examples: []string{
"100 log log10(100) = 2",
"1000 log log10(1000) = 3",
},
},
{
Category: "Arithmetic",
Operator: "ln",
Description: "Natural logarithm (base e)",
Usage: "a ln",
Examples: []string{
"2.71828 ln ln(2.71828) ~= 1",
"1 ln ln(1) = 0",
},
},
// ── Comparison operators ──
{
Category: "Comparison",
Operator: ">",
Aliases: []string{"gt"},
Description: "Greater than: pushes true (1) or false (0)",
Usage: "a b >",
Examples: []string{
"5 3 > 5 > 3 = true (1)",
"3 5 > 3 > 5 = false (0)",
},
},
{
Category: "Comparison",
Operator: "<",
Aliases: []string{"lt"},
Description: "Less than: pushes true (1) or false (0)",
Usage: "a b <",
Examples: []string{
"3 5 < 3 < 5 = true (1)",
"5 3 < 5 < 3 = false (0)",
},
},
{
Category: "Comparison",
Operator: ">=",
Aliases: []string{"gte"},
Description: "Greater than or equal: pushes true (1) or false (0)",
Usage: "a b >=",
Examples: []string{
"5 5 >= 5 >= 5 = true (1)",
"3 5 >= 3 >= 5 = false (0)",
},
},
{
Category: "Comparison",
Operator: "<=",
Aliases: []string{"lte"},
Description: "Less than or equal: pushes true (1) or false (0)",
Usage: "a b <=",
Examples: []string{
"3 3 <= 3 <= 3 = true (1)",
"5 3 <= 5 <= 3 = false (0)",
},
},
{
Category: "Comparison",
Operator: "==",
Aliases: []string{"eq"},
Description: "Equal: pushes true (1) or false (0)",
Usage: "a b ==",
Examples: []string{
"5 5 == 5 == 5 = true (1)",
"5 3 == 5 == 3 = false (0)",
},
},
{
Category: "Comparison",
Operator: "!=",
Aliases: []string{"neq"},
Description: "Not equal: pushes true (1) or false (0)",
Usage: "a b !=",
Examples: []string{
"5 3 != 5 != 3 = true (1)",
"5 5 != 5 != 5 = false (0)",
},
},
// ── Stack operators ──
{
Category: "Stack",
Operator: "dup",
Description: "Duplicate the top stack value",
Usage: "a dup",
Examples: []string{
"5 dup Stack: 5 5",
"3 dup dup Stack: 3 3 3",
},
},
{
Category: "Stack",
Operator: "swap",
Description: "Swap the top two stack values",
Usage: "a b swap",
Examples: []string{
"1 2 swap Stack: 2 1",
},
},
{
Category: "Stack",
Operator: "pop",
Description: "Remove and discard the top stack value",
Usage: "pop",
Examples: []string{
"1 2 3 pop Stack: 1 2",
},
},
{
Category: "Stack",
Operator: "d",
Description: "Pop a symbol from stack and delete that variable",
Usage: ":x d",
Examples: []string{
":x d Delete variable x",
},
},
{
Category: "Stack",
Operator: "show",
Aliases: []string{"showstack", "print"},
Description: "Display the current stack contents",
Usage: "show",
Examples: []string{
"1 2 3 show Prints: 1 2 3",
"show Prints: Stack is empty",
},
},
// ── Variable operators ──
{
Category: "Variables",
Operator: ":=",
Description: "Assign value to variable (name on bottom, value on top)",
Usage: ":name value :=",
Examples: []string{
":x 5 := Set variable x to 5",
":y 3.14 := Set variable y to 3.14",
},
},
{
Category: "Variables",
Operator: "=:",
Description: "Assign value to variable (value on bottom, name on top)",
Usage: "value :name =:",
Examples: []string{
"5 :x =: Set variable x to 5",
},
},
{
Category: "Variables",
Operator: "vars",
Description: "List all defined variables and their values",
Usage: "vars",
Examples: []string{
"vars List all variables",
},
},
{
Category: "Variables",
Operator: "clear",
Description: "Clear all user-defined variables",
Usage: "clear",
Examples: []string{
"clear Remove all variables",
},
},
{
Category: "Variables",
Operator: "convert",
Description: "Convert a value to a target metric (@X syntax)",
Usage: "value @target convert",
Examples: []string{
"100Mbps @bps convert Convert 100Mbps to bps",
},
},
// ── Constants ──
{
Category: "Constants",
Operator: "constants",
Description: "List all available constants",
Usage: "constants",
Examples: []string{
"constants List all built-in constants",
},
},
{
Category: "Constants",
Operator: "clearconstants",
Description: "Reset all constants to built-in defaults",
Usage: "clearconstants",
Examples: []string{
"clearconstants Reset constants",
},
},
// ── Hyper (n-ary) operators ──
{
Category: "Hyper",
Operator: "[+]",
Description: "Add all stack values together (n-ary)",
Usage: "a b c [+] ...",
Examples: []string{
"1 2 3 [+] 1 + 2 + 3 = 6",
"10 20 30 40 [+] 100",
},
},
{
Category: "Hyper",
Operator: "[-]",
Description: "Subtract all stack values left-associative (n-ary)",
Usage: "a b c [-] ...",
Examples: []string{
"10 3 2 [-] 10 - 3 - 2 = 5",
},
},
{
Category: "Hyper",
Operator: "[*]",
Description: "Multiply all stack values together (n-ary)",
Usage: "a b c [*] ...",
Examples: []string{
"2 3 4 [*] 2 * 3 * 4 = 24",
},
},
{
Category: "Hyper",
Operator: "[/]",
Description: "Divide all stack values left-associative (n-ary)",
Usage: "a b c [/] ...",
Examples: []string{
"100 2 5 [/] 100 / 2 / 5 = 10",
},
},
{
Category: "Hyper",
Operator: "[^]",
Description: "Power all stack values left-associative (n-ary)",
Usage: "a b c [^] ...",
Examples: []string{
"2 3 2 [^] 2 ^ 3 ^ 2 = 8",
},
},
{
Category: "Hyper",
Operator: "[%]",
Description: "Modulo all stack values left-associative (n-ary)",
Usage: "a b c [%] ...",
Examples: []string{
"100 10 3 [%] 100 % 10 % 3 = 1",
},
},
{
Category: "Hyper",
Operator: "[lg]",
Description: "Sum of log2 of all stack values (n-ary)",
Usage: "a b [lg] ...",
Examples: []string{
"2 4 [lg] log2(2) + log2(4) = 1 + 2 = 3",
},
},
{
Category: "Hyper",
Operator: "[log]",
Description: "Sum of log10 of all stack values (n-ary)",
Usage: "a b [log] ...",
Examples: []string{
"10 100 [log] log10(10) + log10(100) = 1 + 2 = 3",
},
},
{
Category: "Hyper",
Operator: "[ln]",
Description: "Sum of natural log of all stack values (n-ary)",
Usage: "a b [ln] ...",
Examples: []string{
"1 2.71828 [ln] ln(1) + ln(2.71828) = 0 + 1 = 1",
},
},
// ── Special topics (custom render) ──
{
Category: "REPL",
Operator: "categories",
Description: "List all available help topics by category",
Usage: "help categories",
Examples: []string{"help categories List all topics grouped by category"},
Render: func() string {
var sb strings.Builder
sb.WriteString("Available help topics by category:\n\n")
for _, cat := range categoryOrder {
sb.WriteString(fmt.Sprintf(" %s:\n", cat))
for _, op := range helpByCat[cat] {
t := helpByTopic[op]
aliasStr := ""
if len(t.Aliases) > 0 {
aliasStr = " (" + strings.Join(t.Aliases, ", ") + ")"
}
sb.WriteString(fmt.Sprintf(" %-16s %s%s\n", op, t.Description, aliasStr))
}
sb.WriteString("\n")
}
sb.WriteString("Use 'help <topic>' for details on any topic.\n")
return sb.String()
},
},
}
// buildHelpIndex builds lookup maps from helpTopics at package init.
var (
helpByTopic = make(map[string]*HelpTopic) // operator -> topic
helpByAlias = make(map[string]*HelpTopic) // alias -> topic
helpByCat = make(map[string][]string) // category -> []operator names
categoryOrder []string // insertion order of categories
)
func init() {
seenCat := make(map[string]struct{})
for i := range helpTopics {
t := &helpTopics[i]
for _, a := range t.Aliases {
helpByAlias[a] = t
}
if _, ok := seenCat[t.Category]; !ok {
seenCat[t.Category] = struct{}{}
categoryOrder = append(categoryOrder, t.Category)
}
helpByCat[t.Category] = append(helpByCat[t.Category], t.Operator)
}
// Iterate in reverse so REPL entries (last in slice) take priority
// in helpByTopic. This means `help clear` shows the REPL screen-clear
// help rather than the RPN variable-clear help.
for i := len(helpTopics) - 1; i >= 0; i-- {
t := &helpTopics[i]
helpByTopic[t.Operator] = t
}
}
|