all repos — min @ e109d19de0a5cbc4a24240c4fe81f32f2b87ca3f

A small but practical concatenative programming language.

core/utils.nim

 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
import 
  tables, 
  strutils, 
  macros, 
  critbits, 
  json, 
  os, 
  regex
import 
  types, 
  parser, 
  interpreter

proc isSymbol*(s: MinValue): bool =
  return s.kind == minSymbol

proc isQuotation*(s: MinValue): bool = 
  return s.kind == minQuotation

proc isString*(s: MinValue): bool = 
  return s.kind == minString

proc isFloat*(s: MinValue): bool =
  return s.kind == minFloat

proc isInt*(s: MinValue): bool =
  return s.kind == minInt

proc isNumber*(s: MinValue): bool =
  return s.kind == minInt or s.kind == minFloat

proc isBool*(s: MinValue): bool =
  return s.kind == minBool

proc isStringLike*(s: MinValue): bool =
  return s.isSymbol or s.isString or (s.isQuotation and s.qVal.len == 1 and s.qVal[0].isSymbol)

proc isDictionary*(q: MinValue): bool =
  if not q.isQuotation:
    return false
  if q.qVal.len == 0:
    return true
  for val in q.qVal:
    if not val.isQuotation or val.qVal.len != 2 or not val.qVal[0].isSymbol:
      return false
  return true

# Constructors

proc newVal*(s: string): MinValue =
  return MinValue(kind: minString, strVal: s)

proc newVal*(s: cstring): MinValue =
  return MinValue(kind: minString, strVal: $s)

proc newVal*(q: seq[MinValue]): MinValue =
  return MinValue(kind: minQuotation, qVal: q)

proc newVal*(s: BiggestInt): MinValue =
  return MinValue(kind: minInt, intVal: s)

proc newVal*(s: BiggestFloat): MinValue =
  return MinValue(kind: minFloat, floatVal: s)

proc newVal*(s: bool): MinValue =
  return MinValue(kind: minBool, boolVal: s)

proc newSym*(s: string): MinValue =
  return MinValue(kind: minSymbol, symVal: s)

proc newQuotation*(): MinValue = 
  return MinValue(kind: minQuotation, qVal: newSeq[MinValue](0))

# Error Helpers

proc raiseInvalid*(msg: string) =
  raise MinInvalidError(msg: msg)

proc raiseUndefined*(msg: string) =
  raise MinUndefinedError(msg: msg)

proc raiseOutOfBounds*(msg: string) =
  raise MinOutOfBoundsError(msg: msg)

proc raiseRuntime*(msg: string, qVal: var seq[MinValue]) =
  raise MinRuntimeError(msg: msg, qVal: qVal)

proc raiseEmptyStack*() =
  raise MinEmptyStackError(msg: "Insufficient items on the stack")

proc filetype*(p: PathComponent): string =
  case p
  of pcFile:
    return "file"
  of pcLinkToFile:
    return "filelink"
  of pcDir:
    return "dir"
  of pcLinkToDir:
    return "dirlink"

proc unixPermissions*(s: set[FilePermission]): int =
  result = 0
  for p in s:
    case p:
    of fpUserRead:
      result += 400
    of fpUserWrite:
      result += 200
    of fpUserExec:
      result += 100
    of fpGroupRead:
      result += 40
    of fpGroupWrite:
      result += 20
    of fpGroupExec:
      result += 10
    of fpOthersRead:
      result += 4
    of fpOthersWrite:
      result += 2
    of fpOthersExec:
      result += 1

proc toFilePermissions*(p: BiggestInt): set[FilePermission] =
  let user = ($p)[0].int
  let group = ($p)[1].int
  let others = ($p)[2].int
  if user == 1:
    result.incl fpUserExec
  if user == 2:
    result.incl fpUserWrite
  if user == 3:
    result.incl fpUserExec
    result.incl fpUserWrite
  if user == 4:
    result.incl fpUserRead
  if user == 5:
    result.incl fpUserRead
    result.incl fpUserExec
  if user == 6:
    result.incl fpUserRead
    result.incl fpUserWrite
  if user == 7:
    result.incl fpUserRead
    result.incl fpUserWrite
    result.incl fpUserExec
  if group == 1:
    result.incl fpGroupExec
  if group == 2:
    result.incl fpGroupWrite
  if group == 3:
    result.incl fpGroupExec
    result.incl fpGroupWrite
  if group == 4:
    result.incl fpGroupRead
  if group == 5:
    result.incl fpGroupRead
    result.incl fpGroupExec
  if group == 6:
    result.incl fpGroupRead
    result.incl fpGroupWrite
  if group == 7:
    result.incl fpGroupRead
    result.incl fpGroupWrite
    result.incl fpGroupExec
  if others == 1:
    result.incl fpOthersExec
  if others == 2:
    result.incl fpOthersWrite
  if others == 3:
    result.incl fpOthersExec
    result.incl fpOthersWrite
  if others == 4:
    result.incl fpOthersRead
  if others == 5:
    result.incl fpOthersRead
    result.incl fpOthersExec
  if others == 6:
    result.incl fpOthersRead
    result.incl fpOthersWrite
  if others == 7:
    result.incl fpOthersRead
    result.incl fpOthersWrite
    result.incl fpOthersExec

