Skip to content

Write your Expression

Huajiang Wei edited this page Nov 5, 2021 · 1 revision

How to define an Expression


Introduction of Expression

Expression has four property and function, which be realized in your own type.

  • ReturnType: return type of expression, like number, bool, string
  • Descriptor: display component of expression
  • Type: name of expression
  • ExecuteImpl: function of expression execution, which will be call when running the expression

Kinds of Expression display component

  • TextItemDescriptor - Text
  • ExpressionDescriptor - Expression, where use can put other expression.
  • ParameterDescriptor - parameter of function, which can be dragged to other location of expression.
  • VariableDeclarationDescription - variable which can be dragged to other location
  • StringInputDesciptor - string input in TextBox
  • MultiLineStringInputDesciptor - multi line string input in TextBox
  • SelectionItemDescriptor - selection in ComboBox
  • ImageItemDescriptor - display a image

Example operator !

operator ! is not expression of logical expression. This expression has an boolean argument, and the return type is boolean.

    public class NotExpression : Expression
    {
        public Expression Argument { get; set; }
        public override string ReturnType
        {
            get { return "boolean"; }
        }

        public override Descriptor Descriptor
        {
            get
            {
                Descriptor desc = new Descriptor();
                desc.Add(new TextItemDescriptor(this, "! ", true));
                desc.Add(new ExpressionDescriptor(this, "Argument", "boolean") { IsOnlyNumberAllowed = false });
                return desc;
            }
        }
        public override string Type
        {
            get { return "UnaryExpression"; }
        }

        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Argument == null)
                return Completion.Exception(Properties.Language.NullException, this);
            var a = Argument.Execute(enviroment);
            if (a.Type != CompletionType.Value || !(a.ReturnValue is bool))
                return new Completion(Properties.Language.NotBoolean, CompletionType.Exception);
            return new Completion(!(bool)a.ReturnValue);
        }
    }

After adding to toolbar, the script editor can recognize this expression and its function

输入图片说明

Clone this wiki locally