Skip to content

Commit

Permalink
#31 - visitor.NET upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Jul 26, 2024
1 parent e1e7385 commit d8e8c44
Show file tree
Hide file tree
Showing 40 changed files with 191 additions and 441 deletions.
3 changes: 2 additions & 1 deletion src/HydraScript.Lib/HydraScript.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Visitor.NET" Version="3.0.0" />
<PackageReference Include="Visitor.NET" Version="4.1.1" />
<PackageReference Include="Visitor.NET.AutoVisitableGen" Version="1.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 5 additions & 26 deletions src/HydraScript.Lib/IR/Ast/AbstractSyntaxTreeNode.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
using System.Collections;
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.FrontEnd.GetTokens.Data;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Variables;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast;

public abstract class AbstractSyntaxTreeNode : IEnumerable<AbstractSyntaxTreeNode>,
IVisitable<InstructionProvider, AddressedInstructions>,
IVisitable<SemanticChecker, Type>,
IVisitable<SymbolTableInitializer>,
IVisitable<TypeSystemLoader>,
IVisitable<DeclarationVisitor>
public abstract class AbstractSyntaxTreeNode :
IEnumerable<AbstractSyntaxTreeNode>,
IVisitable<AbstractSyntaxTreeNode>
{
public AbstractSyntaxTreeNode? Parent { get; set; }

Expand Down Expand Up @@ -55,23 +49,8 @@ public bool ChildOf<T>() where T : AbstractSyntaxTreeNode
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();

#region Visitors

public virtual Unit Accept(SymbolTableInitializer visitor) =>
visitor.Visit(this);

public virtual Unit Accept(TypeSystemLoader visitor) =>
visitor.Visit(this);

public virtual Unit Accept(DeclarationVisitor visitor) =>
visitor.Visit(this);

public virtual Type Accept(SemanticChecker visitor) =>
"undefined";

public abstract AddressedInstructions Accept(InstructionProvider visitor);

#endregion
public virtual TReturn Accept<TReturn>(IVisitor<AbstractSyntaxTreeNode, TReturn> visitor) =>
visitor.DefaultVisit;

protected abstract string NodeRepresentation();

Expand Down
10 changes: 5 additions & 5 deletions src/HydraScript.Lib/IR/Ast/Impl/AbstractSyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class AbstractSyntaxTree : IAbstractSyntaxTree
{
private readonly AbstractSyntaxTreeNode _root;

private readonly SymbolTableInitializer _symbolTableInitializer;
private readonly TypeSystemLoader _typeSystemLoader;
private readonly DeclarationVisitor _declarationVisitor;
private readonly IVisitor<AbstractSyntaxTreeNode> _symbolTableInitializer;
private readonly IVisitor<AbstractSyntaxTreeNode> _typeSystemLoader;
private readonly IVisitor<AbstractSyntaxTreeNode> _declarationVisitor;

private readonly SemanticChecker _semanticChecker;
private readonly InstructionProvider _instructionProvider;
private readonly IVisitor<AbstractSyntaxTreeNode, Type> _semanticChecker;
private readonly IVisitor<AbstractSyntaxTreeNode, AddressedInstructions> _instructionProvider;

public AbstractSyntaxTree(AbstractSyntaxTreeNode root)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;

public abstract class AfterTypesAreLoadedDeclaration : Declaration
{
public abstract override Unit Accept(DeclarationVisitor visitor);
public abstract override TReturn Accept<TReturn>(
IVisitor<AbstractSyntaxTreeNode, TReturn> visitor);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Statements;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;

public class FunctionDeclaration : AfterTypesAreLoadedDeclaration
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class FunctionDeclaration : AfterTypesAreLoadedDeclaration
{
private IReadOnlyCollection<ReturnStatement>? _returnStatements;

Expand Down Expand Up @@ -48,16 +46,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()

protected override string NodeRepresentation() =>
"function " + Name;

public override Unit Accept(SymbolTableInitializer visitor) =>
visitor.Visit(this);

public override Unit Accept(DeclarationVisitor visitor) =>
visitor.Visit(this);

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(InstructionProvider visitor) =>
visitor.Visit(this);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations.AfterTypesAreLoaded;

public class LexicalDeclaration : AfterTypesAreLoadedDeclaration
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class LexicalDeclaration(bool readOnly) : AfterTypesAreLoadedDeclaration
{
public bool ReadOnly { get; }
public List<AssignmentExpression> Assignments { get; }

public LexicalDeclaration(bool readOnly)
{
ReadOnly = readOnly;
Assignments = new();
}
public bool ReadOnly { get; } = readOnly;
public List<AssignmentExpression> Assignments { get; } = [];

public void AddAssignment(AssignmentExpression assignment)
{
Expand All @@ -27,13 +19,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator() =>

protected override string NodeRepresentation() =>
ReadOnly ? "const" : "let";

public override Unit Accept(DeclarationVisitor visitor) =>
visitor.Visit(this);

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(InstructionProvider visitor) =>
visitor.Visit(this);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations;

public class TypeDeclaration : Declaration
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class TypeDeclaration : Declaration
{
private readonly TypeValue _typeValue;
public IdentifierReference TypeId { get; }
Expand All @@ -26,9 +24,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()

protected override string NodeRepresentation() =>
$"type {TypeId.Name} = {_typeValue}";

public override AddressedInstructions Accept(InstructionProvider visitor) => new();

public override Unit Accept(TypeSystemLoader visitor) =>
visitor.Visit(this);
}
17 changes: 5 additions & 12 deletions src/HydraScript.Lib/IR/Ast/Impl/Nodes/Declarations/TypeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ public abstract record TypeValue
public abstract Type BuildType(SymbolTable symbolTable);
}

public record TypeIdentValue(
IdentifierReference TypeId
) : TypeValue
public record TypeIdentValue(IdentifierReference TypeId) : TypeValue
{
public override Type BuildType(SymbolTable symbolTable) =>
symbolTable.FindSymbol<TypeSymbol>(TypeId)?.Type ??
Expand All @@ -22,19 +20,15 @@ public override Type BuildType(SymbolTable symbolTable) =>
public override string ToString() => TypeId;
}

public record ArrayTypeValue(
TypeValue TypeValue
) : TypeValue
public record ArrayTypeValue(TypeValue TypeValue) : TypeValue
{
public override Type BuildType(SymbolTable symbolTable) =>
new ArrayType(TypeValue.BuildType(symbolTable));

public override string ToString() => $"{TypeValue}[]";
}

public record NullableTypeValue(
TypeValue TypeValue
) : TypeValue
public record NullableTypeValue(TypeValue TypeValue) : TypeValue
{
public override Type BuildType(SymbolTable symbolTable) =>
new NullableType(TypeValue.BuildType(symbolTable));
Expand All @@ -48,11 +42,10 @@ public record PropertyTypeValue(
{
public override string ToString() =>
$"{Key}: {TypeValue}";
};
}

public record ObjectTypeValue(
IEnumerable<PropertyTypeValue> Properties
) : TypeValue
IEnumerable<PropertyTypeValue> Properties) : TypeValue
{
public override Type BuildType(SymbolTable symbolTable) =>
new ObjectType(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.AccessExpressions;

public abstract class AccessExpression : Expression
Expand All @@ -26,5 +24,6 @@ public bool HasNext() =>
public bool HasPrev() =>
Prev is not null;

public abstract override Type Accept(SemanticChecker visitor);
public abstract override TReturn Accept<TReturn>(
IVisitor<AbstractSyntaxTreeNode, TReturn> visitor);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.AccessExpressions;

public class DotAccess : AccessExpression
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class DotAccess : AccessExpression
{
public IdentifierReference Property { get; }

Expand All @@ -25,10 +23,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()
}

protected override string NodeRepresentation() => ".";

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(ExpressionInstructionProvider visitor) =>
visitor.Visit(this);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.AccessExpressions;

public class IndexAccess : AccessExpression
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class IndexAccess : AccessExpression
{
public Expression Index { get; }

Expand All @@ -24,10 +21,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()
}

protected override string NodeRepresentation() => "[]";

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(ExpressionInstructionProvider visitor) =>
visitor.Visit(this);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Declarations;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions;

public class AssignmentExpression : Expression
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class AssignmentExpression : Expression
{
public LeftHandSideExpression Destination { get; }
public Expression Source { get; }
Expand All @@ -32,10 +30,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()
}

protected override string NodeRepresentation() => "=";

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(ExpressionInstructionProvider visitor) =>
visitor.Visit(this);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions;

public class BinaryExpression : Expression
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class BinaryExpression : Expression
{
public Expression Left { get; }
public string Operator { get; }
Expand All @@ -28,10 +25,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()
}

protected override string NodeRepresentation() => Operator;

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(ExpressionInstructionProvider visitor) =>
visitor.Visit(this);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using HydraScript.Lib.BackEnd;
using HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions.PrimaryExpressions;
using HydraScript.Lib.IR.Ast.Visitors;
using HydraScript.Lib.IR.CheckSemantics.Visitors;

namespace HydraScript.Lib.IR.Ast.Impl.Nodes.Expressions;

public class CallExpression : LeftHandSideExpression
[AutoVisitable<AbstractSyntaxTreeNode>]
public partial class CallExpression : LeftHandSideExpression
{
public MemberExpression Member { get; }
public List<Expression> Parameters { get; }
Expand Down Expand Up @@ -36,10 +34,4 @@ public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()
}

protected override string NodeRepresentation() => "()";

public override Type Accept(SemanticChecker visitor) =>
visitor.Visit(this);

public override AddressedInstructions Accept(ExpressionInstructionProvider visitor) =>
visitor.Visit(this);
}
Loading

0 comments on commit d8e8c44

Please sign in to comment.