Exemplo n.º 1
0
        public IErrorReporter ErrorInitializingWithGetter()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                                   .With(PropertyBuilder.CreateAutoGetter(env.Options, "x", NameFactory.Nat8NameReference()))
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(
                                                                                   Assignment.CreateStatement(NameReference.CreateThised("x"), Nat8Literal.Create("5"))
                                                                                   ))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", null,
                                                        ConstructorCall.StackConstructor(NameReference.Create("Point"))
                                                        // cannot use getter in post-initialization
                                                        .Init("x", Nat8Literal.Create("17"), out Assignment post_init)
                                                        .Build()),
                    Return.Create(NameReference.Create(NameReference.Create("p"), "x"))
                })));
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
 public static IExpression StackConstructor(NameReference typeName, out NameReference constructorReference,
                                            params FunctionArgument[] arguments)
 {
     return(ConstructorCall.StackConstructor(typeName, out constructorReference, arguments).Build());
 }
Exemplo n.º 6
0
 public static IExpression StackConstructor(NameReference typeName, params IExpression[] arguments)
 {
     return(ConstructorCall.StackConstructor(typeName, arguments).Build());
 }
Exemplo n.º 7
0
 public static IExpression StackConstructor(string typeName)
 {
     return(ConstructorCall.StackConstructor(typeName).Build());
 }
Exemplo n.º 8
0
 public static IExpression StackConstructor(string typeName, params FunctionArgument[] arguments)
 {
     return(ConstructorCall.StackConstructor(typeName, arguments).Build());
 }
Exemplo n.º 9
0
 public static IExpression StackConstructor(NameReference typeName)
 {
     return(ConstructorCall.StackConstructor(typeName).Build());
 }