diff --git a/lang/compile.py b/lang/compile.py index b503048..9ff9cf4 100755 --- a/lang/compile.py +++ b/lang/compile.py @@ -243,18 +243,20 @@ return 'IRSelf' class IRLoad(): - def __init__(self, variable): + def __init__(self, variable, pos): self.variable = variable + self.pos = pos def __repr__(self): - return 'IRLoad(variable=%s)' % (self.variable) + return 'IRLoad(variable=%s, pos=%i)' % (self.variable, self.pos) class IRStore(): - def __init__(self, variable): + def __init__(self, variable, pos): self.variable = variable + self.pos = pos def __repr__(self): - return 'IRStore(variable=%s)' % (self.variable) + return 'IRStore(variable=%s, pos=%i)' % (self.variable, self.pos) class IRCall(): def __init__(self, name, args): @@ -297,7 +299,7 @@ if value.name == "Self": return [IRSelf()] else: - return [IRLoad(value.name)] + return [IRLoad(value.name, None)] else: print("Unknown value ast node: %s" % (node)) return None @@ -332,7 +334,7 @@ if not sub_ir: print("Unknown set ast node: %s" % (node)) return None - store = IRStore(ast.name) + store = IRStore(ast.name, None) return sub_ir + [store] def generate_ir_return(ast): @@ -340,7 +342,7 @@ if not value_ir: print("Unknown return ast node: %s" % (node)) return None - store_ret = IRStore("Return") + store_ret = IRStore("Return", None) ret = IRReturn() return value_ir + [store_ret, ret] @@ -407,10 +409,10 @@ for node in ir.statements: if isinstance(node, IRLoad): reg = variables[node.variable] - new_ir.append(IRLoad(reg)) + new_ir.append(IRLoad(node.variable, reg)) elif isinstance(node, IRStore): reg = variables[node.variable] - new_ir.append(IRStore(reg)) + new_ir.append(IRStore(node.variable, reg)) continue elif isinstance(node, IRCall): new_ir.append(node) @@ -468,11 +470,11 @@ bytes += b"\x08" # OP_DROP bytes += node.size.to_bytes(1) elif isinstance(node, IRLoad): - index = int(node.variable) + index = int(node.pos) bytes += b"\x06" # OP_GET bytes += index.to_bytes(1) elif isinstance(node, IRStore): - index = int(node.variable) + index = int(node.pos) bytes += b"\x07" # OP_SET bytes += index.to_bytes(1) elif isinstance(node, IRCall):