private static UnifiedExpression CreateDsym(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "dsym");
     // TODO: Implement
     return(UnifiedVariableIdentifier.Create(node.Value));
 }
        public static UnifiedExpression CreatePrimaryExpression(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "primary_expression");

            /*
             * primary_expression
             * : IDENTIFIER
             | constant
             | '(' expression ')'
             */

            var first = node.FirstElement();

            if (first.Name == "IDENTIFIER")
            {
                return(UnifiedVariableIdentifier.Create(first.Value));
            }
            if (first.Name == "constant")
            {
                return(CreateConstant(first));
            }
            var second = node.NthElement(1);

            if (second.Name == "expression")
            {
                return(CreateExpression(second).ElementAt(0));
            }
            throw new InvalidOperationException();
        }
        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));
        }
예제 #5
0
        public UnifiedElement VisitNamedArgumentExpression(
            NamedArgumentExpression expr, object data)
        {
            var name  = UnifiedVariableIdentifier.Create(expr.Name);
            var value = expr.Expression.TryAcceptForExpression(this);

            return(UnifiedArgument.Create(value: value, target: name));
        }
예제 #6
0
        public UnifiedElement VisitCatchClause(CatchClause catchClause, object data)
        {
            var type = LookupType(catchClause.Type);
            var name = UnifiedVariableIdentifier.Create(catchClause.VariableName);
            var body = catchClause.Body.TryAcceptForExpression(this).ToBlock();

            return(UnifiedCatch.Create(type.ToSet(), name, body));
        }
 public static UnifiedExpression CreateVar(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(
         node.Name() == "lvar" || node.Name() == "ivar" ||
         node.Name() == "cvar" ||
         node.Name() == "gvar");
     return(UnifiedVariableIdentifier.Create(node.Value));
 }
        public void compares_same_expressions_containing_null()
        {
            var o2 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"), null,
                UnifiedInt32Literal.Create(1));
            var o1 = o2;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
예제 #9
0
        public UnifiedElement VisitOperatorDeclaration(OperatorDeclaration dec, object data)
        {
            var attrs      = dec.Attributes.AcceptVisitorAsAttrs(this, data);
            var mods       = LookupModifiers(dec.Modifiers);
            var type       = LookupType(dec.ReturnType);
            var name       = UnifiedVariableIdentifier.Create(dec.Name);
            var parameters = dec.Parameters.AcceptVisitorAsParams(this, data);
            var body       = dec.Body.TryAcceptForExpression(this).ToBlock();

            return(UnifiedFunctionDefinition.Create(
                       attrs, mods, type, /*generics*/ null, name, parameters, /*throws*/ null, body));
        }
        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 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);
        }
        private static UnifiedExpression CreateOpAsgn2(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "op_asgn2");
            var propName   = node.NthElement(1).Value;
            var expression = UnifiedProperty.Create(
                ".", CreateExpresion(node.FirstElement()),
                UnifiedVariableIdentifier.Create(
                    propName.Substring(0, propName.Length - 1)));

            return(UnifiedBinaryExpression.Create(
                       expression,
                       Sign2BinaryOperator[node.NthElement(2).Value + "="].DeepCopy
                           (),
                       CreateExpresion(node.LastElement())));
        }
예제 #13
0
        CreateSpecifierQualifierList(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "specifier_qualifier_list");

            /*
             * specifier_qualifier_list
             * : ( type_qualifier | type_specifier )+
             */
            var modifiers = UnifiedSet <UnifiedModifier> .Create();

            var types = new List <UnifiedType>();

            foreach (var e in node.Elements())
            {
                switch (e.Name())
                {
                case "type_qualifier":
                    modifiers.Add(CreateTypeQualifier(e));
                    break;

                case "type_specifier":
                    types.Add((UnifiedType)CreateTypeSpecifier(e));
                    break;
                }
            }
            // 修飾子が空の場合はnullにする
            if (modifiers.IsEmpty())
            {
                modifiers = null;
            }

            var s      = "";
            var prefix = "";

            foreach (var t in types)
            {
                s     += prefix + t.BasicTypeName;
                prefix = " ";
            }
            var type = s.Equals("")
                                                           ? null
                                                           : UnifiedType.Create(
                UnifiedVariableIdentifier.Create(s));

            return(Tuple.Create(modifiers, type));
        }
