id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
sequence
docstring
stringlengths
3
17.3k
docstring_tokens
sequence
sha
stringlengths
40
40
url
stringlengths
87
242
3,500
boriel/zxbasic
asmparse.py
Asm.argval
def argval(self): """ Solve args values or raise errors if not defined yet """ if gl.has_errors: return [None] if self.asm in ('DEFB', 'DEFS', 'DEFW'): return tuple([x.eval() if isinstance(x, Expr) else x for x in self.arg]) self.arg = tuple([x if not isinstance(x, Expr) else x.eval() for x in self.arg]) if gl.has_errors: return [None] if self.asm.split(' ')[0] in ('JR', 'DJNZ'): # A relative jump? if self.arg[0] < -128 or self.arg[0] > 127: error(self.lineno, 'Relative jump out of range') return [None] return super(Asm, self).argval()
python
def argval(self): if gl.has_errors: return [None] if self.asm in ('DEFB', 'DEFS', 'DEFW'): return tuple([x.eval() if isinstance(x, Expr) else x for x in self.arg]) self.arg = tuple([x if not isinstance(x, Expr) else x.eval() for x in self.arg]) if gl.has_errors: return [None] if self.asm.split(' ')[0] in ('JR', 'DJNZ'): # A relative jump? if self.arg[0] < -128 or self.arg[0] > 127: error(self.lineno, 'Relative jump out of range') return [None] return super(Asm, self).argval()
[ "def", "argval", "(", "self", ")", ":", "if", "gl", ".", "has_errors", ":", "return", "[", "None", "]", "if", "self", ".", "asm", "in", "(", "'DEFB'", ",", "'DEFS'", ",", "'DEFW'", ")", ":", "return", "tuple", "(", "[", "x", ".", "eval", "(", ")", "if", "isinstance", "(", "x", ",", "Expr", ")", "else", "x", "for", "x", "in", "self", ".", "arg", "]", ")", "self", ".", "arg", "=", "tuple", "(", "[", "x", "if", "not", "isinstance", "(", "x", ",", "Expr", ")", "else", "x", ".", "eval", "(", ")", "for", "x", "in", "self", ".", "arg", "]", ")", "if", "gl", ".", "has_errors", ":", "return", "[", "None", "]", "if", "self", ".", "asm", ".", "split", "(", "' '", ")", "[", "0", "]", "in", "(", "'JR'", ",", "'DJNZ'", ")", ":", "# A relative jump?", "if", "self", ".", "arg", "[", "0", "]", "<", "-", "128", "or", "self", ".", "arg", "[", "0", "]", ">", "127", ":", "error", "(", "self", ".", "lineno", ",", "'Relative jump out of range'", ")", "return", "[", "None", "]", "return", "super", "(", "Asm", ",", "self", ")", ".", "argval", "(", ")" ]
Solve args values or raise errors if not defined yet
[ "Solve", "args", "values", "or", "raise", "errors", "if", "not", "defined", "yet" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L159-L178
3,501
boriel/zxbasic
asmparse.py
Expr.eval
def eval(self): """ Recursively evals the node. Exits with an error if not resolved. """ Expr.ignore = False result = self.try_eval() Expr.ignore = True return result
python
def eval(self): Expr.ignore = False result = self.try_eval() Expr.ignore = True return result
[ "def", "eval", "(", "self", ")", ":", "Expr", ".", "ignore", "=", "False", "result", "=", "self", ".", "try_eval", "(", ")", "Expr", ".", "ignore", "=", "True", "return", "result" ]
Recursively evals the node. Exits with an error if not resolved.
[ "Recursively", "evals", "the", "node", ".", "Exits", "with", "an", "error", "if", "not", "resolved", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L244-L252
3,502
boriel/zxbasic
asmparse.py
Expr.try_eval
def try_eval(self): """ Recursively evals the node. Returns None if it is still unresolved. """ item = self.symbol.item if isinstance(item, int): return item if isinstance(item, Label): if item.defined: if isinstance(item.value, Expr): return item.value.try_eval() else: return item.value else: if Expr.ignore: return None # Try to resolve into the global namespace error(self.symbol.lineno, "Undefined label '%s'" % item.name) return None try: if isinstance(item, tuple): return tuple([x.try_eval() for x in item]) if isinstance(item, list): return [x.try_eval() for x in item] if item == '-' and len(self.children) == 1: return -self.left.try_eval() try: return self.funct[item](self.left.try_eval(), self.right.try_eval()) except ZeroDivisionError: error(self.symbol.lineno, 'Division by 0') except KeyError: pass except TypeError: pass return None
python
def try_eval(self): item = self.symbol.item if isinstance(item, int): return item if isinstance(item, Label): if item.defined: if isinstance(item.value, Expr): return item.value.try_eval() else: return item.value else: if Expr.ignore: return None # Try to resolve into the global namespace error(self.symbol.lineno, "Undefined label '%s'" % item.name) return None try: if isinstance(item, tuple): return tuple([x.try_eval() for x in item]) if isinstance(item, list): return [x.try_eval() for x in item] if item == '-' and len(self.children) == 1: return -self.left.try_eval() try: return self.funct[item](self.left.try_eval(), self.right.try_eval()) except ZeroDivisionError: error(self.symbol.lineno, 'Division by 0') except KeyError: pass except TypeError: pass return None
[ "def", "try_eval", "(", "self", ")", ":", "item", "=", "self", ".", "symbol", ".", "item", "if", "isinstance", "(", "item", ",", "int", ")", ":", "return", "item", "if", "isinstance", "(", "item", ",", "Label", ")", ":", "if", "item", ".", "defined", ":", "if", "isinstance", "(", "item", ".", "value", ",", "Expr", ")", ":", "return", "item", ".", "value", ".", "try_eval", "(", ")", "else", ":", "return", "item", ".", "value", "else", ":", "if", "Expr", ".", "ignore", ":", "return", "None", "# Try to resolve into the global namespace", "error", "(", "self", ".", "symbol", ".", "lineno", ",", "\"Undefined label '%s'\"", "%", "item", ".", "name", ")", "return", "None", "try", ":", "if", "isinstance", "(", "item", ",", "tuple", ")", ":", "return", "tuple", "(", "[", "x", ".", "try_eval", "(", ")", "for", "x", "in", "item", "]", ")", "if", "isinstance", "(", "item", ",", "list", ")", ":", "return", "[", "x", ".", "try_eval", "(", ")", "for", "x", "in", "item", "]", "if", "item", "==", "'-'", "and", "len", "(", "self", ".", "children", ")", "==", "1", ":", "return", "-", "self", ".", "left", ".", "try_eval", "(", ")", "try", ":", "return", "self", ".", "funct", "[", "item", "]", "(", "self", ".", "left", ".", "try_eval", "(", ")", ",", "self", ".", "right", ".", "try_eval", "(", ")", ")", "except", "ZeroDivisionError", ":", "error", "(", "self", ".", "symbol", ".", "lineno", ",", "'Division by 0'", ")", "except", "KeyError", ":", "pass", "except", "TypeError", ":", "pass", "return", "None" ]
Recursively evals the node. Returns None if it is still unresolved.
[ "Recursively", "evals", "the", "node", ".", "Returns", "None", "if", "it", "is", "still", "unresolved", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L254-L297
3,503
boriel/zxbasic
asmparse.py
Label.define
def define(self, value, lineno, namespace=None): """ Defines label value. It can be anything. Even an AST """ if self.defined: error(lineno, "label '%s' already defined at line %i" % (self.name, self.lineno)) self.value = value self.lineno = lineno self.namespace = NAMESPACE if namespace is None else namespace
python
def define(self, value, lineno, namespace=None): if self.defined: error(lineno, "label '%s' already defined at line %i" % (self.name, self.lineno)) self.value = value self.lineno = lineno self.namespace = NAMESPACE if namespace is None else namespace
[ "def", "define", "(", "self", ",", "value", ",", "lineno", ",", "namespace", "=", "None", ")", ":", "if", "self", ".", "defined", ":", "error", "(", "lineno", ",", "\"label '%s' already defined at line %i\"", "%", "(", "self", ".", "name", ",", "self", ".", "lineno", ")", ")", "self", ".", "value", "=", "value", "self", ".", "lineno", "=", "lineno", "self", ".", "namespace", "=", "NAMESPACE", "if", "namespace", "is", "None", "else", "namespace" ]
Defines label value. It can be anything. Even an AST
[ "Defines", "label", "value", ".", "It", "can", "be", "anything", ".", "Even", "an", "AST" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L329-L337
3,504
boriel/zxbasic
asmparse.py
Memory.set_org
def set_org(self, value, lineno): """ Sets a new ORG value """ if value < 0 or value > MAX_MEM: error(lineno, "Memory ORG out of range [0 .. 65535]. Current value: %i" % value) self.index = self.ORG = value
python
def set_org(self, value, lineno): if value < 0 or value > MAX_MEM: error(lineno, "Memory ORG out of range [0 .. 65535]. Current value: %i" % value) self.index = self.ORG = value
[ "def", "set_org", "(", "self", ",", "value", ",", "lineno", ")", ":", "if", "value", "<", "0", "or", "value", ">", "MAX_MEM", ":", "error", "(", "lineno", ",", "\"Memory ORG out of range [0 .. 65535]. Current value: %i\"", "%", "value", ")", "self", ".", "index", "=", "self", ".", "ORG", "=", "value" ]
Sets a new ORG value
[ "Sets", "a", "new", "ORG", "value" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L377-L383
3,505
boriel/zxbasic
asmparse.py
Memory.id_name
def id_name(label, namespace=None): """ Given a name and a namespace, resolves returns the name as namespace + '.' + name. If namespace is none, the current NAMESPACE is used """ if not label.startswith(DOT): if namespace is None: namespace = NAMESPACE ex_label = namespace + label # The mangled namespace.labelname label else: if namespace is None: namespace = GLOBAL_NAMESPACE # Global namespace ex_label = label return ex_label, namespace
python
def id_name(label, namespace=None): if not label.startswith(DOT): if namespace is None: namespace = NAMESPACE ex_label = namespace + label # The mangled namespace.labelname label else: if namespace is None: namespace = GLOBAL_NAMESPACE # Global namespace ex_label = label return ex_label, namespace
[ "def", "id_name", "(", "label", ",", "namespace", "=", "None", ")", ":", "if", "not", "label", ".", "startswith", "(", "DOT", ")", ":", "if", "namespace", "is", "None", ":", "namespace", "=", "NAMESPACE", "ex_label", "=", "namespace", "+", "label", "# The mangled namespace.labelname label", "else", ":", "if", "namespace", "is", "None", ":", "namespace", "=", "GLOBAL_NAMESPACE", "# Global namespace", "ex_label", "=", "label", "return", "ex_label", ",", "namespace" ]
Given a name and a namespace, resolves returns the name as namespace + '.' + name. If namespace is none, the current NAMESPACE is used
[ "Given", "a", "name", "and", "a", "namespace", "resolves", "returns", "the", "name", "as", "namespace", "+", ".", "+", "name", ".", "If", "namespace", "is", "none", "the", "current", "NAMESPACE", "is", "used" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L386-L400
3,506
boriel/zxbasic
asmparse.py
Memory.__set_byte
def __set_byte(self, byte, lineno): """ Sets a byte at the current location, and increments org in one. Raises an error if org > MAX_MEMORY """ if byte < 0 or byte > 255: error(lineno, 'Invalid byte value %i' % byte) self.memory_bytes[self.org] = byte self.index += 1
python
def __set_byte(self, byte, lineno): if byte < 0 or byte > 255: error(lineno, 'Invalid byte value %i' % byte) self.memory_bytes[self.org] = byte self.index += 1
[ "def", "__set_byte", "(", "self", ",", "byte", ",", "lineno", ")", ":", "if", "byte", "<", "0", "or", "byte", ">", "255", ":", "error", "(", "lineno", ",", "'Invalid byte value %i'", "%", "byte", ")", "self", ".", "memory_bytes", "[", "self", ".", "org", "]", "=", "byte", "self", ".", "index", "+=", "1" ]
Sets a byte at the current location, and increments org in one. Raises an error if org > MAX_MEMORY
[ "Sets", "a", "byte", "at", "the", "current", "location", "and", "increments", "org", "in", "one", ".", "Raises", "an", "error", "if", "org", ">", "MAX_MEMORY" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L408-L416
3,507
boriel/zxbasic
asmparse.py
Memory.exit_proc
def exit_proc(self, lineno): """ Exits current procedure. Local labels are transferred to global scope unless they have been marked as local ones. Raises an error if no current local context (stack underflow) """ __DEBUG__('Exiting current scope from lineno %i' % lineno) if len(self.local_labels) <= 1: error(lineno, 'ENDP in global scope (with no PROC)') return for label in self.local_labels[-1].values(): if label.local: if not label.defined: error(lineno, "Undefined LOCAL label '%s'" % label.name) return continue name = label.name _lineno = label.lineno value = label.value if name not in self.global_labels.keys(): self.global_labels[name] = label else: self.global_labels[name].define(value, _lineno) self.local_labels.pop() # Removes current context self.scopes.pop()
python
def exit_proc(self, lineno): __DEBUG__('Exiting current scope from lineno %i' % lineno) if len(self.local_labels) <= 1: error(lineno, 'ENDP in global scope (with no PROC)') return for label in self.local_labels[-1].values(): if label.local: if not label.defined: error(lineno, "Undefined LOCAL label '%s'" % label.name) return continue name = label.name _lineno = label.lineno value = label.value if name not in self.global_labels.keys(): self.global_labels[name] = label else: self.global_labels[name].define(value, _lineno) self.local_labels.pop() # Removes current context self.scopes.pop()
[ "def", "exit_proc", "(", "self", ",", "lineno", ")", ":", "__DEBUG__", "(", "'Exiting current scope from lineno %i'", "%", "lineno", ")", "if", "len", "(", "self", ".", "local_labels", ")", "<=", "1", ":", "error", "(", "lineno", ",", "'ENDP in global scope (with no PROC)'", ")", "return", "for", "label", "in", "self", ".", "local_labels", "[", "-", "1", "]", ".", "values", "(", ")", ":", "if", "label", ".", "local", ":", "if", "not", "label", ".", "defined", ":", "error", "(", "lineno", ",", "\"Undefined LOCAL label '%s'\"", "%", "label", ".", "name", ")", "return", "continue", "name", "=", "label", ".", "name", "_lineno", "=", "label", ".", "lineno", "value", "=", "label", ".", "value", "if", "name", "not", "in", "self", ".", "global_labels", ".", "keys", "(", ")", ":", "self", ".", "global_labels", "[", "name", "]", "=", "label", "else", ":", "self", ".", "global_labels", "[", "name", "]", ".", "define", "(", "value", ",", "_lineno", ")", "self", ".", "local_labels", ".", "pop", "(", ")", "# Removes current context", "self", ".", "scopes", ".", "pop", "(", ")" ]
Exits current procedure. Local labels are transferred to global scope unless they have been marked as local ones. Raises an error if no current local context (stack underflow)
[ "Exits", "current", "procedure", ".", "Local", "labels", "are", "transferred", "to", "global", "scope", "unless", "they", "have", "been", "marked", "as", "local", "ones", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L418-L447
3,508
boriel/zxbasic
asmparse.py
Memory.dump
def dump(self): """ Returns a tuple containing code ORG, and a list of OUTPUT """ org = min(self.memory_bytes.keys()) # Org is the lowest one OUTPUT = [] align = [] for i in range(org, max(self.memory_bytes.keys()) + 1): if gl.has_errors: return org, OUTPUT try: try: a = [x for x in self.orgs[i] if isinstance(x, Asm)] # search for asm instructions if not a: align.append(0) # Fill with ZEROes not used memory regions continue OUTPUT += align align = [] a = a[0] if a.pending: a.arg = a.argval() a.pending = False tmp = a.bytes() for r in range(len(tmp)): self.memory_bytes[i + r] = tmp[r] except KeyError: pass OUTPUT.append(self.memory_bytes[i]) except KeyError: OUTPUT.append(0) # Fill with ZEROes not used memory regions return org, OUTPUT
python
def dump(self): org = min(self.memory_bytes.keys()) # Org is the lowest one OUTPUT = [] align = [] for i in range(org, max(self.memory_bytes.keys()) + 1): if gl.has_errors: return org, OUTPUT try: try: a = [x for x in self.orgs[i] if isinstance(x, Asm)] # search for asm instructions if not a: align.append(0) # Fill with ZEROes not used memory regions continue OUTPUT += align align = [] a = a[0] if a.pending: a.arg = a.argval() a.pending = False tmp = a.bytes() for r in range(len(tmp)): self.memory_bytes[i + r] = tmp[r] except KeyError: pass OUTPUT.append(self.memory_bytes[i]) except KeyError: OUTPUT.append(0) # Fill with ZEROes not used memory regions return org, OUTPUT
[ "def", "dump", "(", "self", ")", ":", "org", "=", "min", "(", "self", ".", "memory_bytes", ".", "keys", "(", ")", ")", "# Org is the lowest one", "OUTPUT", "=", "[", "]", "align", "=", "[", "]", "for", "i", "in", "range", "(", "org", ",", "max", "(", "self", ".", "memory_bytes", ".", "keys", "(", ")", ")", "+", "1", ")", ":", "if", "gl", ".", "has_errors", ":", "return", "org", ",", "OUTPUT", "try", ":", "try", ":", "a", "=", "[", "x", "for", "x", "in", "self", ".", "orgs", "[", "i", "]", "if", "isinstance", "(", "x", ",", "Asm", ")", "]", "# search for asm instructions", "if", "not", "a", ":", "align", ".", "append", "(", "0", ")", "# Fill with ZEROes not used memory regions", "continue", "OUTPUT", "+=", "align", "align", "=", "[", "]", "a", "=", "a", "[", "0", "]", "if", "a", ".", "pending", ":", "a", ".", "arg", "=", "a", ".", "argval", "(", ")", "a", ".", "pending", "=", "False", "tmp", "=", "a", ".", "bytes", "(", ")", "for", "r", "in", "range", "(", "len", "(", "tmp", ")", ")", ":", "self", ".", "memory_bytes", "[", "i", "+", "r", "]", "=", "tmp", "[", "r", "]", "except", "KeyError", ":", "pass", "OUTPUT", ".", "append", "(", "self", ".", "memory_bytes", "[", "i", "]", ")", "except", "KeyError", ":", "OUTPUT", ".", "append", "(", "0", ")", "# Fill with ZEROes not used memory regions", "return", "org", ",", "OUTPUT" ]
Returns a tuple containing code ORG, and a list of OUTPUT
[ "Returns", "a", "tuple", "containing", "code", "ORG", "and", "a", "list", "of", "OUTPUT" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L470-L507
3,509
boriel/zxbasic
asmparse.py
Memory.get_label
def get_label(self, label, lineno): """ Returns a label in the current context or in the global one. If the label does not exists, creates a new one and returns it. """ global NAMESPACE ex_label, namespace = Memory.id_name(label) for i in range(len(self.local_labels) - 1, -1, -1): # Downstep result = self.local_labels[i].get(ex_label, None) if result is not None: return result result = Label(ex_label, lineno, namespace=namespace) self.local_labels[-1][ex_label] = result # HINT: no namespace return result
python
def get_label(self, label, lineno): global NAMESPACE ex_label, namespace = Memory.id_name(label) for i in range(len(self.local_labels) - 1, -1, -1): # Downstep result = self.local_labels[i].get(ex_label, None) if result is not None: return result result = Label(ex_label, lineno, namespace=namespace) self.local_labels[-1][ex_label] = result # HINT: no namespace return result
[ "def", "get_label", "(", "self", ",", "label", ",", "lineno", ")", ":", "global", "NAMESPACE", "ex_label", ",", "namespace", "=", "Memory", ".", "id_name", "(", "label", ")", "for", "i", "in", "range", "(", "len", "(", "self", ".", "local_labels", ")", "-", "1", ",", "-", "1", ",", "-", "1", ")", ":", "# Downstep", "result", "=", "self", ".", "local_labels", "[", "i", "]", ".", "get", "(", "ex_label", ",", "None", ")", "if", "result", "is", "not", "None", ":", "return", "result", "result", "=", "Label", "(", "ex_label", ",", "lineno", ",", "namespace", "=", "namespace", ")", "self", ".", "local_labels", "[", "-", "1", "]", "[", "ex_label", "]", "=", "result", "# HINT: no namespace", "return", "result" ]
Returns a label in the current context or in the global one. If the label does not exists, creates a new one and returns it.
[ "Returns", "a", "label", "in", "the", "current", "context", "or", "in", "the", "global", "one", ".", "If", "the", "label", "does", "not", "exists", "creates", "a", "new", "one", "and", "returns", "it", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmparse.py#L532-L548
3,510
boriel/zxbasic
arch/zx48k/backend/__array.py
_addr
def _addr(value): ''' Common subroutine for emitting array address ''' output = [] try: indirect = False if value[0] == '*': indirect = True value = value[1:] value = int(value) & 0xFFFF if indirect: output.append('ld hl, (%s)' % str(value)) else: output.append('ld hl, %s' % str(value)) except ValueError: if value[0] == '_': output.append('ld hl, %s' % str(value)) if indirect: output.append('ld c, (hl)') output.append('inc hl') output.append('ld h, (hl)') output.append('ld l, c') else: output.append('pop hl') if indirect: output.append('ld c, (hl)') output.append('inc hl') output.append('ld h, (hl)') output.append('ld l, c') output.append('call __ARRAY') REQUIRES.add('array.asm') return output
python
def _addr(value): ''' Common subroutine for emitting array address ''' output = [] try: indirect = False if value[0] == '*': indirect = True value = value[1:] value = int(value) & 0xFFFF if indirect: output.append('ld hl, (%s)' % str(value)) else: output.append('ld hl, %s' % str(value)) except ValueError: if value[0] == '_': output.append('ld hl, %s' % str(value)) if indirect: output.append('ld c, (hl)') output.append('inc hl') output.append('ld h, (hl)') output.append('ld l, c') else: output.append('pop hl') if indirect: output.append('ld c, (hl)') output.append('inc hl') output.append('ld h, (hl)') output.append('ld l, c') output.append('call __ARRAY') REQUIRES.add('array.asm') return output
[ "def", "_addr", "(", "value", ")", ":", "output", "=", "[", "]", "try", ":", "indirect", "=", "False", "if", "value", "[", "0", "]", "==", "'*'", ":", "indirect", "=", "True", "value", "=", "value", "[", "1", ":", "]", "value", "=", "int", "(", "value", ")", "&", "0xFFFF", "if", "indirect", ":", "output", ".", "append", "(", "'ld hl, (%s)'", "%", "str", "(", "value", ")", ")", "else", ":", "output", ".", "append", "(", "'ld hl, %s'", "%", "str", "(", "value", ")", ")", "except", "ValueError", ":", "if", "value", "[", "0", "]", "==", "'_'", ":", "output", ".", "append", "(", "'ld hl, %s'", "%", "str", "(", "value", ")", ")", "if", "indirect", ":", "output", ".", "append", "(", "'ld c, (hl)'", ")", "output", ".", "append", "(", "'inc hl'", ")", "output", ".", "append", "(", "'ld h, (hl)'", ")", "output", ".", "append", "(", "'ld l, c'", ")", "else", ":", "output", ".", "append", "(", "'pop hl'", ")", "if", "indirect", ":", "output", ".", "append", "(", "'ld c, (hl)'", ")", "output", ".", "append", "(", "'inc hl'", ")", "output", ".", "append", "(", "'ld h, (hl)'", ")", "output", ".", "append", "(", "'ld l, c'", ")", "output", ".", "append", "(", "'call __ARRAY'", ")", "REQUIRES", ".", "add", "(", "'array.asm'", ")", "return", "output" ]
Common subroutine for emitting array address
[ "Common", "subroutine", "for", "emitting", "array", "address" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__array.py#L20-L56
3,511
boriel/zxbasic
symbols/arglist.py
SymbolARGLIST.make_node
def make_node(cls, node, *args): """ This will return a node with an argument_list. """ if node is None: node = cls() assert isinstance(node, SymbolARGUMENT) or isinstance(node, cls) if not isinstance(node, cls): return cls.make_node(None, node, *args) for arg in args: assert isinstance(arg, SymbolARGUMENT) node.appendChild(arg) return node
python
def make_node(cls, node, *args): if node is None: node = cls() assert isinstance(node, SymbolARGUMENT) or isinstance(node, cls) if not isinstance(node, cls): return cls.make_node(None, node, *args) for arg in args: assert isinstance(arg, SymbolARGUMENT) node.appendChild(arg) return node
[ "def", "make_node", "(", "cls", ",", "node", ",", "*", "args", ")", ":", "if", "node", "is", "None", ":", "node", "=", "cls", "(", ")", "assert", "isinstance", "(", "node", ",", "SymbolARGUMENT", ")", "or", "isinstance", "(", "node", ",", "cls", ")", "if", "not", "isinstance", "(", "node", ",", "cls", ")", ":", "return", "cls", ".", "make_node", "(", "None", ",", "node", ",", "*", "args", ")", "for", "arg", "in", "args", ":", "assert", "isinstance", "(", "arg", ",", "SymbolARGUMENT", ")", "node", ".", "appendChild", "(", "arg", ")", "return", "node" ]
This will return a node with an argument_list.
[ "This", "will", "return", "a", "node", "with", "an", "argument_list", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/arglist.py#L50-L65
3,512
boriel/zxbasic
arch/zx48k/backend/__32bit.py
_mul32
def _mul32(ins): """ Multiplies two last 32bit values on top of the stack and and returns the value on top of the stack Optimizations done: * If any operand is 1, do nothing * If any operand is 0, push 0 """ op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2): op1, op2 = _int_ops(op1, op2) output = _32bit_oper(op1) if op2 == 1: output.append('push de') output.append('push hl') return output # A * 1 = Nothing if op2 == 0: output.append('ld hl, 0') output.append('push hl') output.append('push hl') return output output = _32bit_oper(op1, op2) output.append('call __MUL32') # Inmmediate output.append('push de') output.append('push hl') REQUIRES.add('mul32.asm') return output
python
def _mul32(ins): op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2): op1, op2 = _int_ops(op1, op2) output = _32bit_oper(op1) if op2 == 1: output.append('push de') output.append('push hl') return output # A * 1 = Nothing if op2 == 0: output.append('ld hl, 0') output.append('push hl') output.append('push hl') return output output = _32bit_oper(op1, op2) output.append('call __MUL32') # Inmmediate output.append('push de') output.append('push hl') REQUIRES.add('mul32.asm') return output
[ "def", "_mul32", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "if", "_int_ops", "(", "op1", ",", "op2", ")", ":", "op1", ",", "op2", "=", "_int_ops", "(", "op1", ",", "op2", ")", "output", "=", "_32bit_oper", "(", "op1", ")", "if", "op2", "==", "1", ":", "output", ".", "append", "(", "'push de'", ")", "output", ".", "append", "(", "'push hl'", ")", "return", "output", "# A * 1 = Nothing", "if", "op2", "==", "0", ":", "output", ".", "append", "(", "'ld hl, 0'", ")", "output", ".", "append", "(", "'push hl'", ")", "output", ".", "append", "(", "'push hl'", ")", "return", "output", "output", "=", "_32bit_oper", "(", "op1", ",", "op2", ")", "output", ".", "append", "(", "'call __MUL32'", ")", "# Inmmediate", "output", ".", "append", "(", "'push de'", ")", "output", ".", "append", "(", "'push hl'", ")", "REQUIRES", ".", "add", "(", "'mul32.asm'", ")", "return", "output" ]
Multiplies two last 32bit values on top of the stack and and returns the value on top of the stack Optimizations done: * If any operand is 1, do nothing * If any operand is 0, push 0
[ "Multiplies", "two", "last", "32bit", "values", "on", "top", "of", "the", "stack", "and", "and", "returns", "the", "value", "on", "top", "of", "the", "stack" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__32bit.py#L267-L297
3,513
boriel/zxbasic
arch/zx48k/backend/__32bit.py
_shl32
def _shl32(ins): """ Logical Left shift 32bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd operand is 0, do nothing """ op1, op2 = tuple(ins.quad[2:]) if is_int(op2): output = _32bit_oper(op1) if int(op2) == 0: output.append('push de') output.append('push hl') return output if int(op2) > 1: label = tmp_label() output.append('ld b, %s' % op2) output.append('%s:' % label) output.append('call __SHL32') output.append('djnz %s' % label) else: output.append('call __SHL32') output.append('push de') output.append('push hl') REQUIRES.add('shl32.asm') return output output = _8bit_oper(op2) output.append('ld b, a') output.extend(_32bit_oper(op1)) label = tmp_label() output.append('%s:' % label) output.append('call __SHL32') output.append('djnz %s' % label) output.append('push de') output.append('push hl') REQUIRES.add('shl32.asm') return output
python
def _shl32(ins): op1, op2 = tuple(ins.quad[2:]) if is_int(op2): output = _32bit_oper(op1) if int(op2) == 0: output.append('push de') output.append('push hl') return output if int(op2) > 1: label = tmp_label() output.append('ld b, %s' % op2) output.append('%s:' % label) output.append('call __SHL32') output.append('djnz %s' % label) else: output.append('call __SHL32') output.append('push de') output.append('push hl') REQUIRES.add('shl32.asm') return output output = _8bit_oper(op2) output.append('ld b, a') output.extend(_32bit_oper(op1)) label = tmp_label() output.append('%s:' % label) output.append('call __SHL32') output.append('djnz %s' % label) output.append('push de') output.append('push hl') REQUIRES.add('shl32.asm') return output
[ "def", "_shl32", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "if", "is_int", "(", "op2", ")", ":", "output", "=", "_32bit_oper", "(", "op1", ")", "if", "int", "(", "op2", ")", "==", "0", ":", "output", ".", "append", "(", "'push de'", ")", "output", ".", "append", "(", "'push hl'", ")", "return", "output", "if", "int", "(", "op2", ")", ">", "1", ":", "label", "=", "tmp_label", "(", ")", "output", ".", "append", "(", "'ld b, %s'", "%", "op2", ")", "output", ".", "append", "(", "'%s:'", "%", "label", ")", "output", ".", "append", "(", "'call __SHL32'", ")", "output", ".", "append", "(", "'djnz %s'", "%", "label", ")", "else", ":", "output", ".", "append", "(", "'call __SHL32'", ")", "output", ".", "append", "(", "'push de'", ")", "output", ".", "append", "(", "'push hl'", ")", "REQUIRES", ".", "add", "(", "'shl32.asm'", ")", "return", "output", "output", "=", "_8bit_oper", "(", "op2", ")", "output", ".", "append", "(", "'ld b, a'", ")", "output", ".", "extend", "(", "_32bit_oper", "(", "op1", ")", ")", "label", "=", "tmp_label", "(", ")", "output", ".", "append", "(", "'%s:'", "%", "label", ")", "output", ".", "append", "(", "'call __SHL32'", ")", "output", ".", "append", "(", "'djnz %s'", "%", "label", ")", "output", ".", "append", "(", "'push de'", ")", "output", ".", "append", "(", "'push hl'", ")", "REQUIRES", ".", "add", "(", "'shl32.asm'", ")", "return", "output" ]
Logical Left shift 32bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd operand is 0, do nothing
[ "Logical", "Left", "shift", "32bit", "unsigned", "integers", ".", "The", "result", "is", "pushed", "onto", "the", "stack", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__32bit.py#L826-L868
3,514
boriel/zxbasic
arch/zx48k/backend/__float.py
_float_oper
def _float_oper(op1, op2=None): ''' Returns pop sequence for floating point operands 1st operand in A DE BC, 2nd operand remains in the stack Unlike 8bit and 16bit version, this does not supports operands inversion. Since many of the instructions are implemented as functions, they must support this. However, if 1st operand is a number (immediate) or indirect, the stack will be rearranged, so it contains a 48 bit pushed parameter value for the subroutine to be called. ''' output = [] op = op2 if op2 is not None else op1 indirect = (op[0] == '*') if indirect: op = op[1:] if is_float(op): op = float(op) if indirect: op = int(op) & 0xFFFF output.append('ld hl, (%i)' % op) output.append('call __ILOADF') REQUIRES.add('iloadf.asm') else: A, DE, BC = _float(op) output.append('ld a, %s' % A) output.append('ld de, %s' % DE) output.append('ld bc, %s' % BC) else: if indirect: if op[0] == '_': output.append('ld hl, (%s)' % op) else: output.append('pop hl') output.append('call __ILOADF') REQUIRES.add('iloadf.asm') else: if op[0] == '_': output.append('ld a, (%s)' % op) output.append('ld de, (%s + 1)' % op) output.append('ld bc, (%s + 3)' % op) else: output.extend(_fpop()) if op2 is not None: op = op1 if is_float(op): # An float must be in the stack. Let's pushit A, DE, BC = _float(op) output.append('ld hl, %s' % BC) output.append('push hl') output.append('ld hl, %s' % DE) output.append('push hl') output.append('ld h, %s' % A) output.append('push hl') elif op[0] == '*': # Indirect op = op[1:] output.append('exx') # uses alternate set to put it on the stack output.append("ex af, af'") if is_int(op): # noqa TODO: it will fail op = int(op) output.append('ld hl, %i' % op) elif op[0] == '_': output.append('ld hl, (%s)' % op) else: output.append('pop hl') output.append('call __ILOADF') output.extend(_fpush()) output.append("ex af, af'") output.append('exx') REQUIRES.add('iloadf.asm') elif op[0] == '_': if is_float(op2): tmp = output output = [] output.append('ld hl, %s + 4' % op) ''' output.append('ld hl, (%s + 3)' % op) output.append('push hl') output.append('ld hl, (%s + 1)' % op) output.append('push hl') output.append('ld a, (%s)' % op) output.append('push af') ''' output.append('call __FP_PUSH_REV') output.extend(tmp) REQUIRES.add('pushf.asm') else: ''' output.append('ld hl, (%s + 3)' % op) output.append('push hl') output.append('ld hl, (%s + 1)' % op) output.append('push hl') output.append('ld hl, (%s - 1)' % op) output.append('push hl') ''' output.append('ld hl, %s + 4' % op) output.append('call __FP_PUSH_REV') REQUIRES.add('pushf.asm') else: pass # Else do nothing, and leave the op onto the stack return output
python
def _float_oper(op1, op2=None): ''' Returns pop sequence for floating point operands 1st operand in A DE BC, 2nd operand remains in the stack Unlike 8bit and 16bit version, this does not supports operands inversion. Since many of the instructions are implemented as functions, they must support this. However, if 1st operand is a number (immediate) or indirect, the stack will be rearranged, so it contains a 48 bit pushed parameter value for the subroutine to be called. ''' output = [] op = op2 if op2 is not None else op1 indirect = (op[0] == '*') if indirect: op = op[1:] if is_float(op): op = float(op) if indirect: op = int(op) & 0xFFFF output.append('ld hl, (%i)' % op) output.append('call __ILOADF') REQUIRES.add('iloadf.asm') else: A, DE, BC = _float(op) output.append('ld a, %s' % A) output.append('ld de, %s' % DE) output.append('ld bc, %s' % BC) else: if indirect: if op[0] == '_': output.append('ld hl, (%s)' % op) else: output.append('pop hl') output.append('call __ILOADF') REQUIRES.add('iloadf.asm') else: if op[0] == '_': output.append('ld a, (%s)' % op) output.append('ld de, (%s + 1)' % op) output.append('ld bc, (%s + 3)' % op) else: output.extend(_fpop()) if op2 is not None: op = op1 if is_float(op): # An float must be in the stack. Let's pushit A, DE, BC = _float(op) output.append('ld hl, %s' % BC) output.append('push hl') output.append('ld hl, %s' % DE) output.append('push hl') output.append('ld h, %s' % A) output.append('push hl') elif op[0] == '*': # Indirect op = op[1:] output.append('exx') # uses alternate set to put it on the stack output.append("ex af, af'") if is_int(op): # noqa TODO: it will fail op = int(op) output.append('ld hl, %i' % op) elif op[0] == '_': output.append('ld hl, (%s)' % op) else: output.append('pop hl') output.append('call __ILOADF') output.extend(_fpush()) output.append("ex af, af'") output.append('exx') REQUIRES.add('iloadf.asm') elif op[0] == '_': if is_float(op2): tmp = output output = [] output.append('ld hl, %s + 4' % op) ''' output.append('ld hl, (%s + 3)' % op) output.append('push hl') output.append('ld hl, (%s + 1)' % op) output.append('push hl') output.append('ld a, (%s)' % op) output.append('push af') ''' output.append('call __FP_PUSH_REV') output.extend(tmp) REQUIRES.add('pushf.asm') else: ''' output.append('ld hl, (%s + 3)' % op) output.append('push hl') output.append('ld hl, (%s + 1)' % op) output.append('push hl') output.append('ld hl, (%s - 1)' % op) output.append('push hl') ''' output.append('ld hl, %s + 4' % op) output.append('call __FP_PUSH_REV') REQUIRES.add('pushf.asm') else: pass # Else do nothing, and leave the op onto the stack return output
[ "def", "_float_oper", "(", "op1", ",", "op2", "=", "None", ")", ":", "output", "=", "[", "]", "op", "=", "op2", "if", "op2", "is", "not", "None", "else", "op1", "indirect", "=", "(", "op", "[", "0", "]", "==", "'*'", ")", "if", "indirect", ":", "op", "=", "op", "[", "1", ":", "]", "if", "is_float", "(", "op", ")", ":", "op", "=", "float", "(", "op", ")", "if", "indirect", ":", "op", "=", "int", "(", "op", ")", "&", "0xFFFF", "output", ".", "append", "(", "'ld hl, (%i)'", "%", "op", ")", "output", ".", "append", "(", "'call __ILOADF'", ")", "REQUIRES", ".", "add", "(", "'iloadf.asm'", ")", "else", ":", "A", ",", "DE", ",", "BC", "=", "_float", "(", "op", ")", "output", ".", "append", "(", "'ld a, %s'", "%", "A", ")", "output", ".", "append", "(", "'ld de, %s'", "%", "DE", ")", "output", ".", "append", "(", "'ld bc, %s'", "%", "BC", ")", "else", ":", "if", "indirect", ":", "if", "op", "[", "0", "]", "==", "'_'", ":", "output", ".", "append", "(", "'ld hl, (%s)'", "%", "op", ")", "else", ":", "output", ".", "append", "(", "'pop hl'", ")", "output", ".", "append", "(", "'call __ILOADF'", ")", "REQUIRES", ".", "add", "(", "'iloadf.asm'", ")", "else", ":", "if", "op", "[", "0", "]", "==", "'_'", ":", "output", ".", "append", "(", "'ld a, (%s)'", "%", "op", ")", "output", ".", "append", "(", "'ld de, (%s + 1)'", "%", "op", ")", "output", ".", "append", "(", "'ld bc, (%s + 3)'", "%", "op", ")", "else", ":", "output", ".", "extend", "(", "_fpop", "(", ")", ")", "if", "op2", "is", "not", "None", ":", "op", "=", "op1", "if", "is_float", "(", "op", ")", ":", "# An float must be in the stack. Let's pushit", "A", ",", "DE", ",", "BC", "=", "_float", "(", "op", ")", "output", ".", "append", "(", "'ld hl, %s'", "%", "BC", ")", "output", ".", "append", "(", "'push hl'", ")", "output", ".", "append", "(", "'ld hl, %s'", "%", "DE", ")", "output", ".", "append", "(", "'push hl'", ")", "output", ".", "append", "(", "'ld h, %s'", "%", "A", ")", "output", ".", "append", "(", "'push hl'", ")", "elif", "op", "[", "0", "]", "==", "'*'", ":", "# Indirect", "op", "=", "op", "[", "1", ":", "]", "output", ".", "append", "(", "'exx'", ")", "# uses alternate set to put it on the stack", "output", ".", "append", "(", "\"ex af, af'\"", ")", "if", "is_int", "(", "op", ")", ":", "# noqa TODO: it will fail", "op", "=", "int", "(", "op", ")", "output", ".", "append", "(", "'ld hl, %i'", "%", "op", ")", "elif", "op", "[", "0", "]", "==", "'_'", ":", "output", ".", "append", "(", "'ld hl, (%s)'", "%", "op", ")", "else", ":", "output", ".", "append", "(", "'pop hl'", ")", "output", ".", "append", "(", "'call __ILOADF'", ")", "output", ".", "extend", "(", "_fpush", "(", ")", ")", "output", ".", "append", "(", "\"ex af, af'\"", ")", "output", ".", "append", "(", "'exx'", ")", "REQUIRES", ".", "add", "(", "'iloadf.asm'", ")", "elif", "op", "[", "0", "]", "==", "'_'", ":", "if", "is_float", "(", "op2", ")", ":", "tmp", "=", "output", "output", "=", "[", "]", "output", ".", "append", "(", "'ld hl, %s + 4'", "%", "op", ")", "'''\n output.append('ld hl, (%s + 3)' % op)\n output.append('push hl')\n output.append('ld hl, (%s + 1)' % op)\n output.append('push hl')\n output.append('ld a, (%s)' % op)\n output.append('push af')\n '''", "output", ".", "append", "(", "'call __FP_PUSH_REV'", ")", "output", ".", "extend", "(", "tmp", ")", "REQUIRES", ".", "add", "(", "'pushf.asm'", ")", "else", ":", "'''\n output.append('ld hl, (%s + 3)' % op)\n output.append('push hl')\n output.append('ld hl, (%s + 1)' % op)\n output.append('push hl')\n output.append('ld hl, (%s - 1)' % op)\n output.append('push hl')\n '''", "output", ".", "append", "(", "'ld hl, %s + 4'", "%", "op", ")", "output", ".", "append", "(", "'call __FP_PUSH_REV'", ")", "REQUIRES", ".", "add", "(", "'pushf.asm'", ")", "else", ":", "pass", "# Else do nothing, and leave the op onto the stack", "return", "output" ]
Returns pop sequence for floating point operands 1st operand in A DE BC, 2nd operand remains in the stack Unlike 8bit and 16bit version, this does not supports operands inversion. Since many of the instructions are implemented as functions, they must support this. However, if 1st operand is a number (immediate) or indirect, the stack will be rearranged, so it contains a 48 bit pushed parameter value for the subroutine to be called.
[ "Returns", "pop", "sequence", "for", "floating", "point", "operands", "1st", "operand", "in", "A", "DE", "BC", "2nd", "operand", "remains", "in", "the", "stack" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__float.py#L50-L157
3,515
boriel/zxbasic
arch/zx48k/backend/__float.py
_addf
def _addf(ins): ''' Adds 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) if _f_ops(op1, op2) is not None: opa, opb = _f_ops(op1, op2) if opb == 0: # A + 0 => A output = _float_oper(opa) output.extend(_fpush()) return output output = _float_oper(op1, op2) output.append('call __ADDF') output.extend(_fpush()) REQUIRES.add('addf.asm') return output
python
def _addf(ins): ''' Adds 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) if _f_ops(op1, op2) is not None: opa, opb = _f_ops(op1, op2) if opb == 0: # A + 0 => A output = _float_oper(opa) output.extend(_fpush()) return output output = _float_oper(op1, op2) output.append('call __ADDF') output.extend(_fpush()) REQUIRES.add('addf.asm') return output
[ "def", "_addf", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "if", "_f_ops", "(", "op1", ",", "op2", ")", "is", "not", "None", ":", "opa", ",", "opb", "=", "_f_ops", "(", "op1", ",", "op2", ")", "if", "opb", "==", "0", ":", "# A + 0 => A", "output", "=", "_float_oper", "(", "opa", ")", "output", ".", "extend", "(", "_fpush", "(", ")", ")", "return", "output", "output", "=", "_float_oper", "(", "op1", ",", "op2", ")", "output", ".", "append", "(", "'call __ADDF'", ")", "output", ".", "extend", "(", "_fpush", "(", ")", ")", "REQUIRES", ".", "add", "(", "'addf.asm'", ")", "return", "output" ]
Adds 2 float values. The result is pushed onto the stack.
[ "Adds", "2", "float", "values", ".", "The", "result", "is", "pushed", "onto", "the", "stack", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__float.py#L164-L180
3,516
boriel/zxbasic
arch/zx48k/backend/__float.py
_divf
def _divf(ins): ''' Divides 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) if is_float(op2) and float(op2) == 1: # Nothing to do. A / 1 = A output = _float_oper(op1) output.extend(_fpush()) return output output = _float_oper(op1, op2) output.append('call __DIVF') output.extend(_fpush()) REQUIRES.add('divf.asm') return output
python
def _divf(ins): ''' Divides 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) if is_float(op2) and float(op2) == 1: # Nothing to do. A / 1 = A output = _float_oper(op1) output.extend(_fpush()) return output output = _float_oper(op1, op2) output.append('call __DIVF') output.extend(_fpush()) REQUIRES.add('divf.asm') return output
[ "def", "_divf", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "if", "is_float", "(", "op2", ")", "and", "float", "(", "op2", ")", "==", "1", ":", "# Nothing to do. A / 1 = A", "output", "=", "_float_oper", "(", "op1", ")", "output", ".", "extend", "(", "_fpush", "(", ")", ")", "return", "output", "output", "=", "_float_oper", "(", "op1", ",", "op2", ")", "output", ".", "append", "(", "'call __DIVF'", ")", "output", ".", "extend", "(", "_fpush", "(", ")", ")", "REQUIRES", ".", "add", "(", "'divf.asm'", ")", "return", "output" ]
Divides 2 float values. The result is pushed onto the stack.
[ "Divides", "2", "float", "values", ".", "The", "result", "is", "pushed", "onto", "the", "stack", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__float.py#L219-L233
3,517
boriel/zxbasic
arch/zx48k/backend/__float.py
_modf
def _modf(ins): ''' Reminder of div. 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) output = _float_oper(op1, op2) output.append('call __MODF') output.extend(_fpush()) REQUIRES.add('modf.asm') return output
python
def _modf(ins): ''' Reminder of div. 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) output = _float_oper(op1, op2) output.append('call __MODF') output.extend(_fpush()) REQUIRES.add('modf.asm') return output
[ "def", "_modf", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "output", "=", "_float_oper", "(", "op1", ",", "op2", ")", "output", ".", "append", "(", "'call __MODF'", ")", "output", ".", "extend", "(", "_fpush", "(", ")", ")", "REQUIRES", ".", "add", "(", "'modf.asm'", ")", "return", "output" ]
Reminder of div. 2 float values. The result is pushed onto the stack.
[ "Reminder", "of", "div", ".", "2", "float", "values", ".", "The", "result", "is", "pushed", "onto", "the", "stack", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__float.py#L236-L244
3,518
boriel/zxbasic
asmlex.py
get_uniques
def get_uniques(l): """ Returns a list with no repeated elements. """ result = [] for i in l: if i not in result: result.append(i) return result
python
def get_uniques(l): result = [] for i in l: if i not in result: result.append(i) return result
[ "def", "get_uniques", "(", "l", ")", ":", "result", "=", "[", "]", "for", "i", "in", "l", ":", "if", "i", "not", "in", "result", ":", "result", ".", "append", "(", "i", ")", "return", "result" ]
Returns a list with no repeated elements.
[ "Returns", "a", "list", "with", "no", "repeated", "elements", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/asmlex.py#L161-L170
3,519
boriel/zxbasic
symbols/argument.py
SymbolARGUMENT.typecast
def typecast(self, type_): """ Test type casting to the argument expression. On success changes the node value to the new typecast, and returns True. On failure, returns False, and the node value is set to None. """ self.value = SymbolTYPECAST.make_node(type_, self.value, self.lineno) return self.value is not None
python
def typecast(self, type_): self.value = SymbolTYPECAST.make_node(type_, self.value, self.lineno) return self.value is not None
[ "def", "typecast", "(", "self", ",", "type_", ")", ":", "self", ".", "value", "=", "SymbolTYPECAST", ".", "make_node", "(", "type_", ",", "self", ".", "value", ",", "self", ".", "lineno", ")", "return", "self", ".", "value", "is", "not", "None" ]
Test type casting to the argument expression. On success changes the node value to the new typecast, and returns True. On failure, returns False, and the node value is set to None.
[ "Test", "type", "casting", "to", "the", "argument", "expression", ".", "On", "success", "changes", "the", "node", "value", "to", "the", "new", "typecast", "and", "returns", "True", ".", "On", "failure", "returns", "False", "and", "the", "node", "value", "is", "set", "to", "None", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/argument.py#L88-L94
3,520
boriel/zxbasic
api/errmsg.py
syntax_error
def syntax_error(lineno, msg): """ Generic syntax error routine """ if global_.has_errors > OPTIONS.max_syntax_errors.value: msg = 'Too many errors. Giving up!' msg = "%s:%i: %s" % (global_.FILENAME, lineno, msg) msg_output(msg) if global_.has_errors > OPTIONS.max_syntax_errors.value: sys.exit(1) global_.has_errors += 1
python
def syntax_error(lineno, msg): if global_.has_errors > OPTIONS.max_syntax_errors.value: msg = 'Too many errors. Giving up!' msg = "%s:%i: %s" % (global_.FILENAME, lineno, msg) msg_output(msg) if global_.has_errors > OPTIONS.max_syntax_errors.value: sys.exit(1) global_.has_errors += 1
[ "def", "syntax_error", "(", "lineno", ",", "msg", ")", ":", "if", "global_", ".", "has_errors", ">", "OPTIONS", ".", "max_syntax_errors", ".", "value", ":", "msg", "=", "'Too many errors. Giving up!'", "msg", "=", "\"%s:%i: %s\"", "%", "(", "global_", ".", "FILENAME", ",", "lineno", ",", "msg", ")", "msg_output", "(", "msg", ")", "if", "global_", ".", "has_errors", ">", "OPTIONS", ".", "max_syntax_errors", ".", "value", ":", "sys", ".", "exit", "(", "1", ")", "global_", ".", "has_errors", "+=", "1" ]
Generic syntax error routine
[ "Generic", "syntax", "error", "routine" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/api/errmsg.py#L28-L40
3,521
boriel/zxbasic
api/errmsg.py
warning
def warning(lineno, msg): """ Generic warning error routine """ msg = "%s:%i: warning: %s" % (global_.FILENAME, lineno, msg) msg_output(msg) global_.has_warnings += 1
python
def warning(lineno, msg): msg = "%s:%i: warning: %s" % (global_.FILENAME, lineno, msg) msg_output(msg) global_.has_warnings += 1
[ "def", "warning", "(", "lineno", ",", "msg", ")", ":", "msg", "=", "\"%s:%i: warning: %s\"", "%", "(", "global_", ".", "FILENAME", ",", "lineno", ",", "msg", ")", "msg_output", "(", "msg", ")", "global_", ".", "has_warnings", "+=", "1" ]
Generic warning error routine
[ "Generic", "warning", "error", "routine" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/api/errmsg.py#L43-L48
3,522
boriel/zxbasic
arch/zx48k/optimizer.py
oper
def oper(inst): """ Returns operands of an ASM instruction. Even "indirect" operands, like SP if RET or CALL is used. """ i = inst.strip(' \t\n').split(' ') I = i[0].lower() # Instruction i = ''.join(i[1:]) op = i.split(',') if I in {'call', 'jp', 'jr'} and len(op) > 1: op = op[1:] + ['f'] elif I == 'djnz': op.append('b') elif I in {'push', 'pop', 'call'}: op.append('sp') # Sp is also affected by push, pop and call elif I in {'or', 'and', 'xor', 'neg', 'cpl', 'rrca', 'rlca'}: op.append('a') elif I in {'rra', 'rla'}: op.extend(['a', 'f']) elif I in ('rr', 'rl'): op.append('f') elif I in {'adc', 'sbc'}: if len(op) == 1: op = ['a', 'f'] + op elif I in {'add', 'sub'}: if len(op) == 1: op = ['a'] + op elif I in {'ldd', 'ldi', 'lddr', 'ldir'}: op = ['hl', 'de', 'bc'] elif I in {'cpd', 'cpi', 'cpdr', 'cpir'}: op = ['a', 'hl', 'bc'] elif I == 'exx': op = ['*', 'bc', 'de', 'hl', 'b', 'c', 'd', 'e', 'h', 'l'] elif I in {'ret', 'reti', 'retn'}: op += ['sp'] elif I == 'out': if len(op) and RE_OUTC.match(op[0]): op[0] = 'c' else: op.pop(0) elif I == 'in': if len(op) > 1 and RE_OUTC.match(op[1]): op[1] = 'c' else: op.pop(1) for i in range(len(op)): tmp = RE_INDIR16.match(op[i]) if tmp is not None: op[i] = '(' + op[i].strip()[1:-1].strip().lower() + ')' # ' ( dE ) ' => '(de)' return op
python
def oper(inst): i = inst.strip(' \t\n').split(' ') I = i[0].lower() # Instruction i = ''.join(i[1:]) op = i.split(',') if I in {'call', 'jp', 'jr'} and len(op) > 1: op = op[1:] + ['f'] elif I == 'djnz': op.append('b') elif I in {'push', 'pop', 'call'}: op.append('sp') # Sp is also affected by push, pop and call elif I in {'or', 'and', 'xor', 'neg', 'cpl', 'rrca', 'rlca'}: op.append('a') elif I in {'rra', 'rla'}: op.extend(['a', 'f']) elif I in ('rr', 'rl'): op.append('f') elif I in {'adc', 'sbc'}: if len(op) == 1: op = ['a', 'f'] + op elif I in {'add', 'sub'}: if len(op) == 1: op = ['a'] + op elif I in {'ldd', 'ldi', 'lddr', 'ldir'}: op = ['hl', 'de', 'bc'] elif I in {'cpd', 'cpi', 'cpdr', 'cpir'}: op = ['a', 'hl', 'bc'] elif I == 'exx': op = ['*', 'bc', 'de', 'hl', 'b', 'c', 'd', 'e', 'h', 'l'] elif I in {'ret', 'reti', 'retn'}: op += ['sp'] elif I == 'out': if len(op) and RE_OUTC.match(op[0]): op[0] = 'c' else: op.pop(0) elif I == 'in': if len(op) > 1 and RE_OUTC.match(op[1]): op[1] = 'c' else: op.pop(1) for i in range(len(op)): tmp = RE_INDIR16.match(op[i]) if tmp is not None: op[i] = '(' + op[i].strip()[1:-1].strip().lower() + ')' # ' ( dE ) ' => '(de)' return op
[ "def", "oper", "(", "inst", ")", ":", "i", "=", "inst", ".", "strip", "(", "' \\t\\n'", ")", ".", "split", "(", "' '", ")", "I", "=", "i", "[", "0", "]", ".", "lower", "(", ")", "# Instruction", "i", "=", "''", ".", "join", "(", "i", "[", "1", ":", "]", ")", "op", "=", "i", ".", "split", "(", "','", ")", "if", "I", "in", "{", "'call'", ",", "'jp'", ",", "'jr'", "}", "and", "len", "(", "op", ")", ">", "1", ":", "op", "=", "op", "[", "1", ":", "]", "+", "[", "'f'", "]", "elif", "I", "==", "'djnz'", ":", "op", ".", "append", "(", "'b'", ")", "elif", "I", "in", "{", "'push'", ",", "'pop'", ",", "'call'", "}", ":", "op", ".", "append", "(", "'sp'", ")", "# Sp is also affected by push, pop and call", "elif", "I", "in", "{", "'or'", ",", "'and'", ",", "'xor'", ",", "'neg'", ",", "'cpl'", ",", "'rrca'", ",", "'rlca'", "}", ":", "op", ".", "append", "(", "'a'", ")", "elif", "I", "in", "{", "'rra'", ",", "'rla'", "}", ":", "op", ".", "extend", "(", "[", "'a'", ",", "'f'", "]", ")", "elif", "I", "in", "(", "'rr'", ",", "'rl'", ")", ":", "op", ".", "append", "(", "'f'", ")", "elif", "I", "in", "{", "'adc'", ",", "'sbc'", "}", ":", "if", "len", "(", "op", ")", "==", "1", ":", "op", "=", "[", "'a'", ",", "'f'", "]", "+", "op", "elif", "I", "in", "{", "'add'", ",", "'sub'", "}", ":", "if", "len", "(", "op", ")", "==", "1", ":", "op", "=", "[", "'a'", "]", "+", "op", "elif", "I", "in", "{", "'ldd'", ",", "'ldi'", ",", "'lddr'", ",", "'ldir'", "}", ":", "op", "=", "[", "'hl'", ",", "'de'", ",", "'bc'", "]", "elif", "I", "in", "{", "'cpd'", ",", "'cpi'", ",", "'cpdr'", ",", "'cpir'", "}", ":", "op", "=", "[", "'a'", ",", "'hl'", ",", "'bc'", "]", "elif", "I", "==", "'exx'", ":", "op", "=", "[", "'*'", ",", "'bc'", ",", "'de'", ",", "'hl'", ",", "'b'", ",", "'c'", ",", "'d'", ",", "'e'", ",", "'h'", ",", "'l'", "]", "elif", "I", "in", "{", "'ret'", ",", "'reti'", ",", "'retn'", "}", ":", "op", "+=", "[", "'sp'", "]", "elif", "I", "==", "'out'", ":", "if", "len", "(", "op", ")", "and", "RE_OUTC", ".", "match", "(", "op", "[", "0", "]", ")", ":", "op", "[", "0", "]", "=", "'c'", "else", ":", "op", ".", "pop", "(", "0", ")", "elif", "I", "==", "'in'", ":", "if", "len", "(", "op", ")", ">", "1", "and", "RE_OUTC", ".", "match", "(", "op", "[", "1", "]", ")", ":", "op", "[", "1", "]", "=", "'c'", "else", ":", "op", ".", "pop", "(", "1", ")", "for", "i", "in", "range", "(", "len", "(", "op", ")", ")", ":", "tmp", "=", "RE_INDIR16", ".", "match", "(", "op", "[", "i", "]", ")", "if", "tmp", "is", "not", "None", ":", "op", "[", "i", "]", "=", "'('", "+", "op", "[", "i", "]", ".", "strip", "(", ")", "[", "1", ":", "-", "1", "]", ".", "strip", "(", ")", ".", "lower", "(", ")", "+", "')'", "# ' ( dE ) ' => '(de)'", "return", "op" ]
Returns operands of an ASM instruction. Even "indirect" operands, like SP if RET or CALL is used.
[ "Returns", "operands", "of", "an", "ASM", "instruction", ".", "Even", "indirect", "operands", "like", "SP", "if", "RET", "or", "CALL", "is", "used", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L190-L254
3,523
boriel/zxbasic
arch/zx48k/optimizer.py
result
def result(i): """ Returns which 8-bit registers are used by an asm instruction to return a result. """ ins = inst(i) op = oper(i) if ins in ('or', 'and') and op == ['a']: return ['f'] if ins in {'xor', 'or', 'and', 'neg', 'cpl', 'daa', 'rld', 'rrd', 'rra', 'rla', 'rrca', 'rlca'}: return ['a', 'f'] if ins in {'bit', 'cp', 'scf', 'ccf'}: return ['f'] if ins in {'sub', 'add', 'sbc', 'adc'}: if len(op) == 1: return ['a', 'f'] else: return single_registers(op[0]) + ['f'] if ins == 'djnz': return ['b', 'f'] if ins in {'ldir', 'ldi', 'lddr', 'ldd'}: return ['f', 'b', 'c', 'd', 'e', 'h', 'l'] if ins in {'cpi', 'cpir', 'cpd', 'cpdr'}: return ['f', 'b', 'c', 'h', 'l'] if ins in ('pop', 'ld'): return single_registers(op[0]) if ins in {'inc', 'dec', 'sbc', 'rr', 'rl', 'rrc', 'rlc'}: return ['f'] + single_registers(op[0]) if ins in ('set', 'res'): return single_registers(op[1]) return []
python
def result(i): ins = inst(i) op = oper(i) if ins in ('or', 'and') and op == ['a']: return ['f'] if ins in {'xor', 'or', 'and', 'neg', 'cpl', 'daa', 'rld', 'rrd', 'rra', 'rla', 'rrca', 'rlca'}: return ['a', 'f'] if ins in {'bit', 'cp', 'scf', 'ccf'}: return ['f'] if ins in {'sub', 'add', 'sbc', 'adc'}: if len(op) == 1: return ['a', 'f'] else: return single_registers(op[0]) + ['f'] if ins == 'djnz': return ['b', 'f'] if ins in {'ldir', 'ldi', 'lddr', 'ldd'}: return ['f', 'b', 'c', 'd', 'e', 'h', 'l'] if ins in {'cpi', 'cpir', 'cpd', 'cpdr'}: return ['f', 'b', 'c', 'h', 'l'] if ins in ('pop', 'ld'): return single_registers(op[0]) if ins in {'inc', 'dec', 'sbc', 'rr', 'rl', 'rrc', 'rlc'}: return ['f'] + single_registers(op[0]) if ins in ('set', 'res'): return single_registers(op[1]) return []
[ "def", "result", "(", "i", ")", ":", "ins", "=", "inst", "(", "i", ")", "op", "=", "oper", "(", "i", ")", "if", "ins", "in", "(", "'or'", ",", "'and'", ")", "and", "op", "==", "[", "'a'", "]", ":", "return", "[", "'f'", "]", "if", "ins", "in", "{", "'xor'", ",", "'or'", ",", "'and'", ",", "'neg'", ",", "'cpl'", ",", "'daa'", ",", "'rld'", ",", "'rrd'", ",", "'rra'", ",", "'rla'", ",", "'rrca'", ",", "'rlca'", "}", ":", "return", "[", "'a'", ",", "'f'", "]", "if", "ins", "in", "{", "'bit'", ",", "'cp'", ",", "'scf'", ",", "'ccf'", "}", ":", "return", "[", "'f'", "]", "if", "ins", "in", "{", "'sub'", ",", "'add'", ",", "'sbc'", ",", "'adc'", "}", ":", "if", "len", "(", "op", ")", "==", "1", ":", "return", "[", "'a'", ",", "'f'", "]", "else", ":", "return", "single_registers", "(", "op", "[", "0", "]", ")", "+", "[", "'f'", "]", "if", "ins", "==", "'djnz'", ":", "return", "[", "'b'", ",", "'f'", "]", "if", "ins", "in", "{", "'ldir'", ",", "'ldi'", ",", "'lddr'", ",", "'ldd'", "}", ":", "return", "[", "'f'", ",", "'b'", ",", "'c'", ",", "'d'", ",", "'e'", ",", "'h'", ",", "'l'", "]", "if", "ins", "in", "{", "'cpi'", ",", "'cpir'", ",", "'cpd'", ",", "'cpdr'", "}", ":", "return", "[", "'f'", ",", "'b'", ",", "'c'", ",", "'h'", ",", "'l'", "]", "if", "ins", "in", "(", "'pop'", ",", "'ld'", ")", ":", "return", "single_registers", "(", "op", "[", "0", "]", ")", "if", "ins", "in", "{", "'inc'", ",", "'dec'", ",", "'sbc'", ",", "'rr'", ",", "'rl'", ",", "'rrc'", ",", "'rlc'", "}", ":", "return", "[", "'f'", "]", "+", "single_registers", "(", "op", "[", "0", "]", ")", "if", "ins", "in", "(", "'set'", ",", "'res'", ")", ":", "return", "single_registers", "(", "op", "[", "1", "]", ")", "return", "[", "]" ]
Returns which 8-bit registers are used by an asm instruction to return a result.
[ "Returns", "which", "8", "-", "bit", "registers", "are", "used", "by", "an", "asm", "instruction", "to", "return", "a", "result", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L310-L350
3,524
boriel/zxbasic
arch/zx48k/optimizer.py
block_partition
def block_partition(block, i): """ Returns two blocks, as a result of partitioning the given one at i-th instruction. """ i += 1 new_block = BasicBlock(block.asm[i:]) block.mem = block.mem[:i] block.asm = block.asm[:i] block.update_labels() new_block.update_labels() new_block.goes_to = block.goes_to block.goes_to = IdentitySet() new_block.label_goes = block.label_goes block.label_goes = [] new_block.next = new_block.original_next = block.original_next new_block.prev = block new_block.add_comes_from(block) if new_block.next is not None: new_block.next.prev = new_block new_block.next.add_comes_from(new_block) new_block.next.delete_from(block) block.next = block.original_next = new_block block.update_next_block() block.add_goes_to(new_block) return block, new_block
python
def block_partition(block, i): i += 1 new_block = BasicBlock(block.asm[i:]) block.mem = block.mem[:i] block.asm = block.asm[:i] block.update_labels() new_block.update_labels() new_block.goes_to = block.goes_to block.goes_to = IdentitySet() new_block.label_goes = block.label_goes block.label_goes = [] new_block.next = new_block.original_next = block.original_next new_block.prev = block new_block.add_comes_from(block) if new_block.next is not None: new_block.next.prev = new_block new_block.next.add_comes_from(new_block) new_block.next.delete_from(block) block.next = block.original_next = new_block block.update_next_block() block.add_goes_to(new_block) return block, new_block
[ "def", "block_partition", "(", "block", ",", "i", ")", ":", "i", "+=", "1", "new_block", "=", "BasicBlock", "(", "block", ".", "asm", "[", "i", ":", "]", ")", "block", ".", "mem", "=", "block", ".", "mem", "[", ":", "i", "]", "block", ".", "asm", "=", "block", ".", "asm", "[", ":", "i", "]", "block", ".", "update_labels", "(", ")", "new_block", ".", "update_labels", "(", ")", "new_block", ".", "goes_to", "=", "block", ".", "goes_to", "block", ".", "goes_to", "=", "IdentitySet", "(", ")", "new_block", ".", "label_goes", "=", "block", ".", "label_goes", "block", ".", "label_goes", "=", "[", "]", "new_block", ".", "next", "=", "new_block", ".", "original_next", "=", "block", ".", "original_next", "new_block", ".", "prev", "=", "block", "new_block", ".", "add_comes_from", "(", "block", ")", "if", "new_block", ".", "next", "is", "not", "None", ":", "new_block", ".", "next", ".", "prev", "=", "new_block", "new_block", ".", "next", ".", "add_comes_from", "(", "new_block", ")", "new_block", ".", "next", ".", "delete_from", "(", "block", ")", "block", ".", "next", "=", "block", ".", "original_next", "=", "new_block", "block", ".", "update_next_block", "(", ")", "block", ".", "add_goes_to", "(", "new_block", ")", "return", "block", ",", "new_block" ]
Returns two blocks, as a result of partitioning the given one at i-th instruction.
[ "Returns", "two", "blocks", "as", "a", "result", "of", "partitioning", "the", "given", "one", "at", "i", "-", "th", "instruction", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L2060-L2090
3,525
boriel/zxbasic
arch/zx48k/optimizer.py
partition_block
def partition_block(block): """ If a block is not partitionable, returns a list with the same block. Otherwise, returns a list with the resulting blocks, recursively. """ result = [block] if not block.is_partitionable: return result EDP = END_PROGRAM_LABEL + ':' for i in range(len(block) - 1): if i and block.asm[i] == EDP: # END_PROGRAM label always starts a basic block block, new_block = block_partition(block, i - 1) LABELS[END_PROGRAM_LABEL].basic_block = new_block result.extend(partition_block(new_block)) return result if block.mem[i].is_ender: block, new_block = block_partition(block, i) result.extend(partition_block(new_block)) op = block.mem[i].opers for l in op: if l in LABELS.keys(): JUMP_LABELS.add(l) block.label_goes += [l] return result if block.asm[i] in arch.zx48k.backend.ASMS: if i > 0: block, new_block = block_partition(block, i - 1) result.extend(partition_block(new_block)) return result block, new_block = block_partition(block, i) result.extend(partition_block(new_block)) return result for label in JUMP_LABELS: must_partition = False if LABELS[label].basic_block is block: for i in range(len(block)): cell = block.mem[i] if cell.inst == label: break if cell.is_label: continue if cell.is_ender: continue must_partition = True if must_partition: block, new_block = block_partition(block, i - 1) LABELS[label].basic_block = new_block result.extend(partition_block(new_block)) return result return result
python
def partition_block(block): result = [block] if not block.is_partitionable: return result EDP = END_PROGRAM_LABEL + ':' for i in range(len(block) - 1): if i and block.asm[i] == EDP: # END_PROGRAM label always starts a basic block block, new_block = block_partition(block, i - 1) LABELS[END_PROGRAM_LABEL].basic_block = new_block result.extend(partition_block(new_block)) return result if block.mem[i].is_ender: block, new_block = block_partition(block, i) result.extend(partition_block(new_block)) op = block.mem[i].opers for l in op: if l in LABELS.keys(): JUMP_LABELS.add(l) block.label_goes += [l] return result if block.asm[i] in arch.zx48k.backend.ASMS: if i > 0: block, new_block = block_partition(block, i - 1) result.extend(partition_block(new_block)) return result block, new_block = block_partition(block, i) result.extend(partition_block(new_block)) return result for label in JUMP_LABELS: must_partition = False if LABELS[label].basic_block is block: for i in range(len(block)): cell = block.mem[i] if cell.inst == label: break if cell.is_label: continue if cell.is_ender: continue must_partition = True if must_partition: block, new_block = block_partition(block, i - 1) LABELS[label].basic_block = new_block result.extend(partition_block(new_block)) return result return result
[ "def", "partition_block", "(", "block", ")", ":", "result", "=", "[", "block", "]", "if", "not", "block", ".", "is_partitionable", ":", "return", "result", "EDP", "=", "END_PROGRAM_LABEL", "+", "':'", "for", "i", "in", "range", "(", "len", "(", "block", ")", "-", "1", ")", ":", "if", "i", "and", "block", ".", "asm", "[", "i", "]", "==", "EDP", ":", "# END_PROGRAM label always starts a basic block", "block", ",", "new_block", "=", "block_partition", "(", "block", ",", "i", "-", "1", ")", "LABELS", "[", "END_PROGRAM_LABEL", "]", ".", "basic_block", "=", "new_block", "result", ".", "extend", "(", "partition_block", "(", "new_block", ")", ")", "return", "result", "if", "block", ".", "mem", "[", "i", "]", ".", "is_ender", ":", "block", ",", "new_block", "=", "block_partition", "(", "block", ",", "i", ")", "result", ".", "extend", "(", "partition_block", "(", "new_block", ")", ")", "op", "=", "block", ".", "mem", "[", "i", "]", ".", "opers", "for", "l", "in", "op", ":", "if", "l", "in", "LABELS", ".", "keys", "(", ")", ":", "JUMP_LABELS", ".", "add", "(", "l", ")", "block", ".", "label_goes", "+=", "[", "l", "]", "return", "result", "if", "block", ".", "asm", "[", "i", "]", "in", "arch", ".", "zx48k", ".", "backend", ".", "ASMS", ":", "if", "i", ">", "0", ":", "block", ",", "new_block", "=", "block_partition", "(", "block", ",", "i", "-", "1", ")", "result", ".", "extend", "(", "partition_block", "(", "new_block", ")", ")", "return", "result", "block", ",", "new_block", "=", "block_partition", "(", "block", ",", "i", ")", "result", ".", "extend", "(", "partition_block", "(", "new_block", ")", ")", "return", "result", "for", "label", "in", "JUMP_LABELS", ":", "must_partition", "=", "False", "if", "LABELS", "[", "label", "]", ".", "basic_block", "is", "block", ":", "for", "i", "in", "range", "(", "len", "(", "block", ")", ")", ":", "cell", "=", "block", ".", "mem", "[", "i", "]", "if", "cell", ".", "inst", "==", "label", ":", "break", "if", "cell", ".", "is_label", ":", "continue", "if", "cell", ".", "is_ender", ":", "continue", "must_partition", "=", "True", "if", "must_partition", ":", "block", ",", "new_block", "=", "block_partition", "(", "block", ",", "i", "-", "1", ")", "LABELS", "[", "label", "]", ".", "basic_block", "=", "new_block", "result", ".", "extend", "(", "partition_block", "(", "new_block", ")", ")", "return", "result", "return", "result" ]
If a block is not partitionable, returns a list with the same block. Otherwise, returns a list with the resulting blocks, recursively.
[ "If", "a", "block", "is", "not", "partitionable", "returns", "a", "list", "with", "the", "same", "block", ".", "Otherwise", "returns", "a", "list", "with", "the", "resulting", "blocks", "recursively", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L2093-L2154
3,526
boriel/zxbasic
arch/zx48k/optimizer.py
get_labels
def get_labels(MEMORY, basic_block): """ Traverses memory, to annotate all the labels in the global LABELS table """ for cell in MEMORY: if cell.is_label: label = cell.inst LABELS[label] = LabelInfo(label, cell.addr, basic_block)
python
def get_labels(MEMORY, basic_block): for cell in MEMORY: if cell.is_label: label = cell.inst LABELS[label] = LabelInfo(label, cell.addr, basic_block)
[ "def", "get_labels", "(", "MEMORY", ",", "basic_block", ")", ":", "for", "cell", "in", "MEMORY", ":", "if", "cell", ".", "is_label", ":", "label", "=", "cell", ".", "inst", "LABELS", "[", "label", "]", "=", "LabelInfo", "(", "label", ",", "cell", ".", "addr", ",", "basic_block", ")" ]
Traverses memory, to annotate all the labels in the global LABELS table
[ "Traverses", "memory", "to", "annotate", "all", "the", "labels", "in", "the", "global", "LABELS", "table" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L2172-L2179
3,527
boriel/zxbasic
arch/zx48k/optimizer.py
initialize_memory
def initialize_memory(basic_block): """ Initializes global memory array with the given one """ global MEMORY MEMORY = basic_block.mem get_labels(MEMORY, basic_block) basic_block.mem = MEMORY
python
def initialize_memory(basic_block): global MEMORY MEMORY = basic_block.mem get_labels(MEMORY, basic_block) basic_block.mem = MEMORY
[ "def", "initialize_memory", "(", "basic_block", ")", ":", "global", "MEMORY", "MEMORY", "=", "basic_block", ".", "mem", "get_labels", "(", "MEMORY", ",", "basic_block", ")", "basic_block", ".", "mem", "=", "MEMORY" ]
Initializes global memory array with the given one
[ "Initializes", "global", "memory", "array", "with", "the", "given", "one" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L2182-L2189
3,528
boriel/zxbasic
arch/zx48k/optimizer.py
cleanupmem
def cleanupmem(initial_memory): """ Cleans up initial memory. Each label must be ALONE. Each instruction must have an space, etc... """ i = 0 while i < len(initial_memory): tmp = initial_memory[i] match = RE_LABEL.match(tmp) if not match: i += 1 continue if tmp.rstrip() == match.group(): i += 1 continue initial_memory[i] = tmp[match.end():] initial_memory.insert(i, match.group()) i += 1
python
def cleanupmem(initial_memory): i = 0 while i < len(initial_memory): tmp = initial_memory[i] match = RE_LABEL.match(tmp) if not match: i += 1 continue if tmp.rstrip() == match.group(): i += 1 continue initial_memory[i] = tmp[match.end():] initial_memory.insert(i, match.group()) i += 1
[ "def", "cleanupmem", "(", "initial_memory", ")", ":", "i", "=", "0", "while", "i", "<", "len", "(", "initial_memory", ")", ":", "tmp", "=", "initial_memory", "[", "i", "]", "match", "=", "RE_LABEL", ".", "match", "(", "tmp", ")", "if", "not", "match", ":", "i", "+=", "1", "continue", "if", "tmp", ".", "rstrip", "(", ")", "==", "match", ".", "group", "(", ")", ":", "i", "+=", "1", "continue", "initial_memory", "[", "i", "]", "=", "tmp", "[", "match", ".", "end", "(", ")", ":", "]", "initial_memory", ".", "insert", "(", "i", ",", "match", ".", "group", "(", ")", ")", "i", "+=", "1" ]
Cleans up initial memory. Each label must be ALONE. Each instruction must have an space, etc...
[ "Cleans", "up", "initial", "memory", ".", "Each", "label", "must", "be", "ALONE", ".", "Each", "instruction", "must", "have", "an", "space", "etc", "..." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L2223-L2241
3,529
boriel/zxbasic
arch/zx48k/optimizer.py
optimize
def optimize(initial_memory): """ This will remove useless instructions """ global BLOCKS global PROC_COUNTER LABELS.clear() JUMP_LABELS.clear() del MEMORY[:] PROC_COUNTER = 0 cleanupmem(initial_memory) if OPTIONS.optimization.value <= 2: return '\n'.join(x for x in initial_memory if not RE_PRAGMA.match(x)) optimize_init() bb = BasicBlock(initial_memory) cleanup_local_labels(bb) initialize_memory(bb) BLOCKS = basic_blocks = get_basic_blocks(bb) # 1st partition the Basic Blocks for x in basic_blocks: x.clean_up_comes_from() x.clean_up_goes_to() for x in basic_blocks: x.update_goes_and_comes() LABELS['*START*'].basic_block.add_goes_to(basic_blocks[0]) LABELS['*START*'].basic_block.next = basic_blocks[0] basic_blocks[0].prev = LABELS['*START*'].basic_block LABELS[END_PROGRAM_LABEL].basic_block.add_goes_to(LABELS['*__END_PROGRAM*'].basic_block) for x in basic_blocks: x.optimize() for x in basic_blocks: if x.comes_from == [] and len([y for y in JUMP_LABELS if x is LABELS[y].basic_block]): x.ignored = True return '\n'.join([y for y in flatten_list([x.asm for x in basic_blocks if not x.ignored]) if not RE_PRAGMA.match(y)])
python
def optimize(initial_memory): global BLOCKS global PROC_COUNTER LABELS.clear() JUMP_LABELS.clear() del MEMORY[:] PROC_COUNTER = 0 cleanupmem(initial_memory) if OPTIONS.optimization.value <= 2: return '\n'.join(x for x in initial_memory if not RE_PRAGMA.match(x)) optimize_init() bb = BasicBlock(initial_memory) cleanup_local_labels(bb) initialize_memory(bb) BLOCKS = basic_blocks = get_basic_blocks(bb) # 1st partition the Basic Blocks for x in basic_blocks: x.clean_up_comes_from() x.clean_up_goes_to() for x in basic_blocks: x.update_goes_and_comes() LABELS['*START*'].basic_block.add_goes_to(basic_blocks[0]) LABELS['*START*'].basic_block.next = basic_blocks[0] basic_blocks[0].prev = LABELS['*START*'].basic_block LABELS[END_PROGRAM_LABEL].basic_block.add_goes_to(LABELS['*__END_PROGRAM*'].basic_block) for x in basic_blocks: x.optimize() for x in basic_blocks: if x.comes_from == [] and len([y for y in JUMP_LABELS if x is LABELS[y].basic_block]): x.ignored = True return '\n'.join([y for y in flatten_list([x.asm for x in basic_blocks if not x.ignored]) if not RE_PRAGMA.match(y)])
[ "def", "optimize", "(", "initial_memory", ")", ":", "global", "BLOCKS", "global", "PROC_COUNTER", "LABELS", ".", "clear", "(", ")", "JUMP_LABELS", ".", "clear", "(", ")", "del", "MEMORY", "[", ":", "]", "PROC_COUNTER", "=", "0", "cleanupmem", "(", "initial_memory", ")", "if", "OPTIONS", ".", "optimization", ".", "value", "<=", "2", ":", "return", "'\\n'", ".", "join", "(", "x", "for", "x", "in", "initial_memory", "if", "not", "RE_PRAGMA", ".", "match", "(", "x", ")", ")", "optimize_init", "(", ")", "bb", "=", "BasicBlock", "(", "initial_memory", ")", "cleanup_local_labels", "(", "bb", ")", "initialize_memory", "(", "bb", ")", "BLOCKS", "=", "basic_blocks", "=", "get_basic_blocks", "(", "bb", ")", "# 1st partition the Basic Blocks", "for", "x", "in", "basic_blocks", ":", "x", ".", "clean_up_comes_from", "(", ")", "x", ".", "clean_up_goes_to", "(", ")", "for", "x", "in", "basic_blocks", ":", "x", ".", "update_goes_and_comes", "(", ")", "LABELS", "[", "'*START*'", "]", ".", "basic_block", ".", "add_goes_to", "(", "basic_blocks", "[", "0", "]", ")", "LABELS", "[", "'*START*'", "]", ".", "basic_block", ".", "next", "=", "basic_blocks", "[", "0", "]", "basic_blocks", "[", "0", "]", ".", "prev", "=", "LABELS", "[", "'*START*'", "]", ".", "basic_block", "LABELS", "[", "END_PROGRAM_LABEL", "]", ".", "basic_block", ".", "add_goes_to", "(", "LABELS", "[", "'*__END_PROGRAM*'", "]", ".", "basic_block", ")", "for", "x", "in", "basic_blocks", ":", "x", ".", "optimize", "(", ")", "for", "x", "in", "basic_blocks", ":", "if", "x", ".", "comes_from", "==", "[", "]", "and", "len", "(", "[", "y", "for", "y", "in", "JUMP_LABELS", "if", "x", "is", "LABELS", "[", "y", "]", ".", "basic_block", "]", ")", ":", "x", ".", "ignored", "=", "True", "return", "'\\n'", ".", "join", "(", "[", "y", "for", "y", "in", "flatten_list", "(", "[", "x", ".", "asm", "for", "x", "in", "basic_blocks", "if", "not", "x", ".", "ignored", "]", ")", "if", "not", "RE_PRAGMA", ".", "match", "(", "y", ")", "]", ")" ]
This will remove useless instructions
[ "This", "will", "remove", "useless", "instructions" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L2345-L2388
3,530
boriel/zxbasic
arch/zx48k/optimizer.py
Registers.reset_flags
def reset_flags(self): """ Resets flags to an "unknown state" """ self.C = None self.Z = None self.P = None self.S = None
python
def reset_flags(self): self.C = None self.Z = None self.P = None self.S = None
[ "def", "reset_flags", "(", "self", ")", ":", "self", ".", "C", "=", "None", "self", ".", "Z", "=", "None", "self", ".", "P", "=", "None", "self", ".", "S", "=", "None" ]
Resets flags to an "unknown state"
[ "Resets", "flags", "to", "an", "unknown", "state" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L412-L418
3,531
boriel/zxbasic
arch/zx48k/optimizer.py
Registers.get
def get(self, r): """ Returns precomputed value of the given expression """ if r is None: return None if r.lower() == '(sp)' and self.stack: return self.stack[-1] if r[:1] == '(': return self.mem[r[1:-1]] r = r.lower() if is_number(r): return str(valnum(r)) if not is_register(r): return None return self.regs[r]
python
def get(self, r): if r is None: return None if r.lower() == '(sp)' and self.stack: return self.stack[-1] if r[:1] == '(': return self.mem[r[1:-1]] r = r.lower() if is_number(r): return str(valnum(r)) if not is_register(r): return None return self.regs[r]
[ "def", "get", "(", "self", ",", "r", ")", ":", "if", "r", "is", "None", ":", "return", "None", "if", "r", ".", "lower", "(", ")", "==", "'(sp)'", "and", "self", ".", "stack", ":", "return", "self", ".", "stack", "[", "-", "1", "]", "if", "r", "[", ":", "1", "]", "==", "'('", ":", "return", "self", ".", "mem", "[", "r", "[", "1", ":", "-", "1", "]", "]", "r", "=", "r", ".", "lower", "(", ")", "if", "is_number", "(", "r", ")", ":", "return", "str", "(", "valnum", "(", "r", ")", ")", "if", "not", "is_register", "(", "r", ")", ":", "return", "None", "return", "self", ".", "regs", "[", "r", "]" ]
Returns precomputed value of the given expression
[ "Returns", "precomputed", "value", "of", "the", "given", "expression" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L503-L522
3,532
boriel/zxbasic
arch/zx48k/optimizer.py
Registers.eq
def eq(self, r1, r2): """ True if values of r1 and r2 registers are equal """ if not is_register(r1) or not is_register(r2): return False if self.regs[r1] is None or self.regs[r2] is None: # HINT: This's been never USED?? return False return self.regs[r1] == self.regs[r2]
python
def eq(self, r1, r2): if not is_register(r1) or not is_register(r2): return False if self.regs[r1] is None or self.regs[r2] is None: # HINT: This's been never USED?? return False return self.regs[r1] == self.regs[r2]
[ "def", "eq", "(", "self", ",", "r1", ",", "r2", ")", ":", "if", "not", "is_register", "(", "r1", ")", "or", "not", "is_register", "(", "r2", ")", ":", "return", "False", "if", "self", ".", "regs", "[", "r1", "]", "is", "None", "or", "self", ".", "regs", "[", "r2", "]", "is", "None", ":", "# HINT: This's been never USED??", "return", "False", "return", "self", ".", "regs", "[", "r1", "]", "==", "self", ".", "regs", "[", "r2", "]" ]
True if values of r1 and r2 registers are equal
[ "True", "if", "values", "of", "r1", "and", "r2", "registers", "are", "equal" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L537-L546
3,533
boriel/zxbasic
arch/zx48k/optimizer.py
Registers.inc
def inc(self, r): """ Does inc on the register and precomputes flags """ self.set_flag(None) if not is_register(r): if r[0] == '(': # a memory position, basically: inc(hl) r_ = r[1:-1].strip() v_ = self.getv(self.mem.get(r_, None)) if v_ is not None: v_ = (v_ + 1) & 0xFF self.mem[r_] = str(v_) self.Z = int(v_ == 0) # HINT: This might be improved else: self.mem[r_] = new_tmp_val() return if self.getv(r) is not None: self.set(r, self.getv(r) + 1) else: self.set(r, None)
python
def inc(self, r): self.set_flag(None) if not is_register(r): if r[0] == '(': # a memory position, basically: inc(hl) r_ = r[1:-1].strip() v_ = self.getv(self.mem.get(r_, None)) if v_ is not None: v_ = (v_ + 1) & 0xFF self.mem[r_] = str(v_) self.Z = int(v_ == 0) # HINT: This might be improved else: self.mem[r_] = new_tmp_val() return if self.getv(r) is not None: self.set(r, self.getv(r) + 1) else: self.set(r, None)
[ "def", "inc", "(", "self", ",", "r", ")", ":", "self", ".", "set_flag", "(", "None", ")", "if", "not", "is_register", "(", "r", ")", ":", "if", "r", "[", "0", "]", "==", "'('", ":", "# a memory position, basically: inc(hl)", "r_", "=", "r", "[", "1", ":", "-", "1", "]", ".", "strip", "(", ")", "v_", "=", "self", ".", "getv", "(", "self", ".", "mem", ".", "get", "(", "r_", ",", "None", ")", ")", "if", "v_", "is", "not", "None", ":", "v_", "=", "(", "v_", "+", "1", ")", "&", "0xFF", "self", ".", "mem", "[", "r_", "]", "=", "str", "(", "v_", ")", "self", ".", "Z", "=", "int", "(", "v_", "==", "0", ")", "# HINT: This might be improved", "else", ":", "self", ".", "mem", "[", "r_", "]", "=", "new_tmp_val", "(", ")", "return", "if", "self", ".", "getv", "(", "r", ")", "is", "not", "None", ":", "self", ".", "set", "(", "r", ",", "self", ".", "getv", "(", "r", ")", "+", "1", ")", "else", ":", "self", ".", "set", "(", "r", ",", "None", ")" ]
Does inc on the register and precomputes flags
[ "Does", "inc", "on", "the", "register", "and", "precomputes", "flags" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L561-L581
3,534
boriel/zxbasic
arch/zx48k/optimizer.py
Registers.rrc
def rrc(self, r): """ Does a ROTATION to the RIGHT |>> """ if not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return v_ = self.getv(self.regs[r]) & 0xFF self.regs[r] = str((v_ >> 1) | ((v_ & 1) << 7))
python
def rrc(self, r): if not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return v_ = self.getv(self.regs[r]) & 0xFF self.regs[r] = str((v_ >> 1) | ((v_ & 1) << 7))
[ "def", "rrc", "(", "self", ",", "r", ")", ":", "if", "not", "is_number", "(", "self", ".", "regs", "[", "r", "]", ")", ":", "self", ".", "set", "(", "r", ",", "None", ")", "self", ".", "set_flag", "(", "None", ")", "return", "v_", "=", "self", ".", "getv", "(", "self", ".", "regs", "[", "r", "]", ")", "&", "0xFF", "self", ".", "regs", "[", "r", "]", "=", "str", "(", "(", "v_", ">>", "1", ")", "|", "(", "(", "v_", "&", "1", ")", "<<", "7", ")", ")" ]
Does a ROTATION to the RIGHT |>>
[ "Does", "a", "ROTATION", "to", "the", "RIGHT", "|", ">>" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L605-L614
3,535
boriel/zxbasic
arch/zx48k/optimizer.py
Registers.rlc
def rlc(self, r): """ Does a ROTATION to the LEFT <<| """ if not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return v_ = self.getv(self.regs[r]) & 0xFF self.set(r, ((v_ << 1) & 0xFF) | (v_ >> 7))
python
def rlc(self, r): if not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return v_ = self.getv(self.regs[r]) & 0xFF self.set(r, ((v_ << 1) & 0xFF) | (v_ >> 7))
[ "def", "rlc", "(", "self", ",", "r", ")", ":", "if", "not", "is_number", "(", "self", ".", "regs", "[", "r", "]", ")", ":", "self", ".", "set", "(", "r", ",", "None", ")", "self", ".", "set_flag", "(", "None", ")", "return", "v_", "=", "self", ".", "getv", "(", "self", ".", "regs", "[", "r", "]", ")", "&", "0xFF", "self", ".", "set", "(", "r", ",", "(", "(", "v_", "<<", "1", ")", "&", "0xFF", ")", "|", "(", "v_", ">>", "7", ")", ")" ]
Does a ROTATION to the LEFT <<|
[ "Does", "a", "ROTATION", "to", "the", "LEFT", "<<|" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L630-L639
3,536
boriel/zxbasic
arch/zx48k/optimizer.py
Registers._is
def _is(self, r, val): """ True if value of r is val. """ if not is_register(r) or val is None: return False r = r.lower() if is_register(val): return self.eq(r, val) if is_number(val): val = str(valnum(val)) else: val = str(val) if val[0] == '(': val = self.mem[val[1:-1]] return self.regs[r] == val
python
def _is(self, r, val): if not is_register(r) or val is None: return False r = r.lower() if is_register(val): return self.eq(r, val) if is_number(val): val = str(valnum(val)) else: val = str(val) if val[0] == '(': val = self.mem[val[1:-1]] return self.regs[r] == val
[ "def", "_is", "(", "self", ",", "r", ",", "val", ")", ":", "if", "not", "is_register", "(", "r", ")", "or", "val", "is", "None", ":", "return", "False", "r", "=", "r", ".", "lower", "(", ")", "if", "is_register", "(", "val", ")", ":", "return", "self", ".", "eq", "(", "r", ",", "val", ")", "if", "is_number", "(", "val", ")", ":", "val", "=", "str", "(", "valnum", "(", "val", ")", ")", "else", ":", "val", "=", "str", "(", "val", ")", "if", "val", "[", "0", "]", "==", "'('", ":", "val", "=", "self", ".", "mem", "[", "val", "[", "1", ":", "-", "1", "]", "]", "return", "self", ".", "regs", "[", "r", "]", "==", "val" ]
True if value of r is val.
[ "True", "if", "value", "of", "r", "is", "val", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L655-L673
3,537
boriel/zxbasic
arch/zx48k/optimizer.py
MemCell.opers
def opers(self): """ Returns a list of operators this mnemonic uses """ i = [x for x in self.asm.strip(' \t\n').split(' ') if x != ''] if len(i) == 1: return [] i = ''.join(i[1:]).split(',') if self.condition_flag is not None: i = i[1:] else: i = i[0:] op = [x.lower() if is_register(x) else x for x in i] return op
python
def opers(self): i = [x for x in self.asm.strip(' \t\n').split(' ') if x != ''] if len(i) == 1: return [] i = ''.join(i[1:]).split(',') if self.condition_flag is not None: i = i[1:] else: i = i[0:] op = [x.lower() if is_register(x) else x for x in i] return op
[ "def", "opers", "(", "self", ")", ":", "i", "=", "[", "x", "for", "x", "in", "self", ".", "asm", ".", "strip", "(", "' \\t\\n'", ")", ".", "split", "(", "' '", ")", "if", "x", "!=", "''", "]", "if", "len", "(", "i", ")", "==", "1", ":", "return", "[", "]", "i", "=", "''", ".", "join", "(", "i", "[", "1", ":", "]", ")", ".", "split", "(", "','", ")", "if", "self", ".", "condition_flag", "is", "not", "None", ":", "i", "=", "i", "[", "1", ":", "]", "else", ":", "i", "=", "i", "[", "0", ":", "]", "op", "=", "[", "x", ".", "lower", "(", ")", "if", "is_register", "(", "x", ")", "else", "x", "for", "x", "in", "i", "]", "return", "op" ]
Returns a list of operators this mnemonic uses
[ "Returns", "a", "list", "of", "operators", "this", "mnemonic", "uses" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L948-L963
3,538
boriel/zxbasic
arch/zx48k/optimizer.py
MemCell.affects
def affects(self, reglist): """ Returns if this instruction affects any of the registers in reglist. """ if isinstance(reglist, str): reglist = [reglist] reglist = single_registers(reglist) return len([x for x in self.destroys if x in reglist]) > 0
python
def affects(self, reglist): if isinstance(reglist, str): reglist = [reglist] reglist = single_registers(reglist) return len([x for x in self.destroys if x in reglist]) > 0
[ "def", "affects", "(", "self", ",", "reglist", ")", ":", "if", "isinstance", "(", "reglist", ",", "str", ")", ":", "reglist", "=", "[", "reglist", "]", "reglist", "=", "single_registers", "(", "reglist", ")", "return", "len", "(", "[", "x", "for", "x", "in", "self", ".", "destroys", "if", "x", "in", "reglist", "]", ")", ">", "0" ]
Returns if this instruction affects any of the registers in reglist.
[ "Returns", "if", "this", "instruction", "affects", "any", "of", "the", "registers", "in", "reglist", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1170-L1179
3,539
boriel/zxbasic
arch/zx48k/optimizer.py
MemCell.needs
def needs(self, reglist): """ Returns if this instruction need any of the registers in reglist. """ if isinstance(reglist, str): reglist = [reglist] reglist = single_registers(reglist) return len([x for x in self.requires if x in reglist]) > 0
python
def needs(self, reglist): if isinstance(reglist, str): reglist = [reglist] reglist = single_registers(reglist) return len([x for x in self.requires if x in reglist]) > 0
[ "def", "needs", "(", "self", ",", "reglist", ")", ":", "if", "isinstance", "(", "reglist", ",", "str", ")", ":", "reglist", "=", "[", "reglist", "]", "reglist", "=", "single_registers", "(", "reglist", ")", "return", "len", "(", "[", "x", "for", "x", "in", "self", ".", "requires", "if", "x", "in", "reglist", "]", ")", ">", "0" ]
Returns if this instruction need any of the registers in reglist.
[ "Returns", "if", "this", "instruction", "need", "any", "of", "the", "registers", "in", "reglist", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1181-L1190
3,540
boriel/zxbasic
arch/zx48k/optimizer.py
MemCell.used_labels
def used_labels(self): """ Returns a list of required labels for this instruction """ result = [] tmp = self.asm.strip(' \n\r\t') if not len(tmp) or tmp[0] in ('#', ';'): return result try: tmpLexer = asmlex.lex.lex(object=asmlex.Lexer(), lextab='zxbasmlextab') tmpLexer.input(tmp) while True: token = tmpLexer.token() if not token: break if token.type == 'ID': result += [token.value] except: pass return result
python
def used_labels(self): result = [] tmp = self.asm.strip(' \n\r\t') if not len(tmp) or tmp[0] in ('#', ';'): return result try: tmpLexer = asmlex.lex.lex(object=asmlex.Lexer(), lextab='zxbasmlextab') tmpLexer.input(tmp) while True: token = tmpLexer.token() if not token: break if token.type == 'ID': result += [token.value] except: pass return result
[ "def", "used_labels", "(", "self", ")", ":", "result", "=", "[", "]", "tmp", "=", "self", ".", "asm", ".", "strip", "(", "' \\n\\r\\t'", ")", "if", "not", "len", "(", "tmp", ")", "or", "tmp", "[", "0", "]", "in", "(", "'#'", ",", "';'", ")", ":", "return", "result", "try", ":", "tmpLexer", "=", "asmlex", ".", "lex", ".", "lex", "(", "object", "=", "asmlex", ".", "Lexer", "(", ")", ",", "lextab", "=", "'zxbasmlextab'", ")", "tmpLexer", ".", "input", "(", "tmp", ")", "while", "True", ":", "token", "=", "tmpLexer", ".", "token", "(", ")", "if", "not", "token", ":", "break", "if", "token", ".", "type", "==", "'ID'", ":", "result", "+=", "[", "token", ".", "value", "]", "except", ":", "pass", "return", "result" ]
Returns a list of required labels for this instruction
[ "Returns", "a", "list", "of", "required", "labels", "for", "this", "instruction" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1193-L1216
3,541
boriel/zxbasic
arch/zx48k/optimizer.py
MemCell.replace_label
def replace_label(self, oldLabel, newLabel): """ Replaces old label with a new one """ if oldLabel == newLabel: return tmp = re.compile(r'\b' + oldLabel + r'\b') last = 0 l = len(newLabel) while True: match = tmp.search(self.asm[last:]) if not match: break txt = self.asm self.asm = txt[:last + match.start()] + newLabel + txt[last + match.end():] last += match.start() + l
python
def replace_label(self, oldLabel, newLabel): if oldLabel == newLabel: return tmp = re.compile(r'\b' + oldLabel + r'\b') last = 0 l = len(newLabel) while True: match = tmp.search(self.asm[last:]) if not match: break txt = self.asm self.asm = txt[:last + match.start()] + newLabel + txt[last + match.end():] last += match.start() + l
[ "def", "replace_label", "(", "self", ",", "oldLabel", ",", "newLabel", ")", ":", "if", "oldLabel", "==", "newLabel", ":", "return", "tmp", "=", "re", ".", "compile", "(", "r'\\b'", "+", "oldLabel", "+", "r'\\b'", ")", "last", "=", "0", "l", "=", "len", "(", "newLabel", ")", "while", "True", ":", "match", "=", "tmp", ".", "search", "(", "self", ".", "asm", "[", "last", ":", "]", ")", "if", "not", "match", ":", "break", "txt", "=", "self", ".", "asm", "self", ".", "asm", "=", "txt", "[", ":", "last", "+", "match", ".", "start", "(", ")", "]", "+", "newLabel", "+", "txt", "[", "last", "+", "match", ".", "end", "(", ")", ":", "]", "last", "+=", "match", ".", "start", "(", ")", "+", "l" ]
Replaces old label with a new one
[ "Replaces", "old", "label", "with", "a", "new", "one" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1218-L1234
3,542
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.is_partitionable
def is_partitionable(self): """ Returns if this block can be partitiones in 2 or more blocks, because if contains enders. """ if len(self.mem) < 2: return False # An atomic block if any(x.is_ender or x.asm in arch.zx48k.backend.ASMS for x in self.mem): return True for label in JUMP_LABELS: if LABELS[label].basic_block == self and (not self.mem[0].is_label or self.mem[0].inst != label): return True return False
python
def is_partitionable(self): if len(self.mem) < 2: return False # An atomic block if any(x.is_ender or x.asm in arch.zx48k.backend.ASMS for x in self.mem): return True for label in JUMP_LABELS: if LABELS[label].basic_block == self and (not self.mem[0].is_label or self.mem[0].inst != label): return True return False
[ "def", "is_partitionable", "(", "self", ")", ":", "if", "len", "(", "self", ".", "mem", ")", "<", "2", ":", "return", "False", "# An atomic block", "if", "any", "(", "x", ".", "is_ender", "or", "x", ".", "asm", "in", "arch", ".", "zx48k", ".", "backend", ".", "ASMS", "for", "x", "in", "self", ".", "mem", ")", ":", "return", "True", "for", "label", "in", "JUMP_LABELS", ":", "if", "LABELS", "[", "label", "]", ".", "basic_block", "==", "self", "and", "(", "not", "self", ".", "mem", "[", "0", "]", ".", "is_label", "or", "self", ".", "mem", "[", "0", "]", ".", "inst", "!=", "label", ")", ":", "return", "True", "return", "False" ]
Returns if this block can be partitiones in 2 or more blocks, because if contains enders.
[ "Returns", "if", "this", "block", "can", "be", "partitiones", "in", "2", "or", "more", "blocks", "because", "if", "contains", "enders", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1305-L1319
3,543
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.delete_from
def delete_from(self, basic_block): """ Removes the basic_block ptr from the list for "comes_from" if it exists. It also sets self.prev to None if it is basic_block. """ if basic_block is None: return if self.lock: return self.lock = True if self.prev is basic_block: if self.prev.next is self: self.prev.next = None self.prev = None for i in range(len(self.comes_from)): if self.comes_from[i] is basic_block: self.comes_from.pop(i) break self.lock = False
python
def delete_from(self, basic_block): if basic_block is None: return if self.lock: return self.lock = True if self.prev is basic_block: if self.prev.next is self: self.prev.next = None self.prev = None for i in range(len(self.comes_from)): if self.comes_from[i] is basic_block: self.comes_from.pop(i) break self.lock = False
[ "def", "delete_from", "(", "self", ",", "basic_block", ")", ":", "if", "basic_block", "is", "None", ":", "return", "if", "self", ".", "lock", ":", "return", "self", ".", "lock", "=", "True", "if", "self", ".", "prev", "is", "basic_block", ":", "if", "self", ".", "prev", ".", "next", "is", "self", ":", "self", ".", "prev", ".", "next", "=", "None", "self", ".", "prev", "=", "None", "for", "i", "in", "range", "(", "len", "(", "self", ".", "comes_from", ")", ")", ":", "if", "self", ".", "comes_from", "[", "i", "]", "is", "basic_block", ":", "self", ".", "comes_from", ".", "pop", "(", "i", ")", "break", "self", ".", "lock", "=", "False" ]
Removes the basic_block ptr from the list for "comes_from" if it exists. It also sets self.prev to None if it is basic_block.
[ "Removes", "the", "basic_block", "ptr", "from", "the", "list", "for", "comes_from", "if", "it", "exists", ".", "It", "also", "sets", "self", ".", "prev", "to", "None", "if", "it", "is", "basic_block", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1327-L1349
3,544
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.add_comes_from
def add_comes_from(self, basic_block): """ This simulates a set. Adds the basic_block to the comes_from list if not done already. """ if basic_block is None: return if self.lock: return # Return if already added if basic_block in self.comes_from: return self.lock = True self.comes_from.add(basic_block) basic_block.add_goes_to(self) self.lock = False
python
def add_comes_from(self, basic_block): if basic_block is None: return if self.lock: return # Return if already added if basic_block in self.comes_from: return self.lock = True self.comes_from.add(basic_block) basic_block.add_goes_to(self) self.lock = False
[ "def", "add_comes_from", "(", "self", ",", "basic_block", ")", ":", "if", "basic_block", "is", "None", ":", "return", "if", "self", ".", "lock", ":", "return", "# Return if already added", "if", "basic_block", "in", "self", ".", "comes_from", ":", "return", "self", ".", "lock", "=", "True", "self", ".", "comes_from", ".", "add", "(", "basic_block", ")", "basic_block", ".", "add_goes_to", "(", "self", ")", "self", ".", "lock", "=", "False" ]
This simulates a set. Adds the basic_block to the comes_from list if not done already.
[ "This", "simulates", "a", "set", ".", "Adds", "the", "basic_block", "to", "the", "comes_from", "list", "if", "not", "done", "already", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1376-L1393
3,545
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.is_used
def is_used(self, regs, i, top=None): """ Checks whether any of the given regs are required from the given point to the end or not. """ if i < 0: i = 0 if self.lock: return True regs = list(regs) # make a copy if top is None: top = len(self) else: top -= 1 for ii in range(i, top): for r in self.mem[ii].requires: if r in regs: return True for r in self.mem[ii].destroys: if r in regs: regs.remove(r) if not regs: return False self.lock = True result = self.goes_requires(regs) self.lock = False return result
python
def is_used(self, regs, i, top=None): if i < 0: i = 0 if self.lock: return True regs = list(regs) # make a copy if top is None: top = len(self) else: top -= 1 for ii in range(i, top): for r in self.mem[ii].requires: if r in regs: return True for r in self.mem[ii].destroys: if r in regs: regs.remove(r) if not regs: return False self.lock = True result = self.goes_requires(regs) self.lock = False return result
[ "def", "is_used", "(", "self", ",", "regs", ",", "i", ",", "top", "=", "None", ")", ":", "if", "i", "<", "0", ":", "i", "=", "0", "if", "self", ".", "lock", ":", "return", "True", "regs", "=", "list", "(", "regs", ")", "# make a copy", "if", "top", "is", "None", ":", "top", "=", "len", "(", "self", ")", "else", ":", "top", "-=", "1", "for", "ii", "in", "range", "(", "i", ",", "top", ")", ":", "for", "r", "in", "self", ".", "mem", "[", "ii", "]", ".", "requires", ":", "if", "r", "in", "regs", ":", "return", "True", "for", "r", "in", "self", ".", "mem", "[", "ii", "]", ".", "destroys", ":", "if", "r", "in", "regs", ":", "regs", ".", "remove", "(", "r", ")", "if", "not", "regs", ":", "return", "False", "self", ".", "lock", "=", "True", "result", "=", "self", ".", "goes_requires", "(", "regs", ")", "self", ".", "lock", "=", "False", "return", "result" ]
Checks whether any of the given regs are required from the given point to the end or not.
[ "Checks", "whether", "any", "of", "the", "given", "regs", "are", "required", "from", "the", "given", "point", "to", "the", "end", "or", "not", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1531-L1563
3,546
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.swap
def swap(self, a, b): """ Swaps mem positions a and b """ self.mem[a], self.mem[b] = self.mem[b], self.mem[a] self.asm[a], self.asm[b] = self.asm[b], self.asm[a]
python
def swap(self, a, b): self.mem[a], self.mem[b] = self.mem[b], self.mem[a] self.asm[a], self.asm[b] = self.asm[b], self.asm[a]
[ "def", "swap", "(", "self", ",", "a", ",", "b", ")", ":", "self", ".", "mem", "[", "a", "]", ",", "self", ".", "mem", "[", "b", "]", "=", "self", ".", "mem", "[", "b", "]", ",", "self", ".", "mem", "[", "a", "]", "self", ".", "asm", "[", "a", "]", ",", "self", ".", "asm", "[", "b", "]", "=", "self", ".", "asm", "[", "b", "]", ",", "self", ".", "asm", "[", "a", "]" ]
Swaps mem positions a and b
[ "Swaps", "mem", "positions", "a", "and", "b" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1629-L1633
3,547
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.goes_requires
def goes_requires(self, regs): """ Returns whether any of the goes_to block requires any of the given registers. """ if len(self) and self.mem[-1].inst == 'call' and self.mem[-1].condition_flag is None: for block in self.calls: if block.is_used(regs, 0): return True d = block.destroys() if not len([x for x in regs if x not in d]): return False # If all registers are destroyed then they're not used for block in self.goes_to: if block.is_used(regs, 0): return True return False
python
def goes_requires(self, regs): if len(self) and self.mem[-1].inst == 'call' and self.mem[-1].condition_flag is None: for block in self.calls: if block.is_used(regs, 0): return True d = block.destroys() if not len([x for x in regs if x not in d]): return False # If all registers are destroyed then they're not used for block in self.goes_to: if block.is_used(regs, 0): return True return False
[ "def", "goes_requires", "(", "self", ",", "regs", ")", ":", "if", "len", "(", "self", ")", "and", "self", ".", "mem", "[", "-", "1", "]", ".", "inst", "==", "'call'", "and", "self", ".", "mem", "[", "-", "1", "]", ".", "condition_flag", "is", "None", ":", "for", "block", "in", "self", ".", "calls", ":", "if", "block", ".", "is_used", "(", "regs", ",", "0", ")", ":", "return", "True", "d", "=", "block", ".", "destroys", "(", ")", "if", "not", "len", "(", "[", "x", "for", "x", "in", "regs", "if", "x", "not", "in", "d", "]", ")", ":", "return", "False", "# If all registers are destroyed then they're not used", "for", "block", "in", "self", ".", "goes_to", ":", "if", "block", ".", "is_used", "(", "regs", ",", "0", ")", ":", "return", "True", "return", "False" ]
Returns whether any of the goes_to block requires any of the given registers.
[ "Returns", "whether", "any", "of", "the", "goes_to", "block", "requires", "any", "of", "the", "given", "registers", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1635-L1652
3,548
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.get_label_idx
def get_label_idx(self, label): """ Returns the index of a label. Returns None if not found. """ for i in range(len(self)): if self.mem[i].is_label and self.mem[i].inst == label: return i return None
python
def get_label_idx(self, label): for i in range(len(self)): if self.mem[i].is_label and self.mem[i].inst == label: return i return None
[ "def", "get_label_idx", "(", "self", ",", "label", ")", ":", "for", "i", "in", "range", "(", "len", "(", "self", ")", ")", ":", "if", "self", ".", "mem", "[", "i", "]", ".", "is_label", "and", "self", ".", "mem", "[", "i", "]", ".", "inst", "==", "label", ":", "return", "i", "return", "None" ]
Returns the index of a label. Returns None if not found.
[ "Returns", "the", "index", "of", "a", "label", ".", "Returns", "None", "if", "not", "found", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1654-L1662
3,549
boriel/zxbasic
arch/zx48k/optimizer.py
BasicBlock.get_first_non_label_instruction
def get_first_non_label_instruction(self): """ Returns the memcell of the given block, which is not a LABEL. """ for i in range(len(self)): if not self.mem[i].is_label: return self.mem[i] return None
python
def get_first_non_label_instruction(self): for i in range(len(self)): if not self.mem[i].is_label: return self.mem[i] return None
[ "def", "get_first_non_label_instruction", "(", "self", ")", ":", "for", "i", "in", "range", "(", "len", "(", "self", ")", ")", ":", "if", "not", "self", ".", "mem", "[", "i", "]", ".", "is_label", ":", "return", "self", ".", "mem", "[", "i", "]", "return", "None" ]
Returns the memcell of the given block, which is not a LABEL.
[ "Returns", "the", "memcell", "of", "the", "given", "block", "which", "is", "not", "a", "LABEL", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/optimizer.py#L1664-L1672
3,550
boriel/zxbasic
arch/zx48k/backend/__str.py
_free_sequence
def _free_sequence(tmp1, tmp2=False): ''' Outputs a FREEMEM sequence for 1 or 2 ops ''' if not tmp1 and not tmp2: return [] output = [] if tmp1 and tmp2: output.append('pop de') output.append('ex (sp), hl') output.append('push de') output.append('call __MEM_FREE') output.append('pop hl') output.append('call __MEM_FREE') else: output.append('ex (sp), hl') output.append('call __MEM_FREE') output.append('pop hl') REQUIRES.add('alloc.asm') return output
python
def _free_sequence(tmp1, tmp2=False): ''' Outputs a FREEMEM sequence for 1 or 2 ops ''' if not tmp1 and not tmp2: return [] output = [] if tmp1 and tmp2: output.append('pop de') output.append('ex (sp), hl') output.append('push de') output.append('call __MEM_FREE') output.append('pop hl') output.append('call __MEM_FREE') else: output.append('ex (sp), hl') output.append('call __MEM_FREE') output.append('pop hl') REQUIRES.add('alloc.asm') return output
[ "def", "_free_sequence", "(", "tmp1", ",", "tmp2", "=", "False", ")", ":", "if", "not", "tmp1", "and", "not", "tmp2", ":", "return", "[", "]", "output", "=", "[", "]", "if", "tmp1", "and", "tmp2", ":", "output", ".", "append", "(", "'pop de'", ")", "output", ".", "append", "(", "'ex (sp), hl'", ")", "output", ".", "append", "(", "'push de'", ")", "output", ".", "append", "(", "'call __MEM_FREE'", ")", "output", ".", "append", "(", "'pop hl'", ")", "output", ".", "append", "(", "'call __MEM_FREE'", ")", "else", ":", "output", ".", "append", "(", "'ex (sp), hl'", ")", "output", ".", "append", "(", "'call __MEM_FREE'", ")", "output", ".", "append", "(", "'pop hl'", ")", "REQUIRES", ".", "add", "(", "'alloc.asm'", ")", "return", "output" ]
Outputs a FREEMEM sequence for 1 or 2 ops
[ "Outputs", "a", "FREEMEM", "sequence", "for", "1", "or", "2", "ops" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__str.py#L102-L122
3,551
boriel/zxbasic
arch/zx48k/backend/__str.py
_lenstr
def _lenstr(ins): ''' Returns string length ''' (tmp1, output) = _str_oper(ins.quad[2], no_exaf=True) if tmp1: output.append('push hl') output.append('call __STRLEN') output.extend(_free_sequence(tmp1)) output.append('push hl') REQUIRES.add('strlen.asm') return output
python
def _lenstr(ins): ''' Returns string length ''' (tmp1, output) = _str_oper(ins.quad[2], no_exaf=True) if tmp1: output.append('push hl') output.append('call __STRLEN') output.extend(_free_sequence(tmp1)) output.append('push hl') REQUIRES.add('strlen.asm') return output
[ "def", "_lenstr", "(", "ins", ")", ":", "(", "tmp1", ",", "output", ")", "=", "_str_oper", "(", "ins", ".", "quad", "[", "2", "]", ",", "no_exaf", "=", "True", ")", "if", "tmp1", ":", "output", ".", "append", "(", "'push hl'", ")", "output", ".", "append", "(", "'call __STRLEN'", ")", "output", ".", "extend", "(", "_free_sequence", "(", "tmp1", ")", ")", "output", ".", "append", "(", "'push hl'", ")", "REQUIRES", ".", "add", "(", "'strlen.asm'", ")", "return", "output" ]
Returns string length
[ "Returns", "string", "length" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__str.py#L210-L221
3,552
boriel/zxbasic
arch/zx48k/backend/__8bit.py
_mul8
def _mul8(ins): """ Multiplies 2 las values from the stack. Optimizations: * If any of the ops is ZERO, then do A = 0 ==> XOR A, cause A * 0 = 0 * A = 0 * If any ot the ops is ONE, do NOTHING A * 1 = 1 * A = A """ op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: op1, op2 = _int_ops(op1, op2) output = _8bit_oper(op1) if op2 == 1: # A * 1 = 1 * A = A output.append('push af') return output if op2 == 0: output.append('xor a') output.append('push af') return output if op2 == 2: # A * 2 == A SLA 1 output.append('add a, a') output.append('push af') return output if op2 == 4: # A * 4 == A SLA 2 output.append('add a, a') output.append('add a, a') output.append('push af') return output output.append('ld h, %i' % int8(op2)) else: if op2[0] == '_': # stack optimization op1, op2 = op2, op1 output = _8bit_oper(op1, op2) output.append('call __MUL8_FAST') # Inmmediate output.append('push af') REQUIRES.add('mul8.asm') return output
python
def _mul8(ins): op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: op1, op2 = _int_ops(op1, op2) output = _8bit_oper(op1) if op2 == 1: # A * 1 = 1 * A = A output.append('push af') return output if op2 == 0: output.append('xor a') output.append('push af') return output if op2 == 2: # A * 2 == A SLA 1 output.append('add a, a') output.append('push af') return output if op2 == 4: # A * 4 == A SLA 2 output.append('add a, a') output.append('add a, a') output.append('push af') return output output.append('ld h, %i' % int8(op2)) else: if op2[0] == '_': # stack optimization op1, op2 = op2, op1 output = _8bit_oper(op1, op2) output.append('call __MUL8_FAST') # Inmmediate output.append('push af') REQUIRES.add('mul8.asm') return output
[ "def", "_mul8", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "if", "_int_ops", "(", "op1", ",", "op2", ")", "is", "not", "None", ":", "op1", ",", "op2", "=", "_int_ops", "(", "op1", ",", "op2", ")", "output", "=", "_8bit_oper", "(", "op1", ")", "if", "op2", "==", "1", ":", "# A * 1 = 1 * A = A", "output", ".", "append", "(", "'push af'", ")", "return", "output", "if", "op2", "==", "0", ":", "output", ".", "append", "(", "'xor a'", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "if", "op2", "==", "2", ":", "# A * 2 == A SLA 1", "output", ".", "append", "(", "'add a, a'", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "if", "op2", "==", "4", ":", "# A * 4 == A SLA 2", "output", ".", "append", "(", "'add a, a'", ")", "output", ".", "append", "(", "'add a, a'", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "output", ".", "append", "(", "'ld h, %i'", "%", "int8", "(", "op2", ")", ")", "else", ":", "if", "op2", "[", "0", "]", "==", "'_'", ":", "# stack optimization", "op1", ",", "op2", "=", "op2", ",", "op1", "output", "=", "_8bit_oper", "(", "op1", ",", "op2", ")", "output", ".", "append", "(", "'call __MUL8_FAST'", ")", "# Inmmediate", "output", ".", "append", "(", "'push af'", ")", "REQUIRES", ".", "add", "(", "'mul8.asm'", ")", "return", "output" ]
Multiplies 2 las values from the stack. Optimizations: * If any of the ops is ZERO, then do A = 0 ==> XOR A, cause A * 0 = 0 * A = 0 * If any ot the ops is ONE, do NOTHING A * 1 = 1 * A = A
[ "Multiplies", "2", "las", "values", "from", "the", "stack", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__8bit.py#L244-L290
3,553
boriel/zxbasic
arch/zx48k/backend/__8bit.py
_divu8
def _divu8(ins): """ Divides 2 8bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd op is 1 then do nothing * If 2nd op is 2 then Shift Right Logical """ op1, op2 = tuple(ins.quad[2:]) if is_int(op2): op2 = int8(op2) output = _8bit_oper(op1) if op2 == 1: output.append('push af') return output if op2 == 2: output.append('srl a') output.append('push af') return output output.append('ld h, %i' % int8(op2)) else: if op2[0] == '_': # Optimization when 2nd operand is an id if is_int(op1) and int(op1) == 0: output = list() # Optimization: Discard previous op if not from the stack output.append('xor a') output.append('push af') return output rev = True op1, op2 = op2, op1 else: rev = False output = _8bit_oper(op1, op2, rev) output.append('call __DIVU8_FAST') output.append('push af') REQUIRES.add('div8.asm') return output
python
def _divu8(ins): op1, op2 = tuple(ins.quad[2:]) if is_int(op2): op2 = int8(op2) output = _8bit_oper(op1) if op2 == 1: output.append('push af') return output if op2 == 2: output.append('srl a') output.append('push af') return output output.append('ld h, %i' % int8(op2)) else: if op2[0] == '_': # Optimization when 2nd operand is an id if is_int(op1) and int(op1) == 0: output = list() # Optimization: Discard previous op if not from the stack output.append('xor a') output.append('push af') return output rev = True op1, op2 = op2, op1 else: rev = False output = _8bit_oper(op1, op2, rev) output.append('call __DIVU8_FAST') output.append('push af') REQUIRES.add('div8.asm') return output
[ "def", "_divu8", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "if", "is_int", "(", "op2", ")", ":", "op2", "=", "int8", "(", "op2", ")", "output", "=", "_8bit_oper", "(", "op1", ")", "if", "op2", "==", "1", ":", "output", ".", "append", "(", "'push af'", ")", "return", "output", "if", "op2", "==", "2", ":", "output", ".", "append", "(", "'srl a'", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "output", ".", "append", "(", "'ld h, %i'", "%", "int8", "(", "op2", ")", ")", "else", ":", "if", "op2", "[", "0", "]", "==", "'_'", ":", "# Optimization when 2nd operand is an id", "if", "is_int", "(", "op1", ")", "and", "int", "(", "op1", ")", "==", "0", ":", "output", "=", "list", "(", ")", "# Optimization: Discard previous op if not from the stack", "output", ".", "append", "(", "'xor a'", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "rev", "=", "True", "op1", ",", "op2", "=", "op2", ",", "op1", "else", ":", "rev", "=", "False", "output", "=", "_8bit_oper", "(", "op1", ",", "op2", ",", "rev", ")", "output", ".", "append", "(", "'call __DIVU8_FAST'", ")", "output", ".", "append", "(", "'push af'", ")", "REQUIRES", ".", "add", "(", "'div8.asm'", ")", "return", "output" ]
Divides 2 8bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd op is 1 then do nothing * If 2nd op is 2 then Shift Right Logical
[ "Divides", "2", "8bit", "unsigned", "integers", ".", "The", "result", "is", "pushed", "onto", "the", "stack", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__8bit.py#L293-L336
3,554
boriel/zxbasic
arch/zx48k/backend/__8bit.py
_shru8
def _shru8(ins): """ Shift 8bit unsigned integer to the right. The result is pushed onto the stack. Optimizations: * If 1nd or 2nd op is 0 then do nothing * If 2nd op is < 4 then unroll loop """ op1, op2 = tuple(ins.quad[2:]) if is_int(op2): op2 = int8(op2) output = _8bit_oper(op1) if op2 == 0: output.append('push af') return output if op2 < 4: output.extend(['srl a'] * op2) output.append('push af') return output label = tmp_label() output.append('ld b, %i' % int8(op2)) output.append('%s:' % label) output.append('srl a') output.append('djnz %s' % label) output.append('push af') return output if is_int(op1) and int(op1) == 0: output = _8bit_oper(op2) output.append('xor a') output.append('push af') return output output = _8bit_oper(op1, op2, True) label = tmp_label() label2 = tmp_label() output.append('or a') output.append('ld b, a') output.append('ld a, h') output.append('jr z, %s' % label2) output.append('%s:' % label) output.append('srl a') output.append('djnz %s' % label) output.append('%s:' % label2) output.append('push af') return output
python
def _shru8(ins): op1, op2 = tuple(ins.quad[2:]) if is_int(op2): op2 = int8(op2) output = _8bit_oper(op1) if op2 == 0: output.append('push af') return output if op2 < 4: output.extend(['srl a'] * op2) output.append('push af') return output label = tmp_label() output.append('ld b, %i' % int8(op2)) output.append('%s:' % label) output.append('srl a') output.append('djnz %s' % label) output.append('push af') return output if is_int(op1) and int(op1) == 0: output = _8bit_oper(op2) output.append('xor a') output.append('push af') return output output = _8bit_oper(op1, op2, True) label = tmp_label() label2 = tmp_label() output.append('or a') output.append('ld b, a') output.append('ld a, h') output.append('jr z, %s' % label2) output.append('%s:' % label) output.append('srl a') output.append('djnz %s' % label) output.append('%s:' % label2) output.append('push af') return output
[ "def", "_shru8", "(", "ins", ")", ":", "op1", ",", "op2", "=", "tuple", "(", "ins", ".", "quad", "[", "2", ":", "]", ")", "if", "is_int", "(", "op2", ")", ":", "op2", "=", "int8", "(", "op2", ")", "output", "=", "_8bit_oper", "(", "op1", ")", "if", "op2", "==", "0", ":", "output", ".", "append", "(", "'push af'", ")", "return", "output", "if", "op2", "<", "4", ":", "output", ".", "extend", "(", "[", "'srl a'", "]", "*", "op2", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "label", "=", "tmp_label", "(", ")", "output", ".", "append", "(", "'ld b, %i'", "%", "int8", "(", "op2", ")", ")", "output", ".", "append", "(", "'%s:'", "%", "label", ")", "output", ".", "append", "(", "'srl a'", ")", "output", ".", "append", "(", "'djnz %s'", "%", "label", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "if", "is_int", "(", "op1", ")", "and", "int", "(", "op1", ")", "==", "0", ":", "output", "=", "_8bit_oper", "(", "op2", ")", "output", ".", "append", "(", "'xor a'", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output", "output", "=", "_8bit_oper", "(", "op1", ",", "op2", ",", "True", ")", "label", "=", "tmp_label", "(", ")", "label2", "=", "tmp_label", "(", ")", "output", ".", "append", "(", "'or a'", ")", "output", ".", "append", "(", "'ld b, a'", ")", "output", ".", "append", "(", "'ld a, h'", ")", "output", ".", "append", "(", "'jr z, %s'", "%", "label2", ")", "output", ".", "append", "(", "'%s:'", "%", "label", ")", "output", ".", "append", "(", "'srl a'", ")", "output", ".", "append", "(", "'djnz %s'", "%", "label", ")", "output", ".", "append", "(", "'%s:'", "%", "label2", ")", "output", ".", "append", "(", "'push af'", ")", "return", "output" ]
Shift 8bit unsigned integer to the right. The result is pushed onto the stack. Optimizations: * If 1nd or 2nd op is 0 then do nothing * If 2nd op is < 4 then unroll loop
[ "Shift", "8bit", "unsigned", "integer", "to", "the", "right", ".", "The", "result", "is", "pushed", "onto", "the", "stack", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__8bit.py#L894-L945
3,555
boriel/zxbasic
prepro/macrocall.py
MacroCall.is_defined
def is_defined(self, symbolTable=None): """ True if this macro has been defined """ if symbolTable is None: symbolTable = self.table return symbolTable.defined(self.id_)
python
def is_defined(self, symbolTable=None): if symbolTable is None: symbolTable = self.table return symbolTable.defined(self.id_)
[ "def", "is_defined", "(", "self", ",", "symbolTable", "=", "None", ")", ":", "if", "symbolTable", "is", "None", ":", "symbolTable", "=", "self", ".", "table", "return", "symbolTable", ".", "defined", "(", "self", ".", "id_", ")" ]
True if this macro has been defined
[ "True", "if", "this", "macro", "has", "been", "defined" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/prepro/macrocall.py#L89-L95
3,556
boriel/zxbasic
symbols/boundlist.py
SymbolBOUNDLIST.make_node
def make_node(cls, node, *args): ''' Creates an array BOUND LIST. ''' if node is None: return cls.make_node(SymbolBOUNDLIST(), *args) if node.token != 'BOUNDLIST': return cls.make_node(None, node, *args) for arg in args: if arg is None: continue node.appendChild(arg) return node
python
def make_node(cls, node, *args): ''' Creates an array BOUND LIST. ''' if node is None: return cls.make_node(SymbolBOUNDLIST(), *args) if node.token != 'BOUNDLIST': return cls.make_node(None, node, *args) for arg in args: if arg is None: continue node.appendChild(arg) return node
[ "def", "make_node", "(", "cls", ",", "node", ",", "*", "args", ")", ":", "if", "node", "is", "None", ":", "return", "cls", ".", "make_node", "(", "SymbolBOUNDLIST", "(", ")", ",", "*", "args", ")", "if", "node", ".", "token", "!=", "'BOUNDLIST'", ":", "return", "cls", ".", "make_node", "(", "None", ",", "node", ",", "*", "args", ")", "for", "arg", "in", "args", ":", "if", "arg", "is", "None", ":", "continue", "node", ".", "appendChild", "(", "arg", ")", "return", "node" ]
Creates an array BOUND LIST.
[ "Creates", "an", "array", "BOUND", "LIST", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/boundlist.py#L35-L49
3,557
boriel/zxbasic
zxbparser.py
init
def init(): """ Initializes parser state """ global LABELS global LET_ASSIGNMENT global PRINT_IS_USED global SYMBOL_TABLE global ast global data_ast global optemps global OPTIONS global last_brk_linenum LABELS = {} LET_ASSIGNMENT = False PRINT_IS_USED = False last_brk_linenum = 0 ast = None data_ast = None # Global Variables AST optemps = OpcodesTemps() gl.INITS.clear() del gl.FUNCTION_CALLS[:] del gl.FUNCTION_LEVEL[:] del gl.FUNCTIONS[:] SYMBOL_TABLE = gl.SYMBOL_TABLE = api.symboltable.SymbolTable() OPTIONS = api.config.OPTIONS # DATAs info gl.DATA_LABELS.clear() gl.DATA_IS_USED = False del gl.DATAS[:] gl.DATA_PTR_CURRENT = api.utils.current_data_label() gl.DATA_FUNCTIONS = [] gl.error_msg_cache.clear()
python
def init(): global LABELS global LET_ASSIGNMENT global PRINT_IS_USED global SYMBOL_TABLE global ast global data_ast global optemps global OPTIONS global last_brk_linenum LABELS = {} LET_ASSIGNMENT = False PRINT_IS_USED = False last_brk_linenum = 0 ast = None data_ast = None # Global Variables AST optemps = OpcodesTemps() gl.INITS.clear() del gl.FUNCTION_CALLS[:] del gl.FUNCTION_LEVEL[:] del gl.FUNCTIONS[:] SYMBOL_TABLE = gl.SYMBOL_TABLE = api.symboltable.SymbolTable() OPTIONS = api.config.OPTIONS # DATAs info gl.DATA_LABELS.clear() gl.DATA_IS_USED = False del gl.DATAS[:] gl.DATA_PTR_CURRENT = api.utils.current_data_label() gl.DATA_FUNCTIONS = [] gl.error_msg_cache.clear()
[ "def", "init", "(", ")", ":", "global", "LABELS", "global", "LET_ASSIGNMENT", "global", "PRINT_IS_USED", "global", "SYMBOL_TABLE", "global", "ast", "global", "data_ast", "global", "optemps", "global", "OPTIONS", "global", "last_brk_linenum", "LABELS", "=", "{", "}", "LET_ASSIGNMENT", "=", "False", "PRINT_IS_USED", "=", "False", "last_brk_linenum", "=", "0", "ast", "=", "None", "data_ast", "=", "None", "# Global Variables AST", "optemps", "=", "OpcodesTemps", "(", ")", "gl", ".", "INITS", ".", "clear", "(", ")", "del", "gl", ".", "FUNCTION_CALLS", "[", ":", "]", "del", "gl", ".", "FUNCTION_LEVEL", "[", ":", "]", "del", "gl", ".", "FUNCTIONS", "[", ":", "]", "SYMBOL_TABLE", "=", "gl", ".", "SYMBOL_TABLE", "=", "api", ".", "symboltable", ".", "SymbolTable", "(", ")", "OPTIONS", "=", "api", ".", "config", ".", "OPTIONS", "# DATAs info", "gl", ".", "DATA_LABELS", ".", "clear", "(", ")", "gl", ".", "DATA_IS_USED", "=", "False", "del", "gl", ".", "DATAS", "[", ":", "]", "gl", ".", "DATA_PTR_CURRENT", "=", "api", ".", "utils", ".", "current_data_label", "(", ")", "gl", ".", "DATA_FUNCTIONS", "=", "[", "]", "gl", ".", "error_msg_cache", ".", "clear", "(", ")" ]
Initializes parser state
[ "Initializes", "parser", "state" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/zxbparser.py#L113-L149
3,558
boriel/zxbasic
zxbparser.py
make_func_call
def make_func_call(id_, lineno, params): """ This will return an AST node for a function call. """ return symbols.FUNCCALL.make_node(id_, params, lineno)
python
def make_func_call(id_, lineno, params): return symbols.FUNCCALL.make_node(id_, params, lineno)
[ "def", "make_func_call", "(", "id_", ",", "lineno", ",", "params", ")", ":", "return", "symbols", ".", "FUNCCALL", ".", "make_node", "(", "id_", ",", "params", ",", "lineno", ")" ]
This will return an AST node for a function call.
[ "This", "will", "return", "an", "AST", "node", "for", "a", "function", "call", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/zxbparser.py#L287-L290
3,559
boriel/zxbasic
zxbparser.py
make_label
def make_label(id_, lineno): """ Creates a label entry. Returns None on error. """ entry = SYMBOL_TABLE.declare_label(id_, lineno) if entry: gl.DATA_LABELS[id_] = gl.DATA_PTR_CURRENT # This label points to the current DATA block index return entry
python
def make_label(id_, lineno): entry = SYMBOL_TABLE.declare_label(id_, lineno) if entry: gl.DATA_LABELS[id_] = gl.DATA_PTR_CURRENT # This label points to the current DATA block index return entry
[ "def", "make_label", "(", "id_", ",", "lineno", ")", ":", "entry", "=", "SYMBOL_TABLE", ".", "declare_label", "(", "id_", ",", "lineno", ")", "if", "entry", ":", "gl", ".", "DATA_LABELS", "[", "id_", "]", "=", "gl", ".", "DATA_PTR_CURRENT", "# This label points to the current DATA block index", "return", "entry" ]
Creates a label entry. Returns None on error.
[ "Creates", "a", "label", "entry", ".", "Returns", "None", "on", "error", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/zxbparser.py#L416-L422
3,560
boriel/zxbasic
symbols/bound.py
SymbolBOUND.make_node
def make_node(lower, upper, lineno): """ Creates an array bound """ if not is_static(lower, upper): syntax_error(lineno, 'Array bounds must be constants') return None if isinstance(lower, SymbolVAR): lower = lower.value if isinstance(upper, SymbolVAR): upper = upper.value lower.value = int(lower.value) upper.value = int(upper.value) if lower.value < 0: syntax_error(lineno, 'Array bounds must be greater than 0') return None if lower.value > upper.value: syntax_error(lineno, 'Lower array bound must be less or equal to upper one') return None return SymbolBOUND(lower.value, upper.value)
python
def make_node(lower, upper, lineno): if not is_static(lower, upper): syntax_error(lineno, 'Array bounds must be constants') return None if isinstance(lower, SymbolVAR): lower = lower.value if isinstance(upper, SymbolVAR): upper = upper.value lower.value = int(lower.value) upper.value = int(upper.value) if lower.value < 0: syntax_error(lineno, 'Array bounds must be greater than 0') return None if lower.value > upper.value: syntax_error(lineno, 'Lower array bound must be less or equal to upper one') return None return SymbolBOUND(lower.value, upper.value)
[ "def", "make_node", "(", "lower", ",", "upper", ",", "lineno", ")", ":", "if", "not", "is_static", "(", "lower", ",", "upper", ")", ":", "syntax_error", "(", "lineno", ",", "'Array bounds must be constants'", ")", "return", "None", "if", "isinstance", "(", "lower", ",", "SymbolVAR", ")", ":", "lower", "=", "lower", ".", "value", "if", "isinstance", "(", "upper", ",", "SymbolVAR", ")", ":", "upper", "=", "upper", ".", "value", "lower", ".", "value", "=", "int", "(", "lower", ".", "value", ")", "upper", ".", "value", "=", "int", "(", "upper", ".", "value", ")", "if", "lower", ".", "value", "<", "0", ":", "syntax_error", "(", "lineno", ",", "'Array bounds must be greater than 0'", ")", "return", "None", "if", "lower", ".", "value", ">", "upper", ".", "value", ":", "syntax_error", "(", "lineno", ",", "'Lower array bound must be less or equal to upper one'", ")", "return", "None", "return", "SymbolBOUND", "(", "lower", ".", "value", ",", "upper", ".", "value", ")" ]
Creates an array bound
[ "Creates", "an", "array", "bound" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/bound.py#L44-L67
3,561
boriel/zxbasic
basic.py
Basic.sentence_bytes
def sentence_bytes(self, sentence): """ Return bytes of a sentence. This is a very simple parser. Sentence is a list of strings and numbers. 1st element of sentence MUST match a token. """ result = [TOKENS[sentence[0]]] for i in sentence[1:]: # Remaining bytes if isinstance(i, str): result.extend(self.literal(i)) elif isinstance(i, float) or isinstance(i, int): # A number? result.extend(self.number(i)) else: result.extend(i) # Must be another thing return result
python
def sentence_bytes(self, sentence): result = [TOKENS[sentence[0]]] for i in sentence[1:]: # Remaining bytes if isinstance(i, str): result.extend(self.literal(i)) elif isinstance(i, float) or isinstance(i, int): # A number? result.extend(self.number(i)) else: result.extend(i) # Must be another thing return result
[ "def", "sentence_bytes", "(", "self", ",", "sentence", ")", ":", "result", "=", "[", "TOKENS", "[", "sentence", "[", "0", "]", "]", "]", "for", "i", "in", "sentence", "[", "1", ":", "]", ":", "# Remaining bytes", "if", "isinstance", "(", "i", ",", "str", ")", ":", "result", ".", "extend", "(", "self", ".", "literal", "(", "i", ")", ")", "elif", "isinstance", "(", "i", ",", "float", ")", "or", "isinstance", "(", "i", ",", "int", ")", ":", "# A number?", "result", ".", "extend", "(", "self", ".", "number", "(", "i", ")", ")", "else", ":", "result", ".", "extend", "(", "i", ")", "# Must be another thing", "return", "result" ]
Return bytes of a sentence. This is a very simple parser. Sentence is a list of strings and numbers. 1st element of sentence MUST match a token.
[ "Return", "bytes", "of", "a", "sentence", ".", "This", "is", "a", "very", "simple", "parser", ".", "Sentence", "is", "a", "list", "of", "strings", "and", "numbers", ".", "1st", "element", "of", "sentence", "MUST", "match", "a", "token", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/basic.py#L143-L158
3,562
boriel/zxbasic
basic.py
Basic.line
def line(self, sentences, line_number=None): """ Return the bytes for a basic line. If no line number is given, current one + 10 will be used Sentences if a list of sentences """ if line_number is None: line_number = self.current_line + 10 self.current_line = line_number sep = [] result = [] for sentence in sentences: result.extend(sep) result.extend(self.sentence_bytes(sentence)) sep = [ord(':')] result.extend([ENTER]) result = self.line_number(line_number) + self.numberLH(len(result)) + result return result
python
def line(self, sentences, line_number=None): if line_number is None: line_number = self.current_line + 10 self.current_line = line_number sep = [] result = [] for sentence in sentences: result.extend(sep) result.extend(self.sentence_bytes(sentence)) sep = [ord(':')] result.extend([ENTER]) result = self.line_number(line_number) + self.numberLH(len(result)) + result return result
[ "def", "line", "(", "self", ",", "sentences", ",", "line_number", "=", "None", ")", ":", "if", "line_number", "is", "None", ":", "line_number", "=", "self", ".", "current_line", "+", "10", "self", ".", "current_line", "=", "line_number", "sep", "=", "[", "]", "result", "=", "[", "]", "for", "sentence", "in", "sentences", ":", "result", ".", "extend", "(", "sep", ")", "result", ".", "extend", "(", "self", ".", "sentence_bytes", "(", "sentence", ")", ")", "sep", "=", "[", "ord", "(", "':'", ")", "]", "result", ".", "extend", "(", "[", "ENTER", "]", ")", "result", "=", "self", ".", "line_number", "(", "line_number", ")", "+", "self", ".", "numberLH", "(", "len", "(", "result", ")", ")", "+", "result", "return", "result" ]
Return the bytes for a basic line. If no line number is given, current one + 10 will be used Sentences if a list of sentences
[ "Return", "the", "bytes", "for", "a", "basic", "line", ".", "If", "no", "line", "number", "is", "given", "current", "one", "+", "10", "will", "be", "used", "Sentences", "if", "a", "list", "of", "sentences" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/basic.py#L160-L179
3,563
boriel/zxbasic
zxbasmpplex.py
Lexer.include
def include(self, filename): """ Changes FILENAME and line count """ if filename != STDIN and filename in [x[0] for x in self.filestack]: # Already included? self.warning(' Recursive inclusion') self.filestack.append([filename, 1, self.lex, self.input_data]) self.lex = lex.lex(object=self) result = self.put_current_line() # First #line start with \n (EOL) try: if filename == STDIN: self.input_data = sys.stdin.read() else: self.input_data = api.utils.read_txt_file(filename) if len(self.input_data) and self.input_data[-1] != EOL: self.input_data += EOL except IOError: self.input_data = EOL self.lex.input(self.input_data) return result
python
def include(self, filename): if filename != STDIN and filename in [x[0] for x in self.filestack]: # Already included? self.warning(' Recursive inclusion') self.filestack.append([filename, 1, self.lex, self.input_data]) self.lex = lex.lex(object=self) result = self.put_current_line() # First #line start with \n (EOL) try: if filename == STDIN: self.input_data = sys.stdin.read() else: self.input_data = api.utils.read_txt_file(filename) if len(self.input_data) and self.input_data[-1] != EOL: self.input_data += EOL except IOError: self.input_data = EOL self.lex.input(self.input_data) return result
[ "def", "include", "(", "self", ",", "filename", ")", ":", "if", "filename", "!=", "STDIN", "and", "filename", "in", "[", "x", "[", "0", "]", "for", "x", "in", "self", ".", "filestack", "]", ":", "# Already included?", "self", ".", "warning", "(", "' Recursive inclusion'", ")", "self", ".", "filestack", ".", "append", "(", "[", "filename", ",", "1", ",", "self", ".", "lex", ",", "self", ".", "input_data", "]", ")", "self", ".", "lex", "=", "lex", ".", "lex", "(", "object", "=", "self", ")", "result", "=", "self", ".", "put_current_line", "(", ")", "# First #line start with \\n (EOL)", "try", ":", "if", "filename", "==", "STDIN", ":", "self", ".", "input_data", "=", "sys", ".", "stdin", ".", "read", "(", ")", "else", ":", "self", ".", "input_data", "=", "api", ".", "utils", ".", "read_txt_file", "(", "filename", ")", "if", "len", "(", "self", ".", "input_data", ")", "and", "self", ".", "input_data", "[", "-", "1", "]", "!=", "EOL", ":", "self", ".", "input_data", "+=", "EOL", "except", "IOError", ":", "self", ".", "input_data", "=", "EOL", "self", ".", "lex", ".", "input", "(", "self", ".", "input_data", ")", "return", "result" ]
Changes FILENAME and line count
[ "Changes", "FILENAME", "and", "line", "count" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/zxbasmpplex.py#L300-L322
3,564
boriel/zxbasic
symbols/var.py
SymbolVAR.add_alias
def add_alias(self, entry): """ Adds id to the current list 'aliased_by' """ assert isinstance(entry, SymbolVAR) self.aliased_by.append(entry)
python
def add_alias(self, entry): assert isinstance(entry, SymbolVAR) self.aliased_by.append(entry)
[ "def", "add_alias", "(", "self", ",", "entry", ")", ":", "assert", "isinstance", "(", "entry", ",", "SymbolVAR", ")", "self", ".", "aliased_by", ".", "append", "(", "entry", ")" ]
Adds id to the current list 'aliased_by'
[ "Adds", "id", "to", "the", "current", "list", "aliased_by" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/var.py#L78-L82
3,565
boriel/zxbasic
symbols/var.py
SymbolVAR.make_alias
def make_alias(self, entry): """ Make this variable an alias of another one """ entry.add_alias(self) self.alias = entry self.scope = entry.scope # Local aliases can be "global" (static) self.byref = entry.byref self.offset = entry.offset self.addr = entry.addr
python
def make_alias(self, entry): entry.add_alias(self) self.alias = entry self.scope = entry.scope # Local aliases can be "global" (static) self.byref = entry.byref self.offset = entry.offset self.addr = entry.addr
[ "def", "make_alias", "(", "self", ",", "entry", ")", ":", "entry", ".", "add_alias", "(", "self", ")", "self", ".", "alias", "=", "entry", "self", ".", "scope", "=", "entry", ".", "scope", "# Local aliases can be \"global\" (static)", "self", ".", "byref", "=", "entry", ".", "byref", "self", ".", "offset", "=", "entry", ".", "offset", "self", ".", "addr", "=", "entry", ".", "addr" ]
Make this variable an alias of another one
[ "Make", "this", "variable", "an", "alias", "of", "another", "one" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/var.py#L84-L92
3,566
boriel/zxbasic
symbols/var.py
SymbolVAR.to_label
def to_label(var_instance): """ Converts a var_instance to a label one """ # This can be done 'cause LABEL is just a dummy descent of VAR assert isinstance(var_instance, SymbolVAR) from symbols import LABEL var_instance.__class__ = LABEL var_instance.class_ = CLASS.label var_instance._scope_owner = [] return var_instance
python
def to_label(var_instance): # This can be done 'cause LABEL is just a dummy descent of VAR assert isinstance(var_instance, SymbolVAR) from symbols import LABEL var_instance.__class__ = LABEL var_instance.class_ = CLASS.label var_instance._scope_owner = [] return var_instance
[ "def", "to_label", "(", "var_instance", ")", ":", "# This can be done 'cause LABEL is just a dummy descent of VAR", "assert", "isinstance", "(", "var_instance", ",", "SymbolVAR", ")", "from", "symbols", "import", "LABEL", "var_instance", ".", "__class__", "=", "LABEL", "var_instance", ".", "class_", "=", "CLASS", ".", "label", "var_instance", ".", "_scope_owner", "=", "[", "]", "return", "var_instance" ]
Converts a var_instance to a label one
[ "Converts", "a", "var_instance", "to", "a", "label", "one" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/var.py#L130-L139
3,567
boriel/zxbasic
symbols/var.py
SymbolVAR.to_function
def to_function(var_instance, lineno=None): """ Converts a var_instance to a function one """ assert isinstance(var_instance, SymbolVAR) from symbols import FUNCTION var_instance.__class__ = FUNCTION var_instance.class_ = CLASS.function var_instance.reset(lineno=lineno) return var_instance
python
def to_function(var_instance, lineno=None): assert isinstance(var_instance, SymbolVAR) from symbols import FUNCTION var_instance.__class__ = FUNCTION var_instance.class_ = CLASS.function var_instance.reset(lineno=lineno) return var_instance
[ "def", "to_function", "(", "var_instance", ",", "lineno", "=", "None", ")", ":", "assert", "isinstance", "(", "var_instance", ",", "SymbolVAR", ")", "from", "symbols", "import", "FUNCTION", "var_instance", ".", "__class__", "=", "FUNCTION", "var_instance", ".", "class_", "=", "CLASS", ".", "function", "var_instance", ".", "reset", "(", "lineno", "=", "lineno", ")", "return", "var_instance" ]
Converts a var_instance to a function one
[ "Converts", "a", "var_instance", "to", "a", "function", "one" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/var.py#L142-L150
3,568
boriel/zxbasic
symbols/var.py
SymbolVAR.to_vararray
def to_vararray(var_instance, bounds): """ Converts a var_instance to a var array one """ assert isinstance(var_instance, SymbolVAR) from symbols import BOUNDLIST from symbols import VARARRAY assert isinstance(bounds, BOUNDLIST) var_instance.__class__ = VARARRAY var_instance.class_ = CLASS.array var_instance.bounds = bounds return var_instance
python
def to_vararray(var_instance, bounds): assert isinstance(var_instance, SymbolVAR) from symbols import BOUNDLIST from symbols import VARARRAY assert isinstance(bounds, BOUNDLIST) var_instance.__class__ = VARARRAY var_instance.class_ = CLASS.array var_instance.bounds = bounds return var_instance
[ "def", "to_vararray", "(", "var_instance", ",", "bounds", ")", ":", "assert", "isinstance", "(", "var_instance", ",", "SymbolVAR", ")", "from", "symbols", "import", "BOUNDLIST", "from", "symbols", "import", "VARARRAY", "assert", "isinstance", "(", "bounds", ",", "BOUNDLIST", ")", "var_instance", ".", "__class__", "=", "VARARRAY", "var_instance", ".", "class_", "=", "CLASS", ".", "array", "var_instance", ".", "bounds", "=", "bounds", "return", "var_instance" ]
Converts a var_instance to a var array one
[ "Converts", "a", "var_instance", "to", "a", "var", "array", "one" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/symbols/var.py#L153-L163
3,569
boriel/zxbasic
api/constants.py
TYPE.to_type
def to_type(cls, typename): """ Converts a type ID to name. On error returns None """ NAME_TYPES = {cls.TYPE_NAMES[x]: x for x in cls.TYPE_NAMES} return NAME_TYPES.get(typename, None)
python
def to_type(cls, typename): NAME_TYPES = {cls.TYPE_NAMES[x]: x for x in cls.TYPE_NAMES} return NAME_TYPES.get(typename, None)
[ "def", "to_type", "(", "cls", ",", "typename", ")", ":", "NAME_TYPES", "=", "{", "cls", ".", "TYPE_NAMES", "[", "x", "]", ":", "x", "for", "x", "in", "cls", ".", "TYPE_NAMES", "}", "return", "NAME_TYPES", ".", "get", "(", "typename", ",", "None", ")" ]
Converts a type ID to name. On error returns None
[ "Converts", "a", "type", "ID", "to", "name", ".", "On", "error", "returns", "None" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/api/constants.py#L166-L170
3,570
boriel/zxbasic
arch/zx48k/backend/__f16.py
f16
def f16(op): """ Returns a floating point operand converted to 32 bits unsigned int. Negative numbers are returned in 2 complement. The result is returned in a tuple (DE, HL) => High16 (Int part), Low16 (Decimal part) """ op = float(op) negative = op < 0 if negative: op = -op DE = int(op) HL = int((op - DE) * 2**16) & 0xFFFF DE &= 0xFFFF if negative: # Do C2 DE ^= 0xFFFF HL ^= 0xFFFF DEHL = ((DE << 16) | HL) + 1 HL = DEHL & 0xFFFF DE = (DEHL >> 16) & 0xFFFF return (DE, HL)
python
def f16(op): op = float(op) negative = op < 0 if negative: op = -op DE = int(op) HL = int((op - DE) * 2**16) & 0xFFFF DE &= 0xFFFF if negative: # Do C2 DE ^= 0xFFFF HL ^= 0xFFFF DEHL = ((DE << 16) | HL) + 1 HL = DEHL & 0xFFFF DE = (DEHL >> 16) & 0xFFFF return (DE, HL)
[ "def", "f16", "(", "op", ")", ":", "op", "=", "float", "(", "op", ")", "negative", "=", "op", "<", "0", "if", "negative", ":", "op", "=", "-", "op", "DE", "=", "int", "(", "op", ")", "HL", "=", "int", "(", "(", "op", "-", "DE", ")", "*", "2", "**", "16", ")", "&", "0xFFFF", "DE", "&=", "0xFFFF", "if", "negative", ":", "# Do C2", "DE", "^=", "0xFFFF", "HL", "^=", "0xFFFF", "DEHL", "=", "(", "(", "DE", "<<", "16", ")", "|", "HL", ")", "+", "1", "HL", "=", "DEHL", "&", "0xFFFF", "DE", "=", "(", "DEHL", ">>", "16", ")", "&", "0xFFFF", "return", "(", "DE", ",", "HL", ")" ]
Returns a floating point operand converted to 32 bits unsigned int. Negative numbers are returned in 2 complement. The result is returned in a tuple (DE, HL) => High16 (Int part), Low16 (Decimal part)
[ "Returns", "a", "floating", "point", "operand", "converted", "to", "32", "bits", "unsigned", "int", ".", "Negative", "numbers", "are", "returned", "in", "2", "complement", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/__f16.py#L21-L45
3,571
boriel/zxbasic
arch/zx48k/backend/errors.py
throw_invalid_quad_params
def throw_invalid_quad_params(quad, QUADS, nparams): """ Exception raised when an invalid number of params in the quad code has been emmitted. """ raise InvalidICError(str(quad), "Invalid quad code params for '%s' (expected %i, but got %i)" % (quad, QUADS[quad][0], nparams) )
python
def throw_invalid_quad_params(quad, QUADS, nparams): raise InvalidICError(str(quad), "Invalid quad code params for '%s' (expected %i, but got %i)" % (quad, QUADS[quad][0], nparams) )
[ "def", "throw_invalid_quad_params", "(", "quad", ",", "QUADS", ",", "nparams", ")", ":", "raise", "InvalidICError", "(", "str", "(", "quad", ")", ",", "\"Invalid quad code params for '%s' (expected %i, but got %i)\"", "%", "(", "quad", ",", "QUADS", "[", "quad", "]", "[", "0", "]", ",", "nparams", ")", ")" ]
Exception raised when an invalid number of params in the quad code has been emmitted.
[ "Exception", "raised", "when", "an", "invalid", "number", "of", "params", "in", "the", "quad", "code", "has", "been", "emmitted", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/arch/zx48k/backend/errors.py#L72-L79
3,572
boriel/zxbasic
api/fp.py
fp
def fp(x): """ Returns a floating point number as EXP+128, Mantissa """ def bin32(f): """ Returns ASCII representation for a 32 bit integer value """ result = '' a = int(f) & 0xFFFFFFFF # ensures int 32 for i in range(32): result = str(a % 2) + result a = a >> 1 return result def bindec32(f): """ Returns binary representation of a mantissa x (x is float) """ result = '0' a = f if f >= 1: result = bin32(f) result += '.' c = int(a) for i in range(32): a -= c a *= 2 c = int(a) result += str(c) return result e = 0 # exponent s = 1 if x < 0 else 0 # sign m = abs(x) # mantissa while m >= 1: m /= 2.0 e += 1 while 0 < m < 0.5: m *= 2.0 e -= 1 M = bindec32(m)[3:] M = str(s) + M E = bin32(e + 128)[-8:] if x != 0 else bin32(0)[-8:] return M, E
python
def fp(x): def bin32(f): """ Returns ASCII representation for a 32 bit integer value """ result = '' a = int(f) & 0xFFFFFFFF # ensures int 32 for i in range(32): result = str(a % 2) + result a = a >> 1 return result def bindec32(f): """ Returns binary representation of a mantissa x (x is float) """ result = '0' a = f if f >= 1: result = bin32(f) result += '.' c = int(a) for i in range(32): a -= c a *= 2 c = int(a) result += str(c) return result e = 0 # exponent s = 1 if x < 0 else 0 # sign m = abs(x) # mantissa while m >= 1: m /= 2.0 e += 1 while 0 < m < 0.5: m *= 2.0 e -= 1 M = bindec32(m)[3:] M = str(s) + M E = bin32(e + 128)[-8:] if x != 0 else bin32(0)[-8:] return M, E
[ "def", "fp", "(", "x", ")", ":", "def", "bin32", "(", "f", ")", ":", "\"\"\" Returns ASCII representation for a 32 bit integer value\n \"\"\"", "result", "=", "''", "a", "=", "int", "(", "f", ")", "&", "0xFFFFFFFF", "# ensures int 32", "for", "i", "in", "range", "(", "32", ")", ":", "result", "=", "str", "(", "a", "%", "2", ")", "+", "result", "a", "=", "a", ">>", "1", "return", "result", "def", "bindec32", "(", "f", ")", ":", "\"\"\" Returns binary representation of a mantissa x (x is float)\n \"\"\"", "result", "=", "'0'", "a", "=", "f", "if", "f", ">=", "1", ":", "result", "=", "bin32", "(", "f", ")", "result", "+=", "'.'", "c", "=", "int", "(", "a", ")", "for", "i", "in", "range", "(", "32", ")", ":", "a", "-=", "c", "a", "*=", "2", "c", "=", "int", "(", "a", ")", "result", "+=", "str", "(", "c", ")", "return", "result", "e", "=", "0", "# exponent", "s", "=", "1", "if", "x", "<", "0", "else", "0", "# sign", "m", "=", "abs", "(", "x", ")", "# mantissa", "while", "m", ">=", "1", ":", "m", "/=", "2.0", "e", "+=", "1", "while", "0", "<", "m", "<", "0.5", ":", "m", "*=", "2.0", "e", "-=", "1", "M", "=", "bindec32", "(", "m", ")", "[", "3", ":", "]", "M", "=", "str", "(", "s", ")", "+", "M", "E", "=", "bin32", "(", "e", "+", "128", ")", "[", "-", "8", ":", "]", "if", "x", "!=", "0", "else", "bin32", "(", "0", ")", "[", "-", "8", ":", "]", "return", "M", ",", "E" ]
Returns a floating point number as EXP+128, Mantissa
[ "Returns", "a", "floating", "point", "number", "as", "EXP", "+", "128", "Mantissa" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/api/fp.py#L7-L59
3,573
boriel/zxbasic
api/fp.py
immediate_float
def immediate_float(x): """ Returns C DE HL as values for loading and immediate floating point. """ def bin2hex(y): return "%02X" % int(y, 2) M, E = fp(x) C = '0' + bin2hex(E) + 'h' ED = '0' + bin2hex(M[8:16]) + bin2hex(M[:8]) + 'h' LH = '0' + bin2hex(M[24:]) + bin2hex(M[16:24]) + 'h' return C, ED, LH
python
def immediate_float(x): def bin2hex(y): return "%02X" % int(y, 2) M, E = fp(x) C = '0' + bin2hex(E) + 'h' ED = '0' + bin2hex(M[8:16]) + bin2hex(M[:8]) + 'h' LH = '0' + bin2hex(M[24:]) + bin2hex(M[16:24]) + 'h' return C, ED, LH
[ "def", "immediate_float", "(", "x", ")", ":", "def", "bin2hex", "(", "y", ")", ":", "return", "\"%02X\"", "%", "int", "(", "y", ",", "2", ")", "M", ",", "E", "=", "fp", "(", "x", ")", "C", "=", "'0'", "+", "bin2hex", "(", "E", ")", "+", "'h'", "ED", "=", "'0'", "+", "bin2hex", "(", "M", "[", "8", ":", "16", "]", ")", "+", "bin2hex", "(", "M", "[", ":", "8", "]", ")", "+", "'h'", "LH", "=", "'0'", "+", "bin2hex", "(", "M", "[", "24", ":", "]", ")", "+", "bin2hex", "(", "M", "[", "16", ":", "24", "]", ")", "+", "'h'", "return", "C", ",", "ED", ",", "LH" ]
Returns C DE HL as values for loading and immediate floating point.
[ "Returns", "C", "DE", "HL", "as", "values", "for", "loading", "and", "immediate", "floating", "point", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/api/fp.py#L62-L75
3,574
boriel/zxbasic
ast_/tree.py
Tree.inorder
def inorder(self, funct, stopOn=None): """ Iterates in order, calling the function with the current node. If stopOn is set to True or False, it will stop on true or false. """ if stopOn is None: for i in self.children: i.inorder(funct) else: for i in self.children: if i.inorder(funct) == stopOn: return stopOn return funct(self)
python
def inorder(self, funct, stopOn=None): if stopOn is None: for i in self.children: i.inorder(funct) else: for i in self.children: if i.inorder(funct) == stopOn: return stopOn return funct(self)
[ "def", "inorder", "(", "self", ",", "funct", ",", "stopOn", "=", "None", ")", ":", "if", "stopOn", "is", "None", ":", "for", "i", "in", "self", ".", "children", ":", "i", ".", "inorder", "(", "funct", ")", "else", ":", "for", "i", "in", "self", ".", "children", ":", "if", "i", ".", "inorder", "(", "funct", ")", "==", "stopOn", ":", "return", "stopOn", "return", "funct", "(", "self", ")" ]
Iterates in order, calling the function with the current node. If stopOn is set to True or False, it will stop on true or false.
[ "Iterates", "in", "order", "calling", "the", "function", "with", "the", "current", "node", ".", "If", "stopOn", "is", "set", "to", "True", "or", "False", "it", "will", "stop", "on", "true", "or", "false", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/ast_/tree.py#L101-L113
3,575
boriel/zxbasic
ast_/tree.py
Tree.preorder
def preorder(self, funct, stopOn=None): """ Iterates in preorder, calling the function with the current node. If stopOn is set to True or False, it will stop on true or false. """ if funct(self.symbol) == stopOn and stopOn is not None: return stopOn if stopOn is None: for i in self.children: i.preorder(funct) else: for i in self.children: if i.preorder(funct) == stopOn: return stopOn
python
def preorder(self, funct, stopOn=None): if funct(self.symbol) == stopOn and stopOn is not None: return stopOn if stopOn is None: for i in self.children: i.preorder(funct) else: for i in self.children: if i.preorder(funct) == stopOn: return stopOn
[ "def", "preorder", "(", "self", ",", "funct", ",", "stopOn", "=", "None", ")", ":", "if", "funct", "(", "self", ".", "symbol", ")", "==", "stopOn", "and", "stopOn", "is", "not", "None", ":", "return", "stopOn", "if", "stopOn", "is", "None", ":", "for", "i", "in", "self", ".", "children", ":", "i", ".", "preorder", "(", "funct", ")", "else", ":", "for", "i", "in", "self", ".", "children", ":", "if", "i", ".", "preorder", "(", "funct", ")", "==", "stopOn", ":", "return", "stopOn" ]
Iterates in preorder, calling the function with the current node. If stopOn is set to True or False, it will stop on true or false.
[ "Iterates", "in", "preorder", "calling", "the", "function", "with", "the", "current", "node", ".", "If", "stopOn", "is", "set", "to", "True", "or", "False", "it", "will", "stop", "on", "true", "or", "false", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/ast_/tree.py#L115-L128
3,576
boriel/zxbasic
ast_/tree.py
Tree.postorder
def postorder(self, funct, stopOn=None): """ Iterates in postorder, calling the function with the current node. If stopOn is set to True or False, it will stop on true or false. """ if stopOn is None: for i in range(len(self.children) - 1, -1, -1): self.children[i].postorder(funct) else: for i in range(len(self.children) - 1, -1, -1): if self.children[i].postorder(funct) == stopOn: return stopOn return funct(self.symbol)
python
def postorder(self, funct, stopOn=None): if stopOn is None: for i in range(len(self.children) - 1, -1, -1): self.children[i].postorder(funct) else: for i in range(len(self.children) - 1, -1, -1): if self.children[i].postorder(funct) == stopOn: return stopOn return funct(self.symbol)
[ "def", "postorder", "(", "self", ",", "funct", ",", "stopOn", "=", "None", ")", ":", "if", "stopOn", "is", "None", ":", "for", "i", "in", "range", "(", "len", "(", "self", ".", "children", ")", "-", "1", ",", "-", "1", ",", "-", "1", ")", ":", "self", ".", "children", "[", "i", "]", ".", "postorder", "(", "funct", ")", "else", ":", "for", "i", "in", "range", "(", "len", "(", "self", ".", "children", ")", "-", "1", ",", "-", "1", ",", "-", "1", ")", ":", "if", "self", ".", "children", "[", "i", "]", ".", "postorder", "(", "funct", ")", "==", "stopOn", ":", "return", "stopOn", "return", "funct", "(", "self", ".", "symbol", ")" ]
Iterates in postorder, calling the function with the current node. If stopOn is set to True or False, it will stop on true or false.
[ "Iterates", "in", "postorder", "calling", "the", "function", "with", "the", "current", "node", ".", "If", "stopOn", "is", "set", "to", "True", "or", "False", "it", "will", "stop", "on", "true", "or", "false", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/ast_/tree.py#L130-L141
3,577
boriel/zxbasic
ast_/tree.py
Tree.makenode
def makenode(clss, symbol, *nexts): """ Stores the symbol in an AST instance, and left and right to the given ones """ result = clss(symbol) for i in nexts: if i is None: continue if not isinstance(i, clss): raise NotAnAstError(i) result.appendChild(i) return result
python
def makenode(clss, symbol, *nexts): result = clss(symbol) for i in nexts: if i is None: continue if not isinstance(i, clss): raise NotAnAstError(i) result.appendChild(i) return result
[ "def", "makenode", "(", "clss", ",", "symbol", ",", "*", "nexts", ")", ":", "result", "=", "clss", "(", "symbol", ")", "for", "i", "in", "nexts", ":", "if", "i", "is", "None", ":", "continue", "if", "not", "isinstance", "(", "i", ",", "clss", ")", ":", "raise", "NotAnAstError", "(", "i", ")", "result", ".", "appendChild", "(", "i", ")", "return", "result" ]
Stores the symbol in an AST instance, and left and right to the given ones
[ "Stores", "the", "symbol", "in", "an", "AST", "instance", "and", "left", "and", "right", "to", "the", "given", "ones" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/ast_/tree.py#L154-L166
3,578
boriel/zxbasic
zxbpp.py
get_include_path
def get_include_path(): """ Default include path using a tricky sys calls. """ f1 = os.path.basename(sys.argv[0]).lower() # script filename f2 = os.path.basename(sys.executable).lower() # Executable filename # If executable filename and script name are the same, we are if f1 == f2 or f2 == f1 + '.exe': # under a "compiled" python binary result = os.path.dirname(os.path.realpath(sys.executable)) else: result = os.path.dirname(os.path.realpath(__file__)) return result
python
def get_include_path(): f1 = os.path.basename(sys.argv[0]).lower() # script filename f2 = os.path.basename(sys.executable).lower() # Executable filename # If executable filename and script name are the same, we are if f1 == f2 or f2 == f1 + '.exe': # under a "compiled" python binary result = os.path.dirname(os.path.realpath(sys.executable)) else: result = os.path.dirname(os.path.realpath(__file__)) return result
[ "def", "get_include_path", "(", ")", ":", "f1", "=", "os", ".", "path", ".", "basename", "(", "sys", ".", "argv", "[", "0", "]", ")", ".", "lower", "(", ")", "# script filename", "f2", "=", "os", ".", "path", ".", "basename", "(", "sys", ".", "executable", ")", ".", "lower", "(", ")", "# Executable filename", "# If executable filename and script name are the same, we are", "if", "f1", "==", "f2", "or", "f2", "==", "f1", "+", "'.exe'", ":", "# under a \"compiled\" python binary", "result", "=", "os", ".", "path", ".", "dirname", "(", "os", ".", "path", ".", "realpath", "(", "sys", ".", "executable", ")", ")", "else", ":", "result", "=", "os", ".", "path", ".", "dirname", "(", "os", ".", "path", ".", "realpath", "(", "__file__", ")", ")", "return", "result" ]
Default include path using a tricky sys calls.
[ "Default", "include", "path", "using", "a", "tricky", "sys", "calls", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/zxbpp.py#L89-L102
3,579
boriel/zxbasic
zxbpp.py
search_filename
def search_filename(fname, lineno, local_first): """ Search a filename into the list of the include path. If local_first is true, it will try first in the current directory of the file being analyzed. """ fname = api.utils.sanitize_filename(fname) i_path = [CURRENT_DIR] + INCLUDEPATH if local_first else list(INCLUDEPATH) i_path.extend(OPTIONS.include_path.value.split(':') if OPTIONS.include_path.value else []) if os.path.isabs(fname): if os.path.isfile(fname): return fname else: for dir_ in i_path: path = api.utils.sanitize_filename(os.path.join(dir_, fname)) if os.path.exists(path): return path error(lineno, "file '%s' not found" % fname) return ''
python
def search_filename(fname, lineno, local_first): fname = api.utils.sanitize_filename(fname) i_path = [CURRENT_DIR] + INCLUDEPATH if local_first else list(INCLUDEPATH) i_path.extend(OPTIONS.include_path.value.split(':') if OPTIONS.include_path.value else []) if os.path.isabs(fname): if os.path.isfile(fname): return fname else: for dir_ in i_path: path = api.utils.sanitize_filename(os.path.join(dir_, fname)) if os.path.exists(path): return path error(lineno, "file '%s' not found" % fname) return ''
[ "def", "search_filename", "(", "fname", ",", "lineno", ",", "local_first", ")", ":", "fname", "=", "api", ".", "utils", ".", "sanitize_filename", "(", "fname", ")", "i_path", "=", "[", "CURRENT_DIR", "]", "+", "INCLUDEPATH", "if", "local_first", "else", "list", "(", "INCLUDEPATH", ")", "i_path", ".", "extend", "(", "OPTIONS", ".", "include_path", ".", "value", ".", "split", "(", "':'", ")", "if", "OPTIONS", ".", "include_path", ".", "value", "else", "[", "]", ")", "if", "os", ".", "path", ".", "isabs", "(", "fname", ")", ":", "if", "os", ".", "path", ".", "isfile", "(", "fname", ")", ":", "return", "fname", "else", ":", "for", "dir_", "in", "i_path", ":", "path", "=", "api", ".", "utils", ".", "sanitize_filename", "(", "os", ".", "path", ".", "join", "(", "dir_", ",", "fname", ")", ")", "if", "os", ".", "path", ".", "exists", "(", "path", ")", ":", "return", "path", "error", "(", "lineno", ",", "\"file '%s' not found\"", "%", "fname", ")", "return", "''" ]
Search a filename into the list of the include path. If local_first is true, it will try first in the current directory of the file being analyzed.
[ "Search", "a", "filename", "into", "the", "list", "of", "the", "include", "path", ".", "If", "local_first", "is", "true", "it", "will", "try", "first", "in", "the", "current", "directory", "of", "the", "file", "being", "analyzed", "." ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/zxbpp.py#L118-L136
3,580
boriel/zxbasic
zxbpp.py
filter_
def filter_(input_, filename='<internal>', state='INITIAL'): """ Filter the input string thought the preprocessor. result is appended to OUTPUT global str """ global CURRENT_DIR prev_dir = CURRENT_DIR CURRENT_FILE.append(filename) CURRENT_DIR = os.path.dirname(CURRENT_FILE[-1]) LEXER.input(input_, filename) LEXER.lex.begin(state) parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2) CURRENT_FILE.pop() CURRENT_DIR = prev_dir
python
def filter_(input_, filename='<internal>', state='INITIAL'): global CURRENT_DIR prev_dir = CURRENT_DIR CURRENT_FILE.append(filename) CURRENT_DIR = os.path.dirname(CURRENT_FILE[-1]) LEXER.input(input_, filename) LEXER.lex.begin(state) parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2) CURRENT_FILE.pop() CURRENT_DIR = prev_dir
[ "def", "filter_", "(", "input_", ",", "filename", "=", "'<internal>'", ",", "state", "=", "'INITIAL'", ")", ":", "global", "CURRENT_DIR", "prev_dir", "=", "CURRENT_DIR", "CURRENT_FILE", ".", "append", "(", "filename", ")", "CURRENT_DIR", "=", "os", ".", "path", ".", "dirname", "(", "CURRENT_FILE", "[", "-", "1", "]", ")", "LEXER", ".", "input", "(", "input_", ",", "filename", ")", "LEXER", ".", "lex", ".", "begin", "(", "state", ")", "parser", ".", "parse", "(", "lexer", "=", "LEXER", ",", "debug", "=", "OPTIONS", ".", "Debug", ".", "value", ">", "2", ")", "CURRENT_FILE", ".", "pop", "(", ")", "CURRENT_DIR", "=", "prev_dir" ]
Filter the input string thought the preprocessor. result is appended to OUTPUT global str
[ "Filter", "the", "input", "string", "thought", "the", "preprocessor", ".", "result", "is", "appended", "to", "OUTPUT", "global", "str" ]
23b28db10e41117805bdb3c0f78543590853b132
https://github.com/boriel/zxbasic/blob/23b28db10e41117805bdb3c0f78543590853b132/zxbpp.py#L736-L749
3,581
MAVENSDC/PyTplot
pytplot/QtPlotter/generate.py
_set_pyqtgraph_title
def _set_pyqtgraph_title(layout): """ Private function to add a title to the first row of the window. Returns True if a Title is set. Else, returns False. """ if 'title_size' in pytplot.tplot_opt_glob: size = pytplot.tplot_opt_glob['title_size'] if 'title_text' in pytplot.tplot_opt_glob: if pytplot.tplot_opt_glob['title_text'] != '': layout.addItem(LabelItem(pytplot.tplot_opt_glob['title_text'], size=size, color='k'), row=0, col=0) return True return False
python
def _set_pyqtgraph_title(layout): if 'title_size' in pytplot.tplot_opt_glob: size = pytplot.tplot_opt_glob['title_size'] if 'title_text' in pytplot.tplot_opt_glob: if pytplot.tplot_opt_glob['title_text'] != '': layout.addItem(LabelItem(pytplot.tplot_opt_glob['title_text'], size=size, color='k'), row=0, col=0) return True return False
[ "def", "_set_pyqtgraph_title", "(", "layout", ")", ":", "if", "'title_size'", "in", "pytplot", ".", "tplot_opt_glob", ":", "size", "=", "pytplot", ".", "tplot_opt_glob", "[", "'title_size'", "]", "if", "'title_text'", "in", "pytplot", ".", "tplot_opt_glob", ":", "if", "pytplot", ".", "tplot_opt_glob", "[", "'title_text'", "]", "!=", "''", ":", "layout", ".", "addItem", "(", "LabelItem", "(", "pytplot", ".", "tplot_opt_glob", "[", "'title_text'", "]", ",", "size", "=", "size", ",", "color", "=", "'k'", ")", ",", "row", "=", "0", ",", "col", "=", "0", ")", "return", "True", "return", "False" ]
Private function to add a title to the first row of the window. Returns True if a Title is set. Else, returns False.
[ "Private", "function", "to", "add", "a", "title", "to", "the", "first", "row", "of", "the", "window", ".", "Returns", "True", "if", "a", "Title", "is", "set", ".", "Else", "returns", "False", "." ]
d76cdb95363a4bd4fea6bca7960f8523efa7fa83
https://github.com/MAVENSDC/PyTplot/blob/d76cdb95363a4bd4fea6bca7960f8523efa7fa83/pytplot/QtPlotter/generate.py#L102-L113
3,582
MAVENSDC/PyTplot
pytplot/__init__.py
TVar._check_spec_bins_ordering
def _check_spec_bins_ordering(self): """ This is a private function of the TVar object, this is run during object creation to check if spec_bins are ascending or descending """ if self.spec_bins is None: return if len(self.spec_bins) == len(self.data.index): self.spec_bins_time_varying = True break_top_loop = False for index, row in self.spec_bins.iterrows(): if row.isnull().values.all(): continue else: for i in row.index: if np.isfinite(row[i]) and np.isfinite(row[i + 1]): ascending = row[i] < row[i + 1] break_top_loop = True break else: continue if break_top_loop: break else: ascending = self.spec_bins[0].iloc[0] < self.spec_bins[1].iloc[0] return ascending
python
def _check_spec_bins_ordering(self): if self.spec_bins is None: return if len(self.spec_bins) == len(self.data.index): self.spec_bins_time_varying = True break_top_loop = False for index, row in self.spec_bins.iterrows(): if row.isnull().values.all(): continue else: for i in row.index: if np.isfinite(row[i]) and np.isfinite(row[i + 1]): ascending = row[i] < row[i + 1] break_top_loop = True break else: continue if break_top_loop: break else: ascending = self.spec_bins[0].iloc[0] < self.spec_bins[1].iloc[0] return ascending
[ "def", "_check_spec_bins_ordering", "(", "self", ")", ":", "if", "self", ".", "spec_bins", "is", "None", ":", "return", "if", "len", "(", "self", ".", "spec_bins", ")", "==", "len", "(", "self", ".", "data", ".", "index", ")", ":", "self", ".", "spec_bins_time_varying", "=", "True", "break_top_loop", "=", "False", "for", "index", ",", "row", "in", "self", ".", "spec_bins", ".", "iterrows", "(", ")", ":", "if", "row", ".", "isnull", "(", ")", ".", "values", ".", "all", "(", ")", ":", "continue", "else", ":", "for", "i", "in", "row", ".", "index", ":", "if", "np", ".", "isfinite", "(", "row", "[", "i", "]", ")", "and", "np", ".", "isfinite", "(", "row", "[", "i", "+", "1", "]", ")", ":", "ascending", "=", "row", "[", "i", "]", "<", "row", "[", "i", "+", "1", "]", "break_top_loop", "=", "True", "break", "else", ":", "continue", "if", "break_top_loop", ":", "break", "else", ":", "ascending", "=", "self", ".", "spec_bins", "[", "0", "]", ".", "iloc", "[", "0", "]", "<", "self", ".", "spec_bins", "[", "1", "]", ".", "iloc", "[", "0", "]", "return", "ascending" ]
This is a private function of the TVar object, this is run during object creation to check if spec_bins are ascending or descending
[ "This", "is", "a", "private", "function", "of", "the", "TVar", "object", "this", "is", "run", "during", "object", "creation", "to", "check", "if", "spec_bins", "are", "ascending", "or", "descending" ]
d76cdb95363a4bd4fea6bca7960f8523efa7fa83
https://github.com/MAVENSDC/PyTplot/blob/d76cdb95363a4bd4fea6bca7960f8523efa7fa83/pytplot/__init__.py#L138-L163
3,583
MAVENSDC/PyTplot
pytplot/QtPlotter/CustomImage/UpdatingImage.py
UpdatingImage.paint
def paint(self, p, *args): ''' I have no idea why, but we need to generate the picture after painting otherwise it draws incorrectly. ''' if self.picturenotgened: self.generatePicture(self.getBoundingParents()[0].rect()) self.picturenotgened = False pg.ImageItem.paint(self, p, *args) self.generatePicture(self.getBoundingParents()[0].rect())
python
def paint(self, p, *args): ''' I have no idea why, but we need to generate the picture after painting otherwise it draws incorrectly. ''' if self.picturenotgened: self.generatePicture(self.getBoundingParents()[0].rect()) self.picturenotgened = False pg.ImageItem.paint(self, p, *args) self.generatePicture(self.getBoundingParents()[0].rect())
[ "def", "paint", "(", "self", ",", "p", ",", "*", "args", ")", ":", "if", "self", ".", "picturenotgened", ":", "self", ".", "generatePicture", "(", "self", ".", "getBoundingParents", "(", ")", "[", "0", "]", ".", "rect", "(", ")", ")", "self", ".", "picturenotgened", "=", "False", "pg", ".", "ImageItem", ".", "paint", "(", "self", ",", "p", ",", "*", "args", ")", "self", ".", "generatePicture", "(", "self", ".", "getBoundingParents", "(", ")", "[", "0", "]", ".", "rect", "(", ")", ")" ]
I have no idea why, but we need to generate the picture after painting otherwise it draws incorrectly.
[ "I", "have", "no", "idea", "why", "but", "we", "need", "to", "generate", "the", "picture", "after", "painting", "otherwise", "it", "draws", "incorrectly", "." ]
d76cdb95363a4bd4fea6bca7960f8523efa7fa83
https://github.com/MAVENSDC/PyTplot/blob/d76cdb95363a4bd4fea6bca7960f8523efa7fa83/pytplot/QtPlotter/CustomImage/UpdatingImage.py#L94-L103
3,584
MAVENSDC/PyTplot
pytplot/QtPlotter/CustomImage/UpdatingImage.py
UpdatingImage.setImage
def setImage(self, image=None, autoLevels=None, **kargs): """ Same this as ImageItem.setImage, but we don't update the drawing """ profile = debug.Profiler() gotNewData = False if image is None: if self.image is None: return else: gotNewData = True shapeChanged = (self.image is None or image.shape != self.image.shape) image = image.view(np.ndarray) if self.image is None or image.dtype != self.image.dtype: self._effectiveLut = None self.image = image if self.image.shape[0] > 2**15-1 or self.image.shape[1] > 2**15-1: if 'autoDownsample' not in kargs: kargs['autoDownsample'] = True if shapeChanged: self.prepareGeometryChange() self.informViewBoundsChanged() profile() if autoLevels is None: if 'levels' in kargs: autoLevels = False else: autoLevels = True if autoLevels: img = self.image while img.size > 2**16: img = img[::2, ::2] mn, mx = img.min(), img.max() if mn == mx: mn = 0 mx = 255 kargs['levels'] = [mn,mx] profile() self.setOpts(update=False, **kargs) profile() self.qimage = None self.update() profile() if gotNewData: self.sigImageChanged.emit()
python
def setImage(self, image=None, autoLevels=None, **kargs): profile = debug.Profiler() gotNewData = False if image is None: if self.image is None: return else: gotNewData = True shapeChanged = (self.image is None or image.shape != self.image.shape) image = image.view(np.ndarray) if self.image is None or image.dtype != self.image.dtype: self._effectiveLut = None self.image = image if self.image.shape[0] > 2**15-1 or self.image.shape[1] > 2**15-1: if 'autoDownsample' not in kargs: kargs['autoDownsample'] = True if shapeChanged: self.prepareGeometryChange() self.informViewBoundsChanged() profile() if autoLevels is None: if 'levels' in kargs: autoLevels = False else: autoLevels = True if autoLevels: img = self.image while img.size > 2**16: img = img[::2, ::2] mn, mx = img.min(), img.max() if mn == mx: mn = 0 mx = 255 kargs['levels'] = [mn,mx] profile() self.setOpts(update=False, **kargs) profile() self.qimage = None self.update() profile() if gotNewData: self.sigImageChanged.emit()
[ "def", "setImage", "(", "self", ",", "image", "=", "None", ",", "autoLevels", "=", "None", ",", "*", "*", "kargs", ")", ":", "profile", "=", "debug", ".", "Profiler", "(", ")", "gotNewData", "=", "False", "if", "image", "is", "None", ":", "if", "self", ".", "image", "is", "None", ":", "return", "else", ":", "gotNewData", "=", "True", "shapeChanged", "=", "(", "self", ".", "image", "is", "None", "or", "image", ".", "shape", "!=", "self", ".", "image", ".", "shape", ")", "image", "=", "image", ".", "view", "(", "np", ".", "ndarray", ")", "if", "self", ".", "image", "is", "None", "or", "image", ".", "dtype", "!=", "self", ".", "image", ".", "dtype", ":", "self", ".", "_effectiveLut", "=", "None", "self", ".", "image", "=", "image", "if", "self", ".", "image", ".", "shape", "[", "0", "]", ">", "2", "**", "15", "-", "1", "or", "self", ".", "image", ".", "shape", "[", "1", "]", ">", "2", "**", "15", "-", "1", ":", "if", "'autoDownsample'", "not", "in", "kargs", ":", "kargs", "[", "'autoDownsample'", "]", "=", "True", "if", "shapeChanged", ":", "self", ".", "prepareGeometryChange", "(", ")", "self", ".", "informViewBoundsChanged", "(", ")", "profile", "(", ")", "if", "autoLevels", "is", "None", ":", "if", "'levels'", "in", "kargs", ":", "autoLevels", "=", "False", "else", ":", "autoLevels", "=", "True", "if", "autoLevels", ":", "img", "=", "self", ".", "image", "while", "img", ".", "size", ">", "2", "**", "16", ":", "img", "=", "img", "[", ":", ":", "2", ",", ":", ":", "2", "]", "mn", ",", "mx", "=", "img", ".", "min", "(", ")", ",", "img", ".", "max", "(", ")", "if", "mn", "==", "mx", ":", "mn", "=", "0", "mx", "=", "255", "kargs", "[", "'levels'", "]", "=", "[", "mn", ",", "mx", "]", "profile", "(", ")", "self", ".", "setOpts", "(", "update", "=", "False", ",", "*", "*", "kargs", ")", "profile", "(", ")", "self", ".", "qimage", "=", "None", "self", ".", "update", "(", ")", "profile", "(", ")", "if", "gotNewData", ":", "self", ".", "sigImageChanged", ".", "emit", "(", ")" ]
Same this as ImageItem.setImage, but we don't update the drawing
[ "Same", "this", "as", "ImageItem", ".", "setImage", "but", "we", "don", "t", "update", "the", "drawing" ]
d76cdb95363a4bd4fea6bca7960f8523efa7fa83
https://github.com/MAVENSDC/PyTplot/blob/d76cdb95363a4bd4fea6bca7960f8523efa7fa83/pytplot/QtPlotter/CustomImage/UpdatingImage.py#L168-L222
3,585
MAVENSDC/PyTplot
pytplot/QtPlotter/PyTPlot_Exporter.py
PytplotExporter.getPaintItems
def getPaintItems(self, root=None): """Return a list of all items that should be painted in the correct order.""" if root is None: root = self.item preItems = [] postItems = [] if isinstance(root, QtGui.QGraphicsScene): childs = [i for i in root.items() if i.parentItem() is None] rootItem = [] else: # CHANGE: For GraphicsLayouts, there is no function for childItems(), so I just # replaced it with .items() try: childs = root.childItems() except: childs = root.items() rootItem = [root] childs.sort(key=lambda a: a.zValue()) while len(childs) > 0: ch = childs.pop(0) tree = self.getPaintItems(ch) if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or ( ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0): preItems.extend(tree) else: postItems.extend(tree) return preItems + rootItem + postItems
python
def getPaintItems(self, root=None): if root is None: root = self.item preItems = [] postItems = [] if isinstance(root, QtGui.QGraphicsScene): childs = [i for i in root.items() if i.parentItem() is None] rootItem = [] else: # CHANGE: For GraphicsLayouts, there is no function for childItems(), so I just # replaced it with .items() try: childs = root.childItems() except: childs = root.items() rootItem = [root] childs.sort(key=lambda a: a.zValue()) while len(childs) > 0: ch = childs.pop(0) tree = self.getPaintItems(ch) if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or ( ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0): preItems.extend(tree) else: postItems.extend(tree) return preItems + rootItem + postItems
[ "def", "getPaintItems", "(", "self", ",", "root", "=", "None", ")", ":", "if", "root", "is", "None", ":", "root", "=", "self", ".", "item", "preItems", "=", "[", "]", "postItems", "=", "[", "]", "if", "isinstance", "(", "root", ",", "QtGui", ".", "QGraphicsScene", ")", ":", "childs", "=", "[", "i", "for", "i", "in", "root", ".", "items", "(", ")", "if", "i", ".", "parentItem", "(", ")", "is", "None", "]", "rootItem", "=", "[", "]", "else", ":", "# CHANGE: For GraphicsLayouts, there is no function for childItems(), so I just", "# replaced it with .items()", "try", ":", "childs", "=", "root", ".", "childItems", "(", ")", "except", ":", "childs", "=", "root", ".", "items", "(", ")", "rootItem", "=", "[", "root", "]", "childs", ".", "sort", "(", "key", "=", "lambda", "a", ":", "a", ".", "zValue", "(", ")", ")", "while", "len", "(", "childs", ")", ">", "0", ":", "ch", "=", "childs", ".", "pop", "(", "0", ")", "tree", "=", "self", ".", "getPaintItems", "(", "ch", ")", "if", "int", "(", "ch", ".", "flags", "(", ")", "&", "ch", ".", "ItemStacksBehindParent", ")", ">", "0", "or", "(", "ch", ".", "zValue", "(", ")", "<", "0", "and", "int", "(", "ch", ".", "flags", "(", ")", "&", "ch", ".", "ItemNegativeZStacksBehindParent", ")", ">", "0", ")", ":", "preItems", ".", "extend", "(", "tree", ")", "else", ":", "postItems", ".", "extend", "(", "tree", ")", "return", "preItems", "+", "rootItem", "+", "postItems" ]
Return a list of all items that should be painted in the correct order.
[ "Return", "a", "list", "of", "all", "items", "that", "should", "be", "painted", "in", "the", "correct", "order", "." ]
d76cdb95363a4bd4fea6bca7960f8523efa7fa83
https://github.com/MAVENSDC/PyTplot/blob/d76cdb95363a4bd4fea6bca7960f8523efa7fa83/pytplot/QtPlotter/PyTPlot_Exporter.py#L92-L119
3,586
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/normalize_fun.py
qn_df
def qn_df(df, axis='row', keep_orig=False): ''' do quantile normalization of a dataframe dictionary, does not write to net ''' df_qn = {} for mat_type in df: inst_df = df[mat_type] # using transpose to do row qn if axis == 'row': inst_df = inst_df.transpose() missing_values = inst_df.isnull().values.any() # make mask of missing values if missing_values: # get nan mask missing_mask = pd.isnull(inst_df) # tmp fill in na with zero, will not affect qn inst_df = inst_df.fillna(value=0) # calc common distribution common_dist = calc_common_dist(inst_df) # swap in common distribution inst_df = swap_in_common_dist(inst_df, common_dist) # swap back in missing values if missing_values: inst_df = inst_df.mask(missing_mask, other=np.nan) # using transpose to do row qn if axis == 'row': inst_df = inst_df.transpose() df_qn[mat_type] = inst_df return df_qn
python
def qn_df(df, axis='row', keep_orig=False): ''' do quantile normalization of a dataframe dictionary, does not write to net ''' df_qn = {} for mat_type in df: inst_df = df[mat_type] # using transpose to do row qn if axis == 'row': inst_df = inst_df.transpose() missing_values = inst_df.isnull().values.any() # make mask of missing values if missing_values: # get nan mask missing_mask = pd.isnull(inst_df) # tmp fill in na with zero, will not affect qn inst_df = inst_df.fillna(value=0) # calc common distribution common_dist = calc_common_dist(inst_df) # swap in common distribution inst_df = swap_in_common_dist(inst_df, common_dist) # swap back in missing values if missing_values: inst_df = inst_df.mask(missing_mask, other=np.nan) # using transpose to do row qn if axis == 'row': inst_df = inst_df.transpose() df_qn[mat_type] = inst_df return df_qn
[ "def", "qn_df", "(", "df", ",", "axis", "=", "'row'", ",", "keep_orig", "=", "False", ")", ":", "df_qn", "=", "{", "}", "for", "mat_type", "in", "df", ":", "inst_df", "=", "df", "[", "mat_type", "]", "# using transpose to do row qn", "if", "axis", "==", "'row'", ":", "inst_df", "=", "inst_df", ".", "transpose", "(", ")", "missing_values", "=", "inst_df", ".", "isnull", "(", ")", ".", "values", ".", "any", "(", ")", "# make mask of missing values", "if", "missing_values", ":", "# get nan mask", "missing_mask", "=", "pd", ".", "isnull", "(", "inst_df", ")", "# tmp fill in na with zero, will not affect qn", "inst_df", "=", "inst_df", ".", "fillna", "(", "value", "=", "0", ")", "# calc common distribution", "common_dist", "=", "calc_common_dist", "(", "inst_df", ")", "# swap in common distribution", "inst_df", "=", "swap_in_common_dist", "(", "inst_df", ",", "common_dist", ")", "# swap back in missing values", "if", "missing_values", ":", "inst_df", "=", "inst_df", ".", "mask", "(", "missing_mask", ",", "other", "=", "np", ".", "nan", ")", "# using transpose to do row qn", "if", "axis", "==", "'row'", ":", "inst_df", "=", "inst_df", ".", "transpose", "(", ")", "df_qn", "[", "mat_type", "]", "=", "inst_df", "return", "df_qn" ]
do quantile normalization of a dataframe dictionary, does not write to net
[ "do", "quantile", "normalization", "of", "a", "dataframe", "dictionary", "does", "not", "write", "to", "net" ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/normalize_fun.py#L25-L65
3,587
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/cat_pval.py
main
def main(net): ''' calculate pvalue of category closeness ''' # calculate the distance between the data points within the same category and # compare to null distribution for inst_rc in ['row', 'col']: inst_nodes = deepcopy(net.dat['nodes'][inst_rc]) inst_index = deepcopy(net.dat['node_info'][inst_rc]['clust']) # reorder based on clustered order inst_nodes = [ inst_nodes[i] for i in inst_index] # make distance matrix dataframe dm = dist_matrix_lattice(inst_nodes) node_infos = list(net.dat['node_info'][inst_rc].keys()) all_cats = [] for inst_info in node_infos: if 'dict_cat_' in inst_info: all_cats.append(inst_info) for cat_dict in all_cats: tmp_dict = net.dat['node_info'][inst_rc][cat_dict] pval_name = cat_dict.replace('dict_','pval_') net.dat['node_info'][inst_rc][pval_name] = {} for cat_name in tmp_dict: subset = tmp_dict[cat_name] inst_median = calc_median_dist_subset(dm, subset) hist = calc_hist_distances(dm, subset, inst_nodes) pval = 0 for i in range(len(hist['prob'])): if i == 0: pval = hist['prob'][i] if i >= 1: if inst_median >= hist['bins'][i]: pval = pval + hist['prob'][i] net.dat['node_info'][inst_rc][pval_name][cat_name] = pval
python
def main(net): ''' calculate pvalue of category closeness ''' # calculate the distance between the data points within the same category and # compare to null distribution for inst_rc in ['row', 'col']: inst_nodes = deepcopy(net.dat['nodes'][inst_rc]) inst_index = deepcopy(net.dat['node_info'][inst_rc]['clust']) # reorder based on clustered order inst_nodes = [ inst_nodes[i] for i in inst_index] # make distance matrix dataframe dm = dist_matrix_lattice(inst_nodes) node_infos = list(net.dat['node_info'][inst_rc].keys()) all_cats = [] for inst_info in node_infos: if 'dict_cat_' in inst_info: all_cats.append(inst_info) for cat_dict in all_cats: tmp_dict = net.dat['node_info'][inst_rc][cat_dict] pval_name = cat_dict.replace('dict_','pval_') net.dat['node_info'][inst_rc][pval_name] = {} for cat_name in tmp_dict: subset = tmp_dict[cat_name] inst_median = calc_median_dist_subset(dm, subset) hist = calc_hist_distances(dm, subset, inst_nodes) pval = 0 for i in range(len(hist['prob'])): if i == 0: pval = hist['prob'][i] if i >= 1: if inst_median >= hist['bins'][i]: pval = pval + hist['prob'][i] net.dat['node_info'][inst_rc][pval_name][cat_name] = pval
[ "def", "main", "(", "net", ")", ":", "# calculate the distance between the data points within the same category and", "# compare to null distribution", "for", "inst_rc", "in", "[", "'row'", ",", "'col'", "]", ":", "inst_nodes", "=", "deepcopy", "(", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", ")", "inst_index", "=", "deepcopy", "(", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "'clust'", "]", ")", "# reorder based on clustered order", "inst_nodes", "=", "[", "inst_nodes", "[", "i", "]", "for", "i", "in", "inst_index", "]", "# make distance matrix dataframe", "dm", "=", "dist_matrix_lattice", "(", "inst_nodes", ")", "node_infos", "=", "list", "(", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", ".", "keys", "(", ")", ")", "all_cats", "=", "[", "]", "for", "inst_info", "in", "node_infos", ":", "if", "'dict_cat_'", "in", "inst_info", ":", "all_cats", ".", "append", "(", "inst_info", ")", "for", "cat_dict", "in", "all_cats", ":", "tmp_dict", "=", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "cat_dict", "]", "pval_name", "=", "cat_dict", ".", "replace", "(", "'dict_'", ",", "'pval_'", ")", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "pval_name", "]", "=", "{", "}", "for", "cat_name", "in", "tmp_dict", ":", "subset", "=", "tmp_dict", "[", "cat_name", "]", "inst_median", "=", "calc_median_dist_subset", "(", "dm", ",", "subset", ")", "hist", "=", "calc_hist_distances", "(", "dm", ",", "subset", ",", "inst_nodes", ")", "pval", "=", "0", "for", "i", "in", "range", "(", "len", "(", "hist", "[", "'prob'", "]", ")", ")", ":", "if", "i", "==", "0", ":", "pval", "=", "hist", "[", "'prob'", "]", "[", "i", "]", "if", "i", ">=", "1", ":", "if", "inst_median", ">=", "hist", "[", "'bins'", "]", "[", "i", "]", ":", "pval", "=", "pval", "+", "hist", "[", "'prob'", "]", "[", "i", "]", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "pval_name", "]", "[", "cat_name", "]", "=", "pval" ]
calculate pvalue of category closeness
[ "calculate", "pvalue", "of", "category", "closeness" ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/cat_pval.py#L5-L54
3,588
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/data_formats.py
df_to_dat
def df_to_dat(net, df, define_cat_colors=False): ''' This is always run when data is loaded. ''' from . import categories # check if df has unique values df['mat'] = make_unique_labels.main(net, df['mat']) net.dat['mat'] = df['mat'].values net.dat['nodes']['row'] = df['mat'].index.tolist() net.dat['nodes']['col'] = df['mat'].columns.tolist() for inst_rc in ['row', 'col']: if type(net.dat['nodes'][inst_rc][0]) is tuple: # get the number of categories from the length of the tuple # subtract 1 because the name is the first element of the tuple num_cat = len(net.dat['nodes'][inst_rc][0]) - 1 net.dat['node_info'][inst_rc]['full_names'] = net.dat['nodes']\ [inst_rc] for inst_rcat in range(num_cat): net.dat['node_info'][inst_rc]['cat-' + str(inst_rcat)] = \ [i[inst_rcat + 1] for i in net.dat['nodes'][inst_rc]] net.dat['nodes'][inst_rc] = [i[0] for i in net.dat['nodes'][inst_rc]] if 'mat_up' in df: net.dat['mat_up'] = df['mat_up'].values net.dat['mat_dn'] = df['mat_dn'].values if 'mat_orig' in df: net.dat['mat_orig'] = df['mat_orig'].values categories.dict_cat(net, define_cat_colors=define_cat_colors)
python
def df_to_dat(net, df, define_cat_colors=False): ''' This is always run when data is loaded. ''' from . import categories # check if df has unique values df['mat'] = make_unique_labels.main(net, df['mat']) net.dat['mat'] = df['mat'].values net.dat['nodes']['row'] = df['mat'].index.tolist() net.dat['nodes']['col'] = df['mat'].columns.tolist() for inst_rc in ['row', 'col']: if type(net.dat['nodes'][inst_rc][0]) is tuple: # get the number of categories from the length of the tuple # subtract 1 because the name is the first element of the tuple num_cat = len(net.dat['nodes'][inst_rc][0]) - 1 net.dat['node_info'][inst_rc]['full_names'] = net.dat['nodes']\ [inst_rc] for inst_rcat in range(num_cat): net.dat['node_info'][inst_rc]['cat-' + str(inst_rcat)] = \ [i[inst_rcat + 1] for i in net.dat['nodes'][inst_rc]] net.dat['nodes'][inst_rc] = [i[0] for i in net.dat['nodes'][inst_rc]] if 'mat_up' in df: net.dat['mat_up'] = df['mat_up'].values net.dat['mat_dn'] = df['mat_dn'].values if 'mat_orig' in df: net.dat['mat_orig'] = df['mat_orig'].values categories.dict_cat(net, define_cat_colors=define_cat_colors)
[ "def", "df_to_dat", "(", "net", ",", "df", ",", "define_cat_colors", "=", "False", ")", ":", "from", ".", "import", "categories", "# check if df has unique values", "df", "[", "'mat'", "]", "=", "make_unique_labels", ".", "main", "(", "net", ",", "df", "[", "'mat'", "]", ")", "net", ".", "dat", "[", "'mat'", "]", "=", "df", "[", "'mat'", "]", ".", "values", "net", ".", "dat", "[", "'nodes'", "]", "[", "'row'", "]", "=", "df", "[", "'mat'", "]", ".", "index", ".", "tolist", "(", ")", "net", ".", "dat", "[", "'nodes'", "]", "[", "'col'", "]", "=", "df", "[", "'mat'", "]", ".", "columns", ".", "tolist", "(", ")", "for", "inst_rc", "in", "[", "'row'", ",", "'col'", "]", ":", "if", "type", "(", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "[", "0", "]", ")", "is", "tuple", ":", "# get the number of categories from the length of the tuple", "# subtract 1 because the name is the first element of the tuple", "num_cat", "=", "len", "(", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "[", "0", "]", ")", "-", "1", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "'full_names'", "]", "=", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "for", "inst_rcat", "in", "range", "(", "num_cat", ")", ":", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "'cat-'", "+", "str", "(", "inst_rcat", ")", "]", "=", "[", "i", "[", "inst_rcat", "+", "1", "]", "for", "i", "in", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "]", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "=", "[", "i", "[", "0", "]", "for", "i", "in", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "]", "if", "'mat_up'", "in", "df", ":", "net", ".", "dat", "[", "'mat_up'", "]", "=", "df", "[", "'mat_up'", "]", ".", "values", "net", ".", "dat", "[", "'mat_dn'", "]", "=", "df", "[", "'mat_dn'", "]", ".", "values", "if", "'mat_orig'", "in", "df", ":", "net", ".", "dat", "[", "'mat_orig'", "]", "=", "df", "[", "'mat_orig'", "]", ".", "values", "categories", ".", "dict_cat", "(", "net", ",", "define_cat_colors", "=", "define_cat_colors", ")" ]
This is always run when data is loaded.
[ "This", "is", "always", "run", "when", "data", "is", "loaded", "." ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/data_formats.py#L3-L39
3,589
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/data_formats.py
mat_to_numpy_arr
def mat_to_numpy_arr(self): ''' convert list to numpy array - numpy arrays can not be saved as json ''' import numpy as np self.dat['mat'] = np.asarray(self.dat['mat'])
python
def mat_to_numpy_arr(self): ''' convert list to numpy array - numpy arrays can not be saved as json ''' import numpy as np self.dat['mat'] = np.asarray(self.dat['mat'])
[ "def", "mat_to_numpy_arr", "(", "self", ")", ":", "import", "numpy", "as", "np", "self", ".", "dat", "[", "'mat'", "]", "=", "np", ".", "asarray", "(", "self", ".", "dat", "[", "'mat'", "]", ")" ]
convert list to numpy array - numpy arrays can not be saved as json
[ "convert", "list", "to", "numpy", "array", "-", "numpy", "arrays", "can", "not", "be", "saved", "as", "json" ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/data_formats.py#L69-L72
3,590
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/calc_clust.py
cluster_row_and_col
def cluster_row_and_col(net, dist_type='cosine', linkage_type='average', dendro=True, run_clustering=True, run_rank=True, ignore_cat=False, calc_cat_pval=False, links=False): ''' cluster net.dat and make visualization json, net.viz. optionally leave out dendrogram colorbar groups with dendro argument ''' import scipy from copy import deepcopy from scipy.spatial.distance import pdist from . import categories, make_viz, cat_pval dm = {} for inst_rc in ['row', 'col']: tmp_mat = deepcopy(net.dat['mat']) dm[inst_rc] = calc_distance_matrix(tmp_mat, inst_rc, dist_type) # save directly to dat structure node_info = net.dat['node_info'][inst_rc] node_info['ini'] = list(range( len(net.dat['nodes'][inst_rc]), -1, -1)) # cluster if run_clustering is True: node_info['clust'], node_info['group'] = \ clust_and_group(net, dm[inst_rc], linkage_type=linkage_type) else: dendro = False node_info['clust'] = node_info['ini'] # sorting if run_rank is True: node_info['rank'] = sort_rank_nodes(net, inst_rc, 'sum') node_info['rankvar'] = sort_rank_nodes(net, inst_rc, 'var') else: node_info['rank'] = node_info['ini'] node_info['rankvar'] = node_info['ini'] ################################## if ignore_cat is False: categories.calc_cat_clust_order(net, inst_rc) if calc_cat_pval is True: cat_pval.main(net) # make the visualization json make_viz.viz_json(net, dendro, links) return dm
python
def cluster_row_and_col(net, dist_type='cosine', linkage_type='average', dendro=True, run_clustering=True, run_rank=True, ignore_cat=False, calc_cat_pval=False, links=False): ''' cluster net.dat and make visualization json, net.viz. optionally leave out dendrogram colorbar groups with dendro argument ''' import scipy from copy import deepcopy from scipy.spatial.distance import pdist from . import categories, make_viz, cat_pval dm = {} for inst_rc in ['row', 'col']: tmp_mat = deepcopy(net.dat['mat']) dm[inst_rc] = calc_distance_matrix(tmp_mat, inst_rc, dist_type) # save directly to dat structure node_info = net.dat['node_info'][inst_rc] node_info['ini'] = list(range( len(net.dat['nodes'][inst_rc]), -1, -1)) # cluster if run_clustering is True: node_info['clust'], node_info['group'] = \ clust_and_group(net, dm[inst_rc], linkage_type=linkage_type) else: dendro = False node_info['clust'] = node_info['ini'] # sorting if run_rank is True: node_info['rank'] = sort_rank_nodes(net, inst_rc, 'sum') node_info['rankvar'] = sort_rank_nodes(net, inst_rc, 'var') else: node_info['rank'] = node_info['ini'] node_info['rankvar'] = node_info['ini'] ################################## if ignore_cat is False: categories.calc_cat_clust_order(net, inst_rc) if calc_cat_pval is True: cat_pval.main(net) # make the visualization json make_viz.viz_json(net, dendro, links) return dm
[ "def", "cluster_row_and_col", "(", "net", ",", "dist_type", "=", "'cosine'", ",", "linkage_type", "=", "'average'", ",", "dendro", "=", "True", ",", "run_clustering", "=", "True", ",", "run_rank", "=", "True", ",", "ignore_cat", "=", "False", ",", "calc_cat_pval", "=", "False", ",", "links", "=", "False", ")", ":", "import", "scipy", "from", "copy", "import", "deepcopy", "from", "scipy", ".", "spatial", ".", "distance", "import", "pdist", "from", ".", "import", "categories", ",", "make_viz", ",", "cat_pval", "dm", "=", "{", "}", "for", "inst_rc", "in", "[", "'row'", ",", "'col'", "]", ":", "tmp_mat", "=", "deepcopy", "(", "net", ".", "dat", "[", "'mat'", "]", ")", "dm", "[", "inst_rc", "]", "=", "calc_distance_matrix", "(", "tmp_mat", ",", "inst_rc", ",", "dist_type", ")", "# save directly to dat structure", "node_info", "=", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "node_info", "[", "'ini'", "]", "=", "list", "(", "range", "(", "len", "(", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", ")", ",", "-", "1", ",", "-", "1", ")", ")", "# cluster", "if", "run_clustering", "is", "True", ":", "node_info", "[", "'clust'", "]", ",", "node_info", "[", "'group'", "]", "=", "clust_and_group", "(", "net", ",", "dm", "[", "inst_rc", "]", ",", "linkage_type", "=", "linkage_type", ")", "else", ":", "dendro", "=", "False", "node_info", "[", "'clust'", "]", "=", "node_info", "[", "'ini'", "]", "# sorting", "if", "run_rank", "is", "True", ":", "node_info", "[", "'rank'", "]", "=", "sort_rank_nodes", "(", "net", ",", "inst_rc", ",", "'sum'", ")", "node_info", "[", "'rankvar'", "]", "=", "sort_rank_nodes", "(", "net", ",", "inst_rc", ",", "'var'", ")", "else", ":", "node_info", "[", "'rank'", "]", "=", "node_info", "[", "'ini'", "]", "node_info", "[", "'rankvar'", "]", "=", "node_info", "[", "'ini'", "]", "##################################", "if", "ignore_cat", "is", "False", ":", "categories", ".", "calc_cat_clust_order", "(", "net", ",", "inst_rc", ")", "if", "calc_cat_pval", "is", "True", ":", "cat_pval", ".", "main", "(", "net", ")", "# make the visualization json", "make_viz", ".", "viz_json", "(", "net", ",", "dendro", ",", "links", ")", "return", "dm" ]
cluster net.dat and make visualization json, net.viz. optionally leave out dendrogram colorbar groups with dendro argument
[ "cluster", "net", ".", "dat", "and", "make", "visualization", "json", "net", ".", "viz", ".", "optionally", "leave", "out", "dendrogram", "colorbar", "groups", "with", "dendro", "argument" ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/calc_clust.py#L1-L49
3,591
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/categories.py
check_categories
def check_categories(lines): ''' find out how many row and col categories are available ''' # count the number of row categories rcat_line = lines[0].split('\t') # calc the number of row names and categories num_rc = 0 found_end = False # skip first tab for inst_string in rcat_line[1:]: if inst_string == '': if found_end is False: num_rc = num_rc + 1 else: found_end = True max_rcat = 15 if max_rcat > len(lines): max_rcat = len(lines) - 1 num_cc = 0 for i in range(max_rcat): ccat_line = lines[i + 1].split('\t') # make sure that line has length greater than one to prevent false cats from # trailing new lines at end of matrix if ccat_line[0] == '' and len(ccat_line) > 1: num_cc = num_cc + 1 num_labels = {} num_labels['row'] = num_rc + 1 num_labels['col'] = num_cc + 1 return num_labels
python
def check_categories(lines): ''' find out how many row and col categories are available ''' # count the number of row categories rcat_line = lines[0].split('\t') # calc the number of row names and categories num_rc = 0 found_end = False # skip first tab for inst_string in rcat_line[1:]: if inst_string == '': if found_end is False: num_rc = num_rc + 1 else: found_end = True max_rcat = 15 if max_rcat > len(lines): max_rcat = len(lines) - 1 num_cc = 0 for i in range(max_rcat): ccat_line = lines[i + 1].split('\t') # make sure that line has length greater than one to prevent false cats from # trailing new lines at end of matrix if ccat_line[0] == '' and len(ccat_line) > 1: num_cc = num_cc + 1 num_labels = {} num_labels['row'] = num_rc + 1 num_labels['col'] = num_cc + 1 return num_labels
[ "def", "check_categories", "(", "lines", ")", ":", "# count the number of row categories", "rcat_line", "=", "lines", "[", "0", "]", ".", "split", "(", "'\\t'", ")", "# calc the number of row names and categories", "num_rc", "=", "0", "found_end", "=", "False", "# skip first tab", "for", "inst_string", "in", "rcat_line", "[", "1", ":", "]", ":", "if", "inst_string", "==", "''", ":", "if", "found_end", "is", "False", ":", "num_rc", "=", "num_rc", "+", "1", "else", ":", "found_end", "=", "True", "max_rcat", "=", "15", "if", "max_rcat", ">", "len", "(", "lines", ")", ":", "max_rcat", "=", "len", "(", "lines", ")", "-", "1", "num_cc", "=", "0", "for", "i", "in", "range", "(", "max_rcat", ")", ":", "ccat_line", "=", "lines", "[", "i", "+", "1", "]", ".", "split", "(", "'\\t'", ")", "# make sure that line has length greater than one to prevent false cats from", "# trailing new lines at end of matrix", "if", "ccat_line", "[", "0", "]", "==", "''", "and", "len", "(", "ccat_line", ")", ">", "1", ":", "num_cc", "=", "num_cc", "+", "1", "num_labels", "=", "{", "}", "num_labels", "[", "'row'", "]", "=", "num_rc", "+", "1", "num_labels", "[", "'col'", "]", "=", "num_cc", "+", "1", "return", "num_labels" ]
find out how many row and col categories are available
[ "find", "out", "how", "many", "row", "and", "col", "categories", "are", "available" ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/categories.py#L1-L37
3,592
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/categories.py
dict_cat
def dict_cat(net, define_cat_colors=False): ''' make a dictionary of node-category associations ''' # print('---------------------------------') # print('---- dict_cat: before setting cat colors') # print('---------------------------------\n') # print(define_cat_colors) # print(net.viz['cat_colors']) net.persistent_cat = True for inst_rc in ['row', 'col']: inst_keys = list(net.dat['node_info'][inst_rc].keys()) all_cats = [x for x in inst_keys if 'cat-' in x] for inst_name_cat in all_cats: dict_cat = {} tmp_cats = net.dat['node_info'][inst_rc][inst_name_cat] tmp_nodes = net.dat['nodes'][inst_rc] for i in range(len(tmp_cats)): inst_cat = tmp_cats[i] inst_node = tmp_nodes[i] if inst_cat not in dict_cat: dict_cat[inst_cat] = [] dict_cat[inst_cat].append(inst_node) tmp_name = 'dict_' + inst_name_cat.replace('-', '_') net.dat['node_info'][inst_rc][tmp_name] = dict_cat # merge with old cat_colors by default cat_colors = net.viz['cat_colors'] if define_cat_colors == True: cat_number = 0 for inst_rc in ['row', 'col']: inst_keys = list(net.dat['node_info'][inst_rc].keys()) all_cats = [x for x in inst_keys if 'cat-' in x] for cat_index in all_cats: if cat_index not in cat_colors[inst_rc]: cat_colors[inst_rc][cat_index] = {} cat_names = sorted(list(set(net.dat['node_info'][inst_rc][cat_index]))) # loop through each category name and assign a color for tmp_name in cat_names: # using the same rules as the front-end to define cat_colors inst_color = get_cat_color(cat_number + cat_names.index(tmp_name)) check_name = tmp_name # check if category is string type and non-numeric try: float(check_name) is_string_cat = False except: is_string_cat = True if is_string_cat == True: # check for default non-color if ': ' in check_name: check_name = check_name.split(': ')[1] # if check_name == 'False' or check_name == 'false': if 'False' in check_name or 'false' in check_name: inst_color = '#eee' if 'Not ' in check_name: inst_color = '#eee' # print('cat_colors') # print('----------') # print(cat_colors[inst_rc][cat_index]) # do not overwrite old colors if tmp_name not in cat_colors[inst_rc][cat_index] and is_string_cat: cat_colors[inst_rc][cat_index][tmp_name] = inst_color # print('overwrite: ' + tmp_name + ' -> ' + str(inst_color)) cat_number = cat_number + 1 net.viz['cat_colors'] = cat_colors
python
def dict_cat(net, define_cat_colors=False): ''' make a dictionary of node-category associations ''' # print('---------------------------------') # print('---- dict_cat: before setting cat colors') # print('---------------------------------\n') # print(define_cat_colors) # print(net.viz['cat_colors']) net.persistent_cat = True for inst_rc in ['row', 'col']: inst_keys = list(net.dat['node_info'][inst_rc].keys()) all_cats = [x for x in inst_keys if 'cat-' in x] for inst_name_cat in all_cats: dict_cat = {} tmp_cats = net.dat['node_info'][inst_rc][inst_name_cat] tmp_nodes = net.dat['nodes'][inst_rc] for i in range(len(tmp_cats)): inst_cat = tmp_cats[i] inst_node = tmp_nodes[i] if inst_cat not in dict_cat: dict_cat[inst_cat] = [] dict_cat[inst_cat].append(inst_node) tmp_name = 'dict_' + inst_name_cat.replace('-', '_') net.dat['node_info'][inst_rc][tmp_name] = dict_cat # merge with old cat_colors by default cat_colors = net.viz['cat_colors'] if define_cat_colors == True: cat_number = 0 for inst_rc in ['row', 'col']: inst_keys = list(net.dat['node_info'][inst_rc].keys()) all_cats = [x for x in inst_keys if 'cat-' in x] for cat_index in all_cats: if cat_index not in cat_colors[inst_rc]: cat_colors[inst_rc][cat_index] = {} cat_names = sorted(list(set(net.dat['node_info'][inst_rc][cat_index]))) # loop through each category name and assign a color for tmp_name in cat_names: # using the same rules as the front-end to define cat_colors inst_color = get_cat_color(cat_number + cat_names.index(tmp_name)) check_name = tmp_name # check if category is string type and non-numeric try: float(check_name) is_string_cat = False except: is_string_cat = True if is_string_cat == True: # check for default non-color if ': ' in check_name: check_name = check_name.split(': ')[1] # if check_name == 'False' or check_name == 'false': if 'False' in check_name or 'false' in check_name: inst_color = '#eee' if 'Not ' in check_name: inst_color = '#eee' # print('cat_colors') # print('----------') # print(cat_colors[inst_rc][cat_index]) # do not overwrite old colors if tmp_name not in cat_colors[inst_rc][cat_index] and is_string_cat: cat_colors[inst_rc][cat_index][tmp_name] = inst_color # print('overwrite: ' + tmp_name + ' -> ' + str(inst_color)) cat_number = cat_number + 1 net.viz['cat_colors'] = cat_colors
[ "def", "dict_cat", "(", "net", ",", "define_cat_colors", "=", "False", ")", ":", "# print('---------------------------------')", "# print('---- dict_cat: before setting cat colors')", "# print('---------------------------------\\n')", "# print(define_cat_colors)", "# print(net.viz['cat_colors'])", "net", ".", "persistent_cat", "=", "True", "for", "inst_rc", "in", "[", "'row'", ",", "'col'", "]", ":", "inst_keys", "=", "list", "(", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", ".", "keys", "(", ")", ")", "all_cats", "=", "[", "x", "for", "x", "in", "inst_keys", "if", "'cat-'", "in", "x", "]", "for", "inst_name_cat", "in", "all_cats", ":", "dict_cat", "=", "{", "}", "tmp_cats", "=", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "inst_name_cat", "]", "tmp_nodes", "=", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "for", "i", "in", "range", "(", "len", "(", "tmp_cats", ")", ")", ":", "inst_cat", "=", "tmp_cats", "[", "i", "]", "inst_node", "=", "tmp_nodes", "[", "i", "]", "if", "inst_cat", "not", "in", "dict_cat", ":", "dict_cat", "[", "inst_cat", "]", "=", "[", "]", "dict_cat", "[", "inst_cat", "]", ".", "append", "(", "inst_node", ")", "tmp_name", "=", "'dict_'", "+", "inst_name_cat", ".", "replace", "(", "'-'", ",", "'_'", ")", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "tmp_name", "]", "=", "dict_cat", "# merge with old cat_colors by default", "cat_colors", "=", "net", ".", "viz", "[", "'cat_colors'", "]", "if", "define_cat_colors", "==", "True", ":", "cat_number", "=", "0", "for", "inst_rc", "in", "[", "'row'", ",", "'col'", "]", ":", "inst_keys", "=", "list", "(", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", ".", "keys", "(", ")", ")", "all_cats", "=", "[", "x", "for", "x", "in", "inst_keys", "if", "'cat-'", "in", "x", "]", "for", "cat_index", "in", "all_cats", ":", "if", "cat_index", "not", "in", "cat_colors", "[", "inst_rc", "]", ":", "cat_colors", "[", "inst_rc", "]", "[", "cat_index", "]", "=", "{", "}", "cat_names", "=", "sorted", "(", "list", "(", "set", "(", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "cat_index", "]", ")", ")", ")", "# loop through each category name and assign a color", "for", "tmp_name", "in", "cat_names", ":", "# using the same rules as the front-end to define cat_colors", "inst_color", "=", "get_cat_color", "(", "cat_number", "+", "cat_names", ".", "index", "(", "tmp_name", ")", ")", "check_name", "=", "tmp_name", "# check if category is string type and non-numeric", "try", ":", "float", "(", "check_name", ")", "is_string_cat", "=", "False", "except", ":", "is_string_cat", "=", "True", "if", "is_string_cat", "==", "True", ":", "# check for default non-color", "if", "': '", "in", "check_name", ":", "check_name", "=", "check_name", ".", "split", "(", "': '", ")", "[", "1", "]", "# if check_name == 'False' or check_name == 'false':", "if", "'False'", "in", "check_name", "or", "'false'", "in", "check_name", ":", "inst_color", "=", "'#eee'", "if", "'Not '", "in", "check_name", ":", "inst_color", "=", "'#eee'", "# print('cat_colors')", "# print('----------')", "# print(cat_colors[inst_rc][cat_index])", "# do not overwrite old colors", "if", "tmp_name", "not", "in", "cat_colors", "[", "inst_rc", "]", "[", "cat_index", "]", "and", "is_string_cat", ":", "cat_colors", "[", "inst_rc", "]", "[", "cat_index", "]", "[", "tmp_name", "]", "=", "inst_color", "# print('overwrite: ' + tmp_name + ' -> ' + str(inst_color))", "cat_number", "=", "cat_number", "+", "1", "net", ".", "viz", "[", "'cat_colors'", "]", "=", "cat_colors" ]
make a dictionary of node-category associations
[ "make", "a", "dictionary", "of", "node", "-", "category", "associations" ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/categories.py#L39-L131
3,593
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/categories.py
calc_cat_clust_order
def calc_cat_clust_order(net, inst_rc): ''' cluster category subset of data ''' from .__init__ import Network from copy import deepcopy from . import calc_clust, run_filter inst_keys = list(net.dat['node_info'][inst_rc].keys()) all_cats = [x for x in inst_keys if 'cat-' in x] if len(all_cats) > 0: for inst_name_cat in all_cats: tmp_name = 'dict_' + inst_name_cat.replace('-', '_') dict_cat = net.dat['node_info'][inst_rc][tmp_name] unordered_cats = dict_cat.keys() ordered_cats = order_categories(unordered_cats) # this is the ordering of the columns based on their category, not # including their clustering ordering within category all_cat_orders = [] tmp_names_list = [] for inst_cat in ordered_cats: inst_nodes = dict_cat[inst_cat] tmp_names_list.extend(inst_nodes) # cat_net = deepcopy(Network()) # cat_net.dat['mat'] = deepcopy(net.dat['mat']) # cat_net.dat['nodes'] = deepcopy(net.dat['nodes']) # cat_df = cat_net.dat_to_df() # sub_df = {} # if inst_rc == 'col': # sub_df['mat'] = cat_df['mat'][inst_nodes] # elif inst_rc == 'row': # # need to transpose df # cat_df['mat'] = cat_df['mat'].transpose() # sub_df['mat'] = cat_df['mat'][inst_nodes] # sub_df['mat'] = sub_df['mat'].transpose() # # filter matrix before clustering # ################################### # threshold = 0.0001 # sub_df = run_filter.df_filter_row_sum(sub_df, threshold) # sub_df = run_filter.df_filter_col_sum(sub_df, threshold) # # load back to dat # cat_net.df_to_dat(sub_df) # cat_mat_shape = cat_net.dat['mat'].shape # print('***************') # try: # if cat_mat_shape[0]>1 and cat_mat_shape[1] > 1 and all_are_numbers == False: # calc_clust.cluster_row_and_col(cat_net, 'cos') # inst_cat_order = cat_net.dat['node_info'][inst_rc]['clust'] # else: # inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc]))) # except: # inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc]))) # prev_order_len = len(all_cat_orders) # # add prev order length to the current order number # inst_cat_order = [i + prev_order_len for i in inst_cat_order] # all_cat_orders.extend(inst_cat_order) # # generate ordered list of row/col names, which will be used to # # assign the order to specific nodes # names_clust_list = [x for (y, x) in sorted(zip(all_cat_orders, # tmp_names_list))] names_clust_list = tmp_names_list # calc category-cluster order final_order = [] for i in range(len(net.dat['nodes'][inst_rc])): inst_node_name = net.dat['nodes'][inst_rc][i] inst_node_num = names_clust_list.index(inst_node_name) final_order.append(inst_node_num) inst_index_cat = inst_name_cat.replace('-', '_') + '_index' net.dat['node_info'][inst_rc][inst_index_cat] = final_order
python
def calc_cat_clust_order(net, inst_rc): ''' cluster category subset of data ''' from .__init__ import Network from copy import deepcopy from . import calc_clust, run_filter inst_keys = list(net.dat['node_info'][inst_rc].keys()) all_cats = [x for x in inst_keys if 'cat-' in x] if len(all_cats) > 0: for inst_name_cat in all_cats: tmp_name = 'dict_' + inst_name_cat.replace('-', '_') dict_cat = net.dat['node_info'][inst_rc][tmp_name] unordered_cats = dict_cat.keys() ordered_cats = order_categories(unordered_cats) # this is the ordering of the columns based on their category, not # including their clustering ordering within category all_cat_orders = [] tmp_names_list = [] for inst_cat in ordered_cats: inst_nodes = dict_cat[inst_cat] tmp_names_list.extend(inst_nodes) # cat_net = deepcopy(Network()) # cat_net.dat['mat'] = deepcopy(net.dat['mat']) # cat_net.dat['nodes'] = deepcopy(net.dat['nodes']) # cat_df = cat_net.dat_to_df() # sub_df = {} # if inst_rc == 'col': # sub_df['mat'] = cat_df['mat'][inst_nodes] # elif inst_rc == 'row': # # need to transpose df # cat_df['mat'] = cat_df['mat'].transpose() # sub_df['mat'] = cat_df['mat'][inst_nodes] # sub_df['mat'] = sub_df['mat'].transpose() # # filter matrix before clustering # ################################### # threshold = 0.0001 # sub_df = run_filter.df_filter_row_sum(sub_df, threshold) # sub_df = run_filter.df_filter_col_sum(sub_df, threshold) # # load back to dat # cat_net.df_to_dat(sub_df) # cat_mat_shape = cat_net.dat['mat'].shape # print('***************') # try: # if cat_mat_shape[0]>1 and cat_mat_shape[1] > 1 and all_are_numbers == False: # calc_clust.cluster_row_and_col(cat_net, 'cos') # inst_cat_order = cat_net.dat['node_info'][inst_rc]['clust'] # else: # inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc]))) # except: # inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc]))) # prev_order_len = len(all_cat_orders) # # add prev order length to the current order number # inst_cat_order = [i + prev_order_len for i in inst_cat_order] # all_cat_orders.extend(inst_cat_order) # # generate ordered list of row/col names, which will be used to # # assign the order to specific nodes # names_clust_list = [x for (y, x) in sorted(zip(all_cat_orders, # tmp_names_list))] names_clust_list = tmp_names_list # calc category-cluster order final_order = [] for i in range(len(net.dat['nodes'][inst_rc])): inst_node_name = net.dat['nodes'][inst_rc][i] inst_node_num = names_clust_list.index(inst_node_name) final_order.append(inst_node_num) inst_index_cat = inst_name_cat.replace('-', '_') + '_index' net.dat['node_info'][inst_rc][inst_index_cat] = final_order
[ "def", "calc_cat_clust_order", "(", "net", ",", "inst_rc", ")", ":", "from", ".", "__init__", "import", "Network", "from", "copy", "import", "deepcopy", "from", ".", "import", "calc_clust", ",", "run_filter", "inst_keys", "=", "list", "(", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", ".", "keys", "(", ")", ")", "all_cats", "=", "[", "x", "for", "x", "in", "inst_keys", "if", "'cat-'", "in", "x", "]", "if", "len", "(", "all_cats", ")", ">", "0", ":", "for", "inst_name_cat", "in", "all_cats", ":", "tmp_name", "=", "'dict_'", "+", "inst_name_cat", ".", "replace", "(", "'-'", ",", "'_'", ")", "dict_cat", "=", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "tmp_name", "]", "unordered_cats", "=", "dict_cat", ".", "keys", "(", ")", "ordered_cats", "=", "order_categories", "(", "unordered_cats", ")", "# this is the ordering of the columns based on their category, not", "# including their clustering ordering within category", "all_cat_orders", "=", "[", "]", "tmp_names_list", "=", "[", "]", "for", "inst_cat", "in", "ordered_cats", ":", "inst_nodes", "=", "dict_cat", "[", "inst_cat", "]", "tmp_names_list", ".", "extend", "(", "inst_nodes", ")", "# cat_net = deepcopy(Network())", "# cat_net.dat['mat'] = deepcopy(net.dat['mat'])", "# cat_net.dat['nodes'] = deepcopy(net.dat['nodes'])", "# cat_df = cat_net.dat_to_df()", "# sub_df = {}", "# if inst_rc == 'col':", "# sub_df['mat'] = cat_df['mat'][inst_nodes]", "# elif inst_rc == 'row':", "# # need to transpose df", "# cat_df['mat'] = cat_df['mat'].transpose()", "# sub_df['mat'] = cat_df['mat'][inst_nodes]", "# sub_df['mat'] = sub_df['mat'].transpose()", "# # filter matrix before clustering", "# ###################################", "# threshold = 0.0001", "# sub_df = run_filter.df_filter_row_sum(sub_df, threshold)", "# sub_df = run_filter.df_filter_col_sum(sub_df, threshold)", "# # load back to dat", "# cat_net.df_to_dat(sub_df)", "# cat_mat_shape = cat_net.dat['mat'].shape", "# print('***************')", "# try:", "# if cat_mat_shape[0]>1 and cat_mat_shape[1] > 1 and all_are_numbers == False:", "# calc_clust.cluster_row_and_col(cat_net, 'cos')", "# inst_cat_order = cat_net.dat['node_info'][inst_rc]['clust']", "# else:", "# inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc])))", "# except:", "# inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc])))", "# prev_order_len = len(all_cat_orders)", "# # add prev order length to the current order number", "# inst_cat_order = [i + prev_order_len for i in inst_cat_order]", "# all_cat_orders.extend(inst_cat_order)", "# # generate ordered list of row/col names, which will be used to", "# # assign the order to specific nodes", "# names_clust_list = [x for (y, x) in sorted(zip(all_cat_orders,", "# tmp_names_list))]", "names_clust_list", "=", "tmp_names_list", "# calc category-cluster order", "final_order", "=", "[", "]", "for", "i", "in", "range", "(", "len", "(", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", ")", ")", ":", "inst_node_name", "=", "net", ".", "dat", "[", "'nodes'", "]", "[", "inst_rc", "]", "[", "i", "]", "inst_node_num", "=", "names_clust_list", ".", "index", "(", "inst_node_name", ")", "final_order", ".", "append", "(", "inst_node_num", ")", "inst_index_cat", "=", "inst_name_cat", ".", "replace", "(", "'-'", ",", "'_'", ")", "+", "'_index'", "net", ".", "dat", "[", "'node_info'", "]", "[", "inst_rc", "]", "[", "inst_index_cat", "]", "=", "final_order" ]
cluster category subset of data
[ "cluster", "category", "subset", "of", "data" ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/categories.py#L137-L234
3,594
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/__init__.py
Network.load_file_as_string
def load_file_as_string(self, file_string, filename=''): ''' Load file as a string. ''' load_data.load_file_as_string(self, file_string, filename=filename)
python
def load_file_as_string(self, file_string, filename=''): ''' Load file as a string. ''' load_data.load_file_as_string(self, file_string, filename=filename)
[ "def", "load_file_as_string", "(", "self", ",", "file_string", ",", "filename", "=", "''", ")", ":", "load_data", ".", "load_file_as_string", "(", "self", ",", "file_string", ",", "filename", "=", "filename", ")" ]
Load file as a string.
[ "Load", "file", "as", "a", "string", "." ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/__init__.py#L56-L60
3,595
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/__init__.py
Network.load_df
def load_df(self, df): ''' Load Pandas DataFrame. ''' # self.__init__() self.reset() df_dict = {} df_dict['mat'] = deepcopy(df) # always define category colors if applicable when loading a df data_formats.df_to_dat(self, df_dict, define_cat_colors=True)
python
def load_df(self, df): ''' Load Pandas DataFrame. ''' # self.__init__() self.reset() df_dict = {} df_dict['mat'] = deepcopy(df) # always define category colors if applicable when loading a df data_formats.df_to_dat(self, df_dict, define_cat_colors=True)
[ "def", "load_df", "(", "self", ",", "df", ")", ":", "# self.__init__()", "self", ".", "reset", "(", ")", "df_dict", "=", "{", "}", "df_dict", "[", "'mat'", "]", "=", "deepcopy", "(", "df", ")", "# always define category colors if applicable when loading a df", "data_formats", ".", "df_to_dat", "(", "self", ",", "df_dict", ",", "define_cat_colors", "=", "True", ")" ]
Load Pandas DataFrame.
[ "Load", "Pandas", "DataFrame", "." ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/__init__.py#L148-L158
3,596
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/__init__.py
Network.widget_df
def widget_df(self): ''' Export a DataFrame from the front-end visualization. For instance, a user can filter to show only a single cluster using the dendrogram and then get a dataframe of this cluster using the widget_df method. ''' if hasattr(self, 'widget_instance') == True: if self.widget_instance.mat_string != '': tmp_net = deepcopy(Network()) df_string = self.widget_instance.mat_string tmp_net.load_file_as_string(df_string) df = tmp_net.export_df() return df else: return self.export_df() else: if hasattr(self, 'widget_class') == True: print('Please make the widget before exporting the widget DataFrame.') print('Do this using the widget method: net.widget()') else: print('Can not make widget because Network has no attribute widget_class') print('Please instantiate Network with clustergrammer_widget using: Network(clustergrammer_widget)')
python
def widget_df(self): ''' Export a DataFrame from the front-end visualization. For instance, a user can filter to show only a single cluster using the dendrogram and then get a dataframe of this cluster using the widget_df method. ''' if hasattr(self, 'widget_instance') == True: if self.widget_instance.mat_string != '': tmp_net = deepcopy(Network()) df_string = self.widget_instance.mat_string tmp_net.load_file_as_string(df_string) df = tmp_net.export_df() return df else: return self.export_df() else: if hasattr(self, 'widget_class') == True: print('Please make the widget before exporting the widget DataFrame.') print('Do this using the widget method: net.widget()') else: print('Can not make widget because Network has no attribute widget_class') print('Please instantiate Network with clustergrammer_widget using: Network(clustergrammer_widget)')
[ "def", "widget_df", "(", "self", ")", ":", "if", "hasattr", "(", "self", ",", "'widget_instance'", ")", "==", "True", ":", "if", "self", ".", "widget_instance", ".", "mat_string", "!=", "''", ":", "tmp_net", "=", "deepcopy", "(", "Network", "(", ")", ")", "df_string", "=", "self", ".", "widget_instance", ".", "mat_string", "tmp_net", ".", "load_file_as_string", "(", "df_string", ")", "df", "=", "tmp_net", ".", "export_df", "(", ")", "return", "df", "else", ":", "return", "self", ".", "export_df", "(", ")", "else", ":", "if", "hasattr", "(", "self", ",", "'widget_class'", ")", "==", "True", ":", "print", "(", "'Please make the widget before exporting the widget DataFrame.'", ")", "print", "(", "'Do this using the widget method: net.widget()'", ")", "else", ":", "print", "(", "'Can not make widget because Network has no attribute widget_class'", ")", "print", "(", "'Please instantiate Network with clustergrammer_widget using: Network(clustergrammer_widget)'", ")" ]
Export a DataFrame from the front-end visualization. For instance, a user can filter to show only a single cluster using the dendrogram and then get a dataframe of this cluster using the widget_df method.
[ "Export", "a", "DataFrame", "from", "the", "front", "-", "end", "visualization", ".", "For", "instance", "a", "user", "can", "filter", "to", "show", "only", "a", "single", "cluster", "using", "the", "dendrogram", "and", "then", "get", "a", "dataframe", "of", "this", "cluster", "using", "the", "widget_df", "method", "." ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/__init__.py#L230-L261
3,597
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/__init__.py
Network.write_json_to_file
def write_json_to_file(self, net_type, filename, indent='no-indent'): ''' Save dat or viz as a JSON to file. ''' export_data.write_json_to_file(self, net_type, filename, indent)
python
def write_json_to_file(self, net_type, filename, indent='no-indent'): ''' Save dat or viz as a JSON to file. ''' export_data.write_json_to_file(self, net_type, filename, indent)
[ "def", "write_json_to_file", "(", "self", ",", "net_type", ",", "filename", ",", "indent", "=", "'no-indent'", ")", ":", "export_data", ".", "write_json_to_file", "(", "self", ",", "net_type", ",", "filename", ",", "indent", ")" ]
Save dat or viz as a JSON to file.
[ "Save", "dat", "or", "viz", "as", "a", "JSON", "to", "file", "." ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/__init__.py#L263-L267
3,598
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/__init__.py
Network.filter_sum
def filter_sum(self, inst_rc, threshold, take_abs=True): ''' Filter a network's rows or columns based on the sum across rows or columns. ''' inst_df = self.dat_to_df() if inst_rc == 'row': inst_df = run_filter.df_filter_row_sum(inst_df, threshold, take_abs) elif inst_rc == 'col': inst_df = run_filter.df_filter_col_sum(inst_df, threshold, take_abs) self.df_to_dat(inst_df)
python
def filter_sum(self, inst_rc, threshold, take_abs=True): ''' Filter a network's rows or columns based on the sum across rows or columns. ''' inst_df = self.dat_to_df() if inst_rc == 'row': inst_df = run_filter.df_filter_row_sum(inst_df, threshold, take_abs) elif inst_rc == 'col': inst_df = run_filter.df_filter_col_sum(inst_df, threshold, take_abs) self.df_to_dat(inst_df)
[ "def", "filter_sum", "(", "self", ",", "inst_rc", ",", "threshold", ",", "take_abs", "=", "True", ")", ":", "inst_df", "=", "self", ".", "dat_to_df", "(", ")", "if", "inst_rc", "==", "'row'", ":", "inst_df", "=", "run_filter", ".", "df_filter_row_sum", "(", "inst_df", ",", "threshold", ",", "take_abs", ")", "elif", "inst_rc", "==", "'col'", ":", "inst_df", "=", "run_filter", ".", "df_filter_col_sum", "(", "inst_df", ",", "threshold", ",", "take_abs", ")", "self", ".", "df_to_dat", "(", "inst_df", ")" ]
Filter a network's rows or columns based on the sum across rows or columns.
[ "Filter", "a", "network", "s", "rows", "or", "columns", "based", "on", "the", "sum", "across", "rows", "or", "columns", "." ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/__init__.py#L275-L284
3,599
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/__init__.py
Network.filter_cat
def filter_cat(self, axis, cat_index, cat_name): ''' Filter the matrix based on their category. cat_index is the index of the category, the first category has index=1. ''' run_filter.filter_cat(self, axis, cat_index, cat_name)
python
def filter_cat(self, axis, cat_index, cat_name): ''' Filter the matrix based on their category. cat_index is the index of the category, the first category has index=1. ''' run_filter.filter_cat(self, axis, cat_index, cat_name)
[ "def", "filter_cat", "(", "self", ",", "axis", ",", "cat_index", ",", "cat_name", ")", ":", "run_filter", ".", "filter_cat", "(", "self", ",", "axis", ",", "cat_index", ",", "cat_name", ")" ]
Filter the matrix based on their category. cat_index is the index of the category, the first category has index=1.
[ "Filter", "the", "matrix", "based", "on", "their", "category", ".", "cat_index", "is", "the", "index", "of", "the", "category", "the", "first", "category", "has", "index", "=", "1", "." ]
5acea9bff7eda546cf0647b9e3647f631eb6f5f5
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/__init__.py#L309-L313