Create simple stack model for interpreter
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.backend.common.interpreter.stack
|
||||||
|
|
||||||
|
interface Stack {
|
||||||
|
fun pushFrame(frame: Frame)
|
||||||
|
fun popFrame(): Frame
|
||||||
|
fun peekFrame(): Frame
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Frame {
|
||||||
|
fun addVar(state: State)
|
||||||
|
fun getVar(name: String): State
|
||||||
|
}
|
||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.backend.common.interpreter.stack
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException
|
||||||
|
|
||||||
|
class StackImpl : Stack {
|
||||||
|
private val stack: MutableList<Frame> = mutableListOf()
|
||||||
|
|
||||||
|
override fun pushFrame(frame: Frame) {
|
||||||
|
stack.add(frame)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun popFrame(): Frame {
|
||||||
|
if (stack.isEmpty()) {
|
||||||
|
throw IndexOutOfBoundsException("Stack is empty")
|
||||||
|
}
|
||||||
|
return stack.removeAt(stack.size - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun peekFrame(): Frame {
|
||||||
|
if (stack.isEmpty()) {
|
||||||
|
throw IndexOutOfBoundsException("Stack is empty")
|
||||||
|
}
|
||||||
|
return stack.get(stack.size - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class InterpreterFrame : Frame {
|
||||||
|
val pool: MutableList<State> = mutableListOf()
|
||||||
|
|
||||||
|
override fun addVar(state: State) {
|
||||||
|
pool.add(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getVar(name: String): State {
|
||||||
|
return pool.firstOrNull { it.getName() == name }
|
||||||
|
?: throw NoSuchElementException("Frame pool doesn't contains variable with name $name")
|
||||||
|
}
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.backend.common.interpreter.stack
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.ir.expressions.IrConst
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
fun getState(): List<State>
|
||||||
|
fun getName(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
class Primitive<T>(public val varName: String, public val value: IrConst<T>) : State {
|
||||||
|
override fun getState(): List<State> {
|
||||||
|
return listOf(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getName(): String {
|
||||||
|
return varName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Complex(public val objName: String, public val values: List<State>) : State {
|
||||||
|
override fun getState(): List<State> {
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getName(): String {
|
||||||
|
return objName
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user