private ScriptVarLink Condition(ref bool execute)
        {
            ScriptVarLink a = Shift(ref execute);

            while (_currentLexer.TokenType == ScriptLex.LexTypes.Equal ||
                   _currentLexer.TokenType == ScriptLex.LexTypes.NEqual ||
                   _currentLexer.TokenType == ScriptLex.LexTypes.TypeEqual ||
                   _currentLexer.TokenType == ScriptLex.LexTypes.NTypeEqual ||
                   _currentLexer.TokenType == ScriptLex.LexTypes.LEqual ||
                   _currentLexer.TokenType == ScriptLex.LexTypes.GEqual ||
                   _currentLexer.TokenType == (ScriptLex.LexTypes) '>' ||
                   _currentLexer.TokenType == (ScriptLex.LexTypes) '<'
                   )
            {
                ScriptLex.LexTypes op = _currentLexer.TokenType;
                _currentLexer.Match(op);
                ScriptVarLink b = Shift(ref execute);

                if (execute)
                {
                    ScriptVar res = a.Var.MathsOp(b.Var, op);

                    if (a.Owned)
                    {
                        a = new ScriptVarLink(res, null);
                    }
                    else
                    {
                        a.ReplaceWith(res);
                    }
                }
            }

            return(a);
        }
Beispiel #2
0
        private ScriptVarLink Term(ref bool execute)
        {
            ScriptVarLink a = Unary(ref execute);

            while (_currentLexer.TokenType == (ScriptLex.LexTypes) '*' ||
                   _currentLexer.TokenType == (ScriptLex.LexTypes) '/' ||
                   _currentLexer.TokenType == (ScriptLex.LexTypes) '%')
            {
                ScriptLex.LexTypes op = _currentLexer.TokenType;
                _currentLexer.Match(_currentLexer.TokenType);

                ScriptVarLink b = Unary(ref execute);
                if (execute)
                {
                    ScriptVar res = a.Var.MathsOp(b.Var, op);

                    if (a.Owned)
                    {
                        a = new ScriptVarLink(res, null);
                    }
                    else
                    {
                        a.ReplaceWith(res);
                    }
                }
            }

            return(a);
        }
        private ScriptVarLink Base(ref bool execute)
        {
            ScriptVarLink a = Ternary(ref execute);

            if (_currentLexer.TokenType == (ScriptLex.LexTypes) '=' ||
                _currentLexer.TokenType == ScriptLex.LexTypes.PlusEqual ||
                _currentLexer.TokenType == ScriptLex.LexTypes.MinusEqual)
            {
                if (execute && a.Owned)
                {
                    if (a.Name.Length > 0)
                    {
                        ScriptVarLink aReal = Root.AddChildNoDup(a.Name, a.Var);
                        a = aReal;
                    }
                    else
                    {
                        // Hit an unknow error here
#if !WINDOWS_UWP
                        System.Diagnostics.Trace.TraceWarning("Trying to assign to an unnamed type...");
#endif
                    }
                }


                ScriptLex.LexTypes op = _currentLexer.TokenType;
                _currentLexer.Match(op);

                ScriptVarLink b = Base(ref execute);

                if (execute)
                {
                    switch (op)
                    {
                    case (ScriptLex.LexTypes) '=':
                        a.ReplaceWith(b);
                        break;

                    case ScriptLex.LexTypes.PlusEqual:
                    {
                        ScriptVar res = a.Var.MathsOp(b.Var, (ScriptLex.LexTypes) '+');
                        a.ReplaceWith(res);
                    }
                    break;

                    case ScriptLex.LexTypes.MinusEqual:
                    {
                        ScriptVar res = a.Var.MathsOp(b.Var, (ScriptLex.LexTypes) '-');
                        a.ReplaceWith(res);
                    }
                    break;

                    default:
                        throw new ScriptException("Base broke");
                    }
                }
            }

            return(a);
        }
        private ScriptVarLink Unary(ref bool execute)
        {
            ScriptVarLink a;

            if (_currentLexer.TokenType == (ScriptLex.LexTypes) '!')
            {
                _currentLexer.Match((ScriptLex.LexTypes) '!');
                a = Factor(ref execute);
                if (execute)
                {
                    ScriptVar zero = new ScriptVar(0);
                    ScriptVar res  = a.Var.MathsOp(zero, ScriptLex.LexTypes.Equal);

                    if (a.Owned)
                    {
                        a = new ScriptVarLink(res, null);
                    }
                    else
                    {
                        a.ReplaceWith(res);
                    }
                }
            }
            else
            {
                a = Factor(ref execute);
            }

            return(a);
        }
        public static String Eval(String code, object userdata)
        {
            ScriptEngine engine = (ScriptEngine)userdata;

            ScriptVarLink returnLink = engine.EvalComplex(code);

            return(returnLink.Var.GetString());
        }
