all repos — hex @ next

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

lib/utils.hex

 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
;;;;;; utils.hex
;;;;; Standard Utilities (utils.hex)<a name="utils.hex"></a>
;;;;; This file contains a collection of standardized utility symbols extending the native symbols of the hex programming language.
;;;;; <ul>
;;;;; <li><a href="#utils.hex_input-output">Input/Output Symbols</a></li>
;;;;; <li><a href="#utils.hex_control-flow">Control Flow Symbols</a></li>
;;;;; <li><a href="#utils.hex_quotation">Quotation Symbols</a></li>
;;;;; <li><a href="#utils.hex_stack-management">Stack Management Symbols</a></li>
;;;;; <li><a href="#utils.hex_string">String Symbols</a></li>
;;;;; </ul>

;;;; <a name="utils.hex_input-output"></a>Input/Output Symbols
;;;; These additional symbols are used for input and output operations.

;;; dputs
;; a -> a
;; Duplicates and prints the top item on the stack followed by a new line.
(
  dup puts
) "dputs" ::

;;;; <a name="utils.hex_control-flow"></a>Control Flow Symbols
;;;; These symbols are used for controlling the flow of execution in a program.

;;; unless
;; q1 q2 -> *
;; If %:q1%% pushes $0 on the stack, dequotes %:q2%%.
(
  ()
  swap
  if
) "unless" ::

;;; when
;; q1 q2 -> *
;; If %:q1%% pushes $1 on the stack, dequotes %:q2%%.
(
  ()
  if
) "when" ::

;;;; <a name="utils.hex_quotation"></a>Quotation Symbols
;;;; These additional symbols are used for manipulating quotations.

;;; cons
;; a q1 -> q2
;; Prepends %:a%% to the beginning of %:q1%%.
(
  swap ' swap cat
) "cons" ::

;;; each
;; q1 q2 -> *
;; Applies %:q2%% to each element of %:q1%%.
(
  ($0) cat map drop
) "each" ::

;;; filter
;; q1 q2-> q3
;; Returns %:q3%% containing only the elements of %:q1%% that satisfy %:q2%%.
(
  "_filter_fn" :
  "_filter_list" :
  () "_filter_result" :
  _filter_list (
    "_filter_each_item" :
    (_filter_each_item _filter_fn .)
      (
        _filter_result _filter_each_item push "_filter_result" :
      ) 
    when
  ) each
  _filter_result
  "_filter_fn"     #
  "_filter_list"   #
  "_filter_result" #
) "filter" ::

