def __init__(self, outer, pyname, argsast, bodyast): BaseTranslator.__init__(self, outer) self.function = IRCode(pyname) program.codes.add(self.function) self.function.module = self.currentModule() self.pullDocstring(bodyast) #args can be Names, Assign([Name], expr), or Tuple with IRBlock(self.function.body): self.function.args = [] for argast in argsast.args: if matches(argast, ast.Name): argname = argast.id self.function.args.append(argname) var = self.getTargetNamed(argname) self.function.argvars.append(var) elif matches(argast, ast.Assign([ast.Name], __)): # we assume the expression was evaluated externally argname = argast.targets[0].id self.function.defaults.append(argname) self.function.args.append(argname) var = self.getTargetNamed(argname) self.function.argvars.append(var) elif matches(argast, ast.Tuple): self.function.args.append(None) argvar = IRVar() self.function.argvars.append(argvar) self.makeAssignment(argast, argvar) else: self.error("unexpected argument %s" % argast) self.buildBlock( self.function.body, bodyast ) with IRBlock(self.function.body): Return(NoneLiteral()) self.function.docstring = self.docstring self.function.namespace = self.namespace self.function.globals = self.getGlobals() self.function.locals = self.getLocals() - set(self.function.args)
def _evaluate(root, hash): hash_observables = utils.get_observables_with_hashes(root) for o in hash_observables: id_ = utils.get_id(o) props = utils.get_properties(o) hashes = utils.get_hashes(o) is_match = any(utils.matches(x, hash) for x in hashes) is_singleton = len(props) == 1 # TODO: Does the object only contain a Hashes element? is_negated = bool(o.attrib.get('negate', False)) if is_match and is_singleton: result = RESULT_MATCH elif is_match and not(is_singleton): result = RESULT_PARTIAL else: result = RESULT_UNMATCHED if is_negated and result == RESULT_MATCH: result = RESULT_UNMATCHED if is_negated and result == RESULT_UNMATCHED: result = RESULT_MATCH OBSERVABLE_TO_RESULT[id_] = result
def pullDocstring(s, astcode): if matches(astcode, [ast.Expr(ast.Str), ___]): s.docstring = astcode.pop(0).value.s else: s.docstring = None