all repos — min @ 8bf2154657c0209e53cf153865a0874223de1ee9

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("has?") 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("get") 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("get-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("set") 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("set-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("del") 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("keys") do (i: In):
    let vals = i.expect("dict")
    let d = vals[0]
    i.push i.keys(d)

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

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

  def.symbol("dup") 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("pick") 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("type") do (i: In):
    let vals = i.expect("dict")
    i.push vals[0].objType.newVal

  def.finalize("dict")