Example #1
0
 private Statement ExpandMacro(Type macroType, MacroStatement node)
 {
     using (IAstMacro macro = (IAstMacro)Activator.CreateInstance(macroType))
     {
         macro.Initialize(_context);
         return(macro.Expand(node));
     }
 }
Example #2
0
        override public void OnMacroStatement(MacroStatement node)
        {
            Visit(node.Block);
            Visit(node.Arguments);

            Statement replacement = null;

            IEntity entity = NameResolutionService.ResolveQualifiedName(BuildMacroTypeName(node.Name));

            if (null == entity)
            {
                entity = NameResolutionService.ResolveQualifiedName(node.Name);
            }

            if (null == entity)
            {
                Errors.Add(CompilerErrorFactory.UnknownMacro(node, node.Name));
            }
            else
            {
                if (EntityType.Type != entity.EntityType)
                {
                    Errors.Add(CompilerErrorFactory.InvalidMacro(node, node.Name));
                }
                else
                {
                    IType        macroType = (IType)entity;
                    ExternalType type      = macroType as ExternalType;
                    if (null == type)
                    {
                        Errors.Add(CompilerErrorFactory.AstMacroMustBeExternal(node, macroType.FullName));
                    }
                    else
                    {
                        object macroInstance = Activator.CreateInstance(type.ActualType);
                        if (!(macroInstance is IAstMacro))
                        {
                            Errors.Add(CompilerErrorFactory.InvalidMacro(node, macroType.FullName));
                        }
                        else
                        {
                            try
                            {
                                using (IAstMacro macro = ((IAstMacro)macroInstance))
                                {
                                    macro.Initialize(_context);
                                    replacement = macro.Expand(node);
                                }
                            }
                            catch (Exception error)
                            {
                                Errors.Add(CompilerErrorFactory.MacroExpansionError(node, error));
                            }
                        }
                    }
                }
            }
            if (null != node.Modifier)
            {
                replacement = NormalizeStatementModifiers.CreateModifiedStatement(node.Modifier, replacement);
            }
            ReplaceCurrentNode(replacement);
        }