From 7b4257d7197f47db8ed0e9c134c393206a296972 Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Sun, 10 Sep 2023 20:54:48 -0400 Subject: [PATCH] ast: default value support in generation --- ast/src/main/ast/pork.yml | 1 + ast/src/main/kotlin/gay/pizza/pork/ast/ArgumentSpec.kt | 2 +- .../main/kotlin/gay/pizza/pork/buildext/ast/AstCodegen.kt | 7 ++++++- .../main/kotlin/gay/pizza/pork/buildext/ast/AstValue.kt | 3 ++- .../gay/pizza/pork/buildext/ast/AstValueDescription.kt | 3 ++- .../main/kotlin/gay/pizza/pork/buildext/ast/AstWorld.kt | 7 ++++++- 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/ast/src/main/ast/pork.yml b/ast/src/main/ast/pork.yml index 7b582b5..9221695 100644 --- a/ast/src/main/ast/pork.yml +++ b/ast/src/main/ast/pork.yml @@ -123,6 +123,7 @@ types: type: Symbol - name: multiple type: Boolean + defaultValue: "false" FunctionDefinition: parent: Definition values: diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/ArgumentSpec.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/ArgumentSpec.kt index cf1be91..977565c 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/ArgumentSpec.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/ArgumentSpec.kt @@ -6,4 +6,4 @@ import kotlinx.serialization.Serializable @Serializable @SerialName("argumentSpec") -class ArgumentSpec(var symbol: Symbol, var multiple: Boolean) +class ArgumentSpec(var symbol: Symbol, var multiple: Boolean = false) diff --git a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstCodegen.kt b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstCodegen.kt index 5137182..2769fc5 100644 --- a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstCodegen.kt +++ b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstCodegen.kt @@ -241,6 +241,9 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld for (value in type.values!!) { val member = KotlinMember(value.name, toKotlinType(value.typeRef)) member.abstract = value.abstract + if (value.defaultValue != null) { + member.value = value.defaultValue + } if (type.isParentAbstract(value)) { member.overridden = true } @@ -279,7 +282,9 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld ), isImmediateExpression = true ) - val anyListMembers = type.values?.any { it.typeRef.form == AstTypeRefForm.List } ?: false + val anyListMembers = type.values?.any { + it.typeRef.form == AstTypeRefForm.List + } ?: false val elideVisitChildren: Boolean if (anyListMembers) { val visitParameters = (type.values?.mapNotNull { diff --git a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValue.kt b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValue.kt index 31232fc..95e274e 100644 --- a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValue.kt +++ b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValue.kt @@ -3,5 +3,6 @@ package gay.pizza.pork.buildext.ast class AstValue( val name: String, val typeRef: AstTypeRef, - val abstract: Boolean = false + val abstract: Boolean = false, + val defaultValue: String? = null ) diff --git a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValueDescription.kt b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValueDescription.kt index 38dfaf5..7ef84c1 100644 --- a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValueDescription.kt +++ b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstValueDescription.kt @@ -3,5 +3,6 @@ package gay.pizza.pork.buildext.ast data class AstValueDescription( val name: String, val type: String, - val required: Boolean = false + val required: Boolean = false, + val defaultValue: String? = null ) diff --git a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstWorld.kt b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstWorld.kt index 0fa61b6..c9c8019 100644 --- a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstWorld.kt +++ b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstWorld.kt @@ -59,7 +59,12 @@ class AstWorld { type.markHasValues() for (value in typeDescription.values) { val typeRef = AstTypeRef.parse(value.type, world.typeRegistry) - val typeValue = AstValue(value.name, typeRef, abstract = value.required) + val typeValue = AstValue( + value.name, + typeRef, + abstract = value.required, + defaultValue = value.defaultValue + ) type.addValue(typeValue) } }