public void compares_different_blocks()
        {
            var o1 = UnifiedBlock.Create(
                new UnifiedExpression[] {
                UnifiedBinaryExpression.Create(
                    UnifiedVariableIdentifier.Create("n"),
                    UnifiedBinaryOperator.Create(
                        "=",
                        UnifiedBinaryOperatorKind.Assign),
                    UnifiedInt32Literal.Create(1)),
                UnifiedReturn.Create(
                    UnifiedInt32Literal.Create(2)),
            });
            var o2 = UnifiedBlock.Create(
                new UnifiedExpression[] {
                UnifiedBinaryExpression.Create(
                    UnifiedVariableIdentifier.Create("n"),
                    UnifiedBinaryOperator.Create(
                        "=",
                        UnifiedBinaryOperatorKind.Assign),
                    UnifiedInt32Literal.Create(2)),
                UnifiedReturn.Create(
                    UnifiedInt32Literal.Create(2)),
            });

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
        private static UnifiedExpression CreateAttrasgn(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "attrasgn");
            var left  = CreateExpresion(node.FirstElement());
            var name  = node.NthElement(1);
            var args  = CreateArglist(node.LastElement());
            var right = args[args.Count - 1].Value;

            args.RemoveAt(args.Count - 1);
            right.RemoveSelf();
            UnifiedExpression expression;

            if (name.Value == "[]=")
            {
                expression = UnifiedIndexer.Create(left, args);
            }
            else
            {
                var propName = name.Value;
                propName   = propName.Substring(0, propName.Length - 1);
                expression = UnifiedProperty.Create(
                    ".", left, UnifiedVariableIdentifier.Create(propName));
            }
            return(UnifiedBinaryExpression.Create(
                       expression,
                       UnifiedBinaryOperator.Create(
                           "=", UnifiedBinaryOperatorKind.Assign),
                       right));
        }
 private static IEnumerable <UnifiedExpression> CreateProcessAndAssignStatement(ProcessAndAssignStatement statement)
 {
     yield return(UnifiedBinaryExpression.Create(
                      CreatePhrase(statement.VariablePhrase),
                      UnifiedBinaryOperator.Create("=", UnifiedBinaryOperatorKind.Assign),
                      CreatePhrase(statement.ValuePhrase)));
 }
 private static IEnumerable <UnifiedExpression> CreateAssignStatement(
     AssignStatement statement)
 {
     yield return(UnifiedBinaryExpression.Create(
                      CreatePhrase(statement.Left),
                      UnifiedBinaryOperator.Create("=", UnifiedBinaryOperatorKind.Assign),
                      CreatePhrase(statement.Right)));
 }
Exemple #5
0
        public UnifiedElement VisitIsExpression(IsExpression expr, object data)
        {
            var op = UnifiedBinaryOperator.Create(
                "is", UnifiedBinaryOperatorKind.InstanceOf);
            var value = expr.Expression.TryAcceptForExpression(this);
            var type  = LookupType(expr.Type);

            return(UnifiedBinaryExpression.Create(value, op, type));
        }
 private static UnifiedExpression CreateMatch3(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "match3");
     return(UnifiedBinaryExpression.Create(
                CreateExpresion(node.FirstElement()),
                UnifiedBinaryOperator.Create(
                    "=~", UnifiedBinaryOperatorKind.RegexEqual),
                CreateExpresion(node.LastElement())));
 }
