aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.nim
blob: 56313b584ad4b5b90104fb8c465d400cf89ffac2 (plain)
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
import field
import pointer

type
  Interpreter = object
    f: ref Field
    p: ref Pointer
    argv: seq[string]

func NewInterpreter*(f: ref Field, p: ref Pointer): ref Interpreter =
  new(result)
  result.f = f
  result.p = p

proc Step*(i: var Interpreter): ref int =
  var prev: ref Pointer
  var p = i.p
  while p != nil:
    let (d, v) = p[].Exec(i.f[])
    if v != nil:
      return v
    if d:
      if prev == nil:
        i.p = p.next
      else:
        prev.next = p.next
    p = p.next

proc Run*(i: var Interpreter): int =
  while i.p != nil:
    let v = i.Step()
    if v != nil:
      return v[]