git @ Cat's Eye Technologies Xoomonk / 923735d
Implement Block analysis. Gets a few tests to pass. catseye 9 years ago
1 changed file(s) with 71 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
288288 # the store is saturated, do what you want
289289 self.dict[name] = value
290290
291 def __str__(self):
292 l = []
293 for name in self.variables:
294 l.append("%s=%s" % (name, self.dict[name]))
295 return '[%s]' % ','.join(l)
296
297
298 # Analysis
299
300 def find_used_variables(ast, s):
301 type = ast.type
302 if type == 'Program':
303 for child in ast.children:
304 find_used_variables(child, s)
305 elif type == 'Assignment':
306 find_used_variables(ast.children[1], s)
307 elif type == 'PrintChar':
308 find_used_variables(ast.children[1], s)
309 elif type == 'Print':
310 find_used_variables(ast.children[0], s)
311 elif type == 'Newline':
312 find_used_variables(ast.children[0], s)
313 elif type == 'Ref':
314 s.add(ast.children[0].value)
315 elif type == 'Block':
316 for child in ast.children:
317 find_used_variables(child, s)
318
319
320 def find_assigned_variables(ast, s):
321 type = ast.type
322 if type == 'Program':
323 for child in ast.children:
324 find_assigned_variables(child, s)
325 elif type == 'Assignment':
326 name = ast.children[0].children[0].value
327 s.add(name)
328 elif type == 'Block':
329 for child in ast.children:
330 find_assigned_variables(child, s)
331
332
333 # Evaluation
291334
292335 def eval_xoomonk(ast, state):
293336 type = ast.type
322365 return state.get(name, 0)
323366 elif type == 'IntLit':
324367 return ast.value
368 elif type == 'Block':
369 # OK! What we need to do is to analyze the block to see what
370 # variables in it are assigned values in it.
371 # If all variables in the block are assigned values somewhere
372 # in the block, it is a saturated store, and we can evaluate
373 # the code in it immediately.
374 # If not, we create a MalingeringStore, and associate the
375 # code of the block with it. This object will cause the code
376 # of the block to be executed when the store finally does
377 # become saturated through assignments.
378
379 used_variables = set()
380 find_used_variables(ast, used_variables)
381 assigned_variables = set()
382 find_assigned_variables(ast, assigned_variables)
383
384 if used_variables == assigned_variables:
385 return eval_block(ast, state)
386 else:
387 raise NotImplementedError, "Can't create malingering stores yet"
388 return create_malingering_store_from_block(ast)
325389 else:
326390 raise NotImplementedError, "not an AST type I know: %s" % type
391
392
393 def eval_block(block, enclosing_state):
394 state = {}
395 for child in block.children:
396 value = eval_xoomonk(child, state)
397 return MalingeringStore(state.keys(), [], lambda store: store)
327398
328399
329400 def main(argv):