Esempio n. 1
0
        public IInterpreter InheritingEnums()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true,
                    DebugThrowOnError      = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.CreateEnum("Weekend")
                                   .With(EnumCaseBuilder.Create("Sat", "Sun"))
                                   .SetModifier(EntityModifier.Base));

                root_ns.AddBuilder(TypeBuilder.CreateEnum("First")
                                   .With(EnumCaseBuilder.Create("Mon"))
                                   .Parents("Weekend"));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.NatNameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    // let a Weekend = First.Sat
                    VariableDeclaration.CreateStatement("a", NameReference.Create("Weekend"),
                                                        // please note we only refer to "Sat" through "First", the type is still "Weekend"
                                                        NameReference.Create("First", "Sat")),
                    // var b First = Weekend.Sun
                    VariableDeclaration.CreateStatement("b", NameReference.Create("First"), NameReference.Create("Weekend", "Sun"),
                                                        env.Options.ReassignableModifier()),
                    // b = First.Mon
                    Assignment.CreateStatement(NameReference.Create("b"), NameReference.Create("First", "Mon")),
                    // let x = a to Nat; // 0
                    VariableDeclaration.CreateStatement("x", null, FunctionCall.ConvCall(NameReference.Create("a"),
                                                                                         NameFactory.NatNameReference())),
                    // let y = b to Nat; // 2
                    VariableDeclaration.CreateStatement("y", null, FunctionCall.ConvCall(NameReference.Create("b"),
                                                                                         NameFactory.NatNameReference())),
                    // return x + y
                    Return.Create(ExpressionFactory.Add(NameReference.Create("x"), NameReference.Create("y")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2UL, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 2
0
        public IInterpreter VirtualCall()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true,
                    DebugThrowOnError      = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("MyBase")
                                   .SetModifier(EntityModifier.Base)
                                   .With(FunctionBuilder.Create(
                                             "bar",
                                             ExpressionReadMode.ReadRequired,
                                             NameFactory.Int64NameReference(),
                                             Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("33"))
                }))
                                         .SetModifier(EntityModifier.Base)));

                TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("SomeChild")
                                                              .With(FunctionBuilder.Create("bar",
                                                                                           ExpressionReadMode.ReadRequired,
                                                                                           NameFactory.Int64NameReference(),
                                                                                           Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))
                                                                    .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase))
                                                              .Parents(NameReference.Create("MyBase")));

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("i", NameFactory.PointerNameReference(NameReference.Create("MyBase")),
                                                        ExpressionFactory.HeapConstructor(NameReference.Create("SomeChild"))),
                    Return.Create(FunctionCall.Create(NameReference.Create("i", "bar")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 3
0
        public IInterpreter OverridingMethodWithIndexerGetter()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true,
                    DebugThrowOnError      = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.CreateInterface("IProvider")
                                   .With(FunctionBuilder.CreateDeclaration(NameFactory.PropertyIndexerName, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())
                                         .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference()))));

                root_ns.AddBuilder(TypeBuilder.Create("Middle")
                                   .Parents("IProvider")
                                   .SetModifier(EntityModifier.Base)
                                   .With(FunctionBuilder.Create(NameFactory.PropertyIndexerName, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(Return.Create(Int64Literal.Create("500"))))
                                         .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase)
                                         .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead))));

                root_ns.AddBuilder(TypeBuilder.Create("Last")
                                   .Parents("Middle")
                                   .SetModifier(EntityModifier.Base)
                                   .With(PropertyBuilder.CreateIndexer(env.Options, NameFactory.Int64NameReference())
                                         .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead))
                                         .With(PropertyMemberBuilder.CreateIndexerGetter(Block.CreateStatement(Return.Create(Int64Literal.Create("2"))))
                                               .Modifier(EntityModifier.Override | EntityModifier.UnchainBase))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", NameFactory.PointerNameReference("IProvider"),
                                                        ExpressionFactory.HeapConstructor("Last")),
                    Return.Create(FunctionCall.Create(NameReference.Create("p", NameFactory.PropertyIndexerName),
                                                      FunctionArgument.Create(Int64Literal.Create("18"))))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 4
