public void TestMethodTable()
        {
            var expect = new NovaArray {
                200, 5, 23
            };
            var engine     = GetRuntime().GetEngine("Nova");
            var novascope1 = engine.CreateScope();

            var 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(novascope1);

            var test1 = novascope1.GetVariable("test1");
            var test2 = novascope1.GetVariable("test2");
            var test3 = novascope1.GetVariable("test3");

            var table = new NovaMethodTable("test");

            table.AddFunction(test1);
            table.AddFunction(test2);
            table.AddFunction(test3);
            var novascope2 = engine.CreateScope();

            novascope2.SetVariable("test", table);
            var source2 = engine.CreateScriptSourceFromString("[test(10),test(10,2),test(10,20,7)];");
            var real    = source2.Execute(novascope2);

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestMethodTableNameMatch()
        {
            var expect = new NovaArray {
                6, 18
            };
            var engine     = GetRuntime().GetEngine("Nova");
            var novascope1 = engine.CreateScope();

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

            source1.Execute(novascope1);

            var test1 = novascope1.GetVariable("test1");
            var test2 = novascope1.GetVariable("test2");

            var table = new NovaMethodTable("test");

            table.AddFunction(test1);
            table.AddFunction(test2);
            var novascope2 = engine.CreateScope();

            novascope2.SetVariable("test", table);
            var source2 = engine.CreateScriptSourceFromString("[test(4,y:3,5),test(a:7,6,5)];");
            var real    = source2.Execute(novascope2);

            Assert.That(real, Is.EqualTo(expect));
        }
Example #3
0
        public void TestSingleton()
        {
            var expect = new NovaArray {
                null, 5
            };
            var engine = GetRuntime().GetEngine("Nova");
            var source = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            var testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)), new List <NovaFunction>(),
                                          new List <NovaFunction> {
                scope.GetVariable("test")
            });

            var nscope = engine.CreateScope();

            nscope.SetVariable("Test", testClass);

            var 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));
        }
        public void TestMethodTableTwo()
        {
            var expect = new NovaArray {
                200, 400, 60
            };
            var engine     = GetRuntime().GetEngine("Nova");
            var novascope1 = engine.CreateScope();

            var 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(novascope1);

            var test1 = novascope1.GetVariable("test1");
            var test2 = novascope1.GetVariable("test2");
            var test3 = novascope1.GetVariable("test3");

            var table = new NovaMethodTable("test");

            table.AddFunction(test1);
            table.AddFunction(test2);
            table.AddFunction(test3);
            var novascope2 = engine.CreateScope();

            novascope2.SetVariable("test", table);
            var source2 = engine.CreateScriptSourceFromString("[test(),test(400,20),test(10,5,10,15,20,25)];");
            var real    = source2.Execute(novascope2);

            Assert.That(real, Is.EqualTo(expect));
        }
Example #5
0
        public void TestArrayAdd()
        {
            var expect = new NovaArray {
                "hello"
            };

            Assert.That(CompileAndExecute("a = []; a << 'hello'; a;"), Is.EqualTo(expect));
        }
        public void TestParallelAssignmentSimple()
        {
            var expect = new NovaArray {
                1, 2
            };
            var real = CompileAndExecute("pa,pb=1,2;");

            Assert.That(real, Is.EqualTo(expect));
        }
