mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 21:00:56 +00:00
ast codegen: optional support for inlining visit
This commit is contained in:
parent
9f90e05d8a
commit
073ea09b13
@ -1,3 +1,4 @@
|
|||||||
|
@file:Suppress("ConstPropertyName")
|
||||||
package gay.pizza.pork.buildext.ast
|
package gay.pizza.pork.buildext.ast
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
@ -85,6 +86,12 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
|
|||||||
),
|
),
|
||||||
isImmediateExpression = true
|
isImmediateExpression = true
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (enableVisitAnyInline) {
|
||||||
|
visitAnyFunction.inline = true
|
||||||
|
visitAnyFunction.annotations.add("""@Suppress("NOTHING_TO_INLINE")""")
|
||||||
|
}
|
||||||
|
|
||||||
visitAnyFunction.body.add("when (node) {")
|
visitAnyFunction.body.add("when (node) {")
|
||||||
for (type in world.typeRegistry.types.filter {
|
for (type in world.typeRegistry.types.filter {
|
||||||
world.typeRegistry.roleOfType(it) == AstTypeRole.AstNode
|
world.typeRegistry.roleOfType(it) == AstTypeRole.AstNode
|
||||||
@ -391,6 +398,8 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private const val enableVisitAnyInline = false
|
||||||
|
|
||||||
fun run(pkg: String, astDescriptionFile: Path, outputDirectory: Path) {
|
fun run(pkg: String, astDescriptionFile: Path, outputDirectory: Path) {
|
||||||
if (!outputDirectory.exists()) {
|
if (!outputDirectory.exists()) {
|
||||||
outputDirectory.createDirectories()
|
outputDirectory.createDirectories()
|
||||||
|
@ -2,12 +2,14 @@ package gay.pizza.pork.buildext.codegen
|
|||||||
|
|
||||||
class KotlinFunction(
|
class KotlinFunction(
|
||||||
val name: String,
|
val name: String,
|
||||||
|
var annotations: MutableList<String> = mutableListOf(),
|
||||||
var typeParameters: MutableList<String> = mutableListOf(),
|
var typeParameters: MutableList<String> = mutableListOf(),
|
||||||
var extensionOf: String? = null,
|
var extensionOf: String? = null,
|
||||||
var parameters: MutableList<KotlinParameter> = mutableListOf(),
|
var parameters: MutableList<KotlinParameter> = mutableListOf(),
|
||||||
var returnType: String? = null,
|
var returnType: String? = null,
|
||||||
var abstract: Boolean = false,
|
var abstract: Boolean = false,
|
||||||
var open: Boolean = false,
|
var open: Boolean = false,
|
||||||
|
var inline: Boolean = false,
|
||||||
var overridden: Boolean = false,
|
var overridden: Boolean = false,
|
||||||
var isImmediateExpression: Boolean = false,
|
var isImmediateExpression: Boolean = false,
|
||||||
var body: MutableList<String> = mutableListOf(),
|
var body: MutableList<String> = mutableListOf(),
|
||||||
|
@ -143,6 +143,10 @@ class KotlinWriter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun writeFunction(function: KotlinFunction, index: Int = 0, functionCount: Int = 1, indent: String = ""): Unit = buffer.run {
|
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)
|
append(indent)
|
||||||
|
|
||||||
if (function.overridden) {
|
if (function.overridden) {
|
||||||
@ -157,6 +161,10 @@ class KotlinWriter() {
|
|||||||
append("open ")
|
append("open ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (function.inline) {
|
||||||
|
append("inline ")
|
||||||
|
}
|
||||||
|
|
||||||
append("fun ")
|
append("fun ")
|
||||||
if (function.typeParameters.isNotEmpty()) {
|
if (function.typeParameters.isNotEmpty()) {
|
||||||
append("<${function.typeParameters.joinToString(", ")}> ")
|
append("<${function.typeParameters.joinToString(", ")}> ")
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package gay.pizza.pork.parser
|
package gay.pizza.pork.parser
|
||||||
|
|
||||||
class Token(val type: TokenType, val start: Int, val text: String) {
|
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 {
|
companion object {
|
||||||
fun endOfFile(size: Int): Token =
|
fun endOfFile(size: Int): Token =
|
||||||
|
Loading…
Reference in New Issue
Block a user