-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVmTemplate.kt
More file actions
223 lines (196 loc) · 5.21 KB
/
VmTemplate.kt
File metadata and controls
223 lines (196 loc) · 5.21 KB
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
package net.globulus.simi
import java.nio.ByteBuffer
import kotlin.math.round
object Nil
private const val INITIAL_STACK_SIZE = 1024
private const val STACK_GROWTH_FACTOR = 4
enum class OpCode {
CONST_INT,
CONST_FLOAT,
CONST_ID,
CONST_STR,
NIL,
POP,
SET_LOCAL,
GET_LOCAL,
LT,
LE,
GT,
GE,
EQ,
NE,
NEGATE,
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE,
DIVIDE_INT,
MOD,
PRINT,
JUMP,
JUMP_IF_FALSE,
HALT,
;
val byte = ordinal.toByte()
companion object {
fun from(byte: Byte) = values()[byte.toInt()]
}
}
private val buffer = ByteBuffer.wrap(BYTE_CODE)
private val strings = arrayOf(STRINGS)
private var sp = 0
private var stackSize = INITIAL_STACK_SIZE
private var stack = arrayOfNulls<Any>(INITIAL_STACK_SIZE)
fun main(args: Array<String>) {
interpret()
}
fun interpret() {
loop@ while (true) {
val code = nextCode
when (code) {
OpCode.CONST_INT -> push(nextLong)
OpCode.CONST_FLOAT -> push(nextDouble)
OpCode.CONST_ID -> throw RuntimeException("WTF")
OpCode.CONST_STR -> push(strings[nextInt])
OpCode.NIL -> push(Nil)
OpCode.POP -> sp--
OpCode.SET_LOCAL -> stack[nextInt] = pop()
OpCode.GET_LOCAL -> push(stack[nextInt]!!)
OpCode.NEGATE -> negate()
OpCode.ADD -> add()
OpCode.SUBTRACT, OpCode.MULTIPLY, OpCode.DIVIDE, OpCode.DIVIDE_INT, OpCode.MOD, OpCode.LE, OpCode.LT, OpCode.GE, OpCode.GT -> binaryOpOnStack(code)
OpCode.EQ, OpCode.NE -> checkEquality(code)
OpCode.PRINT -> println(pop())
OpCode.JUMP -> buffer.position(nextInt)
OpCode.JUMP_IF_FALSE -> {
val offset = nextInt
if (isFalsey(peek())) {
buffer.position(offset)
}
}
OpCode.HALT -> break@loop
}
}
printStack()
}
private fun isFalsey(o: Any): Boolean {
return when (o) {
Nil -> true
is Long -> o == 0L
is Double -> o == 0.0
else -> false
}
}
private fun negate() {
val a = pop()
if (a is Long) {
push(-a)
} else if (a is Double) {
push(-a)
}
}
private fun add() {
val b = pop()
val a = pop()
push(when (a) {
is String -> a + b
else -> binaryOp(OpCode.ADD, a, b)
})
}
private fun binaryOpOnStack(opCode: OpCode) {
val b = pop()
val a = pop()
push(binaryOp(opCode, a, b))
}
private fun binaryOp(opCode: OpCode, a: Any, b: Any): Any {
if (a == Nil || b == Nil) {
return Nil
}
return if (a is Long && b is Long) {
binaryOpTwoLongs(opCode, a, b)
} else {
val d1 = if (a is Double) a else (a as Long).toDouble()
val d2 = if (b is Double) b else (b as Long).toDouble()
binaryOpTwoDoubles(opCode, d1, d2)
}
}
private fun binaryOpTwoLongs(opCode: OpCode, a: Long, b: Long): Any {
return when (opCode) {
OpCode.ADD -> a + b
OpCode.SUBTRACT -> a - b
OpCode.MULTIPLY -> a * b
OpCode.DIVIDE -> intIfPossible(a * 1.0 / b)
OpCode.DIVIDE_INT -> a / b
OpCode.MOD -> a % b
OpCode.LT -> boolToLong(a < b)
OpCode.LE -> boolToLong(a <= b)
OpCode.GE -> boolToLong(a >= b)
OpCode.GT -> boolToLong(a > b)
else -> throw IllegalArgumentException("WTF")
}
}
private fun binaryOpTwoDoubles(opCode: OpCode, a: Double, b: Double): Any {
return when (opCode) {
OpCode.ADD -> intIfPossible(a + b)
OpCode.SUBTRACT -> intIfPossible(a - b)
OpCode.MULTIPLY -> intIfPossible(a * b)
OpCode.DIVIDE -> intIfPossible(a / b)
OpCode.MOD ->intIfPossible(a % b)
OpCode.LT -> boolToLong(a < b)
OpCode.LE -> boolToLong(a <= b)
OpCode.GE -> boolToLong(a >= b)
OpCode.GT -> boolToLong(a > b)
else -> throw IllegalArgumentException("WTF")
}
}
private fun intIfPossible(d: Double): Any {
val rounded = round(d)
return if (rounded == d) {
rounded.toLong()
} else {
d
}
}
private fun boolToLong(b: Boolean) = if (b) 1L else 0L
private fun checkEquality(code: OpCode) {
val b = pop()
val a = pop()
val r = areEqual(a, b)
push(boolToLong(if (code == OpCode.EQ) r else !r))
}
private fun areEqual(a: Any, b: Any): Boolean {
// TODO add comparison by equals()
return a == b
}
private fun resizeStackIfNecessary() {
if (sp == stackSize) {
stackSize *= STACK_GROWTH_FACTOR
stack = stack.copyOf(stackSize)
}
}
private fun push(o: Any) {
resizeStackIfNecessary()
stack[sp] = o
sp++
}
private fun pop(): Any {
sp--
return stack[sp]!!
}
private fun peek(): Any {
return stack[sp - 1]!!
}
private fun gc() {
for (i in sp until stackSize) {
stack[i] = null
}
System.gc()
}
private fun printStack() {
println(stack.copyOfRange(0, sp).joinToString(" "))
}
private val nextCode: OpCode get() = OpCode.from(nextByte)
private val nextByte: Byte get() = buffer.get()
private val nextInt: Int get() = buffer.int
private val nextLong: Long get() = buffer.long
private val nextDouble: Double get() = buffer.double