JavaScript backend: DIM and ERASE¶
The JavaScript backend (--compile-js) checks DIM and ERASE when the
program runs, as MBASIC 5.21 does. The expected results come from the binary
under cpmemu. The test is
tests/regression/integration/test_js_dim_erase_runtime.py.
What the binary does¶
ERASEof an array that does not exist isIllegal function call. The arrays that the sameERASEnames before the bad one are erased already.DIMof an array that exists isDuplicate Definition. An array that a use made (A(1)=1) exists. The old array keeps its size.- A bound is any expression. A string is
Type mismatch, a number above 32767 isOverflow, and a negative number isIllegal function call.DIM A(2.5)isDIM A(3). - With
OPTION BASE 1, a bound of 0 isSubscript out of range. This check comes before theDuplicate Definitioncheck. - A
DIMthat fails stops at the bad declaration. The earlier declarations of the same statement are made. The later ones are not made. - All of these errors can be trapped.
ERRandERLgive theDIMorERASEline, andRESUME NEXTgoes on after the statement.
How the generated code works¶
Most programs have one DIM per array with constant bounds, at the start of
the program. The generated code makes these arrays when it starts, and each
access is a plain index. The DIM only sets a flag: if the DIM runs a
second time (a jump back), it gives Duplicate Definition.
An array is a run-time array when one of these is true:
- An
ERASEnames the array. - The array has more than one
DIM. - A use of the array comes before the
DIMin the text. - A bound is not a constant, is negative, or is above 32767.
- The program has a
CLEARafter the firstDIM.CLEARdeletes every array, so aDIMcan follow it.CLEARsets the bounds of the run-time arrays tonull. ACLEARbefore the firstDIMchanges nothing. - The
DIMis not in the first lines of the program. The first lines are those before any statement that can jump (GOTO,GOSUB,IF,FOR,ON ERRORand so on). A jump can skip such aDIMor run it again.
The run-time arrays have a bounds variable (_bd_<name>) that is null
while the array does not exist. _dm_<name> is the DIM, _ar_<name> gives
the array for a use (and makes it with bounds 0 to 10 if it does not exist),
and ERASE sets the bounds to null. _db checks one bound. Each access
to a run-time array goes through a function call, so it is slower than a
fixed array. The semantic analyzer gets the runtime_dims option for the
JavaScript backend. With the option, a bad DIM is not a compile error.
Differences that remain¶
DIM A(32767)and other sizes that do not fit in the memory of the binary are made here (there is no memory model). Only a total above 16 million elements isOut of memory.- A variable and an array with the same name (
P$andP$(1)) share one JavaScript name. The fault is older, and this change does not fix it.