1. Precedence testing
- Posted by petelomax Oct 21, 2020
- 1247 views
- Last edited Nov 07, 2020
I've added six new operators to phix:
&& - bitwise and, same as and_bits(lhs,rhs) || - bitwise or, same as or_bits(lhs,rhs) >> - shift right, same as floor(lhs/power(2,rhs)) << - shift left, same as lhs*power(2,rhs) ~ - same as length(rhs) [unary] . - subscripts and/or struct/class members
and obviously they deserve proper precedence handling. At the same time, I'm going to split == and != to have a slightly lower precedence to < > <= and >= (in line with the rest of the planet, bar Python).
Of course that means I could easily spanner things, and realised the test suite does not actually have a proper set of precedence-related tests, so I'm about to pen a new tests/t66prec.exw and populate it with things like:
2+3*4 is 2 + (3 * 4) = 14, not (2 + 3) * 4 = 20. 2*3+4 is (2 * 3) + 4 = 10, not 2 * (3 + 4) = 14. 1-2+3 is (1 - 2) + 3 = 2, not 1 - (2 + 3) = -4
and figured I may as well ask on here for any similar examples anyone thinks should be added.
The new precedence table will be:
highest precedence: parenthesis/function/type calls/ternary operator subscripts/slices . unary- unary+ not ~ * / + - >> << & < > <= >= = != && || and or xor lowest precedence: { , , , }
Note that phix does not really do associativity handling: of course unary operators are right associative, because they have to be, but everything else is left associative. For example
a*b/c*d/e is ((((a*b)/c)*d)/e) a*-b/c*not d/e is ((((a*(-b))/c)*(not d))/e)
So, if you've got any (perhaps slightly less obvious) code snippets you'd rather not see broken, or any other comments, post 'em here!
2. Re: Precedence testing
- Posted by Spock Oct 22, 2020
- 1197 views
I've added six new operators to phix:
&& - bitwise and, same as and_bits(lhs,rhs) || - bitwise or, same as or_bits(lhs,rhs) >> - shift right, same as floor(lhs/power(2,rhs)) << - shift left, same as lhs*power(2,rhs) ~ - same as length(rhs) [unary] . - subscripts and/or struct/class members
...
This might tempt me to drop Eu 2010 in favour of Phix 2020. What about about !! and ^^ for not_bits() & xor_bits() ? Presumably the equivalent assignment operators will be there as well. For c2ypt0 apps perhaps >>> and <<<? Mind you, a decent integrated assembler would make such bit-twiddling tricks redundant. I have about 90 ASM fragments in my base library to speed up various graphics functions. I use fasm4e but the calling overhead dominates for small functions. Eg, the following ASM code is only about 30% faster than Eu.
"mov eax, rgb", "bswap eax", "shr eax, 8", "ret eax"
[Orac code follows] -- Use for when a colour value is BGR but you need to convert it to RGB (or vice versa) int b = (rgb && #FF) * 65536 int g = rgb && #FF00 int r = floor((rgb && #FF0000)/65536) return b || g || r
How does Phix compare for context switching?
Spock
3. Re: Precedence testing
- Posted by petelomax Oct 22, 2020
- 1223 views
What about about !! and ^^ for not_bits() & xor_bits() ? For c2ypt0 apps perhaps >>> and <<< ?
Sorry, not yet in. &&= and ||= are already in, but not <<= and >>=.
I suspect a bitwise not should really be `~~` instead of `!!`, which has already found some real-world use in other languages, as a logical "not not".
Are 32-bit rotates still 32-bit rotates on 64-bit? anyway, asm:
How does Phix compare for context switching?
Very well, especially when inline. In fact just a couple of weeks ago I penned this which boosted performance compared to non-asm by 90%, although I ended up with something 1000 times better even than that.
A hll call/return won't be so great, but you can define a global label, eg #ilASM{ :!myGlobal ... ret} and #ilASM{ call myGlobal }, which is pretty much what quite a few of the "true builtins" do.
PS I actually managed to make a copy of Orac run on Phix - not very stable and probably still riddled with "illegal-sequence-ops" though, plus I suspect you'd be way better off modifying phix directly than persisting with that approach.
4. Re: Precedence testing
- Posted by petelomax Nov 07, 2020
- 1095 views
Just to let y'all know: I finally got the new precedence levels in, all settled and working, took about ten times longer than expected, but alls well..