Download xBasic 1.0 - Parallax Forums
Transcript
xBasic 1.0 User Manual Copyright © 2009, Kenneth G. Goutal. All rights reserved. Updates Copyright © 2009-2011, David M. Betz. This manual describes the xBasic system, version 1.0. xBasic User Manual Page 1 Table of Contents 1 Introduction .............................................................................................................................................. 5 2 Command Line Syntax .............................................................................................................................. 5 3 Language Syntax ....................................................................................................................................... 7 3.1 Expressions ........................................................................................................................................ 8 3.1.1 Constant Expressions .................................................................................................................. 8 3.1.2 Arithmetic Expressions ............................................................................................................. 11 3.1.3 Relational Expressions .............................................................................................................. 13 3.1.4 Logical Expressions ................................................................................................................... 16 3.1.5 Bitwise Expressions ................................................................................................................... 17 3.1.6 Other Expressions ..................................................................................................................... 24 3.2 Simple Statements ........................................................................................................................... 27 3.2.1 INCLUDE .................................................................................................................................... 28 3.2.2 REM ........................................................................................................................................... 28 3.2.4 DEF ............................................................................................................................................ 29 3.2.5 DIM ........................................................................................................................................... 29 3.2.6 LET ............................................................................................................................................. 29 3.2.7 IF ............................................................................................................................................... 30 3.2.8 GOTO ........................................................................................................................................ 30 3.2.10 PRINT ...................................................................................................................................... 31 3.2.12 STOP ........................................................................................................................................ 32 3.2.13 END ......................................................................................................................................... 32 3.3 Compound Statements .................................................................................................................... 33 3.3.1 DEF ............................................................................................................................................ 33 3.3.3 IF ............................................................................................................................................... 34 3.3.4 SELECT ....................................................................................................................................... 36 3.3.5 FOR ........................................................................................................................................... 39 3.3.6 DO ............................................................................................................................................. 39 5 Language Summary ................................................................................................................................ 41 xBasic User Manual Page 2 5.1 Labels ............................................................................................................................................... 42 5.2 Statements ....................................................................................................................................... 42 5.3 Statement ........................................................................................................................................ 42 5.4 variable-‐defs .................................................................................................................................... 43 5.5 variable-‐def ...................................................................................................................................... 43 5.6 basic-‐variable-‐def ............................................................................................................................ 43 5.7 type .................................................................................................................................................. 43 5.8 initializer .......................................................................................................................................... 44 5.9 init-‐expr ........................................................................................................................................... 44 5.10 expr ................................................................................................................................................ 44 5.11 l-‐value ............................................................................................................................................ 45 5.12 array-‐reference .............................................................................................................................. 45 5.13 variable .......................................................................................................................................... 45 5.14 function-‐call ................................................................................................................................... 45 5.18 name .............................................................................................................................................. 46 5.19 decimal-‐constant ........................................................................................................................... 46 5.20 digit-‐string ...................................................................................................................................... 46 5.21 hex-‐constant .................................................................................................................................. 46 5.22 string-‐constant ............................................................................................................................... 46 5.23 printable-‐characters ...................................................................................................................... 47 5.24 printable-‐character ........................................................................................................................ 47 5.25 alphanums ..................................................................................................................................... 47 5.26 alphanum ....................................................................................................................................... 47 5.27 letter .............................................................................................................................................. 48 5.28 punctuation-‐mark .......................................................................................................................... 48 5.29 hex-‐digit ......................................................................................................................................... 48 5.30 digit ................................................................................................................................................ 48 xBasic User Manual Page 3 Typography Used in This Manual The body of this manual is set using 12pt. Lucida Sans (a variable-width font). Section headings are numbered and in bold face, and the text (but not the numbers) is underlined. Subheadings, for "Syntax", "Examples", and similar topics are merely underlined. Specifications of syntax, shown using BNF, are indented, set using Lucida Sans Typewriter (a fixed-width font) bold, for example: expr1 MOD expr2 Within a syntax specification: • Keywords are shown like this. • Metavariables are shown like this. • Metasyntactic characters (brackets, braces, vertical bars) are shown like this. (That is, monospace, but not bold and not italic.) 1 Within the body of text: • A keyword is shown like this. • A metavariable is shown like this. • A file name is shown like this. • A product name is shown like this. Examples of operators, statements, and functions are shown in 11pt. Lucida Sans Typewriter, for example: PRINT "abcdefghij", 3, 4, "cdef" Headings for such examples are shown in Lucida Sans, but in italic and underlined. xBasic User Manual Page 4 1 Introduction This system consists of the following parts: • the compiler (xbcom.exe) • the virtual machine The compiler program is called XBCOM.EXE. It is an MS-Windows console application; that is, in runs on MS-DOS, or under MS-Windows but only using the COMMAND or CMD application. You will need to use one of those methods to navigate to the folder and run command lines with arguments. A full description of the command line syntax is given below. The virtual machine (VM) runs on the Parallax Propeller Chip (P8X32A). It can either run on a bare Propeller chip using only hub memory or it can make use of various forms of external memory to extend program and data space. 2 Command Line Syntax Use the following syntax to compile an XBasic source file: xbcom [ -b board-type ] -p port ] [ -r ] [ -t ] input-file Use the -p option to specify the serial port to use to download the compiled program to the Propeller chip. If you don't provide this option, the default port is "COM4". The port will be configured to 115200 baud, 8 bits no parity. To select “COM2”, enter either -pCOM2 or -p2. • Use –b hub to compile for hub memory (any Propeller board). • Use –b hub96 to compile for hub memory on a board with a 6mhz crystal (96mhz). • Use –b c3 to compile for the Parallax C3 board placing the code in the SPI flash and data in the SPI SRAMs. • Use –b ssf to compile for the SpinSocket-Flash board by Steve Denson (jazzed). • Use -r to download and run the program. • Use -t to enter terminal mode after the download completes. xBasic User Manual Page 5 The input-file parameter is required as is the -b to select the target board. The compiler produces an output file with the same name as the input file but with the extension .bai in place of .bas. xBasic User Manual Page 6 3 Language Syntax Names: Before going further, a discussion of names is in order. Several kinds of things are identified by names: variables (both scalars and arrays); constants; statement labels; and subroutines and functions. A name can be at most 32 characters long. A name must start with a letter but can contain letters of either case, digits, "$", "%" and "_". Also, a variable name that ends with "$" is assumed to be a string variable unless otherwise specified; similarly, a variable name that ends with "%" is assumed to be an integer variable. It should be noted that XBasic is not case-sensitive: the names “foo”, “Foo”, and “FOO” are all the same name as far as it is concerned. Programs in XBasic consist of statements. Each statement occupies a single line, and each line consists of a single statement. Expressions: Expressions are used in many of the statements of this language. While there are some statements that are so simple that they do not require any expressions, expressions are so fundamental that we will discuss them before discussing the actual statements. Statements: Any statement may be preceded by a label. Doing so is required for some purposes, but most lines do not require them, and should not have them. This dialect of BASIC does not support the concept of line numbers. The use of labels will be discussed later, as necessary. Some statements are not complete in and of themselves, and must be used in groups, or at least in pairs. For example, the DEF statement begins the definition of a function. The END DEF statement is ends the definition. All statements in between the two statements are a part of that function. xBasic User Manual Page 7 3.1 Expressions An expression is either a constant, a variable, or some combination of one or more of those using various operators. There are so many that it is helpful to consider them in groups or categories: • Constant Expressions decimal-constant • - / MOD - OR AND = <> < <= >= > Bitwise Expressions ~ • * Logical Expressions NOT • string-constant Arithmetic Expressions + • 0xhex-constant & | ^ << >> Other Expressions (...) variable array-reference function-call Below are descriptions of each of them, by category. We examine constant expressions first, because we will see them in examples of all the other expressions. 3.1.1 Constant Expressions decimal-constant hexadecimal-constant string-constant 3.1.1.1 decimal constant The value of this expression is a specific integer value represented as a signed decimal number. Syntax: [ { + | - } ] decimal-digit-string where decimal-digit-string is from 1 to 5 decimal digits with no intervening characters of any kind. The lower bound is -32768, and the upper bound is 32767. A “minus” (negative-value) symbol may precede the decimaldigit-string, and space is allowed between the sign and the decimal-digit-string. Examples: This expression: has this value: xBasic User Manual Page 8 0 0 -0 0 000 0 9 9 09 9 -9 -9 -09 -9 32767 32767 -32768 -32768 xBasic User Manual Page 9 3.1.1.2 hexadecimal constant The value of this expression is a specific integer value represented as a unsigned hexadecimal number. Syntax: [ { + | - } ] 0xhex-digit-string where hex-digit-string is from 1 to 4 hexadecimal digits with no intervening characters of any kind. The lower bound is 0000, and the upper bound is FFFF. The two-character string 0x prefixes the hexadecimal number in order to let the compiler (and a subsequent human reader) know that any decimal digits are actually part of a base-sixteen number. No space is allowed between the two characters 0 and x or between the x and the hex-digit-string. The letter x must be in lower case; it must not be a capital or upper-case X. A “minus” (negative-value) symbol may precede the 0x, and space is allowed between the sign and the 0x. Examples: This expression: has this value: 0x0 0 0x00000 0 0x9 9 0x00009 9 0xF 15 0x0000F 15 0xFF 255 0xFFF 4095 0x7FFF 32767 0x8000 -32768 0xFFFF -1 0xFFFFF -1 0xf 15 0x0000f 5 -0xf -15 0Xf (upper-case X) (none) 0xG (invalid hex digit) (none) xBasic User Manual Page 10 3.1.1.3 string constant The value of this expression is a specific sequence of printable characters. Syntax: " printable-characters " The printable characters include the blank (0x20) as well as punctuation, digits, and upper-case and lower-case letters. The characters of the string must be enclosed in a pair of double-quotes ( ”...” ); a double-quote character may be included in the string by preceding it with a backslash (in the manner of the C language and its descendents). Examples: This statement: produces this output: PRINT "ABcd 09 ,.;:!?" ABcd 09 ,.;:!? PRINT "'ABcd 09 ,.;:!?'" 'ABcd 09 ,.;:!?' PRINT "\"ABcd 09 ,.;:!?\"" "ABcd 09 ,.;:!?" 3.1.2 Arithmetic Expressions Arithmetic expressions include those with the following operators: + - * / MOD 3.1.2.1 addition - 2 Syntax: expr1 + expr2 This expression adds expr2 to expr1. number. Example: The value of the following expression is 11: 6+5 3.1.2.2 subtraction Syntax: expr1 – expr2 This expression subtracts expr2 from expr1. Example: The value of the following expression is 6: 11 - 5 3.1.2.3 multiplication xBasic User Manual Page 11 Syntax: expr1 * expr2 This expression multiplies expr2 by expr1. Example: The value of the following expression is 30: 6*5 3.1.2.4 division Syntax: expr1 / expr2 This expression divides expr1 by expr2. Example: The value of the following expression is 6: 30 / 5 3.1.2.5 modulo Syntax: expr1 MOD expr2 This expression divides expr1 by expr2, and returns the remainder. Example: The value of the following expression is 0: 30 MOD 5 The value of the following expression is also 0: 30 MOD 6 The value of the following expression is 1: 31 MOD 5 The value of the following expression is 2: 30 MOD 4 3.1.2.6 negation Syntax: - expr This expression negates, or returns the negative value of, expr. Example: The value of the following expression is negative three: -3 xBasic User Manual Page 12 3.1.3 Relational Expressions = <> < <= > >= Relational expressions make arithmetic comparisons between numbers. They return 0 (zero) to represent FALSE and 1 (one) to represent TRUE. 3.1.3.1 equality The value of this expression is 1 (one) if the specified expressions are equal to each other; otherwise, the value is 0 (zero). Note: This expression should not be confused with the assignment statement! Syntax: expr1 = expr2 Examples: The value of the following expression is 0 (representing FALSE): 3=2 The value of the following expression is 1 (representing TRUE): 4=4 3.1.3.2 inequality The value of this expression is 1 (one) if the specified expressions are not equal to each other; otherwise, the value is 0 (zero). Syntax: expr1 <> expr2 Examples: The value of the following expression is 0 (representing FALSE): 3 <> 3 The value of the following expression is 1 (representing TRUE): 3 <> 4 xBasic User Manual Page 13 3.1.3.3 less-than The value of this expression is the 1 (one) if the value of expr1 is strictly less than the value of expr2; otherwise, the value is 0 (zero). Syntax: expr1 < expr2 Examples: The value of the following expression is 0 (representing FALSE): 4<3 The value of the following expression is 0 (representing FALSE): 4<4 The value of the following expression is 1 (representing TRUE): 4<5 3.1.3.4 less-than-or-equal-to The value of this expression is the 1 (one) if the value of expr1 is less than or equal to the value of expr2; otherwise, the value is 0 (zero). Syntax: expr1 <= expr2 Examples: The value of the following expression is 0 (representing FALSE): 4 <= 3 The value of the following expression is 1 (representing TRUE): 4 <= 4 The value of the following expression is 1 (representing TRUE): 4 <= 5 xBasic User Manual Page 14 3.1.3.5 greater-than The value of this expression is the 1 (one) if the value of expr1 is strictly greater than the value of expr2; otherwise, the value is 0 (zero). Syntax: expr1 > expr2 Examples: The value of the following expression is 0 (representing FALSE): 6>7 The value of the following expression is 0 (representing FALSE): 7>7 The value of the following expression is 1 (representing TRUE): 7>6 3.1.3.6 greater-than-or-equal-to The value of this expression is the 1 (one) if the value of expr1 is greater than or equal to the value of expr2; otherwise, the value is 0 (zero). Syntax: expr1 >= expr2 Examples: The value of the following expression is 0 (representing FALSE): 6 >= 7 The value of the following expression is 1 (representing TRUE): 7 >= 7 The value of the following expression is 1 (representing TRUE): 7 >= 6 xBasic User Manual Page 15 3.1.4 Logical Expressions NOT OR AND Logical expressions treat 0 (zero) as FALSE and any non-zero value as TRUE. Similarly, they return 0 (zero) to represent FALSE and 1 (one) to represent TRUE. Note: These are not the same as bitwise operations with the same or similar names. Logical operators perform their operations on the whole value of each expression, and return either an integer 0 (zero) or an integer 1 (one); bitwise operators (see below) perform their operations on corresponding bits in each of the expressions, and return a new integer representing those result of those operations. 3.1.4.1 logical NOT The value of this expression is TRUE if the specified expression is FALSE , and is FALSE if the specified expression is TRUE. Syntax: NOT expr Examples: The value of the following expression is 1 (representing TRUE): NOT 0 The value of the following expression is 0 (representing FALSE): NOT 3 xBasic User Manual Page 16 3.1.4.2 logical OR The value of this expression is TRUE if the values of either (or both) of the specified expressions is (or are) TRUE. Note: This operator uses "short-circuit evaluation". That is, if expr1 is TRUE, then expr2 is never even evaluated, and the entire expression evaluates to TRUE . Syntax: expr1 OR expr2 Examples: The value of the following expression is 0 (representing FALSE): 0 OR 0 The value of the following expression is 1 (representing TRUE): 0 OR 3 The value of the following expression is 1 (representing TRUE): -12 OR 0 The value of the following expression is 1 (representing TRUE): -11 OR 1 3.1.4.3 logical AND The value of this expression is TRUE if the values of both of the specified expressions are TRUE. Note: This operator uses "short-circuit evaluation". That is, if expr1 is FALSE, then expr2 is never even evaluated, and the entire expression evaluates to FALSE. Syntax: expr1 AND expr2 Examples: The value of the following expression is 0 (representing FALSE): 0 AND 0 The value of the following expression is 0 (representing FALSE): 0 AND 3 The value of the following expression is 0 (representing FALSE): -12 AND 0 The value of the following expression is 1 (representing TRUE): -11 AND 1 3.1.5 Bitwise Expressions ~ xBasic User Manual & | ^ << >> Page 17 3.1.5.1 bitwise NOT The value of this expression is the integer representation of the inversion, or ones-complement, of the bits of the specified expression. Syntax: ~ expr Examples: This expression: has this value: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ xBasic User Manual 0 -1 0x0000 0xFFFF -1 0 0xFFFF 0x0000 -2 1 0xFFFE 0x0001 -256 255 0xFF00 0x00FF -275 274 0xFEED 0x0112 Page 18 3.1.5.2 bitwise inclusive OR The value of this expression is the integer representation of the inclusive OR of the corresponding bits of the specified expressions. That is, if a given bit in expr1 is set to 1 or the corresponding bit in expr2 is set to 1, or both bits are set, then the corresponding bit in the result is set to 1; otherwise, it is set to 0 (zero). Syntax: expr1 | expr2 Examples: This expression: has this value: 0|11 0x0000 | 1|23 0x0001 | 2|33 0x0002 | -256 | 255 0xFF00 | xBasic User Manual 0x0001 0x1 0x0002 0x0003 0x0003 0x0003 -1 0x00FF 0xFFFF Page 19 3.1.5.3 bitwise exclusive OR The value of this expression is the integer representation of the exclusive OR of the corresponding bits of the specified expressions. That is, if a given bit in expr1 is set to 1 or the corresponding bit in expr2 is set to 1, but not both bits are set, then the corresponding bit in the result is set to 1; otherwise, it is set to 0 (zero). Syntax: expr1 ^ expr2 Examples: This expression: has this value: 0 ^ 1 1 0x0000 ^ 0x0001 0x0001 1 ^ 2 3 0x0001 ^ 0x0002 0x0003 2 ^ 3 1 0x0002 ^ 0x0003 0x0001 65280 ^ 255 -1 0xFF00 ^ 0x00FF 0xFFFF 43690 ^ 21845 -1 0xAAAA ^ 0x5555 0xFFFF 43690 ^ 65280 21930 0xAAAA ^ 0xFF00 0x55AA xBasic User Manual Page 20 3.1.5.4 bitwise AND The value of this expression is the integer representation of the AND of the corresponding bits of the specified expressions. That is, if a given bit in expr1 is set to 1 and the corresponding bit in expr2 is set to 1, then the corresponding bit in the result is set to 1; otherwise, it is set to 0 (zero). Syntax: expr1 & expr2 Examples: This expression: has this value: 0 & 1 0 0x0000 & 0x0001 0x0000 1 & 2 0 0x0001 & 0x0002 0x0000 2 & 3 2 0x0002 & 0x0003 0x0002 -256 & 255 0 0xFF00 & 0x00FF 0x0000 -21846 & 21845 0 0xAAAA & 0x5555 0x0000 -21846 & -256 -22016 0xAAAA & 0xFF00 0xAA00 xBasic User Manual Page 21 3.1.5.5 bitwise shift left The value of this expression is the integer representation of shifting expr1 left by the number of bits specified by expr2. Syntax: expr1 << expr2 Examples: This expression: has this value: 1 << 1 2 0x0001 << 0x0001 1 << 2 4 0x0001 << 0x0002 1 << 8 256 0x0001 << 0x0008 15 << 4 240 0x000F << 0x0004 15 << 8 3840 0x000F << 0x0008 255 << 8 -256 0x00FF << 0x0008 255 << 16 0 0x00FF << 0x0010 xBasic User Manual 0x0002 0x0004 0x0100 0x00F0 0x0F00 0xFF00 0x0000 Page 22 3.1.5.6 bitwise shift right The value of this expression is the integer representation of shifting expr1 right by the number of bits specified by expr2. Note: This is an arithmetic shift. Hence, the sign bit (the mostsignificant bit) is preserved, and is also copied to the next bit to its right, for as many bits as specified by expr2. Syntax: expr1 >> expr2 Examples: This expression: has this value: 1 >> 1 0 0x0001 >> 0x0001 1 >> 2 0 0x0001 >> 0x0002 2 >> 1 1 0x0002 >> 0x0001 15 >> 1 7 0x000F >> 0x0001 240 >> 4 15 0x00F0 >> 0x0004 -256 >> 8 -1 0xFF00 >> 0x0008 -256 >> 16 -1 0xFF00 >> 0x0010 0x0000 0x0000 0x0001 0x0007 0x000F 0xFFFF 0xFFFF xBasic User Manual Page 23 3.1.6 Other Expressions (...) variable array-reference function-call 3.1.6.1 parentheses The value of this expression is the value of the expression inside the matched pair of parentheses. Syntax: ( expr ) Parentheses simply provide the traditional way of grouping expressions together, particularly for the purpose of over-riding operator precedence. Examples: This expression: has this value: 6 ( 6 4 ( xBasic User Manual / 6 / + 4 2 + 4 7 / 2 ) + 4 7 (2 + 4) 1 6 / 2 7 + 6 ) / 2 5 Page 24 3.1.6.2 variable A variable is simply a value that changes, while the variable name remains the same. Syntax: variable Examples: This expression or statement: has this value or does this: x (undefined!) LET x = 6 assigns x the value 6 LET y = 2 assigns y the value 2 z = 4 assigns z the value 4 x6 y2 z4 x/y3 y+z6 x/y+z7 (x/y)+z7 x / (y + z) 1 z+x/y7 (z+x)/y5 xBasic User Manual Page 25 3.1.6.3 array reference or element An array is simply a variable that can contain or represent more than one value simultaneously, each one distinguished from the others by its index (or subscript). The index may be any expression whose value is an integer; that is, it may not be a floating-point value or a string. Note: array indexes in XBasic start with 0 (zero) and range up to the array size-1. Generally, an array is used to group together two or more values that are in some sense alike, for instance, the highest temperature on each day of the year, or the wave frequency of each note in a scale or tune. Syntax: variable ( index ) Example: Suppose your program includes the following statements: LET LET LET LET LET LET LET LET LET LET LET LET LET piano(40) piano(41) piano(42) piano(43) piano(44) piano(45) piano(46) piano(47) piano(48) piano(49) piano(50) piano(51) piano(52) = = = = = = = = = = = = = 261 277 293 311 329 349 369 391 415 440 466 493 523 // // // // // // // // // // // // // C4 C#4 or Db4 D4 D#4 or Eb4 E4 F4 F#4 or Gb4 G4 G#4 or Ab4 A4 A#4 or Bb4 B4 C5 This stores the frequencies of the musical pitches noted in the comments into a set of array elements. (Yes, those frequencies are approximate.) The index of each array element is the piano key corresponding to that pitch. Dim Array Syntax: Dim variable ( index ) xBasic User Manual Page 26 3.1.6.4 function call The value of a function call is the value of the name of the function immediately prior to ending (or returning, or exiting). See the section later in this document regarding how to define a function.. Syntax: name ( [ arg [ , arg ] ... ] ) or name That is, the parentheses are optional if there are no arguments.The name is just the name of the function. There can be any number of arguments, even none at all, as long as they match they number of arguments with which the function was defined. Each argument can be any expression, as long as it matches the type of expression of the corresponding argument with which the function was defined. Example: Suppose your program contains the following statements, which define a function that computes the area of a right triangle, given the two orthogonal sides. DEF rightTriangleArea ( side1, side2 ) rightTriangleArea = side1 * side2 / 2 END DEF This function could then be called as follows: LET A = rightTriangleArea ( 3, 4 ) which would set the variable “A” to the value 3*4/2, or 6. Or it could be called this way: PRINT rightTriangleArea(9,8) which would display the number 36 (that is, 9*8/2) on a line by itself. Now we are ready to consider the statements that use all these expressions. 3.2 Simple Statements Here is a list of statements that stand by themselves: INCLUDE REM xBasic User Manual Page 27 DEF DIM IF LET GOTO PRINT STOP END 3 4 Here are descriptions of each of them: 3.2.1 INCLUDE Syntax: INCLUDE filename-string This statement is the way to include the contents of another file in your program. This can be convenient if you have definitions or code that is shared among a number of programs so that you don't have to type it again each time you need to use it. It is also useful for including standard definitions that come with XBasic like the example below. Example: INCLUDE “chameleon.bas” 3.2.2 REM Syntax: REM [ comment text to end of line ] This statement is used to include remarks or comments in the program. They are completely ignored by the compiler, and do not show up in compiled (and downloaded and executed) program in any form. They are included in a program as a means of communicating to some other programmer (or oneself!) at a future time what a certain part of the program is supposed to do, or what algorithm is being used, or something of that sort. Comments can also be included using the syntaxes common to C and many other languages: // [ comment text to end of line ] /* [ comment text ] [ between slash+asterisk pair ] [ and matching asterisk+slash pair ] */ Examples: REM The following takes place xBasic User Manual Page 28 REM on the day of the Massachusetts primary election. REM It is the shortest day of my career. or /* The following takes place on the day of the Massachusetts primary election. It is the shortest day of my career. */ or LET a= 3 LET b= 4 // Set variable to length of one side. // Set variable to length of other side. 3.2.4 DEF Syntax: DEF name = value This form of the DEF statement is self-contained, and merely defines a constant; that is, it defines a name to have an unchangeable value. Example The following defines “hundredpi” to be a constant whose value is always (roughly) 100 times the value of π. DEF hundredpi = 314 3.2.5 DIM Syntax: DIM variable-defs See section "variable-defs", below. This statement is the way to declare one or more either scalar or array variables. The initializers may be spread over more than one line. Examples: DIM A DIM A = 1 DIM B(3) DIM B(3) = { 1, 2, 3 } 3.2.6 LET Syntax: [ LET ] l-value = expr xBasic User Manual Page 29 This is the assignment statement. It assigns the expression to the right of the “equals” sign to the l-value on the left. An l-value is just a way of saying something that can have a value assigned to it, i.e. either a scalar (one-dimensional) variable or a single element of an array. Note that the word LET is optional. However, if present, it must be the first word of the statement, and no other word may be there instead. Example: LET A = 7 pixels_per_brick = 47 let ballWidth=15 3.2.7 IF Syntax: IF expr THEN statement This statement is a way for a program to do a thing or not do a thing. Examples: If a value is zero, set it to some specific (default) value: IF number_of_monsters = 0 THEN LET number_of_monsters = 111 Similarly, if some counter has reached a predetermined maximum, set it back to one. IF N >= 24 THEN N = 1 3.2.8 GOTO Syntax: GOTO label This statement causes the program execute the statement at “label” instead of executing the statement immediately following the GOTO statement. The GOTO statement seems obvious and innocent at first, but has generally been found to cause complexity and confusion if used more than sparingly. The XBasic language has many ways to organize sequences of statements in an orderly way, so the GOTO statement should be easy to avoid in most cases. xBasic User Manual Page 30 Note: GOTO in the main code can refer only to labels in the main code. GOTO within a function or subroutine can refer only to labels within the same function or subroutine. Example: LET x=1 abc: LET x=x+1 GOTO hijk efg: LET x=x-5 GOTO efg hijk: LET x=x+2 STOP END Two questions immediately arise: (1) Does this program ever finish? (2) What is the value of x if and when it does? 3.2.10 PRINT This statement sends text to the serial interface. To send text to the screen, see the DISPLAY statement, below. Syntax: PRINT [ expr [ [ { , | ; } expr ] (...) ] ] The text will represent zero or more expressions, as specified in the statement. Each expression may be a string or decimal or hexadecimal constant, or a scalar variable, or an array element. If no expressions are included, a blank line is displayed. If only one expression is included, no other syntax is required. If more than one expression is included, each must be separated from the next by either a comma or a semicolon. If the separator is a semicolon, the second expression will appear immediately adjacent to the previous expression; in effect, they will appear to be concatenated. On the other hand, if the separator is a comma, the second expression will begin at the next 8th column on the line. Examples: Print an empty or blank line: PRINT Print the number “7” on a line by itself: LET A = 7 PRINT A Print “4715” on a line by itself: xBasic User Manual Page 31 LET pixels_per_brick = 47 LET ballWidth = 15 PRINT pixels_per_brick ; ballWidth Print “47 15” (that is “47” followed by 6 blanks or spaces, followed by “15”) on a line: PRINT pixels_per_brick , ballWidth 3.2.12 STOP Syntax: STOP This statement tells the program to stop altogether, regardless of where in the program it appears or how it was encountered. 3.2.13 END Syntax: END This statement tells the compiler that it is the last statement of the program. It has no effect on the program at run time. It is optional, but its use is encouraged. xBasic User Manual Page 32 3.3 Compound Statements Here is a list of statements that must appear in groups: DEF END DEF SUB END SUB IF ELSE IF ELSE END IF SELECT CASE CASE ELSE END SELECT FOR NEXT DO LOOP 5 6 Here are descriptions of each of them: 3.3.1 DEF This form of the DEF statement defines a function. Syntax: DEF name [ ( [ arg [ , arg ] ... ] ) ] ... END DEF The statement itself (with the name and parentheses and arguments) specifies how the function will be called. It must be followed by a matching END DEF statement (as shown). All the statements in between specify what the function does to achieve the result that it returns. In this form, the END DEF statement is required. Note that if the function does not use any arguments, the entire argument list including the parentheses may be omitted. Inside of the function, the function's name is used as a variable to which to assign the return value; the value of that variable at the time the function completes execution is the return value of the function. There is no RETURN statement, as in some other dialects of BASIC. xBasic User Manual Page 33 Examples: The following defines a function that computes the area of a right triangle, given the two orthogonal sides. The “body” of the function consists of just two statements, which compute the area of the square and divides that by 2, and assigns that the name of the function. DEF rightTriangleArea ( side1, side2 ) rightTriangleArea = side1 * side2 rightTriangleArea = rightTriangleArea / 2 END DEF The body of this function could just as easily be written as a single line, as follows: DEF rightTriangleArea ( side1, side2 ) rightTriangleArea = side1 * side2 / 2 END DEF This function could then be called as follows: LET A = rightTriangleArea ( 3, 4 ) which would set the variable “A” to the value 6. Or it could be called this way: PRINT rightTriangleArea(9,8) which would display the number 36 on a line by itself. 3.3.3 IF Syntax: IF expr THEN statement IF expr THEN [ ELSE IF expr THEN ] [ ELSE ] END IF This statement is the way for a program to do different things instead of each other, depending on circumstances. The simplest case provides the means to either do a thing or not do a thing. The second form provides a way to do several things, or not do them; or to do more than one alternative thing or set of things. Examples: If a value is zero, set it to some specific (default) value: IF number_of_monsters = 0 THEN LET number_of_monsters = 111 Similarly, if some counter has reached a predetermined maximum, set it back to one. IF N >= 24 THEN N = 1 xBasic User Manual Page 34 If you need to do more than one thing (or not), use this form: IF number_of_monsters = 0 THEN LET level = level + 1 LET number_of_monsters = 111 * level END IF If you need to do two different things depending on circumstances, use this form: DEF furry = 1 DEF flying = 2 IF level MOD 2 = 1 THEN monster_type = furry ELSE monster_type = flying END IF xBasic User Manual Page 35 If you need to do more than two different things, the IF ... THEN ... ELSE IF chain may be your answer: DEF Sunday = 1 DEF Monday = 2 ... DEF Saturday = 7 IF (dayOFweek = Saturday) PRINT "Have a nice weekend!" ELSE IF (dayOFweek = Sunday) PRINT "Have a nice Sunday!" ELSE PRINT "Have a nice day!" (This example is based on one in the PHP sections of the w3schools.com web site.) One IF statement can be “nested” inside another: DEF furry = 1 DEF flying = 2 DEF slimy = 3 DEF arach = 4 IF level MOD 2 = 1 THEN IF LEVEL > 5 THEN monster_type = furry ELSE monster_type = slimy END IF ELSE IF level > 5 monster_type = arach ELSE monster_type = flying END IF END IF 3.3.4 SELECT Syntax: SELECT expr0 [ CASE case-expr [ , case-expr ] (...) statements ] (...) [ CASE ELSE statements xBasic User Manual Page 36 ] END SELECT This statement performs one or more different statements (or sequences of statements) based on whether expr0 matches any of the values in the CASE statements. Each CASE statement (except the ELSE variant) includes one (or more) case expressions. If there are more than one, each is separated from the one before it by a comma. Each case-expr can be either an individual value or a range of value, i.e. expr or lower-bound-expr TO upper-bound-expr Individual values and value ranges can be intermixed freely. It works this way: First, expr0 is evaluated. Each case-expr in each CASE statement is examined in turn. If the case-expr is an individual value, then if expr0 is exactly equal to that value, then the immediately following statements will be performed; or, if expr0 is equal to or greater than lower-bound-expr and less than or equal to upper-bound-expr, then the immediately following statements will be performed. If none of the ordinary CASE statements match expr0, but there is a CASE ELSE statement, the immediately following statements will be performed. If and when a matching CASE is encountered, and the immediately following statements are peformed, all further statements, including CASE statements, will be ignored until the matching END SELECT statement. The SELECT statement may be thought of as an "express" version of a sequence of IF ... THEN ... ELSE IF (...) END IF statements where (a) the initial IF comparison and all the ELSE IF comparisons all involve the same variable or expression, and (b) the comparison is always one of equality. Rather than repeating that variable or expression and the equality operator, in the SELECT statement the expression is specified only once, and comparison of equality is implied. xBasic User Manual Page 37 Examples: The following examines a simple variable, and compares it to a range, and to members of a list, and does something different in each case; if neither case applies the CASE ELSE statement does something else entirely. select X case 1 to 3 // Use a range. print “would go at top” case 21, 22, 23 // Use a list. print “would go at bottom” case else // Catch all other cases. print “would go in main area” end select One can readily see that if X is outside the expected range (1 through 23, inclusive), this will behave badly. A better rendering would be: SELECT X CASE 1 to 3 PRINT "would go at top" CASE 4 TO 20 PRINT "would go in main area" CASE 21 TO 23 PRINT "would go at bottom" CASE ELSE PRINT "invalid value" END SELECT In the following example, there is no CASE ELSE statement. Because of this, if the variable does not match one of the six specified ranges, nothing happens. select X CASE 01 TO 03 PRINT X;" is in decade" CASE 11 TO 13 PRINT X;" is in decade" CASE 21 TO 23 PRINT X;" is in decade" CASE 31 TO 33 PRINT X;" is in decade" CASE 41 TO 43 xBasic User Manual ";"first three years";" of first ";"first three years";" of second ";"first three years";" of third ";"first three years";" of fourth Page 38 PRINT X;" is in ";"first three years";" of fifth decade" CASE 51 TO 53 PRINT X;" is in ";"first three years";" of sixth decade" end select 3.3.5 FOR Syntax: FOR variable1 = expr1 TO expr2 [ STEP expr3 ] statements NEXT variable1 This statement is the way to do one or more statements over and over again, a certain number of times, each time setting the value of some variable to a new value. First, the variable is set of the value of the first expression. Then the statements in the middle are executed. The NEXT statement indicates that the variable (note that this is the same variable that is part of the FOR statement) should be set to the next value; if the new value of the variable is greater than the second expression, the statements in the middle are skipped, and the next statement to be executed will be the one immediately following the NEXT statement. By default – i.e.if the STEP clause is omitted – the next value is always one (integer 1) greater than the previous value. The variable may be used in the statements between the FOR and NEXT statements, or not; sometimes you only need it to control how many times a thing is done, not use it for anything else. Examples: Print out the numbers from 1 to 10: FOR j = 1 TO 10 PRINT j NEXT j Print out every 3rd number from 1 to 20 (1, 4, 7, 10, 13, 16, and 19): FOR j = 1 TO 20 STEP 3 PRINT j NEXT j 3.3.6 DO xBasic User Manual Page 39 Syntax: DO { UNTIL | WHILE } expr statements LOOP or DO statements LOOP { UNTIL | WHILE } expr This statement is the way to do one or more statements over and over again, based on very general criteria. In either case in which the test is (or appears) syntactically before the controlled statements (that is, DO UNTIL expr or DO WHILE expr), the test is performed prior to executing the statements. In either case in which the test is (or appears) syntactically after the controlled statements (that is, LOOP UNTIL expr or LOOP WHILE expr) the test is performed after executing the statements and therefore the loop executes at least once no matter what the value of the expression is. The difference between WHILE and UNTIL is that WHILE performs the controlled statements as long as the value of the test expression remains true, whereas UNTIL performs the controlled statements as long as the value of the test expression remains false. IMPORTANT: Unlike the FOR statement, the DO statement in all its forms can very easily become an “infinite”, i.e. never-ending, loop! Specifically, if no statement(s) inside the loop alter any of the variables that make up the expression in the DO or LOOP statement, then the expression will never be altered, and can never become true (for UNTIL) or false (for WHILE). Even changing one or more variables that make up the expression doesn't guarantee that the expression will change from false to true or vice versa, so considerable care is required. xBasic User Manual Page 40 Examples: Get 128 bytes of data from somewhere (using a user-defined function): byteCount = 0 DO until byteCount = 128 CALL loadByte() byteCount = byteCount + 1 LOOP Get bytes of data from somewhere (using a user-defined function) until an EOF byte is encountered. As each byte comes in, store it in a buffer, and keep a count. Don't store the EOF in the buffer or include it in the count: DEF EOF = 0x0F i = 1 do until byte = EOF byte = getByte() if byte != EOF THEN buffer[i] = byte i = i + 1 END IF LOOP byteCount = i – 1 5 Language Summary This section summarizes the entire syntax of XBasic, using a format very similar to one known as Backus-Naur Form, or BNF. In each definition, or “production”, the first term is the one being defined, and it is shown in normal typeface. The actual syntax is shown in bold face. By contrast, the meta-syntax – those characters indicating denoting which pieces of actual syntax are optional or alternatives – are shown in normal case. Keywords are shown in ALL-UPPERCASE, although (as noted above) this is not a requirement of the language; it's just used here to help distinguish keywords from things that are not keywords. Terms that require further definition, and are defined below where they are used, are shown in italics. xBasic User Manual Page 41 As with BNF, brackets ( '[' and ']') enclose optional pieces of syntax – you can include them, or leave them out, either at your whim or as appropriate to the situation. Braces ( '{' and '}' ) enclose sets of alternatives, each alternative separated from its neighbor(s) by a vertical bar ( '|' ). A trio of dots or periods ( '...' ) is used to indicate that the previous piece of syntax may be repeated any number of times. 5.1 Labels Any statement may be preceeded by an identifier followed by a colon. This is called a label and can be the target of a GOTO statement. 5.2 Statements statements ::= statement | statements Note: This definition is somewhat informal. It means that the word “statements” (plural) as used in the syntax descriptions above mean either a single statement or more than one statement, each on a line by itself. 5.3 Statement statement ::= | REM comment text to end of line | OPTION TARGET = { "tile" | "bitmap" } | DEF name = value | DEF name ( [ arg [ , arg ] ... ] ) | END DEF | DIM variable-defs | [ LET ] l-value = expr | IF expr THEN statement | IF expr THEN | ELSE IF expr THEN | ELSE | END IF | SELECT | CASE | CASE ELSE xBasic User Manual Page 42 | | | | | | | | | | | | | | END SELECT FOR var = expr TO expr [ STEP expr ] NEXT var DO DO WHILE expr DO UNTIL expr LOOP LOOP WHILE expr LOOP UNTIL expr GOTO label function-name [ ( [ arg [ , arg ] ... ] ) ] PRINT STOP END 5.4 variable-defs variable-defs ::= variable-def | variable-defs , variable-def 5.5 variable-def variable-def ::= basic-variable-def [ = initializer ] 5.6 basic-variable-def basic-variable-def ::= variable [ AS type ] | variable ( size ) [ AS type ] 5.7 type type ::= xBasic User Manual Page 43 | | BYTE INTEGER STRING 8 5.8 initializer initializer ::= init-expr | { init-expr [ , init-expr ] ... } 5.9 init-expr init-expr ::= an expression composed of integer constants and possible also simple arithmetic operators (+, -, *, /) 5.10 expr In what follows, it may not always be clear that the punctuation marks that either are between one expr and another, or precede the expr, or surround the expr, are in bold face. They are, just like the keywords OR, AND, MOD, and so forth. As such they are required. Likewise it may not be clear that “0x” is in bold face. It is, and is a required part of hexadecimal constant. expr ::= | | | | | | | | | | | | | | xBasic User Manual expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr OR expr AND expr ^ expr | expr & expr = expr <> expr < expr <= expr >= expr > expr << expr >> expr + expr - expr Page 44 | | | | | | | | | | | | | expr * expr expr / expr expr MOD expr - expr NOT expr ~ expr ( expr ) decimal-constant 0xhex-constant string variable array-reference function-call 5.11 l-value l-value ::= array-reference | variable 5.12 array-reference array-reference ::= variable ( index ) 5.13 variable variable ::= name 5.14 function-call function-call ::= name [ ( [ arg [ , arg ] ... ] ) ] Note: The argument list, including the parentheses, may be omitted IFF the function does not require any arguments. xBasic User Manual Page 45 5.18 name name ::= | letter letter alphanums 5.19 decimal-constant decimal-constant ::= [ sign ] digit-string Note: The value of a decimal-constant must be in the range -32768 through 32767, inclusive. Spaces are not allowed within a decimal-constant. 5.20 digit-string digit-string ::= digit | digit digit-string Note: Spaces are not allowed within a digit-string. 5.21 hex-constant hex-constant ::= hex-digit | hex-digit hex-constant Note: The value of a hex-constant must be in the range 0x0000 through 0xFFFF, inclusive. Spaces are not allowed within a hex-constant. 5.22 string-constant string-constant ::= " printable-characters " Note: There is no specific limit to the length of a strong constant, only the practical limit of the available memory. The doublequotes, one at each end of the string constant, are required. xBasic User Manual Page 46 5.23 printable-characters printable-characters ::= printable-characters | printable-character printable-characters 5.24 printable-character printable-character ::= letter | digit | punctuation-mark | blank 5.25 alphanums alphanums ::= alphanum | alphanum alphanums 5.26 alphanum alphanum ::= xBasic User Manual letter | digit Page 47 The following define the which specific characters make up the syntactic items above. 5.27 letter letter ::= A | N | a | n | | | | B O b o | | | | C P c p | | | | D Q d q | | | | E R e r | | | | F S f s | | | | G T g t | | | | H U h u | | | | I V i v | | | | J W j w | | | | K X k x | | | | L Y l y | | | | M z m z 5.28 punctuation-mark punctuation-mark . | , | | ` | ~ | | _ | + | ::= : | ; | ! | ? | / | \ | ' @ | # | $ | % | ^ | & | * - | = | ( | ) | { | } | [ | ] | 5.29 hex-digit hex-digit ::= A | B | C | D | E | F | a | b | c | d | e | f | digit 5.30 digit digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 1Note also that if there is more than one of a particular kind of metavariable, they are subscripted for reference in the description. 3There are two forms of the DEF statement. One is a simple statement, requiring no other statements to be complete. That form is described in this section. The other form requires a matching END DEF statement, and is described in the Compound Statements section, below. 4There are two forms of the IF statement. One is a simple statement, requiring no other statements to be complete. That form is described in this section. The other form requires a matching END IF statement, and may also include ELSE or ELSE IF statements, and is described in the Compound Statements section, below. xBasic User Manual Page 48 5There are two forms of the DEF statement. One is a simple statement, requiring no other statements to be complete. That form is described in the Simple Statements section, above. The other form requires a matching END DEF statement, and is described in the this section. 6There are two forms of the IF statement. One is a simple statement, requiring no other statements to be complete. That form is described in the Simple Statements section, above. The other form requires a matching END DEF statement, and may also include ELSE or ELSE IF statements, and is described in the this section. xBasic User Manual Page 49