Exemple #1
0
        private void createNewConstructors()
        {
            NameReference type_name = this.Name.CreateNameReference(null, this.InstanceOf, isLocal: true);

            if (!this.NestedFunctions.Any(it => it.IsNewConstructor()))
            {
                foreach (FunctionDefinition init_cons in this.NestedFunctions.Where(it => it.IsInitConstructor()).StoreReadOnly())
                {
                    //if (this.NestedFunctions.Any(it => it.IsNewConstructor()
                    //      && it.Name.Arity == init_cons.Name.Arity && it.NOT_USED_CounterpartParameters(init_cons)))
                    //continue;

                    const string local_this = "__this__";

                    var new_cons = FunctionDefinition.CreateHeapConstructor(init_cons.Modifier, init_cons.Parameters,
                                                                            type_name,
                                                                            Block.CreateStatement(new IExpression[] {
                        VariableDeclaration.CreateStatement(local_this, null, Alloc.Create(type_name, useHeap: true)),
                        FunctionCall.Create(NameReference.Create(NameReference.Create(local_this), NameFactory.InitConstructorName),
                                            init_cons.Parameters.Select(it => FunctionArgument.Create(it.Name.CreateNameReference())).ToArray()),
                        Return.Create(NameReference.Create(local_this))
                    }));

                    this.AddNode(new_cons);
                }
            }
        }
Exemple #2
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"))
                })));
Exemple #3
0
        public IInterpreter InitializingWithGetter()
        {
            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")
                                   .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, ExpressionFactory.StackConstructor(NameReference.Create("Point"))),
                    Return.Create(NameReference.Create(NameReference.Create("p"), "x"))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #4
0
        public IErrorReporter ErrorStoredLocalReferenceEscapesFromFunction()
        {
            NameResolver resolver = null;

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

                Return ret = Return.Create(NameReference.Create("o"));

                FunctionDefinition func = root_ns.AddBuilder(FunctionBuilder.Create("notimportant",
                                                                                    ExpressionReadMode.OptionalUse,
                                                                                    NameFactory.OptionNameReference(NameFactory.ReferenceNameReference(NameFactory.IntNameReference())),

                                                                                    Block.CreateStatement(
                                                                                        VariableDeclaration.CreateStatement("h", null, IntLiteral.Create("3")),
                                                                                        VariableDeclaration.CreateStatement("o", null,
                                                                                                                            ExpressionFactory.StackConstructor(
                                                                                                                                NameFactory.OptionNameReference(NameFactory.ReferenceNameReference(NameFactory.IntNameReference())),
                                                                                                                                NameReference.Create("h"))),
                                                                                        // error, we would effectively escape with reference to local variable
                                                                                        ret
                                                                                        )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, ret.Expr));
            }

            return(resolver);
        }
Exemple #5
0
        public IInterpreter RawMethods()
        {
            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;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    Return.Create(FunctionCall.Create(NameReference.Create(Int64Literal.Create("1"), NameFactory.AddOperator),
                                                      FunctionArgument.Create(Int64Literal.Create("1"))))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #6
0
        public IErrorReporter ErrorIgnoringFunctionResult()
        {
            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 func_def = root_ns.AddBuilder(FunctionBuilder.Create(
                                                      "foo",
                                                      ExpressionReadMode.ReadRequired,
                                                      NameFactory.RealNameReference(),
                                                      Block.CreateStatement(new[] {
                    Return.Create(RealLiteral.Create("3.3"))
                }))
                                                  .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")));
                root_ns.AddNode(Block.CreateStatement(new[] { call }));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #7
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);
        }
Exemple #8
0
        public IInterpreter ChannelDeadLockOnReceive()
        {
            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(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("ch", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.ChannelNameReference(NameFactory.Int64NameReference()))),
                    ExpressionFactory.Readout(FunctionCall.Create(NameReference.Create("ch", NameFactory.ChannelReceive))),
                    Return.Create(Int64Literal.Create("0"))
                })));

                int task_id = Task.WaitAny(Task.Delay(TimeSpan.FromSeconds(Interpreter.Interpreter.TimeoutSeconds)), interpreter.TestRunAsync(env));
                Assert.AreEqual(0, task_id);
            }

            return(interpreter);
        }
Exemple #9
0
        public IInterpreter StringToInt()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = 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("s", null, StringLiteral.Create("2")),
                    VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), ExpressionFactory.GetOptionValue(
                                                            FunctionCall.Create(NameReference.Create(NameFactory.Int64NameReference(), NameFactory.ParseFunctionName),
                                                                                NameReference.Create("s"))
                                                            )),
                    Return.Create(NameReference.Create("i"))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #10
0
        public IInterpreter NoMutability()
        {
            var env = Environment.Create(new Options()
            {
                DebugThrowOnError = true
            }
                                         .SetMutability(MutabilityModeOption.OnlyAssignability));
            var root_ns = env.Root;

            root_ns.AddBuilder(FunctionBuilder.Create(
                                   "main",
                                   ExpressionReadMode.OptionalUse,
                                   NameFactory.Nat8NameReference(),
                                   Block.CreateStatement(
                                       VariableDeclaration.CreateStatement("a", NameFactory.Nat8NameReference(TypeMutability.ForceMutable),
                                                                           Nat8Literal.Create("2"), env.Options.ReassignableModifier()),
                                       VariableDeclaration.CreateStatement("b", NameFactory.Nat8NameReference(TypeMutability.ForceConst),
                                                                           Nat8Literal.Create("13")),
                                       Assignment.CreateStatement(NameReference.Create("a"), NameReference.Create("b")),
                                       Return.Create(NameReference.Create("a"))
                                       )));

            var       interpreter = new Interpreter.Interpreter();
            ExecValue result      = interpreter.TestRun(env);

            Assert.AreEqual((byte)13, result.RetValue.PlainValue);

            return(interpreter);
        }
Exemple #11
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);
        }
Exemple #12
0
        public IInterpreter OldSchoolSwapPointers()
        {
            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"))));

                VariableDeclaration decl_a = VariableDeclaration.CreateStatement("a", null,
                                                                                 ExpressionFactory.HeapConstructor(env.Options.ReassignableTypeMutability(),
                                                                                                                   NameFactory.Nat8NameReference(),
                                                                                                                   Nat8Literal.Create("2")));

                FunctionCall swap_call = FunctionCall.Create("swap", AddressOf.CreateReference(NameReference.Create("a")),
                                                             AddressOf.CreateReference(NameReference.Create("b")));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(
                                           decl_a,
                                           VariableDeclaration.CreateStatement("b", null,
                                                                               ExpressionFactory.HeapConstructor(env.Options.ReassignableTypeMutability(),
                                                                                                                 NameFactory.Nat8NameReference(),
                                                                                                                 Nat8Literal.Create("17"))),

                                           VariableDeclaration.CreateStatement("c", null, NameReference.Create("a")),

                                           swap_call,

                                           // here `c` still points to value of original `a`

                                           Return.Create(ExpressionFactory.Sub("a", "c"))
                                           )));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #13
0
        public IErrorReporter ErrorUsingFunctionAsProperty()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                // when writing this test compiler crashed when the function was called like a property
                var env = Skila.Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                NameReference func_name = NameReference.Create("b", NameFactory.IIterableCount);
                root_ns.AddBuilder(FunctionBuilder.Create("bad_call", NameFactory.SizeNameReference(), Block.CreateStatement(
                                                              // using function like a property (error)
                                                              // todo: however it should be another error, because this reference should create functor and the error should
                                                              // say about type mismatch between returning value and result type
                                                              Return.Create(func_name)))
                                   .Parameters(FunctionParameter.Create("b", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()),
                                                                        Variadic.Create(2, 3), null, false))
                                   .Include(NameFactory.LinqExtensionReference()));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #14