0
        public IInterpreter NoExtrasWithCopyConstructor()
        {
            // nothing is written in stone, but for now let's treat assignment in declaration as assignment
            // not copy constructor (as in C++)
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement()))
                                   // copy-constructor
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(
                                                                                   Assignment.CreateStatement(NameReference.CreateThised("x"), Nat8Literal.Create("66"))
                                                                                   )).Parameters(FunctionParameter.Create("p", NameFactory.ReferenceNameReference("Point"),
                                                                                                                          ExpressionReadMode.CannotBeRead)))

                                   .With(PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference())
                                         .With(VariableDeclaration.CreateStatement("f", NameFactory.Nat8NameReference(), null,
                                                                                   env.Options.ReassignableModifier()))
                                         .WithGetter(Block.CreateStatement(Return.Create(NameReference.CreateThised("f"))))
                                         .WithSetter(Block.CreateStatement(Assignment.CreateStatement(NameReference.CreateThised("f"),
                                                                                                      ExpressionFactory.Mul(NameFactory.PropertySetterValueReference(), Nat8Literal.Create("2")))))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", null,
                                                        ConstructorCall.StackConstructor(NameReference.Create("Point"))
                                                        .Init("x", Nat8Literal.Create("7"))
                                                        .Build()),
                    // bit-copy of the object, there is no calling copy-constructor here
                    VariableDeclaration.CreateStatement("r", null, NameReference.Create("p")),
                    Return.Create(NameReference.Create(NameReference.Create("r"), "x"))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)14, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 5
0
        public IInterpreter CheckingHostTraitRuntimeType()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DebugThrowOnError      = true,
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.CreateInterface("ISay")
                                   .With(FunctionBuilder.CreateDeclaration("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())));


                root_ns.AddBuilder(TypeBuilder.Create("NoSay"));

                root_ns.AddBuilder(TypeBuilder.Create("Greeter", "T"));

                root_ns.AddBuilder(TypeBuilder.Create("Greeter", "X")
                                   .Constraints(ConstraintBuilder.Create("X").Inherits("ISay"))
                                   .SetModifier(EntityModifier.Trait)
                                   .Parents("ISay")
                                   .With(FunctionBuilder.Create("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(
                                                                    Return.Create(Int64Literal.Create("2"))
                                                                    )).SetModifier(EntityModifier.Override)));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(
                                           // just plain host, no trait is used
                                           VariableDeclaration.CreateStatement("g", NameFactory.PointerNameReference(NameFactory.IObjectNameReference()),
                                                                               ExpressionFactory.HeapConstructor(NameReference.Create("Greeter", NameReference.Create("NoSay")))),
                                           // we should have fail-test here
                                           Return.Create(ExpressionFactory.Ternary(IsType.Create(NameReference.Create("g"), NameReference.Create("ISay")),
                                                                                   Int64Literal.Create("99"), Int64Literal.Create("2")))
                                           )));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 6
