-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
60 changed files
with
1,789 additions
and
1,550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Echo.Code; | ||
|
||
namespace Echo.Ast.Analysis; | ||
|
||
/// <summary> | ||
/// Provides a wrapper around a <see cref="IPurityClassifier{TInstruction}"/> that is able to classify statements | ||
/// and expressions with <typeparamref name="TInstruction"/> instructions by purity. | ||
/// </summary> | ||
/// <typeparam name="TInstruction">The type of instructions the statements store.</typeparam> | ||
public class AstPurityClassifier<TInstruction> : IPurityClassifier<Statement<TInstruction>> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the <see cref="AstPurityClassifier{TInstruction}"/> class. | ||
/// </summary> | ||
/// <param name="baseClassifier">The base classifier to use for classifying individual instructions in the AST.</param> | ||
public AstPurityClassifier(IPurityClassifier<TInstruction> baseClassifier) | ||
{ | ||
BaseClassifier = baseClassifier; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the base classifier to use for classifying individual instructions in the AST. | ||
/// </summary> | ||
public IPurityClassifier<TInstruction> BaseClassifier | ||
{ | ||
get; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean IsPure(in Statement<TInstruction> instruction) | ||
{ | ||
return instruction.Accept(AstPurityVisitor<TInstruction>.Instance, BaseClassifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Echo.Code; | ||
|
||
namespace Echo.Ast.Analysis; | ||
|
||
/// <summary> | ||
/// Provides a mechanism for traversing an AST and determining its purity. | ||
/// </summary> | ||
/// <typeparam name="TInstruction">The type of instructions to store in each expression.</typeparam> | ||
public class AstPurityVisitor<TInstruction> : IAstNodeVisitor<TInstruction, IPurityClassifier<TInstruction>, Trilean> | ||
{ | ||
/// <summary> | ||
/// Gets the singleton instance of the <see cref="AstPurityVisitor{TInstruction}"/> class. | ||
/// </summary> | ||
public static AstPurityVisitor<TInstruction> Instance | ||
{ | ||
get; | ||
} = new(); | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(AssignmentStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) => false; | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(ExpressionStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) | ||
{ | ||
return statement.Expression.Accept(this, state); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(PhiStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) => false; | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(InstructionExpression<TInstruction> expression, IPurityClassifier<TInstruction> state) | ||
{ | ||
var result = state.IsPure(expression.Instruction); | ||
|
||
for (int i = 0; i < expression.Arguments.Count && result != Trilean.False; i++) | ||
result &= expression.Arguments[i].Accept(this, state); | ||
|
||
return result; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(VariableExpression<TInstruction> expression, IPurityClassifier<TInstruction> state) => true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.