Ejemplo n.º 1
0
        private static Ast ReplaceMacroParams(Ast.MacroCall call, Ast node)
        {
            var macro = call.Target;

            if (node is Ast.MacroDefinition)
            {
                if (node is Ast.Macro)
                {
                    var innerMacro = node as Ast.Macro;
                    return(Ast.Mac(innerMacro.Parameters, innerMacro.Result,
                                   (Ast.Block)innerMacro.Implementation.Transform(n => ReplaceMacroParams(call, n))));
                }
                var macroDef = node as Ast.MacroDefinition;
                var i        = macro.GetMacroDefParamIndex(macroDef);
                if (i >= 0)
                {
                    return(call.Parameters[i]);
                }
            }
            if (node is Ast.MacroParamRef)
            {
                var mp = node as Ast.MacroParamRef;
                var i  = macro.GetParamRefIndex(mp);
                if (i >= 0)
                {
                    return(call.Parameters[i]);
                }
            }
            if (node is Ast.MacroResultVar && node == macro.Result)
            {
                return(call.ResultVar);
            }
            return(node);
        }
Ejemplo n.º 2
0
        private static Ast.Block InstantiateMacro(Ast.MacroCall call)
        {
            var macro = call.Target as Ast.Macro;

            if (macro == null)
            {
                throw new InvalidOperationException("Trying to instantiate macro definition.");
            }
            if (call.Parameters.Any(mp => mp.GetType() == typeof(Ast.MacroDefinition)))
            {
                throw new InvalidOperationException("Uninstantiated macro parameter in macro call.");
            }
            return((Ast.Block)macro.Implementation.Transform(node => ReplaceMacroParams(call, node)));
        }