-
-
Notifications
You must be signed in to change notification settings - Fork 98
Write your Statement
Huajiang Wei edited this page Nov 8, 2021
·
2 revisions
Statement contains four proerties and functions of Expression
- ReturnType: the return type of statement
- Descriptor: display component of the first line of statement
- Type: name of statement
- ExecuteImpl: execution function of statement
Statement has two properties, which has to be overrided in your expression
- BlockDescriptor: other line and statement block of expression
- IsClosing: if the statement is the closing of statement, like return、break.
- TextBlockStatementDescritor - text
- ExpressionStatementDescription - show an expression
- BlockStatementDescriptor - block statement
public class WhileStatement : Statement
{
public Expression Test { get; set; }
public BlockStatement Body { get; set; }= new BlockStatement();
public override string ReturnType
{
get { return "void"; }
}
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
{
if (Test == null)
return Completion.Exception(Properties.Language.TestNullException, this);
var c = Test.Execute(enviroment);
if (c.Type == CompletionType.Value)
{
if (c.ReturnValue is bool)
{
Completion cx = Completion.Void;
while ((bool)c.ReturnValue)
{
ExecutionEnvironment current = new ExecutionEnvironment(enviroment);
cx = Body.Execute(current);
if (cx.Type == CompletionType.Exception)
{
return cx;
}
if (cx.Type == CompletionType.Break)
{
return Completion.Void;
}
if (cx.Type == CompletionType.Return)
return cx;
c = Test.Execute(enviroment);
if (!(c.ReturnValue is bool))
return Completion.Exception(Properties.Language.NotBoolean, Test);
}
return cx;
}
else
return Completion.Exception(Properties.Language.NotBoolean, Test);
}
return c;
}
public override Descriptor Descriptor
{
get
{
Descriptor desc = new Descriptor();
desc.Add(new TextItemDescriptor(this, "while", true));
desc.Add(new TextItemDescriptor(this, "("));
desc.Add(new ExpressionDescriptor(this, "Test", "boolean"));
desc.Add(new TextItemDescriptor(this, " )"));
return desc;
}
}
public override BlockDescriptor BlockDescriptor
{
get
{
BlockDescriptor desc = new BlockDescriptor();
desc.Add(new BlockStatementDescriptor(this, "Body"));
return desc;
}
}
public override string Type
{
get
{
return "WhileStatement";
}
}
public override bool IsClosing
{
get { return false; }
}
}
running result when run