-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathrepl.nk
462 lines (338 loc) · 12.4 KB
/
mathrepl.nk
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
[ dup empty? or: [ dup count 1 - sliceQuoteAt drop ] ] @: rchop
needsCapability: console
33 33 33 rgb $: BG
224 224 224 rgb $: FG
117 117 117 rgb $: DIM_FG
76 175 80 rgb $: OK_FG
244 67 54 rgb $: ERR_FG
[ dup empty? => [ drop 0 ^ ] productBy: val ] @: productByVal?
"The context object is used to store words accessible
from within the prompt. It is an orphan so that no
other Novika words leak to the prompt."
[
PI $: PI
TAU $: TAU
] obj toOrphan $: context
"The Gem of Novika, namely a two-line-long Pratt parser (or a
variant thereof)."
[ asBlock $: context
asBlock $: operators
asBlock @: code
asDecimal $: prec
asWord $: name
"Operators block is used to store operators *only*."
operators name this pushes
"Context block is used to store all words accessible from inside
the REPL, including operators."
context name [
"Put cursor before a looser operator (or, if none, at
block end)."
ahead |beforeOrToEnd: [ operators swap entry:flatFetch? [ .prec prec <= ] and ]
"""Reparent everything between this operator and the looser
operator to ahead:
stack ┌────┐
──────────┐ │ │
│ │ │ │cursor
┌───────┴─────────▼────┼──────┼──────────┐
│ ▼ │ │ │
│ ┌───────┐ ┌────┴────┐ │ ┌───┐ │
│ │ 2 3 │ * │ 12 PI │ │ + │ 2 │ │
│ └───────┘ ▲ └─────────┘ │ ▲ └───┘ │
│ahead │ │ │ │
└──────────────┼──────────────┼─┼────────┘
│ │ │and
│ │so
│ │on
│this word │...
│
──────────────────────────► ─┘
after *, result becomes stack
"""
ahead reparent vals productByVal? code
] opens
] @: definfix
"Operator precedence. Lower means looser, and higher
means tighter."
0 $: _SUM
1 $: _FACTOR
2 $: _POWER
"The operators object is used to store operators, their
precedence, and their action."
orphan $: operators
#+ _SUM [ $: y stack productByVal? y + ] operators context definfix
#- _SUM [ $: y stack productByVal? y - ] operators context definfix
#* _FACTOR [ $: y stack productByVal? y * ] operators context definfix
#/ _FACTOR [ $: y stack productByVal? y / ] operators context definfix
#** _POWER [ $: y stack productByVal? y ** ] operators context definfix
[ $: document
'>>> ' $: prompt
'' $: answer
[] $: answerBlock
this $: self
true $: canBeMovedTo?
[ console:width document.x 2 * - ] @: width
2 $: height
"--------------------------------------------------------------------------"
" Cursor words -"
"--------------------------------------------------------------------------"
0 $: windowStart
1 $: nAlwaysShowLeft
0 $: nAlwaysShowRight
[ windowStart width + ] @: windowEnd
[ answer empty? => [ '' ^ ]
answer windowStart sliceQuoteAt nip $: remainder
remainder empty? => [ '' ^ ]
remainder width clamp: 0 [ remainder count ] sliceQuoteAt drop
] @: answerWindow
"Leaves where the cursor is positioned inside the answer quote."
0 $: cursorIndex
"Leaves start index."
0 $: cursorStartIndex
[ "( -- I ): leaves end Index."
answer count
] @: cursorEndIndex
[ "( I -- ): moves cursor to the given Index."
clamp: cursorStartIndex cursorEndIndex =: cursorIndex
"If cursor falls within the sliding window bounds,
do nothing."
windowStart nAlwaysShowLeft + cursorIndex <
windowEnd nAlwaysShowRight - cursorIndex >
and
=> ^
"If cursor is to the left of the boundary, move the boundary
by the distance between window start and index."
cursorIndex windowStart nAlwaysShowLeft + <=
=> [ windowStart dup cursorIndex - nAlwaysShowLeft + abs - 0 2max =: windowStart ^ ]
"If cursor is to the right of the boundary, move the boundary
forward by the distance between window start and index."
windowStart cursorIndex windowEnd - nAlwaysShowRight 1 + + + =: windowStart
] @: setCursorIndex
[ "( -- B ): leaves Boolean for whether cursor is at the
start of the answer."
cursorIndex cursorStartIndex =
] @: cursorAtStart?
[ "( -- B ): leaves Boolean for whether cursor is at the
end of the answer."
cursorIndex cursorEndIndex =
] @: cursorAtEnd?
[ "( Q -- ): inserts Quote before cursor."
answer empty? => [ setAnswer ^ ]
answer cursorIndex sliceQuoteAt dip: [ swap ~ ] ~ setAnswer
] @: insertBeforeCursor
[ "( -- ): removes the character before cursor."
answer cursorIndex sliceQuoteAt dip: rchop ~ setAnswer
] @: removeBeforeCursor
[ "( -- ): moves the cursor once to the left."
cursorIndex 1 - setCursorIndex
] @: moveCursorLeft
[ "( -- ): moves the cursor once to the right."
cursorIndex 1 + setCursorIndex
] @: moveCursorRight
[ "( -- ): moves the cursor to the start of the answer."
cursorStartIndex setCursorIndex
] @: moveCursorToStart
[ "( -- ): moves the cursor to the end of the answer."
cursorEndIndex setCursorIndex
] @: moveCursorToEnd
[ "( P -- ): appends the answer in Prompt to this prompt's
answer. Does nothing to the cursor position."
answer swap .answer ~ setAnswer
] @: append
"--------------------------------------------------------------------------"
" Result computation "
"--------------------------------------------------------------------------"
0 $: _lastOkResult
"Leaves whether the answer block can be opened without an error."
false $: isAnswerRunnable?
[ "( Q -- ): sets answer equal to Quote."
[ false =: isAnswerRunnable? ] @: __died__
dup =: answer
"Replace parens with brackets for fun and profit!"
orphan swap '(' '[' replaceAll
')' ']' replaceAll slurp =: answerBlock
true =: isAnswerRunnable?
] @: setAnswer
[ "( -- Dr ): leaves Decimal result of current answer, or,
if impossible, last available Decimal result."
[ "If we fail for some reason, set the isAnswerRunnable flag
and leave last valid result."
drop false =: isAnswerRunnable? _lastOkResult
] @: __died__
isAnswerRunnable? => [
"If result block is empty, go back to 0 as the last result."
answerBlock context reparent vals productByVal? =: _lastOkResult
true =: isAnswerRunnable?
]
_lastOkResult
] @: result
"-------------------------------------------------------------------------"
" Painting "
"-------------------------------------------------------------------------"
[ "( -- X ): leaves X position of the answer."
prompt count
] @: answerX
[ "( -- X ): leaves VISUAL X position of the cursor."
answerX cursorIndex + windowStart -
] @: cursorX
[ "( X Y -- ): draws this prompt at X, Y position."
${ x y }
DIM_FG withEchoFg
prompt x y console:appendEcho
'===' x y 1 + console:appendEcho
dropEchoFg
answerWindow answerX x + y console:appendEcho
result answerX x + y 1 +
self document.isActiveNode? br:
[ isAnswerRunnable? OK_FG ERR_FG sel ]
"Do not use colors if prompt is inactive. This'll needlessly
hijack the user's attention."
DIM_FG
withEchoFg
console:appendEcho
dropEchoFg
"Do not highlight the cursor if prompt is inactive."
self document.isActiveNode? => [
FG withEchoBg
BG withEchoFg
cursorX x + y console:change
dropEchoBg
dropEchoFg
]
] @: paint
this
] @: createMathPromptNode
[ ${ document caption }
1 $: height
false $: canBeMovedTo?
[ "( X Y -- ): paints this label node."
caption -rot console:appendEcho
] @: paint
this
] @: createLabelNode
[ $: y $: x
[ ] $: _nodes
"Leaves the index where the nodes start in the node list."
1 $: nodesStartIndex
[ "( -- I ): leaves the Index where the nodes end in the
node list."
_nodes count
] @: nodesEndIndex
[ "( N -- ): adds a Node after the active node and makes
it active *if it can be moved to*. Otherwise, inserts
Node *before* the currently active node."
$: node
_nodes dup empty? node.canBeMovedTo? or br:
[ node shove ]
[ [ <| node |> ] hydrate ]
] @: insert
[ "( -- N ): leaves the active node."
_nodes top
] @: activeNode
[ "( N -- ): leaves whether Node is the active node."
activeNode same?
] @: isActiveNode?
[ "( -- ): makes active the first node that can be moved
to, above."
_nodes |at dup $: hadCursorAt nodesStartIndex = => ^
_nodes dup 1 |- <-| [ dup.canBeMovedTo? => [ stack |at =: hadCursorAt break ] ]
_nodes hadCursorAt |to
] @: moveToNodeAbove
[ "( -- ): makes active the first node that can be moved
to, below."
_nodes |at dup $: hadCursorAt nodesEndIndex = => ^
_nodes |-> [ dup.canBeMovedTo? => [ stack |at =: hadCursorAt break ] ]
_nodes hadCursorAt |to
] @: moveToNodeBelow
[ "( -- ): drops the currently active prompt node, but
only if there is a node that can be moved to above.
Otherwise, it is a noop. Contents of the currently
active prompt node are appended to that node above."
_nodes top $: activePromptWas
moveToNodeAbove
_nodes top $: activePromptIs
activePromptWas activePromptIs same? => ^
"TODO: maybe have something like this in native
code, for the simpler use cases? Seems fairly
inefficient to use without for this."
_nodes without: [ activePromptWas same? ] _nodes resub
activePromptWas activePromptIs.append
] @: dropPrompt
[ "( -- ): drops the prompt below, if it can be
moved to. Otherwise, a noop. Appends the content
of dropped form to the current form."
moveToNodeBelow dropPrompt
] @: ejectPrompt
[
0 $: height
_nodes 4 |around eachWithIndex: [ $: node $: index
x y height + node.paint
height node.height + =: height
]
] @: paint
this
] @: createDocument
[
[
dropEchoBg dropEchoFg
console:off
] @: finalize
[ finalize die ] @: __died__
console:on
console:truecolor
10 5 createDocument $: document
document 'Interactive infix math DSL, backed by Novika.' createLabelNode document.insert
document 'Available words: ' context entry:names sepBy: ' ' ~ createLabelNode document.insert
document createMathPromptNode document.insert
[
console:clear
document.paint
console:present
] @: repaint
[ document.activeNode ] @: prompt
BG withEchoBg
FG withEchoFg
true $: running?
[
console:hadKeyPressed? or: ^
console:hadCtrlPressed?
console:getCharPressed 'c' = and
=> [ false =: running? ^ ]
console:hadPageUpPressed? => [
document 'gibberish' createLabelNode document.insert ^
]
console:getCharPressed '\n' = => [
document createMathPromptNode document.insert ^
]
console:hadBackspacePressed? => [
prompt.cursorAtStart? => [ document.dropPrompt ^ ]
prompt.removeBeforeCursor
prompt.moveCursorLeft
^
]
console:hadDeletePressed? => [
prompt.cursorAtEnd? => [ document.ejectPrompt ^ ]
prompt.moveCursorRight
prompt.removeBeforeCursor
prompt.moveCursorLeft
^
]
console:hadUpPressed? => [ document.moveToNodeAbove ^ ]
console:hadDownPressed? => [ document.moveToNodeBelow ^ ]
console:hadHomePressed? => [ prompt.moveCursorToStart ^ ]
console:hadEndPressed? => [ prompt.moveCursorToEnd ^ ]
console:hadLeftPressed? => [ prompt.moveCursorLeft ^ ]
console:hadRightPressed? => [ prompt.moveCursorRight ^ ]
console:hadCharPressed? or: ^
console:getCharPressed
prompt.insertBeforeCursor
prompt.moveCursorRight
] @: processInput
repaint
while: running?
[
console:readKey processInput
repaint
]
finalize
] @: mainloop
mainloop