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

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

                Int64Literal value = Int64Literal.Create("3");
                root_ns.AddBuilder(FunctionBuilder.Create("foo",
                                                          NameFactory.BoolNameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("x", null,
                                                                                                  ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("2"))),
                                                              Return.Create(IsSame.Create(NameReference.Create("x"), value))
                                                              )));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 2
0
        public IErrorReporter ErrorIsTypeAlienSealed()
        {
            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.CreateInterface("IWhat"));

                root_ns.AddBuilder(TypeBuilder.Create("What")
                                   .Parents("IWhat"));

                // comparison does not make sense, because string is sealed and it is not possible to be given interface
                IsType is_type = IsType.Create(NameReference.Create("x"), NameFactory.StringNameReference());
                root_ns.AddBuilder(FunctionBuilder.Create("foo",
                                                          NameFactory.BoolNameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("x", NameFactory.PointerNameReference("IWhat"), Undef.Create()),
                                                              Return.Create(is_type)
                                                              )));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 3
0
        public IErrorReporter ScopeShadowing()
        {
            NameResolver resolver = null;
            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options() { ScopeShadowing = true, DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability));
                var root_ns = env.Root;

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

                    Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("x",null,Int64Literal.Create("2")),
                    Block.CreateStatement(new IExpression[]{
                        // shadowing
                        VariableDeclaration.CreateStatement("x", null, BoolLiteral.CreateFalse()),
                        VariableDeclaration.CreateStatement("a",NameFactory.BoolNameReference(),NameReference.Create("x")),
                         ExpressionFactory.Readout("a"),
                    }),
                    VariableDeclaration.CreateStatement("b",NameFactory.Int64NameReference(),NameReference.Create("x")),
                     ExpressionFactory.Readout("b"),
                    })));

                resolver = NameResolver.Create(env);

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

            return resolver;
        }
Exemplo n.º 4
0
        public IErrorReporter ErrorUnusedExpression()
        {
            NameResolver resolver = null;

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

                var var_decl = VariableDeclaration.CreateStatement("x", NameFactory.BoolNameReference(), Undef.Create());
                var var_ref  = NameReference.Create("x");
                FunctionDefinition func_def = root_ns.AddBuilder(FunctionBuilder.Create(
                                                                     "foo",
                                                                     ExpressionReadMode.OptionalUse,
                                                                     NameFactory.BoolNameReference(),
                                                                     Block.CreateStatement(new IExpression[] {
                    var_decl,
                    var_ref,
                    Return.Create(BoolLiteral.CreateTrue())
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.AreEqual(ErrorCode.ExpressionValueNotUsed, resolver.ErrorManager.Errors.Single().Code);
                Assert.AreEqual(var_ref, resolver.ErrorManager.Errors.Single().Node);
            }

            return(resolver);
        }
Exemplo n.º 5
0
        public IErrorReporter ErrorSelfAssignment()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("Oint")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(Property.Create(env.Options, "x", NameFactory.Int64NameReference(),
                                                         new[] { Property.CreateAutoField(NameFactory.PropertyAutoField, NameFactory.Int64NameReference(), null, env.Options.ReassignableModifier()) },
                                                         new[] { Property.CreateAutoGetter(NameFactory.PropertyAutoField, NameFactory.Int64NameReference()) },
                                                         new[] { Property.CreateAutoSetter(NameFactory.PropertyAutoField, NameFactory.Int64NameReference()) })));

                var var_decl = VariableDeclaration.CreateStatement("x", NameFactory.BoolNameReference(),
                                                                   Undef.Create(), env.Options.ReassignableModifier());

                IExpression assign_var  = Assignment.CreateStatement(NameReference.Create("x"), NameReference.Create("x"));
                IExpression assign_prop = Assignment.CreateStatement(NameReference.Create("a", "x"), NameReference.Create("a", "x"));

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

                                       Block.CreateStatement(new IExpression[] {
                    var_decl,
                    assign_var,
                    VariableDeclaration.CreateStatement("a", null, ExpressionFactory.StackConstructor("Oint")),
                    VariableDeclaration.CreateStatement("b", null, ExpressionFactory.StackConstructor("Oint")),
                    Assignment.CreateStatement(NameReference.Create("a", "x"), NameReference.Create("b", "x")),
                    assign_prop,
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.SelfAssignment, assign_var));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.SelfAssignment, assign_prop));
            }

            return(resolver);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        public IInterpreter ResolvingGenericArgumentInRuntime()
        {
            var interpreter = new Interpreter.Interpreter();

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

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

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

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemplo n.º 8
0
        public IErrorReporter ErrorIfScope()
        {
            NameResolver resolver = null;

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

                NameReference bad_ref = NameReference.Create("x");

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "testing",
                                       NameFactory.UnitNameReference(),

                                       Block.CreateStatement(
                                           VariableDeclaration.CreateStatement("b", NameFactory.BoolNameReference(), Undef.Create(),
                                                                               env.Options.ReassignableModifier()),

                                           IfBranch.CreateIf(VariableDeclaration.CreateExpression("x", null, BoolLiteral.CreateTrue()),
                                                             // x is in scope
                                                             Assignment.CreateStatement("b", "x"),
                                                             IfBranch.CreateElse(
                                                                 // x is in scope as well
                                                                 Assignment.CreateStatement("b", "x"))),

                                           // here x is not in the scope (is already removed)
                                           Assignment.CreateStatement(NameReference.Create("b"), bad_ref),
                                           ExpressionFactory.Readout("b")
                                           )));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 9
