Example #1
0
        public static UnifiedExpression CreateEnumSpecifier(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "enum_specifier");

            /*
             * enum_specifier
             * : 'enum' '{' enumerator_list '}'
             | 'enum' IDENTIFIER '{' enumerator_list '}'
             | 'enum' IDENTIFIER
             */

            // enum { RED, BLUE, YELLOW } -> 宣言
            // enum COLOR { RED, BLUE, YELLOW } -> 宣言
            // enum COLOR -> 型

            if (node.Elements("enumerator_list").Count() == 0)
            {
                // TODO 型名はどうなるのか?
                return
                    (UnifiedType.Create(
                         "enum " + node.FirstElement("IDENTIFIER").Value));
            }

            var identifier         = node.Element("IDENTIFIER");
            UnifiedIdentifier name = identifier != null
                                                                                         ? UnifiedIdentifier.CreateVariable(
                identifier.Value) : null;

            var body = CreateEnumeratorList(node.Element("enumerator_list"));

            return(UnifiedEnumDefinition.Create(
                       null, null, name, null, null, body));
        }
 private static UnifiedExpression CreateNthRef(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "nth_ref");
     // TODO: 正規表現オブジェクトのパターンにマッチした文字列の処理
     return(UnifiedIdentifier.CreateVariable("$" + node.Value));
 }
Example #3
0
        public UnifiedElement VisitQueryLetClause(
            QueryLetClause let, object data)
        {
            var ident = UnifiedIdentifier.CreateVariable(let.Identifier);
            var expr  = let.Expression.TryAcceptForExpression(this);

            return(UnifiedLetQuery.Create(ident, expr));
        }
        private static IEnumerable <UnifiedExpression> CreateCallExpression(StaticCallExpression statement)
        {
            var args =
                from ph in statement.Phrases.OfType <ActualComplementPhrase>()
                let arg                     = CreatePhrase(ph.PrefixExpression)
                                    let sfx = ph.Particle.Text
                                              select UnifiedArgument.Create(arg, UnifiedIdentifier.CreateVariable(sfx));

            yield return
                (UnifiedCall.Create(
                     UnifiedIdentifier.CreateVariable(statement.MethodInfo.Name), args.ToSet()));
        }
Example #5
0
        public UnifiedElement VisitEnumMemberDeclaration(
            EnumMemberDeclaration dec, object data)
        {
            var attrs = dec.Attributes.AcceptVisitorAsAttrs(this, data);
            var mods  = LookupModifiers(dec.Modifiers);
            var name  = UnifiedIdentifier.CreateVariable(dec.Name);
            var value = dec.Initializer.TryAcceptForExpression(this);

            return(UnifiedVariableDefinition.Create(
                       attrs, mods, /*no type*/ null, name, value,
                       /*no args*/ null, /*no bit field*/ null, /*no body*/ null));
        }
Example #6
0
        public UnifiedElement VisitQueryFromClause(
            QueryFromClause from, object data)
        {
            var ident = UnifiedIdentifier.CreateVariable(from.Identifier);
            var expr  = from.Expression.TryAcceptForExpression(this);

            if (from.Type == null)
            {
                return(UnifiedFromQuery.Create(ident, expr));
            }
            var type = LookupType(from.Type);

            return(UnifiedFromQuery.Create(ident, expr, type));
        }
Example #7
0
        public static IEnumerable <UnifiedExpression> CreateIdentifierList(
            XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "identifier_list");

            /*
             * identifier_list
             * : IDENTIFIER (',' IDENTIFIER)*
             */

            // TODO 使いどころがわからない
            return
                (node.Elements("IDENTIFIER").Select(
                     e => UnifiedIdentifier.CreateVariable(e.Value)));
        }
Example #8
0
        public static UnifiedFunctionDefinition CreateFunctionDefinition(
            XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "function_definition");

            /* function_definition
             * :	declaration_specifiers? declarator
             * (	declaration+ compound_statement	// K&R style
             *   |	compound_statement				// ANSI style
             * )
             */

            UnifiedSet <UnifiedModifier> modifiers = null;
            UnifiedType type = null;
            UnifiedSet <UnifiedGenericParameter> genericParameters = null;
            UnifiedIdentifier             name       = null;
            UnifiedSet <UnifiedParameter> parameters = null;
            UnifiedBlock body = null;

            var first = node.FirstElement();

            if (first.Name() == "declaration_specifiers")
            {
                var modifiersAndType = CreateDeclarationSpecifiers(first);
                modifiers = modifiersAndType.Item1;
                type      = (UnifiedType)modifiersAndType.Item2;
            }

            var declarator = CreateDeclarator(node.Element("declarator"));

            name       = declarator.Item1;
            parameters = declarator.Item2;

            if (node.Elements("declaration").Count() != 0)
            {
                // TODO declaration+ compound_statement に該当するケースが未検出
                throw new NotImplementedException();
            }

            body = CreateCompoundStatement(node.Element("compound_statement"));

            return(UnifiedFunctionDefinition.Create(
                       null, modifiers, type, genericParameters, name, parameters,
                       null, body));
        }
