Run() public method

Runs raw source code and returns the result.
public Run ( string code ) : SObject
code string The source code to run.
return Pokemon3D.Scripting.Types.SObject
Esempio n. 1
0
        static void Main(string[] args)
        {
            bool exit      = false;
            var  processor = new ScriptProcessor();

            while (!exit)
            {
                Console.Write("< ");
                string input = Console.ReadLine();

                var result = processor.Run(input);

                if (result.TypeOf() == Types.SObject.LITERAL_TYPE_ERROR)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Types.SError error = (Types.SError)result;

                    Console.WriteLine("x " + ((Types.SString)error.Members["type"].Data).Value + ": " +
                                      ((Types.SString)error.Members["message"].Data).Value);

                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("> " + result.ToScriptSource());
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            bool exit = false;
            var processor = new ScriptProcessor();

            while (!exit)
            {
                Console.Write("< ");
                string input = Console.ReadLine();
                
                var result = processor.Run(input);

                if (result.TypeOf() == Types.SObject.LITERAL_TYPE_ERROR)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Types.SError error = (Types.SError)result;

                    Console.WriteLine("x " + ((Types.SString)error.Members["type"].Data).Value + ": " +
                                      ((Types.SString)error.Members["message"].Data).Value);

                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("> " + result.ToScriptSource());
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
        }
        /// <summary>
        /// Creates a new <see cref="ScriptProcessor"/> and runs the provided source code.
        /// </summary>
        internal static ScriptProcessor GetRun(string source)
        {
            var processor = new ScriptProcessor();

            processor.Run(source);

            return processor;
        }
Esempio n. 4
0
        public static SObject Eval(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (parameters.Length == 0)
                return processor.Undefined;

            string code;
            if (parameters[0] is SString)
                code = ((SString)parameters[0]).Value;
            else
                code = parameters[0].ToString(processor).Value;

            var evalProcessor = new ScriptProcessor(processor.Context);
            return evalProcessor.Run(code);
        }
Esempio n. 5
0
        public static SObject Eval(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (parameters.Length == 0)
            {
                return(processor.Undefined);
            }

            string code;

            if (parameters[0] is SString)
            {
                code = ((SString)parameters[0]).Value;
            }
            else
            {
                code = parameters[0].ToString(processor).Value;
            }

            var evalProcessor = new ScriptProcessor(processor.Context);

            return(evalProcessor.Run(code));
        }
Esempio n. 6
0
        private SObject ExecuteExecutable(ScriptStatement statement)
        {
            if (statement.IsCompoundStatement)
            {
                var processor = new ScriptProcessor(Context, GetLineNumber());

                // Remove { and }:
                var code = statement.Code.Remove(0, 1);
                code = code.Remove(code.Length - 1, 1);

                var returnObject = processor.Run(code);

                _breakIssued    = processor._breakIssued;
                _continueIssued = processor._continueIssued;
                _returnIssued   = processor._returnIssued;

                return(returnObject);
            }
            else
            {
                var exp = ResolveParentheses(statement.Code).Trim();

                #region QuickConvert

                // have quick conversions for small statements here
                // parameter statements are much faster that way:
                if (exp == SObject.LITERAL_BOOL_TRUE)
                {
                    return(CreateBool(true));
                }
                else if (exp == SObject.LITERAL_BOOL_FALSE)
                {
                    return(CreateBool(false));
                }
                else if (exp == SObject.LITERAL_UNDEFINED || exp == "")
                {
                    return(Undefined);
                }
                else if (exp == SObject.LITERAL_NULL)
                {
                    return(Null);
                }
                else if (exp.StartsWith("\"") && exp.EndsWith("\"") && !exp.Remove(exp.Length - 1, 1).Remove(0, 1).Contains("\""))
                {
                    return(CreateString(exp.Remove(exp.Length - 1, 1).Remove(0, 1)));
                }
                else if (exp.All(char.IsDigit))
                {
                    double num;
                    SNumber.TryParse(exp, out num);
                    return(CreateNumber(num));
                }

                #endregion

                if (exp.Contains("=>"))
                {
                    exp = EvaluateLambda(exp);
                }
                if (exp.Contains("."))
                {
                    exp = EvaluateOperator(exp, ".");
                }
                if (exp.Contains("++"))
                {
                    exp = EvaluateOperator(exp, "++");
                }
                if (exp.Contains("--"))
                {
                    exp = EvaluateOperator(exp, "--");
                }
                if (exp.Contains("!"))
                {
                    exp = EvaluateReverseBool(exp);
                }
                if (exp.Contains("**"))
                {
                    exp = EvaluateOperator(exp, "**");
                }
                if (exp.Contains("*"))
                {
                    exp = EvaluateOperator(exp, "*");
                }
                if (exp.Contains("/"))
                {
                    exp = EvaluateOperator(exp, "/");
                }
                if (exp.Contains("%"))
                {
                    exp = EvaluateOperator(exp, "%");
                }
                if (exp.Contains("+"))
                {
                    exp = EvaluateOperator(exp, "+");
                }
                if (exp.Contains("-"))
                {
                    exp = EvaluateOperator(exp, "-");
                }
                if (exp.Contains("<="))
                {
                    exp = EvaluateOperator(exp, "<=");
                }
                if (exp.Contains(">="))
                {
                    exp = EvaluateOperator(exp, ">=");
                }
                if (exp.Contains("<"))
                {
                    exp = EvaluateOperator(exp, "<");
                }
                if (exp.Contains(">"))
                {
                    exp = EvaluateOperator(exp, ">");
                }
                if (exp.Contains("==="))
                {
                    exp = EvaluateOperator(exp, "===");
                }
                if (exp.Contains("!=="))
                {
                    exp = EvaluateOperator(exp, "!==");
                }
                if (exp.Contains("=="))
                {
                    exp = EvaluateOperator(exp, "==");
                }
                if (exp.Contains("!="))
                {
                    exp = EvaluateOperator(exp, "!=");
                }
                if (exp.Contains("&&"))
                {
                    exp = EvaluateOperator(exp, "&&");
                }
                if (exp.Contains("||"))
                {
                    exp = EvaluateOperator(exp, "||");
                }

                return(ToScriptObject(exp));
            }
        }
        private SObject ExecuteExecutable(ScriptStatement statement)
        {
            if (statement.IsCompoundStatement)
            {
                var processor = new ScriptProcessor(Context, GetLineNumber());

                // Remove { and }:
                var code = statement.Code.Remove(0, 1);
                code = code.Remove(code.Length - 1, 1);

                var returnObject = processor.Run(code);

                _breakIssued = processor._breakIssued;
                _continueIssued = processor._continueIssued;
                _returnIssued = processor._returnIssued;

                return returnObject;
            }
            else
            {
                var exp = ResolveParentheses(statement.Code).Trim();

                #region QuickConvert

                // have quick conversions for small statements here
                // parameter statements are much faster that way:
                if (exp == SObject.LITERAL_BOOL_TRUE)
                {
                    return CreateBool(true);
                }
                else if (exp == SObject.LITERAL_BOOL_FALSE)
                {
                    return CreateBool(false);
                }
                else if (exp == SObject.LITERAL_UNDEFINED || exp == "")
                {
                    return Undefined;
                }
                else if (exp == SObject.LITERAL_NULL)
                {
                    return Null;
                }
                else if (exp.StartsWith("\"") && exp.EndsWith("\"") && !exp.Remove(exp.Length - 1, 1).Remove(0, 1).Contains("\""))
                {
                    return CreateString(exp.Remove(exp.Length - 1, 1).Remove(0, 1));
                }
                else if (exp.All(char.IsDigit))
                {
                    double num;
                    SNumber.TryParse(exp, out num);
                    return CreateNumber(num);
                }

                #endregion

                if (exp.Contains("=>"))
                    exp = EvaluateLambda(exp);
                if (exp.Contains("."))
                    exp = EvaluateOperator(exp, ".");
                if (exp.Contains("++"))
                    exp = EvaluateOperator(exp, "++");
                if (exp.Contains("--"))
                    exp = EvaluateOperator(exp, "--");
                if (exp.Contains("!"))
                    exp = EvaluateReverseBool(exp);
                if (exp.Contains("**"))
                    exp = EvaluateOperator(exp, "**");
                if (exp.Contains("*"))
                    exp = EvaluateOperator(exp, "*");
                if (exp.Contains("/"))
                    exp = EvaluateOperator(exp, "/");
                if (exp.Contains("%"))
                    exp = EvaluateOperator(exp, "%");
                if (exp.Contains("+"))
                    exp = EvaluateOperator(exp, "+");
                if (exp.Contains("-"))
                    exp = EvaluateOperator(exp, "-");
                if (exp.Contains("<="))
                    exp = EvaluateOperator(exp, "<=");
                if (exp.Contains(">="))
                    exp = EvaluateOperator(exp, ">=");
                if (exp.Contains("<"))
                    exp = EvaluateOperator(exp, "<");
                if (exp.Contains(">"))
                    exp = EvaluateOperator(exp, ">");
                if (exp.Contains("==="))
                    exp = EvaluateOperator(exp, "===");
                if (exp.Contains("!=="))
                    exp = EvaluateOperator(exp, "!==");
                if (exp.Contains("=="))
                    exp = EvaluateOperator(exp, "==");
                if (exp.Contains("!="))
                    exp = EvaluateOperator(exp, "!=");
                if (exp.Contains("&&"))
                    exp = EvaluateOperator(exp, "&&");
                if (exp.Contains("||"))
                    exp = EvaluateOperator(exp, "||");

                return ToScriptObject(exp);
            }
        }
Esempio n. 8
0
        public void ObjectTranslateTest()
        {
            var processor = new ScriptProcessor();
            ScriptContextManipulator.AddPrototype(processor, typeof(Pokemon));

            processor.Run("var p = new Pokemon(); p.SetName(\"Pika\");");

            object objp = ScriptContextManipulator.GetVariableTranslated(processor, "p");

            Assert.IsTrue(objp.GetType() == typeof(Pokemon));

            Pokemon p = (Pokemon)objp;

            Assert.AreEqual("Pikachu", p.OriginalName);
            Assert.AreEqual("Pika", p.Name);
        }