0
        public IErrorReporter ErrorInvalidConverters()
        {
            NameResolver resolver = null;

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

                FunctionDefinition conv1 = FunctionBuilder.Create(NameFactory.ConvertFunctionName,
                                                                  NameFactory.Int64NameReference(),
                                                                  Block.CreateStatement(Return.Create(Int64Literal.Create("3"))));
                FunctionDefinition conv2 = FunctionBuilder.Create(NameFactory.ConvertFunctionName,
                                                                  ExpressionReadMode.OptionalUse,
                                                                  NameFactory.BoolNameReference(),
                                                                  Block.CreateStatement(Return.Create(Undef.Create())))
                                           .SetModifier(EntityModifier.Pinned);
                FunctionDefinition conv3 = FunctionBuilder.Create(NameFactory.ConvertFunctionName,
                                                                  NameFactory.StringPointerNameReference(),
                                                                  Block.CreateStatement(Return.Create(Undef.Create())))
                                           .SetModifier(EntityModifier.Pinned)
                                           .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead));

                root_ns.AddBuilder(TypeBuilder.Create("Start")
                                   .SetModifier(EntityModifier.Base)
                                   .With(conv1)
                                   .With(conv2)
                                   .With(conv3));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(3, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ConverterNotPinned, conv1));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ConverterDeclaredWithIgnoredOutput, conv2));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ConverterWithParameters, conv3));
            }

            return(resolver);
        }
Exemplo n.º 10
0
        /*private TypeBuilder WithBaseEnum(string typename)
         * {
         *  if (this.build != null)
         *      throw new InvalidOperationException();
         *
         *  NameReference typename_ref = NameReference.Create(typename);
         *  this.embedTypeNames.Add(typename_ref);
         *  this.With(FunctionDefinition.CreateInitConstructor(EntityModifier.Native | EntityModifier.Implicit,
         *       new[] { FunctionParameter.Create(NameFactory.SourceConvConstructorParameter, typename_ref,
         *              ExpressionReadMode.CannotBeRead) },
         *       Block.CreateStatement()));
         *
         *  return this;
         * }*/
        public static TypeBuilder CreateEnum(string name)
        {
            TypeBuilder builder = new TypeBuilder(NameDefinition.Create(name));

            builder = builder
                      .SetModifier(EntityModifier.Enum)
                      .Parents(NameFactory.IEquatableNameReference())
                      .With(FunctionDefinition.CreateInitConstructor(EntityModifier.Native | EntityModifier.Private,
                                                                     new[] { FunctionParameter.Create(NameFactory.EnumConstructorParameter, NameFactory.NatNameReference(),
                                                                                                      ExpressionReadMode.CannotBeRead) },
                                                                     Block.CreateStatement()))
                      // copy constructor
                      .With(FunctionDefinition.CreateInitConstructor(EntityModifier.Native,
                                                                     new[] { FunctionParameter.Create(NameFactory.SourceCopyConstructorParameter, NameReference.Create(name),
                                                                                                      ExpressionReadMode.CannotBeRead) },
                                                                     Block.CreateStatement()))
                      .With(FunctionBuilder.Create(NameFactory.ConvertFunctionName, ExpressionReadMode.ReadRequired, NameFactory.NatNameReference(),
                                                   Block.CreateStatement())
                            .SetModifier(EntityModifier.Native))
                      // when enum inherits an enum it won't call super to check equality
                      .WithEquatableEquals(EntityModifier.UnchainBase)
                      .With(FunctionBuilder.Create(NameFactory.EqualOperator,
                                                   ExpressionReadMode.ReadRequired, NameFactory.BoolNameReference(),
                                                   Block.CreateStatement(new[] {
                Return.Create(Undef.Create())
            }))
                            .SetModifier(EntityModifier.Native)
                            .Parameters(FunctionParameter.Create("cmp", builder.CreateTypeNameReference(TypeMutability.ReadOnly), ExpressionReadMode.CannotBeRead)))
                      .With(FunctionBuilder.Create(NameFactory.NotEqualOperator,
                                                   ExpressionReadMode.ReadRequired, NameFactory.BoolNameReference(),
                                                   Block.CreateStatement(new[] {
                Return.Create(Undef.Create())
            }))
                            .SetModifier(EntityModifier.Native)
                            .Parameters(FunctionParameter.Create("cmp", builder.CreateTypeNameReference(TypeMutability.ReadOnly), ExpressionReadMode.CannotBeRead)))
            ;

            return(builder);
        }
Exemplo n.º 11
0
        public IErrorReporter ProperBasicMethodOverride()
        {
            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("IX")
                                   .With(FunctionBuilder.CreateDeclaration(
                                             "bar",
                                             ExpressionReadMode.OptionalUse,
                                             NameFactory.PointerNameReference(NameFactory.IObjectNameReference()))
                                         .Parameters(FunctionParameter.Create("x", NameFactory.BoolNameReference(), Variadic.None, null, isNameRequired: false)))
                                   .SetModifier(EntityModifier.Interface));

                TypeDefinition type_impl = 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))
                                                                    .SetModifier(EntityModifier.Override))
                                                              .Parents(NameReference.Create("IX")));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 12
0
        public IErrorReporter ErrorMixingSlicingTypes()
        {
            NameResolver resolver = null;

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

                INameReference typename = NameReferenceUnion.Create(new[] {
                    NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()),
                    NameFactory.BoolNameReference()
                });
                var decl          = VariableDeclaration.CreateStatement("foo", typename, initValue: Undef.Create());
                var func_def_void = root_ns.AddBuilder(FunctionBuilder.Create(
                                                           "notimportant",
                                                           ExpressionReadMode.OptionalUse,
                                                           NameFactory.UnitNameReference(),

                                                           Block.CreateStatement(new[] {
                    decl,
                    ExpressionFactory.Readout("foo")
                })));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 13
0
        public IErrorReporter ErrorCastingToSet()
        {
            NameResolver resolver = null;

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

                NameReferenceUnion type_set = NameReferenceUnion.Create(
                    NameFactory.PointerNameReference(NameFactory.BoolNameReference()),
                    NameFactory.PointerNameReference(NameFactory.Int64NameReference()));
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "foo", null,
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.UnitNameReference(),

                                       Block.CreateStatement(new[] {
                    VariableDeclaration.CreateStatement("x", NameFactory.IObjectNameReference(), Undef.Create()),
                    VariableDeclaration.CreateStatement("c", null, ExpressionFactory.DownCast(NameReference.Create("x"), type_set)),
                    ExpressionFactory.Readout("c"),
                })));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 14