Exemple #7
0
        public UnifiedElement VisitAsExpression(
            AsExpression asExpr, object data)
        {
            var op = UnifiedBinaryOperator.Create(
                "as", UnifiedBinaryOperatorKind.As);
            var value = asExpr.Expression.TryAcceptForExpression(this);
            var type  = LookupType(asExpr.Type);

            return(UnifiedBinaryExpression.Create(value, op, type));
        }
 private static UnifiedExpression CreateOpAsgnOr(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "op_asgn_or");
     // 優先順位の違いはASTの深さ(|| / or)
     return(UnifiedBinaryExpression.Create(
                CreateExpresion(node.FirstElement()),
                UnifiedBinaryOperator.Create(
                    "||=", UnifiedBinaryOperatorKind.OrElseAssign),
                CreateExpresion(node.LastElement())));
 }
 private static UnifiedExpression CreateOpAsgnAnd(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "op_asgn_and");
     // 優先順位の違いはASTの深さ(&& / and)
     return(UnifiedBinaryExpression.Create(
                CreateExpresion(node.FirstElement()),
                UnifiedBinaryOperator.Create(
                    "&&=", UnifiedBinaryOperatorKind.AndAlsoAssign),
                CreateExpresion(node.LastElement())));
 }
        public void compares_same_expressions()
        {
            var o2 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"),
                UnifiedBinaryOperator.Create(
                    "-", UnifiedBinaryOperatorKind.Subtract),
                UnifiedInt32Literal.Create(1));
            var o1 = o2;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
 public static UnifiedBinaryExpression CreateAsgn(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(
         node.Name() == "lasgn" || node.Name() == "masgn" ||
         node.Name() == "iasgn" ||
         node.Name() == "gasgn" || node.Name() == "cvdecl" ||
         node.Name() == "cdecl");
     return(UnifiedBinaryExpression.Create(
                CreateExpresion(node.FirstElement()),
                UnifiedBinaryOperator.Create(
                    "=", UnifiedBinaryOperatorKind.Assign),
                CreateExpresion(node.LastElement())));
 }
        private UnifiedBinaryOperator LookupAssignOperator(AssignmentOperatorType type)
        {
            switch (type)
            {
            case AssignmentOperatorType.Assign:
                return(UnifiedBinaryOperator.Create(
                           "=", UnifiedBinaryOperatorKind.Assign));

            case AssignmentOperatorType.Add:
                return(UnifiedBinaryOperator.Create(
                           "+=", UnifiedBinaryOperatorKind.AddAssign));

            case AssignmentOperatorType.Subtract:
                return(UnifiedBinaryOperator.Create(
                           "-=", UnifiedBinaryOperatorKind.SubtractAssign));

            case AssignmentOperatorType.Multiply:
                return(UnifiedBinaryOperator.Create(
                           "*=", UnifiedBinaryOperatorKind.MultiplyAssign));

            case AssignmentOperatorType.Divide:
                return(UnifiedBinaryOperator.Create(
                           "/=", UnifiedBinaryOperatorKind.DivideAssign));

            case AssignmentOperatorType.Modulus:
                return(UnifiedBinaryOperator.Create(
                           "%=", UnifiedBinaryOperatorKind.ModuloAssign));

            case AssignmentOperatorType.ShiftLeft:
                return(UnifiedBinaryOperator.Create(
                           "<<=", UnifiedBinaryOperatorKind.ArithmeticLeftShiftAssign));

            case AssignmentOperatorType.ShiftRight:
                return(UnifiedBinaryOperator.Create(
                           ">>=", UnifiedBinaryOperatorKind.ArithmeticRightShiftAssign));

            case AssignmentOperatorType.BitwiseAnd:
                return(UnifiedBinaryOperator.Create(
                           "&=", UnifiedBinaryOperatorKind.AndAssign));

            case AssignmentOperatorType.BitwiseOr:
                return(UnifiedBinaryOperator.Create(
                           "|=", UnifiedBinaryOperatorKind.OrAssign));

            case AssignmentOperatorType.ExclusiveOr:
                return(UnifiedBinaryOperator.Create(
                           "^=", UnifiedBinaryOperatorKind.ExclusiveOrAssign));
            }
            throw new ArgumentException("Unknown operator: " + type);
        }
        public void compares_different_expressions_containing_null()
        {
            var o1 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"), null,
                UnifiedInt32Literal.Create(1));
            var o2 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"),
                UnifiedBinaryOperator.Create(
                    null, UnifiedBinaryOperatorKind.Add),
                UnifiedInt32Literal.Create(1));

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
        public void compares_different_calls()
        {
            var o1 =
                UnifiedCall.Create(
                    UnifiedVariableIdentifier.Create("f"),
                    UnifiedSet <UnifiedArgument> .Create(
                        UnifiedArgument.Create(
                            UnifiedVariableIdentifier.Create(
                                "a"), null, null),
                        UnifiedArgument.Create(
                            UnifiedBinaryExpression.Create(
                                UnifiedVariableIdentifier.
                                Create("n"),
                                UnifiedBinaryOperator.Create
                                (
                                    "-",
                                    UnifiedBinaryOperatorKind
                                    .Add),
                                UnifiedInt32Literal.Create(
                                    1)
                                ), null, null)));
            var o2 =
                UnifiedCall.Create(
                    UnifiedVariableIdentifier.Create("f"),
                    UnifiedSet <UnifiedArgument> .Create(
                        UnifiedArgument.Create(
                            UnifiedVariableIdentifier.Create(
                                "a"), null, null),
                        UnifiedArgument.Create(
                            UnifiedBinaryExpression.Create(
                                UnifiedVariableIdentifier.
                                Create("n2"),
                                UnifiedBinaryOperator.Create
                                (
                                    "-",
                                    UnifiedBinaryOperatorKind
                                    .Add),
                                UnifiedInt32Literal.Create(
                                    1)), null, null)));

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
        private static UnifiedBinaryOperator CreateConditionSuffixToken(ConditionSuffixToken token)
        {
            switch (token.CompareType)
            {
            case CompareTypes.Equal:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.Equal));

            case CompareTypes.NotEqual:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.NotEqual));

            case CompareTypes.LargerThan:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.GreaterThan));

            case CompareTypes.LargerEqual:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.GreaterThanOrEqual));

            case CompareTypes.SmallerThan:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.LessThan));

            case CompareTypes.SmallEqual:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.LessThanOrEqual));

            case CompareTypes.And:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.And));

            case CompareTypes.Or:
                return(UnifiedBinaryOperator.Create(
                           token.Text, UnifiedBinaryOperatorKind.Or));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private static UnifiedBinaryOperator LookupBinaryOperator(
            BinaryOperatorType op)
        {
            switch (op)
            {
            case BinaryOperatorType.Add:
                return(UnifiedBinaryOperator.Create(
                           "+", UnifiedBinaryOperatorKind.Add));

            case BinaryOperatorType.Subtract:
                return(UnifiedBinaryOperator.Create(
                           "-", UnifiedBinaryOperatorKind.Subtract));

            case BinaryOperatorType.Multiply:
                return(UnifiedBinaryOperator.Create(
                           "*", UnifiedBinaryOperatorKind.Multiply));

            case BinaryOperatorType.Divide:
                return(UnifiedBinaryOperator.Create(
                           "/", UnifiedBinaryOperatorKind.Divide));

            case BinaryOperatorType.Modulus:
                return(UnifiedBinaryOperator.Create(
                           "%", UnifiedBinaryOperatorKind.Modulo));

            case BinaryOperatorType.ShiftLeft:
                return(UnifiedBinaryOperator.Create(
                           "<<", UnifiedBinaryOperatorKind.ArithmeticLeftShift));

            case BinaryOperatorType.ShiftRight:
                return(UnifiedBinaryOperator.Create(
                           ">>", UnifiedBinaryOperatorKind.ArithmeticRightShift));

            case BinaryOperatorType.BitwiseAnd:
                return(UnifiedBinaryOperator.Create(
                           "&", UnifiedBinaryOperatorKind.And));

            case BinaryOperatorType.BitwiseOr:
                return(UnifiedBinaryOperator.Create(
                           "|", UnifiedBinaryOperatorKind.Or));

            case BinaryOperatorType.ExclusiveOr:
                return(UnifiedBinaryOperator.Create(
                           "^", UnifiedBinaryOperatorKind.ExclusiveOr));

            case BinaryOperatorType.GreaterThan:
                return(UnifiedBinaryOperator.Create(
                           ">", UnifiedBinaryOperatorKind.GreaterThan));

            case BinaryOperatorType.GreaterThanOrEqual:
                return(UnifiedBinaryOperator.Create(
                           ">=", UnifiedBinaryOperatorKind.GreaterThanOrEqual));

            case BinaryOperatorType.LessThanOrEqual:
                return(UnifiedBinaryOperator.Create(
                           "<=", UnifiedBinaryOperatorKind.LessThanOrEqual));

            case BinaryOperatorType.LessThan:
                return(UnifiedBinaryOperator.Create(
                           "<", UnifiedBinaryOperatorKind.LessThan));

            case BinaryOperatorType.Equality:
                return(UnifiedBinaryOperator.Create(
                           "==", UnifiedBinaryOperatorKind.Equal));

            case BinaryOperatorType.InEquality:
                return(UnifiedBinaryOperator.Create(
                           "!=", UnifiedBinaryOperatorKind.NotEqual));

            case BinaryOperatorType.ConditionalAnd:
                return(UnifiedBinaryOperator.Create(
                           "&&", UnifiedBinaryOperatorKind.AndAlso));

            case BinaryOperatorType.ConditionalOr:
                return(UnifiedBinaryOperator.Create(
                           "||", UnifiedBinaryOperatorKind.OrElse));

            case BinaryOperatorType.NullCoalescing:
                return(UnifiedBinaryOperator.Create(
                           "??", UnifiedBinaryOperatorKind.Coalesce));
            }
            throw new ArgumentException("Unknown operator: " + op);
        }
