19.1 Introduction. 4tH 4tH Forth compiler
Publicitate
Publicitate
Chapter 19 uBasic manual
19.1
Introduction
uBasic is an integer Basic interpreter in the tradition of Tiny Basic, with which it is largely compatible. It is derived from a program made by Herbert Schildt in the late eighties and published in his book ”C: The complete reference”. This version is entirely written in 4tH, some bugs have been removed and several additional features have been added. In order to execute a program you can invoke it in the usual way:
4th cxq ubasic.4th lander.bas
Of course, you may opt to create an executable or script suited for your operating system.
19.2
Statements
• Labels are numeric, so 0020 refers to the same label as 20;
• Labels are optional unless there is a GOSUB or GOTO statement referring to them;
• Precedence, from high to low: (, ), unary -, ^, *, / , %, +, -, #, =, <, >;
• A compound statement may consist of a single statement;
• Whitespace is ignored;
• uBasic is not case sensitive.
C
ONCEPT
Label
Number
Function
Variable
Primitive
Operator
Term
Expression
D
EFINITION
Any positive integer
Any signed integer
ABS(), RND(), TOS(), POP()
A to Z
Any variable, function or number
#, =, <, >, *, /, +, - or %
(p o p)
(t o t)
369
A
BBREVIATION a n f v t e p o
CHAPTER 19. UBASIC MANUAL
C
ONCEPT
Array element
String
Quoted string
D
EFINITION
@ ( e )
Any combination of ASCII-7 characters
Any combination of ASCII-7 characters (except a double quote) between double quotes
Keyword IF, PRINT, REM, GOTO, GOSUB,
FOR, NEXT, LET, INPUT,
RETURN, END, STOP, PUSH,
CONTINUE, BREAK, DO, LOOP,
UNTIL, WHILE, ELSE, ENDIF
Correct use of a keyword Statement
Compound statement st : st : st
Line a c
A
BBREVIATION v s q k st c ln
370
LET v = e
GOTO e
GOSUB e
RETURN
IF e THEN c
ELSE
This statement assigns the value of the expression to the variable. The keyword LET is optional.
The GOTO statement permits changes in the sequence of program execution. Normally programs are executed in the numerical sequence of the program line numbers, but the next statement to be executed after a GOTO has the line number derived by the evaluation of the expression in the GOTO statement.
Note that this permits you to compute the line number of the next statement on the basis of program parameters during program execution. An error stop occurs if the evaluation of the expression results in a number for which there is no line.
The GOSUB statement is like the GOTO statement, except that uBasic remembers the line number of the GOSUB statement, so that the next occurrence of a RETURN statement will result in execution proceeding from the statement following the GO-
SUB. Subroutines called by GOSUB statements may be nested up to a predefined depth.
The RETURN statement transfers execution control to the line following the most recent unRETURNed GOSUB. If there is no matching GOSUB an error stop occurs.
The IF statement evaluates an expression. If the expression renders non-zero, the entire compound statement is executed; if zero, the associated compound statement is skipped. The keyword THEN is optional. If an IF statement does not contain a compound statement, multiple line syntax is assumed, requiring the use of the ENDIF keyword. A multiple line syntax block may not contain a single line syntax statement.
Will execute all statements following it until the keyword EN-
DIF is encountered, if the expression of the corresponding IF statement renders zero. Assumes multiple line syntax. The
ELSE clause is optional.
CHAPTER 19. UBASIC MANUAL 371
ENDIF
END
REM s
PUSH e[, e]
Terminates an IF statement in multiple line syntax.
The END statement must be the last executable statement in a program. The END statement may be used to terminate a program at any time, and there may be as many END statements in a program as needed.
The REM statement permits comments to be interspersed in the program. Its execution has no effect on program operation, except for the time taken.
Evaluates one or more expressions, separated by commas, and puts the results on the stack. The rightmost expression is top of stack.
PRINT q|e ,|;
INPUT q ,|; v The quoted string is printed. If the quoted string is omitted, a question mark is prompted. A new line is read in and the input line is scanned for a number. The value thus derived is stored in the variable.
FOR v = e TO e STEP e Sets up a control variable with an initial value (first expression ), an optional limit (second expression), an optional increment (third expression) and a looping address referring to the statement after the FOR statement. If STEP is omitted an increment of 1 is assumed. If TO and STEP are omitted, an infinite loop is assumed. If the initial value is greater than the limit, execution will continue after the matching NEXT. See NEXT.
NEXT v
DO
This statement prints the values of the expressions and/or the contents of the quoted strings. Expressions are evaluated and printed as signed numbers; strings are printed as they occur in the PRINT statement, except for the escape characters listed in table 13.4. When the items are separated by commas the printed values are justified in columns of 8 characters wide; when semicolons are used there is no separation between the printed items.
Thus, PRINT 1,2,3 prints as 1 2 3 and PRINT 1;2;3 prints as
123. Commas and semicolons, strings and expressions may be mixed in one PRINT statement at will. If a PRINT statement ends with a comma or semicolon, uBasic will not terminate the output line so that several PRINT statements may print on the same output line, or an output message may be printed on the same line as an input request (see INPUT). When the PRINT statement does not end with a comma or semicolon the line is terminated.
Increments the control variable and then jumps to the matching
FOR statement. The variable is optional. Every FOR statement must be matched by one single NEXT statement. Poor programming style, such as the use of GOTO to exit loops or conditional NEXT statements, may leave unused frames on the
FOR stack.
A control structure to repeat a set of statements an indefinite number of times, usually WHILE or UNTIL a condition is satisfied. See LOOP.
Descarcă
Publicitate