Пример #1
0
        public void AddAction <T1>(string name, Action <T1> action)
        {
            var t1 = ScriptType.ToEnum(typeof(T1));

            ScriptTypes[] args = { t1 };
            Methods.Add(new ScriptFunction(name, action, args));
        }
Пример #2
0
        /// <summary>
        /// Add a user defined function to the script engine.
        /// </summary>
        /// <param name="name">Function name in the script. Example "dialog"</param>
        /// <param name="function">A Predicate definition.</param>
        public void AddCondition <T1>(string name, Func <T1, bool> condition)
        {
            var t1 = ScriptType.ToEnum(typeof(T1));

            ScriptTypes[] args = { t1 };
            Methods.Add(new ScriptCondition(name, condition, args));
        }
Пример #3
0
        /// <summary>
        /// Add a user defined function to the script engine.
        /// </summary>
        /// <param name="name">Function name in the script. Example "dialog"</param>
        /// <param name="function">A Func definition.</param>
        public void AddFunction <TResult>(string name, Func <TResult> function)
        {
            ScriptTypes[] args = { };
            var           tr   = ScriptType.ToEnum(typeof(TResult));

            Methods.Add(new ScriptFunction(name, function, args, tr));
        }
Пример #4
0
        public T Return <T>()
        {
            var returnT = ScriptType.ToEnum(typeof(T));

            switch (returnT)
            {
            case ScriptTypes.String:
            case ScriptTypes.Integer:
            case ScriptTypes.Double:
            case ScriptTypes.Boolean:
            case ScriptTypes.Regex:
                return((T)this.Value);

            case ScriptTypes.ListString:
                return((T)(object)((List <ScriptVariable>) this.Value).Select(x => x.Value.ToString()).ToList());

            case ScriptTypes.ListInteger:
                return((T)(object)((List <ScriptVariable>)(this.Value)).Select(x => x).ToList());

            case ScriptTypes.ListDouble:
                return((T)(object)((List <ScriptVariable>)(this.Value)).Select(x => x).ToList());

            case ScriptTypes.ListBoolean:
                return((T)(object)((List <ScriptVariable>)(this.Value)).Select(x => x).ToList());

            default:
                return(default(T));
            }
        }
Пример #5
0
        public void AddAction <T1, T2, T3>(string name, Action <T1, T2, T3> action)
        {
            var t1 = ScriptType.ToEnum(typeof(T1));
            var t2 = ScriptType.ToEnum(typeof(T2));
            var t3 = ScriptType.ToEnum(typeof(T3));

            ScriptTypes[] args = { t1, t2, t3 };
            Methods.Add(new ScriptFunction(name, action, args));
        }
Пример #6
0
        public void AddFunction <T1, T2, TResult>(string name, Func <T1, T2, TResult> function)
        {
            var t1 = ScriptType.ToEnum(typeof(T1));
            var t2 = ScriptType.ToEnum(typeof(T2));
            var tr = ScriptType.ToEnum(typeof(TResult));

            ScriptTypes[] args = { t1, t2 };
            Methods.Add(new ScriptFunction(name, function, args, tr));
        }
Пример #7
0
        public ScriptVariable Cast <ReturnT>(Lexer lexer)
        {
            var outputType = ScriptType.ToEnum(typeof(ReturnT));

            switch (outputType)
            {
            case ScriptTypes.String:
                switch (this.Type)
                {
                case ScriptTypes.Integer:
                case ScriptTypes.Double:
                    this.Value = this.Value.ToString();
                    break;

                case ScriptTypes.Boolean:
                    this.Value = (bool)this.Value ? "true" : "false";
                    break;

                case ScriptTypes.Null:
                    this.Value = "null";
                    break;
                }
                this.Type = ScriptTypes.String;
                break;

            case ScriptTypes.Integer:
                switch (this.Type)
                {
                case ScriptTypes.String:
                    int tryInt = 0;
                    if (int.TryParse(this.Value.ToString(), out tryInt))
                    {
                        this.Value = tryInt;
                    }
                    else
                    {
                        goto castError;
                    }
                    break;

                case ScriptTypes.Double:
                    double tryDouble = 0;
                    if (double.TryParse(this.Value.ToString(), out tryDouble))
                    {
                        this.Value = tryDouble;
                    }
                    else
                    {
                        goto castError;
                    }
                    break;

                case ScriptTypes.Boolean:
                    this.Value = (bool)this.Value ? 1 : 0;
                    break;
                }
                this.Type = ScriptTypes.Integer;
                break;

            case ScriptTypes.Double:
                switch (this.Type)
                {
                case ScriptTypes.String:
                case ScriptTypes.Integer:
                    double tryDouble = 0;
                    if (double.TryParse(this.Value.ToString(), out tryDouble))
                    {
                        this.Value = tryDouble;
                    }
                    else
                    {
                        goto castError;
                    }
                    break;

                case ScriptTypes.Boolean:
                    this.Value = (bool)this.Value ? 1.0 : 0.0;
                    break;
                }
                this.Type = ScriptTypes.Double;
                break;

            case ScriptTypes.Boolean:

                switch (this.Type)
                {
                case ScriptTypes.String:
                    this.Value = this.Value.ToString() == "true";
                    break;
                }
                this.Type = ScriptTypes.Boolean;
                break;

            case ScriptTypes.ListString:
            case ScriptTypes.ListInteger:
            case ScriptTypes.ListDouble:
            case ScriptTypes.ListBoolean:

                break;

            case ScriptTypes.Void:
                this.Value = default(ReturnT);
                break;
            }
            return(this);

castError:
            lexer.Prev();
            lexer.Prev();
            throw new ScriptException(
                      message: String.Format("Unable to cast value '{0}' from '{1}' to '{2}' on Line {3} Col {4}",
                                             Value.ToString(),
                                             Type.ToString(),
                                             outputType.ToString(),
                                             lexer.LineNumber,
                                             lexer.Position),
                      row: lexer.LineNumber,
                      column: lexer.Position,
                      method: lexer.TokenContents
                      );
        }