mirror of
				https://github.com/GayPizzaSpecifications/pork.git
				synced 2025-11-03 17:39:38 +00:00 
			
		
		
		
	ast codegen: optional support for inlining visit
This commit is contained in:
		@ -1,3 +1,4 @@
 | 
			
		||||
@file:Suppress("ConstPropertyName")
 | 
			
		||||
package gay.pizza.pork.buildext.ast
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper
 | 
			
		||||
@ -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
 | 
			
		||||
@ -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()
 | 
			
		||||
 | 
			
		||||
@ -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(),
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
@ -157,6 +161,10 @@ class KotlinWriter() {
 | 
			
		||||
      append("open ")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (function.inline) {
 | 
			
		||||
      append("inline ")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    append("fun ")
 | 
			
		||||
    if (function.typeParameters.isNotEmpty()) {
 | 
			
		||||
      append("<${function.typeParameters.joinToString(", ")}> ")
 | 
			
		||||
 | 
			
		||||
@ -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 =
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user