示例#1
0
        public IErrorReporter ErrorMissingThis()
        {
            NameResolver resolver = null;
            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                    .SetModifier(EntityModifier.Base)
                    .With(VariableDeclaration.CreateStatement("x", NameFactory.Int64NameReference(), null, EntityModifier.Protected))
                    .With(FunctionBuilder.Create("foo", ExpressionReadMode.OptionalUse,
                    NameFactory.UnitNameReference(),

                        Block.CreateStatement())));

                NameReference x_ref = NameReference.Create("x");
                NameReference y_ref = NameReference.Create("y");
                NameReference foo_ref = NameReference.Create("foo");
                NameReference bar_ref = NameReference.Create("bar");
                root_ns.AddBuilder(TypeBuilder.Create("Next")
                    .Parents("Point")
                    .With(VariableDeclaration.CreateStatement("y", NameFactory.Int64NameReference(), null))
                    .With(FunctionBuilder.Create("bar", ExpressionReadMode.OptionalUse,
                    NameFactory.UnitNameReference(),

                        Block.CreateStatement()))
                    .With(FunctionBuilder.Create("all", ExpressionReadMode.OptionalUse,
                    NameFactory.UnitNameReference(),

                        Block.CreateStatement(new IExpression[] {
                         ExpressionFactory.Readout(x_ref),
                         ExpressionFactory.Readout(y_ref),
                        FunctionCall.Create(foo_ref),
                        FunctionCall.Create(bar_ref),
                        }))));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(4, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MissingThisPrefix, x_ref));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MissingThisPrefix, y_ref));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MissingThisPrefix, foo_ref));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MissingThisPrefix, bar_ref));
            }

            return resolver;
        }
示例#2
0
 public static FunctionCall Indexer(IExpression expr, params FunctionArgument[] arguments)
 {
     // we create indexer-getters for all cases initially, then we have to only check assignments LHS
     // and convert only those to indexer-setters
     return(new FunctionCall(CallMode.Indexer,
                             // alternative approach (below, commented out) would be to refer indexer accessor indirectly,
                             // via indexer itself, so instead of:
                             // my_obj.|INDEXER-MODE|Get(5)
                             // we would have:
                             // my_obj.idx.idxGet(5)
                             // the latter approach seems cleaner but requires more handling in code
                             NameReference.Create(expr, NameFactory.PropertyGetter),
                             // NameReference.Create(NameReference.Create(name, NameFactory.PropertyIndexerName), NameFactory.PropertyGetter),
                             arguments,
                             requestedOutcomeType: null));
 }
示例#3
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);
        }
