Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions src/AST.kt

This file was deleted.

32 changes: 15 additions & 17 deletions src/Environment.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
import ast.ASTNode

data class ValueAndConstStatus(var value: ASTNode, val constant: Boolean)

class Environment(val parent: Environment?) {
data class ValueAndConstStatus(var value: Value, val constant: Boolean)
val values = HashMap<String, ValueAndConstStatus>()

fun assignUp(name: String, value: Value) : Boolean {
fun assignUp(name: String, value: ASTNode): Boolean {
if (values.contains(name)) {
if (values[name]!!.constant) {
throw Exception("Interpreter error: Cannot reassign constant $name")
}
values[name]?.value = value
return true
}
else if (parent != null) {
} else if (parent != null) {
return parent.assignUp(name, value)
}
return false
}

fun assign(name: String, value: Value, constant: Boolean) {
if (values.contains(name)) {
if (values[name]!!.constant) {
throw Exception("Interpreter error: Cannot reassign constant $name")
}
fun assign(name: String, value: ASTNode, constant: Boolean) {
if (values.contains(name) && values[name]!!.constant) {
throw Exception("Interpreter error: Cannot reassign constant $name")
}
if (assignUp(name, value)) {
return
}

if(name in values) {
if (!values.contains(name)) {
values[name] = ValueAndConstStatus(value, constant)
}
else {
} else {
throw Exception("Interpreter error: Cannot assign variable $name which has not been defined. If this is supposed to be new, prefix it with 'let' or 'const'")
}
}


fun let(name: String, value: Value, constant: Boolean) {
fun let(name: String, value: ASTNode, constant: Boolean) {
if (values.contains(name)) {
if (values[name]!!.constant) {
throw Exception("Interpreter error: Cannot reassign constant $name")
Expand All @@ -44,11 +43,10 @@ class Environment(val parent: Environment?) {
values[name] = ValueAndConstStatus(value, constant)
}

fun get(name: String) : Value? {
fun get(name: String): ASTNode? {
if (values.contains(name)) {
return values[name]?.value
}
else if(parent != null){
return values[name]!!.value
} else if (parent != null) {
return parent.get(name)
}
return null
Expand Down
Loading