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

@ -106,6 +106,15 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
While(condition, block)
}
private fun readForIn(): ForIn = within {
expect(TokenType.For)
val symbol = readSymbolRaw()
expect(TokenType.In)
val value = readExpression()
val block = readBlock()
ForIn(symbol, value, block)
}
private fun readNative(): Native = within {
expect(TokenType.Native)
val form = readSymbolRaw()
@ -160,6 +169,10 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
readWhile()
}
TokenType.For -> {
readForIn()
}
TokenType.Break -> {
expect(TokenType.Break)
Break()

View File

@ -28,6 +28,15 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
append(node.value.toString())
}
override fun visitForIn(node: ForIn) {
append("for ")
visit(node.symbol)
append(" in ")
visit(node.expression)
append(" ")
visit(node.block)
}
override fun visitStringLiteral(node: StringLiteral) {
append("\"")
append(StringEscape.escape(node.text))

View File

@ -47,6 +47,8 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
If(ManyChars("if"), KeywordFamily),
Else(ManyChars("else"), KeywordFamily),
While(ManyChars("while"), KeywordFamily),
For(ManyChars("for"), KeywordFamily),
In(ManyChars("in"), KeywordFamily),
Continue(ManyChars("continue"), KeywordFamily),
Break(ManyChars("break"), KeywordFamily),
Import(ManyChars("import"), KeywordFamily),