proc getString*(v: MinValue): string =
  if v.isSymbol:
    return v.symVal
  elif v.isString:
    return v.strVal
  elif v.isQuotation:
    if v.qVal.len != 1:
      raiseInvalid("Quotation is not a quoted symbol")
    let sym = v.qVal[0]
    if sym.isSymbol:
      return sym.symVal
    else:
      raiseInvalid("Quotation is not a quoted symbol")

proc warn*(s: string) =
  stderr.writeLine s

proc previous*(scope: ref MinScope): ref MinScope =
  if scope.parent.isNil:
    return scope #### was: ROOT
  else:
    return scope.parent

proc replace*[T](c: var CritBitTree, s: string, v: T): T {.discardable.}=
  if c.hasKey(s):
    c.excl(s)
  c[s] = v

proc define*(i: In, name: string): ref MinScope =
  var scope = new MinScope
  scope.name = name
  scope.parent = i.scope
  return scope

proc symbol*(scope: ref MinScope, sym: string, p: MinOperatorProc): ref MinScope =
  scope.symbols[sym] = MinOperator(prc: p, kind: minProcOp, sealed: true)
  return scope

proc symbol*(scope: ref MinScope, sym: string, v: MinValue): ref MinScope =
  scope.symbols[sym] = MinOperator(val: v, kind: minValOp, sealed: true)
  return scope

proc sigil*(scope: ref MinScope, sym: string, p: MinOperatorProc): ref MinScope =
  scope.previous.sigils[sym] = MinOperator(prc: p, kind: minProcOp, sealed: true)
  return scope

proc sigil*(scope: ref MinScope, sym: string, v: MinValue): ref MinScope =
  scope.previous.sigils[sym] = MinOperator(val: v, kind: minValOp, sealed: true)
  return scope

proc finalize*(scope: ref MinScope) =
  var mdl = newSeq[MinValue](0).newVal
  mdl.scope = scope
  let op = proc(i: In) {.gcsafe, closure.} =
    i.evaluating = true
    i.push mdl
    i.evaluating = false
  mdl.scope.previous.symbols[scope.name] = MinOperator(kind: minProcOp, prc: op)

template alias*[T](varname: untyped, value: var T) =
  var varname {.inject.}: type(value)
  shallowCopy varname, value

proc to*(q: MinValue, T: typedesc): T =
  return cast[T](q.obj)

proc `%`*(c: CritBitTree[string]): JsonNode =
  result = newJObject()
  for key, value in c.pairs:
    result[key] = %value

proc critbit*(o: JsonNode): CritBitTree[string] =
  for key, value in o.pairs:
    result[key] = value.getStr

proc `%`*(a: MinValue): JsonNode =
  case a.kind:
    of minBool:
      return %a.boolVal
    of minSymbol:
      return %(";sym:$1" % [a.symVal])
    of minString:
      return %a.strVal
    of minInt:
      return %a.intVal
    of minFloat:
      return %a.floatVal
    of minQuotation:
      if a.isDictionary:
        result = newJObject()
        for i in a.qVal:
          result[$i.qVal[0].symVal] = %i.qVal[1]
      else:
        result = newJArray()
        for i in a.qVal:
          result.add %i

proc fromJson*(json: JsonNode): MinValue = 
  case json.kind:
    of JNull:
      result = newSeq[MinValue](0).newVal
    of JBool: 
      result = json.getBVal.newVal
    of JInt:
      result = json.getNum.newVal
    of JFloat:
      result = json.getFNum.newVal
    of JString:
      let s = json.getStr
      if s.match("^;sym:"):
        result = regex.replace(s, "^;sym:", "").newSym
      else:
        result = json.getStr.newVal
    of JObject:
      var res = newSeq[MinValue](0)
      for key, value in json.pairs:
        res.add @[key.newSym, value.fromJson].newVal
      return res.newVal
    of JArray:
      var res = newSeq[MinValue](0)
      for value in json.items:
        res.add value.fromJson
      return res.newVal

# Dictionary Methods

proc dget*(q: MinValue, s: MinValue): MinValue =
  # Assumes q is a dictionary
  for v in q.qVal:
    if v.qVal[0].getString == s.getString:
      return v.qVal[1]
  raiseInvalid("Key '$1' not found" % [s.getString])

proc ddel*(q: var MinValue, s: MinValue): MinValue =
  # Assumes q is a dictionary
  var found = false
  var c = -1
  for v in q.qVal:
    c.inc
    if v.qVal[0].getString == s.getString:
      found = true
      break
  if found:
    q.qVal.delete(c)
  return q
      
proc dset*(q: var MinValue, s: MinValue, m: MinValue): MinValue {.discardable.}=
  # Assumes q is a dictionary
  var found = false
  var c = -1
  for v in q.qVal:
    c.inc
    if v.qVal[0].getString == s.getString:
      found = true
      break
  if found:
      q.qVal.delete(c)
      q.qVal.insert(@[s.getString.newSym, m].newVal, c)
  return q

