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 |
import critbits import ../core/parser, ../core/value, ../core/interpreter, ../core/utils # Operations on dictionaries 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("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("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("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.sigil("?") do (i: In): i.push("dhas?".newSym) def.sigil("/") do (i: In): i.push("dget".newSym) def.sigil("%") do (i: In): i.push("dset".newSym) def.finalize("dict") |