예제 #14
0
        public static UnifiedType CreateStructOrUnionSpecifier(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "struct_or_union_specifier");

            /*	struct_or_union_specifier
             * : struct_or_union IDENTIFIER? '{' struct_declaration_list '}'
             * | struct_or_union IDENTIFIER
             */
            // 構造体の定義と宣言の両方をこのメソッドで作成
            // 常に UnifiedType を返すが、
            // 構造体定義をしている場合だけ関数の呼び出し元で UnifiedType の中身をとりだす

            // typedef "struct {}" data; -> クラス?
            // "struct data {}"; -> クラス
            // "struct data" a; -> 型

            var isStruct   = CreateStructOrUnion(node.FirstElement()) == "struct";
            var identifier = node.Element("IDENTIFIER");
            var typeName   = identifier == null
                                                                   ? null
                                                                   : UnifiedVariableIdentifier.Create(
                identifier.Value);

            // 型の場合
            if (node.Elements().Count() == 2)
            {
                var baseType = UnifiedType.Create(typeName);
                return(isStruct ? baseType.WrapStruct() : baseType.WrapUnion());
            }

            // struct or union の定義がある場合
            var body =
                CreateStructDeclarationList(
                    node.Element("struct_declaration_list"));
            var structOrUnion = isStruct
                                                                                ? (UnifiedClassLikeDefinition)
                                UnifiedStructDefinition.
                                Create(
                name: typeName,
                body: body)
                                                                                : UnifiedUnionDefinition.Create(
                name: typeName, body: body);

            // TODO struct or unionはあくまでもTypeとして返すののか?
            return(UnifiedType.Create(structOrUnion));
        }
        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);
        }
예제 #16
0
        CreateDirectDeclarator(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "direct_declarator");

            /* direct_declarator
             * :   (	IDENTIFIER  | '(' declarator ')' ) declarator_suffix*
             */

            var identifier = node.Element("IDENTIFIER");
            UnifiedIdentifier name;

            if (identifier != null)
            {
                name = UnifiedVariableIdentifier.Create(identifier.Value);
            }
            else if (node.Element("declarator") != null)
            {
                // TODO (test())(){ }が許されるとして、その場合はどうするのか
                return(CreateDeclarator(node.Element("declarator")));
            }
            else
            {
                throw new InvalidOperationException();
            }

            UnifiedSet <UnifiedParameter> parameters = null;

            if (node.Elements("declarator_suffix").Count() > 1)
            {
                // TODO test()()となるケースが未検出
                throw new NotImplementedException();
            }
            else if (node.Elements("declarator_suffix").Count() == 1)
            {
                parameters =
                    CreateDeclaratorSuffix(
                        node.Element("declarator_suffix"));
            }

            return(Tuple.Create(name, parameters));
        }
예제 #17
0
        public UnifiedElement VisitMethodDeclaration(
            MethodDeclaration dec, object data)
        {
            var attrs    = dec.Attributes.AcceptVisitorAsAttrs(this, data);
            var mods     = LookupModifiers(dec.Modifiers);
            var type     = LookupType(dec.ReturnType);
            var name     = UnifiedVariableIdentifier.Create(dec.Name);
            var generics = dec.TypeParameters.AcceptVisitorAsTypeParams(
                this, data);

            if (generics.IsEmptyOrNull())
            {
                generics = null;
            }
            var parameters = dec.Parameters.AcceptVisitorAsParams(this, data);
            var body       = dec.Body.TryAcceptForExpression(this).ToBlock();

            // TODO constraint
            return(UnifiedFunctionDefinition.Create(
                       attrs, mods, type, generics, name, parameters,
                       /* no throws */ null, body));
        }
예제 #18
0
        public static UnifiedExpression CreateTypeSpecifier(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "type_specifier");

            /*	type_specifier
             * : 'void'
             * | 'char'
             * | 'short'
             * | 'int'
             * | 'long'
             * | 'float'
             * | 'double'
             * | 'signed'
             * | 'unsigned'
             * | struct_or_union_specifier
             * | enum_specifier
             * | type_id
             */
            var first = node.FirstElement();

            switch (first.Name())
            {
            case "struct_or_union_specifier":
                return(CreateStructOrUnionSpecifier(first));

            case "enum_specifier":
                return(CreateEnumSpecifier(first));

            case "type_id":
                return(CreateTypeId(first));

            default:
                return
                    (UnifiedType.Create(
                         UnifiedVariableIdentifier.Create(first.Value)));
            }
        }
