neilsf / xc-basic3

A BASIC cross compiler for MOS 6502-based machines
MIT License
45 stars 5 forks source link

allow expression in CONST when the expression resolve to a constant #240

Open nippur72 opened 1 year ago

nippur72 commented 1 year ago

somewhat related to #63, CONST should accept any expression that evaluates to a constant value (the same way as DASM does).

E.g.

CONST ROWS = 24
CONST COLS = 22
CONST SIZE = ROWS * COL   ' not currently accepted
neilsf commented 1 year ago

I had this in my mind for a while but I haven't yet found a good way to implement it.

The easy implementation would be to simply have the machine that you compile on (your PC) evaluate the expression. Well, it will evaluate it differently than the target machine (your Commodore) because of the differences in the architectures and the underlying math libraries.

In your example, the compiler would evaluate the expression as SIZE = 528 but according to XC=BASIC runtime rules, the result should be SIZE = 16. (read why: https://xc-basic.net/doku.php?id=v3:expressions)

I think users would be confused by the fact that they get different expression results in compile time and run time.

The better approach would be to emulate the entire XC=BASIC (runtime) math library in the compiler itself. This way you'd get the same results in compile time. Implementing this is quite substantial, and unfortunately, my schedule doesn't allow me the necessary time to tackle it currently.

orlof commented 1 year ago

I understand that this is a design choice, but I would be happy if source code constant expressions were folded at compile time to literals without complying with runtime rules.