all repos — hex @ af3eb02d6c082cc6f269a04021643b94287ee1d0

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
;;; fail
;; s -> 
;; Throws an error and prints %:s%% to stderr.
(
  warn
  0x1 exit
) "fail" ::

;;; _
;; a -> a
;; Duplicates and prints (with newline) the top item on the stack.
(dup puts) "_" ::

;; isi
;; a -> i
;; Pushes %%0x1%% on the stack if %:a%% is an integer, %%0x0%% otherwise.
(
  type "integer" ==
) "isi" ::

;; iss
;; a -> i
;; Pushes $0x1$$ on the stack if %:a%% is a string, $0x0$$ otherwise.
(
  type "string" ==
) "iss" ::

;; isq
;; a -> i
;; Pushes $0x1$$ on the stack if %%a%% is a quotation, $0x0$$ otherwise.
(
  type "quotation" ==
) "isq" ::

;; insert
;; (q1|s1) a i -> (q2|s2)
;; Inserts item %:a%% at position %:i%% within a quotation or string.
(
  "_index" :
  "_item" :
  "_list" :
  0x0 "_is-str" :
  (_index isi not)
    ("[symbol insert] Integer index required" fail .)
  when
  (_list isi)
    ("[symbol insert] String or quotation required" fail)
  when
  _list len "_len" :
  (_index dup _len >= 0x0 < and)
    ("[symbol insert] Index out of bounds" fail)
  when
  (_list iss)
    ( 
      0x1 "_is-str" :
      (_item iss not)
        ("[symbol insert] A string item is required" fail)
      when
      _list "" split "_list" :
    )
  when
  0x0 "_c" :
  () "_result" :
  (_c _len <)
    (
      (_c _index ==)
        (_result _item ' cat "_result" :)
      when
      _result _list _c get ' cat "_result" :
      _c 0x1 + "_c" :
    )
  while
  (_is-str)
    (_result "" join "_result" :)
  when
  _result
  "_len" #
  "_is-str" #
  "_c" #
  "_result" #
  "_index" #
  "_item" #
  "_list" #
) "insert" ::


;;; push
;; (q1|s1) a -> (q2|s2)
;; Pushes %:a%% to the end of a quotation or string.
(
  "_item" :
  "_list" :
  0x0 "_is-str" :
  (_list isi)
    ("[symbol push] String or quotation required" fail)
  when
  (_list iss)
    (
      (_item iss not)
        ("[symbol push] A string item is required" fail)
      when
      0x1 "_is-str" :
      _list "" split "_list" :
    )
  when
  _list _item ' cat
  (_is-str)
    ( "" join)
  when
  "_item" #
  "_list" #
  "_is-str" #
) "push" ::


;;; reverse
;; (q1|s1) -> (q2|s2)
;; Reverses the order of the elements in a string or quotation.
(
  "_list" :
  (_list isi)
    ("[symbol reverse] String or quotation required" fail)
  when
  (_list iss)
    (_list "" split "_list" :)
  when
  _list len 0x1 - "_c" : 
  () "_result" :
  (_c 0x0 <=)
    (
       _result _list _c get ' cat "_result" :
       _c 0x1 - "_c" :
    )
  while
  _result
  (_list iss)
    (_result "" join "_result" :)
  when
  "_list" #
  "_c" #
  "_result" #
) "reverse" ::



;;; sort
;; (q1|s1) q2 -> (q3|s2)
;; Sorts the items of a quotation or string.
(
  "_check" :
  "_list" :
  (_list isi)
    ("[symbol sort] String or quotation required" fail)
  when
  (_list iss)
    (_list "" split "_list" :)
  when
  _list len "_len" :
  _list _len 0x1 - "_pivot" :
  0x0 "_c" :
  () "_left" :
  () "_right" :
  (0x1 len <=)
    (_list)
    (
      (_c len <)
        (
          (_list _c get _pivot _check .)
            (_left _item push "_left" :)
            (_right _item push "_right" :)
          if
          _c 0x1 + "_c" :
        )
      while
      _left sort pivot ' _right sort 
      cat cat "_result" :
    )
  if
  (_list iss)
    (_result "" join "_result" :)
  when
  _result
  "_c"      #
  "_left"   #
  "_righr"  #
  "_pivot"  #
  "_len"    #
  "_check"  #
  "_list"   #
  "_result" #
) "sort" ::


;;; replace-all
;; s1 s2 s3 -> s4
;; Replaces all occurrences of %:s2%% with %%s3%% in %:s1%%.
(
  "_rep"  :
  "_src"  :
  "_text" :
  (_text iss _src iss _rep iss and and not)
    ("[symbol replace-all] Three strings required." fail)
  when
  (_text _src index 0x0 >=)
    (_text _src _rep replace "_text" :)
  while
  _text
  "_rep"  #
  "_src"  #
  "_text" #
) "replace-all" :

;;; min
;; q -> a
;; Pushes the minimum item in a quotation on the stack.
(
  "_list" :
  () "_result" :
  (_list isq)
    ("[symbol min] Quotation required" fail)
  when
  (_list len 0x0 ==)
    ("[symbol min] Not enough items" fail)
  when
  0x0 get "_result" :
  0x1 "_c" :
  _list len "_len" :
  (_c len <)
    (
      (_result _list _c get <)
        (_list _c get "_result" :)
      when
    )
  while
  _result
  "_list" #
  "_result" #
  "_len" #
  "_c" #
) "min" ::

;;; max
;; q -> a
;; Pushes the maximum item in a quotation on the stack.
(
  "_list" :
  () "_result" :
  (_list isq not)
    ("[symbol max] Quotation required" fail)
  when
  (_list len 0x0 ==)
    ("[symbol max] Not enough items" fail)
  when
  0x0 get "_result" :
  0x1 "_c" :
  _list len "_len" :
  (_c len <)
    (
      (_result _list _c get >)
        (_list _c get "_result" :)
      when
    )
  while
  _result
  "_list" #
  "_result" #
  "_len" #
  "_c" #
) "max" ::

;;; intpl
;; s1 q -> s2
;; Substitutes %:$0%% to %%$9%% placeholders in %:s1%% with items in %:q%%.
(
  "_s" :
  "_q" :
  (_s iss _q isq and not)
    ("[symbol intpl] String and quotation required" fail)
  when 
  _q len "_len" :
  0x0 "_c" :
  (_c len <)
     (
       _s "$" _c str cat _list _c get cat replace-all "_s" :
       _c 0x1 + "_c" :
     )
   while
   "_s" #
   "_q" #
   "_c" #
   "_len" #
) "intpl" ::

;;; each
;; q1 q2 -> *
;; Applies %:q2%% to each element of %:q1%%.
(
  "_fn" :
  "_list" :
  (_fn isq _list isq and not)
    ("[symbol each] Two quotations required" fail)
   when
   _list len "_len" :
   0x0 "_c" :
   (_c len <)
     (
       _list _c get _fn .
       _c 0x1 + "_c" :
     )
   while
   "_fn" #
   "_list" #
   "_len" # 
   "_c" #
) "each" ::