Exemplo n.º 1
0
        void Expr4(out ScriptVar v)
        {
            ScriptVar b;

            Expr5(out v);
            while (la.kind == 22 || la.kind == 23)
            {
                bool isDiv;
                if (la.kind == 22)
                {
                    Get();
                    isDiv = false;
                }
                else
                {
                    Get();
                    isDiv = true;
                }
                Expr5(out b);
                if (!_isParsing)
                {
                    if (!v.IsNumber || !b.IsNumber)
                    {
                        SemErr(string.Format("Operator '{0}' cant be applied to numbers only", isDiv ? '/' : '*'));
                    }
                    v.AsNumber = isDiv ? (b.AsNumber == 0f ? 0f : v.AsNumber / b.AsNumber) : v.AsNumber * b.AsNumber;
                }
            }
        }
Exemplo n.º 2
0
        public static void ExecImpl(ScriptVar var, object userData)
        {
            var engine = (ScriptEngine)userData;
            var script = var.GetParameter("str").String;

            engine.Execute(script);
        }
        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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        private ScriptVarLink Logic(ref bool execute)
        {
            var a = Condition(ref execute);

            ScriptVarLink b;

            while (currentLexer.TokenType == (ScriptLex.LexTypes) '&' ||
                   currentLexer.TokenType == (ScriptLex.LexTypes) '|' ||
                   currentLexer.TokenType == (ScriptLex.LexTypes) '^' ||
                   currentLexer.TokenType == ScriptLex.LexTypes.AndAnd ||
                   currentLexer.TokenType == ScriptLex.LexTypes.OrOr)
            {
                var noExecute = false;
                var op        = currentLexer.TokenType;
                currentLexer.Match(op);

                var shortcut = false;
                var isBool   = false;

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

                if (shortcut)
                {
                    b = Condition(ref noExecute);
                }
                else
                {
                    b = Condition(ref execute);
                }

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

                        CreateLink(ref a, newA);
                        CreateLink(ref b, newB);
                    }

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

                    CreateLink(ref a, res);
                }
            }

            return(a);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void BoolScriptVarIsIntAndTrue()
        {
            var v = new ScriptVar(true);

            Assert.IsTrue(v.IsInt);
            Assert.IsTrue(v.Bool);
        }
