expr icon indicating copy to clipboard operation
expr copied to clipboard

Bitwise operators

Open munnik opened this issue 4 years ago • 6 comments

Added bitwise operators the the package

munnik avatar Mar 21 '21 09:03 munnik

Nice, but what is reason to add such? And how casting will work? Example, Int16 & uint32?

antonmedv avatar Mar 21 '21 11:03 antonmedv

Reason: I use expr mainly to parse Modbus data, typical expression I would like to use are:

273.15 + (registers[0] << 16 | registers[1]) / 1000.0 (registers[0] & 8) == 8

The first one is now solved with:

273.15 + (registers[0] * 65536 + registers[1]) / 1000.0

but I don't know an easy way to solve the second one. Besides that the first expressions are easier to understand.

Casting: don't really now how to solve that in a clean way. Now you can only use unsigned integer types. Technically it wouldn't matter to expect signed integers as well but I think it gives unexpected behaviors for the binary not operator.

munnik avatar Mar 21 '21 12:03 munnik

I see. Let me test your solution. Also lolike ci failed.

antonmedv avatar Mar 21 '21 19:03 antonmedv

Hi @munnik

Adding this snippet,

case OpLeftShift:
	code("OpLeftShift")

case OpRightShift:
	code("OpRightShift")

case OpBitwiseAnd:
	code("OpBitwiseAnd")

case OpBitwiseXor:
	code("OpBitwiseXor")

case OpBitwiseOr:
	code("OpBitwiseOr")

case OpBitwiseNot:
	code("OpBitwiseNot")

after,

https://github.com/antonmedv/expr/blob/2c1881a9909453f9f1047886d9be0b94e0ba5c48/vm/program.go#L145-L147

should pass the tests. It wasn't able to resolve the opcodes for disassembly of bitwise operators.

aaditya-panik avatar Apr 01 '21 12:04 aaditya-panik

Hi @aaditya-panik,

Thank you for the suggestion, it fixed the tests.

munnik avatar Apr 06 '21 08:04 munnik

Hi @munnik, @antonmedv

I noticed that using numeric literals for bitwise operations seems to throw an error because they are parsed as int and the operations check for uint type. I have another branch which I had used to fix this (https://github.com/aaditya-panik/expr/commit/d24c08d94a400a11db1660531ab28fee3d4c1c23), let me know what you think. (Yet to add some tests)

aaditya-panik avatar Apr 07 '21 11:04 aaditya-panik

Is it still actial? Not sure we want to add bitwise ops here.

antonmedv avatar Oct 16 '22 20:10 antonmedv