87 lines
2.6 KiB
C#
87 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace StgSharp.Script.Express
|
|
{
|
|
public class ExpSwitchToken : ExpToken
|
|
{
|
|
private ExpToken _target;
|
|
private List<ExpToken> _default;
|
|
private Dictionary<ExpToken, List<ExpToken>> _cases;
|
|
|
|
public List<ExpToken> this[ExpToken index]
|
|
{
|
|
get
|
|
{
|
|
if (_cases.TryGetValue(index, out var ret))
|
|
{
|
|
return ret;
|
|
}
|
|
return _default;
|
|
}
|
|
}
|
|
|
|
public ExpSwitchToken(string name, ExpSchema context,
|
|
ExpToken target, IEnumerable<ExpToken> @default ,
|
|
params (ExpToken label, IEnumerable<ExpToken> op)[] cases
|
|
) : base(name, context)
|
|
{
|
|
_target = target;
|
|
if (@default == null)
|
|
{
|
|
_default = new List<ExpToken>();
|
|
}
|
|
else
|
|
{
|
|
_default = new List<ExpToken>(@default);
|
|
}
|
|
_cases = new Dictionary<ExpToken, List<ExpToken>>();
|
|
foreach (var item in cases)
|
|
{
|
|
if (item.op == null)
|
|
{
|
|
_cases.Add(item.label, new List<ExpToken>());
|
|
}
|
|
else
|
|
{
|
|
_cases.Add(item.label, new List<ExpToken>(item.op));
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool ContainsCase(ExpToken token)
|
|
{
|
|
return _cases.ContainsKey(token);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds an expression to certain <see langword="case"/>.
|
|
/// The added expression will be run at last.
|
|
/// </summary>
|
|
/// <param name="label">Label of case to add operation</param>
|
|
/// <param name="expressionRoot">Root token of the expression to be added.</param>
|
|
/// <exception cref="ExpCaseNotFoundException">Cannot fine the case label in current witch expression.</exception>
|
|
public void AddOperationToCase(ExpToken label, ExpToken expressionRoot)
|
|
{
|
|
AssertSchemaInclude(expressionRoot);
|
|
if (_cases.TryGetValue(label,out var op))
|
|
{
|
|
op.Add(expressionRoot);
|
|
}
|
|
else
|
|
{
|
|
throw new ExpCaseNotFoundException(this, label);
|
|
}
|
|
}
|
|
|
|
public void AddOperationToDefault(ExpToken expressionRoot)
|
|
{
|
|
if (expressionRoot != null)
|
|
{
|
|
_default.Add(expressionRoot);
|
|
}
|
|
}
|
|
}
|
|
}
|