Example #9
0
        public UnifiedElement VisitPropertyDeclaration(
            PropertyDeclaration dec, object data)
        {
            var attrs = dec.Attributes.AcceptVisitorAsAttrs(this, data);
            var mods  = LookupModifiers(dec.Modifiers);
            var type  = LookupType(dec.ReturnType);
            var name  = UnifiedIdentifier.CreateVariable(dec.Name);
            var get   =
                dec.Getter.AcceptVisitor(this, data) as
                UnifiedPropertyDefinitionPart;
            var set =
                dec.Setter.AcceptVisitor(this, data) as
                UnifiedPropertyDefinitionPart;

            return(UnifiedPropertyDefinition.Create(
                       attrs, mods, type, name, null /*no parameter*/, get, set));
        }
Example #10
0
        public static UnifiedVariableDefinition CreateEnumerator(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "enumerator");

            /*
             * enumerator
             * : IDENTIFIER ('=' constant_expression)?
             */

            var identifier =
                UnifiedIdentifier.CreateVariable(node.NthElement(0).Value);
            UnifiedExpression value = null;
            var expression          = node.Element("constant_expression");

            if (expression != null)
            {
                value = CreateConstantExpression(expression);
            }
            return(UnifiedVariableDefinition.Create(
                       null, null, null, identifier, value));
        }
Example #11
0
        public static UnifiedVariableDefinition FindDefinition(
            UnifiedIdentifier variable)
        {
            var            scopes   = variable.Ancestors <UnifiedBlock>();
            var            name     = variable.Name;
            UnifiedElement searched = variable;

            foreach (var scope in scopes)
            {
                var definition = scope
                                 .DescendantsUntil(e2 => e2 is UnifiedBlock)
                                 .TakeWhile(e => e != searched)
                                 .OfType <UnifiedVariableDefinition>()
                                 .FirstOrDefault(e => e.Name.Name == name);
                if (definition != null)
                {
                    return(definition);
                }

                searched = scope;
            }
            return(null);
        }
 private static UnifiedThisIdentifier CreateSelf(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "self");
     return(UnifiedIdentifier.CreateThis("self"));
 }
        public static UnifiedExpression CreatePostfixExpression(XElement node)
        {
            Contract.Requires(node != null);
            Contract.Requires(node.Name() == "postfix_expression");

            /*
             * postfix_expression
             * :   primary_expression
             *      (   '[' expression ']'
             |   '(' ')'
             |   '(' argument_expression_list ')'
             |   '.' IDENTIFIER
             |   '->' IDENTIFIER
             |   '++'
             |   '--'
             |      )*
             */

            var result =
                CreatePrimaryExpression(node.Element("primary_expression"));
            var elements = node.Elements().Skip(1);             //先頭以外のすべての要素を取得
            var length   = elements.Count();

            for (var i = 0; i < length; i++)
            {
                switch (elements.ElementAt(i++).Value)
                {
                case "[":
                    result = UnifiedIndexer.Create(
                        result,
                        UnifiedSet <UnifiedArgument> .Create(
                            UnifiedArgument.Create(
                                CreateExpression(
                                    elements.ElementAt(i++)).
                                ElementAt(0))));
                    i++;                     // ']'読み飛ばし
                    break;

                case "(":
                    if (elements.ElementAt(i).Name == "argument_expression_list")
                    {
                        result = UnifiedCall.Create(
                            result,
                            CreateArgumentExpressionList(
                                elements.ElementAt(i++)));
                    }
                    else
                    {
                        result = UnifiedCall.Create(
                            result, UnifiedSet <UnifiedArgument> .Create());
                    }
                    i++;                     // ')'読み飛ばし
                    break;

                case ".":
                    result = UnifiedProperty.Create(
                        ".", result,
                        UnifiedIdentifier.CreateVariable(
                            elements.ElementAt(i++).Value));
                    break;

                case "->":
                    result = UnifiedProperty.Create(
                        "->", result,
                        UnifiedIdentifier.CreateVariable(
                            elements.ElementAt(i++).Value));
                    // TODO ポインタ型に展開してから処理するのか?
                    break;

                case "++":
                    result = UnifiedUnaryExpression.Create(
                        result,
                        UnifiedUnaryOperator.Create(
                            "++",
                            UnifiedUnaryOperatorKind.PostIncrementAssign));
                    break;

                case "--":
                    result = UnifiedUnaryExpression.Create(
                        result,
                        UnifiedUnaryOperator.Create(
                            "--",
                            UnifiedUnaryOperatorKind.PostDecrementAssign));
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }

            return(result);
        }
 private static UnifiedExpression CreateVariableToken(VariableToken token)
 {
     return(UnifiedIdentifier.CreateVariable(token.Variable.Name));
 }
 public static UnifiedVariableIdentifier CreateConst(XElement node)
 {
     Contract.Requires(node != null);
     Contract.Requires(node.Name() == "const");
     return(UnifiedIdentifier.CreateVariable(node.Value));
 }