;; ins
;; q1 a i -> q2
;; Inserts item %:a%% before position %:i%% within a quotation.
(
  "_ins_index" :
  "_ins_item" :
  "_ins_list" :
  $0 "_ins_c" :
  () "_ins_result" :
  _ins_list len "_ins_len" :
  (_ins_index _ins_len >= _ins_index $0 < or)
    ("[symbol ins] Index out of bounds" throw)
  when
  (_ins_c _ins_len <)
    (
      (_ins_c _ins_index ==)
        (_ins_result _ins_item ' cat "_ins_result" :)
      when
      _ins_result _ins_list  _ins_c get ' cat "_ins_result" :
      _ins_c $1 + "_ins_c" :
    )
  while
  _ins_result
  "_ins_index"   #
  "_ins_item"    #
  "_ins_list"    #
  "_ins_c"       #
  "_ins_result"  #
  "_ins_len"     #
) "ins" ::

;;; max
;; q -> a
;; Pushes the maximum item of q on the stack.
(
  "_max_list" :
  _max_list $0 get "_max_result" :
  $1 "_max_c" :
  (_max_c _max_list len <)
    (
      (_max_result _max_list _max_c get <)
        (_max_list _max_c get "_max_result" :)
      when
      _max_c $1 + "_max_c" :
    )
  while
  _max_result
  "_max_list"   #
  "_max_result" #
  "_max_c"      #
) "max" ::

;;; min
;; q -> a
;; Pushes the minimum item of q on the stack.
(
  "_min_list" :
  _min_list $0 get "_min_result" :
  $1 "_min_c" :
  (_min_c _min_list len <)
    (
      (_min_result _min_list _min_c get >)
        (_min_list _min_c get "_min_result" :)
      when
      _min_c $1 + "_min_c" :
    )
  while
  _min_result
  "_min_list"   #
  "_min_result" #
  "_min_c"      #
) "min" ::

;;; pop
;; q1 -> q2
;; Removes the last item at the end of %:q1%%.
(
  "_pop_q" :
  () "_pop_res" :
  $0 "_pop_c" :
  (_pop_c _pop_q len $1 - <)
    (
      _pop_res _pop_q _pop_c get push "_pop_res" :
      _pop_c $1 + "_pop_c" :
    )
  while
  _pop_res
  "_pop_q"   #
  "_pop_res" #
  "_pop_c"   #
) "pop" ::


;;; push
;; q1 a -> q2
;; Appends %:a%% to the end of %:q1%%.
(
  ' cat
) "push" ::

;;; rem
;; q1 i -> q2
;; Removes the item at index %:i%% of %:q1%%.
(
  "_rem_index" :
  "_rem_list"  :
  () "_rem_result" :
  (_rem_index _rem_list len >= _rem_index $0 < or)
    ("[symbol rem] Index out of bounds" throw)
  when
  $0 "_rem_c" :
  (_rem_c _rem_list len <)
    (
      (_rem_c _rem_index !=)
        (_rem_result _rem_list _rem_c get ' cat "_rem_result" :)
      when
      _rem_c $1 + "_rem_c" :
    )
  while
  _rem_result
  "_rem_index"   #
  "_rem_c"       #
  "_rem_list"    #
  "_rem_result"  #
) "rem" ::

;;; reverse
;; q1 -> q2
;; Reverses the order of the items in a quotation.
(
  "_reverse_list" :
  _reverse_list len $1 - "_reverse_c" : 
  () "_reverse_result" :
  (_reverse_c $0 >=)
    (
       _reverse_result _reverse_list _reverse_c get ' cat "_reverse_result" :
       _reverse_c $1 - "_reverse_c" :
    )
  while
  _reverse_result
  "_reverse_list"   #
  "_reverse_c"      # 
  "_reverse_result" # 
) "reverse" ::

;;; set
;; q1 a i -> q2
;; Sets the item at index %:i%% of %:q1%% to %:a%%.
(
  "_set_index" :
  "_set_item"  :
  "_set_list"  :
  () "_set_result" :
  (_set_index _set_list len >= _set_index $0 < or)
    ("[symbol set] Index out of bounds" throw)
  when
  $0 "_set_c" :
  (_set_c _set_list len <)
    (
      (_set_c _set_index ==)
        (_set_result _set_item ' cat "_set_result" :)
        (_set_result _set_list  _set_c get ' cat "_set_result" :)
      if
      _set_c $1 + "_set_c" :
    )
  while
  _set_result
  "_set_index"   #
  "_set_c"       #
  "_set_item"    #
  "_set_list"    #
  "_set_result"  #
) "set" ::

;;; sort
;; q1 q2 -> q3
;; Sorts the items of q1 based on the comparison quotation q2 (must push $0 or $1 on the stack).
(
  "_sort_check"            :
  "_sort_list"             :
  _sort_list len "_sort_n" :
  $0 "_sort_i"            :
  $1 "_sort_swapped"      :
  (_sort_swapped)
    (
      $0 "_sort_swapped"    :
      $0 "_sort_i"          :
      (_sort_i _sort_n $1 - <)
        (
          _sort_list _sort_i get "_sort_current" :
          _sort_list _sort_i $1 + get "_sort_next" :
          (_sort_current _sort_next _sort_check .)
            (
              _sort_list 
                _sort_current _sort_i $1 + set
                _sort_next _sort_i set
              "_sort_list" :
              $1 "_sort_swapped" :
            )
          when
          _sort_i $1 + "_sort_i" :
          "_sort_current" #
          "_sort_next"    #
        )
      while
    )
  while
  _sort_list
  "_sort_check"     #
  "_sort_list"      #
  "_sort_n"         #
  "_sort_i"         #
  "_sort_swapped"   #
) "sort" ::

;;; swons
;; q1 a -> q2
;; Prepends %:a%% to the beginning of %:q1%%.
(
  ' swap cat
) "swons" ::


;;; times
;; q i -> *
;; Execute quotation %:q%% %:i%% times.
(
  "_times_i" :
  "_times_q" :
  (_times_i $0 >)
    (
      _times_q . 
      _times_i $1 - "_times_i" :
    )
  while
  "_times_q" #
  "_times_i" #
) "times" ::

;;; uncons
;; q1 -> a q2
;; Removes the first item from %:q1%% and pushes it on the stack along with %:q2%% containing the rest of the items of %:q1%%.
(
  "_uncons_list" :
  () "_uncons_result" :
  $1 "_uncons_c" :
  (_uncons_c _uncons_list len <)
    (
      _uncons_result _uncons_list _uncons_c get push "_uncons_result" :
      _uncons_c $1 + "_uncons_c" :
    )
  while
  _uncons_list $0 get ; Push the first item
  _uncons_result ; Push the rest of the items
  "_uncons_list"   #
  "_uncons_result" #
  "_uncons_c"      #
) "uncons" ::


;;; unswons
;; q1 -> q2 a
;; Removes the first item from %:q1%% and pushes it on the stack along with %:q2%% containing the rest of the items of %:q1%%.
(
  "_unswons_list" :
  () "_unswons_result" :
  $1 "_unswons_c" :
  (_unswons_c _unswons_list len <)
    (
      _unswons_result _unswons_list _unswons_c get push "_unswons_result" :
      _unswons_c $1 + "_unswons_c" :
    )
  while
  _unswons_result ; Push the rest of the items
  _unswons_list $0 get ; Push the first item
  "_unswons_list"   #
  "_unswons_result" #
  "_unswons_c"      #
) "unswons" ::

;;;; <a name="utils.hex_stack-management"></a>Stack Management Symbols
;;;; These additional symbols are used for manipulating the stack.

;;; dip
;; a1 (a2) -> a2 a1
;; Dequotes the first item on the stack and restores the second item on top.
(
  swap ' cat .
) "dip" ::

;;; over
;; a1 a2 -> a1 a2 a1
;; Copies the second item on the stack and pushes it on top
(
  "_over_a" :
  dup _over_a swap
  "_over_a" #
) "over" ::

;;;; <a name="utils.hex_string"></a>String Symbols
;;;; These additional symbols are used for manipulating strings.

;;; begins
;; s1 s2 -> s3
;; Pushes $!1$$ on the stack if %:s1%% begins with %:s2%%, or $!0$$ otherwise.
(
  index $0 ==
) "begins" ::

;;; ends
;; s1 s2 -> s3
;; Pushes $!1$$ on the stack if %:s1%% ends with %:s2%%, or $!0$$ otherwise.
(
  "_ends_suffix" :
  "_ends_s"      :
  _ends_s _ends_suffix index 
  _ends_s len _ends_suffix len -
  ==
  "_ends_suffix" #
  "_ends_s"      #
) "ends" ::

;;; fmt
;; s1 q -> s2
;; Substitutes %:$\{0\}%% to %%$\{9\}%% placeholders in %:s1%% with items in %:q%%.
(
  "_fmt_q" :
  "_fmt_s" :
  $0 "_fmt_c" :
  (_fmt_q len $a >)
    ("[symbol fmt] Only a maximum of 10 placeholders are supported" throw)
  when
  (_fmt_c _fmt_q len <)
    (
      _fmt_s "${" _fmt_c str cat "}" cat _fmt_q _fmt_c get gsub "_fmt_s" :
      _fmt_c $1 + "_fmt_c" :
    )
  while
  _fmt_s
  "_fmt_s" #
  "_fmt_q" #
  "_fmt_c" #
) "fmt" ::

;;; gsub
;; s1 s2 s3 -> s4
;; Replaces all occurrences of %:s2%% with %%s3%% in %:s1%%.
(
  "_gsub_rep"  :
  "_gsub_src"  :
  "_gsub_text" :
  (_gsub_text _gsub_src index $0 >=)
    (_gsub_text _gsub_src _gsub_rep sub "_gsub_text" :)
  while
  _gsub_text
  "_gsub_rep"  #
  "_gsub_src"  #
  "_gsub_text" #
) "gsub" ::

;;; slice
;; s1 i1 i2 -> s2
;; Extracts the portion of the string between indices %:i1%% and %:i2%%.
(
  "_slice_end" :
  "_slice_start" :
  "_slice_str" :
  "" "_slice_result" :
  (_slice_end _slice_str len >= _slice_start $0 < or)
    ("[symbol slice] Index out of bounds" throw)
  when
  (_slice_start _slice_end <=)
    (
      _slice_result _slice_str _slice_start get cat "_slice_result" : 
      _slice_start $1 + "_slice_start" :
    )
  while
  _slice_result
  "_slice_end"    #
  "_slice_start"  #
  "_slice_str"    #
  "_slice_result" #
) "slice" ::