Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>#define</code> lines create constants. Just create variables in python for those:</p> <pre><code>STDIN_BASEADDRESS = 0x40600000 STDOUT_BASEADDRESS = 0x40600000 # ****************************************************************** # Definitions for driver UARTLITE XPAR_XUARTLITE_NUM_INSTANCES = 1 </code></pre> <p>So all you need to do is remove the <code>#define</code> part, and insert a <code>=</code> equals sign. A set of regular expressions would do the job:</p> <pre><code>import re from functools import partial replacements = ( partial(re.compile(r'\s*#define ([A-Z_]+) (.*)').sub, r'\1 = \2'), partial(re.compile(r'\s*/\*\s*(.*?)\s*\*/').sub, r'# \1'), ) for line in inputsequence: for transform in replacements: line = transform(line) print line </code></pre> <p>Demo:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; from functools import partial &gt;&gt;&gt; replacements = ( ... partial(re.compile(r'\s*#define ([A-Z_]+) (.*)').sub, r'\1 = \2'), ... partial(re.compile(r'\s*/\*\s*(.*?)\s*\*/').sub, r'# \1'), ... ) &gt;&gt;&gt; for line in inputsequence: ... for transform in replacements: ... line = transform(line) ... print line ... STDIN_BASEADDRESS = 0x40600000 STDOUT_BASEADDRESS = 0x40600000 # **************************************************************** # Definitions for driver UARTLITE XPAR_XUARTLITE_NUM_INSTANCES = 1 </code></pre> <p>If you still want to transform the defines to functions, just adjust the replacement pattern:</p> <pre><code>partial(re.compile(r'\s*#define ([A-Z_]+) (.*)').sub, r'def \1():\n return \2'), </code></pre> <p>which results in:</p> <pre><code>def STDIN_BASEADDRESS(): return 0x40600000 def STDOUT_BASEADDRESS(): return 0x40600000 # **************************************************************** # Definitions for driver UARTLITE def XPAR_XUARTLITE_NUM_INSTANCES(): return 1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload