language: implement list indexing

This commit is contained in:
2023-09-17 08:38:11 -07:00
parent 3b101bd48a
commit a08526c92c
14 changed files with 90 additions and 0 deletions

View File

@ -366,6 +366,21 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
topLevelUsedError("ImportDeclaration", "CompilationUnitContext")
}
override fun visitIndexedBy(node: IndexedBy): Any {
val value = node.expression.visit(this)
val index = node.index.visit(this)
if (value is List<*> && index is Number) {
return value[index.toInt()] ?: None
}
if (value is Array<*> && index is Number) {
return value[index.toInt()] ?: None
}
throw RuntimeException("Failed to index '${value}' by '${index}': Unsupported types used.")
}
override fun visitCompilationUnit(node: CompilationUnit): Any {
topLevelUsedError("CompilationUnit", "CompilationUnitContext")
}