45 lines
1.1 KiB
C#
45 lines
1.1 KiB
C#
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)=>{
|
|
if(args.Length > 0)
|
|
{
|
|
return TObject.Boolean(!args[0].AsBoolean || !isRunning);
|
|
}
|
|
isRunning=false;
|
|
return TObject.True;
|
|
});
|
|
|
|
TObject obj=TObject.Uninit;
|
|
|
|
if(!DoLoop) isRunning = Condition.Execute(env).AsBoolean;
|
|
while(isRunning)
|
|
{
|
|
|
|
obj=Body.Execute(env);
|
|
if(isRunning)
|
|
isRunning = Condition.Execute(env).AsBoolean;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
|
|
}
|
|
} |