I'm trying to write something in perl that I need badly in order to save time, as doing it manually is just going to take too long. I need something that can expand out a command that is prefixed by a '@' symbol that describes a multiply-accumulate unit. it would appear as a line in a program that is otherwise to all intents and purposes ANSI C (it's actually a subset). Here's the line that the perl program would need to find and replace:

@MAC(Result,A,B,2); would become:

Result =
(A0*B0)+(A1*B1);

@MAC(output,X,Y,8); would become:

output =
(((X0*Y0)+(X1*Y1))+((X2*Y2)+(X3*Y3)))+
(((X4*Y4)+(X5*Y5))+((X6*Y6)+(X7*Y7)));

The nice neat layout is not that important, what is important is getting the brackets in the right places. The 4th argument would always be a power of two. Essentially I'm trying to let a C-to-VHDL compiler get the hint about the structure of hardware I want.
I would be grateful if anyone had any bright ideas.

In reply to Complicated Search and Replace by gantlord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.