inexorabletash / jsbasic

Applesoft BASIC in JavaScript
https://calormen.com/jsbasic
Other
187 stars 39 forks source link

Operator NOT probably has wrong precedence for Applesoft BASIC #41

Closed mobluse closed 2 years ago

mobluse commented 2 years ago

I believe the operator NOT in Applesoft BASIC in Javascript https://www.calormen.com/jsbasic/ has too high precedence and not the one it should have in Applesoft BASIC i.e. below the relational operators: Operators have the following precedence; things within the same precedence level are evaluated left to right:

http://wiki.apple2.org/index.php?title=Applesoft_BASIC_Ref

Example code:

5 ?
10 ?not 3<7
20 ?not (3<7)
30 ?not 3
40 rem ?inv 3
50 ?(not 3)<7
60 ?3<7

Output:

1
0
0
1
1

But it should probably be:

0
0
0
1
1

If there are examples that depend on NOT having the wrong high precedence they could be fixed by adding parenthesis: NOT A AND B, becomes (NOT A) AND B. Applesoft BASIC was developed by Microsoft and other Microsoft BASICs have the same precedence for NOT, e.g. GW-BASIC and VBA. http://gwbasicprograms.blogspot.com/2012/01/operator-precedence-in-basic.html https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/operator-precedence

inexorabletash commented 2 years ago

An actual Apple or emulator (I tried Virtual II) gives:

1

0

0

1

1

Try https://www.scullinsteel.com/apple2/ for example.

Page 36 of Apple's Applesoft Basic Programming Reference Manual (1978) gives this:

[image: image.png] It appears that the wiki page you cited is incorrect.

On Tue, Jan 11, 2022 at 1:50 PM Mikael O. Bonnier @.***> wrote:

I believe the operator NOT in Applesoft BASIC in Javascript https://www.calormen.com/jsbasic/ has too high precedence and not the one it should have in Applesoft BASIC i.e. below the relational operators: Operators have the following precedence; things within the same precedence level are evaluated left to right:

  • Highest precedence: () [parentheses]
  • ^ [exponentiation]
  • - [unary minus]
  • * [multiplication], / [division]
  • + [addition], - [subtraction]
  • = [equal], <> or >< [not equal], < [less than], > [greater than],
  • <= or =< [less than or equal], >= or => [greater than or equal]
  • NOT [logical complement]
  • AND [logical AND]
  • Lowest precedence: OR [logical OR]

http://wiki.apple2.org/index.php?title=Applesoft_BASIC_Ref

Example code:

5 ?

10 ?not 3<7

20 ?not (3<7)

30 ?not 3

40 rem ?inv 3

50 ?(not 3)<7

60 ?3<7

Output:

1

0

0

1

1

But it should probably be:

0

0

0

1

1

If there are examples that depend on NOT having the wrong high precedence they could be fixed by adding parenthesis: NOT A AND B, becomes (NOT A) AND B. Applesoft BASIC was developed by Microsoft and other Microsoft BASICs have the same precedence for NOT, e.g. GW-BASIC and VBA.

http://gwbasicprograms.blogspot.com/2012/01/operator-precedence-in-basic.html

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/operator-precedence

— Reply to this email directly, view it on GitHub https://github.com/inexorabletash/jsbasic/issues/41, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAF4LWYAX6P6DUOLN3RQIHTUVSQ2BANCNFSM5LXM5VXQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

inexorabletash commented 2 years ago

Sorry, image is:

image

mobluse commented 2 years ago

Thanks for the correction! I was tricked by some desinformation on the net and that in most BASICs NOT has lower precedence than the relational operators. This is a better source than I had first: http://cini.classiccmp.org/pdf/Apple/AppleSoft%20II%20Basic%20Programming%20Manual.PDF Still, it might be usuful to have this precedence chart in the documentation for your system (jsbasic) since most developers come from other BASICs such as VBA. MMBasic seems to have the same precedence for NOT (but not for AND and OR) as AppleSoft BASIC. JavaScript also, where applicable: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence. Python seems to have the same precedence as VBA: https://docs.python.org/3/reference/expressions.html#operator-precedence

inexorabletash commented 2 years ago

Added! https://calormen.com/jsbasic/reference.html#Operator-Precedence

It was implicit in the grammar https://calormen.com/jsbasic/reference.html#Process-and-Grammars but doesn't hurt to call it out and note that not all BASICs are the same.

mobluse commented 2 years ago

In https://calormen.com/jsbasic/reference.html#Operator-Precedence "Operators are the same line are of the same priority" should probably be "Operators on the same line are of the same priority".