Skip to content

Commit

Permalink
ast codegen: optional support for inlining visit
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 6, 2023
1 parent 9f90e05 commit 073ea09
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@file:Suppress("ConstPropertyName")
package gay.pizza.pork.buildext.ast

import com.fasterxml.jackson.databind.ObjectMapper
Expand Down Expand Up @@ -85,6 +86,12 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
),
isImmediateExpression = true
)

if (enableVisitAnyInline) {
visitAnyFunction.inline = true
visitAnyFunction.annotations.add("""@Suppress("NOTHING_TO_INLINE")""")
}

visitAnyFunction.body.add("when (node) {")
for (type in world.typeRegistry.types.filter {
world.typeRegistry.roleOfType(it) == AstTypeRole.AstNode
Expand Down Expand Up @@ -391,6 +398,8 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
}

companion object {
private const val enableVisitAnyInline = false

fun run(pkg: String, astDescriptionFile: Path, outputDirectory: Path) {
if (!outputDirectory.exists()) {
outputDirectory.createDirectories()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package gay.pizza.pork.buildext.codegen

class KotlinFunction(
val name: String,
var annotations: MutableList<String> = mutableListOf(),
var typeParameters: MutableList<String> = mutableListOf(),
var extensionOf: String? = null,
var parameters: MutableList<KotlinParameter> = mutableListOf(),
var returnType: String? = null,
var abstract: Boolean = false,
var open: Boolean = false,
var inline: Boolean = false,
var overridden: Boolean = false,
var isImmediateExpression: Boolean = false,
var body: MutableList<String> = mutableListOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ class KotlinWriter() {
}

fun writeFunction(function: KotlinFunction, index: Int = 0, functionCount: Int = 1, indent: String = ""): Unit = buffer.run {
for (annotation in function.annotations) {
append(indent)
appendLine(annotation)
}
append(indent)

if (function.overridden) {
Expand All @@ -157,6 +161,10 @@ class KotlinWriter() {
append("open ")
}

if (function.inline) {
append("inline ")
}

append("fun ")
if (function.typeParameters.isNotEmpty()) {
append("<${function.typeParameters.joinToString(", ")}> ")
Expand Down
3 changes: 2 additions & 1 deletion parser/src/main/kotlin/gay/pizza/pork/parser/Token.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package gay.pizza.pork.parser

class Token(val type: TokenType, val start: Int, val text: String) {
override fun toString(): String = "$start ${type.name} '${text.replace("\n", "\\n")}'"
override fun toString(): String =
"$start ${type.name} '${text.replace("\n", "\\n")}'"

companion object {
fun endOfFile(size: Int): Token =
Expand Down

0 comments on commit 073ea09

Please sign in to comment.