0
        public IInterpreter TypeUnion()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowProtocols         = true,
                    AllowInvalidMainResult = true,
                    DebugThrowOnError      = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("GetPos")
                                   .With(FunctionBuilder.Create("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("3"))
                }))));

                root_ns.AddBuilder(TypeBuilder.Create("GetNeg")
                                   .With(FunctionBuilder.Create("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("-1"))
                }))));

                NameReferenceUnion union = NameReferenceUnion.Create(NameFactory.PointerNameReference(NameReference.Create("GetNeg")),
                                                                     NameFactory.PointerNameReference(NameReference.Create("GetPos")));
                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("a", union, Undef.Create(), env.Options.ReassignableModifier()),
                    VariableDeclaration.CreateStatement("b", union, Undef.Create(), env.Options.ReassignableModifier()),
                    Assignment.CreateStatement(NameReference.Create("a"), ExpressionFactory.HeapConstructor("GetPos")),
                    Assignment.CreateStatement(NameReference.Create("b"), ExpressionFactory.HeapConstructor("GetNeg")),
                    VariableDeclaration.CreateStatement("x", null, FunctionCall.Create(NameReference.Create("a", "getSome"))),
                    VariableDeclaration.CreateStatement("y", null, FunctionCall.Create(NameReference.Create("b", "getSome"))),
                    Return.Create(ExpressionFactory.Add(NameReference.Create("x"), NameReference.Create("y")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 7
0
        public IInterpreter HasConstraintWithValue()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true,
                    AllowProtocols         = true,
                    DebugThrowOnError      = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe",
                                                                                       ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference());
                root_ns.AddBuilder(FunctionBuilder.Create("proxy",
                                                          TemplateParametersBuffer.Create().Add("T").Values,
                                                          ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] {
                    Return.Create(FunctionCall.Create(NameReference.Create("t", "getMe")))
                }))
                                   .Constraints(ConstraintBuilder.Create("T").Has(func_constraint))
                                   .Parameters(FunctionParameter.Create("t", NameReference.Create("T"))));

                TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("Y")
                                                              .With(FunctionBuilder.Create("getMe",
                                                                                           ExpressionReadMode.ReadRequired,
                                                                                           NameFactory.Int64NameReference(),
                                                                                           Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))));

                FunctionCall call = FunctionCall.Create(NameReference.Create("proxy"), FunctionArgument.Create(NameReference.Create("y")));
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("y", null, ExpressionFactory.StackConstructor(NameReference.Create("Y"))),
                    Return.Create(call)
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 8
0
        public IInterpreter OldSchoolSwapValuesViaPointers()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DebugThrowOnError = true, AllowDereference = true
                }
                                             .SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(FunctionBuilder.Create("swap", "T", VarianceMode.None,
                                                          NameFactory.UnitNameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("t", NameReference.Create("T"), NameReference.Create("a")),
                                                              Assignment.CreateStatement(Dereference.Create(NameReference.Create("a")), NameReference.Create("b")),
                                                              Assignment.CreateStatement(Dereference.Create(NameReference.Create("b")), NameReference.Create("t"))
                                                              ))
                                   .Constraints(ConstraintBuilder.Create("T")
                                                .SetModifier(env.Options.ReassignableModifier()))
                                   .Parameters(FunctionParameter.Create("a", NameFactory.ReferenceNameReference("T")),
                                               FunctionParameter.Create("b", NameFactory.ReferenceNameReference("T"))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(
                                           VariableDeclaration.CreateStatement("a", null,
                                                                               ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(env.Options.ReassignableTypeMutability()),
                                                                                                                 Nat8Literal.Create("2"))),
                                           VariableDeclaration.CreateStatement("b", null,
                                                                               ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(env.Options.ReassignableTypeMutability()),
                                                                                                                 Nat8Literal.Create("17"))),
                                           FunctionCall.Create("swap", NameReference.Create("a"), NameReference.Create("b")),
                                           Return.Create(ExpressionFactory.Sub("a", "b"))
                                           )));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)15, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 9
0
        public IInterpreter DirectRefCountings()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DebugThrowOnError = true, AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var inc_def = root_ns.AddBuilder(FunctionBuilder.Create(
                                                     "inc",
                                                     ExpressionReadMode.ReadRequired,
                                                     NameFactory.Int64NameReference(),
                                                     Block.CreateStatement(new IExpression[] {
                    // let temp *Int = n; // n is argument
                    VariableDeclaration.CreateStatement("temp", NameFactory.PointerNameReference(NameFactory.Int64NameReference()),
                                                        NameReference.Create("n")),
                    // return temp + 1;
                    Return.Create(FunctionCall.Create(NameReference.Create(NameReference.Create("temp"), NameFactory.AddOperator),
                                                      FunctionArgument.Create(Int64Literal.Create("1"))))
                }))
                                                 .Parameters(FunctionParameter.Create("n", NameFactory.PointerNameReference(NameFactory.Int64NameReference()))));
                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    // let p_int *Int = new *1;
                    VariableDeclaration.CreateStatement("p_int", NameFactory.PointerNameReference(NameFactory.Int64NameReference()),
                                                        ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), FunctionArgument.Create(Int64Literal.Create("1")))),
                    // let proxy *Int = p_int;
                    VariableDeclaration.CreateStatement("proxy", NameFactory.PointerNameReference(NameFactory.Int64NameReference()), NameReference.Create("p_int")),
                    // return inc(proxy);
                    Return.Create(FunctionCall.Create(NameReference.Create("inc"), FunctionArgument.Create(NameReference.Create("proxy")))),
                })));


                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 10
