Пример #1
0
            public void Add(T t)
            {
                if (t.CompareTo(Value) > 0)
                {
                    if (Greater == null)
                    {
                        Greater = new TreeNode(t, this);
                        return;
                    }

                    Greater.Add(t);
                }
                else if (t.CompareTo(Value) < 0)
                {
                    if (Smaller == null)
                    {
                        Smaller = new TreeNode(t, this);
                        return;
                    }

                    Smaller.Add(t);
                }
                else // t = Value
                {
                    throw new ArgumentException("Can not have two elements with this same value.");
                }
            }
Пример #2
0
        public static void ToSql(Greater expression, StringBuilder builder)
        {
            if (null == expression)
            {
                throw new ArgumentNullException("expression");
            }
            if (null == builder)
            {
                throw new ArgumentNullException("builder");
            }

            if (expression.Operands.Count > 0)
            {
                IToken operand1 = expression.Operands
                                  .First();
                IToken operand2 = expression.Operands
                                  .ElementAtOrDefault(1);
                if (null != operand1 && null != operand2)
                {
                    operand1.ToSql(builder);
                    builder.Append(" >= ");
                    operand2.ToSql(builder);
                }
            }
        }
Пример #3
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (property == null)
        {
            return;
        }

        EditorGUI.BeginChangeCheck();

        Greater greaterAttribute = (Greater)attribute;

        int intThreshold = greaterAttribute.intThreshold;

        EditorGUI.PropertyField(position, property, label);

        if (property.propertyType == SerializedPropertyType.Integer)
        {
            if (property.intValue <= intThreshold)
            {
                property.intValue = intThreshold + 1;
            }
        }

        if (EditorGUI.EndChangeCheck())
        {
            property.serializedObject.ApplyModifiedProperties();
        }
    }
Пример #4
0
 public void Visit(Greater n)
 {
     Helpers.Write("(");
     n.LeftExpression.Accept(this);
     Helpers.Write(" > ");
     n.RightExpression.Accept(this);
     Helpers.Write(")");
 }
Пример #5
0
        public override void VisitGreater(Greater n)
        {
            SetupOperands(n);

            _gen.Emit(OpCodes.Cgt);

            _lastWalkedType = typeof(bool);
        }
Пример #6
0
        public void BinaryGreaterTest()
        {
            Greater op = new Greater();

            Assert.AreEqual(t, new BinaryStatement(op, oneIntn, zeroIntn).Evaluate(varTable));
            Assert.AreEqual(f, new BinaryStatement(op, minusOneIntn, zeroIntn).Evaluate(varTable));
            Assert.AreEqual(t, new BinaryStatement(op, halfFloatn, zeroFloatn).Evaluate(varTable));
            Assert.AreEqual(f, new BinaryStatement(op, zeroFloatn, zeroFloatn).Evaluate(varTable));
        }
Пример #7
0
        public void Visit(Greater bin)
        {
            var left  = bin.Left;
            var right = bin.Right;

            PrepareBinaryOperation(left, right);

            EmitStackDown("cgt");
        }
Пример #8
0
        public void BinaryGreaterErrorTest()
        {
            Greater op = new Greater();

            Assert.Throws <System.InvalidOperationException>(() => new BinaryStatement(op, oneFloatn, zeroIntn).Evaluate(varTable));
            Assert.Throws <System.InvalidOperationException>(() => new BinaryStatement(op, abn, an).Evaluate(varTable));
            Assert.Throws <System.InvalidOperationException>(() => new BinaryStatement(op, tn, fn).Evaluate(varTable));
            Assert.Throws <System.InvalidOperationException>(() => new BinaryStatement(op, oneFloatn, tn).Evaluate(varTable));
            Assert.Throws <System.InvalidOperationException>(() => new BinaryStatement(op, fn, zeroIntn).Evaluate(varTable));
        }
Пример #9
0
        public void Check_Whether_First_Is_Greater_Than_Second(object param1, object param2, bool expected)
        {
            // given
            var sut = new Greater(param1, param2);

            // when
            var result = Evaluator.Evaluate(sut);

            // then
            Assert.Equal(expected, result);
        }
Пример #10
0
 /// <summary>
 /// Serves as a hash of this type.
 /// </summary>
 /// <returns>A hash code for the current instance.</returns>
 public override int GetHashCode()
 {
     return
         (Width.GetHashCode() ^
          Height.GetHashCode() ^
          X.GetHashCode() ^
          Y.GetHashCode() ^
          IsPercentage.GetHashCode() ^
          IgnoreAspectRatio.GetHashCode() ^
          Less.GetHashCode() ^
          Greater.GetHashCode() ^
          FillArea.GetHashCode() ^
          LimitPixels.GetHashCode());
 }
Пример #11
0
        public string Visit(Greater node)
        {
            var result = "";

            result += "\t\tldc.i4 42\n";
            result += Visit((dynamic)node[0]);
            result += Visit((dynamic)node[1]);
            var labelSuccess = GenerateLabel();

            result += "\t\tbgt " + labelSuccess + "\n";
            result += "\t\tpop\n";
            result += "\t\tldc.i4 0\n";
            result += "\t\t" + labelSuccess + ":\n";
            return(result);
        }
Пример #12
0
        public void Visit(Greater n)
        {
            Helpers.WriteLine($"{_tab}{n.Text} [{n.Location.StartLine}, {n.Location.StartColumn}]");
            Tab();

            Helpers.WriteLine($"{_tab}Left");
            Tab();
            n.LeftExpression.Accept(this);
            Untab();
            Helpers.WriteLine($"{_tab}Right");
            Tab();
            n.RightExpression.Accept(this);
            Untab();

            Untab();
        }
