parser: implement long literal and handle overflow

This commit is contained in:
2023-09-12 00:25:13 -04:00
parent 7aa9d95221
commit 1b363dcf56
12 changed files with 55 additions and 2 deletions

View File

@ -182,6 +182,11 @@ types:
values:
- name: value
type: Int
LongLiteral:
parent: Expression
values:
- name: value
type: Long
DoubleLiteral:
parent: Expression
values:

View File

@ -20,6 +20,7 @@ digraph A {
type_If [shape=box,label="If"]
type_ImportDeclaration [shape=box,label="ImportDeclaration"]
type_IntegerLiteral [shape=box,label="IntegerLiteral"]
type_LongLiteral [shape=box,label="LongLiteral"]
type_DoubleLiteral [shape=box,label="DoubleLiteral"]
type_ListLiteral [shape=box,label="ListLiteral"]
type_Parentheses [shape=box,label="Parentheses"]
@ -49,6 +50,7 @@ digraph A {
type_Expression -> type_FunctionCall
type_Expression -> type_If
type_Expression -> type_IntegerLiteral
type_Expression -> type_LongLiteral
type_Expression -> type_DoubleLiteral
type_Expression -> type_ListLiteral
type_Expression -> type_Parentheses

View File

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

View File

@ -50,6 +50,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitListLiteral(node: ListLiteral): Unit =
handle(node)
override fun visitLongLiteral(node: LongLiteral): Unit =
handle(node)
override fun visitNative(node: Native): Unit =
handle(node)

View File

@ -22,6 +22,7 @@ enum class NodeType(val parent: NodeType? = null) {
LetAssignment(Expression),
LetDefinition(Definition),
ListLiteral(Expression),
LongLiteral(Expression),
Native(Node),
Parentheses(Expression),
PrefixOperation(Expression),

View File

@ -34,6 +34,8 @@ interface NodeVisitor<T> {
fun visitListLiteral(node: ListLiteral): T
fun visitLongLiteral(node: LongLiteral): T
fun visitNative(node: Native): T
fun visitParentheses(node: Parentheses): T

View File

@ -17,6 +17,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is If -> visitIf(node)
is ImportDeclaration -> visitImportDeclaration(node)
is IntegerLiteral -> visitIntegerLiteral(node)
is LongLiteral -> visitLongLiteral(node)
is DoubleLiteral -> visitDoubleLiteral(node)
is ListLiteral -> visitListLiteral(node)
is Parentheses -> visitParentheses(node)