Skip to content

3.1 Core language reference

6502Nerd edited this page Feb 20, 2025 · 15 revisions

Core language

Statements

dflat stores and executes statements in sequence based on line number order, for example;

  • 100 def_test()
  • 110 println "Hello world"
  • 120 enddef

The above result in these lines being stored sequentially in memory. To delete a line simply type the line number without any code as per most BASICs. To edit a line, simply write new statements with the line number that needs replacing.

Entering statements from the command line with no line number will attempt to execute it immediately.

dflat is a tokenising language - any lines entered with or without line numbers are first pre-processed and only if no errors are found at this point will the statements be stored or executed. This helps to speed up dflat (e.g. variables are tokenised as single byte look ups), although it does not mean runtime error will not be thrown by the language (e.g. dimensioning an array that is already dimensioned).

Multiple statements can be written on the same line, they need to be separated by ':' for example;

  • print "Hello ":println "world"

Comments can be inserted in to programs with the ';' symbol for example;

  • 10 ;this is a comment

When a comment is encountered, the rest of the line is ignored. Not that currently, to put a comment after a statement, you first must indicate the current statement is done using the ':' symbol, for example:

  • 10 println "Hello world":; Comment will be stored but ignored

Data types

dflat recognises only two primitive data types, <int> and <string>, allows arrays of these types and provides support to manipulate these data types.

Numbers

All <int> are stored as 2 byte signed integers with a range of -32768 to 32767. To convert numbers to text and vice-versa, use;

  • val(<n:string>) tries to interpret the provided string literal or variable as a number and returns the result as an integer. If the string was garbage, the number returned is 0 - careful!
  • hex(<n:int>) converts the supplied integer to a 4 digit hex string e.g. hex(16) returns "0010"

Operators on numbers are as follow and in the following priority (for same priority, evaluation is always left to right)

  • * , / , \ : multiply, divide, remainder
  • << , >> : shift left, shift right
  • + , - : plus and minus
  • & , | , ^ : bitwise and, or, xor
  • < , <= , >= , > : less than, less than equal, greater than equal, greater than
  • == : equality

The comparison operators evaluate to zero for false and -1 (0xffff) for true.

Arrays

Before explaining strings, it's logical to describe arrays which can be defined for both integers and strings in the same way (with some differences in how they are accessed).

Important features

dflat allows up to 2 dimensions for an array, and array subscripts are referenced from index 1 (this was done for implementation ease not for compatibility with any other BASICs)

It's important to know that the array storage is allocated from the next free space after the end of the stored program. Of course this means that entering program code will possibly trash over array storage - so currently in dflat, before any statement is stored or executed, all array storage is 'released'. Primarily the impact is that it's not possible to use multiple immediate mode commands to dimension and then access an array - because once it is dimensioned, it will be undimensioned when you enter the next immediate command. It's limitation I will look to take away at some point if there is space in the ROM.

When an array variable is referenced with out any indices, the address of the array is returned. This is particularly useful when needing to allocate space for assembly code, but also it used by some other functions of dflat.

dim statement (and redim)

When dflat encounters the dim statement, it allocates memory for the array to accommodate the size based on the dimensions. The following examples illustrate;

  • dim a[10] - allocates storage for 10 integers referenced through the array variable 'a'
  • dim b[4,3] - allocates storage for 12 integers, conceptually as 4 columns and 3 rows
  • dim a[10],b[20] - shows that multiple arrays can be defined as comma separated items
  • redim b[4,3] - this keyword from version 3.0 enables passing arrays (and strings) whilst ensuring they are correctly dimensions in a procedure

Strings

All strings are treated a 1 byte arrays of characters with a zero terminator. Space for strings must be allocated by dimensioning an appropriate array as a string variable (i.e. a variable name with '$' suffix), for example

  • dim a$[10] declares a string called a$ with space for 9 characters plus zero terminator
  • dim a$[10,3] declares an array of 3 strings, each of maximum 10 characters including zero terminator.

Operations on strings are as follows;

  • a$=<string> assigns a string to a variable
  • a$=<string>+<string> concatenates two strings together
  • < <= == >= > <> are the comparison operators using the ASCII code of each character

The following functions operate on strings;

  • chr(<n:int>) generates a single character ASCII string of the input integer (plus the zero terminator)
  • left(<string>,<n:int>) returns the left 'n' characters as a string (i.e. with a zero terminator)
  • right(<string>,<n:int>) returns the right 'n' characters
  • mid(<string>,<s:int>,<n:int>) returns the substring starting at position 's' with 'n' characters
  • hex(<n:int>) returns a string that is the 4 digit hex version of the input n
  • dec(<n:int>) returns a string that is the decimal version of the input n

Clone this wiki locally