0
        public IInterpreter ResultTypeInference()
        {
            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;

                IExpression lambda = FunctionBuilder.CreateLambda(null,
                                                                  Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })).Build();
                root_ns.AddBuilder(FunctionBuilder.Create("main",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.Int64NameReference(),
                                                          Block.CreateStatement(new IExpression[] {
                    // f = () => x
                    VariableDeclaration.CreateStatement("f", null, lambda),
                    // return f()
                    Return.Create(FunctionCall.Create(NameReference.Create("f")))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #15
0
        private IErrorReporter errorDuckTypingValues(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")
                                   .SetModifier(env.Options.InterfaceDuckTyping ? EntityModifier.Interface : EntityModifier.Protocol));

                root_ns.AddBuilder(TypeBuilder.Create("X"));

                IExpression init_value = ExpressionFactory.StackConstructor(NameReference.Create("X"));
                // even with duck typing we cannot make the assigment because slicing is forbidden in all cases
                VariableDeclaration decl = VariableDeclaration.CreateStatement("i", NameReference.Create("IX"), init_value);
                var main_func            = root_ns.AddBuilder(FunctionBuilder.Create(
                                                                  "main",
                                                                  ExpressionReadMode.OptionalUse,
                                                                  NameFactory.Int64NameReference(),
                                                                  Block.CreateStatement(new IExpression[] {
                    decl,
                    ExpressionFactory.Readout("i"),
                    Return.Create(Int64Literal.Create("2"))
                })));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #16
0
        public IInterpreter RealDividingByZeroWithoutNaNs()
        {
            var interpreter = new Interpreter.Interpreter();

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

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Nat8NameReference(),
                                                       Block.CreateStatement(
                                                           VariableDeclaration.CreateStatement("a", null, Real64Literal.Create(5.0)),
                                                           VariableDeclaration.CreateStatement("b", null, Real64Literal.Create(0.0)),
                                                           ExpressionFactory.Readout(ExpressionFactory.Divide("a", "b")),
                                                           Return.Create(Nat8Literal.Create("2"))
                                                           )));

                ExecValue result = interpreter.TestRun(env);

                Assert.IsTrue(result.IsThrow);
            }

            return(interpreter);
        }
Exemple #17
0
        public IErrorReporter ErrorMutabilityLaunderingOnReturn()
        {
            NameResolver resolver = null;

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

                Return ret = Return.Create(NameReference.Create("coll"));
                root_ns.AddBuilder(FunctionBuilder.Create("laundering", "T", VarianceMode.None,
                                                          NameFactory.ReferenceNameReference(NameFactory.ISequenceNameReference("T")),
                                                          Block.CreateStatement(new IExpression[] {
                    ret
                }))
                                   .Parameters(FunctionParameter.Create("coll",
                                                                        NameFactory.ReferenceNameReference(NameFactory.ISequenceNameReference("T", mutability: TypeMutability.ForceMutable)))));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #18
0
        public IInterpreter RealNotANumber()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                // at this point Skila adheres to the standard but maybe should raise an exception for every NaN
                // and this way remove them from the language (similarly to null pointers)
                // https://stackoverflow.com/questions/5394424/causes-for-nan-in-c-application-that-do-no-raise-a-floating-point-exception
                // https://stackoverflow.com/questions/2941611/can-i-make-gcc-tell-me-when-a-calculation-results-in-nan-or-inf-at-runtime
                var env = Environment.Create(new Options()
                {
                    DebugThrowOnError = true, AllowRealMagic = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Nat8NameReference(),
                                                       Block.CreateStatement(
                                                           VariableDeclaration.CreateStatement("a", null, Real64Literal.Create(double.NaN)),
                                                           VariableDeclaration.CreateStatement("b", null, Real64Literal.Create(double.NaN)),
                                                           Return.Create(ExpressionFactory.Ternary(ExpressionFactory.IsEqual("a", "b"),
                                                                                                   Nat8Literal.Create("15"), Nat8Literal.Create("2")))
                                                           )));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #19
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);
        }
Exemple #20
0
        public IInterpreter DateDayOfWeek()
        {
            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;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.NatNameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("d", null, ExpressionFactory.StackConstructor(NameFactory.DateNameReference(),
                                                                                                      // it is Friday
                                                                                                      Int16Literal.Create("2017"), Nat8Literal.Create("12"), Nat8Literal.Create("29"))),
                    VariableDeclaration.CreateStatement("i", null,
                                                        FunctionCall.ConvCall(NameReference.Create("d", NameFactory.DateDayOfWeekProperty), NameFactory.NatNameReference())),
                    Return.Create(NameReference.Create("i"))
                })));

                ExecValue result = interpreter.TestRun(env);

                Assert.AreEqual(5UL, result.RetValue.PlainValue);
            }

            return(interpreter);
        }
Exemple #21
0
        public IErrorReporter ErrorDereferencingValue()
        {
            NameResolver resolver = null;

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

                Int64Literal value = Int64Literal.Create("3");
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "foo", null,
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new[] {
                    Return.Create(Dereference.Create(value)),
                })));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #22
