all repos — min @ d0d644faeeecdd1812927dbd61b3061ab3e81187

A small but practical concatenative programming language.

minpkg/lib/min_dict.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
import
  std/critbits
import
  ../core/parser,
  ../core/value,
  ../core/interpreter,
  ../core/utils

proc dict_module*(i: In) =

  let def = i.define()

  def.symbol("dhas?") do (i: In):
    let vals = i.expect("'sym", "dict")
    let k = vals[0]
    let d = vals[1]
    i.push d.dhas(k).newVal

  def.symbol("dget") do (i: In):
    let vals = i.expect("'sym", "dict")
    let k = vals[0]
    let d = vals[1]
    i.push i.dget(d, k)

  def.symbol("dget-raw") do (i: In):
    let vals = i.expect("'sym", "dict")
    let k = vals[0]
    let d = vals[1]
    let v = i.dget(d, k)
    var rv = newDict(i.scope)
    rv.objType = "rawval"
    i.dset(rv, "type", v.typeName.newVal)
    i.dset(rv, "val", v)
    i.dset(rv, "str", newVal($v))
    i.push rv

  def.symbol("dset") do (i: In):
    let vals = i.expect("'sym", "a", "dict")
    let k = vals[0]
    let m = vals[1]
    var d = vals[2]
    i.push i.dset(d, k, m)

  def.symbol("dset-sym") do (i: In):
    let vals = i.expect("'sym", "'sym", "dict")
    let k = vals[0]
    let m = newSym(vals[1].getString)
    var d = vals[2]
    i.push i.dset(d, k, m)

  def.symbol("ddel") do (i: In):
    let vals = i.expect("'sym", "dict")
    let k = vals[0]
    var d = vals[1]
    i.push i.ddel(d, k)

  def.symbol("dkeys") do (i: In):
    let vals = i.expect("dict")
    let d = vals[0]
    i.push i.keys(d)

  def.symbol("dvalues") do (i: In):
    let vals = i.expect("dict")
    let d = vals[0]
    i.push i.values(d)

  def.symbol("dpairs") do (i: In):
    let vals = i.expect("dict")
    let d = vals[0]
    i.push i.pairs(d)

  def.symbol("ddup") do (i: In):
    let vals = i.expect("dict")
    let d = vals[0]
    var r = newDict(i.scope)
    for item in d.dVal.pairs:
      r.scope.symbols[item.key] = item.val
    i.push r

  def.symbol("dpick") do (i: In):
    let vals = i.expect("quot", "dict")
    var q = vals[0]
    var d = vals[1]
    var res = newDict(i.scope)
    for k in q.qVal:
      if d.dhas(k):
        i.dset(res, k, i.dget(d, k))
    i.push res

  def.symbol("dtype") do (i: In):
    let vals = i.expect("dict")
    i.push vals[0].objType.newVal

  def.finalize("dict")