from error import error import math as m class Variable: def __init__(self, name: str, bytes: int, startByte: int, type: int): self.name: str = name self.bytes: int = bytes self.startByte: int = startByte self.type: int = type def __repr__(self) -> str: return f"[{self.name}, {self.bytes}, {self.startByte}, {self.type}]" variables: list[Variable] = [] bytesum: int = 0 types = { "bool": 0, "string": 1, "int": 2, } bool = { "true": 1, "false": 0, } def generateCode(line: list[str], offset: int) -> str: try: bytesum = variables[-1].startByte + variables[-1].bytes except IndexError: bytesum = 0 keyword = line[0] if keyword == 'create': if len(line) != 5: error(f"Create command requires 4 arguments (got {len(line)-1})") name = line[1] try: type = types[line[2]] except KeyError: error(f"Invalid type: {line[2]}") quit() bytes = int(line[3]) value = line[4] if type == 0: variables.append(Variable(name, bytes, bytesum, type)) if (value == 'true'): return '>' * offset + '[>]' + '>' * (bytesum + 1) + '+' + '<' * (bytesum + 1) + '<[<]' + '<' * (offset - 1) elif (value == 'false'): return '' else: error(f'Invalid bool: {value}') elif type == 1: variables.append(Variable(name, bytes, bytesum, type)) returnval: str = '>' * offset + '[>]' + '>' * (bytesum + 1) for i in range(bytes): returnval += '+' * ord(value[i]) + '>' returnval += '<' * (bytes+bytesum+2) + '[<]' + '<' * (offset - 1) return returnval elif type == 2: # I hate integers error('Integers are not yet implemented') else: error(f"Type {type} not yet implemented") error() quit() elif keyword == 'print': if len(line) != 2: error(f"Print command requires 1 argument (got {len(line)-1})") # Find value of variable var: Variable | str = '' for v in variables: if v.name == line[1]: var = v if var == '': error(f'Could not find variable {line[1]}') quit() print(var.startByte) if var.type == 0: # Create copy returnval = '>' * offset + '[>]' + '>' * (var.startByte + 1) + '[' + '<' * (var.startByte+2) + '[<]<+<+>>>' + '[>]>' + '>' * var.startByte + '-]' + '<' * (var.startByte + 2) + '[<]<' + '[>>[>]' + '>' * (var.startByte + 1) + '+' + '<' * (var.startByte + 2) + '[<]<-]' # If true returnval += '+<[>-<' + '+' * 115 + '.--.+++.----------------.[-]' + ']' # else false returnval += '>[' + '+' * 101 + '.-----.+++++++++++.+++++++.--------------.[-]' + ']<<<' return returnval elif var.type == 1: return '>' * offset + '[>]' + '>' * (var.startByte + 1) + '.>' * var.bytes + '<' * (var.startByte+var.bytes+2) + '[<]' + '<' * (offset - 1) else: error('Type not yet supported') quit() else: error(f"Invalid token: {keyword}") quit()