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