Skip to content

Instantly share code, notes, and snippets.

@kayabaNerve
Last active August 24, 2018 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayabaNerve/50725f50249c3ac387d4b4086dbb113e to your computer and use it in GitHub Desktop.
Save kayabaNerve/50725f50249c3ac387d4b4086dbb113e to your computer and use it in GitHub Desktop.
import tables
#Valid object times.
type NaturalObjectKind* = enum
nokBoolean, nokInt, nokString, nokArray, nokObject
#Natural Object.
type NaturalObject* = ref object of RootObj
case kind*: NaturalObjectKind
of nokBoolean:
boolean: bool
of nokInt:
num: int
of nokString:
str: string
of nokArray:
arr: seq[NaturalObject]
of nokObject:
represents: int
properties: Table[string, NaturalObject]
#Create a NO non-object.
proc newNO(kind: NaturalObjectKind): NaturalObject =
NaturalObject(
kind: kind
)
#Create a NaturalObject object.
proc newNO*(): NaturalObject =
NaturalObject(
kind: nokObject,
properties: initTable[string, NaturalObject]()
)
#Set what a NaturalObject represents.
proc setRepresents*(NO: NaturalObject, represents: int) =
NO.represents = represents
#Add various values to an array of NaturalObjects.
proc add*(NO: var seq[NaturalObject], value: bool) =
var newNO: NaturalObject = newNO(nokBoolean)
newNO.boolean = value
NO.add(newNO)
proc add*(NO: var seq[NaturalObject], value: int) =
var newNO: NaturalObject = newNO(nokInt)
newNO.num = value
NO.add(newNO)
proc add*(NO: var seq[NaturalObject], value: string) =
var newNO: NaturalObject = newNO(nokString)
newNO.str = value
NO.add(newNO)
proc add*(NO: var seq[NaturalObject], value: seq[NaturalObject]) =
var newNO: NaturalObject = newNO(nokArray)
newNO.arr = value
NO.add(newNO)
proc add*(NO: var seq[NaturalObject], value: NaturalObject) =
NO.add(value)
#Set the value behind a NaturalObject (not public).
proc setValue(NO: var NaturalObject, value: bool) =
if NO.kind != nokBoolean:
NO = newNO(nokBoolean)
NO.boolean = value
proc setValue(NO: var NaturalObject, value: int) =
if NO.kind != nokInt:
NO = newNO(nokInt)
NO.num = value
proc setValue(NO: var NaturalObject, value: string) =
if NO.kind != nokString:
NO = newNO(nokString)
NO.str = value
proc setValue(NO: var NaturalObject, value: seq[NaturalObject]) =
if NO.kind != nokArray:
NO = newNO(nokArray)
NO.arr = value
#Set the property of a Natural Object.
proc setProperty*(NO: NaturalObject, field: string, value: NaturalObject) =
NO.properties[field] = value
proc setProperty*(NO: NaturalObject, field: string, value: bool) =
var newNO: NaturalObject = newNO(nokBoolean)
newNO.setValue(value)
NO.setProperty(field, newNO)
proc setProperty*(NO: NaturalObject, field: string, value: int) =
var newNO: NaturalObject = newNO(nokInt)
newNO.setValue(value)
NO.setProperty(field, newNO)
proc setProperty*(NO: NaturalObject, field: string, value: string) =
var newNO: NaturalObject = newNO(nokString)
newNO.setValue(value)
NO.setProperty(field, newNO)
proc setProperty*(NO: NaturalObject, field: string, value: seq[NaturalObject]) =
var newNO: NaturalObject = newNO(nokArray)
newNO.setValue(value)
NO.setProperty(field, newNO)
#Get the values from various Natural Objects (that aren't objects).
proc getBool*(NO: NaturalObject): bool =
NO.boolean
proc getNum*(NO: NaturalObject): int =
NO.num
proc getString*(NO: NaturalObject): string =
NO.str
proc getArray*(NO: NaturalObject): seq[NaturalObject] =
NO.arr
#Get what a Natural Object represents/its properties.
proc getRepresents(NO: NaturalObject): int =
NO.represents
proc getProperty(NO: NaturalObject, field: string): NaturalObject =
NO.properties[field]
#Testing code.
var
noTrue: NaturalObject = newNO(nokBoolean)
noFalse: NaturalObject = newNO(nokBoolean)
noNum: NaturalObject = newNO(nokInt)
noArr: seq[NaturalObject] = @[]
noObject: NaturalObject = newNO()
noTrue.setValue(true)
noFalse.setValue(false)
noNum.setValue(5)
noArr.add(true)
noArr.add(false)
noArr.add(5)
noObject.setProperty("a", noTrue)
noObject.setProperty("b", noFalse)
noObject.setProperty("c", noNum)
noObject.setProperty("d", noArr)
var
propA: NaturalObject = noObject.getProperty("a")
propB: NaturalObject = noObject.getProperty("b")
propC: NaturalObject = noObject.getProperty("c")
propD: NaturalObject = noObject.getProperty("d")
echo propA.getBool()
echo propB.getBool()
echo propC.getNum()
for i in 0 ..< propD.getArray().len:
var elem = propD.getArray()[i]
case elem.kind:
of nokBoolean:
echo elem.getBool()
of nokInt:
echo elem.getNum()
else:
echo "Not supported."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment