コード例 #1
0
        public IInterpreter OptionalNoLimitsVariadicFunction()
        {
            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("provider", NameFactory.ChunkNameReference(NameFactory.Int64NameReference()),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("x", null,
                                                                                                  ExpressionFactory.StackConstructor(NameFactory.ChunkNameReference(NameFactory.Int64NameReference()),
                                                                                                                                     FunctionArgument.Create(NatLiteral.Create("2")))),
                                                              Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("x"), FunctionArgument.Create(NatLiteral.Create("0"))),
                                                                                         Int64Literal.Create("-6")),
                                                              Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("x"), FunctionArgument.Create(NatLiteral.Create("1"))),
                                                                                         Int64Literal.Create("8")),
                                                              Return.Create(NameReference.Create("x"))
                                                              )));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "sum",
                                       ExpressionReadMode.ReadRequired,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    // let i0 = n.at(0)
                    VariableDeclaration.CreateStatement("i0", null, FunctionCall.Create(NameReference.Create("n", NameFactory.PropertyIndexerName),
                                                                                        FunctionArgument.Create(NatLiteral.Create("0")))),
                    // let i1 = n.at(1)
                    VariableDeclaration.CreateStatement("i1", null, FunctionCall.Create(NameReference.Create("n", NameFactory.PropertyIndexerName),
                                                                                        FunctionArgument.Create(NatLiteral.Create("1")))),
                    // return i0+i1
                    Return.Create(ExpressionFactory.Add("i0", "i1"))
                }))
                                   .Parameters(FunctionParameter.Create("n", NameFactory.Int64NameReference(), Variadic.Create(),
                                                                        FunctionCall.Create(NameReference.Create("provider")), isNameRequired: false)));

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

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
コード例 #2
0
        public IInterpreter StoringSequenceAsSequence()
        {
            var interpreter = new Interpreter.Interpreter();

            foreach (var mutability in Options.AllMutabilityModes)
            {
                // todo: add analogous test with storing iterable as sequence

                // yes, this test seems like pointless thing but it is important nevertheless because
                // we have to check if conversion does NOT happen
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Nat8NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("x", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.ChunkNameReference(NameFactory.Int64NameReference()),
                                                                                          FunctionArgument.Create(NatLiteral.Create("2"))),
                                                        env.Options.ReassignableModifier()),
                    ExpressionFactory.InitializeIndexable("x", Int64Literal.Create("-6"), Int64Literal.Create("8")),
                    // conversion should NOT happen
                    VariableDeclaration.CreateStatement("stored", null,
                                                        FunctionCall.Create(NameFactory.StoreFunctionReference(), NameReference.Create("x"))),
                    Return.Create(ExpressionFactory.Ternary(IsSame.Create("x", "stored"), Nat8Literal.Create("2"), Nat8Literal.Create("7")))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
コード例 #3
0
        public IInterpreter StackChunkWithBasicPointers()
        {
            var interpreter = new Interpreter.Interpreter();

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

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("chicken", null,
                                                        ExpressionFactory.StackConstructor(NameFactory.ChunkNameReference(NameFactory.PointerNameReference(NameFactory.Int64NameReference())),
                                                                                           FunctionArgument.Create(NatLiteral.Create("2")))),
                    Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("0")),
                                               ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("-6"))),
                    Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("1")),
                                               ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("8"))),
                    Return.Create(ExpressionFactory.Add(FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("0")),
                                                        FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("1"))))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }