all repos — min @ 6924adcb0ecfb0e37832b24ec05e3effb91b365a

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
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
import 
  streams, 
  strutils, 
  critbits, 
  os
import 
  value,
  parser

type
  MinRuntimeError* = ref object of SystemError
    qVal*: seq[MinValue]

proc raiseRuntime*(msg: string, qVal: var seq[MinValue]) =
  raise MinRuntimeError(msg: msg, qVal: qVal)

proc fullname*(scope: ref MinScope): string =
  result = scope.name
  if not scope.parent.isNil:
    result = scope.parent.fullname & ":" & result

proc getSymbol*(scope: ref MinScope, key: string): MinOperator =
  if scope.symbols.hasKey(key):
    return scope.symbols[key]
  elif not scope.parent.isNil:
    return scope.parent.getSymbol(key)
  else:
    raiseUndefined("Symbol '$1' not found." % key)

proc hasSymbol*(scope: ref MinScope, key: string): bool =
  if scope.symbols.hasKey(key):
    return true
  elif not scope.parent.isNil:
    return scope.parent.hasSymbol(key)
  else:
    return false

proc delSymbol*(scope: ref MinScope, key: string): bool {.discardable.}=
  if scope.symbols.hasKey(key):
    if scope.symbols[key].sealed:
      raiseInvalid("Symbol '$1' is sealed." % key) 
    scope.symbols.excl(key)
    return true
  return false

proc setSymbol*(scope: ref MinScope, key: string, value: MinOperator): bool {.discardable.}=
  result = false
  # check if a symbol already exists in current scope
  if not scope.isNil and scope.symbols.hasKey(key):
    if scope.symbols[key].sealed:
      raiseInvalid("Symbol '$1' is sealed." % key) 
    scope.symbols[key] = value
    result = true
  else:
    # Go up the scope chain and attempt to find the symbol
    if not scope.parent.isNil:
      result = scope.parent.setSymbol(key, value)

proc getSigil*(scope: ref MinScope, key: string): MinOperator =
  if scope.sigils.hasKey(key):
    return scope.sigils[key]
  elif not scope.parent.isNil:
    return scope.parent.getSigil(key)
  else:
    raiseUndefined("Sigil '$1' not found." % key)

proc hasSigil*(scope: ref MinScope, key: string): bool =
  if scope.sigils.hasKey(key):
    return true
  elif not scope.parent.isNil:
    return scope.parent.hasSigil(key)
  else:
    return false

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

proc debug*(i: In, value: MinValue) =
  if i.debugging: 
    echo $value
    stderr.writeLine("-- " & i.dump & $value)

proc debug*(i: In, value: string) =
  if i.debugging: 
    stderr.writeLine("-- " & value)

proc newScope*(i: In, id: string, q: var MinValue) =
  q.scope = new MinScope
  q.scope.name = id
  q.scope.parent = i.scope

template createScope*(i: In, id: string, q: MinValue, body: untyped): untyped =
  q.scope = new MinScope
  q.scope.name = id
  q.scope.parent = i.scope
  let scope = i.scope
  i.scope = q.scope
  body
  i.scope = scope

template withScope*(i: In, q: MinValue, body: untyped): untyped =
  #i.debug "[scope] " & q.scope.fullname
  let origScope = i.scope
  i.scope = q.scope
  body
  #i.debug "[scope] " & scope.fullname
  i.scope = origScope

template addScope*(i: In, id: string, q: MinValue, body: untyped): untyped =
  var added = new MinScope
  added.name = id
  if q.scope.isNil:
    q.scope = i.scope
  added.parent = q.scope
  let scope = i.scope
  i.scope = added
  body
  i.scope = scope

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

proc newMinInterpreter*(debugging = false): MinInterpreter =
  var st:MinStack = newSeq[MinValue](0)
  var pr:MinParser
  var scope = new MinScope
  scope.name = "ROOT"
  var i:MinInterpreter = MinInterpreter(
    filename: "input", 
    pwd: "",
    parser: pr, 
    stack: st,
    scope: scope,
    debugging: debugging, 
    unsafe: false,
    halt: false,
    currSym: MinValue(column: 1, line: 1, kind: minSymbol, symVal: "")
  )
  return i

