From 429d0d98fe5197eabdc04bcd130ed8b98866f601 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Thu, 21 May 2026 07:53:56 +0200 Subject: [PATCH] feat: update railroad diagrams with revised syntax --- syntax/midas.typ | 175 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 126 insertions(+), 49 deletions(-) diff --git a/syntax/midas.typ b/syntax/midas.typ index 17db6d6..56d2884 100644 --- a/syntax/midas.typ +++ b/syntax/midas.typ @@ -1,4 +1,15 @@ -#import "@preview/fervojo:0.1.1": render +#import "@preview/fervojo:0.1.1": default-css, render + +#let extra-css = ```css +svg.railroad .terminal rect { + fill: #F7DCD4; +} +``` +#let css = default-css() + bytes(extra-css.text) + +#let variable = ``` +{[`variable` 'identifier'*"."]} +``` #let value = ``` {[`value` < @@ -8,90 +19,156 @@ >]} ``` -#let constraint = ``` -{[`constraint` <"_", 'value'> <">", "<", ">=", "<=", "==", "!="> <"_", 'value'>]} +#let lambda-value = ``` +{[`lambda-value` <"_", 'value', 'variable'>]} ``` -#let type-with-constraints = ``` -{[`type-with-constraints` 'identifier' ]} +#let lambda-operator = ``` +{[`lambda-operator` <">", "<", ">=", "<=", "==", "!=">]} +``` + +#let lambda = ``` +{[`lambda` 'lambda-value' ['lambda-operator' 'lambda-value']*!]} +``` + +#let simple-type = ``` +{[`simple-type` 'identifier' ]} +``` + +#let template = ``` +{[`template` "[" 'simple-type' "]"]} +``` + +#let type = ``` +{[`type` 'identifier' ]} +``` + +#let constraint = ``` +{[`constraint` <'identifier', 'lambda'>]} +``` + +#let wrapped-constraint = ``` +{[`wrapped-constraint` <'constraint', ["(" 'constraint' ")"]>]} +``` + +#let constraints = ``` +{[`constraints` 'wrapped-constraint'*"&"]} ``` #let type-property = ``` -{[`type-property` 'identifier' ":" 'type-with-constraints']} +{[`type-property` 'identifier' ":" 'type' ]} ``` #let type-body = ``` {[`type-body` "{" "}"]} ``` -#let operation-type = ``` -{[`operation-type` "<" 'type-with-constraints' ">"]} -``` - #let type-statement = ``` -{[`type-statement` "type" 'identifier' "<" 'type-with-constraints'*"," ">" ]} +{[`type-statement` "type" 'identifier' <[["(" 'type' ")"] ], 'type-body'>]} ``` -#let operation-statement = ``` -{[`operation-statement` "op" 'operation-type' "operator" 'operation-type' "=" 'operation-type']} +#let op-definition = ``` +{[`op-definition` "op" <'identifier', ["__" 'identifier' "__"]> "(" 'type' ")" "->" 'type']} ``` -#let constraint-statement = ``` -{[`constraint-statement` "constraint" 'identifier' "=" 'constraint']} +#let extend-statement = ``` +{[`extend-statement` "extend" 'type' "{" "}"]} +``` + +#let predicate-statement = ``` +{[`predicate-statement` "predicate" 'identifier' "(" 'identifier' ":" 'type' ")" "=" 'constraints']} ``` #let statement = ``` -{[`statement` <'type-statement', 'operation-statement', 'constraint-statement'>]} +{[`statement` <'type-statement', 'extend-statement', 'predicate-statement'>]} ``` #let rules = ( - value, - constraint, - type-with-constraints, - type-property, - type-body, - operation-type, - type-statement, - operation-statement, - constraint-statement, - statement, + variable: variable, + value: value, + lambda-value: lambda-value, + lambda-operator: lambda-operator, + lambda: lambda, + simple-type: simple-type, + template: template, + type: type, + constraint: constraint, + wrapped-constraint: wrapped-constraint, + constraints: constraints, + type-property: type-property, + type-body: type-body, + type-statement: type-statement, + op-definition: op-definition, + extend-statement: extend-statement, + predicate-statement: predicate-statement, + statement: statement, +) + +#let inline = ( + "value", + "variable", + "lambda-operator", + "template", + "lambda", + "simple-type", + "wrapped-constraint", + "type-property", + "type-body", + "op-definition", + "type-statement", + "extend-statement", + "predicate-statement", ) #set text(font: "Source Sans 3") -= Midas type definition syntax +#title[Midas type definition syntax] -#for rule in rules { - render(rule) -} += Outline -/* -#let by-name = ( - value: value, - constraint: constraint, - type-with-constraints: type-with-constraints, - type-property: type-property, - type-body: type-body, - operation-type: operation-type, - type-statement: type-statement, - operation-statement: operation-statement, - constraint-statement: constraint-statement, +#box( + columns( + 2, + outline(title: none), + ), + height: 8cm, + stroke: 1pt, + inset: 1em, ) += Statements and expressions + +#for (name, rule) in rules.pairs().rev() { + [== #name] + render(rule, css: css) +} + #let substitute(base-rule) = { let new-rule = base-rule - for (key, rule) in by-name.pairs() { - new-rule = new-rule.replace("'" + key + "'", rule.text.slice(1, -1)) + for name in inline { + let rule = rules.at(name) + let replacement = rule.text.slice(1, -1).replace(regex("\[`.*?`"), "[") + replacement = "[" + replacement + "#`" + name + "`]" + new-rule = new-rule.replace( + "'" + name + "'", + replacement, + ) } if new-rule != base-rule { new-rule = substitute(new-rule) } - return new-rule.replace(regex("`.*?`"), "") + return new-rule } -#let combined = raw(substitute(statement.text)) - - #set page(flipped: true) -#render(combined) -*/ \ No newline at end of file + += Combined rules + +#for (name, rule) in rules.pairs() { + if not name in inline { + [== #name] + let combined = substitute(rule.text) + render(raw(combined), css: css) + //raw(block: true, combined) + } +}