0
        public IInterpreter ImplicitClosureWithValue()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Beep")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(VariableDeclaration.CreateStatement("m", NameFactory.Int64NameReference(), null,
                                                                             EntityModifier.Public | env.Options.ReassignableModifier()))
                                   .With(FunctionBuilder.Create("getIt",
                                                                ExpressionReadMode.OptionalUse,
                                                                NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new IExpression[] {
                    Return.Create(NameReference.Create(NameFactory.ThisVariableName, "m"))
                }))));
                root_ns.AddBuilder(FunctionBuilder.Create("main",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.Int64NameReference(),
                                                          Block.CreateStatement(new IExpression[] {
                    // b = new Beep()
                    VariableDeclaration.CreateStatement("b", null, ExpressionFactory.StackConstructor(NameReference.Create("Beep"))),
                    // b.m = 2
                    Assignment.CreateStatement(NameReference.Create("b", "m"), Int64Literal.Create("2")),
                    // f = b.getIt // "b" value is sucked in, so we have a copy
                    VariableDeclaration.CreateStatement("f", null, NameReference.Create("b", "getIt")),
                    // b.m = 5
                    Assignment.CreateStatement(NameReference.Create("b", "m"), Int64Literal.Create("5")),
                    // return f()
                    Return.Create(FunctionCall.Create(NameReference.Create("f")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 11
0
        public IInterpreter DereferenceOnAssignment()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DebugThrowOnError      = true,
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    // p_int *Int = new Int(1)
                    VariableDeclaration.CreateStatement("p_int", NameFactory.PointerNameReference(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability())),
                                                        ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability()),
                                                                                          FunctionArgument.Create(Int64Literal.Create("1"))), env.Options.ReassignableModifier()),
                    // v_int Int = *p_int // automatic dereference
                    VariableDeclaration.CreateStatement("v_int", NameFactory.Int64NameReference(), NameReference.Create("p_int")),
                    // z_int Int
                    VariableDeclaration.CreateStatement("z_int", NameFactory.Int64NameReference(), null, env.Options.ReassignableModifier()),
                    // z_int = *p_int // automatic derereference
                    Assignment.CreateStatement(NameReference.Create("z_int"), NameReference.Create("p_int")),
                    // p_int = new Int(77)
                    Assignment.CreateStatement(NameReference.Create("p_int"),
                                               ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability()),
                                                                                 FunctionArgument.Create(Int64Literal.Create("77")))),
                    // return v_int + z_int
                    Return.Create(FunctionCall.Create(NameReference.Create(NameReference.Create("v_int"), NameFactory.AddOperator),
                                                      FunctionArgument.Create(NameReference.Create("z_int"))))
                })));


                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 12
0
        public IInterpreter RefCountsOnReadingFunctionCall()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                // the aim of this test is to test heap manager if it correctly handles reading function which returns pointer

                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true,
                    DebugThrowOnError      = true
                }
                                                      .SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "provider",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.PointerNameReference(NameFactory.Int64NameReference()),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p_int", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(),
                                                                                          FunctionArgument.Create(Int64Literal.Create("77")))),
                    Return.Create(NameReference.Create("p_int")),
                })));

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(),
                                                        FunctionCall.Create(NameReference.Create("provider"))),
                    Return.Create(NameReference.Create("i")),
                })));


                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(77L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 13
0
        public IInterpreter LocalVariablesLeakCheck()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "last",
                                       ExpressionReadMode.ReadRequired,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("z", null, Int64Literal.Create("2")),
                    Return.Create(NameReference.Create("z"))
                })));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "pass",
                                       ExpressionReadMode.ReadRequired,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("z", null, Int64Literal.Create("0")),
                    VariableDeclaration.CreateStatement("t", null, FunctionCall.Create(NameReference.Create("last"))),
                    Return.Create(ExpressionFactory.Add(NameReference.Create("z"), NameReference.Create("t")))
                })));
                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    Return.Create(FunctionCall.Create(NameReference.Create("pass")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 14
0
        public IInterpreter SingleMessage()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "sender",
                                       ExpressionReadMode.CannotBeRead,
                                       NameFactory.UnitNameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    ExpressionFactory.AssertTrue(FunctionCall.Create(NameReference.Create("ch", NameFactory.ChannelSend),
                                                                     FunctionArgument.Create(Int64Literal.Create("2")))),
                }))
                                   .Parameters(FunctionParameter.Create("ch", NameFactory.PointerNameReference(NameFactory.ChannelNameReference(NameFactory.Int64NameReference())),
                                                                        Variadic.None, null, isNameRequired: false)));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("ch", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.ChannelNameReference(NameFactory.Int64NameReference()))),
                    Spawn.Create(FunctionCall.Create(NameReference.Create("sender"), FunctionArgument.Create(NameReference.Create("ch")))),
                    VariableDeclaration.CreateStatement("r", null,
                                                        FunctionCall.Create(NameReference.Create("ch", NameFactory.ChannelReceive))),
                    ExpressionFactory.AssertOptionIsSome(NameReference.Create("r")),
                    Return.Create(ExpressionFactory.GetOptionValue(NameReference.Create("r")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 15
0
        private IInterpreter duckVirtualCall(Options options)
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env     = Environment.Create(options.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("X")
                                   .SetModifier(options.InterfaceDuckTyping ? EntityModifier.Interface : EntityModifier.Protocol)
                                   .With(FunctionBuilder.Create(
                                             "bar",
                                             null,
                                             NameFactory.Int64NameReference(),
                                             Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("33"))
                }))));

                TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("Y")
                                                              .With(FunctionBuilder.Create("bar",
                                                                                           null,
                                                                                           NameFactory.Int64NameReference(),
                                                                                           Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))));

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("i", NameFactory.PointerNameReference(NameReference.Create("X")),
                                                        ExpressionFactory.HeapConstructor(NameReference.Create("Y"))),
                    Return.Create(FunctionCall.Create(NameReference.Create("i", "bar")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 16
0
        public IInterpreter TestingSamePointers()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult             = true,
                    DebugThrowOnError                  = true,
                    DiscardingAnyExpressionDuringTests = true
                }.SetMutability(mutability));
                var root_ns = env.Root;


                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("x", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("2"))),
                    VariableDeclaration.CreateStatement("y", null, NameReference.Create("x")),
                    VariableDeclaration.CreateStatement("z", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("2"))),
                    VariableDeclaration.CreateStatement("acc", null, Int64Literal.Create("0"), env.Options.ReassignableModifier()),
                    IfBranch.CreateIf(IsSame.Create(NameReference.Create("x"), NameReference.Create("y")), new[] {
                        Assignment.CreateStatement(NameReference.Create("acc"),
                                                   ExpressionFactory.Add(NameReference.Create("acc"), Int64Literal.Create("2")))
                    }),
                    IfBranch.CreateIf(IsSame.Create(NameReference.Create("x"), NameReference.Create("z")), new[] {
                        Assignment.CreateStatement(NameReference.Create("acc"),
                                                   ExpressionFactory.Add(NameReference.Create("acc"), Int64Literal.Create("7")))
                    }),
                    Return.Create(NameReference.Create("acc"))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 17
