blob: b34ef9c45d8ccb3d455208a00895dcc726334d41 (
plain)
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
|
# Variables and Assignment Operators
The gt calculator supports named variables that store numeric values. Variables persist across expressions within a single RPN session (REPL or multi-expression input), enabling reusable values and multi-step calculations.
## Assignment Operators
Three assignment operators are available, differing in token order:
| Operator | Syntax | Description | Example |
|----------|--------|-------------|---------|
| `:=` | `name value :=` | Right assignment | `x 5 :=` → `x = 5` |
| `=:` | `value name =:` | Left assignment | `10 y =:` → `y = 10` |
| `=` | `name value =` or `name = value` | Standard assignment | `x 10 =` or `x = 10` |
### `:=` (Right Assignment)
The value appears on the right (top of stack). This is the most idiomatic RPN form.
```
x 5 := → x = 5
price 9.99 := → price = 9.99
```
### `=:` (Left Assignment)
The value appears on the left (bottom of stack). Useful when you think in "value first" order.
```
42 answer =: → answer = 42
3.14159 pi =: → pi = 3.14159
```
### `=` (Standard Assignment)
Supports both infix-style (`name = value`) and postfix-style (`name value =`) syntax. Also supports expression continuation — you can append an expression after the assignment to evaluate immediately.
```
x = 5 → x = 5
x 10 = → x = 10
x 10 = x 5 + → 15 (assigns x=10, then evaluates x+5)
```
## Variable Management
### `vars` — List Variables
Shows all defined variables, sorted alphabetically with their values.
```
vars
→ No variables defined (when none exist)
→ a = 10
b = 3 (when variables are defined)
```
### `clear` — Clear All Variables
Removes all defined variables at once.
```
clear
→ All variables cleared
```
### `d` — Delete a Variable
Removes a single variable. The variable name must be pushed as a symbol (using `:` prefix) onto the stack before `d`.
```
x 5 := → x = 5
:x d → (deletes x)
x → :x (x is now undefined, shown as symbol)
```
Attempting to delete a non-existent variable returns an error:
```
:nonexistent d → error: variable not found: nonexistent
```
## Variable Lifecycle
1. **Creation**: A variable is created when a value is assigned using `:=`, `=:`, or `=`.
2. **Usage**: Reference the variable by name in an expression; its value is pushed onto the stack.
3. **Reassignment**: Assigning to an existing variable overwrites its previous value.
4. **Deletion**: Remove with `:name d` or clear all with `clear`.
5. **Session scope**: Variables persist only within a single RPN session. Each CLI invocation starts fresh.
## Using Variables in Expressions
Once defined, variables are used by name in expressions. The variable's value is pushed onto the stack for computation.
### Single Variable Reuse
```
x 5 :=
x x + → 10
x 2 * → 10
```
### Multi-Variable Expressions
```
a 10 :=
b 3 :=
a b + → 13
a b * → 30
```
### Chained Assignments
Multiple variables can be assigned in a single expression:
```
a 10 := b 3 := c 2 :=
a b + c * → 26
```
### Variable Reassignment
Variables can be reassigned to new values:
```
x 5 := → x = 5
x 10 := → x = 10
x → 10
```
### Assignment with Expression Continuation
The `=` operator supports evaluating an expression immediately after assignment:
```
x 10 = x 5 + → 15 (assigns x=10, then computes x+5)
```
## Examples
### Storing Reusable Values
Store constants or frequently-used values to avoid retyping:
```
pi 3.14159265 :=
radius 7 :=
2 pi radius * → 43.9822971 (circumference)
```
### Multi-Step Calculations
Break complex calculations into named intermediate steps:
```
base 100 :=
tax_rate 0.08 :=
tax base tax_rate * → 8
total base tax + → 108
```
### Iterative Refinement
Update a running value across multiple operations:
```
sum 0 :=
sum 10 + → 10
sum 20 + → 30
sum 5 - → 25
```
|