Пример #13
0
        public void Visit(Greater n)
        {
            Globals.Builder.Start($"greater (>) [{n.Location.StartLine}, {n.Location.StartColumn}]");

            WriteBinaryExpression("cmp", n.LeftExpression, n.RightExpression);

            var labelFalse = $"greaterFalse{Globals.Builder.Count()}";
            var labelEnd   = $"greaterEnd{Globals.Builder.Count()}";

            Globals.Builder.WriteUnaryOp("jng", labelFalse);
            Globals.Builder.WriteBinaryOp("mov", "rax", 1.ToString());
            Globals.Builder.WriteUnaryOp("jmp", labelEnd);
            Globals.Builder.WriteLabel(labelFalse);
            Globals.Builder.WriteBinaryOp("mov", "rax", 0.ToString());
            Globals.Builder.WriteLabel(labelEnd);

            Globals.Builder.End($"greater (>) [{n.Location.EndLine}, {n.Location.EndColumn}]");
        }
Пример #14
0
 public virtual void VisitGreater(Greater n)
 {
 }
Пример #15
0
 public virtual void VisitGreater(Greater n)
 {
 }
Пример #16
0
        public static void ToSql(Greater expression, StringBuilder builder)
        {
            if (null == expression) throw new ArgumentNullException("expression");
            if (null == builder) throw new ArgumentNullException("builder");

            if (expression.Operands.Count > 0)
            {
                IToken operand1 = expression.Operands
                                            .First();
                IToken operand2 = expression.Operands
                                            .ElementAtOrDefault(1);
                if (null != operand1 && null != operand2)
                {
                    operand1.ToSql(builder);
                    builder.Append(" >= ");
                    operand2.ToSql(builder);
                }
            }
        }
 public SemanticAtom Visit(Greater n)
 {
     n.RealizedType = BinaryExpressionHelper(n, Primitive.Int, ">", Primitive.Boolean);
     return(n.RealizedType);
 }
Пример #18
0
        private static void Main()
        {
            var of = Greater.Of(.7, .9);

            Console.WriteLine(of);
        }
Пример #19
0
        protected internal virtual void create()
        {
            int  num  = this.string_0.Length;
            Unit unit = null;
            int  num2 = 0;

            while (this.int_0 < num)
            {
                char ch2 = this.string_0[this.int_0];
                if (char.IsWhiteSpace(ch2))
                {
                    goto Label_041D;
                }
                unit = null;
                char ch = ch2;
                switch (ch)
                {
                case '!':
                    this.int_0++;
                    if ((this.int_0 < num) && (this.string_0[this.int_0] == '='))
                    {
                        unit = new NotEquals();
                        this.int_0++;
                    }
                    if (unit == null)
                    {
                        unit = new Not();
                    }
                    goto Label_03B6;

                case '"':
                case '#':
                case '$':
                case '\'':
                case ';':
                    goto Label_02D1;

                case '%':
                    unit = new Mod();
                    this.int_0++;
                    goto Label_03B6;

                case '&':
                    this.int_0++;
                    if ((this.int_0 >= num) || (this.string_0[this.int_0] != '&'))
                    {
                        break;
                    }
                    unit = new And();
                    this.int_0++;
                    goto Label_03B6;

                case '(':
                    goto Label_02DD;

                case ')':
                    goto Label_02F4;

                case '*':
                    goto Label_0312;

                case '+':
                    goto Label_032B;

                case ',':
                    throw new ReportError(new StringBuilder("位置").Append(this.int_0).Append("不应该出现逗号").ToString().ToString());

                case '-':
                    goto Label_0341;

                case '.':
                    goto Label_0357;

                case '/':
                    unit = new Divide();
                    this.int_0++;
                    goto Label_03B6;

                case ':':
                    unit = new Link(this.cellExt1_0);
                    this.int_0++;
                    goto Label_03B6;

                case '<':
                    this.int_0++;
                    if ((this.int_0 < num) && (this.string_0[this.int_0] == '='))
                    {
                        unit = new NotGreater();
                        this.int_0++;
                    }
                    if (unit == null)
                    {
                        unit = new Smaller();
                    }
                    goto Label_03B6;

                case '=':
                    this.int_0++;
                    if ((this.int_0 < num) && (this.string_0[this.int_0] == '='))
                    {
                        this.int_0++;
                    }
                    unit = new Equals();
                    goto Label_03B6;

                case '>':
                    this.int_0++;
                    if ((this.int_0 < num) && (this.string_0[this.int_0] == '='))
                    {
                        unit = new NotSmaller();
                        this.int_0++;
                    }
                    if (unit == null)
                    {
                        unit = new Greater();
                    }
                    goto Label_03B6;

                default:
                    if (ch != '|')
                    {
                        goto Label_02D1;
                    }
                    this.int_0++;
                    if ((this.int_0 >= num) || (this.string_0[this.int_0] != '|'))
                    {
                        throw new ReportError("不能识别标识符|");
                    }
                    unit = new Or();
                    this.int_0++;
                    goto Label_03B6;
                }
                unit = LoadFunction.newFunction("address", this.env_0, this.cellExt1_0, this.dataSet_0);
                goto Label_03B6;
Label_02D1:
                unit = this.createNode();
                goto Label_03B6;
Label_02DD:
                num2++;
                this.int_0++;
                continue;
Label_02F4:
                if (--num2 < 0)
                {
                    throw new ReportError("括号不匹配,右括号太多!");
                }
                this.int_0++;
                continue;
Label_0312:
                unit = new Multiply();
                this.int_0++;
                goto Label_03B6;
Label_032B:
                unit = new Add();
                this.int_0++;
                goto Label_03B6;
Label_0341:
                unit = new Subtract();
                this.int_0++;
                goto Label_03B6;
Label_0357:
                if ((this.unit_1 != null) && !this.unit_1.Operator)
                {
                    unit = LoadFunction.newFunction("dsmember", this.env_0, this.cellExt1_0, this.dataSet_0);
                    this.int_0++;
                }
                else
                {
                    unit = this.createNode();
                }
Label_03B6:
                unit.InBrackets = num2;
                this.unit_1     = unit;
                if (this.unit_0 != null)
                {
                    Unit right = this.unit_0;
                    Unit unit2 = null;
                    while (right != null)
                    {
                        if (right.Priority >= unit.Priority)
                        {
                            break;
                        }
                        unit2 = right;
                        right = right.Right;
                    }
                    unit.Left = right;
                    if (unit2 == null)
                    {
                        this.unit_0 = unit;
                    }
                    else
                    {
                        unit2.Right = unit;
                    }
                    continue;
                }
                this.unit_0 = unit;
                continue;
Label_041D:
                this.int_0++;
            }
            if (num2 > 0)
            {
                throw new ReportError("括号不匹配,左括号太多!");
            }
        }
 public SemanticAtom Visit(Greater n)
 {
     n.LeftExpression.Accept(this);
     n.RightExpression.Accept(this);
     return(null);
 }
 public void Visit(Greater node)
 {
     VisitChildren(node);
 }
