-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.nim
71 lines (63 loc) · 1.82 KB
/
runner.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
import
lexer,
std/deques,
std/strutils,
std/sugar
type
Op = enum
Dump,
Plus,
Push,
Instruction = tuple
op: Op
value: NorthType
token: Token
Word = seq[Instruction]
NorthKind = enum
nkInt,
nkString,
nkWord,
nkBuiltin,
NorthType = object
case kind: Northkind
of nkInt: intVal:int
of nkWord: wordVal: Word
of nkBuiltin: builtInVal: string
of nkString: stringVal: string
proc toInstructions(program: Program): Deque[Instruction] =
result = initDeque[Instruction]()
for token in program:
case token.kind
of TokenKind.Word:
case token.token
of "+":
result.addLast((Op.Plus, NorthType(kind: nkBuiltin, builtInVal: token.token), token))
of ".":
result.addLast((Op.Dump, NorthType(kind: nkBuiltin, builtInVal: token.token), token))
of TokenKind.Int:
result.addLast((Op.Push, NorthType(kind: nkInt, intVal: parseInt(token.token)), token))
of TokenKind.String:
result.addLast((Op.Push, NorthType(kind: nkString, stringVal: token.token), token))
else:
doassert false, "Unknown Token kind " & $token.kind
proc run*(progIn: Program) =
var stack = initDeque[NorthType]()
var program = toInstructions(progIn)
var isCompiling = false
for inst in program:
case inst.op
of Push:
stack.addFirst(inst.value)
of Plus:
let a = stack.popFirst()
doassert a.kind == nkInt, $inst.token & " Use plus on a non-number"
let b = stack.popFirst()
assert b.kind == nkInt, $inst.token & " Use of plus on a non-number"
stack.addFirst(NorthType(kind: nkint, intVal: a.intVal+b.intVal))
of Dump:
let val = stack.popFirst()
case val.kind
of nkInt: echo val.intVal
of nkString: echo val.stringVal
else:
echo "can't dump the type " & $val.kind