Exemplo n.º 8
0
        public static void MathRandomImpl(ScriptVar var, object userData)
        {
            var random       = new Random();
            var randomNumber = random.NextDouble();

            var.ReturnVar.Float = (randomNumber);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        public static void MathATanImpl(ScriptVar var, object userData)
        {
            var val       = var.GetParameter("val").Float;
            var returnVal = Math.Atan(val);

            var.ReturnVar.Float = (returnVal);
        }
Exemplo n.º 11
0
        public static void Round(ScriptVar var, object userData)
        {
            var val       = var.GetParameter("val").Float;
            var returnVal = Math.Round(val);

            var.ReturnVar.Float = (returnVal);
        }
Exemplo n.º 12
0
        public static void IntValueOfImpl(ScriptVar var, object userData)
        {
            var str = var.GetParameter("str").String;

            var intResult = Convert.ToInt32(str[0]);

            var.ReturnVar.Int = intResult;
        }
Exemplo n.º 13
0
        public static void CharToIntImpl(ScriptVar var, object userData)
        {
            var charStr   = var.GetParameter("char").String;
            var charParam = charStr[0];
            var charAsInt = Convert.ToInt32(charParam);

            var.ReturnVar.Int = charAsInt;
        }
Exemplo n.º 14
0
        public static void StringFromCharCodeImpl(ScriptVar var, object userData)
        {
            var charVar = var.GetParameter("char").Int;

            var charAsChar = Convert.ToChar(charVar);

            var.ReturnVar.String = charAsChar.ToString();
        }
Exemplo n.º 15
0
        public static void EvalImpl(ScriptVar var, object userData)
        {
            var engine    = (ScriptEngine)userData;
            var script    = var.GetParameter("str").String;
            var returnVal = engine.EvalComplex(script);

            var.ReturnVar = returnVal.Var;
        }
Exemplo n.º 16
0
        public static void MathATan2Impl(ScriptVar var, object userData)
        {
            var y         = var.GetParameter("y").Float;
            var x         = var.GetParameter("x").Float;
            var returnVal = Math.Atan2(y, x);

            var.ReturnVar.Float = (returnVal);
        }
Exemplo n.º 17
0
        public static void StringIndexOfImpl(ScriptVar var, object userData)
        {
            var search      = var.GetParameter("search").String;
            var searchInStr = var.GetParameter("this").String;

            var index = searchInStr.IndexOf(search);

            var.ReturnVar.Int = index;
        }
Exemplo n.º 18
0
        public static void MathMaxImpl(ScriptVar var, object userData)
        {
            var val1 = var.GetParameter("val1").Float;
            var val2 = var.GetParameter("val2").Float;

            var returnVal = Math.Max(val1, val2);

            var.ReturnVar.Float = (returnVal);
        }
Exemplo n.º 19
0
        public static void MathPowImpl(ScriptVar var, object userData)
        {
            var y = var.GetParameter("y").Float;
            var x = var.GetParameter("x").Float;

            var returnVal = Math.Pow(x, y);

            var.ReturnVar.Float = (returnVal);
        }
Exemplo n.º 20
0
        public static void StringSubStringImpl(ScriptVar var, object userData)
        {
            var str = var.GetParameter("this").String;
            var lo  = var.GetParameter("lo").Int;
            var hi  = var.GetParameter("hi").Int;

            var substr = str.Substring(lo, hi - lo);

            var.ReturnVar.String = substr;
        }
Exemplo n.º 21
0
        void Expr2(out ScriptVar v)
        {
            ScriptVar b; int mode;

            Expr3(out v);
            while (StartOf(3))
            {
                if (la.kind == 16)
                {
                    Get();
                    mode = 0;
                }
                else if (la.kind == 17)
                {
                    Get();
                    mode = 1;
                }
                else if (la.kind == 18)
                {
                    Get();
                    mode = 2;
                }
                else
                {
                    Get();
                    mode = 3;
                }
                Expr3(out b);
                if (!_isParsing)
                {
                    if (!v.IsNumber || !b.IsNumber)
                    {
                        SemErr("'<' operator can be applied to numbers only");
                    }
                    switch (mode)
                    {
                    case 0:
                        v.AsNumber = v.AsNumber < b.AsNumber ? 1f : 0f;
                        break;

                    case 1:
                        v.AsNumber = v.AsNumber > b.AsNumber ? 1f : 0f;
                        break;

                    case 2:
                        v.AsNumber = v.AsNumber <= b.AsNumber ? 1f : 0f;
                        break;

                    case 3:
                        v.AsNumber = v.AsNumber >= b.AsNumber ? 1f : 0f;
                        break;
                    }
                }
            }
        }
Exemplo n.º 22
0
        public static void IntParseIntImpl(ScriptVar var, object userData)
        {
            var str = var.GetParameter("str").String;

            if (int.TryParse(str, out int intResult) == false)
            {
                intResult = 0;
            }

            var.ReturnVar.Int = intResult;
        }
Exemplo n.º 23
0
        public static void MathRandomIntImpl(ScriptVar var, object userData)
        {
            var random = new Random();

            var min = var.GetParameter("min").Int;
            var max = var.GetParameter("max").Int;

            var randomInt = random.Next(min, max);

            var.ReturnVar.Int = randomInt;
        }
Exemplo n.º 24
0
        public static void JsonStringifyImpl(ScriptVar var, object userData)
        {
            var stream = new MemoryStream();

            var.GetParameter("obj").GetJSON(stream, "");

            stream.Seek(0, SeekOrigin.Begin);

            var streamReader = new StreamReader(stream);
            var json         = streamReader.ReadToEnd();

            var.ReturnVar.String = json;
        }
Exemplo n.º 25
0
        public static void StringCharCodeAtImpl(ScriptVar var, object userData)
        {
            var str = var.GetParameter("this").String;
            var pos = var.GetParameter("pos").Int;

            var charCode = 0;

            if (str.Length > pos)
            {
                charCode = Convert.ToInt32(str[pos]);
            }

            var.ReturnVar.Int = charCode;
        }
Exemplo n.º 26
0
        public static void StringCharAtImpl(ScriptVar var, object userData)
        {
            var str = var.GetParameter("this").String;
            var pos = var.GetParameter("pos").Int;

            var charStr = string.Empty;

            if (str.Length > pos)
            {
                charStr = str[pos].ToString();
            }

            var.ReturnVar.String = charStr;
        }
Exemplo n.º 27
0
        public string CallFunction()
        {
            la          = Scanner.EmptyToken;
            _isReturned = false;
            RetVal      = new ScriptVar();
            try {
                Get();
                Block();
            } catch (Exception ex) {
                return(ex.Message);
            }

            return(null);
        }
Exemplo n.º 28
0
        public static void StringSplitImpl(ScriptVar var, object userData)
        {
            var str = var.GetParameter("this").String;
            var sep = var.GetParameter("sep").String;

            var spltStrs = str.Split(new[] { sep }, StringSplitOptions.None);


            var.ReturnVar.SetArray();
            for (int x = 0; x < spltStrs.Length; x++)
            {
                var.ReturnVar.SetArrayIndex(x, new ScriptVar(spltStrs[x]));
            }
        }
Exemplo n.º 29
0
        private ScriptVarLink Unary(ref bool execute)
        {
            ScriptVarLink a;

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

                    CreateLink(ref a, res);
                }
            }
            else if (currentLexer.TokenType == ScriptLex.LexTypes.RTypeOf)
            {
                currentLexer.Match(ScriptLex.LexTypes.RTypeOf);
                a = Factor(ref execute);
                if (execute)
                {
                    var varType = new ScriptVar(a.Var.GetObjectType());

                    CreateLink(ref a, varType);
                }
            }
            else if (currentLexer.TokenType == ScriptLex.LexTypes.PlusPlus || currentLexer.TokenType == ScriptLex.LexTypes.MinusMinus)
            {
                var op = currentLexer.TokenType == ScriptLex.LexTypes.PlusPlus ? '+' : '-';
                currentLexer.Match(currentLexer.TokenType);

                a = Factor(ref execute);
                if (execute)
                {
                    var one = new ScriptVar(1);
                    var res = a.Var.MathsOp(one, (ScriptLex.LexTypes)op);
                    a.ReplaceWith(res);
                    //CreateLink(ref a, res);
                }
            }
            else
            {
                a = Factor(ref execute);
            }

            return(a);
        }
