Skip to content

Commit

Permalink
implemented node contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
retailcoder committed Sep 14, 2019
1 parent 425bb6e commit f057121
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 15 deletions.
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -15,7 +14,7 @@ public interface IAssignmentNode : IExecutableNode
/// <summary>
/// Gets all references to this assignment in the specified <see cref="IExecutionContext"/>.
/// </summary>
IImmutableSet<IReferenceNode> References(IExecutionContext context);
IReadOnlyList<IReferenceNode> References(IExecutionContext context);
/// <summary>
/// Adds a reference to the value of this assignment operation in the specified <see cref="IExecutionContext"/>.
/// </summary>
Expand Down
Expand Up @@ -2,6 +2,10 @@
{
public interface IExecutionContext
{
void Assign(IAssignmentNode node);
void EnterBranch(IBranchNode node);
void EnterLoop(ILoopNode node);
void EnterJump(IJumpNode node);

}
}
@@ -0,0 +1,78 @@
using Rubberduck.Parsing.Grammar.Abstract.CodePathAnalysis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rubberduck.Parsing.Grammar
{
public partial class VBAParser
{
public partial class SetStmtContext : IAssignmentNode
{
private readonly IDictionary<IExecutionContext, bool> _hasExecuted
= new Dictionary<IExecutionContext, bool>();

public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

private readonly IDictionary<IExecutionContext, IList<IReferenceNode>> _references
= new Dictionary<IExecutionContext, IList<IReferenceNode>>();

public void AddReference(IReferenceNode node, IExecutionContext context)
{
if (!_references.TryGetValue(context, out var refs) || refs is null)
{
_references[context] = refs = new List<IReferenceNode>();
}
refs.Add(node);
}

public IReadOnlyList<IReferenceNode> References(IExecutionContext context)
{
if (!_references.TryGetValue(context, out var refs) || refs is null)
{
_references[context] = refs = new List<IReferenceNode>();
}
return refs.ToList();
}
}

public partial class LetStmtContext : IAssignmentNode
{
private readonly IDictionary<IExecutionContext, bool> _hasExecuted
= new Dictionary<IExecutionContext, bool>();

public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

private readonly IDictionary<IExecutionContext, IList<IReferenceNode>> _references
= new Dictionary<IExecutionContext, IList<IReferenceNode>>();

public void AddReference(IReferenceNode node, IExecutionContext context)
{
if (!_references.TryGetValue(context, out var refs) || refs is null)
{
_references[context] = refs = new List<IReferenceNode>();
}
refs.Add(node);
}

public IReadOnlyList<IReferenceNode> References(IExecutionContext context)
{
if (!_references.TryGetValue(context, out var refs) || refs is null)
{
_references[context] = refs = new List<IReferenceNode>();
}
return refs.ToList();
}
}
}
}
24 changes: 24 additions & 0 deletions Rubberduck.Parsing/Grammar/PartialExtensions/BlockStmtContext.cs
@@ -0,0 +1,24 @@
using Rubberduck.Parsing.Grammar.Abstract.CodePathAnalysis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rubberduck.Parsing.Grammar.PartialExtensions
{
public partial class VBAParser
{
public partial class BlockStmtContext : IExecutableNode
{
private readonly IDictionary<IExecutionContext, bool> _hasExecuted
= new Dictionary<IExecutionContext, bool>();

public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;
}
}
}
Expand Up @@ -17,7 +17,7 @@ public partial class IfStmtContext : IBranchNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand All @@ -31,7 +31,7 @@ public partial class SingleLineIfStmtContext : IBranchNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand All @@ -45,7 +45,7 @@ public partial class IfWithEmptyThenStmtContext : IBranchNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand All @@ -59,7 +59,7 @@ public partial class IfWithNonEmptyThenContext : IBranchNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand All @@ -73,7 +73,7 @@ public partial class ElseIfBlockContext : IBranchNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand Down
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Antlr4.Runtime.Misc;
using Rubberduck.Parsing.Grammar.Abstract;
using Rubberduck.Parsing.Grammar.Abstract.CodePathAnalysis;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Parsing.Grammar
Expand Down
Expand Up @@ -19,7 +19,7 @@ public partial class GoToStmtContext : IJumpNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;
}

Expand All @@ -33,7 +33,7 @@ public partial class OnErrorStmtContext : IJumpNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;
}

Expand All @@ -47,7 +47,7 @@ public partial class ResumeStmtContext : IJumpNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;
}

Expand All @@ -61,7 +61,7 @@ public partial class ReturnStmtContext : IJumpNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;
}

Expand All @@ -75,7 +75,7 @@ public partial class GoSubStmtContext : IJumpReferenceNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;


Expand Down
Expand Up @@ -17,7 +17,7 @@ public partial class ForNextStmtContext : ILoopNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand All @@ -31,7 +31,7 @@ public partial class ForEachStmtContext : ILoopNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand All @@ -44,7 +44,7 @@ public partial class DoLoopStmtContext : ILoopNode
public bool HasExecuted(IExecutionContext context)
=> _hasExecuted.TryGetValue(context, out var value) && value;

public void Execute(IExecutionContext context)
public void Execute(IExecutionContext context)
=> _hasExecuted[context] = true;

public IEvaluatableNode ConditionExpression { get; set; }
Expand Down

0 comments on commit f057121

Please sign in to comment.