neilsf / xc-basic3

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

Typecast constants in compile time #244

Open neilsf opened 1 year ago

neilsf commented 1 year ago

Members of an expression are converted to the same type in runtime only. For example, the expression

DIM x AS FLOAT, y AS FLOAT
x = y + 1

will be compiled to the following runtime code sequence:

  1. Push y as float onto stack
  2. Push 1 as byte onto stack
  3. Convert top byte on stack to float
  4. Add top two floats on stack
  5. Pull float result off of stack

We should avoid conversion in runtime and do the work in compile time.

Note: using floating point literals solves the problem, but programmers aren't always aware of this, e. g:

x = y + 1.0