tlang-interperter-cs/tlanglib/ForLoop.cs

36 lines
996 B
C#
Raw Normal View History

2023-03-09 23:57:16 +00:00
namespace tlang
{
internal class ForLoop : Node
{
public Node Init {get;set;}
public Node Condition {get;set;}
public Node Increment {get;set;}
public Node Body {get;set;}
public ForLoop(Node init, Node condition, Node inc, Node body)
{
Init = init;
Condition = condition;
Increment = inc;
Body = body;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var env = nodeEnv.SubEnv;
bool isRunning=true;
env["last_ittr"]= new TExternalMethod((args)=>{
isRunning=false;
return new TNumber(1);
});
Init.Execute(env);
TObject obj=new TUninit();
while(Condition.Execute(env).AsBoolean && isRunning)
{
obj=Body.Execute(env);
Increment.Execute(env);
}
return obj;
}
}
}