示例#4
0
        public IErrorReporter OutgoingConversion()
        {
            NameResolver resolver = null;

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

                var type_foo_def = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Foo"))
                                                      .With(FunctionBuilder.Create(
                                                                NameFactory.ConvertFunctionName,
                                                                null, ExpressionReadMode.ReadRequired, NameReference.Create("Bar"),
                                                                Block.CreateStatement(new IExpression[] { Return.Create(Undef.Create()) }))
                                                            .SetModifier(EntityModifier.Implicit))
                                                      // added second conversion to check if compiler correctly disambiguate the call
                                                      .With(FunctionBuilder.Create(
                                                                NameFactory.ConvertFunctionName,
                                                                null, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new IExpression[] { Return.Create(Undef.Create()) }))
                                                            .SetModifier(EntityModifier.Implicit)));
                var type_bar_def = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Bar")));


                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "wrapper",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.UnitNameReference(),

                                       Block.CreateStatement(new[] {
                    VariableDeclaration.CreateStatement("f", NameReference.Create("Foo"),
                                                        initValue: Undef.Create()),
                    VariableDeclaration.CreateStatement("b", NameReference.Create("Bar"),
                                                        initValue: NameReference.Create("f")),
                    ExpressionFactory.Readout("b")
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }
示例#5
0
        public IErrorReporter DistinctTypesOverloadCall()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    GlobalVariables = true, RelaxedMode = true
                }.SetMutability(mutability));
                var root_ns   = env.Root;
                var system_ns = env.SystemNamespace;

                var func_def1 = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "foo",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.RealNameReference(),
                                                       Block.CreateStatement(new[] {
                    Return.Create(RealLiteral.Create("3.3"))
                }))
                                                   .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), usageMode: ExpressionReadMode.CannotBeRead)));
                var func_def2 = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "foo",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.RealNameReference(),
                                                       Block.CreateStatement(new[] {
                    Return.Create(RealLiteral.Create("3.3"))
                }))
                                                   .Parameters(FunctionParameter.Create("x", NameFactory.RealNameReference(), usageMode: ExpressionReadMode.CannotBeRead)));
                root_ns.AddNode(VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), Undef.Create()));
                root_ns.AddNode(VariableDeclaration.CreateStatement("s", NameFactory.RealNameReference(), Undef.Create()));
                var call1 = FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(NameReference.Create("i")));
                var call2 = FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(NameReference.Create("s")));
                root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameFactory.RealNameReference(),
                                                                    call1, modifier: EntityModifier.Public));
                root_ns.AddNode(VariableDeclaration.CreateStatement("y", NameFactory.RealNameReference(),
                                                                    call2, modifier: EntityModifier.Public));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
                Assert.AreEqual(func_def1, call1.Resolution.TargetFunctionInstance.Target);
                Assert.AreEqual(func_def2, call2.Resolution.TargetFunctionInstance.Target);
            }

            return(resolver);
        }
示例#6
0
        public IExpression DetachFieldInitialization()
        {
            if (this.InitValue.IsUndef())
            {
                return(null);
            }

            if (this.InitValue == null)
            {
                // we need to save it for later to change the errors, user does not see this call, but she/he
                // sees the field
                this.autoFieldDefaultInit = FunctionCall.Constructor(NameReference.Create(fieldReference(),
                                                                                          NameFactory.InitConstructorName));
                return(this.autoFieldDefaultInit);
            }
            else if (!this.InitValue.IsUndef())
            {
                this.initValue.DetachFrom(this);

                IExpression init;

                // if the init value is constructor call, there is no point in creating around it
                // another constructor call. Instead get the init step and reuse it, this time
                // with given field directly, for example
                // x = Foo()
                // translates to
                // x = (__this__ = alloc Foo ; __this__.init() ; __this__)
                // so we rip off the init step and replace the object, which results in
                // x.init()
                if (this.initValue is New block
                    // do not use this optimization for heap objects!
                    && !block.IsHeapInitialization)
                {
                    FunctionCall cons_call = block.InitConstructorCall;
                    cons_call.DetachFrom(block);

                    cons_call.Name.ReplacePrefix(fieldReference());
                    init = cons_call;
                }
                else
                {
                    init = Assignment.CreateStatement(fieldReference(), this.initValue);
                }

                this.initValue = null;
                return(init);
            }
示例#7
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);
        }
