port from perforce
This commit is contained in:
48
intromat/Intromat/Nodes/Code/ForLoopValue.cs
Normal file
48
intromat/Intromat/Nodes/Code/ForLoopValue.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user