Пример #22
0
        public static ParamLessInstruction GetInstructionForOperator(
            string operatorVal,
            bool twoArg = true,
            DatSymbolType leftSideType = DatSymbolType.Void)
        {
            ParamLessInstruction instruction = new ParamLessInstruction();

            switch (operatorVal)
            {
            case "=":
                instruction = GetAssignInstructionForDatSymbolType(leftSideType);
                break;

            case "+=":
                instruction = new AssignAdd();
                break;

            case "-=":
                instruction = new AssignSubtract();
                break;

            case "*=":
                instruction = new AssignMultiply();
                break;

            case "/=":
                instruction = new AssignDivide();
                break;


            case "+":
                if (twoArg)
                {
                    instruction = new Add();
                }
                else
                {
                    instruction = new Plus();
                }

                break;

            case "-":
                if (twoArg)
                {
                    instruction = new Subtract();
                }
                else
                {
                    instruction = new Minus();
                }

                break;


            case "<<":
                instruction = new ShiftLeft();
                break;

            case ">>":
                instruction = new ShiftRight();
                break;


            case ">":
                instruction = new Greater();
                break;

            case ">=":
                instruction = new GreaterOrEqual();
                break;

            case "<":
                instruction = new Less();
                break;

            case "<=":
                instruction = new LessOrEqual();
                break;


            case "==":
                instruction = new Equal();
                break;

            case "!=":
                instruction = new NotEqual();
                break;


            case "!":
                instruction = new Not();
                break;

            case "~":
                instruction = new Negate();
                break;


            case "*":
                instruction = new Multiply();
                break;

            case "/":
                instruction = new Divide();
                break;

            case "%":
                instruction = new Modulo();
                break;


            case "&":
                instruction = new BitAnd();
                break;

            case "|":
                instruction = new BitOr();
                break;

            case "&&":
                instruction = new LogAnd();
                break;

            case "||":
                instruction = new LogOr();
                break;
            }

            if (instruction == null)
            {
                throw new Exception($"'{operatorVal}' does't have insctruction");
            }

            return(instruction);
        }
Пример #23
0
 public void SayHello_ShouldReturnNotEmptyString()
 {
     Greater.SayHello().Should().NotBeEmpty("Because only invalid greeting can be empty");
 }
Пример #24
0
 //-----------------------------------------------------------
 public string Visit(Greater node, Table table)
 {
     return(VisitBinaryOperator("cgt", node, table));
 }