0
        public IInterpreter NoLimitsVariadicFunction()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "sum",
                                       ExpressionReadMode.ReadRequired,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    // let i0 = n.at(0)
                    VariableDeclaration.CreateStatement("i0", null, FunctionCall.Create(NameReference.Create("n", NameFactory.PropertyIndexerName),
                                                                                        FunctionArgument.Create(NatLiteral.Create("0")))),
                    // let i1 = n.at(1)
                    VariableDeclaration.CreateStatement("i1", null, FunctionCall.Create(NameReference.Create("n", NameFactory.PropertyIndexerName),
                                                                                        FunctionArgument.Create(NatLiteral.Create("1")))),
                    // return i0+i1
                    Return.Create(ExpressionFactory.Add("i0", "i1"))
                }))
                                   .Parameters(FunctionParameter.Create("n", NameFactory.Int64NameReference(), Variadic.Create(), null, isNameRequired: false)));

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    Return.Create(FunctionCall.Create(NameReference.Create("sum"),
                                                      Int64Literal.Create("-6"), Int64Literal.Create("8")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 18
0
        public IInterpreter AutoPropertiesWithPointers()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var point_type = root_ns.AddBuilder(TypeBuilder.Create("Point")
                                                    .SetModifier(EntityModifier.Mutable)
                                                    .With(Property.Create(env.Options, "x", NameFactory.PointerNameReference(NameFactory.Nat8NameReference()),
                                                                          new[] { Property.CreateAutoField(NameFactory.PropertyAutoField, NameFactory.PointerNameReference(NameFactory.Nat8NameReference()),
                                                                                                           ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(), Nat8Literal.Create("1")),
                                                                                                           env.Options.ReassignableModifier()) },
                                                                          new[] { Property.CreateAutoGetter(NameFactory.PropertyAutoField, NameFactory.PointerNameReference(NameFactory.Nat8NameReference())) },
                                                                          new[] { Property.CreateAutoSetter(NameFactory.PropertyAutoField, NameFactory.PointerNameReference(NameFactory.Nat8NameReference())) }
                                                                          )));
                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Nat8NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    // p = Point() // p.x is initialized with 1
                    VariableDeclaration.CreateStatement("p", null, ExpressionFactory.StackConstructor(NameReference.Create("Point"))),
                    // p.x = 1+p.x
                    Assignment.CreateStatement(NameReference.Create(NameReference.Create("p"), "x"),
                                               ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(),
                                                                                 FunctionCall.Create(NameReference.Create(Nat8Literal.Create("1"), NameFactory.AddOperator),
                                                                                                     FunctionArgument.Create(NameReference.Create("p", "x"))))),
                    // return p.x
                    Return.Create(NameReference.Create("p", "x"))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)2, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 19
