Book a Demo

Author Topic: Code generation and backslashes  (Read 2728 times)

ssinnige

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Code generation and backslashes
« on: September 19, 2006, 06:30:03 am »
I'm trying to generate code from the operation's initial code field as a #define statement with each line ended with a backslash. It all works fine but the only problem is generating the backslash. The only thing that more or less works is:

 $code = %WRAP_LINES(opCode, 80, "", "\\" )%
 #define %opName% $code

but that generates two backslahes instead of one!

Any suggestions?

thomaskilian

  • Guest
Re: Code generation and backslashes
« Reply #1 on: September 19, 2006, 06:59:34 am »
Isn't there a special token for a backslash like for quote and dollar?

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Code generation and backslashes
« Reply #2 on: September 19, 2006, 10:25:16 am »
According to the user guide there are only macros for the double quote, dollar, and percent sign.

What happens if you use something like "%\%" with or without the surrounding quotes?
No, you can't have it!

thomaskilian

  • Guest
Re: Code generation and backslashes
« Reply #3 on: September 19, 2006, 01:02:59 pm »
I'm not sure but someone else asked this question a few months ago and I found the answer by fiddling around some minutes. I don't have the time now :-/

ssinnige

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: Code generation and backslashes
« Reply #4 on: September 19, 2006, 04:42:28 pm »
With a bit of fiddling I found out how to solve it. The problem is in the EA code generation lexical analysis as the backslash is regarded as an escape character for characters like \n and \t while the escape character itself is not escaped as is the case with C/C++ through \\. You can add a single backslash in a string, as long as that it not followed by a double quote. So, I wrap the lines and set the end-string to "\@" (a backslash followed by an arbitrary character". Then I replace the result by removing every occurence of "@\n" leaving the single backslah in tact. Finally, I remove the occurence of "\@" that may still be at the end of the initial code as #define statements normally do not need a line continuation marker at the end of the specification. In short:

 $code = %WRAP_LINES(opCode, "-1", "\t", "\@")%
 $code = %REPLACE($code, "@\n", "\n")%
 $code = %TRIM_RIGHT($code, "\@")%
 #define %opName% $code

It would have been much easier if \\ was seen as an escaped backslash  :)