예제 #19
0
        public UnifiedElement VisitTypeDeclaration(
            TypeDeclaration dec, object data)
        {
            var attrs      = dec.Attributes.AcceptVisitorAsAttrs(this, data);
            var mods       = LookupModifiers(dec.Modifiers);
            var name       = UnifiedVariableIdentifier.Create(dec.Name);
            var typeParams = dec.TypeParameters.AcceptVisitorAsTypeParams(
                this, data);

            if (typeParams.Count == 0)
            {
                typeParams = null;
            }
            var extends = dec.BaseTypes.AcceptVisitorAsConstrains(this, data);
            var body    = UnifiedBlock.Create();

            foreach (var node in dec.Members)
            {
                var uExpr = node.TryAcceptForExpression(this);
                if (uExpr != null)
                {
                    body.Add(uExpr);
                }
            }
            // set constraint
            var dic = CreateDictionary(dec.Constraints);

            if (typeParams != null)
            {
                foreach (
                    var generic in
                    typeParams.Descendants <UnifiedGenericParameter>(

                        ))
                {
                    var tName = GetTypeName(generic.Type);
                    if (dic.ContainsKey(tName))
                    {
                        foreach (var c in dic[tName])
                        {
                            if (generic.Constrains == null)
                            {
                                generic.Constrains =
                                    UnifiedSet <UnifiedTypeConstrain> .Create();
                            }
                            generic.Constrains.Add(c.DeepCopy());
                        }
                    }
                }
            }
            foreach (
                var generic in
                extends.Descendants <UnifiedGenericParameter>())
            {
                var tName = GetTypeName(generic.Type);
                if (dic.ContainsKey(tName))
                {
                    foreach (var c in dic[tName])
                    {
                        if (generic.Constrains == null)
                        {
                            generic.Constrains =
                                UnifiedSet <UnifiedTypeConstrain> .Create();
                        }
                        generic.Constrains.Add(c.DeepCopy());
                    }
                }
            }

            switch (dec.ClassType)
            {
            case ClassType.Class:
                return(UnifiedClassDefinition.Create(
                           attrs, mods, name, typeParams, extends, body));

            case ClassType.Struct:
                return(UnifiedStructDefinition.Create(
                           attrs, mods, name, typeParams, extends, body));

            case ClassType.Interface:
                return(UnifiedInterfaceDefinition.Create(
                           attrs, mods, name, typeParams, extends, body));

            case ClassType.Enum:
                return(UnifiedEnumDefinition.Create(
                           attrs, mods, name, typeParams, extends, body));
            }
            var msg = "LookupClassKind : " + dec.ClassType + "には対応していません。";

            throw new InvalidOperationException(msg);
        }
 public static UnifiedVariableIdentifier CreateSymbol(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "Symbol");
     return(UnifiedVariableIdentifier.Create(node.Value));
 }
예제 #21
0
 public static UnifiedVariableIdentifier CreateVariable(string name)
 {
     return(UnifiedVariableIdentifier.Create(name));
 }
예제 #22
0
 public UnifiedElement VisitIdentifierExpression(
     IdentifierExpression ident, object data)
 {
     return(UnifiedVariableIdentifier.Create(ident.Identifier));
 }
예제 #23
0
        CreateDeclarationSpecifiers(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "declaration_specifiers");

            /*	declaration_specifiers
             * :   ( storage_class_specifier
             *       |   type_specifier
             *       |   type_qualifier      )+
             */
            var modifiers = UnifiedSet <UnifiedModifier> .Create();

            IList <UnifiedType> types       = new List <UnifiedType>();
            UnifiedExpression   declaration = null;

            foreach (var e in node.Elements())
            {
                switch (e.Name())
                {
                case "storage_class_specifier":
                    modifiers.Add(CreateStorageClassSpecifier(e));
                    break;

                case "type_specifier":
                    var typeSpecifier = CreateTypeSpecifier(e);
                    if (typeSpecifier.GetType().Equals(typeof(UnifiedType)))
                    {
                        types.Add((UnifiedType)CreateTypeSpecifier(e));
                    }
                    else
                    {
                        declaration = typeSpecifier;
                    }
                    break;

                case "type_qualifier":
                    //TODO: const または volatileのことであるが、安直にリストに追加していいか要確認
                    modifiers.Add(CreateTypeQualifier(e));
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }
            // 修飾子が空の場合はnullにする
            if (modifiers.IsEmpty())
            {
                modifiers = null;
            }

            if (declaration != null)
            {
                return(Tuple.Create(modifiers, declaration));
            }

            UnifiedExpression type;

            if (types.Count == 1)
            {
                type = types[0];
            }
            else
            {
                var s      = "";
                var prefix = "";
                // TODO unsigned int, long long int などは そのまま1つの型で表されるのか?
                foreach (var t in types)
                {
                    s += prefix
                         + ((UnifiedVariableIdentifier)t.BasicTypeName).Name;
                    prefix = " ";
                }
                type = UnifiedType.Create(UnifiedVariableIdentifier.Create(s));
            }
            return(Tuple.Create(modifiers, type));
        }
예제 #24
0
 public static UnifiedVariableIdentifier ToVariableIdentifier(
     this string name)
 {
     return(UnifiedVariableIdentifier.Create(name));
 }