Example #7
0
        public void TestAccessPostIncrement()
        {
            var expect = new NovaArray {
                2, 3
            };
            var real = CompileAndExecute("x = [1,2,3]; v=x[1]++;[v, x[1]];");

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestParallelAssignmentWildcardOnlyRhs()
        {
            var expect = new NovaArray {
                1, 2, 3
            };
            var real = CompileAndExecute("pb=[1,2,3]; pa,pb,pc = *pb;");

            Assert.That(real, Is.EqualTo(expect));
        }
Example #9
0
        public void TestInstanceSingletonRemoveMethod()
        {
            var expect = new NovaArray {
                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));
        }
        public void TestParallelAssignmentWildcardRhsInMiddle()
        {
            var expect = new NovaArray {
                1, 2, 3, 4
            };
            var real = CompileAndExecute("pb=[2,3];pa,pb,pc,pd = 1,*pb,4;");

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestParallelAssignmentEvalAll()
        {
            var expect = new NovaArray {
                0, 1, 2
            };
            var real = CompileAndExecute("px = 0; pa,pb,pc=px,++px,++px;");

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestNestedAssignment()
        {
            var expect = new NovaArray {
                5, 5
            };

            var real = CompileAndExecute("x = y = 5; [x,y];");

            Assert.That(real, Is.EqualTo(expect));
        }
Example #13
0
        public void TestArrayAssignment()
        {
            var expect = new NovaArray {
                1, 6, 3
            };

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

            Assert.That(real, Is.EqualTo(expect));
        }
Example #14
0
        public void TestSimpleArray()
        {
            var expect = new NovaArray {
                1, "hello", 2.0
            };

            var real = CompileAndExecute("x = [1, 'hello', 2.0];");

            Assert.That(real, Is.EqualTo(expect));
        }
Example #15
0
        public void TestArrayDynamicReassignment()
        {
            var expect = new NovaArray {
                1, "hello", 3
            };

            var real = CompileAndExecute("x = [1, 2, 3]; x[1] = 'hello'; x;");

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestPartialFunctionCall()
        {
            var expect = new NovaArray {
                2, 3, 4
            };

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

            Assert.That(real, Is.EqualTo(expect));
        }
Example #17
0
        public void TestRegex2()
        {
            var expect = new NovaArray {
                "ell", "orl"
            };

            Assert.That(
                CompileAndExecute("x = 'Hello World!'; x =~ %/[^e]+(?<test>el+).+W(?<word>o.+)d/; [test,word];"),
                Is.EqualTo(expect));
        }
Example #18
0
        public void TestSimpleArrayTwo()
        {
            var expect = new NovaArray {
                1, 2, 3
            };

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

            Assert.That(real, Is.EqualTo(expect));
        }
Example #19
0
        public void TestUnaryCustomOp()
        {
            var expect = new NovaArray {
                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));
        }
Example #20
0
        public void TestHashSymbolDifferent()
        {
            var expect = new NovaArray {
                "world", "universe"
            };

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

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestParallelAssignmentWildcardLhsAtEnd()
        {
            var expect = new NovaArray {
                1, 2, new NovaArray {
                    3, 4
                }
            };
            var real = CompileAndExecute("pa,pb,*pc=1,2,3,4");

            Assert.That(real, Is.EqualTo(expect));
        }
Example #22
0
        public void TestSubModule()
        {
            var expect = new NovaArray {
                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));
        }
        public void TestParallelAssigmentWildcardLhsInMiddle()
        {
            var expect = new NovaArray {
                1, new NovaArray {
                    2, 3
                }, null
            };
            var real = CompileAndExecute("pa,*pb,pc=1,2,3");

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestNestedParallelAssignmentTwo()
        {
            var expect = new NovaArray {
                1, new NovaArray {
                    2, null
                }, 3
            };
            var real = CompileAndExecute("pa,(pb,pc),pd=1,2,3;");

            Assert.That(real, Is.EqualTo(expect));
        }
        public void TestPartialFunctionCallBackward()
        {
            var expect = new NovaArray {
                10, 4, 2
            };

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

            Assert.That(real, Is.EqualTo(expect));
        }
Example #26
0
        public void TestNestedArray()
        {
            var expect = new NovaArray {
                new NovaArray {
                    1, 2, 3
                },
                new NovaArray {
                    4, 5, 6
                },
                new NovaArray {
                    7, 8, 9
                }
            };

            var real = CompileAndExecute("x = [[1,2,3],[4,5,6],[7,8,9]];");

            Assert.That(real, Is.EqualTo(expect));
        }
Example #27
0
        public void TestNestedArrayAssignment()
        {
            var expect = new NovaArray {
                new NovaArray {
                    1, 6, 3
                },
                new NovaArray {
                    4, 10, 6
                },
                new NovaArray {
                    7, 14, 9
                }
            };

            var 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 TestMethodTableNativeFunction()
        {
            var expect = new NovaArray {
                17, 37
            };
            var engine = GetRuntime().GetEngine("Nova");
            var scope  = engine.CreateScope();

            var table = new NovaMethodTable("test");

            table.AddFunction(new NovaNativeFunction(typeof(NativeHelper),
                                                     typeof(NativeHelper).GetMethod("MethodTableTest", new[] { typeof(int) })));
            table.AddFunction(new NovaNativeFunction(typeof(NativeHelper),
                                                     typeof(NativeHelper).GetMethod("MethodTableTest", new[] { typeof(string) })));

            scope.SetVariable("test", table);
            var source = engine.CreateScriptSourceFromString("[test('hello'),test(27)];");
            var real   = source.Execute(scope);

            Assert.That(real, Is.EqualTo(expect));
        }
 private dynamic ConvertElements(NovaArray res)
 {
     for (var i = 0; i < res.Count(); i++)
     {
         if (res[i] is NovaString)
         {
             res[i] = (string)res[i];
         }
         if (res[i] is NovaNumber)
         {
             res[i] = NovaNumber.Convert(res[i]);
         }
         if (res[i] is NovaArray)
         {
             res[i] = ConvertElements((NovaArray)res[i]);
         }
         if (res[i] is NovaDictionary)
         {
             res[i] = ConvertElements((NovaDictionary)res[i]);
         }
     }
     return(res);
 }
        internal static dynamic ParallelAssign(
            List <ParallelAssignmentExpression.ParallelAssignmentInfo> leftHandValues,
            List <ParallelAssignmentExpression.ParallelAssignmentInfo> rightHandValues, object rawScope)
        {
            var scope   = rawScope as NovaScope;
            var rvalues = new List <object>();
            var 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 (var rvalue in rightHandValues)
                {
                    var 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);
                    }
                }
            }

            var i      = 0;
            var k      = 0;
            var result = new NovaArray();

            foreach (var _lvalue in leftHandValues)
            {
                var 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 NovaArray();
                    for (var 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 (var j = k; j < leftHandValues.Count; j++)
                {
                    if (leftHandValues[j].Value is List <ParallelAssignmentExpression.ParallelAssignmentInfo> )
                    {
                        var lvalues =
                            leftHandValues[j].Value as List <ParallelAssignmentExpression.ParallelAssignmentInfo>;
                        for (var 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]);
        }