0
        public IInterpreter OptionalParameters()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var point_type = root_ns.AddBuilder(TypeBuilder.Create("Point")
                                                    .SetModifier(EntityModifier.Mutable)
                                                    .With(VariableDeclaration.CreateStatement("x", NameFactory.Int64NameReference(),
                                                                                              Int64Literal.Create("2"), env.Options.ReassignableModifier()))
                                                    .With(FunctionBuilder.Create("pass",
                                                                                 ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] {
                    Return.Create(NameReference.Create("v"))
                }))
                                                          .Parameters(FunctionParameter.Create("v", NameFactory.Int64NameReference(), Variadic.None,
                                                                                               FunctionCall.Create(NameReference.Create(NameFactory.ThisVariableName, "getme")), isNameRequired: false)))
                                                    .With(FunctionBuilder.Create("getme", null,
                                                                                 ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] {
                    Return.Create(NameReference.Create(NameFactory.ThisVariableName, "x"))
                }))));

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", null, ExpressionFactory.StackConstructor(NameReference.Create("Point"))),
                    Return.Create(FunctionCall.Create(NameReference.Create("p", "pass")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 20
0
        public IInterpreter ResolvingGenericArgumentInRuntime()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(FunctionBuilder.Create("oracle", "O", VarianceMode.None,
                                                          NameFactory.BoolNameReference(),
                                                          Block.CreateStatement(
                                                              Return.Create(IsType.Create(NameReference.Create("thing"), NameReference.Create("O")))
                                                              ))
                                   .Parameters(FunctionParameter.Create("thing", NameFactory.ReferenceNameReference(NameFactory.IObjectNameReference()))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(
                                           VariableDeclaration.CreateStatement("acc", null, Nat8Literal.Create("0"), env.Options.ReassignableModifier()),
                                           VariableDeclaration.CreateStatement("i", null, ExpressionFactory.HeapConstructor(NameFactory.IntNameReference(), IntLiteral.Create("7"))),
                                           VariableDeclaration.CreateStatement("d", null, ExpressionFactory.HeapConstructor(NameFactory.RealNameReference(), RealLiteral.Create("3.3"))),
                                           IfBranch.CreateIf(FunctionCall.Create(NameReference.Create("oracle", NameFactory.IntNameReference()),
                                                                                 NameReference.Create("i")),
                                                             new[] { ExpressionFactory.IncBy("acc", Nat8Literal.Create("2")) }),
                                           IfBranch.CreateIf(FunctionCall.Create(NameReference.Create("oracle", NameFactory.IntNameReference()),
                                                                                 NameReference.Create("d")),
                                                             new[] { ExpressionFactory.IncBy("acc", Nat8Literal.Create("88")) }),
                                           Return.Create(NameReference.Create("acc"))
                                           )));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)2, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 21
0
        public IInterpreter RecursiveCall()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                IExpression i_eq_2  = ExpressionFactory.IsEqual(NameReference.Create("i"), Int64Literal.Create("2"));
                IExpression i_add_1 = ExpressionFactory.Add(NameReference.Create("i"), Int64Literal.Create("1"));
                root_ns.AddBuilder(FunctionBuilder.Create("foo",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.Int64NameReference(),
                                                          Block.CreateStatement(new[] {
                    // if i==2 then return i
                    IfBranch.CreateIf(i_eq_2, new[] { Return.Create(NameReference.Create("i")) },
                                      // else return self(i+1)
                                      IfBranch.CreateElse(new[] {
                        Return.Create(FunctionCall.Create(NameReference.Create(NameFactory.RecurFunctionName),
                                                          FunctionArgument.Create(i_add_1)))
                    }))
                }))
                                   .Parameters(FunctionParameter.Create("i", NameFactory.Int64NameReference())));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    Return.Create(FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(Int64Literal.Create("0"))))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 22
