# Path: krakatau-lib/src/main/resources/Lib/Krakatau/ssa/objtypes.py
# def TypeTT(baset, dim):
# def baset(tt): return tt[0]
# def dim(tt): return tt[1]
# def withDimInc(tt, inc): return TypeTT(baset(tt), dim(tt)+inc)
# def withNoDim(tt): return TypeTT(baset(tt), 0)
# def isBaseTClass(tt): return not baset(tt).startswith('.')
# def className(tt): return baset(tt) if not baset(tt).startswith('.') else None
# def primName(tt): return baset(tt)[1:] if baset(tt).startswith('.') else None
# def isSubtype(env, x, y):
# def commonSupertype(env, tts):
# def verifierToSynthetic_seq(vtypes):
# def verifierToSynthetic(vtype):
# def declTypeToActual(env, decltype):
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/java/stringescape.py
# def escapeString(u):
#     if set(u) <= ok_chars:
#         return u 
# 
#     escaped = []
#     for c in u:
#         if c in ok_chars:
#             escaped.append(c)
#         elif c in late_escape:
#             escaped.append(late_escape[c])
#         else:
#             i = ord(c)
#             if i <= 0xFFFF:
#                 escaped.append(r'\u{0:04x}'.format(i))
#             else:
#                 i -= 0x10000
#                 high = 0xD800 + (i>>10)
#                 low = 0xDC00 + (i & 0x3FF)
#                 escaped.append(r'\u{0:04x}\u{1:04x}'.format(high,low))
#     return ''.join(escaped)


import itertools, math
from ..ssa import objtypes
from .stringescape import escapeString

    def getScopes(self): return self,

    def print_(self, printer, print_):
        assert(self.labelable or self.label is None)
        contents = '\n'.join(print_(x) for x in self.statements)
        indented = ['    '+line for line in contents.splitlines()]
        # indented[:0] = ['    //{} {}'.format(self,x) for x in (self.continueKey, self.breakKey, self.jumpKey)]
        lines = [self.getLabelPrefix() + '{'] + indented + ['}']
        return '\n'.join(lines)

    @staticmethod
    def join(*scopes):
        blists = [s.bases for s in scopes if s is not None] #allow None to represent the universe (top element)
        if not blists:
            return None
        common = [x for x in zip(*blists) if len(set(x)) == 1]
        return common[-1][0]

    def tree(self, printer, tree): return ['blockS', self.label, map(tree, self.statements)]

#Temporary hack
class StringStatement(JavaStatement):
    def __init__(self, s):
        self.s = s
    def print_(self, printer, print_): return self.s
    def tree(self, printer, tree): return ['stringS', self.s]

#############################################################################################################################################
# Careful, order is important here!

# Path: krakatau-lib/src/main/resources/Lib/Krakatau/ssa/objtypes.py
# def TypeTT(baset, dim):
# def baset(tt): return tt[0]
# def dim(tt): return tt[1]
# def withDimInc(tt, inc): return TypeTT(baset(tt), dim(tt)+inc)
# def withNoDim(tt): return TypeTT(baset(tt), 0)
# def isBaseTClass(tt): return not baset(tt).startswith('.')
# def className(tt): return baset(tt) if not baset(tt).startswith('.') else None
# def primName(tt): return baset(tt)[1:] if baset(tt).startswith('.') else None
# def isSubtype(env, x, y):
# def commonSupertype(env, tts):
# def verifierToSynthetic_seq(vtypes):
# def verifierToSynthetic(vtype):
# def declTypeToActual(env, decltype):
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/java/stringescape.py
# def escapeString(u):
#     if set(u) <= ok_chars:
#         return u 
# 
#     escaped = []
#     for c in u:
#         if c in ok_chars:
#             escaped.append(c)
#         elif c in late_escape:
#             escaped.append(late_escape[c])
#         else:
#             i = ord(c)
#             if i <= 0xFFFF:
#                 escaped.append(r'\u{0:04x}'.format(i))
#             else:
#                 i -= 0x10000
#                 high = 0xD800 + (i>>10)
#                 low = 0xDC00 + (i & 0x3FF)
#                 escaped.append(r'\u{0:04x}\u{1:04x}'.format(high,low))
#     return ''.join(escaped)


# Path: krakatau-lib/src/main/resources/Lib/Krakatau/java/ast.py
import itertools, math
from ..ssa import objtypes
from .stringescape import escapeString


    def getScopes(self): return self,

    def print_(self, printer, print_):
        assert(self.labelable or self.label is None)
        contents = '\n'.join(print_(x) for x in self.statements)
        indented = ['    '+line for line in contents.splitlines()]
        # indented[:0] = ['    //{} {}'.format(self,x) for x in (self.continueKey, self.breakKey, self.jumpKey)]
        lines = [self.getLabelPrefix() + '{'] + indented + ['}']
        return '\n'.join(lines)

    @staticmethod
    def join(*scopes):
        blists = [s.bases for s in scopes if s is not None] #allow None to represent the universe (top element)
        if not blists:
            return None
        common = [x for x in zip(*blists) if len(set(x)) == 1]
        return common[-1][0]

    def tree(self, printer, tree): return ['blockS', self.label, map(tree, self.statements)]

#Temporary hack
class StringStatement(JavaStatement):
    def __init__(self, s):
        self.s = s
    def print_(self, printer, print_): return self.s
    def tree(self, printer, tree): return ['stringS', self.s]

#############################################################################################################################################
# Careful, order is important here!

_assignable_sprims = objtypes.ByteTT, objtypes.ShortTT, objtypes.CharTT