This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Xenthera's profile picture

Convert bitwise operators to bit api calls

Started by Xenthera, 02 February 2019 - 10:01 PM
Xenthera #1
Posted 02 February 2019 - 11:01 PM
Hello Lua aficionados. Does anyone know of a way/program to convert lua 5.3 bitwise operators to a lua <= 5.2 corresponding bit api call?

Example:


self.pc = self.core.mem_read(self, ((self.pc + 1) &amp; 65535)) | (self.core.mem_read(self, ((self.pc + 2) &amp; 65535)) << 8)

- to -

self.pc = self.core.mem_read(self, bit32.bor(bit32.band((self.pc + 1), 65535)), (self.core.mem_read(self, bit32.rshift(bit32.band((self.pc + 2), 65535), 8))))

Not asking anyone to write a program to do this, just inquiring whether one exists.
I'd just do it manually but I have about 3000 lines of code i'm working with and need to make it compatible with computercraft.

Thanks for any help.

-X
SquidDev #2
Posted 02 February 2019 - 11:43 PM
I had some luck with running the code through LuaFmt with a patch to the printer in order print bitops as calls to the corresponding functions. It's super ugly, but seemed to work OK. Be warned - I haven't used this in 6 months, so no clue whether it's still functional.
Xenthera #3
Posted 03 February 2019 - 12:11 AM
Thanks, i'll give it a try!
Xenthera #4
Posted 03 February 2019 - 07:50 AM
So i'm not terribly familiar with github patches, especially when applying them to node projects. Can you help me out applying it?
SquidDev #5
Posted 03 February 2019 - 02:35 PM
So i'm not terribly familiar with github patches, especially when applying them to node projects. Can you help me out applying it?
If you cloned the repo using git, you should be able to do git apply printer.ts.patch (assuming you've saved it as that). Otherwise, it's probably going to be easier to apply it manually. Open the src/printer.ts file and look around for the printNodeNoParens function (should be line 104). You then just want to copy the two blocks of changes to roughly where they look like they belong in the original file.
Xenthera #6
Posted 03 February 2019 - 04:27 PM
So i'm not terribly familiar with github patches, especially when applying them to node projects. Can you help me out applying it?
If you cloned the repo using git, you should be able to do git apply printer.ts.patch (assuming you've saved it as that). Otherwise, it's probably going to be easier to apply it manually. Open the src/printer.ts file and look around for the printNodeNoParens function (should be line 104). You then just want to copy the two blocks of changes to roughly where they look like they belong in the original file.

So I was able to get it to work, however is there a reason &amp; isn't included? I'm about to add it and see what happens.. I was just curious as to its omission.
SquidDev #7
Posted 03 February 2019 - 04:31 PM
So I was able to get it to work, however is there a reason &amp; isn't included?
I forgot about it! I suspect the program I used this for didn't end up using it, otherwise I'd have noticed this at the time.
Xenthera #8
Posted 03 February 2019 - 07:26 PM
Thanks for your help it was exactly what I needed. Would've taken me days otherwise.
Currently working on a Z80 emulator written in lua…