Introduce the requirement of let for assignment.

This commit is contained in:
2023-09-04 02:33:13 -07:00
parent 128f40bcf4
commit 3545aa076f
10 changed files with 40 additions and 28 deletions

View File

@ -31,6 +31,14 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
ListLiteral(items)
}
private fun readLetAssignment(): LetAssignment = within {
expect(TokenType.Let)
val symbol = readSymbolRaw()
expect(TokenType.Equals)
val value = readExpression()
LetAssignment(symbol, value)
}
private fun readSymbolRaw(): Symbol = within {
expect(TokenType.Symbol) { Symbol(it.text) }
}
@ -43,8 +51,6 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
}
expect(TokenType.RightParentheses)
FunctionCall(symbol, arguments)
} else if (next(TokenType.Equals)) {
Assignment(symbol, readExpression())
} else {
SymbolReference(symbol)
}
@ -114,6 +120,10 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
readListLiteral()
}
TokenType.Let -> {
readLetAssignment()
}
TokenType.Symbol -> {
readSymbolCases()
}

View File

@ -71,7 +71,8 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
append(")")
}
override fun visitDefine(node: Assignment) {
override fun visitLetAssignment(node: LetAssignment) {
append("let ")
visit(node.symbol)
append(" = ")
visit(node.value)

View File

@ -31,6 +31,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
Import(Keyword("import"), KeywordFamily),
Export(Keyword("export"), KeywordFamily),
Func(Keyword("func"), KeywordFamily),
Let(Keyword("let"), KeywordFamily),
Whitespace(CharConsumer { it == ' ' || it == '\r' || it == '\n' || it == '\t' }),
BlockComment(CommentFamily),
LineComment(CommentFamily),