0
        public IErrorReporter ErrorReadingFunctionVoidResult()
        {
            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;

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "foo",
                                       ExpressionReadMode.CannotBeRead,
                                       NameFactory.BoolNameReference(),
                                       Block.CreateStatement(new[] {
                    Return.Create(BoolLiteral.CreateTrue())
                }))
                                   .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), usageMode: ExpressionReadMode.CannotBeRead)));

                root_ns.AddNode(VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), Undef.Create()));
                var call = FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(NameReference.Create("i")));
                VariableDeclaration decl = VariableDeclaration.CreateStatement("x", NameFactory.BoolNameReference(),
                                                                               call, EntityModifier.Public);
                root_ns.AddNode(decl);

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 15
0
        public IErrorReporter ErrorCompoundDefaultValue()
        {
            NameResolver resolver = null;

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

                var decl = root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameReferenceUnion.Create(
                                                                                   new[] { NameFactory.PointerNameReference(NameFactory.BoolNameReference()),
                                                                                           NameFactory.PointerNameReference(NameFactory.Int64NameReference()) }),
                                                                               initValue: null, modifier: EntityModifier.Public));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemplo n.º 16
0
        public IInterpreter DereferenceOnIfCondition()
        {
            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[] {
                    VariableDeclaration.CreateStatement("ptr", NameFactory.PointerNameReference(NameFactory.BoolNameReference()),
                                                        ExpressionFactory.HeapConstructor(NameFactory.BoolNameReference(), FunctionArgument.Create(BoolLiteral.CreateTrue()))),
                    Return.Create(IfBranch.CreateIf(NameReference.Create("ptr"), new[] { Int64Literal.Create("2") },
                                                    IfBranch.CreateElse(new[] { Int64Literal.Create("5") }))),
                })));


                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemplo n.º 17
0
        public IErrorReporter TranslationTableOfInferredCommonTypes()
        {
            NameResolver resolver = null;

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

                TemplateParameter template_param = TemplateParametersBuffer.Create("T").Values.Single();
                root_ns.AddBuilder(FunctionBuilder.Create("getMe", new[] { template_param },
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.UnitNameReference(), Block.CreateStatement())
                                   .Parameters(FunctionParameter.Create("a", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead),
                                               FunctionParameter.Create("b", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead)));

                FunctionCall call = FunctionCall.Create(NameReference.Create("getMe"), NameReference.Create("x"), NameReference.Create("y"));
                root_ns.AddBuilder(FunctionBuilder.Create("common",
                                                          NameFactory.UnitNameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("x", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()),
                                                                                                  Int64Literal.Create("3")),
                                                              VariableDeclaration.CreateStatement("y", NameFactory.ReferenceNameReference(NameFactory.BoolNameReference()),
                                                                                                  BoolLiteral.CreateTrue()),
                                                              call
                                                              )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
                // the actual point of this test are those two lines checking if we get correct translation table for entire
                // instance of the called function
                Assert.IsTrue(call.Resolution.TargetFunctionInstance.Translation.Translate(template_param,
                                                                                           out IEntityInstance common_instance));
                Assert.AreEqual(resolver.Context.Env.IEquatableType.InstanceOf, common_instance);
            }

            return(resolver);
        }
