Skip to content

Write your Statement

Huajiang Wei edited this page Nov 8, 2021 · 2 revisions

How to define Statement


Introduction of Statement

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.

BlockDescriptor - display component of Statement

  • TextBlockStatementDescritor - text
  • ExpressionStatementDescription - show an expression
  • BlockStatementDescriptor - block statement

Example while

    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

输入图片说明