Пример #1
0
 public static void Serialize(this SerializingContainer2 sc, ref NameReference name)
 {
     if (sc.IsLoading)
     {
         name = sc.ms.ReadNameReference(sc.Pcc);
     }
     else
     {
         sc.ms.Writer.WriteNameReference(name, sc.Pcc);
     }
 }
Пример #2
0
        protected override void Serialize(SerializingContainer2 sc)
        {
            if (sc.IsLoading)
            {
                IsIndexed = !sc.ms.ReadBoolInt();
                sc.ms.Skip(-4);
            }
            else
            {
                //If there are no cells, IsIndexed has to be true, since there is no way to differentiate between
                //a cell count of zero and the extra zero that is present when IsIndexed is true.
                IsIndexed |= Cells.IsEmpty();
            }

            if (IsIndexed)
            {
                sc.SerializeConstInt(0);
            }

            int cellIndex = 0;

            //If IsIndexed, the index needs to be read and written, so just use the normal Serialize for ints.
            //If it's not indexed, we don't need to write anything, but the Dictionary still needs to be populated with a value
            sc.Serialize(ref Cells, IsIndexed ? (SCExt.SerializeDelegate <int>)SCExt.Serialize : (SerializingContainer2 sc2, ref int idx) => idx = cellIndex++, SCExt.Serialize);
            if (!IsIndexed)
            {
                sc.SerializeConstInt(0);
            }

            int count = ColumnNames?.Count ?? 0;

            sc.Serialize(ref count);
            if (sc.IsLoading)
            {
                int index = 0;
                ColumnNames = new List <NameReference>(count);
                for (int i = 0; i < count; i++)
                {
                    NameReference tmp = default;
                    sc.Serialize(ref tmp);
                    sc.Serialize(ref index);
                    ColumnNames.Add(tmp);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    NameReference tmp = ColumnNames[i];
                    sc.Serialize(ref tmp);
                    sc.Serialize(ref i);
                }
            }
        }
Пример #3
0
        public ConstructorCall Init(string member, IExpression initValue, out Assignment assignment)
        {
            if (this.build != null)
            {
                throw new InvalidOperationException();
            }

            assignment = Assignment.CreateInitialization(NameReference.Create(varReference, member), initValue);
            this.objectInitialization.Add(assignment);
            return(this);
        }
Пример #4
0
        public FunctionCall ConvertIndexerIntoSetter(IExpression rhs)
        {
            this.ChildrenNodes.WhereType <IOwnedNode>().ForEach(it => it.DetachFrom(this));
            NameReference idx_getter = this.Name;

            idx_getter.Prefix.DetachFrom(idx_getter);

            return(new FunctionCall(CallMode.Indexer,
                                    NameReference.Create(this.Name.Prefix, NameFactory.PropertySetter),
                                    this.UserArguments.Concat(FunctionArgument.Create(NameFactory.PropertySetterValueParameter, rhs)),
                                    requestedOutcomeType: null));
        }
Пример #5
0
        public IInterpreter InitializationWithOptionalAssignment()
        {
            var interpreter = new Interpreter.Interpreter();

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

                // this test is a bit tougher than regular opt.assignment, because variables will be
                // initialized for the first time with this assigment
                root_ns.AddBuilder(FunctionBuilder.Create("main",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.Nat8NameReference(),
                                                          Block.CreateStatement(
                                                              VariableDeclaration.CreateStatement("acc", null, Nat8Literal.Create("0"), env.Options.ReassignableModifier()),


                                                              VariableDeclaration.CreateStatement("x", null,
                                                                                                  ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("3"))),
                                                              VariableDeclaration.CreateStatement("z", null,
                                                                                                  ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("5"))),

                                                              VariableDeclaration.CreateStatement("a", NameFactory.Nat8NameReference(), null, env.Options.ReassignableModifier()),
                                                              VariableDeclaration.CreateStatement("b", NameFactory.Nat8NameReference(), null, env.Options.ReassignableModifier()),

                                                              IfBranch.CreateIf(ExpressionFactory.OptionalAssignment(
                                                                                    new[] { NameReference.Create("a"), NameReference.Create("b") },
                                                                                    new[] { NameReference.Create("x"), NameReference.Create("z") }),
                                                                                new[] {
                    // assign tracker should recognize the variable is initialized
                    ExpressionFactory.IncBy("acc", NameReference.Create("a")),
                },
                                                                                // making else branch a dead one
                                                                                IfBranch.CreateElse(ExpressionFactory.GenericThrow())),

                                                              // assign tracker should recognize the variable is initialized (because `else` branch of above `if` is dead)
                                                              ExpressionFactory.IncBy("acc", NameReference.Create("b")),

                                                              Return.Create(NameReference.Create("acc"))
                                                              )));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #6
0
        public IInterpreter MinLimitVariadicFunctionWithSpread()
        {
            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(
                                       "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(2), null, isNameRequired: false)));

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("x", null,
                                                        ExpressionFactory.HeapConstructor(NameFactory.ChunkNameReference(NameFactory.Int64NameReference()),
                                                                                          FunctionArgument.Create(NatLiteral.Create("2"))),
                                                        env.Options.ReassignableModifier()),
                    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(FunctionCall.Create(NameReference.Create("sum"),
                                                      FunctionArgument.Create(Spread.Create(NameReference.Create("x")))))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #7
0
        public IInterpreter CallingTraitMethod()
        {
            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;

                root_ns.AddBuilder(TypeBuilder.CreateInterface("ISay")
                                   .With(FunctionBuilder.CreateDeclaration("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())));

                root_ns.AddBuilder(TypeBuilder.Create("Say")
                                   .With(FunctionBuilder.Create("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))
                                         .SetModifier(EntityModifier.Override))
                                   .Parents("ISay"));

                root_ns.AddBuilder(TypeBuilder.Create("Greeter", "T"));

                root_ns.AddBuilder(TypeBuilder.Create("Greeter", "X")
                                   .SetModifier(EntityModifier.Trait)
                                   .Constraints(ConstraintBuilder.Create("X").Inherits("ISay"))
                                   .With(FunctionBuilder.Create("hello", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(
                                                                    Return.Create(FunctionCall.Create(NameReference.Create("s", "say")))
                                                                    ))
                                         .Parameters(FunctionParameter.Create("s", NameFactory.ReferenceNameReference("X")))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(
                                           VariableDeclaration.CreateStatement("g", null,
                                                                               ExpressionFactory.StackConstructor(NameReference.Create("Greeter", NameReference.Create("Say")))),
                                           VariableDeclaration.CreateStatement("y", null, ExpressionFactory.StackConstructor("Say")),
                                           Return.Create(FunctionCall.Create(NameReference.Create("g", "hello"), NameReference.Create("y")))
                                           )));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #8
0
        public override void Evaluate(ComputationContext ctx)
        {
            if (this.Evaluation == null)
            {
                this.Evaluation = ctx.Env.UnitEvaluation;

                NameReference   req_typename  = NameFactory.PointerNameReference(NameFactory.ExceptionNameReference());
                IEntityInstance eval_typename = req_typename.Evaluated(ctx, EvaluationCall.AdHocCrossJump);

                this.DataTransfer(ctx, ref this.expr, eval_typename);
            }
        }
Пример #9
0
        internal NameReference GetThisNameReference()
        {
            if (this.thisNameReference == null)
            {
                this.thisNameReference = NameReference.Create(null, NameFactory.ThisVariableName, null,
                                                              //this,
                                                              this.MetaThisParameter.InstanceOf, isLocal: true);
                this.thisNameReference.AttachTo(this);
            }

            return(this.thisNameReference);
        }
Пример #10
0
        public IInterpreter InheritingEnums()
        {
            var interpreter = new Interpreter.Interpreter();

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

                root_ns.AddBuilder(TypeBuilder.CreateEnum("Weekend")
                                   .With(EnumCaseBuilder.Create("Sat", "Sun"))
                                   .SetModifier(EntityModifier.Base));

                root_ns.AddBuilder(TypeBuilder.CreateEnum("First")
                                   .With(EnumCaseBuilder.Create("Mon"))
                                   .Parents("Weekend"));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.NatNameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    // let a Weekend = First.Sat
                    VariableDeclaration.CreateStatement("a", NameReference.Create("Weekend"),
                                                        // please note we only refer to "Sat" through "First", the type is still "Weekend"
                                                        NameReference.Create("First", "Sat")),
                    // var b First = Weekend.Sun
                    VariableDeclaration.CreateStatement("b", NameReference.Create("First"), NameReference.Create("Weekend", "Sun"),
                                                        env.Options.ReassignableModifier()),
                    // b = First.Mon
                    Assignment.CreateStatement(NameReference.Create("b"), NameReference.Create("First", "Mon")),
                    // let x = a to Nat; // 0
                    VariableDeclaration.CreateStatement("x", null, FunctionCall.ConvCall(NameReference.Create("a"),
                                                                                         NameFactory.NatNameReference())),
                    // let y = b to Nat; // 2
                    VariableDeclaration.CreateStatement("y", null, FunctionCall.ConvCall(NameReference.Create("b"),
                                                                                         NameFactory.NatNameReference())),
                    // return x + y
                    Return.Create(ExpressionFactory.Add(NameReference.Create("x"), NameReference.Create("y")))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #11
0
        public NameReference ReadNameRef()
        {
            if (Position + 8 > Size)
            {
                return(new NameReference());
            }
            var NameRef = new NameReference();

            NameRef.Index     = BitConverter.ToInt32(_data, _position(4));
            NameRef.ModNumber = BitConverter.ToInt32(_data, _position(4)) - 1;

            return(NameRef);
        }
Пример #12
0
 public static T TryGetTargetEntity <T>(this IExpression expr, out NameReference nameReference)
     where T : class, IEntity
 {
     nameReference = expr as NameReference;
     if (nameReference != null)
     {
         return(nameReference.Binding.Match.Instance.Target as T);
     }
     else
     {
         return(null);
     }
 }
Пример #13
0
        public IErrorReporter ErrorAbusingForcedConst()
        {
            NameResolver resolver = null;

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

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

                root_ns.AddBuilder(TypeBuilder.Create("Mutator", "M")
                                   .With(FunctionBuilder.Create("violate", NameFactory.UnitNameReference(), Block.CreateStatement())
                                         .SetModifier(EntityModifier.Mutable))
                                   .SetModifier(EntityModifier.Mutable));

                root_ns.AddBuilder(TypeBuilder.Create("Mangler")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(VariableDeclaration.CreateStatement("m",
                                                                             NameFactory.PointerNameReference(NameReference.Create("Mutator", NameReference.Create("Stone"))),
                                                                             Undef.Create(),
                                                                             EntityModifier.Public | env.Options.ReassignableModifier()))
                                   );

                FunctionCall mut_call   = FunctionCall.Create(NameReference.CreateThised("f", "m", NameFactory.MutableName("violate")));
                IExpression  assignment = Assignment.CreateStatement(NameReference.CreateThised("f", "m"), Undef.Create());
                root_ns.AddBuilder(TypeBuilder.Create("Keeper")
                                   .With(VariableDeclaration.CreateStatement("f",
                                                                             NameFactory.PointerNameReference(NameReference.Create(TypeMutability.ForceConst, "Mangler")),
                                                                             Undef.Create(),
                                                                             EntityModifier.Public))
                                   .With(FunctionBuilder.Create("testing", NameFactory.UnitNameReference(),
                                                                Block.CreateStatement(
                                                                    mut_call,
                                                                    assignment
                                                                    ))));



                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AlteringNonMutableInstance, mut_call));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AlteringNonMutableInstance, assignment));
            }

            return(resolver);
        }
Пример #14
0
        public IErrorReporter ErrorMatchingIntersection()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.CreateInterface("IGetPos")
                                   .With(FunctionBuilder.CreateDeclaration("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())));

                root_ns.AddBuilder(TypeBuilder.CreateInterface("IGetNeg")
                                   .With(FunctionBuilder.CreateDeclaration("getMore", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())));

                root_ns.AddBuilder(TypeBuilder.Create("GetAll")
                                   .With(FunctionBuilder.Create("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("3"))
                })))
                                   .With(FunctionBuilder.Create("getMore", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("-1"))
                }))));

                NameReferenceIntersection intersection = NameReferenceIntersection.Create(
                    NameFactory.PointerNameReference(NameReference.Create("IGetNeg")),
                    NameFactory.PointerNameReference(NameReference.Create("IGetPos")));
                IExpression init_value = ExpressionFactory.HeapConstructor("GetAll");
                var         main_func  = root_ns.AddBuilder(FunctionBuilder.Create(
                                                                "foo",
                                                                ExpressionReadMode.CannotBeRead,
                                                                NameFactory.UnitNameReference(),

                                                                Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("a", intersection, init_value),
                    ExpressionFactory.Readout("a")
                })));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Пример #15
0
        public IInterpreter VirtualCall()
        {
            var interpreter = new Interpreter.Interpreter();

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

                root_ns.AddBuilder(TypeBuilder.Create("MyBase")
                                   .SetModifier(EntityModifier.Base)
                                   .With(FunctionBuilder.Create(
                                             "bar",
                                             ExpressionReadMode.ReadRequired,
                                             NameFactory.Int64NameReference(),
                                             Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("33"))
                }))
                                         .SetModifier(EntityModifier.Base)));

                TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("SomeChild")
                                                              .With(FunctionBuilder.Create("bar",
                                                                                           ExpressionReadMode.ReadRequired,
                                                                                           NameFactory.Int64NameReference(),
                                                                                           Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))
                                                                    .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase))
                                                              .Parents(NameReference.Create("MyBase")));

                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("i", NameFactory.PointerNameReference(NameReference.Create("MyBase")),
                                                        ExpressionFactory.HeapConstructor(NameReference.Create("SomeChild"))),
                    Return.Create(FunctionCall.Create(NameReference.Create("i", "bar")))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #16
0
        public IErrorReporter ErrorEscapingReferenceWithAttachmentObject()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("Keeper")
                                   .With(VariableDeclaration.CreateStatement("world",
                                                                             NameFactory.PointerNameReference(NameFactory.IntNameReference()), Undef.Create(), EntityModifier.Public))
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(
                                                                                   Assignment.CreateStatement(NameReference.CreateThised("world"), NameReference.Create("in_value"))
                                                                                   ))
                                         .Parameters(FunctionParameter.Create("in_value", NameFactory.PointerNameReference(NameFactory.IntNameReference())))));

                Assignment assign = Assignment.CreateStatement(NameReference.Create("attach"),
                                                               ExpressionFactory.StackConstructor("Keeper", NameReference.Create("i"))).Cast <Assignment>();

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

                                                                                    Block.CreateStatement(
                                                                                        VariableDeclaration.CreateStatement("attach", NameReference.Create("Keeper"),
                                                                                                                            Undef.Create(), EntityModifier.Reassignable),
                                                                                        Block.CreateStatement(
                                                                                            VariableDeclaration.CreateStatement("i", NameFactory.ReferenceNameReference(NameFactory.IntNameReference()),
                                                                                                                                IntLiteral.Create("0")),
                                                                                            // cannot execute this assignment because the reference would move from here to outer scope
                                                                                            // just wrapped in `Keeper` instance
                                                                                            // please note this `Keeper` instance is attached to `i`
                                                                                            // (triggered by conversion reference->pointer in constructor), so it cannot outlive it
                                                                                            assign,
                                                                                            ExpressionFactory.Readout("attach")
                                                                                            )
                                                                                        )));


                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, assign.RhsValue));
            }
            return(resolver);
        }
Пример #17
0
        public IErrorReporter ErrorCallingTraitMethodOnHost()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.CreateInterface("ISay")
                                   .With(FunctionBuilder.CreateDeclaration("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())));

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

                // this function is located in trait, thus unavailable
                FunctionCall int_call = FunctionCall.Create(NameReference.CreateThised("hello"));
                root_ns.AddBuilder(TypeBuilder.Create("Greeter", "T")
                                   .With(FunctionBuilder.Create("reaching", NameFactory.UnitNameReference(),
                                                                Block.CreateStatement(int_call))));

                root_ns.AddBuilder(TypeBuilder.Create("Greeter", "X")
                                   .SetModifier(EntityModifier.Trait)
                                   .Constraints(ConstraintBuilder.Create("X").Inherits("ISay"))
                                   .With(FunctionBuilder.Create("hello", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(
                                                                    Return.Create(Int64Literal.Create("7"))
                                                                    ))));

                FunctionCall ext_call = FunctionCall.Create(NameReference.Create("g", "hello"));
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(
                                           VariableDeclaration.CreateStatement("g", null,
                                                                               ExpressionFactory.StackConstructor(NameReference.Create("Greeter", NameReference.Create("NoSay")))),
                                           Return.Create(ext_call)
                                           )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceNotFound, ext_call.Name));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceNotFound, int_call.Name));
            }

            return(resolver);
        }
Пример #18
0
        public IInterpreter NoExtrasWithCopyConstructor()
        {
            // nothing is written in stone, but for now let's treat assignment in declaration as assignment
            // not copy constructor (as in C++)
            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")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement()))
                                   // copy-constructor
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(
                                                                                   Assignment.CreateStatement(NameReference.CreateThised("x"), Nat8Literal.Create("66"))
                                                                                   )).Parameters(FunctionParameter.Create("p", NameFactory.ReferenceNameReference("Point"),
                                                                                                                          ExpressionReadMode.CannotBeRead)))

                                   .With(PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference())
                                         .With(VariableDeclaration.CreateStatement("f", NameFactory.Nat8NameReference(), null,
                                                                                   env.Options.ReassignableModifier()))
                                         .WithGetter(Block.CreateStatement(Return.Create(NameReference.CreateThised("f"))))
                                         .WithSetter(Block.CreateStatement(Assignment.CreateStatement(NameReference.CreateThised("f"),
                                                                                                      ExpressionFactory.Mul(NameFactory.PropertySetterValueReference(), Nat8Literal.Create("2")))))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Nat8NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", null,
                                                        ConstructorCall.StackConstructor(NameReference.Create("Point"))
                                                        .Init("x", Nat8Literal.Create("7"))
                                                        .Build()),
                    // bit-copy of the object, there is no calling copy-constructor here
                    VariableDeclaration.CreateStatement("r", null, NameReference.Create("p")),
                    Return.Create(NameReference.Create(NameReference.Create("r"), "x"))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #19
0
        public IInterpreter OverridingMethodWithIndexerGetter()
        {
            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(TypeBuilder.CreateInterface("IProvider")
                                   .With(FunctionBuilder.CreateDeclaration(NameFactory.PropertyIndexerName, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())
                                         .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference()))));

                root_ns.AddBuilder(TypeBuilder.Create("Middle")
                                   .Parents("IProvider")
                                   .SetModifier(EntityModifier.Base)
                                   .With(FunctionBuilder.Create(NameFactory.PropertyIndexerName, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(Return.Create(Int64Literal.Create("500"))))
                                         .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase)
                                         .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead))));

                root_ns.AddBuilder(TypeBuilder.Create("Last")
                                   .Parents("Middle")
                                   .SetModifier(EntityModifier.Base)
                                   .With(PropertyBuilder.CreateIndexer(env.Options, NameFactory.Int64NameReference())
                                         .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead))
                                         .With(PropertyMemberBuilder.CreateIndexerGetter(Block.CreateStatement(Return.Create(Int64Literal.Create("2"))))
                                               .Modifier(EntityModifier.Override | EntityModifier.UnchainBase))));

                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("p", NameFactory.PointerNameReference("IProvider"),
                                                        ExpressionFactory.HeapConstructor("Last")),
                    Return.Create(FunctionCall.Create(NameReference.Create("p", NameFactory.PropertyIndexerName),
                                                      FunctionArgument.Create(Int64Literal.Create("18"))))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #20
0
        public IErrorReporter ErrorEscapingReceivedReferenceFromFunction()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(FunctionBuilder.Create("selector",
                                                          ExpressionReadMode.ReadRequired,
                                                          NameFactory.ReferenceNameReference(NameFactory.IntNameReference()),

                                                          Block.CreateStatement(
                                                              ExpressionFactory.Readout("b"),
                                                              Return.Create(NameReference.Create("a"))
                                                              ))
                                   .Parameters(
                                       FunctionParameter.Create("a", NameFactory.ReferenceNameReference(NameFactory.IntNameReference())),
                                       FunctionParameter.Create("b", NameFactory.ReferenceNameReference(NameFactory.IntNameReference()))));


                FunctionCall call = FunctionCall.Create("selector", IntLiteral.Create("2"), IntLiteral.Create("3"));

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

                                                                                    Block.CreateStatement(
                                                                                        VariableDeclaration.CreateStatement("h", NameFactory.ReferenceNameReference(NameFactory.IntNameReference()),
                                                                                                                            IntLiteral.Create("0"), EntityModifier.Reassignable),
                                                                                        Block.CreateStatement(
                                                                                            // error: the most alive reference the function can return is limited to this scope
                                                                                            // so it cannot be assigned to outer-scope variable
                                                                                            Assignment.CreateStatement(NameReference.Create("h"), call)
                                                                                            ),
                                                                                        ExpressionFactory.Readout("h")
                                                                                        )));


                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, call));
            }
            return(resolver);
        }
Пример #21
0
        public IErrorReporter ErrorHasConstraint()
        {
            NameResolver resolver = null;

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

                FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe",
                                                                                       ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference());

                // this function accepts any parameter where parameter type has function "getMe"
                FunctionDefinition constrained_func = root_ns.AddBuilder(FunctionBuilder.Create("proxy",
                                                                                                TemplateParametersBuffer.Create().Add("T").Values,
                                                                                                ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] {
                    Return.Create(FunctionCall.Create(NameReference.Create("t", "getMe")))
                }))
                                                                         .Constraints(ConstraintBuilder.Create("T").Has(func_constraint))
                                                                         .Parameters(FunctionParameter.Create("t", NameFactory.PointerNameReference("T"))));

                // this type does NOT have function "getMe"
                TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("YMan")
                                                              .With(FunctionBuilder.Create("missing",
                                                                                           ExpressionReadMode.ReadRequired,
                                                                                           NameFactory.Int64NameReference(),
                                                                                           Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))));

                FunctionCall call = FunctionCall.Create(NameReference.Create("proxy"), FunctionArgument.Create(NameReference.Create("y_man")));
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("y_man", null, ExpressionFactory.HeapConstructor(NameReference.Create("YMan"))),
                    Return.Create(call)
                })));

                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Пример #22
0
        public IErrorReporter InternalDirectTranslationTables()
        {
            NameResolver resolver = null;

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

                const string parent_typename = "Oldman";
                const string parent_elemtype = "PT";

                FunctionDefinition base_func = FunctionBuilder.CreateDeclaration("getMe",
                                                                                 ExpressionReadMode.CannotBeRead,
                                                                                 NameFactory.ReferenceNameReference(parent_elemtype));
                TypeDefinition parent = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create(parent_typename,
                                                                                                    TemplateParametersBuffer.Create(parent_elemtype).Values))
                                                           .SetModifier(EntityModifier.Abstract)
                                                           .With(base_func));

                const string child_typename = "Kid";
                const string child_elemtype = "CT";

                FunctionDefinition deriv_func = FunctionBuilder.Create("getMe",
                                                                       ExpressionReadMode.CannotBeRead,
                                                                       NameFactory.ReferenceNameReference(child_elemtype),
                                                                       Block.CreateStatement(Return.Create(Undef.Create())))
                                                .SetModifier(EntityModifier.Override);
                TypeDefinition child = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create(child_typename,
                                                                                                   TemplateParametersBuffer.Create(child_elemtype).Values))
                                                          .Parents(NameReference.Create(parent_typename, NameReference.Create(child_elemtype)))
                                                          .With(deriv_func));

                resolver = NameResolver.Create(env);

                // testing here template translation
                EntityInstance  child_ancestor = child.Inheritance.OrderedAncestorsWithoutObject.Single();
                IEntityInstance translated     = base_func.ResultTypeName.Evaluation.Components.TranslateThrough(child_ancestor);

                // we have single function overriden, so it is easy to debug and spot if something goes wrong
                bool result = FunctionDefinitionExtension.IsDerivedOf(resolver.Context, deriv_func, base_func, child_ancestor);

                Assert.IsTrue(result);
            }

            return(resolver);
        }
Пример #23
0
        public static bool IsValue(this IExpression @this, IOptions options)
        {
            NameReference nameReference = (@this as NameReference);

            // todo: make it nice, such exception is ugly
            if (nameReference?.Name == NameFactory.BaseVariableName)
            {
                return(true);
            }
            IEntity entity = nameReference?.Binding.Match.Instance.Target;
            bool    result = (entity == null || (!entity.IsType() && !entity.IsNamespace()));

            return(result);
        }
Пример #24
0
        public IErrorReporter ErrorAccessNotGranted()
        {
            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 forbidden_access = NameReference.CreateThised("x");

                VariableDeclaration decl = VariableBuilder.CreateStatement("y", NameFactory.Int64NameReference(), null)
                        .Modifier(EntityModifier.Public | env.Options.ReassignableModifier())
                        .GrantAccess("twin");

                root_ns.AddBuilder(TypeBuilder.Create("Point")
                    .SetModifier(EntityModifier.Mutable)
                    .With(FunctionBuilder.Create("friendly",
                        NameFactory.UnitNameReference(),
                        Block.CreateStatement( ExpressionFactory.Readout(NameFactory.ThisVariableName, "x"))))

                        .With(FunctionBuilder.Create("foe",
                            NameFactory.UnitNameReference(),
                            Block.CreateStatement( ExpressionFactory.Readout(forbidden_access))))

                        .With(FunctionBuilder.Create("twin",
                            NameFactory.UnitNameReference(),
                            Block.CreateStatement()))

                        .With(FunctionBuilder.Create("twin",
                            NameFactory.UnitNameReference(),
                            Block.CreateStatement(Return.Create(NameReference.Create("p"))))
                            .Parameters(FunctionParameter.Create("p", NameFactory.UnitNameReference())))

                        .With(VariableBuilder.CreateStatement("x", NameFactory.Int64NameReference(), null)
                        .Modifier(EntityModifier.Private | env.Options.ReassignableModifier())
                        .GrantAccess("friendly"))

                        .With(decl));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(3, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AccessForbidden, forbidden_access));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AmbiguousReference, decl.AccessGrants.Single()));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AccessGrantsOnExposedMember, decl.AccessGrants.Single()));
            }

            return resolver;
        }
Пример #25
0
        public IErrorReporter ErrorViolatingConstConstraint()
        {
            NameResolver resolver = null;

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

                root_ns.AddBuilder(TypeBuilder.Create("Bar")
                                   .SetModifier(EntityModifier.Mutable));
                root_ns.AddBuilder(TypeBuilder.Create("Foo"));

                // we build type Point<T> with enforced "const" on T -- meaning we can pass only trully immutable types
                // as T
                VariableDeclaration field = VariableDeclaration.CreateStatement("m", NameReference.Create("T"),
                                                                                Undef.Create(), modifier: EntityModifier.Public);
                TypeDefinition point_type = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Point",
                                                                                                        TemplateParametersBuffer.Create().Add("T").Values))
                                                               .Constraints(ConstraintBuilder.Create("T")
                                                                            .SetModifier(EntityModifier.Const))
                                                               .With(field));

                // Bar is mutable type, so we cannot construct Point<Bar> since Point requires immutable type
                NameReference wrong_type = NameReference.Create("Point", NameReference.Create("Bar"));
                var           func_def   = root_ns.AddBuilder(FunctionBuilder.Create(
                                                                  "foo", null,
                                                                  ExpressionReadMode.OptionalUse,
                                                                  NameFactory.UnitNameReference(),

                                                                  Block.CreateStatement(new[] {
                    VariableDeclaration.CreateStatement("x", NameReference.Create("Point", NameReference.Create("Foo")), Undef.Create()),
                    VariableDeclaration.CreateStatement("y", wrong_type, Undef.Create()),
                    ExpressionFactory.Readout("x"),
                    ExpressionFactory.Readout("y"),
                })));


                resolver = NameResolver.Create(env);

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

            return(resolver);
        }
Пример #26
0
        protected override void Serialize(SerializingContainer2 sc)
        {
            if (sc.Game == MEGame.ME2)
            {
                int dummy = 0;
                sc.Serialize(ref dummy);
                sc.Serialize(ref dummy);
                sc.Serialize(ref dummy);
                sc.SerializeFileOffset();
            }

            if (sc.Game == MEGame.UDK)
            {
                if (sc.IsSaving && RawAnimationData is null)
                {
                    DecompressAnimationData();
                }
                sc.Serialize(ref RawAnimationData, SCExt.Serialize);
            }

            if (sc.IsLoading)
            {
                compressedDataSource = sc.Game;
                NumFrames            = Export.GetProperty <IntProperty>("NumFrames")?.Value ?? 0;
                RateScale            = Export.GetProperty <FloatProperty>("RateScale")?.Value ?? 1f;
                SequenceLength       = Export.GetProperty <FloatProperty>("SequenceLength")?.Value ?? 0;
                Name         = Export.GetProperty <NameProperty>("SequenceName")?.Value ?? Export.ObjectName;
                TrackOffsets = Export.GetProperty <ArrayProperty <IntProperty> >("CompressedTrackOffsets").Select(i => i.Value).ToArray();
                if (compressedDataSource == MEGame.UDK)
                {
                    Bones = ((ExportEntry)Export.Parent)?.GetProperty <ArrayProperty <NameProperty> >("TrackBoneNames")?.Select(np => $"{np}").ToList();
                }
                else
                {
                    var animsetData = Export.GetProperty <ObjectProperty>("m_pBioAnimSetData");
                    //In ME2, BioAnimSetData can sometimes be in a different package.
                    Bones = animsetData != null && Export.FileRef.IsUExport(animsetData.Value)
                        ? Export.FileRef.GetUExport(animsetData.Value)?.GetProperty <ArrayProperty <NameProperty> >("TrackBoneNames")?.Select(np => $"{np}").ToList()
                        : null;
                }

                Bones ??= Enumerable.Repeat("???", TrackOffsets.Length / 4).ToList();
                Enum.TryParse(Export.GetProperty <EnumProperty>("KeyEncodingFormat")?.Value.Name, out keyEncoding);
                Enum.TryParse(Export.GetProperty <EnumProperty>("RotationCompressionFormat")?.Value.Name, out rotCompression);
                Enum.TryParse(Export.GetProperty <EnumProperty>("TranslationCompressionFormat")?.Value.Name, out posCompression);
            }

            sc.Serialize(ref CompressedAnimationData, SCExt.Serialize);
        }
