def visit_Str(self, node): if isinstance(node.s, unicode): expr_str = 'πg.NewUnicode({}).ToObject()'.format( util.go_str(node.s.encode('utf-8'))) else: expr_str = '{}.ToObject()'.format(self.block.root.intern(node.s)) return expr.GeneratedLiteral(expr_str)
def visit_Yield(self, node): if node.value: value = self.visit(node.value) else: value = expr.GeneratedLiteral('πg.None') resume_label = self.block.genlabel(is_checkpoint=True) self.writer.write('πF.PushCheckpoint({})'.format(resume_label)) self.writer.write('return {}, nil'.format(value.expr)) self.writer.write_label(resume_label) result = self.block.alloc_temp() self.writer.write('{} = πSent'.format(result.name)) return result
def visit_Slice(self, node): result = self.block.alloc_temp() lower = upper = step = expr.GeneratedLiteral('πg.None') if node.lower: lower = self.visit(node.lower) if node.upper: upper = self.visit(node.upper) if node.step: step = self.visit(node.step) with lower, upper, step: self.writer.write_checked_call2( result, 'πg.SliceType.Call(πF, πg.Args{{{}, {}, {}}}, nil)', lower.expr, upper.expr, step.expr) return result
def visit_Num(self, node): if isinstance(node.n, int): expr_str = 'NewInt({})'.format(node.n) elif isinstance(node.n, long): a = abs(node.n) gobytes = '' while a: gobytes = hex(int(a&255)) + ',' + gobytes a >>= 8 expr_str = 'NewLongFromBytes([]byte{{{}}})'.format(gobytes) if node.n < 0: expr_str = expr_str + '.Neg()' elif isinstance(node.n, float): expr_str = 'NewFloat({})'.format(node.n) elif isinstance(node.n, complex): expr_str = 'NewComplex(complex({}, {}))'.format(node.n.real, node.n.imag) else: msg = 'number type not yet implemented: ' + type(node.n).__name__ raise util.ParseError(node, msg) return expr.GeneratedLiteral('πg.' + expr_str + '.ToObject()')