min_preserves: add record and set constructors
This commit is contained in:
parent
e572357dcb
commit
e52a6dc5ba
|
@ -1,5 +1,5 @@
|
|||
# Package
|
||||
version = "20240423"
|
||||
version = "20240424"
|
||||
author = "Emery Hemingway"
|
||||
description = "Syndicate bindings for the Min language"
|
||||
license = "unlicense"
|
||||
|
|
|
@ -6,7 +6,11 @@ import
|
|||
min,
|
||||
preserves
|
||||
|
||||
proc toMin(i: In; pr: sink Value): MinValue =
|
||||
const
|
||||
recordType* = "preserves-record"
|
||||
setType* = "preserves-set"
|
||||
|
||||
proc toMin*(i: In; pr: sink Value): MinValue =
|
||||
case pr.kind
|
||||
of pkBoolean:
|
||||
result = pr.bool.newVal
|
||||
|
@ -27,12 +31,12 @@ proc toMin(i: In; pr: sink Value): MinValue =
|
|||
|
||||
of pkRecord:
|
||||
result = newDict(i.scope)
|
||||
result.objType = "preserves-record"
|
||||
result.objType = recordType
|
||||
var fields = newSeq[MinValue](pr.record.len.pred)
|
||||
for j, _ in fields:
|
||||
fields[j] = i.toMin pr.record[j].move
|
||||
i.dset(result, "fields", fields.newVal)
|
||||
i.dset(result, "label", i.toMin pr.record[pr.record.high].move)
|
||||
result = i.dset(result, "fields", fields.newVal)
|
||||
result = i.dset(result, "label", i.toMin pr.record[pr.record.high].move)
|
||||
of pkSequence:
|
||||
var entries = newSeq[MinValue](pr.sequence.len)
|
||||
for j, _ in pr.sequence:
|
||||
|
@ -40,14 +44,13 @@ proc toMin(i: In; pr: sink Value): MinValue =
|
|||
result = entries.newVal
|
||||
of pkSet:
|
||||
result = newDict(i.scope)
|
||||
result.objType = "preserves-set"
|
||||
result.objType = setType
|
||||
var set = newSeq[MinValue](pr.set.len)
|
||||
for j, _ in pr.set:
|
||||
set[j] = i.toMin pr.set[j].move
|
||||
i.dset(result, "set", set.newVal)
|
||||
of pkDictionary:
|
||||
result = newDict(i.scope)
|
||||
result.objType = "preserves-dictionary"
|
||||
for (k, v) in pr.dict.mitems:
|
||||
i.dset(result, i.toMin k.move, i.toMin v.move)
|
||||
|
||||
|
@ -56,7 +59,7 @@ proc toMin(i: In; pr: sink Value): MinValue =
|
|||
result.objType = "preserves-embedded"
|
||||
result.obj = pr.embeddedRef[].addr
|
||||
|
||||
proc toPreserves(i: In; val: MinValue): Value =
|
||||
proc toPreserves*(i: In; val: MinValue): Value =
|
||||
case val.kind
|
||||
of minInt:
|
||||
result = val.intVal.toPreserves
|
||||
|
@ -70,7 +73,7 @@ proc toPreserves(i: In; val: MinValue): Value =
|
|||
discard
|
||||
of minDictionary:
|
||||
case val.objType
|
||||
of "preserves-record":
|
||||
of recordType:
|
||||
let fields = i.dget(val, "fields")
|
||||
result = initRecord(
|
||||
i.toPreserves i.dget(val, "label"),
|
||||
|
@ -78,7 +81,7 @@ proc toPreserves(i: In; val: MinValue): Value =
|
|||
)
|
||||
for j, f in fields.qVal:
|
||||
result.record[j] = i.toPreserves f
|
||||
of "preserves-set":
|
||||
of setType:
|
||||
result = initSet()
|
||||
for e in i.dget(val, "set").qVal:
|
||||
result.incl i.toPreserves e
|
||||
|
@ -98,6 +101,21 @@ proc toPreserves(i: In; val: MinValue): Value =
|
|||
proc preserves_module*(i: In) =
|
||||
let def = i.define()
|
||||
|
||||
def.symbol("to-record") do (i: In):
|
||||
let vals = i.expect("a", "quot")
|
||||
var dict = i.scope.newDict
|
||||
dict.objType = recordType
|
||||
dict = i.dset(dict, "label", vals[0])
|
||||
dict = i.dset(dict, "fields", vals[1])
|
||||
i.push dict
|
||||
|
||||
def.symbol("to-set") do (i: In):
|
||||
let vals = i.expect("quot")
|
||||
var dict = i.scope.newDict
|
||||
dict.objType = setType
|
||||
dict = i.dset(dict, "set", vals[0])
|
||||
i.push dict
|
||||
|
||||
def.symbol("from-preserves") do (i: In):
|
||||
let vals = i.expect("str")
|
||||
let s = vals[0].getString()
|
||||
|
|
Loading…
Reference in New Issue