Пример #27
0
        public IInterpreter TypeUnion()
        {
            var interpreter = new Interpreter.Interpreter();

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

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

                root_ns.AddBuilder(TypeBuilder.Create("GetNeg")
                                   .With(FunctionBuilder.Create("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("-1"))
                }))));

                NameReferenceUnion union = NameReferenceUnion.Create(NameFactory.PointerNameReference(NameReference.Create("GetNeg")),
                                                                     NameFactory.PointerNameReference(NameReference.Create("GetPos")));
                var main_func = root_ns.AddBuilder(FunctionBuilder.Create(
                                                       "main",
                                                       ExpressionReadMode.OptionalUse,
                                                       NameFactory.Int64NameReference(),
                                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("a", union, Undef.Create(), env.Options.ReassignableModifier()),
                    VariableDeclaration.CreateStatement("b", union, Undef.Create(), env.Options.ReassignableModifier()),
                    Assignment.CreateStatement(NameReference.Create("a"), ExpressionFactory.HeapConstructor("GetPos")),
                    Assignment.CreateStatement(NameReference.Create("b"), ExpressionFactory.HeapConstructor("GetNeg")),
                    VariableDeclaration.CreateStatement("x", null, FunctionCall.Create(NameReference.Create("a", "getSome"))),
                    VariableDeclaration.CreateStatement("y", null, FunctionCall.Create(NameReference.Create("b", "getSome"))),
                    Return.Create(ExpressionFactory.Add(NameReference.Create("x"), NameReference.Create("y")))
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #28
0
        public IInterpreter HasConstraintWithValue()
        {
            var interpreter = new Interpreter.Interpreter();

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

                FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe",
                                                                                       ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference());
                root_ns.AddBuilder(FunctionBuilder.Create("proxy",
                                                          TemplateParametersBuffer.Create().Add("T").Values,
                                                          ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] {
                    Return.Create(FunctionCall.Create(NameReference.Create("t", "getMe")))
                }))
                                   .Constraints(ConstraintBuilder.Create("T").Has(func_constraint))
                                   .Parameters(FunctionParameter.Create("t", NameReference.Create("T"))));

                TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("Y")
                                                              .With(FunctionBuilder.Create("getMe",
                                                                                           ExpressionReadMode.ReadRequired,
                                                                                           NameFactory.Int64NameReference(),
                                                                                           Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("2"))
                }))));

                FunctionCall call = FunctionCall.Create(NameReference.Create("proxy"), FunctionArgument.Create(NameReference.Create("y")));
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "main",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.Int64NameReference(),
                                       Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("y", null, ExpressionFactory.StackConstructor(NameReference.Create("Y"))),
                    Return.Create(call)
                })));

                ExecValue result = interpreter.TestRun(env);

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

            return(interpreter);
        }
Пример #29
0
        public static PropertyBuilder CreateAutoFull(IOptions options, string name, NameReference typename,
                                                     out PropertyMembers members, IExpression initValue = null)
        {
            PropertyBuilder builder = PropertyBuilder.Create(options, name, () => typename)
                                      .WithAutoField(initValue, options.ReassignableModifier(), out VariableDeclaration field)
                                      .WithAutoGetter(out FunctionDefinition getter)
                                      .WithAutoSetter(out FunctionDefinition setter);

            members = new PropertyMembers()
            {
                Field = field, Getter = getter, Setter = setter
            };

            return(builder);
        }
Пример #30
0
 public static IExpression Tuple(params IExpression[] arguments)
 {
     if (arguments.Length == 0)
     {
         throw new System.Exception();
     }
     else if (arguments.Length == 1)
     {
         return(arguments.Single());
     }
     else
     {
         return(FunctionCall.Create(NameReference.Create(NameFactory.TupleFactoryReference(), NameFactory.CreateFunctionName), arguments));
     }
 }
Пример #31
0
        public override bool Deserialize()
        {
            var result = base.Deserialize();

            var ArrayInfo = Data.ReadInt32();
            ArraySize = (UInt16)(ArrayInfo & 0x0000FFFFU);
            ArrayElementSize = (UInt16)(ArrayInfo >> 16);

            PropertyFlags = (PropertyFlags)Data.ReadUInt64();
            if (PropertyFlags.HasFlag(PropertyFlags.Net))
                ReplicateOffset = Data.ReadUInt16();

            _CategoryNameRef = Data.ReadNameRef();
            Category = PCC.GetName(_CategoryNameRef);

            ReplicateOffset = Data.ReadUInt16(); // TODO: verify, see code
            ReplicateIndex = Data.ReadUInt16();

            return result;
        }
Пример #32
0
        public bool Deserialize()
        {
            NameRef = Data.ReadNameRef();
            Name = PCC.GetName(NameRef);
            if (String.Equals(Name, "None", StringComparison.OrdinalIgnoreCase))
                return false;

            if (NameRef.ModNumber > -1)
            {
                if (NameRef.ModNumber > 0x1F)   // Some weird inner name
                {                               // TODO: figure this out!
                    NameReference secondary;
                    secondary.Index = NameRef.ModNumber;
                    secondary.ModNumber = Data.ReadInt32();
                    SecondaryName = PCC.GetName(secondary);

                    Name = SecondaryName + "_" + NameRef.Index; // TODO: correct?
                }
                else
                {
                    Name = Name + "_" + NameRef.ModNumber;
                }
            }

            /*if (Name == String.Empty && SecondaryName == String.Empty) //(SecondaryName == String.Empty || SecondaryName == null))
                return false;*/

            TypeNameRef = Data.ReadNameRef();
            if (TypeNameRef.ModNumber > -1) // another weird thing, this type name something unknown, but possibly the modnumber represents component type?
            {
                if (Data.ReadInt32() != 0)
                    return false;
                TypeName = PCC.Names[TypeNameRef.ModNumber + 1]; // TODO: nonstandard, clean up.
                if (String.Equals(TypeName, "None", StringComparison.OrdinalIgnoreCase))
                    return false; //TODO: Sort this arcane shit out.
                var pTypeName = PCC.GetName(Data.ReadNameRef());
                Type = (PropertyType)Enum.Parse(typeof(PropertyType), pTypeName);
            }
            else
            {
                TypeName = PCC.GetName(TypeNameRef);
                /*if (TypeName == String.Empty)
                    return false;*/
                Type = (PropertyType)Enum.Parse(typeof(PropertyType), TypeName);
            }

            Size = Data.ReadUInt32();
            ArrayIndex = Data.ReadUInt32();

            return DeserializeValue(Type, out Value, Size);
        }