Пример #25
0
        public void Parse()
        {
            var current = _reader.BaseStream.Position;

            _reader.BaseStream.Seek(_offset, SeekOrigin.Begin);
            bool parsing     = true;
            bool branched    = false;
            int  branchBytes = 0;

            while (parsing)
            {
                //now reader the instructions
                var type    = _reader.ReadByteAsEnum <InstructionType>();
                var aligned = InstructionAlignment.IsAligned(type);

                if (aligned)
                {
                    var padding = _reader.Align(4);
                    if (padding > 0)
                    {
                        Items.Add(new Padding(padding));
                        if (branched)
                        {
                            branchBytes -= (int)padding;

                            if (branchBytes <= 0)
                            {
                                branched    = false;
                                branchBytes = 0;
                            }
                        }
                    }
                }

                InstructionBase instruction = null;
                List <Value>    parameters  = new List <Value>();

                switch (type)
                {
                case InstructionType.ToNumber:
                    instruction = new ToNumber();
                    break;

                case InstructionType.NextFrame:
                    instruction = new NextFrame();
                    break;

                case InstructionType.Play:
                    instruction = new Play();
                    break;

                case InstructionType.Stop:
                    instruction = new Stop();
                    break;

                case InstructionType.Add:
                    instruction = new Add();
                    break;

                case InstructionType.Subtract:
                    instruction = new Subtract();
                    break;

                case InstructionType.Multiply:
                    instruction = new Multiply();
                    break;

                case InstructionType.Divide:
                    instruction = new Divide();
                    break;

                case InstructionType.Not:
                    instruction = new Not();
                    break;

                case InstructionType.StringEquals:
                    instruction = new StringEquals();
                    break;

                case InstructionType.Pop:
                    instruction = new Pop();
                    break;

                case InstructionType.ToInteger:
                    instruction = new ToInteger();
                    break;

                case InstructionType.GetVariable:
                    instruction = new GetVariable();
                    break;

                case InstructionType.SetVariable:
                    instruction = new SetVariable();
                    break;

                case InstructionType.StringConcat:
                    instruction = new StringConcat();
                    break;

                case InstructionType.GetProperty:
                    instruction = new GetProperty();
                    break;

                case InstructionType.SetProperty:
                    instruction = new SetProperty();
                    break;

                case InstructionType.Trace:
                    instruction = new Trace();
                    break;

                case InstructionType.Delete:
                    instruction = new Delete();
                    break;

                case InstructionType.Delete2:
                    instruction = new Delete2();
                    break;

                case InstructionType.DefineLocal:
                    instruction = new DefineLocal();
                    break;

                case InstructionType.CallFunction:
                    instruction = new CallFunction();
                    break;

                case InstructionType.Return:
                    instruction = new Return();
                    break;

                case InstructionType.NewObject:
                    instruction = new NewObject();
                    break;

                case InstructionType.InitArray:
                    instruction = new InitArray();
                    break;

                case InstructionType.InitObject:
                    instruction = new InitObject();
                    break;

                case InstructionType.TypeOf:
                    instruction = new InitObject();
                    break;

                case InstructionType.Add2:
                    instruction = new Add2();
                    break;

                case InstructionType.LessThan2:
                    instruction = new LessThan2();
                    break;

                case InstructionType.Equals2:
                    instruction = new Equals2();
                    break;

                case InstructionType.ToString:
                    instruction = new ToString();
                    break;

                case InstructionType.PushDuplicate:
                    instruction = new PushDuplicate();
                    break;

                case InstructionType.GetMember:
                    instruction = new GetMember();
                    break;

                case InstructionType.SetMember:
                    instruction = new SetMember();
                    break;

                case InstructionType.Increment:
                    instruction = new Increment();
                    break;

                case InstructionType.Decrement:
                    instruction = new Decrement();
                    break;

                case InstructionType.CallMethod:
                    instruction = new CallMethod();
                    break;

                case InstructionType.Enumerate2:
                    instruction = new Enumerate2();
                    break;

                case InstructionType.EA_PushThis:
                    instruction = new PushThis();
                    break;

                case InstructionType.EA_PushZero:
                    instruction = new PushZero();
                    break;

                case InstructionType.EA_PushOne:
                    instruction = new PushOne();
                    break;

                case InstructionType.EA_CallFunc:
                    instruction = new CallFunc();
                    break;

                case InstructionType.EA_CallMethodPop:
                    instruction = new CallMethodPop();
                    break;

                case InstructionType.BitwiseXOr:
                    instruction = new BitwiseXOr();
                    break;

                case InstructionType.Greater:
                    instruction = new Greater();
                    break;

                case InstructionType.EA_PushThisVar:
                    instruction = new PushThisVar();
                    break;

                case InstructionType.EA_PushGlobalVar:
                    instruction = new PushGlobalVar();
                    break;

                case InstructionType.EA_ZeroVar:
                    instruction = new ZeroVar();
                    break;

                case InstructionType.EA_PushTrue:
                    instruction = new PushTrue();
                    break;

                case InstructionType.EA_PushFalse:
                    instruction = new PushFalse();
                    break;

                case InstructionType.EA_PushNull:
                    instruction = new PushNull();
                    break;

                case InstructionType.EA_PushUndefined:
                    instruction = new PushUndefined();
                    break;

                case InstructionType.GotoFrame:
                    instruction = new GotoFrame();
                    parameters.Add(Value.FromInteger(_reader.ReadInt32()));
                    break;

                case InstructionType.GetURL:
                    instruction = new GetUrl();
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    break;

                case InstructionType.SetRegister:
                    instruction = new SetRegister();
                    parameters.Add(Value.FromInteger(_reader.ReadInt32()));
                    break;

                case InstructionType.ConstantPool:
                {
                    instruction = new ConstantPool();
                    var count     = _reader.ReadUInt32();
                    var constants = _reader.ReadFixedSizeArrayAtOffset <uint>(() => _reader.ReadUInt32(), count);

                    foreach (var constant in constants)
                    {
                        parameters.Add(Value.FromConstant(constant));
                    }
                }
                break;

                case InstructionType.GotoLabel:
                    instruction = new GotoLabel();
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    break;

                case InstructionType.DefineFunction2:
                {
                    instruction = new DefineFunction2();
                    var name       = _reader.ReadStringAtOffset();
                    var nParams    = _reader.ReadUInt32();
                    var nRegisters = _reader.ReadByte();
                    var flags      = _reader.ReadUInt24();

                    //list of parameter strings
                    var paramList = _reader.ReadFixedSizeListAtOffset <FunctionArgument>(() => new FunctionArgument()
                        {
                            Register  = _reader.ReadInt32(),
                            Parameter = _reader.ReadStringAtOffset(),
                        }, nParams);

                    parameters.Add(Value.FromString(name));
                    parameters.Add(Value.FromInteger((int)nParams));
                    parameters.Add(Value.FromInteger((int)nRegisters));
                    parameters.Add(Value.FromInteger((int)flags));
                    foreach (var param in paramList)
                    {
                        parameters.Add(Value.FromInteger(param.Register));
                        parameters.Add(Value.FromString(param.Parameter));
                    }
                    //body size of the function
                    parameters.Add(Value.FromInteger(_reader.ReadInt32()));
                    //skip 8 bytes
                    _reader.ReadUInt64();
                }
                break;

                case InstructionType.PushData:
                {
                    instruction = new PushData();

                    var count     = _reader.ReadUInt32();
                    var constants = _reader.ReadFixedSizeArrayAtOffset <uint>(() => _reader.ReadUInt32(), count);

                    foreach (var constant in constants)
                    {
                        parameters.Add(Value.FromConstant(constant));
                    }
                }
                break;

                case InstructionType.BranchAlways:
                    instruction = new BranchAlways();
                    if (!branched)
                    {
                        branchBytes = _reader.ReadInt32();
                        parameters.Add(Value.FromInteger(branchBytes));

                        if (branchBytes > 0)
                        {
                            branchBytes += (int)instruction.Size + 1;
                            branched     = true;
                        }
                    }
                    else
                    {
                        parameters.Add(Value.FromInteger(_reader.ReadInt32()));
                    }
                    break;

                case InstructionType.GetURL2:
                    instruction = new GetUrl2();
                    break;

                case InstructionType.DefineFunction:
                {
                    instruction = new DefineFunction();
                    var name = _reader.ReadStringAtOffset();
                    //list of parameter strings
                    var paramList = _reader.ReadListAtOffset <string>(() => _reader.ReadStringAtOffset());

                    parameters.Add(Value.FromString(name));
                    parameters.Add(Value.FromInteger(paramList.Count));
                    foreach (var param in paramList)
                    {
                        parameters.Add(Value.FromString(param));
                    }
                    //body size of the function
                    parameters.Add(Value.FromInteger(_reader.ReadInt32()));
                    //skip 8 bytes
                    _reader.ReadUInt64();
                }
                break;

                case InstructionType.BranchIfTrue:
                    instruction = new BranchIfTrue();
                    if (!branched)
                    {
                        branchBytes = _reader.ReadInt32();
                        parameters.Add(Value.FromInteger(branchBytes));

                        if (branchBytes > 0)
                        {
                            branchBytes += (int)instruction.Size + 1;
                            branched     = true;
                        }
                    }
                    else
                    {
                        parameters.Add(Value.FromInteger(_reader.ReadInt32()));
                    }
                    break;

                case InstructionType.GotoFrame2:
                    instruction = new GotoFrame2();
                    parameters.Add(Value.FromInteger(_reader.ReadByte()));
                    break;

                case InstructionType.EA_PushString:
                    instruction = new PushString();
                    //the constant id that should be pushed
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    break;

                case InstructionType.EA_PushConstantByte:
                    instruction = new PushConstantByte();
                    //the constant id that should be pushed
                    parameters.Add(Value.FromConstant(_reader.ReadByte()));
                    break;

                case InstructionType.EA_GetStringVar:
                    instruction = new GetStringVar();
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    break;

                case InstructionType.EA_SetStringVar:
                    instruction = new SetStringMember();
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    break;

                case InstructionType.EA_GetStringMember:
                    instruction = new GetStringMember();
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    break;

                case InstructionType.EA_SetStringMember:
                    instruction = new SetStringMember();
                    parameters.Add(Value.FromString(_reader.ReadStringAtOffset()));
                    break;

                case InstructionType.EA_PushValueOfVar:
                    instruction = new PushValueOfVar();
                    //the constant id that should be pushed
                    parameters.Add(Value.FromConstant(_reader.ReadByte()));
                    break;

                case InstructionType.EA_GetNamedMember:
                    instruction = new GetNamedMember();
                    parameters.Add(Value.FromConstant(_reader.ReadByte()));
                    break;

                case InstructionType.EA_CallNamedFuncPop:
                    instruction = new CallNamedFuncPop();
                    parameters.Add(Value.FromConstant(_reader.ReadByte()));
                    break;

                case InstructionType.EA_CallNamedFunc:
                    instruction = new CallNamedFunc();
                    parameters.Add(Value.FromConstant(_reader.ReadByte()));
                    break;

                case InstructionType.EA_CallNamedMethodPop:
                    instruction = new CallNamedMethodPop();
                    parameters.Add(Value.FromConstant(_reader.ReadByte()));
                    break;

                case InstructionType.EA_PushFloat:
                    instruction = new PushFloat();
                    parameters.Add(Value.FromFloat(_reader.ReadSingle()));
                    break;

                case InstructionType.EA_PushByte:
                    instruction = new PushByte();
                    parameters.Add(Value.FromInteger(_reader.ReadByte()));
                    break;

                case InstructionType.EA_PushShort:
                    instruction = new PushShort();
                    parameters.Add(Value.FromInteger(_reader.ReadUInt16()));
                    break;

                case InstructionType.End:
                    instruction = new End();

                    if (!branched)
                    {
                        parsing = false;
                    }
                    break;

                case InstructionType.EA_CallNamedMethod:
                    instruction = new CallNamedMethod();
                    parameters.Add(Value.FromConstant(_reader.ReadByte()));
                    break;

                case InstructionType.Var:
                    instruction = new Var();

                    break;

                default:
                    throw new InvalidDataException("Unimplemented bytecode instruction:" + type.ToString());
                }

                if (instruction != null)
                {
                    instruction.Parameters = parameters;
                    Items.Add(instruction);
                }

                if (branched)
                {
                    branchBytes -= (int)instruction.Size + 1;

                    if (branchBytes <= 0)
                    {
                        branched = false;
                    }
                }
            }
            _reader.BaseStream.Seek(current, SeekOrigin.Begin);
        }
Пример #26
0
        public static InstructionCollection Parse(Stream input, long instructionsPosition)
        {
            var instructions = new SortedList <int, InstructionBase>();

            using (var helper = new InstructionParseHelper(input, instructionsPosition))
            {
                var reader = helper.GetReader();
                while (helper.CanParse(instructions))
                {
                    //now reader the instructions
                    var instructionPosition = helper.CurrentPosition;
                    var type             = reader.ReadByteAsEnum <InstructionType>();
                    var requireAlignment = InstructionAlignment.IsAligned(type);

                    if (requireAlignment)
                    {
                        reader.Align(4);
                    }

                    InstructionBase instruction = null;
                    var             parameters  = new List <Value>();

                    switch (type)
                    {
                    case InstructionType.ToNumber:
                        instruction = new ToNumber();
                        break;

                    case InstructionType.NextFrame:
                        instruction = new NextFrame();
                        break;

                    case InstructionType.Play:
                        instruction = new Play();
                        break;

                    case InstructionType.Stop:
                        instruction = new Stop();
                        break;

                    case InstructionType.Add:
                        instruction = new Add();
                        break;

                    case InstructionType.Subtract:
                        instruction = new Subtract();
                        break;

                    case InstructionType.Multiply:
                        instruction = new Multiply();
                        break;

                    case InstructionType.Divide:
                        instruction = new Divide();
                        break;

                    case InstructionType.Not:
                        instruction = new Not();
                        break;

                    case InstructionType.StringEquals:
                        instruction = new StringEquals();
                        break;

                    case InstructionType.Pop:
                        instruction = new Pop();
                        break;

                    case InstructionType.ToInteger:
                        instruction = new ToInteger();
                        break;

                    case InstructionType.GetVariable:
                        instruction = new GetVariable();
                        break;

                    case InstructionType.SetVariable:
                        instruction = new SetVariable();
                        break;

                    case InstructionType.StringConcat:
                        instruction = new StringConcat();
                        break;

                    case InstructionType.GetProperty:
                        instruction = new GetProperty();
                        break;

                    case InstructionType.SetProperty:
                        instruction = new SetProperty();
                        break;

                    case InstructionType.Trace:
                        instruction = new Trace();
                        break;

                    case InstructionType.Random:
                        instruction = new RandomNumber();
                        break;

                    case InstructionType.Delete:
                        instruction = new Delete();
                        break;

                    case InstructionType.Delete2:
                        instruction = new Delete2();
                        break;

                    case InstructionType.DefineLocal:
                        instruction = new DefineLocal();
                        break;

                    case InstructionType.CallFunction:
                        instruction = new CallFunction();
                        break;

                    case InstructionType.Return:
                        instruction = new Return();
                        break;

                    case InstructionType.Modulo:
                        instruction = new Modulo();
                        break;

                    case InstructionType.NewObject:
                        instruction = new NewObject();
                        break;

                    case InstructionType.InitArray:
                        instruction = new InitArray();
                        break;

                    case InstructionType.InitObject:
                        instruction = new InitObject();
                        break;

                    case InstructionType.TypeOf:
                        instruction = new TypeOf();
                        break;

                    case InstructionType.Add2:
                        instruction = new Add2();
                        break;

                    case InstructionType.LessThan2:
                        instruction = new LessThan2();
                        break;

                    case InstructionType.Equals2:
                        instruction = new Equals2();
                        break;

                    case InstructionType.ToString:
                        instruction = new ToString();
                        break;

                    case InstructionType.PushDuplicate:
                        instruction = new PushDuplicate();
                        break;

                    case InstructionType.GetMember:
                        instruction = new GetMember();
                        break;

                    case InstructionType.SetMember:
                        instruction = new SetMember();
                        break;

                    case InstructionType.Increment:
                        instruction = new Increment();
                        break;

                    case InstructionType.Decrement:
                        instruction = new Decrement();
                        break;

                    case InstructionType.CallMethod:
                        instruction = new CallMethod();
                        break;

                    case InstructionType.Enumerate2:
                        instruction = new Enumerate2();
                        break;

                    case InstructionType.EA_PushThis:
                        instruction = new PushThis();
                        break;

                    case InstructionType.EA_PushZero:
                        instruction = new PushZero();
                        break;

                    case InstructionType.EA_PushOne:
                        instruction = new PushOne();
                        break;

                    case InstructionType.EA_CallFunc:
                        instruction = new CallFunc();
                        break;

                    case InstructionType.EA_CallMethodPop:
                        instruction = new CallMethodPop();
                        break;

                    case InstructionType.BitwiseXOr:
                        instruction = new BitwiseXOr();
                        break;

                    case InstructionType.Greater:
                        instruction = new Greater();
                        break;

                    case InstructionType.EA_PushThisVar:
                        instruction = new PushThisVar();
                        break;

                    case InstructionType.EA_PushGlobalVar:
                        instruction = new PushGlobalVar();
                        break;

                    case InstructionType.EA_ZeroVar:
                        instruction = new ZeroVar();
                        break;

                    case InstructionType.EA_PushTrue:
                        instruction = new PushTrue();
                        break;

                    case InstructionType.EA_PushFalse:
                        instruction = new PushFalse();
                        break;

                    case InstructionType.EA_PushNull:
                        instruction = new PushNull();
                        break;

                    case InstructionType.EA_PushUndefined:
                        instruction = new PushUndefined();
                        break;

                    case InstructionType.GotoFrame:
                        instruction = new GotoFrame();
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        break;

                    case InstructionType.GetURL:
                        instruction = new GetUrl();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.SetRegister:
                        instruction = new SetRegister();
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        break;

                    case InstructionType.ConstantPool:
                    {
                        instruction = new ConstantPool();
                        var count     = reader.ReadUInt32();
                        var constants = reader.ReadFixedSizeArrayAtOffset <uint>(() => reader.ReadUInt32(), count);

                        foreach (var constant in constants)
                        {
                            parameters.Add(Value.FromConstant(constant));
                        }
                    }
                    break;

                    case InstructionType.GotoLabel:
                        instruction = new GotoLabel();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.DefineFunction2:
                    {
                        instruction = new DefineFunction2();
                        var name       = reader.ReadStringAtOffset();
                        var nParams    = reader.ReadUInt32();
                        var nRegisters = reader.ReadByte();
                        var flags      = reader.ReadUInt24();

                        //list of parameter strings
                        var paramList = reader.ReadFixedSizeListAtOffset <FunctionArgument>(() => new FunctionArgument()
                            {
                                Register  = reader.ReadInt32(),
                                Parameter = reader.ReadStringAtOffset(),
                            }, nParams);

                        parameters.Add(Value.FromString(name));
                        parameters.Add(Value.FromInteger((int)nParams));
                        parameters.Add(Value.FromInteger((int)nRegisters));
                        parameters.Add(Value.FromInteger((int)flags));
                        foreach (var param in paramList)
                        {
                            parameters.Add(Value.FromInteger(param.Register));
                            parameters.Add(Value.FromString(param.Parameter));
                        }
                        //body size of the function
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        //skip 8 bytes
                        reader.ReadUInt64();
                    }
                    break;

                    case InstructionType.PushData:
                    {
                        instruction = new PushData();

                        var count     = reader.ReadUInt32();
                        var constants = reader.ReadFixedSizeArrayAtOffset <uint>(() => reader.ReadUInt32(), count);

                        foreach (var constant in constants)
                        {
                            parameters.Add(Value.FromConstant(constant));
                        }
                    }
                    break;

                    case InstructionType.BranchAlways:
                    {
                        instruction = new BranchAlways();
                        var offset = reader.ReadInt32();
                        parameters.Add(Value.FromInteger(offset));
                        helper.ReportBranchOffset(offset);
                    }
                    break;

                    case InstructionType.GetURL2:
                        instruction = new GetUrl2();
                        break;

                    case InstructionType.DefineFunction:
                    {
                        instruction = new DefineFunction();
                        var name = reader.ReadStringAtOffset();
                        //list of parameter strings
                        var paramList = reader.ReadListAtOffset <string>(() => reader.ReadStringAtOffset());

                        parameters.Add(Value.FromString(name));
                        parameters.Add(Value.FromInteger(paramList.Count));
                        foreach (var param in paramList)
                        {
                            parameters.Add(Value.FromString(param));
                        }
                        //body size of the function
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        //skip 8 bytes
                        reader.ReadUInt64();
                    }
                    break;

                    case InstructionType.BranchIfTrue:
                    {
                        instruction = new BranchIfTrue();
                        var offset = reader.ReadInt32();
                        parameters.Add(Value.FromInteger(offset));
                        helper.ReportBranchOffset(offset);
                    }
                    break;

                    case InstructionType.GotoFrame2:
                        instruction = new GotoFrame2();
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        break;

                    case InstructionType.EA_PushString:
                        instruction = new PushString();
                        //the constant id that should be pushed
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_PushConstantByte:
                        instruction = new PushConstantByte();
                        //the constant id that should be pushed
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_GetStringVar:
                        instruction = new GetStringVar();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_SetStringVar:
                        instruction = new SetStringVar();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_GetStringMember:
                        instruction = new GetStringMember();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_SetStringMember:
                        instruction = new SetStringMember();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_PushValueOfVar:
                        instruction = new PushValueOfVar();
                        //the constant id that should be pushed
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_GetNamedMember:
                        instruction = new GetNamedMember();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_CallNamedFuncPop:
                        instruction = new CallNamedFuncPop();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_CallNamedFunc:
                        instruction = new CallNamedFunc();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_CallNamedMethodPop:
                        instruction = new CallNamedMethodPop();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_PushFloat:
                        instruction = new PushFloat();
                        parameters.Add(Value.FromFloat(reader.ReadSingle()));
                        break;

                    case InstructionType.EA_PushByte:
                        instruction = new PushByte();
                        parameters.Add(Value.FromInteger(reader.ReadByte()));
                        break;

                    case InstructionType.EA_PushShort:
                        instruction = new PushShort();
                        parameters.Add(Value.FromInteger(reader.ReadUInt16()));
                        break;

                    case InstructionType.End:
                        instruction = new End();
                        break;

                    case InstructionType.EA_CallNamedMethod:
                        instruction = new CallNamedMethod();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.Var:
                        instruction = new Var();
                        break;

                    case InstructionType.EA_PushRegister:
                        instruction = new PushRegister();
                        parameters.Add(Value.FromInteger(reader.ReadByte()));
                        break;

                    case InstructionType.EA_PushConstantWord:
                        instruction = new PushConstantWord();
                        parameters.Add(Value.FromConstant(reader.ReadUInt16()));
                        break;

                    case InstructionType.EA_CallFuncPop:
                        instruction = new CallFunctionPop();
                        break;

                    case InstructionType.StrictEqual:
                        instruction = new StrictEquals();
                        break;

                    default:
                        throw new InvalidDataException("Unimplemented bytecode instruction:" + type.ToString());
                    }

                    if (instruction != null)
                    {
                        instruction.Parameters = parameters;
                        instructions.Add(instructionPosition, instruction);
                    }
                }
            }

            return(new InstructionCollection(instructions));
        }