示例#8
0
        public IInterpreter OverridingMethodWithGetter()
        {
            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("getMe", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())));

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

                root_ns.AddBuilder(TypeBuilder.Create("Last")
                                   .Parents("Middle")
                                   .SetModifier(EntityModifier.Base)
                                   .With(PropertyBuilder.Create(env.Options, "getMe", () => NameFactory.Int64NameReference())
                                         .With(PropertyMemberBuilder.CreateGetter(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", "getMe")))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
示例#9
0
        public IErrorReporter InheritanceMatching()
        {
            NameResolver resolver = null;

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

                var unrelated_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Separate")));
                var abc_type       = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("ABC")));
                var derived_type   = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Deriv"))
                                                          .Parents(NameReference.Create("ABC")));
                var foo_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Foo", "V", VarianceMode.Out))
                                                    .Parents(NameReference.Create("ABC")));
                var tuple_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Tuple", "T", VarianceMode.None))
                                                      .Parents(NameReference.Create("Foo", NameReference.Create("T"))));


                var separate_ref    = system_ns.AddNode(NameReference.Create("Separate"));
                var abc_ref         = system_ns.AddNode(NameReference.Create("ABC"));
                var deriv_ref       = system_ns.AddNode(NameReference.Create("Deriv"));
                var tuple_deriv_ref = system_ns.AddNode(NameReference.Create("Tuple", NameReference.Create("Deriv")));
                var foo_abc_ref     = system_ns.AddNode(NameReference.Create("Foo", NameReference.Create("ABC")));
                var tuple_abc_ref   = system_ns.AddNode(NameReference.Create("Tuple", NameReference.Create("ABC")));
                var foo_deriv_ref   = system_ns.AddNode(NameReference.Create("Foo", NameReference.Create("Deriv")));

                resolver = NameResolver.Create(env);

                Assert.AreNotEqual(TypeMatch.Same, separate_ref.Binding.Match.Instance.MatchesTarget(resolver.Context, abc_ref.Binding.Match.Instance,
                                                                                                     TypeMatching.Create(env.Options.InterfaceDuckTyping, allowSlicing: true)));
                Assert.AreEqual(TypeMatch.Substitute, deriv_ref.Binding.Match.Instance.MatchesTarget(resolver.Context, abc_ref.Binding.Match.Instance,
                                                                                                     TypeMatching.Create(env.Options.InterfaceDuckTyping, allowSlicing: true)));
                Assert.AreEqual(TypeMatch.Substitute, tuple_deriv_ref.Binding.Match.Instance.MatchesTarget(resolver.Context, foo_abc_ref.Binding.Match.Instance,
                                                                                                           TypeMatching.Create(env.Options.InterfaceDuckTyping, allowSlicing: true)));
                TypeMatch match = tuple_abc_ref.Binding.Match.Instance.MatchesTarget(resolver.Context, foo_deriv_ref.Binding.Match.Instance,
                                                                                     TypeMatching.Create(env.Options.InterfaceDuckTyping, allowSlicing: true));
                Assert.AreNotEqual(TypeMatch.Same, match);
                Assert.AreNotEqual(TypeMatch.Substitute, match);
            }

            return(resolver);
        }
示例#10
0
 public static IExpression Tuple(params IExpression[] arguments)
 {
     if (arguments.Length == 0)
     {
         throw new System.Exception();
     }
     else if (arguments.Length == 1)
     {
         return(arguments.Single());
     }
     else
     {
         return(FunctionCall.Create(
                    NameReference.Create(NameFactory.TupleFactoryReference(), NameFactory.CreateFunctionName),
                    arguments));
     }
 }
示例#11
0
文件: Templates.cs 项目: macias/Skila
        public IErrorReporter PassingTraitAsIncorrectInterface()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = 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)));

                IExpression init_value = ExpressionFactory.HeapConstructor(NameReference.Create("Greeter", NameReference.Create("NoSay")));
                root_ns.AddBuilder(FunctionBuilder.Create("major",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.UnitNameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("g", NameFactory.PointerNameReference("ISay"),
                                                                                                  init_value),
                                                              ExpressionFactory.Readout("g")
                                                              )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, init_value));
            }

            return(resolver);
        }
示例#12
0
文件: Closures.cs 项目: macias/Skila
        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);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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);
        }
