def visit_Assign(self, node):
if len(node.targets) > 1:
raise SyntaxError("GPUMAP: multiple assignment not supported")
target_types = map(lambda target: type(target), node.targets)
if tuple in target_types or list in target_types:
raise SyntaxError("GPUMAP: No unpacking allowed")
target = node.targets[0]
# assignment into object field
output = ""
value = self.visit(node.value)
# assignment into variable
if isinstance(target, _ast.Name):
# assignment into new variable
# not sure about the type just yet..
# see if it's a primitive
if target.id not in self.local_vars:
# binops and boolops return primitives
if isinstance(node.value, _ast.Num) or isinstance(node.value, _ast.Compare) or isinstance(node.value, _ast.BinOp) \
or isinstance(node.value, _ast.BoolOp) or isinstance(node.value, _ast.NameConstant):
output += "auto "
# check if referenced list contains primitives
elif isinstance(node.value, _ast.Subscript):
list_name = value[:value.find("[")]
try:
idx = self.func_repr.args.index(list_name)
t = self.func_repr.arg_types[idx]
item_type = t[t.find("<") + 1: t.find(">")]
if item_type in map(lambda t: t.__name__, primitive_map.keys()):
output += "auto "
else:
output += "auto&& "
except:
raise RuntimeError("THIS SHOULD NEVER HAPPEN")
else:
# check if it's an existing variable
try:
idx = self.func_repr.args.index(value)
t = self.func_repr.arg_types[idx]
if t in primitive_map:
output += "auto "
else:
output += "auto&& "
except ValueError:
output += "auto&& "
self.local_vars[target.id] = None
output += self.visit(target)
output += " = " + value
return output
评论列表
文章目录