2019-02-13 17:10:39 +00:00
|
|
|
using System.Collections;
|
2018-11-27 17:38:28 +00:00
|
|
|
using System.Collections.Generic;
|
2019-02-13 15:29:58 +00:00
|
|
|
using Upsilon.BaseTypes;
|
|
|
|
using Upsilon.Evaluator;
|
2018-11-25 18:30:18 +00:00
|
|
|
using Upsilon.Text;
|
|
|
|
|
2018-11-13 11:48:50 +00:00
|
|
|
namespace Upsilon.Binder
|
|
|
|
{
|
|
|
|
public class BoundIfStatement : BoundStatement
|
|
|
|
{
|
2018-11-25 18:30:18 +00:00
|
|
|
public BoundIfStatement(BoundExpressionStatement condition, BoundBlockStatement block, TextSpan span): base(span)
|
2018-11-13 11:48:50 +00:00
|
|
|
{
|
|
|
|
Condition = condition;
|
2018-11-13 12:54:51 +00:00
|
|
|
Block = block;
|
|
|
|
}
|
2018-11-13 14:15:44 +00:00
|
|
|
|
2018-11-25 18:30:18 +00:00
|
|
|
public BoundIfStatement(BoundExpressionStatement condition, BoundBlockStatement block, BoundElseStatement elseStatement, TextSpan span) : base(span)
|
2018-11-13 12:54:51 +00:00
|
|
|
{
|
2018-11-13 14:15:44 +00:00
|
|
|
Condition = condition;
|
|
|
|
Block = block;
|
2018-11-13 12:54:51 +00:00
|
|
|
ElseStatement = elseStatement;
|
2018-11-13 11:48:50 +00:00
|
|
|
}
|
2018-11-25 18:30:18 +00:00
|
|
|
public BoundIfStatement(BoundExpressionStatement condition, BoundBlockStatement block, BoundIfStatement nextElseIf, TextSpan span) : base(span)
|
2018-11-13 14:15:44 +00:00
|
|
|
{
|
|
|
|
Condition = condition;
|
|
|
|
Block = block;
|
|
|
|
NextElseIf = nextElseIf;
|
|
|
|
}
|
2018-11-13 11:48:50 +00:00
|
|
|
|
|
|
|
public override BoundKind Kind => BoundKind.BoundIfStatement;
|
2019-01-17 12:56:53 +00:00
|
|
|
public override IEnumerable<BoundNode> GetChildren()
|
|
|
|
{
|
|
|
|
yield return Condition;
|
|
|
|
yield return Block;
|
|
|
|
if (NextElseIf != null) yield return NextElseIf;
|
|
|
|
if (ElseStatement != null) yield return ElseStatement;
|
|
|
|
}
|
|
|
|
|
2018-11-13 11:48:50 +00:00
|
|
|
public BoundExpressionStatement Condition { get; }
|
|
|
|
public BoundBlockStatement Block { get; }
|
2018-11-13 14:15:44 +00:00
|
|
|
public BoundIfStatement NextElseIf { get; }
|
2018-11-13 12:54:51 +00:00
|
|
|
public BoundElseStatement ElseStatement { get; }
|
2019-02-13 15:29:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
internal override void Evaluate(EvaluationScope scope, Diagnostics diagnostics, ref EvaluationState state)
|
|
|
|
{
|
|
|
|
var condition = Condition.Expression.Evaluate(scope, diagnostics, ref state);
|
|
|
|
if ((ScriptBoolean) condition)
|
|
|
|
{
|
|
|
|
Block.Evaluate(scope, diagnostics, ref state);
|
|
|
|
}
|
|
|
|
else if (NextElseIf != null)
|
|
|
|
{
|
|
|
|
NextElseIf.Evaluate(scope, diagnostics, ref state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ElseStatement?.Evaluate(scope, diagnostics, ref state);
|
|
|
|
}
|
|
|
|
}
|
2019-02-13 17:10:39 +00:00
|
|
|
|
|
|
|
internal override IEnumerator EvaluateCoroutine(EvaluationScope scope, Diagnostics diagnostics, EvaluationState state)
|
|
|
|
{
|
|
|
|
var condition = Condition.Expression.Evaluate(scope, diagnostics, ref state);
|
|
|
|
if ((ScriptBoolean) condition)
|
|
|
|
{
|
2019-02-17 16:53:22 +00:00
|
|
|
yield return Block.EvaluateCoroutine(scope, diagnostics, state);
|
2019-02-13 17:10:39 +00:00
|
|
|
}
|
|
|
|
if (NextElseIf != null)
|
|
|
|
{
|
2019-02-17 16:53:22 +00:00
|
|
|
yield return NextElseIf.EvaluateCoroutine(scope, diagnostics, state);
|
|
|
|
}
|
|
|
|
if (ElseStatement != null)
|
|
|
|
{
|
|
|
|
yield return ElseStatement.EvaluateCoroutine(scope, diagnostics, state);
|
2019-02-13 17:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-13 12:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class BoundElseStatement : BoundStatement
|
|
|
|
{
|
|
|
|
public BoundBlockStatement Block { get; }
|
|
|
|
|
2018-11-25 18:30:18 +00:00
|
|
|
public BoundElseStatement(BoundBlockStatement block, TextSpan span) : base(span)
|
2018-11-13 12:54:51 +00:00
|
|
|
{
|
|
|
|
Block = block;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override BoundKind Kind => BoundKind.BoundElseStatement;
|
2019-01-17 12:56:53 +00:00
|
|
|
|
|
|
|
public override IEnumerable<BoundNode> GetChildren()
|
|
|
|
{
|
|
|
|
yield return Block;
|
|
|
|
}
|
2019-02-13 15:29:58 +00:00
|
|
|
|
|
|
|
internal override void Evaluate(EvaluationScope scope, Diagnostics diagnostics, ref EvaluationState state)
|
|
|
|
{
|
|
|
|
Block.Evaluate(scope, diagnostics, ref state);
|
|
|
|
}
|
2019-02-13 17:10:39 +00:00
|
|
|
|
|
|
|
internal override IEnumerator EvaluateCoroutine(EvaluationScope scope, Diagnostics diagnostics, EvaluationState state)
|
|
|
|
{
|
|
|
|
return Block.EvaluateCoroutine(scope, diagnostics, state);
|
|
|
|
}
|
2018-11-13 11:48:50 +00:00
|
|
|
}
|
|
|
|
}
|