language: add unary plus & minus, post increment & decrement operators, non-newline print builtin. fix block comments (#4)

This commit is contained in:
a dinosaur
2023-09-11 14:34:10 +10:00
committed by GitHub
parent c3afd790b5
commit 8d310e337a
18 changed files with 235 additions and 32 deletions

View File

@ -181,6 +181,12 @@ types:
- name: Negate
values:
token: "!"
- name: UnaryPlus
values:
token: "+"
- name: UnaryMinus
values:
token: "-"
PrefixOperation:
parent: Expression
values:
@ -188,6 +194,24 @@ types:
type: PrefixOperator
- name: expression
type: Expression
SuffixOperator:
values:
- name: token
type: String
enums:
- name: Increment
values:
token: "++"
- name: Decrement
values:
token: "--"
SuffixOperation:
parent: Expression
values:
- name: op
type: SuffixOperator
- name: reference
type: SymbolReference
StringLiteral:
parent: Expression
values:

View File

@ -24,6 +24,8 @@ digraph A {
type_Parentheses [shape=box,label="Parentheses"]
type_PrefixOperator [shape=box,label="PrefixOperator"]
type_PrefixOperation [shape=box,label="PrefixOperation"]
type_SuffixOperator [shape=box,label="SuffixOperator"]
type_SuffixOperation [shape=box,label="SuffixOperation"]
type_StringLiteral [shape=box,label="StringLiteral"]
type_SymbolReference [shape=box,label="SymbolReference"]
type_While [shape=box,label="While"]
@ -49,6 +51,7 @@ digraph A {
type_Expression -> type_ListLiteral
type_Expression -> type_Parentheses
type_Expression -> type_PrefixOperation
type_Expression -> type_SuffixOperation
type_Expression -> type_StringLiteral
type_Expression -> type_SymbolReference
type_Expression -> type_While
@ -84,6 +87,8 @@ digraph A {
type_Parentheses -> type_Expression [style=dotted]
type_PrefixOperation -> type_PrefixOperator [style=dotted]
type_PrefixOperation -> type_Expression [style=dotted]
type_SuffixOperation -> type_SuffixOperator [style=dotted]
type_SuffixOperation -> type_SymbolReference [style=dotted]
type_SymbolReference -> type_Symbol [style=dotted]
type_While -> type_Expression [style=dotted]
type_While -> type_Block [style=dotted]

View File

@ -59,6 +59,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitStringLiteral(node: StringLiteral): Unit =
handle(node)
override fun visitSuffixOperation(node: SuffixOperation): Unit =
handle(node)
override fun visitSymbol(node: Symbol): Unit =
handle(node)

View File

@ -25,6 +25,7 @@ enum class NodeType(val parent: NodeType? = null) {
PrefixOperation(Expression),
SetAssignment(Expression),
StringLiteral(Expression),
SuffixOperation(Expression),
Symbol(Node),
SymbolReference(Expression),
VarAssignment(Expression),

View File

@ -40,6 +40,8 @@ interface NodeVisitor<T> {
fun visitStringLiteral(node: StringLiteral): T
fun visitSuffixOperation(node: SuffixOperation): T
fun visitSymbol(node: Symbol): T
fun visitSymbolReference(node: SymbolReference): T

View File

@ -20,6 +20,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is ListLiteral -> visitListLiteral(node)
is Parentheses -> visitParentheses(node)
is PrefixOperation -> visitPrefixOperation(node)
is SuffixOperation -> visitSuffixOperation(node)
is StringLiteral -> visitStringLiteral(node)
is SymbolReference -> visitSymbolReference(node)
is While -> visitWhile(node)

View File

@ -7,5 +7,7 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("prefixOperator")
enum class PrefixOperator(val token: String) {
Negate("!")
Negate("!"),
UnaryPlus("+"),
UnaryMinus("-")
}

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("suffixOperation")
class SuffixOperation(val op: SuffixOperator, val reference: SymbolReference) : Expression() {
override val type: NodeType = NodeType.SuffixOperation
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(reference)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitSuffixOperation(this)
override fun equals(other: Any?): Boolean {
if (other !is SuffixOperation) return false
return other.op == op && other.reference == reference
}
override fun hashCode(): Int {
var result = op.hashCode()
result = 31 * result + reference.hashCode()
result = 31 * result + type.hashCode()
return result
}
}

View File

@ -0,0 +1,12 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@SerialName("suffixOperator")
enum class SuffixOperator(val token: String) {
Increment("++"),
Decrement("--")
}