mirror of
				https://github.com/GayPizzaSpecifications/pork.git
				synced 2025-11-03 17:39:38 +00:00 
			
		
		
		
	parser: implement long literal and handle overflow
This commit is contained in:
		@ -182,6 +182,11 @@ types:
 | 
				
			|||||||
    values:
 | 
					    values:
 | 
				
			||||||
    - name: value
 | 
					    - name: value
 | 
				
			||||||
      type: Int
 | 
					      type: Int
 | 
				
			||||||
 | 
					  LongLiteral:
 | 
				
			||||||
 | 
					    parent: Expression
 | 
				
			||||||
 | 
					    values:
 | 
				
			||||||
 | 
					    - name: value
 | 
				
			||||||
 | 
					      type: Long
 | 
				
			||||||
  DoubleLiteral:
 | 
					  DoubleLiteral:
 | 
				
			||||||
    parent: Expression
 | 
					    parent: Expression
 | 
				
			||||||
    values:
 | 
					    values:
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,7 @@ digraph A {
 | 
				
			|||||||
  type_If [shape=box,label="If"]
 | 
					  type_If [shape=box,label="If"]
 | 
				
			||||||
  type_ImportDeclaration [shape=box,label="ImportDeclaration"]
 | 
					  type_ImportDeclaration [shape=box,label="ImportDeclaration"]
 | 
				
			||||||
  type_IntegerLiteral [shape=box,label="IntegerLiteral"]
 | 
					  type_IntegerLiteral [shape=box,label="IntegerLiteral"]
 | 
				
			||||||
 | 
					  type_LongLiteral [shape=box,label="LongLiteral"]
 | 
				
			||||||
  type_DoubleLiteral [shape=box,label="DoubleLiteral"]
 | 
					  type_DoubleLiteral [shape=box,label="DoubleLiteral"]
 | 
				
			||||||
  type_ListLiteral [shape=box,label="ListLiteral"]
 | 
					  type_ListLiteral [shape=box,label="ListLiteral"]
 | 
				
			||||||
  type_Parentheses [shape=box,label="Parentheses"]
 | 
					  type_Parentheses [shape=box,label="Parentheses"]
 | 
				
			||||||
@ -49,6 +50,7 @@ digraph A {
 | 
				
			|||||||
  type_Expression -> type_FunctionCall
 | 
					  type_Expression -> type_FunctionCall
 | 
				
			||||||
  type_Expression -> type_If
 | 
					  type_Expression -> type_If
 | 
				
			||||||
  type_Expression -> type_IntegerLiteral
 | 
					  type_Expression -> type_IntegerLiteral
 | 
				
			||||||
 | 
					  type_Expression -> type_LongLiteral
 | 
				
			||||||
  type_Expression -> type_DoubleLiteral
 | 
					  type_Expression -> type_DoubleLiteral
 | 
				
			||||||
  type_Expression -> type_ListLiteral
 | 
					  type_Expression -> type_ListLiteral
 | 
				
			||||||
  type_Expression -> type_Parentheses
 | 
					  type_Expression -> type_Parentheses
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										25
									
								
								ast/src/main/kotlin/gay/pizza/pork/ast/LongLiteral.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								ast/src/main/kotlin/gay/pizza/pork/ast/LongLiteral.kt
									
									
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -50,6 +50,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
 | 
				
			|||||||
  override fun visitListLiteral(node: ListLiteral): Unit =
 | 
					  override fun visitListLiteral(node: ListLiteral): Unit =
 | 
				
			||||||
    handle(node)
 | 
					    handle(node)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  override fun visitLongLiteral(node: LongLiteral): Unit =
 | 
				
			||||||
 | 
					    handle(node)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun visitNative(node: Native): Unit =
 | 
					  override fun visitNative(node: Native): Unit =
 | 
				
			||||||
    handle(node)
 | 
					    handle(node)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -22,6 +22,7 @@ enum class NodeType(val parent: NodeType? = null) {
 | 
				
			|||||||
  LetAssignment(Expression),
 | 
					  LetAssignment(Expression),
 | 
				
			||||||
  LetDefinition(Definition),
 | 
					  LetDefinition(Definition),
 | 
				
			||||||
  ListLiteral(Expression),
 | 
					  ListLiteral(Expression),
 | 
				
			||||||
 | 
					  LongLiteral(Expression),
 | 
				
			||||||
  Native(Node),
 | 
					  Native(Node),
 | 
				
			||||||
  Parentheses(Expression),
 | 
					  Parentheses(Expression),
 | 
				
			||||||
  PrefixOperation(Expression),
 | 
					  PrefixOperation(Expression),
 | 
				
			||||||
 | 
				
			|||||||
@ -34,6 +34,8 @@ interface NodeVisitor<T> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  fun visitListLiteral(node: ListLiteral): T
 | 
					  fun visitListLiteral(node: ListLiteral): T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  fun visitLongLiteral(node: LongLiteral): T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  fun visitNative(node: Native): T
 | 
					  fun visitNative(node: Native): T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  fun visitParentheses(node: Parentheses): T
 | 
					  fun visitParentheses(node: Parentheses): T
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
 | 
				
			|||||||
    is If -> visitIf(node)
 | 
					    is If -> visitIf(node)
 | 
				
			||||||
    is ImportDeclaration -> visitImportDeclaration(node)
 | 
					    is ImportDeclaration -> visitImportDeclaration(node)
 | 
				
			||||||
    is IntegerLiteral -> visitIntegerLiteral(node)
 | 
					    is IntegerLiteral -> visitIntegerLiteral(node)
 | 
				
			||||||
 | 
					    is LongLiteral -> visitLongLiteral(node)
 | 
				
			||||||
    is DoubleLiteral -> visitDoubleLiteral(node)
 | 
					    is DoubleLiteral -> visitDoubleLiteral(node)
 | 
				
			||||||
    is ListLiteral -> visitListLiteral(node)
 | 
					    is ListLiteral -> visitListLiteral(node)
 | 
				
			||||||
    is Parentheses -> visitParentheses(node)
 | 
					    is Parentheses -> visitParentheses(node)
 | 
				
			||||||
 | 
				
			|||||||
@ -4,5 +4,6 @@ enum class AstPrimitive(val id: kotlin.String) {
 | 
				
			|||||||
  Boolean("Boolean"),
 | 
					  Boolean("Boolean"),
 | 
				
			||||||
  String("String"),
 | 
					  String("String"),
 | 
				
			||||||
  Int("Int"),
 | 
					  Int("Int"),
 | 
				
			||||||
 | 
					  Long("Long"),
 | 
				
			||||||
  Double("Double")
 | 
					  Double("Double")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -39,6 +39,8 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
 | 
				
			|||||||
  override fun visitListLiteral(node: ListLiteral): Any =
 | 
					  override fun visitListLiteral(node: ListLiteral): Any =
 | 
				
			||||||
    node.items.map { it.visit(this) }
 | 
					    node.items.map { it.visit(this) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  override fun visitLongLiteral(node: LongLiteral): Any = node.value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun visitSymbol(node: Symbol): Any = None
 | 
					  override fun visitSymbol(node: Symbol): Any = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun visitFunctionCall(node: FunctionCall): Any {
 | 
					  override fun visitFunctionCall(node: FunctionCall): Any {
 | 
				
			||||||
 | 
				
			|||||||
@ -11,7 +11,15 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
 | 
				
			|||||||
      if (it.text.contains(".")) {
 | 
					      if (it.text.contains(".")) {
 | 
				
			||||||
        DoubleLiteral(it.text.toDouble())
 | 
					        DoubleLiteral(it.text.toDouble())
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        IntegerLiteral(it.text.toInt())
 | 
					        val integer = it.text.toIntOrNull()
 | 
				
			||||||
 | 
					        if (integer != null) {
 | 
				
			||||||
 | 
					          IntegerLiteral(integer)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        val long = it.text.toLongOrNull()
 | 
				
			||||||
 | 
					        if (long != null) {
 | 
				
			||||||
 | 
					          LongLiteral(long)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        throw ParseError("Illegal integer value")
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -70,6 +70,10 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
 | 
				
			|||||||
    append("]")
 | 
					    append("]")
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  override fun visitLongLiteral(node: LongLiteral) {
 | 
				
			||||||
 | 
					    append(node.value.toString())
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun visitNative(node: Native) {
 | 
					  override fun visitNative(node: Native) {
 | 
				
			||||||
    append("native ")
 | 
					    append("native ")
 | 
				
			||||||
    visit(node.form)
 | 
					    visit(node.form)
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,6 @@ import com.intellij.openapi.util.TextRange
 | 
				
			|||||||
import com.intellij.psi.PsiElement
 | 
					import com.intellij.psi.PsiElement
 | 
				
			||||||
import com.intellij.psi.util.elementType
 | 
					import com.intellij.psi.util.elementType
 | 
				
			||||||
import gay.pizza.pork.ast.NodeType
 | 
					import gay.pizza.pork.ast.NodeType
 | 
				
			||||||
import gay.pizza.pork.parser.TokenType
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Suppress("UnstableApiUsage")
 | 
					@Suppress("UnstableApiUsage")
 | 
				
			||||||
class PorkSymbolDeclaration(val element: PsiElement) : PsiSymbolDeclaration {
 | 
					class PorkSymbolDeclaration(val element: PsiElement) : PsiSymbolDeclaration {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user