-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.sts
674 lines (523 loc) · 16.6 KB
/
stdlib.sts
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# this file is released into the public domain
# This is the STS standard library. It is written in STS
# instead of C to keep the source of the interpreter
# smaller than it would be if this were entirely C.
# The following implements hashmaps, string management,
# file parsing, and other uncategorized utility functions.
# stdlib globals ========================
global stdlib-current-error ""
# sts type globals (are functions to make constant)
function STS_EXTERNAL {copy 0}
function STS_NIL {copy 1}
function STS_NUMBER {copy 2}
function STS_STRING {copy 3}
function STS_ARRAY {copy 4}
function STS_FUNCTION {copy 5}
function STS_BOOLEAN {copy 6}
# (incorrect, but unfortunately stuck) hashmap implementation ================
# hashmap structure:
# [array
# [array *hash* *key string* *value*]
# [array *hash* *key string* *value*]
# ...
# ]
function hashmap {
local ret $nil
local row $nil
local break 0
local i 0
# TODO: possibly insert at 0 so its 3 less function calls
if(! (sizeof $...)) {
set $ret [array]
}
elseif(% (sizeof $...) 2) {
stdlib-set-error hashmap "arguments provided were not an even number"
}
else {
set $ret [array]
loop(&& [< $i (sizeof $...)] [! $break]) {
if(!= (typeof [get $... $i]) (STS_STRING)) {
stdlib-set-error hashmap "keys need to be strings"
set $ret $nil
++ $break
}
else {
set $row [array (string-hash [get $... $i]) (get $... $i) (get $... (+ $i 1))]
# theres a quirk with the language that results in just inserting the literal
# row value itself. This makes all rows the extact same and just duplicates forever.
# The solution is to just create space in the array and set the new space to
# the new row. The set action shallow copies the value so the array itself is
# duplicated but all row values are just references to the originals passed.
# The most important part is initializing the space with anything that is generated
# rather than part of the ast (so literals will NOT work). $nil is special because
# the eval function creates a new nil value every time it is used. This makes it
# perfect for this.
insert $ret (sizeof $ret) $nil
set (get $ret [- (sizeof $ret) 1]) $row
# unfortunately, this will not work
# insert $ret (sizeof $ret) $row
}
+= $i 2
}
}
pass $ret
}
function hashmap-get map key {
local ret $nil
local hash $nil
local break 0
local i 0
if(&& [!= (typeof $map) (STS_ARRAY)] [!= (typeof $key) (STS_STRING)]) {
stdlib-set-error hashmap "the map arg must be an array and the key must be a string"
}
else {
set $hash (string-hash $key)
loop(&& [< $i (sizeof $map)] [! $break]) {
if(== $hash [get [get $map $i] 0]) {
set $ret [get [get $map $i] 2]
++ $break
}
++ $i
}
}
if(== $ret $nil) {
stdlib-set-error hashmap "could not find value for key provided"
}
pass $ret
}
function hashmap-exists map key {
local ret 1
if(&& [== [hashmap-get $map $key] $nil] [== (stdlib-get-error) "hashmap: could not find value for key provided"]) {
set $ret 0
}
pass $ret
}
# hashmap-set returns 0 if the key already existed and 1 if a new one had to be created
function hashmap-set map key value {
local ret $nil
local row $nil
local hash $nil
local break 0
local i 0
if(&& [!= (typeof $map) (STS_ARRAY)] [!= (typeof $key) (STS_STRING)]) {
stdlib-set-error hashmap "the map arg must be an array and the key must be a string"
}
else {
set $hash (string-hash $key)
loop(&& [< $i (sizeof $map)] [! $break]) {
if(== $hash [get [get $map $i] 0]) {
set [get [get $map $i] 2] $value
set $ret 0
++ $break
}
++ $i
}
}
# add new key instead
if(== $ret $nil) {
set $row [array (string-hash $key) $key $value]
insert $map (sizeof $map) $nil
set (get $map [- (sizeof $map) 1]) $row
set $ret 1
}
pass $ret
}
# hashmap-replace returns 0 if the key already existed and 1 if a new one had to be created
function hashmap-replace map key value {
local ret $nil
local row $nil
local hash $nil
local break 0
local i 0
if(&& [!= (typeof $map) (STS_ARRAY)] [!= (typeof $key) (STS_STRING)]) {
stdlib-set-error hashmap "the map arg must be an array and the key must be a string"
}
else {
set $hash (string-hash $key)
loop(&& [< $i (sizeof $map)] [! $break]) {
if(== $hash [get [get $map $i] 0]) {
# set [get [get $map $i] 2] $value
replace (get $map $i) 2 $value
set $ret 0
++ $break
}
++ $i
}
}
# add new key instead
if(== $ret $nil) {
set $ret (hashmap-set $map $key $value)
}
pass $ret
}
function hashmap-remove map key {
local ret 0
local hash $nil
local break 0
local i 0
if(&& [!= (typeof $map) (STS_ARRAY)] [!= (typeof $key) (STS_STRING)]) {
stdlib-set-error hashmap "the map arg must be an array and the key must be a string"
}
else {
set $hash (string-hash $key)
loop(&& [< $i (sizeof $map)] [! $break]) {
if(== $hash [get [get $map $i] 0]) {
remove $map $i
set $ret 1
++ $break
}
++ $i
}
}
pass $ret
}
function hashmap-keys map {
local ret $nil
local i 0
if(!= (typeof $map) (STS_ARRAY)) {
stdlib-set-error hashmap "the map arg must be an array"
}
else {
set $ret [array]
loop(< $i (sizeof $map)) {
insert $ret (sizeof $ret) [get [get $map $i] 1]
++ $i
}
}
pass $ret
}
function hashmap-values map {
local ret $nil
local i 0
if(!= (typeof $map) (STS_ARRAY)) {
stdlib-set-error hashmap "the map arg must be an array"
}
else {
set $ret [array]
loop(< $i (sizeof $map)) {
insert $ret (sizeof $ret) [get [get $map $i] 2]
++ $i
}
}
pass $ret
}
# because keys are passed by reference and stored in the row rather than
# copied into the row, the key could change at some point and thats mostly
# ok, but the string hash needs to be updated as well. Call this if theres
# a possibility that the keys updated in the hashmap
function hashmap-update map {
local ret $nil
local i 0
if(!= $map (STS_ARRAY)) {
stdlib-set-error hashmap "the map arg must be an array"
}
else {
loop(< $i (sizeof $map)) {
set [get [get $map $i] 0] (string-hash [get [get $map $i] 1])
++ $i
}
}
pass $ret
}
# string management =====================
function string-tokenize string token {
local ret [array]
local break 0
local last 0
local found 0
local at 0
loop(&& [<= (+ $last [sizeof $token]) (sizeof $string)] [! $break]) {
set $at (string-search $string $token $last)
if(!= $at -1) {
++ $found
if(== $at 0) {
insert $ret (sizeof $ret) (copy "")
}
else {
if(== $last $at) {
insert $ret (sizeof $ret) (copy "")
}
else {
insert $ret (sizeof $ret) (string-range $string $last [- $at 1])
}
}
set $last (+ $at [sizeof $token])
}
else {
++ $break
}
}
if(! $found) {
insert $ret (sizeof $ret) (copy $string)
}
elseif(< $last [sizeof $string]) { # copy the remaining part of the string if there is any
insert $ret (sizeof $ret) (string-range $string $last [- (sizeof $string) 1])
}
else {
insert $ret (sizeof $ret) (copy "")
}
pass $ret
}
function string-combine array between_string {
local ret $nil
local i 1 # need to start after the first token
if(== (sizeof $array) 1) {
set $ret (copy [get $array 0])
}
elseif(== (sizeof $array) 2) {
set $ret (string [get $array 0] $between_string [get $array 1])
}
else {
set $ret (copy [get $array 0])
#loop(< $i [- (sizeof $array) 1]) {
loop(< $i (sizeof $array)) {
set $ret (string $ret $between_string [get $array $i])
++ $i
}
# set $ret (string $ret $between_string ENDING_REPLACE)
}
pass $ret
}
function string-range string start end {
local ret $nil
local i 0
if(&& [<= $start $end] [< $start (sizeof $string)] [< $end (sizeof $string)] [<= 0 $start] [<= 0 $end]) {
set $i $start
set $ret ""
loop(<= $i $end) {
set $ret (string $ret [get $string $i])
++ $i
}
}
else {
stdlib-set-error string "the range must be inside the bounds of the string length - 1"
}
pass $ret
}
# optionally takes one more argument for an offset from the start
function string-search string needle {
local ret -1
local break 0
local i 0
if(< (sizeof $string) (sizeof $needle)) {
stdlib-set-error string "the needle must be as big or smaller than the source string"
++ $break
}
if(&& [== (sizeof $...) 1] [== (typeof (get $... 0)) (STS_NUMBER)] [< (get $... 0) (sizeof $string)]) {
set $i (get $... 0)
}
loop(&& [< $i [- (sizeof $string) (sizeof $needle) -1]] [! $break]) {
if(== (string-range $string $i (+ $i [- (sizeof $needle) 1])) $needle) {
set $ret $i
++ $break
}
++ $i
}
pass $ret
}
function string-rsearch string needle {
local ret -1
local break 0
local i [- (sizeof $string) (sizeof $needle)]
if(< (sizeof $string) (sizeof $needle)) {
stdlib-set-error string "the needle must be as big or smaller than the source string"
++ $break
}
loop(&& [>= $i 0] [! $break]) {
if(== (string-range $string $i (+ $i [- (sizeof $needle) 1])) $needle) {
set $ret $i
++ $break
}
-- $i
}
pass $ret
}
function string-insert string position instring {
local ret $nil
if(>= $position 0) {
if(>= $position (sizeof $string)) { # append
set $ret (string $string $instring)
}
elseif(&& [< $position (sizeof $string)] [> $position 0]) { # insert at any point after 0
set $ret (string [string-range $string 0 (- $position 1)] $instring [string-range $string $position (- [sizeof $string] 1)])
}
else { # insert at 0
set $ret (string $instring $string)
}
}
else {
stdlib-set-error string "the position must be inside the string bounds"
}
pass $ret
}
function string-remove string position {
local ret $nil
if(&& [>= $position 0] [< $position (sizeof $string)]) {
if(== (- [sizeof $string] 1) $position) { # pop off of the end
set $ret (string-range $string 0 (- (sizeof $string) 2))
}
elseif(&& [< $position (sizeof $string)] [> $position 0]) { # remove at any point after 0
set $ret [string (string-range $string 0 (- $position 1)) (string-range $string (+ $position 1) (- (sizeof $string) 1))]
}
else { # remove at 0
set $ret (string-range $string 1 (- (sizeof $string) 1))
}
}
else {
stdlib-set-error string "the position must be inside the string bounds"
}
pass $ret
}
function string-replace string replacee replacement {
local ret ""
local break 0
local last 0
local replaced 0
local at 0
loop(&& [<= (+ $last [sizeof $replacee]) (sizeof $string)] [! $break]) {
set $at (string-search $string $replacee $last)
if(!= $at -1) {
++ $replaced
if(== $at 0) {
set $ret [string $ret $replacement]
}
else {
if(== $last $at) {
set $ret [string $ret $replacement]
}
else {
set $ret [string $ret (string-range $string $last [- $at 1]) $replacement]
}
}
set $last (+ $at [sizeof $replacee])
}
else {
++ $break
}
}
if(! $replaced) {
set $ret (copy $string)
}
elseif(< $last [sizeof $string]) { # copy the remaining part of the string if there is any
set $ret [string $ret (string-range $string $last [- (sizeof $string) 1])]
}
pass $ret
}
# uncategorized =========================
function num2bin number {
local ret (string "")
local i 0
local observe 0
if(== [typeof $number] [STS_NUMBER]) {
loop (< $i 4) {
set $observe [& (>> $number [* $i 8]) 0xFF]
set $ret (string $ret [asc $observe])
++ $i
}
}
else {
set $ret $nil
}
pass $ret
}
function getpwd self {
local str $nil
local pos 0
set $pos (string-rsearch $self /)
if(< 0 $pos) {
set $str (string-range $self 0 $pos)
}
else {
set $str [string ""]
}
pass $str
}
function relimport self script {
pass (import [string (getpwd $self) $script])
}
function stdlib-get-error {
local ret "no error"
if(== (typeof $stdlib-current-error) (STS_STRING)) {
set $ret (copy $stdlib-current-error)
}
# clear the error
set $stdlib-current-error $nil
pass $ret
}
function stdlib-set-error component_str error_str {
set $stdlib-current-error (string $component_str ": " $error_str)
}
# returns a string of a recursively indexed value. Useful for debugging data structures
function string-value-print value {
local ret $nil
local i 0
if(== (typeof $value) (STS_NIL)) {
if(!= (typeof $ret) (STS_STRING)) {set $ret (string $value)}
}
elseif(== (typeof $value) (STS_FUNCTION)) {
if(!= (typeof $ret) (STS_STRING)) {set $ret (string $value)}
}
elseif(== (typeof $value) (STS_EXTERNAL)) {
if(!= (typeof $ret) (STS_STRING)) {set $ret (string $value)}
}
elseif(== (typeof $value) (STS_ARRAY)) {
if(!= (typeof $ret) (STS_STRING)) {set $ret (string "[array (" (sizeof $value) " elements):")}
loop(< $i (sizeof $value)) {
set $ret (string $ret ", " [string-value-print (get $value $i)])
++ $i
}
set $ret (string $ret "]")
}
elseif(== (typeof $value) (STS_NUMBER)) {
if(!= (typeof $ret) (STS_STRING)) {set $ret (string $value)}
}
elseif(== (typeof $value) (STS_STRING)) {
if(!= (typeof $ret) (STS_STRING)) {set $ret (string "\"" $value "\"")}
}
elseif(== (typeof $value) (STS_BOOLEAN)) {
if(!= (typeof $ret) (STS_STRING)) {set $ret (string $value)}
}
pass $ret
}
function typeof-string value {
local ret $nil
if(== (typeof $value) (STS_NIL)) {
set $ret (copy "STS_NIL")
}
elseif(== (typeof $value) (STS_FUNCTION)) {
set $ret (copy "STS_FUNCTION")
}
elseif(== (typeof $value) (STS_EXTERNAL)) {
set $ret (copy "STS_EXTERNAL")
}
elseif(== (typeof $value) (STS_ARRAY)) {
set $ret (copy "STS_ARRAY")
}
elseif(== (typeof $value) (STS_NUMBER)) {
set $ret (copy "STS_NUMBER")
}
elseif(== (typeof $value) (STS_STRING)) {
set $ret (copy "STS_STRING")
}
elseif(== (typeof $value) (STS_BOOLEAN)) {
set $ret (copy "STS_BOOLEAN")
}
pass $ret
}
function expr { # turn arguments into an expression and evaluate it
local $ret 0
pass $ret
}
function enum {
local i 0
loop(< $i (sizeof $...)) {
global (get $... $i) $i
++ $i
}
}
function += lvalue rvalue {set $lvalue (+ $lvalue $rvalue); pass $lvalue} # because all arguments are passed by value, this passes the value and performs the operation on it
function -= lvalue rvalue {set $lvalue (- $lvalue $rvalue); pass $lvalue}
function *= lvalue rvalue {set $lvalue (* $lvalue $rvalue); pass $lvalue}
function /= lvalue rvalue {set $lvalue (/ $lvalue $rvalue); pass $lvalue}
function %= lvalue rvalue {set $lvalue (% $lvalue $rvalue); pass $lvalue}
function **= lvalue rvalue {set $lvalue (** $lvalue $rvalue); pass $lvalue}