ZX BASIC:Language

From BorielWiki

Jump to: navigation, search

Contents

Introduction

This page is about the ZX Basic Language. It is part of the Language Reference Guide. ZX Basic aims to be a modern BASIC dialect but tries to keep some (many) of the original Sinclair BASIC features for the nostalgic. So you can use it in both ways.

The BASIC dialect is mainly based in FreeBasic. Many of the language specifications have been taken from there.

ZX BASIC Syntax

If you ever has programming in Legacy BASIC (either Sinclair BASIC or any other flavour) you will already know that BASIC languages are line oriented. Each sentence or group of sentences are separated in lines (ended with a carriage return).

Nowadays this is not necessary, but ZX Basic allows you to use lines and line numbers for compatibility (and nostalgic!) reasons:

10 REM This is a comment. 
20 PRINT "Hello world!"

Other than that, line numbers are ignored (well, not exactly: line numbers are treated as labels). So the previous BASIC program could be also written this way:

REM This is a comment
PRINT "Hello world!"

Lines and Sentences

Since ZX BASIC is line oriented this implies that the end of line (also known as carriage return or <CR>) is taken into account during syntax checking, and you cannot break lines in the middle of a sentence:

REM The following line has a syntax error
PRINT 
   "Hello world!"

Other languages (like C or Pascal) allows this because they're not line oriented. If you need to break a line, use the underline character (_) at the end of line to tell the compiler the current line continues in the next one:

REM The following line is broken into two, but it's ok
PRINT _
   "Hello world!"

REM Notice the _ character at the end of line.

Sentences and Block of Sentences

A sentence is the simplest BASIC instruction (e.g. PRINT). Sentences might contain arguments and can be separated by a colon (:) as in Sinclair BASIC or by end of line. A block of sentences are just a group of sentences one after another. Usually the reserved word END denotes the end of the block. E.g.

IF a > b THEN
    PRINT "A is greather than B"
    PRINT "and that's all"
END IF

In the previous example, everything between THEN and END IF conforms a block of sentences. Some sentences (like the shown IF) works with sentences block. They are called compound sentences.

Identifiers

Identifiers are used to denote variables, labels, sentences and functions. Some identifiers are reserved for ZX BASIC statements (e.g. PRINT) or predefined functions (e.g. COS). Proceed to the identifiers page for a list of reserved words.

Comments

As shown in the previous examples, the reserved word REM is used for comments and remarks. However, you can also use the single quote (') character for comments instead of REM:

10 REM This is a comment
20 'This is also a comment
30 PRINT "Hello world!"

Data Types

ZX Basic types ranges from 8 to 32 bits for integer formats. It also supports floating point format (the ZX ROM 40 bits floating point from the ROM FP Calculator) and Fixed for fixed poin arithmetic. See types page for more information.

Personal tools