Exemplo n.º 1
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.º 2
0
        public IErrorReporter ErrorTestingValueType()
        {
            NameResolver resolver = null;

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

                IsType is_type  = IsType.Create(NameReference.Create("foo"), NameFactory.RealNameReference());
                var    decl_src = VariableDeclaration.CreateStatement("foo", NameFactory.IObjectNameReference(), initValue: Undef.Create(), modifier: EntityModifier.Public);
                var    decl_dst = VariableDeclaration.CreateStatement("bar", null, initValue: is_type, modifier: EntityModifier.Public);
                root_ns.AddNode(decl_src);
                root_ns.AddNode(decl_dst);

                IsType is_type_ref = IsType.Create(NameReference.Create("u"), NameFactory.ISequenceNameReference("G"));
                root_ns.AddBuilder(FunctionBuilder.Create("more", "G", VarianceMode.None,
                                                          NameFactory.UnitNameReference(),
                                                          Block.CreateStatement(
                                                              ExpressionFactory.Readout(is_type_ref)
                                                              ))
                                   .Parameters(FunctionParameter.Create("u", NameFactory.ReferenceNameReference(
                                                                            NameFactory.ISequenceNameReference("G", mutability: TypeMutability.ReadOnly)))));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.IsTypeOfKnownTypes, is_type));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.IsTypeOfKnownTypes, is_type_ref));
            }

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

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true,
                    // we test this option here or more precisely duck typing
                    InterfaceDuckTyping = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                // please note it is not the interface is empty (it is not because IObject is not empty),
                // but it does not add anything, so in this sense it is empty
                root_ns.AddBuilder(TypeBuilder.CreateInterface("IWhat"));

                root_ns.AddBuilder(FunctionBuilder.Create("foo",
                                                          NameFactory.UnitNameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("x", NameFactory.PointerNameReference(NameFactory.IObjectNameReference()), Undef.Create()),
                                                              // should be legal despite duck typing, i.e. we should not error that the types are exchangable
                                                              // they are in sense of duck typing but checking if the type IS another type should be duck-free
                                                              ExpressionFactory.Readout(IsType.Create(NameReference.Create("x"), NameReference.Create("IWhat"))),
                                                              VariableDeclaration.CreateStatement("y", NameFactory.PointerNameReference(NameReference.Create("IWhat")), Undef.Create()),
                                                              ExpressionFactory.Readout(IsSame.Create(NameReference.Create("x"), NameReference.Create("y")))
                                                              )));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }