all repos — min @ 2660bf2b92938be12251ff420bb825c93c55a2f2

A small but practical concatenative programming language.

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
import
  critbits
import
  parser,
  scope

proc typeName*(v: MinValue): string {.extern:"min_exported_symbol_$1".}=
  case v.kind:
    of minInt:
      return "int"
    of minFloat:
      return "float"
    of minDictionary: 
      if v.isTypedDictionary:
        return "dict:" & v.objType
      else: 
        return "dict"
    of minQuotation:
      return "quot"
    of minString:
      return "string"
    of minSymbol:
      return "sym"
    of minBool:
      return "bool"

# Constructors

proc newVal*(s: string): MinValue {.extern:"min_exported_symbol_$1".}=
  return MinValue(kind: minString, strVal: s)

proc newVal*(s: cstring): MinValue {.extern:"min_exported_symbol_$1_2".}=
  return MinValue(kind: minString, strVal: $s)

proc newVal*(q: seq[MinValue]): MinValue {.extern:"min_exported_symbol_$1_3".}=
  return MinValue(kind: minQuotation, qVal: q)

proc newVal*(i: BiggestInt): MinValue {.extern:"min_exported_symbol_$1_4".}=
  return MinValue(kind: minInt, intVal: i)

proc newVal*(f: BiggestFloat): MinValue {.extern:"min_exported_symbol_$1_5".}=
  return MinValue(kind: minFloat, floatVal: f)

proc newVal*(s: bool): MinValue {.extern:"min_exported_symbol_$1_6".}=
  return MinValue(kind: minBool, boolVal: s)

proc newDict*(parentScope: ref MinScope): MinValue {.extern:"min_exported_symbol_$1".}=
  return MinValue(kind: minDictionary, scope: newScopeRef(parentScope))

proc newSym*(s: string): MinValue {.extern:"min_exported_symbol_$1".}=
  return MinValue(kind: minSymbol, symVal: s)

# Get string value from string or quoted symbol

proc getFloat*(v: MinValue): float {.extern:"min_exported_symbol_$1".}=
  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 {.extern:"min_exported_symbol_$1".}=
  if v.isSymbol:
    return v.symVal
  elif v.isString:
    return v.strVal
  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")