public string ReplaceParms(MacroEntry me, string[] actual_parms)
        {
            Match  m;
            int    istart = 0;
            string subst  = me.Subst;

            while ((m = iden.Match(subst, istart)).Success)
            {
                int idx = Array.IndexOf(me.Parms, m.Value);
                int len = m.Length;
                if (idx != -1)
                {
                    string actual = actual_parms[idx];
                    // A _single_ # before a token  means the 'stringizing' operator
                    if (m.Index > 0 && subst[m.Index - 1] == '#')
                    {
                        // whereas ## means 'token-pasting'!  #s will be removed later!
                        if (!(m.Index > 1 && subst[m.Index - 2] == '#'))
                        {
                            actual = '\"' + actual + '\"';
                        }
                    }
                    subst = iden.Replace(subst, actual, 1, istart);
                    len   = actual.Length;
                }
                istart = m.Index + len;
            }
            subst = subst.Replace("#", "");
            return(subst);
        }
Example #2
0
 public string ReplaceParms(MacroEntry me, string[] actual_parms)
 {
     Match m;
     int istart = 0;
     string subst = me.Subst;
     while ((m = iden.Match(subst, istart)).Success)
     {
         int idx = Array.IndexOf(me.Parms, m.Value);
         int len = m.Length;
         if (idx != -1)
         {
             string actual = actual_parms[idx];
             // A _single_ # before a token  means the 'stringizing' operator
             if (m.Index > 0 && subst[m.Index - 1] == '#')
             {
                 // whereas ## means 'token-pasting'!  #s will be removed later!
                 if (!(m.Index > 1 && subst[m.Index - 2] == '#'))
                     actual = '\"' + actual + '\"';
             }
             subst = iden.Replace(subst, actual, 1, istart);
             len = actual.Length;
         }
         istart = m.Index + len;
     }
     subst = subst.Replace("#", "");
     return subst;
 }
Example #3
0
 public void AddMacro(string name, string subst, string[] parms)
 {
     MacroEntry me = new MacroEntry();
     me.Subst = subst;
     me.Parms = parms;
     macroTable[name] = me;
 }
        public void AddMacro(string name, string subst, string[] parms)
        {
            MacroEntry me = new MacroEntry();

            me.Subst         = subst;
            me.Parms         = parms;
            macroTable[name] = me;
        }
        void ProcessCommand(string line)
        {
            Match  m   = cmdSplit.Match(line);
            string cmd = m.Groups[1].ToString();
            string arg = m.Groups[2].ToString().TrimStart(null);

            switch (cmd)
            {
            case "n":
                AddNamespace(arg);
                break;

            case "r":
                AddReference(arg);
                break;

            case "v":
                foreach (string v in varTable.Keys)
                {
                    Utils.Print(v + " = " + varTable[v]);
                }
                break;

            case "dcl":
                MustDeclare = !MustDeclare;
                break;

            case "code":     //  show code sent to compiler!
                showCode = !showCode;
                break;

            default:
                // a macro may be used as a command; the line is split up and
                // and supplied as arguments.
                // For macros taking one argument, the whole line is supplied.
                MacroEntry me = macro.Lookup(cmd);
                if (me != null && me.Parms != null)
                {
                    string[] parms;
                    if (me.Parms.Length > 1)
                    {
                        parms = spaces.Split(arg);
                    }
                    else
                    {
                        parms = new string[] { arg }
                    };
                    string s = macro.ReplaceParms(me, parms);
                    ExecuteLine(s);
                }
                else
                {
                    Utils.Print("unrecognized command, or bad macro");
                }
                break;
            }
        }
        public string Substitute(string str)
        {
            Match m;
            int   istart = 0;

            while ((m = iden.Match(str, istart)).Success)
            {
                MacroEntry me = (MacroEntry)macroTable[m.Value];
                if (me != null)
                {
                    string subst = me.Subst;
                    if (me.Parms != null)
                    {
                        int i = m.Index + m.Length;  // points to char just beyond match
                        while (i < str.Length && str[i] != '(')
                        {
                            i++;
                        }
                        i++; // just past '('
                        int      parenDepth = 1;
                        string[] actuals = new string[me.Parms.Length];
                        int      idx = 0, isi = i;
                        while (parenDepth > 0 && i < str.Length)
                        {
                            char ch = str[i];
                            if (parenDepth == 1 && (ch == ',' || ch == ')'))
                            {
                                actuals[idx] = str.Substring(isi, i - isi);
                                idx++;
                                isi = i + 1;  // past ',' or ')'
                            }
                            // understands commas within braces or square brackets (e.g. 'matrix' indexing)
                            if (ch == '(' || ch == '{' || ch == '[')
                            {
                                parenDepth++;
                            }
                            else
                            if (ch == ')' || ch == '}' || ch == ']')
                            {
                                parenDepth--;
                            }
                            i++;
                        }
                        if (parenDepth != 0)
                        {
                            return("**Badly formed macro call**");
                        }
                        subst  = ReplaceParms(me, actuals);
                        istart = m.Index;
                        str    = str.Remove(istart, i - istart);
                        str    = str.Insert(istart, subst);
                    }
                    else
                    {
                        str = iden.Replace(str, subst, 1, istart);
                    }
                }
                else
                {
                    istart = m.Index + m.Length;
                }
            }
            return(str);
        }