2023-06-25 01:15:37 +00:00
|
|
|
type Field = { Type: string; Name: string }
|
|
|
|
type Type = { Name: string; Fields: list<Field> }
|
|
|
|
|
|
|
|
let types =
|
2023-06-28 23:18:31 +00:00
|
|
|
[ { Name = "Sequence"
|
|
|
|
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Right" } ] }
|
|
|
|
{ Name = "Binary"
|
2023-06-25 01:15:37 +00:00
|
|
|
Fields =
|
|
|
|
[ { Type = "Expr"; Name = "Left" }
|
|
|
|
{ Type = "Token"; Name = "Op" }
|
|
|
|
{ Type = "Expr"; Name = "Right" } ] }
|
|
|
|
{ Name = "Grouping"
|
|
|
|
Fields = [ { Type = "Expr"; Name = "Expression" } ] }
|
|
|
|
{ Name = "Literal"
|
|
|
|
Fields =
|
|
|
|
[ { Type = "System.Object"
|
|
|
|
Name = "Value" } ] }
|
|
|
|
{ Name = "Unary"
|
2023-06-28 22:26:08 +00:00
|
|
|
Fields = [ { Type = "Token"; Name = "Op" }; { Type = "Expr"; Name = "Right" } ] }
|
|
|
|
{ Name = "If"
|
|
|
|
Fields =
|
|
|
|
[ { Type = "Expr"; Name = "Condition" }
|
|
|
|
{ Type = "Expr"; Name = "Then" }
|
2023-07-01 06:12:02 +00:00
|
|
|
{ Type = "Expr"; Name = "Else" } ] }
|
|
|
|
{ Name = "Identifier"
|
|
|
|
Fields = [ { Type = "Name"; Name = "Value" } ] }
|
2023-07-02 05:13:24 +00:00
|
|
|
{ Name = "List"
|
|
|
|
Fields = [ { Type = "Expr[]"; Name = "Elements" } ] }
|
2023-07-02 05:30:24 +00:00
|
|
|
{ Name = "Variant"
|
|
|
|
Fields = [ { Type = "Name"; Name = "Tag" }; { Type = "Expr?"; Name = "Argument" } ] }
|
2023-07-02 17:38:48 +00:00
|
|
|
{ Name = "Record"
|
|
|
|
Fields =
|
2023-07-02 17:46:18 +00:00
|
|
|
[ { Type = "Field[]"
|
|
|
|
Name = "Extensions" }
|
2023-07-02 17:38:48 +00:00
|
|
|
{ Type = "BaseRecord?"; Name = "Base" } ] }
|
2023-07-01 06:12:02 +00:00
|
|
|
{ Name = "Selector"
|
2023-07-02 17:46:18 +00:00
|
|
|
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Name"; Name = "FieldName" } ] }
|
|
|
|
{ Name = "Indexer"
|
2023-07-02 18:05:46 +00:00
|
|
|
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Index" } ] }
|
|
|
|
{ Name = "Call"
|
|
|
|
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr?[]"; Name = "Arguments" } ] } ]
|
2023-06-25 01:15:37 +00:00
|
|
|
|
|
|
|
let visitorMethod baseName t = $"visit{t.Name}{baseName}"
|
|
|
|
|
|
|
|
let renderIVisitor (sw: System.IO.StreamWriter) (baseName: string) types =
|
|
|
|
sw.Write($"public interface IVisitor<T> {{\n")
|
|
|
|
|
|
|
|
for t in types do
|
|
|
|
sw.Write($"\tT {visitorMethod baseName t}({t.Name} {baseName.ToLower()});\n")
|
|
|
|
|
|
|
|
sw.Write("}\n")
|
|
|
|
|
|
|
|
let renderType (sw: System.IO.StreamWriter) baseName t =
|
|
|
|
sw.Write
|
|
|
|
$"public class {t.Name} : {baseName}
|
|
|
|
{{\n"
|
|
|
|
|
|
|
|
for f in t.Fields do
|
|
|
|
sw.Write $"\tpublic required {f.Type} {f.Name} {{ get; init; }}\n"
|
|
|
|
|
|
|
|
sw.Write
|
|
|
|
$"\tpublic override T accept<T>(IVisitor<T> visitor)
|
|
|
|
\t{{
|
|
|
|
\t\treturn visitor.{visitorMethod baseName t}(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. //
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
using System;
|
2023-07-02 05:13:24 +00:00
|
|
|
|
2023-06-25 01:15:37 +00:00
|
|
|
namespace Finn.AST;
|
|
|
|
|
|
|
|
public abstract class {baseName} {{
|
|
|
|
\tpublic abstract T accept<T>(IVisitor<T> visitor);
|
|
|
|
}}\n"
|
|
|
|
|
|
|
|
renderIVisitor sw baseName types
|
|
|
|
List.iter (renderType sw baseName) types
|
|
|
|
|
|
|
|
renderAST "." "Expr" types
|