Exemplo n.º 18
0
 public static TypeBuilder WithEquatableEquals(this TypeBuilder builder, EntityModifier modifier = null)
 {
     return(builder.With(FunctionBuilder.Create(NameFactory.EqualOperator,
                                                ExpressionReadMode.ReadRequired, NameFactory.BoolNameReference(),
                                                Block.CreateStatement(
                                                    IfBranch.CreateIf(IsSame.Create(NameReference.CreateThised(), NameReference.Create("cmp")),
                                                                      new[] { Return.Create(BoolLiteral.CreateTrue()) }),
                                                    // let obj = cmp cast? Self
                                                    VariableDeclaration.CreateStatement("obj", null, ExpressionFactory.CheckedSelfCast("cmp",
                                                                                                                                       NameFactory.ReferenceNameReference(builder.CreateTypeNameReference(TypeMutability.ReadOnly)))),
                                                    // return this==obj.value
                                                    Return.Create(ExpressionFactory.IsEqual(NameReference.Create(NameFactory.ThisVariableName),
                                                                                            NameReference.Create("obj")))))
                         .SetModifier(EntityModifier.Override | modifier)
                         .Parameters(FunctionParameter.Create("cmp",
                                                              NameFactory.ReferenceNameReference(NameFactory.IEquatableNameReference(TypeMutability.ReadOnly))))));
 }
Exemplo n.º 19
0
Arquivo: Flow.cs Projeto: macias/Skila
        public IInterpreter ShortcutComputationInOptionalDeclaration()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                // purpose: check if RHS of the optional declaration is computed only when it is needed
                // here we count RHS computations and since we declare two variables
                // let (x,y) =? (None,Some)
                // Some should not be executed, because `x` assigment fails first
                var env = Language.Environment.Create(new Options()
                {
                    DebugThrowOnError = true,
                    DiscardingAnyExpressionDuringTests = true,
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Mutator")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(VariableDeclaration.CreateStatement("c", NameFactory.IntNameReference(), null,
                                                                             env.Options.ReassignableModifier() | EntityModifier.Public)));

                // return Some or None depending on the `f` parameter, and also increments the count of option evaluations
                root_ns.AddBuilder(FunctionBuilder.Create("give", NameFactory.OptionNameReference(NameFactory.Nat8NameReference()),
                                                          Block.CreateStatement(
                                                              ExpressionFactory.Inc(() => NameReference.Create("m", "c")),
                                                              Return.Create(ExpressionFactory.Ternary(NameReference.Create("f"),
                                                                                                      ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("11")),
                                                                                                      ExpressionFactory.OptionEmpty(NameFactory.Nat8NameReference())))
                                                              ))
                                   .Parameters(FunctionParameter.Create("f", NameFactory.BoolNameReference()),
                                               FunctionParameter.Create("m", NameFactory.ReferenceNameReference(NameReference.Create("Mutator")))));

                IExpression opt_declaration = ExpressionFactory.OptionalDeclaration(new[] {
                    VariablePrototype.Create("x", NameFactory.Nat8NameReference()),
                    VariablePrototype.Create("y", NameFactory.Nat8NameReference())
                }, new[] {
                    FunctionCall.Create("give", BoolLiteral.CreateFalse(), NameReference.Create("mut")),
                    FunctionCall.Create("give", BoolLiteral.CreateTrue(), NameReference.Create("mut")),
                });

                root_ns.AddBuilder(FunctionBuilder.Create("main",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.Nat8NameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("mut", null, ExpressionFactory.StackConstructor(NameReference.Create("Mutator"))),

                                                              IfBranch.CreateIf(opt_declaration,
                                                                                new[] {
                    ExpressionFactory.Readout("x"),
                    ExpressionFactory.Readout("y"),
                    ExpressionFactory.GenericThrow(),
                },
                                                                                IfBranch.CreateElse(
                                                                                    // crucial check -- we should not evaluate the second option
                                                                                    ExpressionFactory.AssertEqual(IntLiteral.Create("1"), NameReference.Create("mut", "c"))
                                                                                    )),

                                                              Return.Create(Nat8Literal.Create("0"))
                                                              )));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemplo n.º 20
0
 private BoolLiteral(bool value)
     : base(value ? "true" : "false", NameFactory.BoolNameReference(  ))
 {
     this.Value = value;
 }