-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKompilerTemplate.kt
185 lines (160 loc) · 3.78 KB
/
KompilerTemplate.kt
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
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 var sp = 0
private var stackSize = INITIAL_STACK_SIZE
private var stack = arrayOfNulls<Any>(INITIAL_STACK_SIZE)
fun main(args: Array<String>) {
INTERPRET
}
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(" "))
}