astral-compiler/astral/compiler/views/com/astraldb/codegen/Rule.scala.scala
2023-07-13 14:18:40 -04:00

36 lines
1,006 B
Scala

@import com.astraldb.spec.Rule
@import com.astraldb.spec.Type
@import com.astraldb.spec.Definition
@import com.astraldb.codegen.Match
@import com.astraldb.codegen.Expression
@import com.astraldb.codegen.Code
@import com.astraldb.codegen.CodeScope
@import com.astraldb.typecheck.TypecheckMatch
@(schema: Definition, rule: Rule)
object @{rule.safeLabel} extends Rule[@{rule.family.scalaType}]
{
def apply(plan: @{rule.family.scalaType}): @{rule.family.scalaType} =
{
@{
val matchSchema = TypecheckMatch(
rule.pattern,
rule.family,
schema,
schema.globals
)
}
@Match(
schema = schema,
pattern = rule.pattern,
target = Code.Literal("plan"),
targetPath = Seq.empty,
targetType = rule.family,
onSuccess = Expression(schema, rule.rewrite, _),
onFail = { _ => Code.Literal("plan") },
name = Some("plan"),
scope = CodeScope(schema.globals)
).toString(4).stripPrefix(" ")
}
}