Skip to content

Commit

Permalink
BUGFIX: Register/unregister variables added/remove from existing assi…
Browse files Browse the repository at this point in the history
…gnment statement.
  • Loading branch information
Washi1337 committed Feb 1, 2024
1 parent e6c2f8a commit fe30e67
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Core/Echo.Ast/AssignmentStatement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Echo.Code;
using Echo.Graphing;

Expand Down Expand Up @@ -30,7 +29,9 @@ public AssignmentStatement(IVariable variable, Expression<TInstruction> expressi
public AssignmentStatement(IEnumerable<IVariable> variables, Expression<TInstruction> expression)
{
Expression = expression;
Variables = variables.ToList();
Variables = new VariableCollection<TInstruction>(this);
foreach (var variable in variables)
Variables.Add(variable);
OriginalRange = expression.OriginalRange;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Core/Echo.Ast/AstNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Collections.Generic;
using System.IO;
using Echo.Code;
using Echo.ControlFlow.Serialization.Dot;
using Echo.Graphing;
using Echo.Graphing.Serialization.Dot;

namespace Echo.Ast
{
Expand Down
46 changes: 46 additions & 0 deletions src/Core/Echo.Ast/VariableCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.ObjectModel;
using Echo.Code;

namespace Echo.Ast;

internal sealed class VariableCollection<TInstruction> : Collection<IVariable>
{
private readonly Statement<TInstruction> _owner;

public VariableCollection(Statement<TInstruction> owner)
{
_owner = owner;
}

protected override void InsertItem(int index, IVariable item)
{
base.InsertItem(index, item);
_owner.GetParentCompilationUnit()?.RegisterVariableWrite(item, _owner);
}

protected override void SetItem(int index, IVariable item)
{
var root = _owner.GetParentCompilationUnit();
root?.UnregisterVariableWrite(Items[index], _owner);
base.SetItem(index, item);
root?.RegisterVariableWrite(item, _owner);
}

protected override void RemoveItem(int index)
{
var variable = Items[index];
base.RemoveItem(index);
_owner.GetParentCompilationUnit()?.UnregisterVariableWrite(variable, _owner);
}

protected override void ClearItems()
{
if (_owner.GetParentCompilationUnit() is {} root)
{
foreach (var item in Items)
root.UnregisterVariableWrite(item, _owner);
}

base.ClearItems();
}
}

0 comments on commit fe30e67

Please sign in to comment.