4 # Copyright © 2011 Intel Corporation
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 # and/or sell copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # Chen, Yangyang <yangyang.chen@intel.com>
27 # Han, Haofu <haofu.han@intel.com>
33 def __init__(self, ln=0, s=None):
34 assert type(ln) == int
35 assert type(s) == str or s == None
40 def append(self, block):
41 self.subblocks.append(block)
43 def checkfor(self, line):
46 if re.match(p, line) == None:
47 raise Exception(self.__errmsg('syntax error'))
48 tail = line.split('(', 1)[1].rsplit(')', 1)
49 conds = tail[0].split(';')
52 raise Exception(self.__errmsg('missing "{"'))
54 raise Exception(self.__errmsg('syntax error(miss ";"?)'))
58 self.__parse_init(init)
59 self.__parse_cond(cond)
60 self.__parse_step(step)
62 def __parse_init(self, init):
63 inits = init.split(',')
68 self.param_init.append(val)
70 raise Exception(self.__errmsg('non an exp: %s'%ini))
71 self.param_num = len(inits)
73 def __parse_cond(self, cond):
75 if cond[0] in ['<', '>']:
77 self.param_op = cond[:2]
80 self.param_op = cond[0]
83 self.param_limit = eval(limit)
85 raise Exception(self.__errmsg('non an exp: %s'%limit))
87 raise Exception(self.__errmsg('syntax error'))
89 def __parse_step(self, step):
90 steps = step.split(',')
91 if len(steps) != self.param_num:
92 raise Exception(self.__errmsg('params number no match'))
97 self.param_step.append(val)
99 raise Exception(self.__errmsg('non an exp: %s'%st))
101 def __errmsg(self, msg=''):
102 return '%d: %s' % (self.lineno, msg)
105 lines = f.readlines()
109 tmp = line.split('\\n')
115 def parselines(lines):
122 if line.startswith('$'):
123 block = Block(lineno)
125 stack[-1].append(block)
127 elif line.startswith('}'):
129 elif line and not line.startswith('#'):
130 stack[-1].append(Block(lineno, line))
133 def writeblocks(outfile, blocks):
136 def check_cond(op, cur, lim):
137 assert op in ['<', '>', '<=', '>=']
138 assert type(cur) == int
139 assert type(lim) == int
140 return eval('%d %s %d' % (cur, op, lim))
142 def do_writeblock(block, curs):
143 if block.text != None:
147 params = set(re.findall(p, block.text))
149 index = int(param) - 1
150 if index >= len(curs):
151 raise Exception('%d: too many param(%%%d)'%(block.lineno, index+1))
152 newline = newline.replace('%%%d'%(index+1), str(curs[index]))
154 not newline.startswith('.') and \
155 not newline.endswith(':') and \
156 not newline.endswith(';'):
160 for_curs = block.param_init
161 while check_cond(block.param_op, for_curs[0], block.param_limit):
162 for sblock in block.subblocks:
163 do_writeblock(sblock, for_curs)
164 for i in range(0, block.param_num):
165 for_curs[i] += block.param_step[i]
167 for block in blocks.subblocks:
168 do_writeblock(block, [])
169 outfile.write('\n'.join(buf))
172 if __name__ == '__main__':
175 print >>sys.stderr, 'no input file'
179 infile = open(sys.argv[1], 'r')
181 print >>sys.stderr, 'can not open %s' % sys.argv[1]
188 outfile = open(sys.argv[2], 'w')
190 print >>sys.stderr, 'can not write to %s' % sys.argv[2]
193 lines = readlines(infile)
199 blocks = parselines(lines)
200 writeblocks(outfile, blocks)