示例#16
0
        private IErrorReporter duckTyping(Options options)
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("IX")
                                   .With(FunctionBuilder.CreateDeclaration(
                                             "bar",
                                             ExpressionReadMode.OptionalUse,
                                             NameFactory.PointerNameReference(NameFactory.IObjectNameReference()))
                                         .Parameters(FunctionParameter.Create("x", NameFactory.BoolNameReference(), Variadic.None, null, isNameRequired: false)))
                                   .SetModifier(env.Options.InterfaceDuckTyping ? EntityModifier.Interface : EntityModifier.Protocol));

                root_ns.AddBuilder(TypeBuilder.Create("X")
                                   .With(FunctionBuilder.Create("bar",
                                                                ExpressionReadMode.OptionalUse,
                                                                // subtype of original result typename -- this is legal
                                                                NameFactory.PointerNameReference(NameFactory.Int64NameReference()),
                                                                Block.CreateStatement(new[] {
                    Return.Create(ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("2")))
                }))
                                         .Parameters(FunctionParameter.Create("x", NameFactory.BoolNameReference(), usageMode: ExpressionReadMode.CannotBeRead))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("i", NameFactory.PointerNameReference(NameReference.Create("IX")), null, env.Options.ReassignableModifier()),
                    Assignment.CreateStatement(NameReference.Create("i"), ExpressionFactory.HeapConstructor(NameReference.Create("X"))),
                    ExpressionFactory.Readout("i"),
                    Return.Create(Int64Literal.Create("2"))
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }
示例#17
0
        public IErrorReporter ErrorSpawningMutables()
        {
            NameResolver resolver = null;

            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(), null,
                                                                                              EntityModifier.Public | env.Options.ReassignableModifier()))
                                                    .With(FunctionBuilder.Create("empty",
                                                                                 ExpressionReadMode.CannotBeRead,
                                                                                 NameFactory.UnitNameReference(),

                                                                                 Block.CreateStatement())
                                                          .Parameters(FunctionParameter.Create("p", NameFactory.PointerNameReference(NameReference.Create("Point")), Variadic.None,
                                                                                               null, isNameRequired: false, usageMode: ExpressionReadMode.CannotBeRead))));

                FunctionArgument mutable_arg    = FunctionArgument.Create(NameReference.Create("r"));
                NameReference    mutable_method = NameReference.Create("r", "empty");
                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("r", null, ExpressionFactory.HeapConstructor(NameReference.Create("Point"))),
                    Spawn.Create(FunctionCall.Create(mutable_method, mutable_arg)),
                    Return.Create(NameReference.Create("r", "x"))
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotSpawnWithMutableArgument, mutable_arg));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotSpawnOnMutableContext, mutable_method));
            }

            return(resolver);
        }
示例#18
0
        public static IExpression OptionalDeclaration(string name, INameReference typeName, IExpression rhs)
        {
            var instructions = new List <IExpression>();

            string temp = AutoName.Instance.CreateNew("opt_temp");

            instructions.Add(VariableDeclaration.CreateStatement(temp, null, rhs));

            // partial declaration (initialization will be done later)
            instructions.Add(VariableDeclaration.CreateStatement(name,
                                                                 typeName ?? NameReference.Create(NameReference.Create(temp),
                                                                                                  BrowseMode.InstanceToStatic, NameFactory.OptionTypeParameterMember),
                                                                 null));

            instructions.Add(OptionalAssignment(NameReference.Create(name), NameReference.Create(temp)));

            return(Chain.Create(instructions));
        }
示例#19
0
        public static IExpression CheckedSelfCast(string paramName, INameReference currentTypeName)
        {
            return(Block.CreateExpression(
                       // this is basically check for Self type
                       // assert this.getType()==other.GetType()
                       // please note
                       // assert other is CurrentType
                       // has different (and incorrect) meaning, because it does not really check both entities type
                       // consider this to be IFooChild of IFoo->IFooParent->IFooChild
                       // and current type would be IFooParent and the other would be of this type
                       AssertTrue(IsSame.Create(FunctionCall.Create(NameReference.CreateThised(NameFactory.GetTypeFunctionName)),
                                                FunctionCall.Create(NameReference.Create(paramName, NameFactory.GetTypeFunctionName)))),

                       // maybe in future it would be possible to dynamically cast to the actual type of other
                       // this would mean static dispatch to later calls
                       ExpressionFactory.GetOptionValue(ExpressionFactory.DownCast(NameReference.Create(paramName), currentTypeName))
                       ));
        }
