all repos — hex @ b389ca018fe6ed4ed28057e1705baec630f075c7

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
;;; dputs
;; a -> a
;; Duplicates and prints (with newline) the top item on the stack.
(dup puts) "dputs" ::

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

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

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

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

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

;;; 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" ::

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

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

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

;; insert
;; q1 a i -> q2
;; Inserts item %:a%% at position %:i%% within a quotation.
(
  i "_insert_index" :
  "_insert_item" :
  i "_insert_list" :
  0x0 "_insert_c" :
  () "_insert_result" :
  _insert_list len "_insert_len" :
  (_insert_index _insert_len >= _insert_index 0x0 < or)
    ("[symbol insert] Index out of bounds" throw)
  when
  (_insert_c _insert_len <)
    (
      (_insert_c _insert_index ==)
        (_insert_result _insert_item ' cat "_insert_result" :)
      when
      _insert_result _insert_list  _insert_c get ' cat "_insert_result" :
      _insert_c 0x1 + "_insert_c" :
    )
  while
  _insert_result
  "_insert_index"   #
  "_insert_item"    #
  "_insert_list"    #
  "_insert_c"       #
  "_insert_result"  #
  "_insert_len"     #
) "insert" ::

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

;;; sort
;; q1 q2 -> q3
;; Sorts the items of a quotation.
(
  "_sort_check" :
  "_sort_list" :
  () "_sort_result" :
  _sort_list _len 0x1 - "_sort_pivot" :
  0x0 "_sort_c" :
  () "_sort_left" :
  () "_sort_right" :
  (0x1 _sort_list len <=)
    (_sort_list)
    (
      (_sort_c len <)
        (
          (_sort_list _sort_c get _sort_pivot _sort_check .)
            (_sort_left _sort_item push "_sort_left" :)
            (_sort_right _sort_item push "_sort_right" :)
          if
          _sort_c 0x1 + "_sort_c" :
        )
      while
      _sort_left sort _sort_pivot ' _sort_right sort 
      cat cat "_sort_result" :
    )
  if
  _sort_result
  "_sort_check"  #
  "_sort_list"   #
  "_sort_result" #
  "_sort_pivot"  #
  "_sort_c"      #
  "_sort_left"   #
  "_sort_right"  #
) "sort" ::

;;; 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 0x0 >=)
    (_gsub_text _gsub_src _gsub_rep replace "_gsub_text" :)
  while
  _gsub_text
  "_gsub_rep"  #
  "_gsub_src"  #
  "_gsub_text" #
) "gsub" :

;;; min
;; q -> a
;; Pushes the minimum item in a quotation on the stack.
(
  "_min_list" :
  () "_min_result" :
  0x0 get "_min_result" :
  0x1 "_min_c" :
  (_min_c len <)
    (
      (_min_result _min_list _min_c get <)
        (_min_list _min_c get "_min_result" :)
      when
    )
  while
  _min_result
  "_min_list"   #
  "_min_result" #
  "_min_result" #
  "_min_c"      #
) "min" ::

;;; max
;; q -> a
;; Pushes the maximum item in a quotation on the stack.
(
  "_max_list" :
  () "_max_result" :
  0x0 get "_max_result" :
  0x1 "_max_c" :
  (_max_c len <)
    (
      (_max_result _max_list _max_c get >)
        (_max_list _max_c get "_max_result" :)
      when
    )
  while
  _max_result
  "_max_list"   #
  "_max_result" #
  "_max_result" #
  "_max_c"      #
) "max" ::

;;; intpl
;; s1 q -> s2
;; Substitutes %:$0%% to %%$9%% placeholders in %:s1%% with items in %:q%%.
(
  "_intpl_s" :
  "_intpl_q" :
  0x0 "_intpl_c" :
  (_intpl_c _intpl_s len <)
     (
       _intpl_s "$" _intpl_c str cat _intpl_list _intpl_c get cat gsub "_intpl_s" :
       _intpl_c 0x1 + "_intpl_c" :
     )
   while
  "_intpl_s" #
  "_intpl_q" #
  "_intpl_c" #
) "intpl" ::

;;; each
;; q1 q2 -> *
;; Applies %:q2%% to each element of %:q1%%.
(
  0x0 ' cat map pop
) "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" ::

;;; slice
;; s1 i1 i2 -> s2
;; Extracts the portion of the string between indices %:i1%% and %:i2%%.
(
  "_slice_end" :
  "_slice_start" :
  "_slice_str" :
  (_slice_start _slice_end <)
    (0x0 "_slice_result" :)
    (
      _slice_str _slice_start get _slice_result : 
      _slice_start 0x1 + "_slice_start" :
    )
  while
  "_slice_end"   #
  "_slice_start" #
  "_slice_str"   #
) "slice" ::