refactoring: switched to match/case

wow! python actually added switch cases! too bad this is just syntax sugar...
This commit is contained in:
CPunch 2023-12-09 12:01:04 -06:00
parent a22aa808e0
commit df8e9f7e83
3 changed files with 123 additions and 122 deletions

View File

@ -322,48 +322,48 @@ class LuaDecomp:
def parseInstr(self): def parseInstr(self):
instr = self.__getCurrInstr() instr = self.__getCurrInstr()
# python, add switch statements *please* match instr.opcode:
if instr.opcode == Opcodes.MOVE: # move is a fake ABC instr, C is ignored case Opcodes.MOVE: # move is a fake ABC instr, C is ignored
# move registers # move registers
self.__setReg(instr.A, self.__getReg(instr.B)) self.__setReg(instr.A, self.__getReg(instr.B))
elif instr.opcode == Opcodes.LOADK: case Opcodes.LOADK:
self.__setReg(instr.A, self.chunk.getConstant(instr.B).toCode()) self.__setReg(instr.A, self.chunk.getConstant(instr.B).toCode())
elif instr.opcode == Opcodes.LOADBOOL: case Opcodes.LOADBOOL:
if instr.B == 0: if instr.B == 0:
self.__setReg(instr.A, "false") self.__setReg(instr.A, "false")
else: else:
self.__setReg(instr.A, "true") self.__setReg(instr.A, "true")
elif instr.opcode == Opcodes.GETGLOBAL: case Opcodes.GETGLOBAL:
self.__setReg(instr.A, self.chunk.getConstant(instr.B).data) self.__setReg(instr.A, self.chunk.getConstant(instr.B).data)
elif instr.opcode == Opcodes.GETTABLE: case Opcodes.GETTABLE:
self.__setReg(instr.A, self.__getReg(instr.B) + "[" + self.__readRK(instr.C) + "]") self.__setReg(instr.A, self.__getReg(instr.B) + "[" + self.__readRK(instr.C) + "]")
elif instr.opcode == Opcodes.SETGLOBAL: case Opcodes.SETGLOBAL:
self.__addExpr(self.chunk.getConstant(instr.B).data + " = " + self.__getReg(instr.A)) self.__addExpr(self.chunk.getConstant(instr.B).data + " = " + self.__getReg(instr.A))
self.__endStatement() self.__endStatement()
elif instr.opcode == Opcodes.SETTABLE: case Opcodes.SETTABLE:
self.__addExpr(self.__getReg(instr.A) + "[" + self.__readRK(instr.B) + "] = " + self.__readRK(instr.C)) self.__addExpr(self.__getReg(instr.A) + "[" + self.__readRK(instr.B) + "] = " + self.__readRK(instr.C))
self.__endStatement() self.__endStatement()
elif instr.opcode == Opcodes.NEWTABLE: case Opcodes.NEWTABLE:
self.__parseNewTable(instr.A) self.__parseNewTable(instr.A)
elif instr.opcode == Opcodes.ADD: case Opcodes.ADD:
self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " + ") self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " + ")
elif instr.opcode == Opcodes.SUB: case Opcodes.SUB:
self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " - ") self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " - ")
elif instr.opcode == Opcodes.MUL: case Opcodes.MUL:
self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " * ") self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " * ")
elif instr.opcode == Opcodes.DIV: case Opcodes.DIV:
self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " / ") self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " / ")
elif instr.opcode == Opcodes.MOD: case Opcodes.MOD:
self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " % ") self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " % ")
elif instr.opcode == Opcodes.POW: case Opcodes.POW:
self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " ^ ") self.__emitOperand(instr.A, self.__readRK(instr.B), self.__readRK(instr.C), " ^ ")
elif instr.opcode == Opcodes.UNM: case Opcodes.UNM:
self.__setReg(instr.A, "-" + self.__getReg(instr.B)) self.__setReg(instr.A, "-" + self.__getReg(instr.B))
elif instr.opcode == Opcodes.NOT: case Opcodes.NOT:
self.__setReg(instr.A, "not " + self.__getReg(instr.B)) self.__setReg(instr.A, "not " + self.__getReg(instr.B))
elif instr.opcode == Opcodes.LEN: case Opcodes.LEN:
self.__setReg(instr.A, "#" + self.__getReg(instr.B)) self.__setReg(instr.A, "#" + self.__getReg(instr.B))
elif instr.opcode == Opcodes.CONCAT: case Opcodes.CONCAT:
count = instr.C-instr.B+1 count = instr.C-instr.B+1
concatStr = "" concatStr = ""
@ -372,20 +372,20 @@ class LuaDecomp:
concatStr += self.__getReg(instr.B + i) + (" .. " if not i == count - 1 else "") concatStr += self.__getReg(instr.B + i) + (" .. " if not i == count - 1 else "")
self.__setReg(instr.A, concatStr) self.__setReg(instr.A, concatStr)
elif instr.opcode == Opcodes.JMP: case Opcodes.JMP:
pass pass
elif instr.opcode == Opcodes.EQ: case Opcodes.EQ:
self.__condJmp(" == ") self.__condJmp(" == ")
elif instr.opcode == Opcodes.LT: case Opcodes.LT:
self.__condJmp(" < ") self.__condJmp(" < ")
elif instr.opcode == Opcodes.LE: case Opcodes.LE:
self.__condJmp(" <= ") self.__condJmp(" <= ")
elif instr.opcode == Opcodes.TEST: case Opcodes.TEST:
if instr.C == 0: if instr.C == 0:
self.__condJmp("", False) self.__condJmp("", False)
else: else:
self.__condJmp("not ", False) self.__condJmp("not ", False)
elif instr.opcode == Opcodes.CALL: case Opcodes.CALL:
preStr = "" preStr = ""
callStr = "" callStr = ""
ident = "" ident = ""
@ -415,15 +415,15 @@ class LuaDecomp:
self.__addExpr(preStr + callStr) self.__addExpr(preStr + callStr)
self.__endStatement() self.__endStatement()
elif instr.opcode == Opcodes.RETURN: case Opcodes.RETURN:
self.__endStatement() self.__endStatement()
pass # no-op for now pass # no-op for now
elif instr.opcode == Opcodes.FORLOOP: case Opcodes.FORLOOP:
pass # no-op for now pass # no-op for now
elif instr.opcode == Opcodes.FORPREP: case Opcodes.FORPREP:
self.__addExpr("for %s = %s, %s, %s " % (self.__getLocal(instr.A+3), self.__getReg(instr.A), self.__getReg(instr.A + 1), self.__getReg(instr.A + 2))) self.__addExpr("for %s = %s, %s, %s " % (self.__getLocal(instr.A+3), self.__getReg(instr.A), self.__getReg(instr.A + 1), self.__getReg(instr.A + 2)))
self.__startScope("do", self.pc, instr.B) self.__startScope("do", self.pc, instr.B)
elif instr.opcode == Opcodes.SETLIST: case Opcodes.SETLIST:
# LFIELDS_PER_FLUSH (50) is the number of elements that *should* have been set in the list in the *last* SETLIST # LFIELDS_PER_FLUSH (50) is the number of elements that *should* have been set in the list in the *last* SETLIST
# eg. # eg.
# [ 49] LOADK : R[49] K[1] ; load 0.0 into R[49] # [ 49] LOADK : R[49] K[1] ; load 0.0 into R[49]
@ -439,8 +439,8 @@ class LuaDecomp:
for i in range(numElems): for i in range(numElems):
self.__addExpr("%s[%d] = %s" % (ident, (startAt + i + 1), self.__getReg(instr.A + i + 1))) self.__addExpr("%s[%d] = %s" % (ident, (startAt + i + 1), self.__getReg(instr.A + i + 1)))
self.__endStatement() self.__endStatement()
elif instr.opcode == Opcodes.CLOSURE: case Opcodes.CLOSURE:
proto = LuaDecomp(self.chunk.protos[instr.B], headChunk=False, scopeOffset=len(self.scope)) proto = LuaDecomp(self.chunk.protos[instr.B], headChunk=False, scopeOffset=len(self.scope))
self.__setReg(instr.A, proto.getPseudoCode()) self.__setReg(instr.A, proto.getPseudoCode())
else: case _:
raise Exception("unsupported instruction: %s" % instr.toString()) raise Exception("unsupported instruction: %s" % instr.toString())

1
main.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
import sys import sys
import lundump import lundump
import lparser import lparser