示例#20
0
        private ConstructorCall(NameReference typeName,
                                // todo: hack, we don't have nice error translation from generic error to more specific one
                                out NameReference constructorReference,
                                Memory memory,
                                TypeMutability mutability,
                                params FunctionArgument[] arguments)
        {
            this.localThis       = AutoName.Instance.CreateNew("cons_obj");
            this.varReference    = NameReference.Create(localThis);
            constructorReference = NameReference.Create(varReference, NameFactory.InitConstructorName);

            this.allocTypeName   = typeName;
            this.allocMemory     = memory;
            this.allocMutability = mutability;
            this.initCall        = FunctionCall.Constructor(constructorReference, arguments);

            this.objectInitialization = new List <IExpression>();
        }
示例#21
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);
        }
示例#22
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);
        }
示例#23
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);
        }
示例#24
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);
        }
示例#25
0
        public IErrorReporter ErrorAssigningMutableToImmutable()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("Bar")
                                   .SetModifier(EntityModifier.Mutable));

                IExpression mutable_init = ExpressionFactory.HeapConstructor(NameReference.Create("Bar"));

                VariableDeclaration decl = VariableDeclaration.CreateStatement("x",
                                                                               NameFactory.PointerNameReference(NameFactory.IObjectNameReference()),
                                                                               mutable_init);

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "foo", null,
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.UnitNameReference(),

                                       Block.CreateStatement(new[] {
                    decl,
                    ExpressionFactory.Readout("x"),
                    // this is OK, we mark target as mutable type and we pass indeed mutable one
                    VariableDeclaration.CreateStatement("y",
                                                        NameFactory.PointerNameReference(NameFactory.IObjectNameReference(overrideMutability: TypeMutability.ForceMutable)),
                                                        ExpressionFactory.HeapConstructor(NameReference.Create("Bar"))),
                    ExpressionFactory.Readout("y"),
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, mutable_init));
            }

            return(resolver);
        }
示例#26
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);
        }
示例#27
0
        public IErrorReporter FunctionAssignment()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    GlobalVariables = true, RelaxedMode = true
                }.SetMutability(mutability));
                var root_ns   = env.Root;
                var system_ns = env.SystemNamespace;

                var func_def = root_ns.AddBuilder(FunctionBuilder.Create(
                                                      "foo",
                                                      ExpressionReadMode.OptionalUse,
                                                      NameFactory.RealNameReference(),
                                                      Block.CreateStatement(new[] {
                    Return.Create(RealLiteral.Create("3.3"))
                })).Parameters(FunctionParameter.Create("t", NameFactory.Int64NameReference(), Variadic.None,
                                                        null, isNameRequired: false, usageMode: ExpressionReadMode.CannotBeRead)));

                // x *IFunction<Int,Double> = foo
                root_ns.AddNode(VariableDeclaration.CreateStatement("x",
                                                                    NameFactory.PointerNameReference(NameReference.Create(NameFactory.IFunctionTypeName, NameFactory.Int64NameReference(), NameFactory.RealNameReference())),
                                                                    initValue: NameReference.Create("foo"), modifier: EntityModifier.Public));
                var foo_ref = NameReference.Create("foo");
                // y *IFunction<Double,Int> = foo
                VariableDeclaration decl = VariableDeclaration.CreateStatement("y",
                                                                               NameFactory.PointerNameReference(NameReference.Create(NameFactory.IFunctionTypeName, NameFactory.RealNameReference(), NameFactory.Int64NameReference())),
                                                                               initValue: foo_ref, modifier: EntityModifier.Public);
                root_ns.AddNode(decl);

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, foo_ref.Binding.Matches.Count);
                Assert.AreEqual(func_def, foo_ref.Binding.Match.Instance.Target);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, decl.InitValue));
            }

            return(resolver);
        }
