During the development stages of my compiler I ran into a pretty complex problem: how to store weakly typed variables in my language.
Since I allow variables to be declared without explicitly specifying their type, and allow functions to return either type (e.g. function can return a scalar OR array), I am now facing the difficulty of what form to store these variables in.
Here are the possibilities I've concidered, but all of them have a significant overhead:
- Regard all variables as lists of doubles (
List<double>
) and have the first element specify whether it's a scalar or array (0
or1
for instance). - Regard all variables as
object
instances. - Regard all variables as a
TVar
(custom class), which can be either adouble
orList<double>
.
To keep in mind:
- The only two types of variables I intend to have are doubles and double arrays, since all others can be derived from such (e.g. char is a case of a double, string is an array of chars, e.t.c.)
- I am using
ILAsm
which is a higher-level flavour of assembly (.NET intermediate language basically)