all repos — min @ a0894a184e135b97649ee65977582137a9f20008

A small but practical concatenative programming language.

core/utils.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
import tables, strutils
import parser, interpreter

template minsym*(name: string, body: stmt): stmt {.immediate.} =
  SYMBOLS[name] = proc (i: var TMinInterpreter) =
    body

template minsigil*(name: char, body: stmt): stmt {.immediate.} =
  SIGILS[name] = proc (i: var TMinInterpreter) =
    body

proc isSymbol*(s: TMinValue): bool =
  return s.kind == minSymbol

proc isQuotation*(s: TMinValue): bool = 
  return s.kind == minQuotation

proc isString*(s: TMinValue): bool = 
  return s.kind == minString

proc isFloat*(s: TMinValue): bool =
  return s.kind == minFloat

proc isInt*(s: TMinValue): bool =
  return s.kind == minInt

proc isNumber*(s: TMinValue): bool =
  return s.kind == minInt or s.kind == minFloat

proc isBool*(s: TMinValue): bool =
  return s.kind == minBool

proc newVal*(s: string): TMinValue =
  return TMinValue(kind: minString, strVal: s)

proc newVal*(q: seq[TMinValue]): TMinValue =
  return TMinValue(kind: minQuotation, qVal: q)

proc newVal*(s: int): TMinValue =
  return TMinValue(kind: minInt, intVal: s)

proc newVal*(s: float): TMinValue =
  return TMinValue(kind: minFloat, floatVal: s)

proc newVal*(s: bool): TMinValue =
  return TMinValue(kind: minBool, boolVal: s)

proc warn*(s: string) =
  stderr.writeln s

proc linrec*(i: var TMinInterpreter, p, t, r1, r2: TMinValue) =
  i.push p.qVal
  var check = i.pop
  if check.isBool and check.boolVal == true:
    i.push t.qVal
  else:
    i.push r1.qVal
    i.linrec(p, t, r1, r2)
    i.push r2.qVal