示例#28
0
        public IErrorReporter UnionMatching()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Separate")));
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("ABC")));
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Deriv"))
                                   .Parents(NameReference.Create("ABC")));
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Deriz"))
                                   .Parents(NameReference.Create("Deriv")));
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("qwerty"))
                                   .Parents(NameReference.Create("ABC")));
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("sink"))
                                   .Parents(NameReference.Create("qwerty"), NameReference.Create("Separate")));


                var separate_deriv_union = root_ns.AddNode(NameReferenceUnion.Create(NameReference.Create("Separate"), NameReference.Create("Deriv")));
                var separate_deriz_union = root_ns.AddNode(NameReferenceUnion.Create(NameReference.Create("Separate"), NameReference.Create("Deriz")));
                var separate_abc_union   = root_ns.AddNode(NameReferenceUnion.Create(NameReference.Create("Separate"), NameReference.Create("ABC")));
                var sink_union           = root_ns.AddNode(NameReferenceUnion.Create(NameReference.Create("sink")));
                var sink_deriv_union     = root_ns.AddNode(NameReferenceUnion.Create(NameReference.Create("sink"), NameReference.Create("Deriv")));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(TypeMatch.Substitute, separate_deriz_union.Evaluation.Components.MatchesTarget(resolver.Context,
                                                                                                               separate_deriv_union.Evaluation.Components, TypeMatching.Create(env.Options.InterfaceDuckTyping, allowSlicing: true)));
                Assert.AreEqual(TypeMatch.Substitute, sink_union.Evaluation.Components.MatchesTarget(resolver.Context,
                                                                                                     separate_abc_union.Evaluation.Components, TypeMatching.Create(env.Options.InterfaceDuckTyping, allowSlicing: true)));
                TypeMatch match = sink_deriv_union.Evaluation.Components.MatchesTarget(resolver.Context,
                                                                                       separate_deriz_union.Evaluation.Components, TypeMatching.Create(env.Options.InterfaceDuckTyping, allowSlicing: true));
                Assert.AreNotEqual(TypeMatch.Same, match);
                Assert.AreNotEqual(TypeMatch.Substitute, match);
            }

            return(resolver);
        }
示例#29
0
文件: Templates.cs 项目: macias/Skila
        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);
        }
示例#30
0
        public IErrorReporter ProperGenericWithCostraintsMethodOverride()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.CreateInterface(NameDefinition.Create("IBar",
                                                                                     TemplateParametersBuffer.Create("TA").Values))
                                   .With(FunctionBuilder.CreateDeclaration(
                                             "barend", "FA", VarianceMode.None,
                                             ExpressionReadMode.OptionalUse,
                                             NameFactory.Int64NameReference())
                                         .Constraints(ConstraintBuilder.Create("FA").Inherits(NameReference.Create("TA")))
                                         .Parameters(FunctionParameter.Create("x", NameReference.Create("TA")))));

                TypeDefinition type_impl = root_ns.AddBuilder(
                    TypeBuilder.Create(NameDefinition.Create("Impl", TemplateParametersBuffer.Create("TB").Values))
                    .With(FunctionBuilder.Create(
                              "barend", TemplateParametersBuffer.Create("FB").Values,
                              ExpressionReadMode.OptionalUse,
                              NameFactory.Int64NameReference(),
                              Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))
                          .Parameters(FunctionParameter.Create("x", NameReference.Create("TB"),
                                                               usageMode: ExpressionReadMode.CannotBeRead))
                          .Constraints(ConstraintBuilder.Create("FB").Inherits(NameReference.Create("TB")))
                          .SetModifier(EntityModifier.Override))
                    .Parents(NameReference.Create("IBar", NameReference.Create("TB"))));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }