Upsilon/Upsilon/Binder/BoundStatements/BoundIfStatement.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2019-02-13 17:10:39 +00:00
using System.Collections;
using System.Collections.Generic;
using Upsilon.BaseTypes;
using Upsilon.Evaluator;
using Upsilon.Text;
2018-11-13 11:48:50 +00:00
namespace Upsilon.Binder
{
public class BoundIfStatement : BoundStatement
{
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
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
}
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; }
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)
{
yield return Block.EvaluateCoroutine(scope, diagnostics, state);
2019-02-13 17:10:39 +00:00
}
if (NextElseIf != null)
{
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; }
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;
}
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
}
}