tlang-interperter-cs/tlanglib/WhileLoop.cs

45 lines
1.1 KiB
C#
Raw Normal View History

2023-03-09 23:57:16 +00:00
namespace tlang
{
public class WhileLoop : Node
{
public Node Condition {get;set;}
public Node Body {get;set;}
public bool DoLoop {get;set;}
public WhileLoop(Node condition, Node yes, bool v)
{
Condition = condition;
Body = yes;
DoLoop = v;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
bool isRunning=true;
var env = nodeEnv.SubEnv;
env["last_ittr"]= new TExternalMethod((args)=>{
2023-06-08 18:14:35 +00:00
if(args.Length > 0)
{
return TObject.Boolean(!args[0].AsBoolean || !isRunning);
}
2023-03-09 23:57:16 +00:00
isRunning=false;
2023-06-08 18:14:35 +00:00
return TObject.True;
2023-03-09 23:57:16 +00:00
});
2023-06-08 18:14:35 +00:00
TObject obj=TObject.Uninit;
2023-03-09 23:57:16 +00:00
if(!DoLoop) isRunning = Condition.Execute(env).AsBoolean;
while(isRunning)
{
obj=Body.Execute(env);
2023-06-08 18:14:35 +00:00
if(isRunning)
2023-03-09 23:57:16 +00:00
isRunning = Condition.Execute(env).AsBoolean;
}
return obj;
}
2023-06-08 18:14:35 +00:00
2023-03-09 23:57:16 +00:00
}
}