Esempio n. 1
0
        public IErrorReporter ErrorSettingPropertyReferenceField()
        {
            NameResolver resolver = null;

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

                // this is incorrect we use &Int in setter (OK) but we store this reference in a field too (bad)
                Property property = PropertyBuilder.Create(env.Options, "meow",
                                                           () => NameFactory.ReferenceNameReference(NameFactory.IntNameReference()))
                                    .WithAutoField(Undef.Create(), EntityModifier.Reassignable)
                                    .WithAutoSetter();

                root_ns.AddBuilder(TypeBuilder.Create("whatever")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(property));

                resolver = NameResolver.Create(env);

                var setter_assignment = property.Setter.UserBody.Instructions.Single().Cast <Assignment>();

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, setter_assignment.RhsValue));
            }
            return(resolver);
        }
Esempio n. 2
0
        public IErrorReporter ErrorMutableMethodInImmutableType()
        {
            NameResolver resolver = null;

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

                Property property = PropertyBuilder.Create(env.Options, "bar", () => NameFactory.Int64NameReference())
                                    .WithSetter(body: null);
                FunctionDefinition function = FunctionBuilder.CreateDeclaration("getMe", NameFactory.UnitNameReference())
                                              .SetModifier(EntityModifier.Mutable);
                root_ns.AddBuilder(TypeBuilder.Create("Whatever")
                                   .SetModifier(EntityModifier.Abstract)
                                   .With(property)
                                   .With(function));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.PropertySetterInImmutableType, property));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MutableFunctionInImmutableType, function));
            }

            return(resolver);
        }
Esempio n. 3
0
        public IErrorReporter ErrorGetterOverridesNothing()
        {
            NameResolver resolver = null;

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

                Property property = PropertyBuilder.Create(env.Options, "getMe", () => NameFactory.Int64NameReference())
                                    .With(PropertyMemberBuilder.CreateGetter(Block.CreateStatement(Return.Create(Int64Literal.Create("2"))))
                                          .Modifier(EntityModifier.Override));

                var type = root_ns.AddBuilder(TypeBuilder.Create("Last")
                                              .SetModifier(EntityModifier.Base)
                                              .With(property));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NothingToOverride, property.Getter));
            }

            return(resolver);
        }
Esempio n. 4
0
        public IErrorReporter ErrorSettingCustomGetter()
        {
            NameResolver resolver = null;

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

                // we can assign property using getter (only in constructor) but getter has to be auto-generated,
                // here it is custom code, thus it is illegal
                IExpression assign = Assignment.CreateStatement(NameReference.CreateThised("x"), IntLiteral.Create("3"));

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(PropertyBuilder.Create(env.Options, "x", () => NameFactory.IntNameReference())
                                         .WithGetter(Block.CreateStatement(Return.Create(IntLiteral.Create("5")))))
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(
                                                                                   assign
                                                                                   ))));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Esempio n. 5
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. 6
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);
        }
Esempio n. 7
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. 8
0
        public IErrorReporter ErrorCustomGetterWithInitialization()
        {
            NameResolver resolver = null;

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

                // error: custom getter (with no setter) + post initialization
                Property property = PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference())
                                    .With(PropertyMemberBuilder.CreateGetter(Block.CreateStatement(Return.Create(Nat8Literal.Create("3")))))
                                    .SetModifier(EntityModifier.PostInitialization);

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                                   .With(property));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", null,
                                                        ConstructorCall.StackConstructor(NameReference.Create("Point"))
                                                        // this cannot be executed but do not report an error for it, because the type has to be fixed first
                                                        .Init("x", Nat8Literal.Create("17"))
                                                        .Build()),
                    Return.Create(NameReference.Create(NameReference.Create("p"), "x"))
                })));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Esempio n. 9
0
        public IErrorReporter ErrorIgnoringGetter()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(PropertyBuilder.Create(env.Options, "x", () => NameFactory.Int64NameReference())
                                         .WithAutoField(Int64Literal.Create("1"), env.Options.ReassignableModifier())
                                         .WithAutoGetter()
                                         .WithAutoSetter()));

                NameReference getter_call = NameReference.Create("p", "x");
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "getter",
                                       null,
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.UnitNameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", null, ExpressionFactory.StackConstructor("Point")),
                    getter_call
                })));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Esempio n. 10
0
 public PropertyBuilder Property(string type, string name) => PropertyBuilder.Create(this, type, name);