48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System.Diagnostics;
|
|
using System.Text;
|
|
using Intromat.Model;
|
|
using Intromat.Model.Compiler;
|
|
|
|
namespace Intromat.Nodes.Code
|
|
{
|
|
public class ForLoopValue : ExecutionValueBase
|
|
{
|
|
public IStatement? LoopBody { get; set; }
|
|
|
|
public ITypedExpression<int>? LowerBound { get; set; }
|
|
public ITypedExpression<int>? UpperBound { get; set; }
|
|
|
|
public InlineVariableDefinition<int> CurrentIndex { get; } = new();
|
|
|
|
public override void Compile(CompilerContext context, StringBuilder sb)
|
|
{
|
|
Debug.Assert(UpperBound != null, nameof(UpperBound) + " != null");
|
|
|
|
context.EnterNewScope("For loop");
|
|
|
|
CurrentIndex.Value = LowerBound;
|
|
sb.Append("for (");
|
|
CurrentIndex.Compile(context, sb);
|
|
sb.Append("; ");
|
|
sb.Append(CurrentIndex.VariableName);
|
|
sb.Append(" <= ");
|
|
UpperBound.Compile(context, sb);
|
|
sb.Append("; ++");
|
|
sb.Append(CurrentIndex.VariableName);
|
|
sb.Append(")\n{\n");
|
|
LoopBody?.Compile(context, sb);
|
|
sb.Append("\n}\n");
|
|
|
|
context.LeaveScope();
|
|
|
|
base.Compile(context, sb);
|
|
}
|
|
|
|
public override void CompileHeader(CompilerContext context, StringBuilder sb)
|
|
{
|
|
LoopBody?.CompileHeader(context, sb);
|
|
|
|
base.CompileHeader(context, sb);
|
|
}
|
|
}
|
|
} |