Exemplo n.º 1
0
 public void RepeatedArithmeticOperators()
 {
     // Making sure that we're properly parsing and generating all of these when there's multiples of the operator.
     runBasicTest(
         "x = 100\n" +
         "a = x + 2 + 3\n" +
         "b = x - 2 - 3\n" +
         "c = x * 2 * 3\n" +
         "d = x / 4 / 2\n" +
         "e = x % 9 % 3\n" +
         "f = x // 2 // 3\n" +
         "g = x ** 2 ** 3\n" +
         "h = x & 3 & 2\n" +
         "i = x | 13 | 1 \n" +
         "j = x ^ 2 ^ 1\n" +
         "k = x >> 2 >> 3\n" +
         "l = x << 2 << 3\n"
         , new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyInteger.Create(100 + 2 + 3) },
         { "b", PyInteger.Create(100 - 2 - 3) },
         { "c", PyInteger.Create(100 * 2 * 3) },
         { "d", PyFloat.Create(100.0 / 4.0 / 2.0) },
         { "e", PyInteger.Create(100 % 9 % 3) },
         { "f", PyInteger.Create(100 / 2 / 3) },
         { "g", PyInteger.Create((BigInteger)Math.Pow(100.0, 8.0)) },           // 2 ** 3 gets evaluated first and becomes 8. This is what CPython does too!
         { "h", PyInteger.Create(100 & 3 & 2) },
         { "i", PyInteger.Create(100 | 13 | 1) },
         { "j", PyInteger.Create(100 ^ 2 ^ 1) },
         { "k", PyInteger.Create(100 >> 2 >> 3) },
         { "l", PyInteger.Create(100 << 2 << 3) }
     }), 1);
 }
Exemplo n.º 2
0
 public void SimpleFloatMath()
 {
     runBasicTest("a = 10.0 * (2.0 + 4.0) / 3.0\n", new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyFloat.Create(20.0) }
     }), 1);
 }
Exemplo n.º 3
0
 public void ComprehensiveArithmeticOperators()
 {
     runBasicTest(
         "x = 10\n" +
         "a = x + 2\n" +
         "b = x - 2\n" +
         "c = x * 2\n" +
         "d = x / 2\n" +
         "e = x % 9\n" +
         "f = x // 3\n" +
         "g = x ** 2\n" +
         "h = x & 2\n" +
         "i = x | 14\n" +
         "j = x ^ 2\n" +
         "k = x >> 2\n" +
         "l = x << 2\n"
         , new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyInteger.Create(12) },
         { "b", PyInteger.Create(8) },
         { "c", PyInteger.Create(20) },
         { "d", PyFloat.Create(5.0) },
         { "e", PyInteger.Create(1) },
         { "f", PyInteger.Create(3) },
         { "g", PyInteger.Create(100) },
         { "h", PyInteger.Create(2) },
         { "i", PyInteger.Create(14) },
         { "j", PyInteger.Create(8) },
         { "k", PyInteger.Create(2) },
         { "l", PyInteger.Create(40) }
     }), 1);
 }
Exemplo n.º 4
0
 public async Task SimpleIntMath()
 {
     await runBasicTest("a = 10 * (2 + 4) / 3\n", new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyFloat.Create(20.0) }
     }), 1);
 }
Exemplo n.º 5
0
 public void AssignmentOperators()
 {
     // https://www.w3schools.com/python/python_operators.asp
     // +=	x += 3	x = x + 3
     // -=	x -= 3	x = x - 3
     // *=	x *= 3	x = x * 3
     // /=	x /= 3	x = x / 3
     // %=	x %= 3	x = x % 3
     // //=	x //= 3	x = x // 3
     // **=	x **= 3	x = x ** 3
     // &=	x &= 3	x = x & 3
     // |=	x |= 3	x = x | 3
     // ^=	x ^= 3	x = x ^ 3
     // >>=	x >>= 3	x = x >> 3
     // <<=	x <<= 3	x = x << 3
     runBasicTest(
         "a = 10\n" +
         "b = 10\n" +
         "c = 10\n" +
         "d = 10\n" +
         "e = 10\n" +
         "f = 10\n" +
         "g = 10\n" +
         "h = 10\n" +
         "i = 10\n" +
         "j = 10\n" +
         "k = 10\n" +
         "l = 10\n" +
         "a += 2\n" +
         "b -= 2\n" +
         "c *= 2\n" +
         "d /= 2\n" +
         "e %= 9\n" +
         "f //= 3\n" +
         "g **= 2\n" +
         "h &= 2\n" +
         "i |= 14\n" +
         "j ^= 2\n" +
         "k >>= 2\n" +
         "l <<= 2\n"
         , new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyInteger.Create(12) },
         { "b", PyInteger.Create(8) },
         { "c", PyInteger.Create(20) },
         { "d", PyFloat.Create(5.0) },
         { "e", PyInteger.Create(1) },
         { "f", PyInteger.Create(3) },
         { "g", PyInteger.Create(100) },
         { "h", PyInteger.Create(2) },
         { "i", PyInteger.Create(14) },
         { "j", PyInteger.Create(8) },
         { "k", PyInteger.Create(2) },
         { "l", PyInteger.Create(40) }
     }), 1);
 }
Exemplo n.º 6
0
        public static object CreateNumber(IParseTree context)
        {
            string rawText = context.GetText();

            if (DecimalPointNumberRegex.Match(rawText).Success)
            {
                return(PyFloat.Create(Decimal.Parse(rawText)));
            }
            return(PyInteger.Create(BigInteger.Parse(context.GetText())));
        }
Exemplo n.º 7
0
 public void BasicWait()
 {
     runBasicTest(
         "a = 10 * (2 + 4) / 3\n" +
         "wait\n" +
         "b = a + 3\n", new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyFloat.Create(20.0) },
         { "b", PyFloat.Create(23.0) }
     }), 2);
 }
Exemplo n.º 8
0
 public async Task NumericStringConversions()
 {
     await runBasicTest(
         "a_string = '1'\n" +
         "as_int = int(a_string)\n" +
         "as_float = float(a_string)\n" +
         "as_bool = bool(a_string)\n" +
         "int_str = str(1)\n" +
         "float_str = str(1.0)\n" +
         "bool_str = str(True)\n",
         new VariableMultimap(new TupleList <string, object>
     {
         { "as_int", PyInteger.Create(1) },
         { "as_float", PyFloat.Create(1.0) },
         { "as_bool", PyBool.True },
         { "int_str", PyString.Create("1") },
         { "float_str", PyString.Create("1.0") },
         { "bool_str", PyString.Create("True") },
     }), 1);
 }