0
        public IErrorReporter Basics()
        {
            NameResolver resolver = null;

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

                var func_def = FunctionBuilder.Create(
                    "foo",
                    ExpressionReadMode.OptionalUse,
                    NameFactory.RealNameReference(),
                    Block.CreateStatement(new[] { Return.Create(RealLiteral.Create("3.3")) }));

                var type_def = root_ns.AddBuilder(TypeBuilder.Create("Foo").With(func_def));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #23
0
        public IErrorReporter ErrorTemporaryValueReferenceEscapesFromFunction()
        {
            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("Hi")
                                   .With(FunctionBuilder.Create("give", NameFactory.UnitNameReference(), Block.CreateStatement())));

                Return ret = Return.Create(ExpressionFactory.StackConstructor("Hi"));

                FunctionDefinition func = root_ns.AddBuilder(FunctionBuilder.Create("notimportant",
                                                                                    ExpressionReadMode.OptionalUse,
                                                                                    NameFactory.ReferenceNameReference("Hi"),

                                                                                    Block.CreateStatement(
                                                                                        ret
                                                                                        )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, ret.Expr));
            }

            return(resolver);
        }
Exemple #24
0
        public IErrorReporter ErrorCallingConstructorFromBody()
        {
            NameResolver resolver = null;

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

                FunctionCall constructor_call = FunctionCall.Create(NameReference.Create(NameFactory.ThisVariableName,
                                                                                         NameFactory.InitConstructorName));
                var type_def = root_ns.AddBuilder(TypeBuilder.Create("Foo")
                                                  .SetModifier(EntityModifier.Base)
                                                  .With(FunctionDefinition.CreateInitConstructor(EntityModifier.None, null,
                                                                                                 Block.CreateStatement()))
                                                  .With(FunctionBuilder.Create("foo", null,
                                                                               ExpressionReadMode.OptionalUse,
                                                                               NameFactory.RealNameReference(),
                                                                               Block.CreateStatement(new IExpression[] {
                    constructor_call,
                    Return.Create(RealLiteral.Create("3.3"))
                }))
                                                        .SetModifier(EntityModifier.Base)));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #25
0
        public IErrorReporter ErrorNothingToOverride()
        {
            NameResolver resolver = null;

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

                FunctionDefinition function = FunctionBuilder.Create("getSome",
                                                                     ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                     Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("3"))
                }))
                                              .SetModifier(EntityModifier.Override);
                root_ns.AddBuilder(TypeBuilder.Create("GetPos")
                                   .With(function));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #26
0
        public IInterpreter InstanceCallStaticDispatch()
        {
            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;

                Extension ext = root_ns.AddNode(Extension.Create());

                ext.AddBuilder(FunctionBuilder.Create("paf", NameFactory.Nat8NameReference(), Block.CreateStatement(
                                                          Return.Create(ExpressionFactory.Mul("x", "x"))))
                               .Parameters(FunctionParameter.Create("x", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()),
                                                                    EntityModifier.This)));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("i", null, Nat8Literal.Create("5")),
                    Return.Create(FunctionCall.Create(NameReference.Create("i", "paf")))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #27
0
        public IInterpreter GenericTypeAliasing()
        {
            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(NameDefinition.Create("Point", "V", VarianceMode.None))
                                   .With(Alias.Create("VType", NameReference.Create("V"), EntityModifier.Public)));

                VariableDeclaration decl = VariableDeclaration.CreateStatement("x", NameReference.Create("p", "VType"), Undef.Create(),
                                                                               env.Options.ReassignableModifier());
                root_ns.AddBuilder(FunctionBuilder.Create("main", NameFactory.Nat8NameReference(), Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("p", null,
                                                                                                  ExpressionFactory.StackConstructor(NameReference.Create("Point", NameFactory.Nat8NameReference()))),
                                                              decl,
                                                              Assignment.CreateStatement(NameReference.Create("x"), Nat8Literal.Create("5")),
                                                              Return.Create(NameReference.Create("x"))
                                                              )));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Exemple #28
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);
        }
Exemple #29
0
        public IErrorReporter ErrorStaticMemberReference()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("Foo")
                                   .With(VariableDeclaration.CreateStatement("field", NameFactory.RealNameReference(), null,
                                                                             EntityModifier.Static | EntityModifier.Public)));

                NameReference field_ref = NameReference.Create("f", "field");
                root_ns.AddBuilder(FunctionBuilder.Create("foo",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.RealNameReference(),
                                                          Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("f", NameReference.Create("Foo"), Undef.Create()),
                    Return.Create(field_ref)
                })));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Exemple #30
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);
        }