def from_ctypes(self, value): if isinstance(value, (int, long)): return objects.Integer(value) if isinstance(value, float): return objects.Float(value) if value is None: return objects.null raise Exception("Unknown cell value: %r" % value)
def expr(self, expr): if expr.type == 'identifier': var = self.function.lookup(expr.string) assert var is not None, repr(expr.string) return var if expr.type == 'string': return objects.String(expr.string) if expr.type == 'number': if "." in expr.string: return objects.Float(float(expr.string)) elif expr.string.startswith('0x'): return objects.Integer(int(expr.string, 16)) else: return objects.Integer(int(expr.string)) if expr.type == 'member': arg = self.expr(expr[0]) return self.append(member(arg, expr.string)) if expr.type == 'call': args = [self.expr(sub_expr) for sub_expr in expr] callee = args.pop(0) return self.append(call(callee, args)) if expr.type == 'def': argv = [arg.string for arg in expr[0]] body = expr[1:] return self.new_function(argv, body) if expr.type == 'op': lhs, rhs = expr opname = '' if expr.string == '<': opname = 'lt' if expr.string == '>': opname = 'gt' if expr.string == '<=': opname = 'le' if expr.string == '>=': opname = 'ge' if expr.string == '!=': opname = 'ne' if expr.string == '==': opname = 'eq' if len(opname) > 0: res = call(self.function.lookup(opname), [self.expr(lhs), self.expr(rhs)]) self.append(res) return res raise Exception("%s not built" % expr.repr())
def compile_float(context, ast): assert(isinstance(ast,ast_objects.Float)) value = objects.Float(ast.value) index = context.register_constant(value) context.emit(bytecode.LOAD_CONST,index)
def from_ctypes(self, value): return objects.Float(value)
def sub(a, b): if isinstance(a, objects.Float) or isinstance(b, objects.Float): return objects.Float(a.value - b.value) if isinstance(a, objects.Integer) and isinstance(b, objects.Integer): return objects.Integer(a.value - b.value) raise Exception("cannot subtract %r, %r" % (a, b))