Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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())
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
 def from_ctypes(self, value):
     return objects.Float(value)
Ejemplo n.º 5
0
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))