language: var, reassign, comparison operators

This commit is contained in:
2023-09-10 04:34:50 -04:00
parent f433ba2776
commit 71999032ac
20 changed files with 262 additions and 26 deletions

View File

@ -14,5 +14,9 @@ enum class InfixOperator(val token: String) {
Equals("=="),
NotEquals("!="),
EuclideanModulo("mod"),
Remainder("rem")
Remainder("rem"),
Lesser("<"),
Greater(">"),
GreaterEqual(">="),
LesserEqual("<=")
}

View File

@ -53,6 +53,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitPrefixOperation(node: PrefixOperation): Unit =
handle(node)
override fun visitSetAssignment(node: SetAssignment): Unit =
handle(node)
override fun visitStringLiteral(node: StringLiteral): Unit =
handle(node)
@ -62,6 +65,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitSymbolReference(node: SymbolReference): Unit =
handle(node)
override fun visitVarAssignment(node: VarAssignment): Unit =
handle(node)
override fun visitWhile(node: While): Unit =
handle(node)

View File

@ -23,8 +23,10 @@ enum class NodeType(val parent: NodeType? = null) {
Native(Node),
Parentheses(Expression),
PrefixOperation(Expression),
SetAssignment(Expression),
StringLiteral(Expression),
Symbol(Node),
SymbolReference(Expression),
VarAssignment(Expression),
While(Expression)
}

View File

@ -36,11 +36,15 @@ interface NodeVisitor<T> {
fun visitPrefixOperation(node: PrefixOperation): T
fun visitSetAssignment(node: SetAssignment): T
fun visitStringLiteral(node: StringLiteral): T
fun visitSymbol(node: Symbol): T
fun visitSymbolReference(node: SymbolReference): T
fun visitVarAssignment(node: VarAssignment): T
fun visitWhile(node: While): T
}

View File

@ -7,6 +7,8 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is Block -> visitBlock(node)
is CompilationUnit -> visitCompilationUnit(node)
is LetAssignment -> visitLetAssignment(node)
is VarAssignment -> visitVarAssignment(node)
is SetAssignment -> visitSetAssignment(node)
is InfixOperation -> visitInfixOperation(node)
is BooleanLiteral -> visitBooleanLiteral(node)
is FunctionCall -> visitFunctionCall(node)

View File

@ -0,0 +1,29 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@SerialName("setAssignment")
class SetAssignment(val symbol: Symbol, val value: Expression) : Expression() {
override val type: NodeType = NodeType.SetAssignment
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol, value)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitSetAssignment(this)
override fun equals(other: Any?): Boolean {
if (other !is SetAssignment) return false
return other.symbol == symbol && other.value == value
}
override fun hashCode(): Int {
var result = symbol.hashCode()
result = 31 * result + value.hashCode()
result = 31 * result + type.hashCode()
return result
}
}

View File

@ -0,0 +1,29 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@SerialName("varAssignment")
class VarAssignment(val symbol: Symbol, val value: Expression) : Expression() {
override val type: NodeType = NodeType.VarAssignment
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol, value)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitVarAssignment(this)
override fun equals(other: Any?): Boolean {
if (other !is VarAssignment) return false
return other.symbol == symbol && other.value == value
}
override fun hashCode(): Int {
var result = symbol.hashCode()
result = 31 * result + value.hashCode()
result = 31 * result + type.hashCode()
return result
}
}