all repos — min @ 323618c811ea88c8d4456cabbce6bfd8c9d728c6

A small but practical concatenative programming language.

core/interpreter.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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
import streams, strutils, tables
import parser, ../vendor/linenoise

type 
  MinInterpreter* = object
    stack*: MinStack
    parser*: MinParser
    currSym: MinValue
    filename*: string
    debugging*: bool 
    evaluating*: bool 
  MinOperator* = proc (i: var MinInterpreter)
  MinSigil* = proc (i: var MinInterpreter, sym: string)
  MinError* = enum
    errSystem,
    errParser,
    errGeneric,
    errEmptyStack,
    errNoQuotation,
    errUndefined,
    errIncorrect,
    errTwoNumbersRequired,
    errDivisionByZero


const ERRORS: array [MinError, string] = [
  "A system error occurred",
  "A parsing error occurred",
  "A generic error occurred",
  "Insufficient items on the stack", 
  "Quotation not found on the stack",
  "Symbol undefined",
  "Incorrect items on the stack",
  "Two numbers are required on the stack",
  "Division by zero"
]

var SYMBOLS* = initTable[string, MinOperator]()
var SIGILS* = initTable[string, MinOperator]()

proc newMinInterpreter*(debugging = false): MinInterpreter =
  var s:MinStack = newSeq[MinValue](0)
  var p:MinParser
  var i:MinInterpreter = MinInterpreter(filename: "input", parser: p, stack: s, debugging: debugging, currSym: MinValue(column: 1, line: 1, kind: minSymbol, symVal: ""))
  return i

proc error*(i: MinInterpreter, status: MinError, message = "") =
  var msg = if message == "": ERRORS[status] else: message
  if i.filename == "":
    stderr.writeln("`$1`: Error - $2" %[i.currSym.symVal, msg])
  else:
    stderr.writeln("$1 [$2,$3] `$4`: Error - $5" %[i.filename, $i.currSym.line, $i.currSym.column, i.currSym.symVal, msg])
    quit(int(status))

proc open*(i: var MinInterpreter, stream:Stream, filename: string) =
  i.filename = filename
  i.parser.open(stream, filename)

proc close*(i: var MinInterpreter) = 
  i.parser.close();

proc dump*(i: MinInterpreter): string =
  var s = ""
  for item in i.stack:
    s = s & $item & " "
  return s

proc debug(i: var MinInterpreter, value: MinValue) =
  if i.debugging: 
    stderr.writeln("-- " &i.dump & $value)

proc push*(i: var MinInterpreter, val: MinValue) = 
  i.debug val
  if val.kind == minSymbol:
    if not i.evaluating:
      i.currSym = val
    let symbol = val.symVal
    let sigil = "" & symbol[0]
    if SYMBOLS.hasKey(val.symVal):
      try:
        SYMBOLS[val.symVal](i) 
      except:
        i.error(errSystem, getCurrentExceptionMsg())
    else:
      if SIGILS.hasKey(sigil) and symbol.len > 1:
        let sym = symbol[1..symbol.len-1]
        try:
          i.stack.add(MinValue(kind: minString, strVal: sym))
          SIGILS[sigil](i) 
        except:
          i.error(errSystem, getCurrentExceptionMsg())
      else:
        i.error(errUndefined, "Undefined symbol: '"&val.symVal&"'")
  else:
    i.stack.add(val)

proc push*(i: var MinInterpreter, q: seq[MinValue]) =
  for e in q:
    i.push e

proc pop*(i: var MinInterpreter): MinValue =
  if i.stack.len > 0:
    return i.stack.pop
  else:
    i.error(errEmptyStack)

proc peek*(i: MinInterpreter): MinValue = 
  if i.stack.len > 0:
    return i.stack[i.stack.len-1]
  else:
    i.error(errEmptyStack)

proc interpret*(i: var MinInterpreter) = 
  var val: MinValue
  while i.parser.token != tkEof: 
    try:
      val = i.parser.parseMinValue
    except:
      i.error errParser, getCurrentExceptionMsg()
    i.push val

proc eval*(i: var MinInterpreter, s: string) =
  let fn = i.filename
  try:
    i.open(newStringStream(s), "eval")
    discard i.parser.getToken() 
    i.interpret()
  except:
    stderr.writeln getCurrentExceptionMsg()
  finally:
    i.filename = fn

proc load*(i: var MinInterpreter, s: string) =
  let fn = i.filename
  try:
    i.open(newStringStream(s.readFile), s)
    discard i.parser.getToken() 
    i.interpret()
  except:
    stderr.writeln getCurrentExceptionMsg()
  finally:
    i.filename = fn

proc apply*(i: var MinInterpreter, symbol: string) =
  SYMBOLS[symbol](i)

proc copystack*(i: var MinInterpreter): MinStack =
  var s = newSeq[MinValue](0)
  for i in i.stack:
    s.add i
  return s