Files
bluflame/intromat/Intromat/Model/Compiler/CompilerContext.cs
2026-04-18 22:31:51 +02:00

49 lines
1.2 KiB
C#

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));
}
}
}