Beispiel #6
0
        private ScriptVarLink Ternary(ref bool execute)
        {
            ScriptVarLink a = Logic(ref execute);

            if (_currentLexer.TokenType == (ScriptLex.LexTypes) '?')
            {
                _currentLexer.Match((ScriptLex.LexTypes) '?');

                if (!execute)
                {
                    Base(ref execute);

                    _currentLexer.Match((ScriptLex.LexTypes) ':');

                    Base(ref execute);
                }
                else
                {
                    bool first = a.Var.GetBool();

                    if (first)
                    {
                        a = Base(ref execute);
                        _currentLexer.Match((ScriptLex.LexTypes) ':');
                        Base(ref execute);
                    }
                    else
                    {
                        Base(ref execute);
                        _currentLexer.Match((ScriptLex.LexTypes) ':');
                        a = Base(ref execute);
                    }
                }
            }

            return(a);
        }
Beispiel #7
0
        private ScriptVarLink Expression(ref bool execute)
        {
            var negate = false;

            if (currentLexer.TokenType == (ScriptLex.LexTypes) '-')
            {
                currentLexer.Match((ScriptLex.LexTypes) '-');
                negate = true;
            }

            var a = Term(ref execute);

            if (negate)
            {
                var zero = new ScriptVar(0);
                var res  = zero.MathsOp(a.Var, (ScriptLex.LexTypes) '-');

                if (a.Owned)
                {
                    a = new ScriptVarLink(res, null);
                }
                else
                {
                    a.ReplaceWith(res);
                }
            }

            while (currentLexer.TokenType == (ScriptLex.LexTypes) '+' ||
                   currentLexer.TokenType == (ScriptLex.LexTypes) '-' ||
                   currentLexer.TokenType == ScriptLex.LexTypes.PlusPlus ||
                   currentLexer.TokenType == ScriptLex.LexTypes.MinusMinus)
            {
                var op = currentLexer.TokenType;
                currentLexer.Match(op);

                if (op == ScriptLex.LexTypes.PlusPlus || op == ScriptLex.LexTypes.MinusMinus)
                {
                    if (execute)
                    {
                        var one    = new ScriptVar(1);
                        var res    = a.Var.MathsOp(one, (ScriptLex.LexTypes)(op == ScriptLex.LexTypes.PlusPlus ? '+' : '-'));
                        var oldVal = new ScriptVarLink(a.Var, null);

                        a.ReplaceWith(res);
                        a = oldVal;
                    }
                }
                else
                {
                    var b = Term(ref execute);
                    if (execute)
                    {
                        var res = a.Var.MathsOp(b.Var, op);

                        CreateLink(ref a, res);
                    }
                }
            }

            return(a);
        }
        private ScriptVarLink Logic(ref bool execute)
        {
            ScriptVarLink a = Condition(ref execute);

            while (_currentLexer.TokenType == (ScriptLex.LexTypes) '&' ||
                   _currentLexer.TokenType == (ScriptLex.LexTypes) '|' ||
                   _currentLexer.TokenType == (ScriptLex.LexTypes) '^' ||
                   _currentLexer.TokenType == ScriptLex.LexTypes.AndAnd ||
                   _currentLexer.TokenType == ScriptLex.LexTypes.OrOr)
            {
                ScriptLex.LexTypes op = _currentLexer.TokenType;
                _currentLexer.Match(op);

                bool shortcut = false;
                bool isBool   = false;

                if (op == ScriptLex.LexTypes.AndAnd)
                {
                    op       = (ScriptLex.LexTypes) '&';
                    shortcut = !a.Var.GetBool();
                    isBool   = true;
                }
                else if (op == ScriptLex.LexTypes.OrOr)
                {
                    op       = (ScriptLex.LexTypes) '|';
                    shortcut = a.Var.GetBool();
                    isBool   = true;
                }

                bool          condition = !shortcut && execute;
                ScriptVarLink b         = Condition(ref condition);

                if (execute && !shortcut)
                {
                    if (isBool)
                    {
                        ScriptVar newA = new ScriptVar(a.Var.GetBool());
                        ScriptVar newB = new ScriptVar(b.Var.GetBool());

                        if (a.Owned)
                        {
                            a = new ScriptVarLink(newA, null);
                        }
                        else
                        {
                            a.ReplaceWith(newA);
                        }

                        if (b.Owned)
                        {
                            b = new ScriptVarLink(newB, null);
                        }
                        else
                        {
                            b.ReplaceWith(newA);
                        }
                    }

                    ScriptVar res = a.Var.MathsOp(b.Var, op);

                    if (a.Owned)
                    {
                        a = new ScriptVarLink(res, null);
                    }
                    else
                    {
                        a.ReplaceWith(res);
                    }
                }
            }

            return(a);
        }