Пример #27
0
        public void SayHelloTest()
        {
            var greater = new Greater();

            Assert.AreEqual("Hello my dear Anton", greater.SayHello("Anton"));
        }
Пример #28
0
        private static Operator GetOperator(char[] sql, int beginIndex, int endIndex)
        {
            char c;

            Operator op1 = null;
            Operator op2 = null;

            for (int i = beginIndex; i < endIndex;)
            {
                c = sql[i];

                if (c == '+')
                {
                    op2 = new Add(sql, i, i);
                    i++;
                }
                else if (c == '-')
                {
                    op2 = new Sub(sql, i, i);
                    i++;
                }
                else if (c == '*')
                {
                    op2 = new Multi(sql, i, i);
                    i++;
                }
                else if (c == '/')
                {
                    op2 = new Div(sql, i, i);
                    i++;
                }
                else if (
                    (i + 2 <= endIndex)
                    &&
                    (c == 'a' && sql[i + 1] == 'n' && sql[i + 2] == 'd')
                    &&
                    (i == beginIndex || StrUtil.IsOneOf(sql[i - 1], Parser._whiteSpaces))
                    &&
                    (i + 2 == endIndex || i + 3 <= endIndex && StrUtil.IsOneOf(sql[i + 3], Parser._whiteSpaces))
                    )
                {
                    op2 = new And(sql, i, i + 2);
                    i   = i + 3;
                }
                else if (
                    (i + 1 <= endIndex)
                    &&
                    (c == 'o' && sql[i + 1] == 'r')
                    &&
                    (i == beginIndex || StrUtil.IsOneOf(sql[i - 1], Parser._whiteSpaces))
                    &&
                    (i + 1 == endIndex || i + 2 <= endIndex && StrUtil.IsOneOf(sql[i + 2], Parser._whiteSpaces))
                    )
                {
                    op2 = new Or(sql, i, i + 1);
                    i   = i + 2;
                }
                else if (
                    (i + 2 <= endIndex)
                    &&
                    (c == 'n' && sql[i + 1] == 'o' && sql[i + 2] == 't')
                    &&
                    (i == beginIndex || StrUtil.IsOneOf(sql[i - 1], Parser._whiteSpaces))
                    &&
                    (i + 2 == endIndex || i + 3 <= endIndex && StrUtil.IsOneOf(sql[i + 3], Parser._whiteSpaces))
                    )
                {
                    op2 = new Not(sql, i, i + 2);
                    i   = i + 3;
                }
                else if (
                    (i + 1 <= endIndex)
                    &&
                    (c == '>' && sql[i + 1] == '=')
                    )
                {
                    op2 = new GreaterEqual(sql, i, i + 1);
                    i   = i + 2;
                }
                else if (
                    (i + 1 <= endIndex)
                    &&
                    (c == '<' && sql[i + 1] == '=')
                    )
                {
                    op2 = new LessEqual(sql, i, i + 1);
                    i   = i + 2;
                }
                else if (
                    (i + 1 <= endIndex)
                    &&
                    (c == '!' && sql[i + 1] == '=')
                    )
                {
                    op2 = new NotEqual(sql, i, i + 1);
                    i   = i + 2;
                }
                else if (c == '=')
                {
                    op2 = new Equal(sql, i, i);
                    i++;
                }
                else if (c == '>')
                {
                    op2 = new Greater(sql, i, i);
                    i++;
                }
                else if (c == '<')
                {
                    op2 = new Less(sql, i, i);
                    i++;
                }
                else
                {
                    i++;
                }



                if (op1 == null)
                {
                    if (op2 != null)
                    {
                        op1 = op2;
                    }
                }
                //  对于 Priority(优先级) 相同 的 运算符,取 最左边 的 ,  这是 为了 适应  Not(非) 运算符 的 特性
                //  Not 运算符 是 一元运算符 。 其它 运算符 都是 二元运算符 , 从左往右 结合 和 从右往左 结合 都一样
                //  但 Not 运算符 只能 从左往右 结合, 因为 从右往左 结合 会 找不到 操作数
                //  从 编程 的 角度 来 看, 从左往右 的 思路 也 比较 顺
                //  但 等价性 上来看, 我们 人类 的 计算习惯 是 从左到右, 而 这个 计算顺序 对应在 计算机 里 是 从右往左 结合运算符
                //  计算机 从右往左 结合运算符 之后 执行计算 的 顺序 就是 从左到右
                //  而 从左往右 结合运算符 的 执行计算顺序 是 从右到左
                //  但 不管 计算顺序 是 从左到右 还是 从右到左, 结果 都一样
                //  综上, 从 编程 的 角度 考虑, 我们选择 了 从左到右 结合 运算符, 执行计算 的 顺序 就是 从右到左
                else if (op2 != null && op2.Priority < op1.Priority)
                {
                    op1 = op2;
                }

                //if (c == '+')
                //    return new Add(sql, i, i);
                //else if (c == '-')
                //    return new Sub(sql, i, i);
                //else if (c == '*')
                //    return new Multi(sql, i, i);
                //else if (c == '/')
                //    return new Div(sql, i, i);
                //else if (c == 'a' && sql[i + 1] == 'n' && sql[i + 2] == 'd')
                //    return new And(sql, i, i + 2);
                //else if (c == 'o' && sql[i + 1] == 'r')
                //    return new Or(sql, i, i + 1);
                //else if (c == 'n' && sql[i + 1] == 'o' && sql[i + 1] == 't')
                //    return new Not(sql, i, i + 2);
                //else if (c == '=')
                //    return new Equal(sql, i, i + 1);
                //else if (c == '>')
                //    return new Greater(sql, i, i);
                //else if (c == '<')
                //    return new Less(sql, i, i);
                //else if (c == '>' && sql[i + 1] == '=')
                //    return new GreaterEqual(sql, i, i + 1);
                //else if (c == '<' && sql[i + 1] == '=')
                //    return new LessEqual(sql, i, i + 1);
                //else if (c == '!' && sql[i + 1] == '=')
                //    return new NotEqual(sql, i, i + 1);
            }

            return(op1);
            //return null;
        }
