finn-lang/ast_classes.fsx

147 lines
5.2 KiB
Plaintext

type Field = { Type: string; Name: string }
type Type = { Name: string; Fields: list<Field>; Start: string }
let exprTypes: Type list =
[ { Name = "Sequence"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Right" } ]
Start = "Left.Start" }
{ Name = "Binary"
Fields =
[ { Type = "Expr"; Name = "Left" }
{ Type = "Token"; Name = "Op" }
{ Type = "Expr"; Name = "Right" } ]
Start = "Left.Start" }
{ Name = "Grouping"
Fields = [ { Type = "Expr"; Name = "Expression" } ;{Type="Token";Name="LParen"}]
Start = "LParen"}
{ Name = "Literal"
Fields =
[ { Type = "System.Object"
Name = "Value" }; {Type="Token"; Name="Token"} ]
Start = "Token"}
{ Name = "Unary"
Fields = [ { Type = "Token"; Name = "Op" }; { Type = "Expr"; Name = "Right" } ]
Start = "Op" }
{ Name = "If"
Fields =
[ { Type = "Expr"; Name = "Condition" }
{ Type = "Expr"; Name = "Then" }
{ Type = "Expr"; Name = "Else" }
{ Type = "Token"; Name = "IfToken" } ]
Start = "IfToken" }
{ Name = "Variable"
Fields = [ { Type = "Token"; Name = "Value" } ]
Start = "Value" }
{ Name = "List"
Fields = [ { Type = "Expr[]"; Name = "Elements" }; {Type="Token"; Name = "LBracket"} ]
Start = "LBracket" }
{ Name = "Variant"
Fields = [ { Type = "Token"; Name = "Tag" }; { Type = "Expr?"; Name = "Argument" }; {Type="Token";Name="Backtick"}]
Start = "Backtick" }
{ Name = "Record"
Fields =
[ { Type = "Field[]"
Name = "Extensions" }
{ Type = "BaseRecord?"; Name = "Base" }
{ Type = "Token"; Name= "LBrace"} ]
Start = "LBrace"}
{ Name = "Selector"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Token"; Name = "FieldName" } ]
Start = "Left.Start" }
{ Name = "Indexer"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Index" } ]
Start = "Left.Start" }
{ Name = "Call"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr?[]"; Name = "Arguments" } ]
Start = "Left.Start" }
{ Name = "Let"
Fields =
[ { Type = "Binding[]"
Name = "Bindings" }
{ Type = "Expr"; Name = "Body" }
{ Type = "Token"; Name = "LetToken"} ]
Start = "LetToken" }
{ Name = "When"
Fields =
[ { Type = "Expr"; Name = "Head" }
{ Type = "VarBinding[]"
Name = "Cases" }
{ Type = "Token"; Name = "WhenToken"} ]
Start = "WhenToken" } ]
let patternTypes =
[ { Name = "SimplePattern"
Fields = [ { Type = "Token?"; Name = "Identifier" }; {Type = "Token"; Name = "Token"}]
Start = "Token" }
{ Name = "VariantPattern"
Fields = [ { Type = "Token"; Name = "Tag" }; { Type = "Pattern?"; Name = "Argument" }; {Type="Token"; Name = "Backtick"} ]
Start = "Backtick" }
{ Name = "FieldPattern"
Fields = [ { Type = "Token"; Name = "Name" }; { Type = "Pattern?"; Name = "Pattern" } ]
Start = "Name" }
{ Name = "RecordPattern"
Fields =
[ { Type = "FieldPattern[]"
Name = "Fields" }
{ Type = "SimplePattern?"
Name = "Rest" }
{ Type = "Token"
Name = "LBrace"} ]
Start = "LBrace" } ]
let visitorMethod baseName t = $"visit{t.Name}{baseName}"
let renderIVisitor (sw: System.IO.StreamWriter) (baseName: string) types =
sw.Write($"public interface I{baseName}Visitor<TContext, TResult> {{\n")
for t in types do
sw.Write($"\tTResult {visitorMethod baseName t}(TContext context, {t.Name} {baseName.ToLower()});\n")
sw.Write("}\n")
let renderType (sw: System.IO.StreamWriter) baseName t =
let writeField i field =
if i > 0 then
sw.Write(", ")
sw.Write $"{field.Type} {field.Name}"
sw.Write $"public partial record {t.Name}("
List.iteri writeField t.Fields
sw.Write
$") : {baseName}({t.Start})
{{\n"
sw.Write
$"\tpublic override TResult accept<TContext, TResult>(TContext context, I{baseName}Visitor<TContext, TResult> visitor)
\t{{
\t\treturn visitor.{visitorMethod baseName t}(context, this);
\t}}\n"
sw.Write("}\n")
let renderAST outputDir baseName types =
let path = System.IO.Path.Combine(outputDir, baseName + ".g.cs")
use sw = new System.IO.StreamWriter(path)
sw.Write
$"////////////////////////////////////////////////////////////
// THIS FILE IS GENERATED. //
// DO NOT EDIT. //
////////////////////////////////////////////////////////////
#nullable enable
namespace Finn.AST;
public abstract record {baseName}(Token Start) {{
\tpublic abstract TResult accept<TContext, TResult>(TContext context, I{baseName}Visitor<TContext, TResult> visitor);
}}\n"
renderIVisitor sw baseName types
List.iter (renderType sw baseName) types
renderAST "." "Expr" exprTypes
renderAST "." "Pattern" patternTypes