63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Upsilon.BaseTypes.ScriptFunction;
|
|
using Upsilon.Binder.VariableSymbols;
|
|
using Upsilon.Evaluator;
|
|
using Upsilon.Text;
|
|
|
|
namespace Upsilon.Binder
|
|
{
|
|
public class BoundFunctionAssignmentStatement : BoundStatement
|
|
{
|
|
public VariableSymbol Variable { get; }
|
|
public BoundFunctionExpression Func { get; }
|
|
|
|
public BoundFunctionAssignmentStatement(VariableSymbol variable, BoundFunctionExpression func, TextSpan span): base(span)
|
|
{
|
|
Variable = variable;
|
|
Func = func;
|
|
}
|
|
|
|
public override BoundKind Kind => BoundKind.BoundFunctionAssignmentStatement;
|
|
|
|
public override IEnumerable<BoundNode> GetChildren()
|
|
{
|
|
yield return Func;
|
|
}
|
|
|
|
internal override void Evaluate(EvaluationScope scope, Diagnostics diagnostics, ref EvaluationState state)
|
|
{
|
|
var func = (ScriptRuntimeFunction)Func.Evaluate(scope, diagnostics, ref state);
|
|
func.SetName(Variable.Name);
|
|
if (Variable.Local)
|
|
{
|
|
if (scope.Variables.TryGetValue(Variable.Name, out var f) && f is ScriptRuntimeFunction scriptRuntimeFunction)
|
|
{
|
|
scriptRuntimeFunction.AddOption(func.Options[0]);
|
|
}
|
|
else
|
|
{
|
|
scope.CreateLocal(Variable, func);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (scope.TryGet(Variable, out var f) && f is ScriptRuntimeFunction scriptRuntimeFunction)
|
|
{
|
|
scriptRuntimeFunction.AddOption(func.Options[0]);
|
|
}
|
|
else
|
|
{
|
|
scope.AssignToNearest(Variable, func);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
internal override IEnumerator EvaluateCoroutine(EvaluationScope scope, Diagnostics diagnostics, EvaluationState state)
|
|
{
|
|
Evaluate(scope, diagnostics, ref state);
|
|
yield break;
|
|
}
|
|
}
|
|
} |