proc copy*(i: MinInterpreter, filename: string): MinInterpreter =
  result = newMinInterpreter(debugging = i.debugging)
  result.filename = filename
  result.pwd =  filename.parentDir
  result.stack = i.copystack
  result.scope = i.scope
  result.currSym = MinValue(column: 1, line: 1, kind: minSymbol, symVal: "")

proc error(i: MinInterpreter, message: string) =
  if i.currSym.filename == "":
    stderr.writeLine("`$1`: Error - $2" % [i.currSym.symVal, message])
  else:
    stderr.writeLine("$1 [$2,$3] `$4`: Error - $5" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, i.currSym.symVal, message])

template execute(i: In, body: untyped) =
  let stack = i.copystack
  try:
    body
  except MinRuntimeError:
    i.stack = stack
    stderr.writeLine("$1 [$2,$3]: $4" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, getCurrentExceptionMsg()])
    i.halt = true
  except:
    i.stack = stack
    i.error(getCurrentExceptionMsg())
    i.halt = true

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

proc close*(i: In) = 
  i.parser.close();

proc push*(i: In, val: MinValue) {.gcsafe.}

proc apply*(i: In, op: MinOperator, name="apply") =
  case op.kind
  of minProcOp:
    op.prc(i)
  of minValOp:
    if op.val.kind == minQuotation:
      var q = op.val
      i.addScope(name, q):
        #echo "a1: ", i.scope.fullname
        for e in q.qVal:
          i.push e
    else:
      i.push(op.val)

proc push*(i: In, val: MinValue) = 
  if i.halt:
    return
  i.debug val
  if val.kind == minSymbol:
    if not i.evaluating:
      #val.parent = i.currSym
      i.currSym = val
    let symbol = val.symVal
    let sigil = "" & symbol[0]
    let found = i.scope.hasSymbol(symbol)
    if found:
      let sym = i.scope.getSymbol(symbol) 
      if i.unsafe:
        let stack = i.copystack
        try:
          i.apply(sym) 
        except:
          i.stack = stack
          raise
      else:
        i.execute:
          i.apply(sym)
    else:
      let found = i.scope.hasSigil(sigil)
      if symbol.len > 1 and found:
        let sig = i.scope.getSigil(sigil) 
        let sym = symbol[1..symbol.len-1]
        i.stack.add(MinValue(kind: minString, strVal: sym))
        if i.unsafe:
          let stack = i.copystack
          try:
            i.apply(sig)
          except:
            i.stack = stack
            raise
        else:
          i.execute:
            i.apply(sig)
      else:
        raiseUndefined("Undefined symbol '$1' in scope '$2'" % [val.symVal, i.scope.fullname])
  else:
    i.stack.add(val)

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

proc pop*(i: In): MinValue =
  if i.stack.len > 0:
    return i.stack.pop
  else:
    raiseEmptyStack()

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

proc interpret*(i: In) {.gcsafe.}= 
  var val: MinValue
  while i.parser.token != tkEof and not i.halt: 
    i.execute:
      val = i.parser.parseMinValue
      i.push val

proc unquote*(i: In, name: string, q: var MinValue) =
  i.createScope(name, q): 
    for v in q.qVal:
      i.push v

proc eval*(i: In, s: string, name="<eval>") =
  let fn = i.filename
  try:
    var i2 = i.copy(name)
    i2.open(newStringStream(s), name)
    discard i2.parser.getToken() 
    i2.interpret()
    i.stack = i2.stack
    i.scope = i2.scope
  except:
    stderr.writeLine getCurrentExceptionMsg()
  finally:
    i.filename = fn

proc load*(i: In, s: string) =
  let fn = i.filename
  try:
    var i2 = i.copy(s)
    i2.open(newStringStream(s.readFile), s)
    discard i2.parser.getToken() 
    i2.interpret()
    i.stack = i2.stack
    i.scope = i2.scope
  except:
    stderr.writeLine getCurrentExceptionMsg()
  finally:
    i.filename = fn