Exemplo n.º 1
0
 public void TestAccessPostIncrement()
 {
     var expect = new SilverArray {2, 3};
     dynamic real = CompileAndExecute("x = [1,2,3]; v=x[1]++;[v, x[1]];");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 2
0
 public void TestNestedParallelAssignmentTwo()
 {
     var expect = new SilverArray {1, new SilverArray {2, null}, 3};
     dynamic real = CompileAndExecute("pa,(pb,pc),pd=1,2,3;");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 3
0
        public void TestNestedArray()
        {
            var expect = new SilverArray {
                new SilverArray {1, 2, 3},
                new SilverArray {4, 5, 6},
                new SilverArray {7, 8, 9}
            };

            dynamic real = CompileAndExecute("x = [[1,2,3],[4,5,6],[7,8,9]];");
            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 4
0
        public void TestSimpleArray()
        {
            var expect = new SilverArray {1, "hello", 2.0};

            dynamic real = CompileAndExecute("x = [1, 'hello', 2.0];");
            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 5
0
        public void TestPartialFunctionCallForward()
        {
            var expect = new SilverArray {2, 3, 4};

            dynamic real =
                CompileAndExecute("def add(x,y) { return x + y; }; add1 = add(1); [1 |> add1,2 |> add1,3 |> add1];");

            Assert.That(real, Is.EqualTo(expect));
        }
        internal static dynamic ParallelAssign(
            List<ParallelAssignmentExpression.ParallelAssignmentInfo> leftHandValues,
            List<ParallelAssignmentExpression.ParallelAssignmentInfo> rightHandValues, object rawScope)
        {
            var scope = rawScope as SilverScope;
            var rvalues = new List<object>();
            dynamic fval = CompilerServices.CompileExpression(rightHandValues[0].Value as Expression, scope);

            if (fval is List<object> && !rightHandValues[0].IsWildcard) {
                rvalues = new List<object>(fval as List<object>);
            }
            else {
                foreach (ParallelAssignmentExpression.ParallelAssignmentInfo rvalue in rightHandValues) {
                    dynamic val = CompilerServices.CompileExpression(rvalue.Value as Expression, scope);
                    if (rvalue.IsWildcard) {
                        if (val is List<object>) {
                            (val as List<object>).ForEach(value => rvalues.Add(value));
                        }
                        else {
                            rvalues.Add(val);
                        }
                    }
                    else {
                        rvalues.Add(val);
                    }
                }
            }

            int i = 0;
            int k = 0;
            var result = new SilverArray();

            foreach (ParallelAssignmentExpression.ParallelAssignmentInfo _lvalue in leftHandValues) {
                dynamic lvalue = _lvalue.Value;
                if (i >= rvalues.Count) {
                    break;
                }
                k++;
                if (lvalue is List<ParallelAssignmentExpression.ParallelAssignmentInfo>) {
                    result.Add(ParallelAssign(lvalue as List<ParallelAssignmentExpression.ParallelAssignmentInfo>,
                        new List<ParallelAssignmentExpression.ParallelAssignmentInfo> {
                            new ParallelAssignmentExpression.ParallelAssignmentInfo {
                                IsWildcard = false,
                                Value = Expression.Constant(rvalues[i++])
                            }
                        }, scope));
                }
                else if (_lvalue.IsWildcard) {
                    var mvalues = new SilverArray();
                    for (int j = i; j < rvalues.Count; j++) {
                        mvalues.Add(rvalues[j]);
                    }
                    result.Add(Assign(lvalue as VariableExpression, mvalues, E.Assign, false, rawScope));
                    break;
                }
                else {
                    result.Add(Assign(lvalue as VariableExpression, rvalues[i++], E.Assign, false, rawScope));
                }
            }

            if (k < leftHandValues.Count) {
                for (int j = k; j < leftHandValues.Count; j++) {
                    if (leftHandValues[j].Value is List<ParallelAssignmentExpression.ParallelAssignmentInfo>) {
                        var lvalues =
                            leftHandValues[j].Value as List<ParallelAssignmentExpression.ParallelAssignmentInfo>;
                        for (int l = 0; l < lvalues.Count; l++) {
                            result.Add(Assign(lvalues[l].Value as VariableExpression, null, E.Assign, false, scope));
                        }
                    }
                    else {
                        result.Add(Assign(leftHandValues[j].Value as VariableExpression, null, E.Assign, false, scope));
                    }
                }
            }

            return result.Count > 1 ? result : result[0];
        }
Exemplo n.º 7
0
 public void TestParallelAssignmentWildcardRhsInMiddle()
 {
     var expect = new SilverArray {1, 2, 3, 4};
     dynamic real = CompileAndExecute("pb=[2,3];pa,pb,pc,pd = 1,*pb,4;");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 8
0
        public void TestMethodTableTwo()
        {
            var expect = new SilverArray {200, 400, 60};
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptScope silverscope1 = engine.CreateScope();

            ScriptSource source1 =
                engine.CreateScriptSourceFromString(
                    "def test1(x=10) { return x * 20; }; def test2(x,z,y=20) { return (x/y)*z; }; def test3(a,b,*c) { res = a; for(z in c) { res += (z-b); }; res; };");
            source1.Execute(silverscope1);

            dynamic test1 = silverscope1.GetVariable("test1");
            dynamic test2 = silverscope1.GetVariable("test2");
            dynamic test3 = silverscope1.GetVariable("test3");

            var table = new SilverMethodTable("test");
            table.AddFunction(test1);
            table.AddFunction(test2);
            table.AddFunction(test3);
            ScriptScope silverscope2 = engine.CreateScope();
            silverscope2.SetVariable("test", table);
            ScriptSource source2 = engine.CreateScriptSourceFromString("[test(),test(400,20),test(10,5,10,15,20,25)];");
            dynamic real = source2.Execute(silverscope2);

            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 9
0
 public void TestParallelAssignmentWildcardLhsAtEnd()
 {
     var expect = new SilverArray {1, 2, new SilverArray {3, 4}};
     dynamic real = CompileAndExecute("pa,pb,*pc=1,2,3,4");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 10
0
 public void TestParallelAssignmentWildcardOnlyRhs()
 {
     var expect = new SilverArray {1, 2, 3};
     dynamic real = CompileAndExecute("pb=[1,2,3]; pa,pb,pc = *pb;");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 11
0
 public void TestParallelAssignmentSimple()
 {
     var expect = new SilverArray {1, 2};
     dynamic real = CompileAndExecute("pa,pb=1,2;");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 12
0
 public void TestParallelAssignmentEvalAll()
 {
     var expect = new SilverArray {0, 1, 2};
     dynamic real = CompileAndExecute("px = 0; pa,pb,pc=px,++px,++px;");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 13
0
 public void TestParallelAssigmentWildcardLhsInMiddle()
 {
     var expect = new SilverArray {1, new SilverArray {2, 3}, null};
     dynamic real = CompileAndExecute("pa,*pb,pc=1,2,3");
     Assert.That(real, Is.EqualTo(expect));
 }
Exemplo n.º 14
0
        public void TestMethodTableNameMatch()
        {
            var expect = new SilverArray {6, 18};
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptScope silverscope1 = engine.CreateScope();

            ScriptSource source1 =
                engine.CreateScriptSourceFromString(
                    "def test1(x,y,z) { return (x%y)+z; }; def test2(a,b,c) { return a+b+c; }");
            source1.Execute(silverscope1);

            dynamic test1 = silverscope1.GetVariable("test1");
            dynamic test2 = silverscope1.GetVariable("test2");

            var table = new SilverMethodTable("test");
            table.AddFunction(test1);
            table.AddFunction(test2);
            ScriptScope silverscope2 = engine.CreateScope();
            silverscope2.SetVariable("test", table);
            ScriptSource source2 = engine.CreateScriptSourceFromString("[test(4,y:3,5),test(a:7,6,5)];");
            dynamic real = source2.Execute(silverscope2);

            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 15
0
        public void TestInstanceSingletonRemoveMethod()
        {
            var expect = new SilverArray {25, null};

            Assert.That(
                CompileAndExecute("class RemoveMethodSingletonSuperTest { def test { return nil; }; }; class RemoveMethodSingletonSubTest < RemoveMethodSingletonSuperTest { def test { return 25; }; }; x = RemoveMethodSingletonSubTest(); y = RemoveMethodSingletonSubTest(); y.remove_method :test; [x.test, y.test];"), Is.EqualTo(expect));
        }
Exemplo n.º 16
0
        public void TestMethodTableNativeFunction()
        {
            var expect = new SilverArray {17, 37};
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptScope scope = engine.CreateScope();

            var table = new SilverMethodTable("test");
            table.AddFunction(new SilverNativeFunction(typeof (NativeHelper),
                typeof (NativeHelper).GetMethod("MethodTableTest", new[] {typeof (int)})));
            table.AddFunction(new SilverNativeFunction(typeof (NativeHelper),
                typeof (NativeHelper).GetMethod("MethodTableTest", new[] {typeof (string)})));

            scope.SetVariable("test", table);
            ScriptSource source = engine.CreateScriptSourceFromString("[test('hello'),test(27)];");
            dynamic real = source.Execute(scope);
            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 17
0
        public void TestArrayAdd()
        {
            var expect = new SilverArray {"hello"};

            Assert.That(CompileAndExecute("a = []; a << 'hello'; a;"), Is.EqualTo(expect));
        }
Exemplo n.º 18
0
        public void TestPartialFunctionCallBackward()
        {
            var expect = new SilverArray {10, 4, 2};

            dynamic real =
                CompileAndExecute(
                    "def div(x,y) { return y / x; }; div20 = div(20); [div20 <| 2,div20 <| 5,div20 <| 10];");

            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 19
0
        public void TestSingleton()
        {
            var expect = new SilverArray {null, 5};
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            var testClass = new SilverClass("Test", Silver.Box(typeof (object)), new List<SilverFunction>(),
                new List<SilverFunction> {scope.GetVariable("test")});

            ScriptScope nscope = engine.CreateScope();
            nscope.SetVariable("Test", testClass);

            ScriptSource nsource =
                engine.CreateScriptSourceFromString(
                    "x = Test();  y = Test(); def x.single(y) { return y - 5; }; [y.single(10),x.single(10)]");
            Assert.That(nsource.Execute(nscope), Is.EqualTo(expect));
        }
Exemplo n.º 20
0
 private dynamic ConvertElements(SilverArray res)
 {
     for (int i = 0; i < res.Count(); i++)
     {
         if (res[i] is SilverString)
         {
             res[i] = (string)res[i];
         }
         if (res[i] is SilverNumber)
         {
             res[i] = SilverNumber.Convert(res[i]);
         }
         if (res[i] is SilverArray)
         {
             res[i] = ConvertElements((SilverArray)res[i]);
         }
         if (res[i] is SilverDictionary)
         {
             res[i] = ConvertElements((SilverDictionary)res[i]);
         }
     }
     return res;
 }
Exemplo n.º 21
0
 public void TestSubModule()
 {
     var expect = new SilverArray {25, 10};
     Assert.That(
             CompileAndExecute(
                 "module TestModule2 { class ModuleClass2 { def testFunc(x) { return x * 5; }; }; class ModuleClass3 { def testFunc2(x) { return x/5; }; }; }; class ModuleTestClass2 { include TestModule2::ModuleClass2;  def new() { tmp = ModuleClass2(); @x = tmp.testFunc(5); @y = 0; begin { @y = ModuleClass3(); } rescue Exception => ex { @y = 10; }; }; }; x = ModuleTestClass2(); [x.x, x.y];"),
         Is.EqualTo(expect));
 }
Exemplo n.º 22
0
        public void TestHashSymbolDifferent()
        {
            var expect = new SilverArray {"world", "universe"};

            dynamic real =
                CompileAndExecute("x = { :hello => 'world', 'hello' => 'universe' }; [x[:hello], x['hello']];");

            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 23
0
 public void TestUnaryCustomOp()
 {
     var expect = new SilverArray {490, 10};
     Assert.That(
         CompileAndExecute(
             "class sv_custom_op_test { def new(x) { @x = x; }; def =!= { if (__postfix) { return @x * 7; } else { return @x / 7 }; }; }; x = sv_custom_op_test(70); [x =!=, =!= x];"),
         Is.EqualTo(expect));
 }
Exemplo n.º 24
0
        public void TestNestedArrayAssignment()
        {
            var expect = new SilverArray {
                new SilverArray {1, 6, 3},
                new SilverArray {4, 10, 6},
                new SilverArray {7, 14, 9}
            };

            dynamic real =
                CompileAndExecute("x = [[1,2,3],[4,5,6],[7,8,9]]; x[0][1] = 6; x[1][1] = 10; x[2][1] = 14; x;");
            Assert.That(real, Is.EqualTo(expect));
        }
 public void TestRegex2()
 {
     var expect = new SilverArray {"ell", "orl"};
     Assert.That(
         CompileAndExecute("x = 'Hello World!'; x =~ %/[^e]+(?<test>el+).+W(?<word>o.+)d/; [test,word];"),
         Is.EqualTo(expect));
 }
Exemplo n.º 26
0
        public void TestSimpleArrayTwo()
        {
            var expect = new SilverArray {1, 2, 3};

            dynamic real = CompileAndExecute("x = [1, 2, 3];");

            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 27
0
        public void TestMethodTable()
        {
            var expect = new SilverArray {200, 5, 23};
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptScope silverscope1 = engine.CreateScope();

            ScriptSource source1 =
                engine.CreateScriptSourceFromString(
                    "def test1(x) { return x * 20; }; def test2(x,y) { return x/y; }; def test3(a,b,c) { return a+(b-c); };");
            source1.Execute(silverscope1);

            dynamic test1 = silverscope1.GetVariable("test1");
            dynamic test2 = silverscope1.GetVariable("test2");
            dynamic test3 = silverscope1.GetVariable("test3");

            var table = new SilverMethodTable("test");
            table.AddFunction(test1);
            table.AddFunction(test2);
            table.AddFunction(test3);
            ScriptScope silverscope2 = engine.CreateScope();
            silverscope2.SetVariable("test", table);
            ScriptSource source2 = engine.CreateScriptSourceFromString("[test(10),test(10,2),test(10,20,7)];");
            dynamic real = source2.Execute(silverscope2);

            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 28
0
        public void TestArrayAssignment()
        {
            var expect = new SilverArray {1, 6, 3};

            dynamic real = CompileAndExecute("x = [1, 2, 3]; x[1] = 6; x;");
            Assert.That(real, Is.EqualTo(expect));
        }
Exemplo n.º 29
0
        public void TestNestedAssignment()
        {
            var expect = new SilverArray {5, 5};

            dynamic real = CompileAndExecute("x = y = 5; [x,y];");
            Assert.That(real, Is.EqualTo(expect));
        }