Exemplo n.º 30
0
        void Expr1(out ScriptVar v)
        {
            ScriptVar b; int op;

            Expr2(out v);
            while (la.kind == 14 || la.kind == 15)
            {
                if (la.kind == 14)
                {
                    Get();
                    op = 0;
                }
                else
                {
                    Get();
                    op = 1;
                }
                Expr2(out b);
                if (!_isParsing)
                {
                    switch (op)
                    {
                    case 0:
                        if (v.Type != b.Type)
                        {
                            v.AsNumber = 0f;
                        }
                        else
                        {
                            v.AsNumber = v.IsNumber ? (v.AsNumber == b.AsNumber ? 1f : 0f) : (v.AsString == b.AsString ? 1f : 0f);
                        }
                        break;

                    case 1:
                        if (v.Type != b.Type)
                        {
                            v.AsNumber = 1f;
                        }
                        else
                        {
                            v.AsNumber = v.IsNumber ? (v.AsNumber != b.AsNumber ? 1f : 0f) : (v.AsString != b.AsString ? 1f : 0f);
                        }
                        break;
                    }
                }
            }
        }
Exemplo n.º 31
0
        void Expr3(out ScriptVar v)
        {
            ScriptVar b; bool isSub;
            Expr4(out v);
            while (la.kind == 20 || la.kind == 21) {
            if (la.kind == 20) {
                Get();
                isSub = false;
            } else {
                Get();
                isSub = true;
            }
            Expr4(out b);
            if (!_isParsing) {
            if (v.IsString || b.IsString) {
                if (isSub) {
                    SemErr("Operator '-' cant be applied to strings");
                } else {
                    v.AsString = v.AsString + b.AsString;
                }
            } else {
                if (v.IsNumber || b.IsNumber) {
                    v.AsNumber = v.AsNumber + (isSub ? -b.AsNumber : b.AsNumber);
                }
            }
            }

            }
        }
Exemplo n.º 32
0
        void Expr4(out ScriptVar v)
        {
            ScriptVar b;
            Expr5(out v);
            while (la.kind == 22 || la.kind == 23) {
            bool isDiv;
            if (la.kind == 22) {
                Get();
                isDiv = false;
            } else {
                Get();
                isDiv = true;
            }
            Expr5(out b);
            if (!_isParsing) {
            if (!v.IsNumber || !b.IsNumber) {
                SemErr(string.Format("Operator '{0}' cant be applied to numbers only", isDiv ? '/' : '*'));
            }
            v.AsNumber = isDiv ? (b.AsNumber == 0f ? 0f : v.AsNumber / b.AsNumber) : v.AsNumber * b.AsNumber;
            }

            }
        }
Exemplo n.º 33
0
 void Expr5(out ScriptVar v)
 {
     v = new ScriptVar(); var isNegative = false;
     if (la.kind == 21) {
     Get();
     isNegative = true;
     }
     if (la.kind == 5) {
     Get();
     Expr(out v);
     Expect(7);
     } else if (la.kind == 1 || la.kind == 2 || la.kind == 3) {
     Term(out v);
     } else SynErr(34);
     if (!_isParsing) {
     if (isNegative) {
     if (v.IsNumber) {
             v.AsNumber = -v.AsNumber;
     } else {
         SemErr("Minus can be applied only on numbers");
     }
     }
     }
 }
