Пример #1
0
        public IErrorReporter ErrorUnusedVariableWithinUsedExpression()
        {
            NameResolver resolver = null;

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

                VariableDeclaration decl = VariableDeclaration.CreateExpression("result", NameFactory.Int64NameReference(), initValue: Undef.Create());
                root_ns.AddBuilder(FunctionBuilder.Create("anything", null,
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.UnitNameReference(),

                                                          Block.CreateStatement(
                                                              // the declaration-expression is used, but the variable itself is not
                                                              // thus we report it as unused (in such code user should pass the init-value itself w/o creating variable)
                                                              ExpressionFactory.Readout(decl)
                                                              )));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Пример #2
0
        public IErrorReporter ErrorReservedKeyword()
        {
            NameResolver resolver = null;
            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability));
                var root_ns = env.Root;

                VariableDeclaration decl = VariableDeclaration.CreateExpression(NameFactory.RecurFunctionName, null, Int64Literal.Create("3"));
                root_ns.AddBuilder(FunctionBuilder.Create(
                    "anything", null,
                    ExpressionReadMode.OptionalUse,
                    NameFactory.UnitNameReference(),

                    Block.CreateStatement(new IExpression[] {
                     ExpressionFactory.Readout( decl)
                    })));

                resolver = NameResolver.Create(env);

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

            return resolver;
        }
Пример #3
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);
        }
Пример #4
0
        public static IExpression OptionalAssignment(IEnumerable <IExpression> lhsExpressions, IEnumerable <IExpression> rhsExpressions)
        {
            // todo: add support for spread
            if (lhsExpressions.Count() != rhsExpressions.Count())
            {
                throw new NotImplementedException();
            }

            // please note we could have dummy assignments in form
            // _ ?= x
            // in such case we are not interested in the assigment but the fact it was sucessful or not

            lhsExpressions = lhsExpressions.Select(lhs => lhs is NameReference lhs_name && lhs_name.IsSink ? null : lhs);

            var         temp_names = new List <string>();
            IExpression condition  = null;

            foreach (Tuple <IExpression, IExpression> pair in rhsExpressions.SyncZip(lhsExpressions))
            {
                IExpression rhs = pair.Item1;
                IExpression lhs = pair.Item2;

                IExpression opt;

                if (lhs == null)
                {
                    temp_names.Add(null);
                    opt = rhs;
                }
                else
                {
                    string temp = AutoName.Instance.CreateNew("optassign");
                    temp_names.Add(temp);
                    opt = VariableDeclaration.CreateExpression(temp, null, rhs);
                }


                IExpression curr = NameReference.Create(opt, BrowseMode.Decompose, NameFactory.OptionHasValue);

                if (condition == null)
                {
                    condition = curr;
                }
                else
                {
                    condition = And(condition, curr);
                }
            }

            var success_body = new List <IExpression>();
            {
                foreach (Tuple <IExpression, string> pair in lhsExpressions.SyncZip(temp_names))
                {
                    IExpression lhs  = pair.Item1;
                    string      temp = pair.Item2;

                    if (lhs != null)
                    {
                        success_body.Add(Assignment.CreateStatement(lhs,
                                                                    NameReference.Create(NameReference.Create(temp), BrowseMode.Decompose, NameFactory.OptionValue)));
                    }
                }

                success_body.Add(BoolLiteral.CreateTrue());
            }

            IfBranch result = IfBranch.CreateIf(condition,
                                                success_body,
                                                IfBranch.CreateElse(BoolLiteral.CreateFalse()));

            return(result);
        }