Add dynamic bindings and better error positions

This commit is contained in:
2023-08-06 15:44:15 -06:00
parent a7421927ae
commit 7d006d8889
8 changed files with 136 additions and 100 deletions

View File

@@ -1,65 +1,93 @@
type Field = { Type: string; Name: string }
type Type = { Name: string; Fields: list<Field> }
type Type = { Name: string; Fields: list<Field>; Start: string }
let exprTypes =
let exprTypes: Type list =
[ { Name = "Sequence"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Right" } ] }
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" } ] }
{ Type = "Expr"; Name = "Right" } ]
Start = "Left.Start" }
{ Name = "Grouping"
Fields = [ { Type = "Expr"; Name = "Expression" } ] }
Fields = [ { Type = "Expr"; Name = "Expression" } ;{Type="Token";Name="LParen"}]
Start = "LParen"}
{ Name = "Literal"
Fields =
[ { Type = "System.Object"
Name = "Value" } ] }
Name = "Value" }; {Type="Token"; Name="Token"} ]
Start = "Token"}
{ Name = "Unary"
Fields = [ { Type = "Token"; Name = "Op" }; { Type = "Expr"; Name = "Right" } ] }
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 = "Expr"; Name = "Else" }
{ Type = "Token"; Name = "IfToken" } ]
Start = "IfToken" }
{ Name = "Variable"
Fields = [ { Type = "Token"; Name = "Value" } ] }
Fields = [ { Type = "Token"; Name = "Value" } ]
Start = "Value" }
{ Name = "List"
Fields = [ { Type = "Expr[]"; Name = "Elements" } ] }
Fields = [ { Type = "Expr[]"; Name = "Elements" }; {Type="Token"; Name = "LBracket"} ]
Start = "LBracket" }
{ Name = "Variant"
Fields = [ { Type = "Token"; Name = "Tag" }; { Type = "Expr?"; Name = "Argument" } ] }
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 = "BaseRecord?"; Name = "Base" }
{ Type = "Token"; Name= "LBrace"} ]
Start = "LBrace"}
{ Name = "Selector"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Token"; Name = "FieldName" } ] }
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Token"; Name = "FieldName" } ]
Start = "Left.Start" }
{ Name = "Indexer"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Index" } ] }
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Index" } ]
Start = "Left.Start" }
{ Name = "Call"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr?[]"; Name = "Arguments" } ] }
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr?[]"; Name = "Arguments" } ]
Start = "Left.Start" }
{ Name = "Let"
Fields =
[ { Type = "Binding[]"
Name = "Bindings" }
{ Type = "Expr"; Name = "Body" } ] }
{ Type = "Expr"; Name = "Body" }
{ Type = "Token"; Name = "LetToken"} ]
Start = "LetToken" }
{ Name = "When"
Fields = [ { Type = "Expr"; Name = "Head" }; { Type = "VarBinding[]"; Name = "Cases" } ] } ]
Fields =
[ { Type = "Expr"; Name = "Head" }
{ Type = "VarBinding[]"
Name = "Cases" }
{ Type = "Token"; Name = "WhenToken"} ]
Start = "WhenToken" } ]
let patternTypes =
[ { Name = "SimplePattern"
Fields = [ { Type = "Token?"; Name = "Identifier" } ] }
Fields = [ { Type = "Token?"; Name = "Identifier" }; {Type = "Token"; Name = "Token"}]
Start = "Token" }
{ Name = "VariantPattern"
Fields = [ { Type = "Token"; Name = "Tag" }; { Type = "Pattern?"; Name = "Argument" } ] }
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" } ] }
Fields = [ { Type = "Token"; Name = "Name" }; { Type = "Pattern?"; Name = "Pattern" } ]
Start = "Name" }
{ Name = "RecordPattern"
Fields =
[ { Type = "FieldPattern[]"
Name = "Fields" }
{ Type = "SimplePattern?"
Name = "Rest" } ] } ]
Name = "Rest" }
{ Type = "Token"
Name = "LBrace"} ]
Start = "LBrace" } ]
let visitorMethod baseName t = $"visit{t.Name}{baseName}"
@@ -82,7 +110,7 @@ let renderType (sw: System.IO.StreamWriter) baseName t =
List.iteri writeField t.Fields
sw.Write
$") : {baseName}()
$") : {baseName}({t.Start})
{{\n"
sw.Write
@@ -107,7 +135,7 @@ let renderAST outputDir baseName types =
namespace Finn.AST;
public abstract record {baseName}() {{
public abstract record {baseName}(Token Start) {{
\tpublic abstract TResult accept<TContext, TResult>(TContext context, I{baseName}Visitor<TContext, TResult> visitor);
}}\n"