3 # Copyright © 2011 Intel Corporation
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 # DEALINGS IN THE SOFTWARE.
24 # This file contains helper functions for manipulating sexps in Python.
26 # We represent a sexp in Python using nested lists containing strings.
27 # So, for example, the sexp (constant float (1.000000)) is represented
28 # as ['constant', 'float', ['1.000000']].
32 if sys
.version_info
>= (3, 0, 0):
38 """Verify that the argument is a proper sexp.
40 That is, raise an exception if the argument is not a string or a
41 list, or if it contains anything that is not a string or a list at
44 if isinstance(sexp
, list):
47 elif not isinstance(sexp
, (STRING_TYPE
, bytes
)):
48 raise Exception('Not a sexp: {0!r}'.format(sexp
))
51 """Convert a string, of the form that would be output by mesa,
52 into a sexp represented as nested lists containing strings.
54 sexp_token_regexp
= re
.compile(
55 '[a-zA-Z_]+(@[0-9]+)?|[0-9]+(\\.[0-9]+)?|[^ \n]')
57 for match
in sexp_token_regexp
.finditer(sexp
):
58 token
= match
.group(0)
63 raise Exception('Unmatched )')
65 stack
[-1].append(sexp
)
67 stack
[-1].append(token
)
69 raise Exception('Unmatched (')
70 if len(stack
[0]) != 1:
71 raise Exception('Multiple sexps')
74 def sexp_to_string(sexp
):
75 """Convert a sexp, represented as nested lists containing strings,
76 into a single string of the form parseable by mesa.
78 if isinstance(sexp
, STRING_TYPE
):
80 if isinstance(sexp
, bytes
):
81 return sexp
.encode('utf-8')
82 assert isinstance(sexp
, list)
85 sub_result
= sexp_to_string(s
)
88 elif '\n' not in result
and '\n' not in sub_result
and \
89 len(result
) + len(sub_result
) + 1 <= 70:
90 result
+= ' ' + sub_result
92 result
+= '\n' + sub_result
93 return '({0})'.format(result
.replace('\n', '\n '))
96 """Sort all toplevel variable declarations in sexp.
98 This is used to work around the fact that
99 ir_reader::read_instructions reorders declarations.
101 assert isinstance(sexp
, list)
105 if isinstance(s
, list) and len(s
) >= 4 and s
[0] == 'declare':
109 return sorted(decls
) + other_code