Jane - Precedence
Change of JavaScript Precedence

 

Working with the compiler to validate operator precedence, it became obvious that the order of operations was somewhat out of order.  It seems that the JavaScript order of precedence was application dependent, which required some thinking. I was required to read documentation to determine what was going on.  Here are a few cases:

Case 1:

Bitwise operators ( | and &) and logical operators (== === !== !===) have lower precedence than the equality operators.

(1 | 2 ==  3)   result = 1  not true as I expected.  This was done as an application specific requirement to treat existence of a variable as a boolean. This is great, except it is broken when a value contains a zero value.   A = 0;  if (A) alert(true);

JavaScript: (by precedence)

1 | 2 ==  3

2 == 3  = false      (== first by higher precedence)
1 | false  = 1         which is really (1 | 0)  ( bitwise or is second by lower precedence)

Jane: (change precedence) (treat bitwise operators as math +/- operators, at the same precedence level)

1 | 2 ==  3

1 | 2   =  3             (bitwise OR is a math operation, which should be done first)
3 == 3  = true       (== done after all math operations, by my thinking)

Case 2:

Logical Operators AND (&&) has a lower precedence than OR (||).

(true || true && false)   result is true not false as expected. I would expect the same precedence level going left to right. This is not the case.

JavaScript: (by precedence)

true || true && false

true && false = false
true || false = true

Jane: (by order)

true || true && false

true || true  = true
true && false =  false

 

Case 3:

Bitwise AND ,XOR , OR  and SHIFT each having difference precedence. I will make them all on the same level as addition and subtraction, forcing explicit coding. Quit having to look at documentation to be able to read the code, and to determine if this is what the programmer intended, did the programmer make a mistake (I made this mistake).

The order of addition, subtract and multiple, divide have a history in mathematics and are grandfathered in. The order of bitwise OR, AND, XOR and SHIFT have no such provenance. To have them on four different order of execution is extreme. Especially since they are very rarely used, and even more rare to be used together.

JavaScript (by precedence)

7 | 5 + 3  =  15

(5 + 3) = 8
(7 | 8) = 15

Jane: (by order)

7 | 5 + 3 = 10

(7  |  5) = 7
(7 + 3) = 10

Case 4:

Change the value of false to be a boolean not a value of 0. JavaScript treats all logical operations as a numeric compare, true = 1 and false = 0. 

A = 1;
if (A == true) alert('true'); else alert('false');     
TRUE  (This will be an invalid statement in Jane)

A = 0;
if (A == false) alert('true') ; else alert('false');   
TRUE (This will be an invalid statement in Jane)

A = 5;
if (A == true) alert('true') ; else alert('false');      
FALSE (This will be an invalid statement in Jane)
if (A) alert('true') ; else alert('false');                   
TRUE  (a change in compiler logic, if A is true then why does A not equal true)

Jane will make logical compares of a boolean to other data types an invalid statement.  So 1 == true will be invalid. The objective is to make code readable. I cannot read the instruction (1 == true)  since it has no meaning. It only has meaning if we make an assumption. We can not make the assumption that "true" has a value of "1", it could just as easily have a value of "Apple".

 

conclusion:

Do I propagate bad, application specific logic, or do I correct it in Jane?  I was stuck a few months ago by case 3, I wasted four hours trying to determine the problem. My thinking is to remove the need for documentation, thereby forcing a simpler syntax, and force developers to use parentheses thereby making the program explicit and easier to read.  I want to quit thinking about what the compiler is doing, and make programs easier to read. So I will make the changes. I am sure someone had a reason for what is what, but I cannot think of one. I can only think they just followed the precedence order of "C". I will not make that mistake. Make all the rules of logic simple and not have to look them up.