astral-compiler/astral/compiler/views/com/astraldb/codegen/Rule.scala.scala
2023-07-08 18:03:20 -04:00

36 lines
981 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[LogicalPlan]
{
def apply(plan: LogicalPlan): LogicalPlan =
{
@{
val matchSchema = TypecheckMatch(
rule.pattern,
Some("plan"),
Type.AST(rule.family),
schema,
schema.globals
)
}
@Match(
schema = schema,
pattern = rule.pattern,
target = Code.Literal("plan"),
targetType = Type.AST(rule.family),
onSuccess = Expression(schema, rule.rewrite, _),
onFail = { _ => Code.Literal("plan") },
name = Some("plan"),
scope = CodeScope(schema.globals)
).toString(4).stripPrefix(" ")
}
}