Ejemplo n.º 1
0
        // Generates a codedom instantiation expression: new foo() or new foo[x].
        public static CodeExpression Emit(Instantiation instantiation)
        {
            // Array instantiation needs a different treatment.
            if (instantiation.IsArray)
            {
                var c = new CodeArrayCreateExpression();

                c.CreateType = new CodeTypeReference(instantiation.Name);
                c.SizeExpression = CodeDomEmitter.EmitCodeExpression(instantiation.Parameters.ChildExpressions[0]);
                return c;
            }
            else // Non-array instantiation
            {
                var c = new CodeObjectCreateExpression();

                // The type that is being created
                var createType = new CodeTypeReference(instantiation.Name);

                // Apply the generic type names, if any.
                foreach (var g in instantiation.GenericTypes)
                {
                    createType.TypeArguments.Add(new CodeTypeReference(g));
                }
                c.CreateType = createType;

                // Translate the instantiation parameters.
                foreach (var a in instantiation.Parameters.ChildExpressions)
                    c.Parameters.Add(CodeDomEmitter.EmitCodeExpression(a));

                return c;
            }
        }
Ejemplo n.º 2
0
        // Build an instantiation expression "new foo()"
        public static void BuildInstantiation(IronyParser parser, Root root, Expression parentExpression, ParseTreeNode currentNode)
        {
            var ist = new Instantiation(parentExpression, currentNode.Token.Convert());
            parentExpression.ChildExpressions.Add(ist);

            // A non-array instantiation
            if (currentNode.ChildNodes[1].ChildNodes[0].Term.ToString() == "new_instantiate")
            {

                ist.Name = parser.CheckAlias(currentNode.ChildNodes[1].ChildNodes[0].FindTokenAndGetText());

                if (currentNode.ChildNodes[1].ChildNodes[0].ChildNodes[1].ChildNodes.Count > 0)
                    foreach (var n in currentNode.ChildNodes[1].ChildNodes[0].ChildNodes[1].ChildNodes[0].ChildNodes[1].ChildNodes)
                    {
                        ist.GenericTypes.Add(parser.CheckAlias(n.FindTokenAndGetText()));
                    }

                foreach (var n in currentNode.ChildNodes[1].ChildNodes[0].ChildNodes[2].ChildNodes)
                {
                    parser.ConsumeParseTree(root, ist.Parameters, n);
                }
            }
            else // An array instantiation
            {
                ist.IsArray = true;
                ist.Name = parser.CheckAlias(currentNode.ChildNodes[1].ChildNodes[0].ChildNodes[0].FindTokenAndGetText());

                parser.ConsumeParseTree(root, ist.Parameters, currentNode.ChildNodes[1].ChildNodes[0].ChildNodes[2]);
            }
        }