Пример #29
0
        public object compilar(Entorno ent, Errores errores)
        {
            //CREO MIS UTILIDADES
            //ASIGNACION INICIAL
            AsignacionId target            = new AsignacionId(id, null, linea, columna);
            Asignacion   asignacionInicial = new Asignacion(target, primero, linea, columna);
            //ACTUALIZACION DE LA VARIABLE Y CONDICION A EVALUAR
            Asignacion actualizarVariable;
            Primitivo  valorFAD = new Primitivo(Tipos.INTEGER, "1", linea, columna);
            Expresion  condicion;
            AccessId   left = new AccessId(id, null, linea, columna);

            if (fad.Equals("to"))
            {
                Less menorIgual = new Less(true, left, segundo, linea, columna);
                condicion = (Expresion)menorIgual;
                Suma suma = new Suma(left, valorFAD, linea, columna);
                actualizarVariable = new Asignacion(target, (Expresion)suma, linea, columna);
            }
            else   //downto
            {
                Greater mayorIgual = new Greater(true, left, segundo, linea, columna);
                condicion = (Expresion)mayorIgual;
                Resta resta = new Resta(left, valorFAD, linea, columna);
                actualizarVariable = new Asignacion(target, (Expresion)resta, linea, columna);
            }
            //INICIO LA COMPILACION
            try
            {
                Generator generator = Generator.getInstance();
                generator.addComment("Inicia FOR");
                asignacionInicial.compilar(ent, errores);
                string lblFor = generator.newLabel();
                generator.addLabel(lblFor);
                Retorno retcondicion = condicion.compilar(ent);
                if (retcondicion.type.tipo == Tipos.BOOLEAN)
                {
                    ent.fors.AddLast(new IteFor(true, actualizarVariable));
                    ent.ybreak.AddLast(retcondicion.falseLabel);
                    ent.ycontinue.AddLast(lblFor);
                    generator.addLabel(retcondicion.trueLabel);
                    foreach (Instruccion sentencia in sentencias)
                    {
                        sentencia.compilar(ent, errores);
                    }
                    actualizarVariable.compilar(ent, errores);
                    generator.addGoto(lblFor);
                    generator.addLabel(retcondicion.falseLabel);
                    ent.fors.RemoveLast();
                    ent.ybreak.RemoveLast();
                    ent.ycontinue.RemoveLast();
                    generator.addComment("Finaliza FOR");
                }
                else
                {
                    throw new Error("Semántico", "La condicion a evaluar en el for no es de tipo Boolean", ent.obtenerAmbito(), linea, columna);
                }
            } catch (Error ex)
            {
                errores.agregarError(ex);
            }
            return(null);
        }
 //-----------------------------------------------------------
 private Type Visit(Greater node, Table table)
 {
     VisitBinaryOperator(">", node, Type.INTEGER, table);
     return(Type.BOOLEAN);
 }
Пример #31
0
 public GreaterToQ()
 {
     SourceTag = new Greater();
     ResultTag = new Q();
 }