def pretty(self, depth=0): str = " " * depth + "[%s : %s" % (self.type, self.leaf) if self.datatype != None: str += "(%s)" % fracttypes.strOfType(self.datatype) if self.children: str += "\n" for child in self.children: assert (isinstance(child, Node)) str += child.pretty(depth + 1) + "\n" str += " " * depth + "]" else: str += "]" return str
def pretty(self,depth=0): str = " " * depth + "[%s : %s" % (self.type , self.leaf) if self.datatype != None: str += "(%s)" % fracttypes.strOfType(self.datatype) if self.children: str += "\n" for child in self.children: assert(isinstance(child,Node)) str += child.pretty(depth+1) + "\n" str += " " * depth + "]" else: str += "]" return str
def __str__(self): return "Var<%s>(%s" % (fracttypes.strOfType(self.datatype), self.name)
def __str__(self): return "Cast<%s,%s>(\n" % \ (fracttypes.strOfType(self.children[0].datatype), fracttypes.strOfType(self.datatype))
def cast(self,t): 'Generate code to cast child of type child.datatype to t.datatype' child = t.children[0] src = self.generate_code(child) dst = None if t.datatype == Complex: dst = ComplexArg(self.newTemp(Float), self.newTemp(Float)) if child.datatype == Int or child.datatype == Bool: assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem,[src], [dst.re])) assem = "%(d0)s = 0.0;" self.out.append(Oper(assem,[src], [dst.im])) elif child.datatype == Float: assem = "%(d0)s = %(s0)s;" self.out.append(Oper(assem,[src], [dst.re])) assem = "%(d0)s = 0.0;" self.out.append(Oper(assem,[src], [dst.im])) elif t.datatype == Float: if child.datatype == Int or child.datatype == Bool: dst = self.newTemp(Float) assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem,[src], [dst])) elif t.datatype == Int: if child.datatype == Bool: # needn't do anything dst = src elif t.datatype == Bool: dst = self.newTemp(Bool) if child.datatype == Int or child.datatype == Bool: assem = "%(d0)s = (%(s0)s != 0);" self.out.append(Oper(assem,[src], [dst])) elif child.datatype == Float: assem = "%(d0)s = (%(s0)s != 0.0);" self.out.append(Oper(assem,[src], [dst])) elif child.datatype == Complex: assem = "%(d0)s = ((%(s0)s != 0.0) || (%(s1)s != 0.0));" self.out.append(Oper(assem,[src.re, src.im], [dst])) else: dst = None elif t.datatype == Hyper or t.datatype == Color: dst = HyperArg(self.newTemp(Float), self.newTemp(Float), self.newTemp(Float), self.newTemp(Float)) if child.datatype == Int or \ child.datatype == Bool or child.datatype == Float: assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem,[src], [dst.parts[0]])) assem = "%(d0)s = 0.0;" for i in xrange(1,4): self.out.append(Oper(assem,[src], [dst.parts[i]])) elif child.datatype == Complex: assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem,[src.re], [dst.parts[0]])) self.out.append(Oper(assem,[src.im], [dst.parts[1]])) assem = "%(d0)s = 0.0;" self.out.append(Oper(assem,[src], [dst.parts[2]])) self.out.append(Oper(assem,[src], [dst.parts[3]])) else: dst = None elif t.datatype == IntArray or t.datatype == FloatArray or t.datatype == ComplexArray: if child.datatype == VoidArray: if t.datatype == IntArray: cast = "(int *)" else: cast = "(double *)" assem = "%%(d0)s = (%s%%(s0)s);" % cast dst = self.newTemp(t.datatype) self.out.append(Oper(assem, [src], [dst])) if dst == None: msg = "%d: Invalid Cast from %s to %s" % \ (t.node.pos,fracttypes.strOfType(child.datatype), fracttypes.strOfType(t.datatype)) raise fracttypes.TranslationError(msg) return dst
def cast(self, t): 'Generate code to cast child of type child.datatype to t.datatype' child = t.children[0] src = self.generate_code(child) dst = None if t.datatype == Complex: dst = ComplexArg(self.newTemp(Float), self.newTemp(Float)) if child.datatype == Int or child.datatype == Bool: assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem, [src], [dst.re])) assem = "%(d0)s = 0.0;" self.out.append(Oper(assem, [src], [dst.im])) elif child.datatype == Float: assem = "%(d0)s = %(s0)s;" self.out.append(Oper(assem, [src], [dst.re])) assem = "%(d0)s = 0.0;" self.out.append(Oper(assem, [src], [dst.im])) elif t.datatype == Float: if child.datatype == Int or child.datatype == Bool: dst = self.newTemp(Float) assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem, [src], [dst])) elif t.datatype == Int: if child.datatype == Bool: # needn't do anything dst = src elif t.datatype == Bool: dst = self.newTemp(Bool) if child.datatype == Int or child.datatype == Bool: assem = "%(d0)s = (%(s0)s != 0);" self.out.append(Oper(assem, [src], [dst])) elif child.datatype == Float: assem = "%(d0)s = (%(s0)s != 0.0);" self.out.append(Oper(assem, [src], [dst])) elif child.datatype == Complex: assem = "%(d0)s = ((%(s0)s != 0.0) || (%(s1)s != 0.0));" self.out.append(Oper(assem, [src.re, src.im], [dst])) else: dst = None elif t.datatype == Hyper or t.datatype == Color: dst = HyperArg(self.newTemp(Float), self.newTemp(Float), self.newTemp(Float), self.newTemp(Float)) if child.datatype == Int or \ child.datatype == Bool or child.datatype == Float: assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem, [src], [dst.parts[0]])) assem = "%(d0)s = 0.0;" for i in xrange(1, 4): self.out.append(Oper(assem, [src], [dst.parts[i]])) elif child.datatype == Complex: assem = "%(d0)s = ((double)%(s0)s);" self.out.append(Oper(assem, [src.re], [dst.parts[0]])) self.out.append(Oper(assem, [src.im], [dst.parts[1]])) assem = "%(d0)s = 0.0;" self.out.append(Oper(assem, [src], [dst.parts[2]])) self.out.append(Oper(assem, [src], [dst.parts[3]])) else: dst = None elif t.datatype == IntArray or t.datatype == FloatArray or t.datatype == ComplexArray: if child.datatype == VoidArray: if t.datatype == IntArray: cast = "(int *)" else: cast = "(double *)" assem = "%%(d0)s = (%s%%(s0)s);" % cast dst = self.newTemp(t.datatype) self.out.append(Oper(assem, [src], [dst])) if dst == None: msg = "%d: Invalid Cast from %s to %s" % \ (t.node.pos,fracttypes.strOfType(child.datatype), fracttypes.strOfType(t.datatype)) raise fracttypes.TranslationError(msg) return dst
def strOfType(t): return fracttypes.strOfType(t).capitalize()