language: implement for in

This commit is contained in:
2023-09-11 05:34:09 -04:00
parent 9ba150bb69
commit 0aab45094a
12 changed files with 105 additions and 0 deletions

View File

@ -8,6 +8,29 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
override fun visitIntegerLiteral(node: IntegerLiteral): Any = node.value
override fun visitDoubleLiteral(node: DoubleLiteral): Any = node.value
override fun visitForIn(node: ForIn): Any {
val blockFunction = node.block.visit(this) as BlockFunction
var result: Any? = null
val value = node.expression.visit(this)
if (value !is Iterable<*>) {
throw RuntimeException("Unable to iterate on value that is not a iterable.")
}
for (item in value) {
try {
scoped {
currentScope.define(node.symbol.id, item ?: None)
result = blockFunction.call()
}
} catch (_: BreakMarker) {
break
} catch (_: ContinueMarker) {
continue
}
}
return result ?: None
}
override fun visitStringLiteral(node: StringLiteral): Any = node.text
override fun visitBooleanLiteral(node: BooleanLiteral): Any = node.value