port from perforce
This commit is contained in:
49
intromat/Intromat/Model/Compiler/CompilerContext.cs
Normal file
49
intromat/Intromat/Model/Compiler/CompilerContext.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Intromat.Model.Compiler
|
||||
{
|
||||
public class ScopeDefinition
|
||||
{
|
||||
public ScopeDefinition(string identifier)
|
||||
{
|
||||
Identifier = identifier;
|
||||
}
|
||||
|
||||
public string Identifier { get; }
|
||||
|
||||
public List<IVariableDefinition> Variables { get; } = new();
|
||||
}
|
||||
|
||||
public class CompilerContext
|
||||
{
|
||||
public Stack<ScopeDefinition> VariablesScopesStack { get; } = new();
|
||||
|
||||
public string FindFreeVariableName()
|
||||
{
|
||||
return "v" + VariablesScopesStack.SelectMany(s => s.Variables).Count();
|
||||
}
|
||||
|
||||
public void AddVariableToCurrentScope(IVariableDefinition variable)
|
||||
{
|
||||
VariablesScopesStack.Peek().Variables.Add(variable);
|
||||
}
|
||||
|
||||
public void EnterNewScope(string scopeIdentifier)
|
||||
{
|
||||
VariablesScopesStack.Push(new ScopeDefinition(scopeIdentifier));
|
||||
}
|
||||
|
||||
public void LeaveScope()
|
||||
{
|
||||
VariablesScopesStack.Pop();
|
||||
}
|
||||
|
||||
public bool IsInScope(IVariableDefinition variable)
|
||||
{
|
||||
if (variable == null) return false;
|
||||
|
||||
return VariablesScopesStack.Any(s => s.Variables.Contains(variable));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user