proc keys*(q: MinValue): MinValue =
  # Assumes q is a dictionary
  result = newSeq[MinValue](0).newVal
  for v in q.qVal:
    result.qVal.add v.qVal[0]

proc values*(q: MinValue): MinValue =
  # Assumes q is a dictionary
  result = newSeq[MinValue](0).newVal
  for v in q.qVal:
    result.qVal.add v.qVal[1]

# Validators

proc reqBool*(i: var MinInterpreter, a: var MinValue) =
  a = i.pop
  if not a.isBool:
    raiseInvalid("A bool value is required on the stack")

proc reqTwoBools*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not a.isBool or not b.isBool:
    raiseInvalid("Two bool values are required on the stack")

proc reqInt*(i: var MinInterpreter, a: var MinValue) =
  a = i.pop
  if not a.isInt:
    raiseInvalid("An integer is required on the stack")

proc reqNumber*(i: var MinInterpreter, a: var MinValue) =
  a = i.pop
  if not a.isNumber:
    raiseInvalid("A number is required on the stack")

proc reqTwoInts*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not a.isInt or not b.isInt:
    raiseInvalid("Two integers are required on the stack")

proc reqQuotation*(i: var MinInterpreter, a: var MinValue) =
  a = i.pop
  if not a.isQuotation:
    raiseInvalid("A quotation is required on the stack")

proc reqIntAndQuotation*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not (a.isInt and b.isQuotation):
    raiseInvalid("An integer and a quotation are required on the stack")

proc reqTwoNumbers*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not (a.isNumber and b.isNumber):
    raiseInvalid("Two numbers are required on the stack")

proc reqTwoNumbersOrStrings*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not (a.isString and b.isString or a.isNumber and b.isNumber):
    raiseInvalid("Two numbers or two strings are required on the stack")

proc reqStringAndNumber*(i: var MinInterpreter, a, b: var MinValue) =
  b = i.pop
  a = i.pop
  if not (a.isString and b.isNumber):
    raiseInvalid("A string and a number are required on the stack")

proc reqString*(i: var MinInterpreter, a: var MinValue) =
  a = i.pop
  if not a.isString:
    raiseInvalid("A string is required on the stack")

proc reqStringLikeAndQuotation*(i: var MinInterpreter, a, q: var MinValue) =
  a = i.pop
  q = i.pop
  if not a.isStringLike or not q.isQuotation:
    raiseInvalid("A string or symbol and a quotation are required on the stack")

proc reqQuotationAndString*(i: var MinInterpreter, q, a: var MinValue) =
  q = i.pop
  a = i.pop
  if not a.isString or not q.isQuotation:
    raiseInvalid("A string and a quotation are required on the stack")

proc reqStringOrQuotation*(i: var MinInterpreter, a: var MinValue) =
  a = i.pop
  if not a.isQuotation and not a.isString:
    raiseInvalid("A quotation or a string is required on the stack")

proc reqStringLike*(i: var MinInterpreter, a: var MinValue) =
  a = i.pop
  if not a.isStringLike:
    raiseInvalid("A quoted symbol or a string is required on the stack")

proc reqTwoStrings*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not a.isString or not b.isString:
    raiseInvalid("Two strings are required on the stack")

proc reqTwoStringLike*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not a.isStringLike or not b.isStringLike:
    raiseInvalid("Two symbols or strings are required on the stack")

proc reqThreeStrings*(i: var MinInterpreter, a, b, c: var MinValue) =
  a = i.pop
  b = i.pop
  c = i.pop
  if not a.isString or not b.isString or not c.isString: 
    raiseInvalid("Three strings are required on the stack")

proc reqTwoQuotations*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not a.isQuotation or not b.isQuotation:
    raiseInvalid("Two quotations are required on the stack")

proc reqTwoQuotationsOrStrings*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not (a.isQuotation and b.isQuotation or a.isString and b.isString):
    raiseInvalid("Two quotations or two strings are required on the stack")

proc reqThreeQuotations*(i: var MinInterpreter, a, b, c: var MinValue) =
  a = i.pop
  b = i.pop
  c = i.pop
  if not a.isQuotation or not b.isQuotation or not c.isQuotation: 
    raiseInvalid("Three quotations are required on the stack")

proc reqFourQuotations*(i: var MinInterpreter, a, b, c, d: var MinValue) =
  a = i.pop
  b = i.pop
  c = i.pop
  d = i.pop
  if not a.isQuotation or not b.isQuotation or not c.isQuotation or not d.isQuotation:
    raiseInvalid("Four quotations are required on the stack")

proc reqTwoSimilarTypesNonSymbol*(i: var MinInterpreter, a, b: var MinValue) =
  a = i.pop
  b = i.pop
  if not ((a.kind == a.kind or (a.isNumber and a.isNumber)) and not a.isSymbol):
    raiseInvalid("Two non-symbol values of similar type are required on the stack")

proc reqDictionary*(i: In, q: var MinValue) =
  q = i.pop
  if not q.isDictionary:
    raiseInvalid("An dictionary is required on the stack")