minpkg/core/value.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 |
import
parser,
std/hashes
proc typeName*(v: MinValue): string =
case v.kind:
of minInt:
return "int"
of minFloat:
return "flt"
of minCommand:
return "cmd"
of minDictionary:
if v.isTypedDictionary:
return "dict:" & v.objType
else:
return "dict"
of minQuotation:
return "quot"
of minString:
return "str"
of minSymbol:
return "sym"
of minNull:
return "null"
of minBool:
return "bool"
# Constructors
proc newNull*(): MinValue =
return MinValue(kind: minNull)
proc newVal*(s: string): MinValue =
return MinValue(kind: minString, strVal: s)
proc newVal*(s: cstring): MinValue =
return MinValue(kind: minString, strVal: $s)
proc newVal*(q: seq[MinValue]): MinValue =
return MinValue(kind: minQuotation, qVal: q)
proc newVal*(i: BiggestInt): MinValue =
return MinValue(kind: minInt, intVal: i)
proc newVal*(f: BiggestFloat): MinValue =
return MinValue(kind: minFloat, floatVal: f)
proc newVal*(s: bool): MinValue =
return MinValue(kind: minBool, boolVal: s)
proc newDict*(parentScope: ref MinScope): MinValue =
return MinValue(kind: minDictionary, scope: newScopeRef(parentScope))
proc newSym*(s: string): MinValue =
return MinValue(kind: minSymbol, symVal: s)
proc newCmd*(s: string): MinValue =
return MinValue(kind: minCommand, cmdVal: s)
proc hash*(v: MinValue): Hash =
return hash($v)
# Get string value from string or quoted symbol
proc getFloat*(v: MinValue): float =
if v.isInt:
return v.intVal.float
elif v.isFloat:
return v.floatVal
else:
raiseInvalid("Value is not a number")
proc getString*(v: MinValue): string =
if v.isSymbol:
return v.symVal
elif v.isString:
return v.strVal
elif v.isCommand:
return v.cmdVal
elif v.isQuotation:
if v.qVal.len != 1:
raiseInvalid("Quotation is not a quoted symbol")
let sym = v.qVal[0]
if sym.isSymbol:
return sym.symVal
else:
raiseInvalid("Quotation is not a quoted symbol")
|