neilsf / XC-BASIC

A compiling BASIC dialect for the Commodore-64
https://xc-basic.net/
MIT License
74 stars 15 forks source link

Explicit mode #100

Closed neilsf closed 3 years ago

neilsf commented 4 years ago

There was a voting on Twitter about this topic and it seems that while the majority of voters are okay with dropping sigils in the upcoming version, still many of them are conservative and would keep on coding the sigil-style.

I think we should go both ways, just like QB64 does and allow both styles. Here is an early idea.

A compiler directive (PRAGMA) should declare whether the code uses the old style or whishes to define variables explicitly. The programmer may go on the old style if they set the following directive in the beginning of the program:

PRAGMA implicit_declaration = 1
LET undefd_byte! = 42

However if the directive is unset or is set to 0, the new style is used:

DIM my_var AS BYTE
LET my_var = 42
LET undefd_var = 42 : REM !!! this will throw a compiler error

In this case you can still use the $#%! characters at the end of the variable names but they will be ignored by the compiler.

Other things to take into consideration:

Please add your thoughts.

JJFlash-IT commented 4 years ago

I wasn't thinking about the built-in functions, the casting and the DATA statement when I proposed on Twitter to go the way of QuickBASIC. Taking all that into account, I believe that's too much trouble to do all that adapting work.

I think the most important thing is the explicit definition of variables by default: that can save a lot of trouble with debugging, especially if you don't/can't use an auto-completing editor while writing the XC=Basic code. It's too easy to misspell a variable name and cause some obscure trouble that can be hard to debug. After all, that's why the explicit definition was introduced in other languages :slightly_smiling_face:

Since this is a BASIC dialect for old platforms, I think you could keep the sigils: I grew up with TI 99 4/A Basic, then ZX Spectrum Basic, and then C64 Basic. All of those used "$" for string variables, and the C64 dialect used other sigils too, so I don't see a problem with keeping using those. I'm fine with "#" identifying "semi-long" variables, for instance.

When I started using XC=Basic, I got immediately comfortable with the sigils you implemented, and I relied on the compiler to error out whenever I forgot to add the "!" sigil to my byte variables.

So, to recap, my suggestion is: do implement PRAGMA to be backwards-compatible with old programs, make explicit definition of variables the default, and keep the rest as it is :slightly_smiling_face:

neilsf commented 4 years ago

Yes, explicit declaration will save from errors caused by misspelled variable names. That's one thing. The other thing is that it's very easy to forget to append the sigil to the variable name causing a compiler error. If there is a solution for function calls, sigils won't be needed any more (in explicit mode).

mrg-75 commented 4 years ago

I'd far prefer type definitions, as I often forget to add the sigils after I've defined a variable, and have to go back and correct my code.

This is probably just me being new to the language though, and I'm sure I'd get used to sigils over time, if implementing type definitions causes too many problems.

neilsf commented 3 years ago

Entirely rewritten in V3.