Exemplo n.º 34
0
        void Expr2(out ScriptVar v)
        {
            ScriptVar b; int mode;
            Expr3(out v);
            while (StartOf(3)) {
            if (la.kind == 16) {
                Get();
                mode = 0;
            } else if (la.kind == 17) {
                Get();
                mode = 1;
            } else if (la.kind == 18) {
                Get();
                mode = 2;
            } else {
                Get();
                mode = 3;
            }
            Expr3(out b);
            if (!_isParsing) {
            if (!v.IsNumber || !b.IsNumber) {
                SemErr("'<' operator can be applied to numbers only");
            }
            switch (mode) {
                case 0:
                    v.AsNumber = v.AsNumber < b.AsNumber ? 1f : 0f;
                    break;
                case 1:
                    v.AsNumber = v.AsNumber > b.AsNumber ? 1f : 0f;
                    break;
                case 2:
                    v.AsNumber = v.AsNumber <= b.AsNumber ? 1f : 0f;
                    break;
                case 3:
                    v.AsNumber = v.AsNumber >= b.AsNumber ? 1f : 0f;
                    break;
            }
            }

            }
        }
Exemplo n.º 35
0
        void Term(out ScriptVar v)
        {
            v = new ScriptVar(); var callOffset = int.MaxValue; var isCall = false;
            if (la.kind == 1) {
            Get();
            var identName = t.val;
            if (la.kind == 5) {
                Get();
                ScriptVar p; isCall = true; if (!_isParsing) { callOffset = CallParams.Count; }
                if (StartOf(2)) {
                    Expr(out p);
                    if (!_isParsing) { CallParams.Add(p); }
                    while (la.kind == 6) {
                        Get();
                        Expr(out p);
                        if (!_isParsing) { CallParams.Add(p); }
                    }
                }
                Expect(7);
            }
            if (isCall) {
            if (!Vars.IsHostFunctionExists(identName)) {
                SemErr(string.Format("Cant find host function with name '{0}'", identName));
            }
            }
            if (!_isParsing) {
            if (isCall) {
                CallParamsOffset = callOffset;
                v = Vars.CallHostFunction(identName);
                while (CallParams.Count > callOffset) {
                    CallParams.RemoveAt(CallParams.Count - 1);
                }
            } else {
                if (!Vars.IsVarExists(t.val)) {
                    SemErr(string.Format("Variable '{0}' not found", identName));
                }
                v = Vars.GetVar(identName);
            }
            }

            } else if (la.kind == 2) {
            Get();
            if (!_isParsing) {
            v.AsNumber = t.val.ToFloatUnchecked();
            }

            } else if (la.kind == 3) {
            Get();
            if (!_isParsing) {
            v.AsString = t.val;
            }

            } else SynErr(35);
        }
Exemplo n.º 36
0
        public string CallFunction()
        {
            la = Scanner.EmptyToken;
            _isReturned = false;
            RetVal = new ScriptVar();
            try {
            Get();
            Block();
            } catch (Exception ex) {
            return ex.Message;
            }

            return null;
        }
Exemplo n.º 37
0
        void Expr1(out ScriptVar v)
        {
            ScriptVar b; int op;
            Expr2(out v);
            while (la.kind == 14 || la.kind == 15) {
            if (la.kind == 14) {
                Get();
                op = 0;
            } else {
                Get();
                op = 1;
            }
            Expr2(out b);
            if (!_isParsing) {
            switch (op) {
                case 0:
                    if (v.Type != b.Type) {
                        v.AsNumber = 0f;
                    } else {
                        v.AsNumber = v.IsNumber ? (v.AsNumber == b.AsNumber ? 1f : 0f) : (v.AsString == b.AsString ? 1f : 0f);
                    }
                    break;
                case 1:
                    if (v.Type != b.Type) {
                        v.AsNumber = 1f;
                    } else {
                        v.AsNumber = v.IsNumber ? (v.AsNumber != b.AsNumber ? 1f : 0f) : (v.AsString != b.AsString ? 1f : 0f);
                    }
                    break;
            }
            }

            }
        }
Exemplo n.º 38
0
        void Expr(out ScriptVar v)
        {
            ScriptVar b; int op;
            Expr1(out v);
            while (la.kind == 12 || la.kind == 13) {
            if (la.kind == 12) {
                Get();
                op = 0;
            } else {
                Get();
                op = 1;
            }
            Expr1(out b);
            if (!_isParsing) {
            if (!v.IsNumber || !b.IsNumber) {
                SemErr("'<' operator can be applied to numbers only");
            }
            switch (op) {
                case 0:
                    v.AsNumber = v.AsNumber != 0f || b.AsNumber != 0f ? 1f : 0f;
                    break;
                case 1:
                    v.AsNumber = v.AsNumber != 0f && b.AsNumber != 0f ? 1f : 0f;
                    break;
            }
            }

            }
        }