Exemple #17
0
        public static UnifiedBinaryOperator CreateAssignmentOperator(
            XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "assignment_operator");

            /*
             * assignment_operator
             * : '='
             | '*='
             | '/='
             | '%='
             | '+='
             | '-='
             | '<<='
             | '>>='
             | '&='
             | '^='
             | '|='
             */

            UnifiedBinaryOperatorKind kind;

            switch (node.Value)
            {
            case "=":
                kind = UnifiedBinaryOperatorKind.Assign;
                break;

            case "*=":
                kind = UnifiedBinaryOperatorKind.MultiplyAssign;
                break;

            case "/=":
                kind = UnifiedBinaryOperatorKind.DivideAssign;
                break;

            case "%=":
                kind = UnifiedBinaryOperatorKind.ModuloAssign;
                break;

            case "+=":
                kind = UnifiedBinaryOperatorKind.AddAssign;
                break;

            case "-=":
                kind = UnifiedBinaryOperatorKind.SubtractAssign;
                break;

            case "<<=":
                kind = UnifiedBinaryOperatorKind.LogicalLeftShiftAssign;
                break;

            case ">>=":
                kind = UnifiedBinaryOperatorKind.ArithmeticRightShiftAssign;
                break;

            case "&=":
                kind = UnifiedBinaryOperatorKind.AndAssign;
                break;

            case "^=":
                kind = UnifiedBinaryOperatorKind.ExclusiveOrAssign;
                break;

            case "|=":
                kind = UnifiedBinaryOperatorKind.OrAssign;
                break;

            default:
                throw new InvalidOperationException();
            }

            return(UnifiedBinaryOperator.Create(node.Value, kind));
        }
        CreateBinaryOperatorDictionary()
        {
            var dict = new[] {
                // Arithmetic
                UnifiedBinaryOperator.Create(
                    "+", UnifiedBinaryOperatorKind.Add),
                UnifiedBinaryOperator.Create(
                    "-", UnifiedBinaryOperatorKind.Subtract),
                UnifiedBinaryOperator.Create(
                    "*", UnifiedBinaryOperatorKind.Multiply),
                UnifiedBinaryOperator.Create(
                    "**", UnifiedBinaryOperatorKind.Power),
                UnifiedBinaryOperator.Create(
                    "/", UnifiedBinaryOperatorKind.Divide),
                UnifiedBinaryOperator.Create(
                    "//", UnifiedBinaryOperatorKind.FloorDivide),
                UnifiedBinaryOperator.Create(
                    "%", UnifiedBinaryOperatorKind.Modulo),
                // Shift
                UnifiedBinaryOperator.Create(
                    "<<", UnifiedBinaryOperatorKind.LogicalLeftShift),
                UnifiedBinaryOperator.Create(
                    ">>", UnifiedBinaryOperatorKind.ArithmeticRightShift)
                ,
                UnifiedBinaryOperator.Create(
                    ">>>", UnifiedBinaryOperatorKind.LogicalRightShift),
                // Comparison
                UnifiedBinaryOperator.Create(
                    ">", UnifiedBinaryOperatorKind.GreaterThan),
                UnifiedBinaryOperator.Create(
                    ">=", UnifiedBinaryOperatorKind.GreaterThanOrEqual),
                UnifiedBinaryOperator.Create(
                    "<", UnifiedBinaryOperatorKind.LessThan),
                UnifiedBinaryOperator.Create(
                    "<=", UnifiedBinaryOperatorKind.LessThanOrEqual),
                UnifiedBinaryOperator.Create(
                    "==", UnifiedBinaryOperatorKind.Equal),
                UnifiedBinaryOperator.Create(
                    "!=", UnifiedBinaryOperatorKind.NotEqual),
                UnifiedBinaryOperator.Create(
                    "<>", UnifiedBinaryOperatorKind.NotEqual),
                UnifiedBinaryOperator.Create(
                    "===", UnifiedBinaryOperatorKind.StrictEqual),
                UnifiedBinaryOperator.Create(
                    "!==", UnifiedBinaryOperatorKind.StrictNotEqual),
                UnifiedBinaryOperator.Create(
                    "is", UnifiedBinaryOperatorKind.ReferenceEqual),
                UnifiedBinaryOperator.Create(
                    "in", UnifiedBinaryOperatorKind.In),
                // Logocal
                UnifiedBinaryOperator.Create(
                    "&&", UnifiedBinaryOperatorKind.AndAlso),
                UnifiedBinaryOperator.Create(
                    "||", UnifiedBinaryOperatorKind.OrElse),
                UnifiedBinaryOperator.Create(
                    "and", UnifiedBinaryOperatorKind.AndAlso),
                UnifiedBinaryOperator.Create(
                    "or", UnifiedBinaryOperatorKind.OrElse),
                // Bit
                UnifiedBinaryOperator.Create(
                    "&", UnifiedBinaryOperatorKind.And),
                UnifiedBinaryOperator.Create(
                    "|", UnifiedBinaryOperatorKind.Or),
                UnifiedBinaryOperator.Create(
                    "^", UnifiedBinaryOperatorKind.ExclusiveOr),
                // Arithmetic Assignment
                UnifiedBinaryOperator.Create(
                    "=", UnifiedBinaryOperatorKind.Assign),
                UnifiedBinaryOperator.Create(
                    "+=", UnifiedBinaryOperatorKind.AddAssign),
                UnifiedBinaryOperator.Create(
                    "-=", UnifiedBinaryOperatorKind.SubtractAssign),
                UnifiedBinaryOperator.Create(
                    "*=", UnifiedBinaryOperatorKind.MultiplyAssign),
                UnifiedBinaryOperator.Create(
                    "**=", UnifiedBinaryOperatorKind.PowerAssign),
                UnifiedBinaryOperator.Create(
                    "/=", UnifiedBinaryOperatorKind.DivideAssign),
                UnifiedBinaryOperator.Create(
                    "//=", UnifiedBinaryOperatorKind.FloorDivideAssign),
                UnifiedBinaryOperator.Create(
                    "%=", UnifiedBinaryOperatorKind.ModuloAssign),
                // Bit Assignment
                UnifiedBinaryOperator.Create(
                    "&=", UnifiedBinaryOperatorKind.AndAssign),
                UnifiedBinaryOperator.Create(
                    "|=", UnifiedBinaryOperatorKind.OrAssign),
                UnifiedBinaryOperator.Create(
                    "^=", UnifiedBinaryOperatorKind.ExclusiveOrAssign),
                // Logocal Assignment
                UnifiedBinaryOperator.Create(
                    "&&=", UnifiedBinaryOperatorKind.AndAlsoAssign),
                UnifiedBinaryOperator.Create(
                    "||=", UnifiedBinaryOperatorKind.OrElseAssign),
                // Shift Assignment
                UnifiedBinaryOperator.Create(
                    "<<=",
                    UnifiedBinaryOperatorKind.LogicalLeftShiftAssign),
                UnifiedBinaryOperator.Create(
                    ">>=",
                    UnifiedBinaryOperatorKind.ArithmeticRightShiftAssign)
                ,
                UnifiedBinaryOperator.Create(
                    ">>>=",
                    UnifiedBinaryOperatorKind.LogicalRightShiftAssign),
                // InstanceOf
                UnifiedBinaryOperator.Create(
                    "instanceof", UnifiedBinaryOperatorKind.InstanceOf),
            }
            .ToDictionary(o => o.Sign);

            dict["isnot"] = UnifiedBinaryOperator.Create(
                "is not", UnifiedBinaryOperatorKind.ReferenceNotEqual);
            dict["notin"] = UnifiedBinaryOperator.Create(
                "not in", UnifiedBinaryOperatorKind.NotIn);
            return(dict);
        }