Пример #33
0
        public static List<Property> ReadProp(PCCObject pcc, byte[] raw, int start)
        {
            Property p;
            PropertyValue v;
            int sname;
            List<Property> result = new List<Property>();
            int pos = start;
            if (raw.Length - pos < 8)
                return result;
            int name = (int)BitConverter.ToInt64(raw, pos);
            if (!pcc.IsName(name))
                return result;

            int MEtype = pcc.GameVersion;
            string t = pcc.GetNameEntry(name);
            bool test = t == "None";
            if (MEtype == 3)
            {
                t = pcc.Names[name];
                test = pcc.Names[name] == "None";
            }
            else if (MEtype != 1 && MEtype != 2)
            {
                DebugOutput.PrintLn("Failed to get ME Game Type.");
                return new List<Property>();
            }

            if (test)
            {
                p = new Property();
                p.Name = t;
                p.TypeVal = Type.None;
                p.i = 0;
                p.offsetval = pos;
                p.Size = (MEtype == 3) ? 8 : 20;
                p.Value = new PropertyValue();
                p.raw = BitConverter.GetBytes((Int64)name);
                p.offend = pos + ((MEtype == 3) ? 8 : 20);
                result.Add(p);
                return result;
            }
            int type = (int)BitConverter.ToInt64(raw, pos + 8);
            int size = BitConverter.ToInt32(raw, pos + 16);
            int idx = BitConverter.ToInt32(raw, pos + 20); //Unused
            if (!pcc.IsName(type) || size < 0 || size >= raw.Length)
                return result;
            string tp = (MEtype == 3) ? pcc.Names[type] : pcc.GetNameEntry(type);
            switch (tp)
            {
                case "BoolProperty":
                    p = new Property();
                    p.TypeVal = Type.BoolProperty;
                    p.Name = t;
                    p.Size = size;
                    v = new PropertyValue();
                    if (MEtype == 3)
                    {
                        p.offsetval = pos + 24;
                        pos += 25;
                        byte temp = raw[p.offsetval];
                        v.IntValue = (int)temp;
                        v.Boolereno = temp == 1;
                    }
                    else
                    {
                        p.i = 0;
                        v.IntValue = BitConverter.ToInt32(raw, pos + ((MEtype == 2) ? 24 : 16)); //Guess. I haven't seen a true boolproperty yet
                        pos += 28;
                    }
                    p.Value = v;
                    break;
                case "NameProperty":
                    p = new Property();
                    p.TypeVal = Type.NameProperty;
                    p.Name = t;
                    p.Size = size;
                    v = new PropertyValue();
                    if (MEtype == 3)
                    {
                        pos += 24;
                        v.IntValue = BitConverter.ToInt32(raw, pos);
                        v.StringValue = pcc.GetNameEntry(v.IntValue);
                        // Heff: Temporary modification to handle name refs properly, until we can rewrite the whole property system accross all tools
                        var nameRef = new NameReference();
                        nameRef.index = v.IntValue;
                        nameRef.count = BitConverter.ToInt32(raw, pos + 4);
                        nameRef.Name = pcc.GetNameEntry(nameRef.index);
                        if (nameRef.count > 0)
                            nameRef.Name += "_" + (nameRef.count - 1);
                        v.NameValue = nameRef;
                        pos += size;
                    }
                    else
                    {
                        //pos += 32;
                        //int tempInt = BitConverter.ToInt32(raw, pos + 24);
                        p.i = 0;
                        v.IntValue = BitConverter.ToInt32(raw, pos + 24);
                        v.StringValue = pcc.GetNameEntry(v.IntValue);
                        // Heff: Temporary modification to handle name refs properly, until we can rewrite the whole property system accross all tools
                        var nameRef = new NameReference();
                        nameRef.index = v.IntValue;
                        nameRef.count = BitConverter.ToInt32(raw, pos + 4);
                        nameRef.Name = pcc.GetNameEntry(nameRef.index);
                        if (nameRef.count > 0)
                            nameRef.Name += "_" + (nameRef.count - 1);
                        v.NameValue = nameRef;
                        pos += 32;
                    }
                    p.Value = v;
                    break;
                case "IntProperty":
                    p = new Property();
                    p.TypeVal = Type.IntProperty;
                    v = new PropertyValue();
                    p.Name = t;
                    p.Size = size;
                    if (MEtype == 3)
                    {
                        pos += 24;
                        v.IntValue = (int)BitConverter.ToInt64(raw, pos);
                        pos += size;
                    }
                    else
                    {
                        v.IntValue = BitConverter.ToInt32(raw, pos + 24);
                        p.i = 0;
                        p.offsetval = pos + 24;
                        pos += 28;
                    }
                    p.Value = v;
                    break;
                case "DelegateProperty":
                    p = new Property();
                    p.Name = t;
                    p.TypeVal = Type.DelegateProperty;
                    p.i = 0;
                    p.offsetval = pos + 24;
                    v = new PropertyValue();
                    v.IntValue = BitConverter.ToInt32(raw, pos + 28);
                    v.len = size;
                    v.Array = new List<PropertyValue>();
                    if (MEtype != 3)
                        p.Size = size;
                    pos += 24;
                    for (int i = 0; i < size; i++)
                    {
                        PropertyValue v2 = new PropertyValue();
                        if (pos < raw.Length)
                            v2.IntValue = raw[pos];
                        v.Array.Add(v2);
                        pos++;
                    }
                    p.Value = v;
                    break;
                case "ArrayProperty":
                    int count = (int)BitConverter.ToInt64(raw, pos + 24);
                    p = new Property();
                    p.Name = t;
                    if (MEtype != 3)
                        p.Size = size;
                    p.TypeVal = Type.ArrayProperty;
                    p.i = 0;
                    p.offsetval = pos + 24;
                    v = new PropertyValue();
                    v.IntValue = type;
                    v.len = size - 4;
                    count = v.len;//TODO can be other objects too
                    v.Array = new List<PropertyValue>();
                    pos += 28;
                    for (int i = 0; i < count; i++)
                    {
                        PropertyValue v2 = new PropertyValue();
                        if (pos < raw.Length)
                            v2.IntValue = raw[pos];
                        v.Array.Add(v2);
                        pos++;
                    }
                    p.Value = v;
                    break;
                case "StrProperty":
                    if (MEtype == 2)
                        count = (int)BitConverter.ToInt32(raw, pos + 24);
                    else
                        count = (int)BitConverter.ToInt64(raw, pos + 24);
                    p = new Property();
                    p.Name = t;
                    if (MEtype != 3)
                        p.Size = size;
                    p.TypeVal = Type.StrProperty;
                    p.i = 0;
                    p.offsetval = pos + 24;
                    if (MEtype == 3)
                        count *= -1;
                    v = new PropertyValue();
                    v.IntValue = type;
                    v.len = count;
                    pos += 28;
                    string s = "";
                    for (int i = 0; i < ((MEtype == 3) ? count : count - 1); i++)
                    {
                        s += (char)raw[pos];
                        if (MEtype == 3)
                            pos += 2;
                        else
                            pos++;
                    }
                    if (MEtype != 3)
                        pos++;
                    v.StringValue = s;
                    p.Value = v;
                    break;
                case "StructProperty":
                    sname = (int)BitConverter.ToInt64(raw, pos + 24);
                    p = new Property();
                    p.Name = t;
                    p.Size = size;
                    p.TypeVal = Type.StructProperty;
                    p.i = 0;
                    p.offsetval = pos + 24;
                    v = new PropertyValue();
                    v.IntValue = sname;
                    v.len = size;
                    v.Array = new List<PropertyValue>();
                    if (MEtype == 3)
                        v.StringValue = pcc.Names[sname];
                    else if (MEtype == 3)
                        v.StringValue = pcc.GetNameEntry(sname);
                    pos += 32;
                    for (int i = 0; i < size; i += ((MEtype == 3) ? 1 : 4))
                    {
                        PropertyValue v2 = new PropertyValue();
                        //if (pos < raw.Length)
                        //    v2.IntValue = raw[pos];
                        //v.Array.Add(v2);
                        //pos++;
                        if (pos < raw.Length)
                            v2.IntValue = (MEtype == 3) ? raw[pos] : BitConverter.ToInt32(raw, pos);
                        v.Array.Add(v2);
                        pos += (MEtype == 3) ? 1 : 4;
                    }
                    p.Value = v;
                    break;
                case "ByteProperty":
                    if (MEtype == 3)
                        sname = (int)BitConverter.ToInt64(raw, pos + 24);
                    else
                        sname = BitConverter.ToInt32(raw, pos + 24);
                    p = new Property();
                    p.Name = t;
                    p.Size = size;
                    p.TypeVal = Type.ByteProperty;
                    p.i = 0;
                    p.offsetval = pos + 32;
                    v = new PropertyValue();
                    v.StringValue = (MEtype == 3) ? pcc.GetNameEntry(sname) : pcc.GetNameEntry(sname);
                    v.len = size;
                    if (MEtype == 3)
                    {
                        pos += 32;
                        v.IntValue = BitConverter.ToInt32(raw, pos);
                        v.String2 = pcc.Names[v.IntValue];
                        // Heff: Temporary modification to handle name refs properly, until we can rewrite the whole property system accross all tools
                        // This is stupid for enum properties, but as the enum itself will probably never be a name with index it works..
                        var nameRef = new NameReference();
                        nameRef.index = v.IntValue;
                        nameRef.count = BitConverter.ToInt32(raw, pos + 4);
                        nameRef.Name = pcc.GetNameEntry(nameRef.index);
                        if (nameRef.count > 0)
                            nameRef.Name += "_" + (nameRef.count - 1);
                        v.NameValue = nameRef;
                        pos += size;
                    }
                    else
                    {
                        v.IntValue = BitConverter.ToInt32(raw, pos + 28);
                        if (MEtype == 2)
                            v.String2 = pcc.GetNameEntry(v.IntValue);
                        pos += 32;
                    }

                    //v.IntValue = (int)BitConverter.ToInt64(raw, pos);
                    //pos += size;
                    p.Value = v;
                    break;
                case "FloatProperty":
                    if (MEtype == 1)
                        sname = (int)BitConverter.ToInt64(raw, pos + 24);
                    else if (MEtype == 2)
                        sname = BitConverter.ToInt32(raw, pos + 24);
                    p = new Property();
                    p.Name = t;
                    p.Size = size;
                    p.TypeVal = Type.FloatProperty;
                    p.i = 0;
                    p.offsetval = (MEtype != 3) ? pos + 24 : 24;
                    v = new PropertyValue();
                    v.FloatValue = BitConverter.ToSingle(raw, pos + 24);
                    v.len = size;
                    pos += 28;
                    p.Value = v;
                    break;
                default:
                    p = new Property();
                    p.Name = t;
                    p.TypeVal = getType(pcc, type);
                    p.i = 0;
                    if (MEtype != 3)
                        p.Size = size;
                    p.offsetval = pos + 24;
                    p.Value = ReadValue(pcc, raw, pos + 24, type);
                    pos += p.Value.len + 24;
                    break;
            }
            p.raw = new byte[pos - start];
            p.offend = pos;
            if (pos < raw.Length)
                for (int i = 0; i < pos - start; i++)
                    p.raw[i] = raw[start + i];
            result.Add(p);
            if (pos != start) result.AddRange(ReadProp(pcc, raw, pos));
            return result;
        }
Пример #34
0
 private static PropertyValue ReadValue(PCCObject pcc, byte[] raw, int start, int type)
 {
     PropertyValue v = new PropertyValue();
     switch (pcc.GetNameEntry(type))
     {
         case "IntProperty":
         case "FloatProperty":
         case "ObjectProperty":
         case "StringRefProperty":
             v.IntValue = BitConverter.ToInt32(raw, start);
             v.len = 4;
             break;
         case "NameProperty":
             v.IntValue = BitConverter.ToInt32(raw, start);
             var nameRef = new NameReference();
             nameRef.index = v.IntValue;
             nameRef.count = BitConverter.ToInt32(raw, start + 4);
             nameRef.Name = pcc.GetNameEntry(nameRef.index);
             if (nameRef.count > 0)
                 nameRef.Name += "_" + (nameRef.count - 1);
             v.NameValue = nameRef;
             v.len = 8;
             break;
         case "BoolProperty":
             if (start < raw.Length)
                 v.IntValue = raw[start];
             v.len = 1;
             break;
     }
     return v;
 }