0
        public IInterpreter ClosureRecursiveCall()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                IExpression        i_eq_jack = ExpressionFactory.IsEqual(NameReference.Create("i"), NameReference.Create("jack"));
                IExpression        i_add_1   = ExpressionFactory.Add(NameReference.Create("i"), Nat8Literal.Create("1"));
                FunctionDefinition lambda    = FunctionBuilder.CreateLambda(NameFactory.Nat8NameReference(),
                                                                            Block.CreateStatement(new[] {
                    // if i==jack then return i
                    IfBranch.CreateIf(i_eq_jack, new[] { Return.Create(NameReference.Create("i")) },
                                      // else return self(i+1)
                                      IfBranch.CreateElse(new[] {
                        Return.Create(FunctionCall.Create(NameReference.Create(NameFactory.RecurFunctionName),
                                                          FunctionArgument.Create(i_add_1)))
                    }))
                }))
                                               .Parameters(FunctionParameter.Create("i", NameFactory.Nat8NameReference()));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("jack", null, Nat8Literal.Create("50")),
                    // Immediately-Invoked Function Expression (IIEFE) in Javascript world
                    Return.Create(FunctionCall.Create(lambda, FunctionArgument.Create(Nat8Literal.Create("0"))))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)50, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 23
0
        public IInterpreter NestedRefCountings()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true, DebugThrowOnError = true
                }
                                                      .SetMutability(mutability));
                var root_ns = env.Root;

                var chain_type = root_ns.AddBuilder(TypeBuilder.Create("Chain")
                                                    .SetModifier(EntityModifier.Mutable)
                                                    .With(VariableDeclaration.CreateStatement("v", NameFactory.Int64NameReference(), null,
                                                                                              EntityModifier.Public | env.Options.ReassignableModifier()))
                                                    .With(VariableDeclaration.CreateStatement("n", NameFactory.PointerNameReference(NameReference.Create("Chain")),
                                                                                              Undef.Create(), EntityModifier.Public | env.Options.ReassignableModifier())));
                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", NameFactory.PointerNameReference(NameReference.Create("Chain")),
                                                        ExpressionFactory.HeapConstructor(NameReference.Create("Chain"))),
                    Assignment.CreateStatement(NameReference.Create("p", "n"),
                                               ExpressionFactory.HeapConstructor(NameReference.Create("Chain"))),
                    Assignment.CreateStatement(NameReference.Create("p", "n", "v"), Int64Literal.Create("2")),
                    Return.Create(NameReference.Create("p", "n", "v")),
                })));


                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 24
0
        public IInterpreter InitializingWithCustomSetter()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference())
                                         .With(VariableDeclaration.CreateStatement("f", NameFactory.Nat8NameReference(), null,
                                                                                   env.Options.ReassignableModifier()))
                                         .WithGetter(Block.CreateStatement(Return.Create(NameReference.CreateThised("f"))))
                                         .WithSetter(Block.CreateStatement(Assignment.CreateStatement(NameReference.CreateThised("f"),
                                                                                                      ExpressionFactory.Mul(NameFactory.PropertySetterValueReference(), Nat8Literal.Create("2")))))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", null,
                                                        ConstructorCall.StackConstructor(NameReference.Create("Point"))
                                                        .Init("x", Nat8Literal.Create("7"))
                                                        .Build()),
                    Return.Create(NameReference.Create(NameReference.Create("p"), "x"))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)14, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 25
0
        public IInterpreter ReturningUnit()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true,
                    AllowInvalidMainResult             = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "getMe",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.UnitNameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    Return.Create()
                })));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("u", NameFactory.UnitNameReference(),
                                                        FunctionCall.Create(NameReference.Create("getMe"))),
                    ExpressionFactory.Readout("u"),
                    Return.Create(Int64Literal.Create("2"))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 26
0
        public IInterpreter StoringSequenceAsSequence()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                // todo: add analogous test with storing iterable as sequence

                // yes, this test seems like pointless thing but it is important nevertheless because
                // we have to check if conversion does NOT happen
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Nat8NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("x", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.ChunkNameReference(NameFactory.Int64NameReference()),
                                                                                          FunctionArgument.Create(NatLiteral.Create("2"))),
                                                        env.Options.ReassignableModifier()),
                    ExpressionFactory.InitializeIndexable("x", Int64Literal.Create("-6"), Int64Literal.Create("8")),
                    // conversion should NOT happen
                    VariableDeclaration.CreateStatement("stored", null,
                                                        FunctionCall.Create(NameFactory.StoreFunctionReference(), NameReference.Create("x"))),
                    Return.Create(ExpressionFactory.Ternary(IsSame.Create("x", "stored"), Nat8Literal.Create("2"), Nat8Literal.Create("7")))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)2, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 27
0
File: Io.cs Progetto: macias/Skila
        public IInterpreter CommandLine()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Nat8NameReference(),
                                                       Block.CreateStatement(
                                                           ExpressionFactory.AssertEqual(StringLiteral.Create(Interpreter.Interpreter.CommandLineTestProgramPath),
                                                                                         NameReference.Create(NameFactory.CommandLineProgramPath)),

                                                           ExpressionFactory.AssertEqual(StringLiteral.Create(Interpreter.Interpreter.CommandLineTestArgument),
                                                                                         FunctionCall.Create(NameReference.Create(NameFactory.CommandLineArguments, NameFactory.AtFunctionName),
                                                                                                             NatLiteral.Create("0"))),

                                                           Return.Create(Nat8Literal.Create("0"))
                                                           )).
                                                   Parameters(FunctionParameter.Create(NameFactory.CommandLineProgramPath,
                                                                                       NameFactory.StringPointerNameReference(TypeMutability.ReadOnly)),
                                                              FunctionParameter.Create(NameFactory.CommandLineArguments,
                                                                                       NameFactory.StringPointerNameReference(TypeMutability.ReadOnly), Variadic.Create(), null, isNameRequired: false)));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)0, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 28
0
File: Flow.cs Progetto: macias/Skila
        public IInterpreter ParallelOptionalDeclaration()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DebugThrowOnError = true,
                }.SetMutability(mutability));
                var root_ns = env.Root;

                IExpression opt_declaration = ExpressionFactory.OptionalDeclaration(new[] {
                    VariablePrototype.Create("x", NameFactory.Nat8NameReference()),
                    VariablePrototype.Create("y", NameFactory.Nat8NameReference())
                }, new[] {
                    ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("2")),
                    ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("7")),
                });

                root_ns.AddBuilder(FunctionBuilder.Create("main",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.Nat8NameReference(),
                                                          Block.CreateStatement(
                                                              IfBranch.CreateIf(opt_declaration,
                                                                                Return.Create(ExpressionFactory.Add("x", "y")),
                                                                                IfBranch.CreateElse(
                                                                                    ExpressionFactory.GenericThrow()
                                                                                    ))
                                                              )));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual((byte)9, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 29
0
File: Io.cs Progetto: macias/Skila
        public IInterpreter FileReadingLines()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DebugThrowOnError = true, AllowInvalidMainResult = true
                }
                                             .SetMutability(mutability));
                var root_ns = env.Root;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Nat64NameReference(),
                                                       Block.CreateStatement(
                                                           VariableDeclaration.CreateStatement("lines", null,
                                                                                               ExpressionFactory.GetOptionValue(FunctionCall.Create(NameReference.Create(NameFactory.FileNameReference(), NameFactory.FileReadLines),
                                                                                                                                                    StringLiteral.Create(randomTextFilePath)))),
                                                           // first line is "It was" (without quotes)
                                                           VariableDeclaration.CreateStatement("first", null,
                                                                                               FunctionCall.Create(NameReference.Create("lines", NameFactory.PropertyIndexerName), NatLiteral.Create("0"))),
                                                           // 6
                                                           VariableDeclaration.CreateStatement("len", null,
                                                                                               NameReference.Create("first", NameFactory.IIterableCount)),
                                                           Return.Create(NameReference.Create("len"))
                                                           )));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(6UL, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Esempio n. 30
0
        public IInterpreter TraitFunctionCall()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowInvalidMainResult = true,
                    DebugThrowOnError      = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    // e &IEquatable = 3
                    VariableDeclaration.CreateStatement("e", NameFactory.ReferenceNameReference(NameFactory.IEquatableNameReference()),
                                                        Int64Literal.Create("3")),
                    // i Int = 7
                    VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), Int64Literal.Create("7")),
                    // if e!=i and i!=e then return 2
                    IfBranch.CreateIf(ExpressionFactory.And(ExpressionFactory.IsNotEqual("e", "i"), ExpressionFactory.IsNotEqual("e", "i")),
                                      new[] { Return.Create(Int64Literal.Create("2")) }),
                    // return 15
                    Return.Create(Int64Literal.Create("15"))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(2L, result.RetValue.PlainValue);
            }

            return(interpreter);
        }