def __mod__(self, other): if isinstance(other, (Integer, Float)): return Float(self.value % other.value) else: raise FXException("Can't calculate the modulo of a Float with a " + str(type(other)))
def __floordiv__(self, other): if isinstance(other, (Integer, Float)): return Float(self.value // other.value) else: raise FXException("Can't floor divide Float by " + str(type(other)))
def __sub__(self, other): if isinstance(other, (Integer, Float)): return Float(self.value - other.value) else: raise FXException("Can't subtract " + str(type(other)) + " from Float")
def eval(self, cell, attrib, config, cell_dict): # TODO if (self.name == "R"): return Integer(cell.row) elif (self.name == "C"): return Integer(cell.col) elif (self.name == "true"): return Boolean(True) elif (self.name == "false"): return Boolean(False) else: raise FXException("Unknown variable: " + self.name) ranges = file.File.parse_cell_ranges(self.name) if (len(ranges) > 1): raise FXException("Only 1 range can be specified as variable") coordinates = ranges[0].get_coordinates() if (len(coordinates) != 1): raise FXException("Only 1 cell can be specified as variable")
def __pow__(self, other): if self.value < 0: if isinstance(other, Integer): return Float(self.value**other.value) else: raise FXException( "Can't raise a (negative) Float to the power of a " + str(type(other))) else: if isinstance(other, (Float, Integer)): return Float(self.value**other.value) else: raise FXException("Can't raise a Float to the power of a " + str(type(other)))
def eval(self, cell, attrib, config, cell_dict): if self.op == '-': return -self.elem.eval(cell, attrib, config, cell_dict) elif self.op == '+': return +self.elem.eval(cell, attrib, config, cell_dict) elif self.op == 'not': return not self.elem.eval(cell, attrib, config, cell_dict) else: raise FXException()
def __truediv__(self, other): if isinstance(other, Integer): return Integer(self.value / other.value) elif isinstance(other, Float): return Float(self.value / other.value) else: raise FXException("Can't divide Integer by " + str(type(other)))
def __add__(self, other): if isinstance(other, Integer): return Integer(self.value + other.value) elif isinstance(other, Float): return Float(self.value + other.value) else: raise FXException("Can't add Integer to " + str(type(other)))
def __mul__(self, other): if isinstance(other, Integer): return Integer(self.value * other.value) elif isinstance(other, Float): return Float(self.value * other.value) else: raise FXException("Can't multiply Integer with " + str(type(other)))
def eval_condition(self, lst, cell, attrib, config, cell_dict): if not lst: raise FXException("If statements need an else clause") if len(lst) == 1: return lst[0].eval(cell, attrib, config, cell_dict) else: if lst[0].eval(cell, attrib, config, cell_dict): return lst[1].eval(cell, attrib, config, cell_dict) else: return self.eval_condition(lst[2:], cell, attrib, config, cell_dict)
def __init__(self, path): self.path = path self.statements = [] self.attrib = None self.expressions = {} if 'config.fx' != os.path.basename(self.path): try: self.attrib = constants.attrib_dict[os.path.basename(path) [:-3]] except KeyError: raise FXException('Invalid file name: ' + os.path.basename(path))
def parse_cell_ranges(text): text = ''.join(text.split()) cell_ranges = [] cell_ranges_str = text.split(',') for cell_range_str in cell_ranges_str: matches = re.findall(r"([rcRC][0-9]+(:[0-9]+)?)", cell_range_str) matches = {s[0][0].upper(): s[0][1:] for s in matches} if len(matches) == 0: raise FXException("Invalid syntax") cell_ranges.append(CellRange(matches.get('R'), matches.get('C'))) return cell_ranges
def parse(self): if not 'config.fx' in os.listdir(self.directory): raise FXException('Every FRuTeX project needs a config file') fx_files = [filename for filename in os.listdir(self.directory) if os.path.splitext(filename)[1] == '.fx' and os.path.basename(filename) != 'config.fx'] self.config = Config(self.directory + '/config.fx') self.config.parse() files = {} for filename in fx_files: file = File(self.directory + '/' + filename) file.parse() files.update({file.attrib: file}) self.files = files for file in self.files.values(): file.apply_statements(self.config, self.cell_dict, None)
def __init__(self, args, dependent_info): if type(args) != list or len(args) != 1: raise FXException("Illegal arguments to getColor(): " + str(args)) super().__init__(args, dependent_info)
def __ne__(self, other): if not isinstance(other, (Integer, Float)): raise FXException("Can't compare Integer to " + str(type(other))) return Boolean(self.value != other.value)