• Aucun résultat trouvé

Expressions

Dans le document Compiler Language (Page 20-0)

2 Identifiers, Numbers, and Expressions

2.6 Expressions

2.6 Expressions

Expressions consist of algebraic combinations of function references, variables, con-stants, and operators. Expressions evaluate to an integer, real, or string value. The following are examples of expressions.

cost + overhead * percent a*b/c(1.2+xyz)

last.nalTle$ + I I , II + first.narTle$

index% + 1

Table 2-1. Hierarchy of Operators

Hierarchy Operator Definition

2.6 Expressions CBASIC Compiler Reference Manual Arithmetic and relational operations work with integers and real numbers. An integer value converts to a real number if the operation combines a real and integer value.

The operation then uses the two real values, resulting in a real value. This is mixed-mode arithmetic.

Mixed-mode operations require more time to execute because the compiler generates more code. A mixed-mode expression always evaluates to a real value.

The power operator calculates the logarithm of the mantissa if the calculation uses real values. A warning results when the number to the left of the operator is negative because the logarithm of a negative number is undefined. The absolute value of the negative number is used to calculate the result. The exponent can be positive or negative.

If both values used with the power operator are either integer constants or integer variables, CBASIC calculates the result by successive multiplications. This allows you to raise a negative integer number to an integer power. With integers, if the exponent is negative, the result is zero. In all cases, 0 A 0 is 1, and 0 A X, where X is not equal to 0, is O.

If the exponent is an integer but the base is real, the integer converts to a real value before calculating the result. Likewise, if the exponent is real but the base is an integer quantity, CBASIC calculates the result using real values.

Only the relational operators and +, the concatenation operator, work with string variables. CBASIC does not support mixed string and numeric operations. The mne-monic relational operators (LT, LE, etc.,) are interchangeable with the corresponding algebraic operators

«,

<

=,

etc.). Relational operators result in integer values. A 0 is false and a -1 is true.

Logical operators AND, NOT, OR, and XOR operate on integer values and result in an integer number. The result is bitwise logical. If you use a real value with logical operators, it first converts to an integer.

CBASIC Compiler Reference Manual 2.6 Expressions If a numeric quantity exceeds the range from 32,767 to -32,768, you cannot represent it with a 16-bit two's complement binary number. Logical operations on such a number produce unpredictable results.

These are results of logical operations:

12 AND 3 =0 1100B AND 0101B

=4

NOT-1 =0 NOT3H

=

-4

12 OR 3 = 15 OCH OR 5H

=

13

12.4 XOR 3.2

=

15 12.4 XOR 3.7 =8

You can increase efficiency by using integer expressions instead of real expressions for relational tests and logical operations.

If a series of digits contains no decimal point or ends in a decimal point, the compiler attempts to store it as an integer. If the resulting number is in the range of CBASIC integers, the compiler treats it as an integer. If the constant is then required in an expression as a real number, the constant converts to a real number at run-time. For example,

x = X

+

1.

causes the integer constant 1 to convert to a real value before adding it to X. To eliminate this extra conversion, embed the decimal in the number as shown:

X

=

X + 1.0

Actually, there is very little difference in execution speed. A similar situation exists in the following statement:

Y"t. = X'x. + 1.0

In this case, the X% converts to a real number before adding it to the real constant.

2.6 Expressions CBASIC Compiler Reference Manual Generally, you should avoid mixed-mode expressions whenever possible, and do not use real constants with integer variables. CBASIC stores most whole numbers used in a program as integers. This provides the most effective execution.

If an overflow occurs during an operation between real values, an execution error occurs.

End of Section 2

Section 3

Statements and Functions

The syntax notation in this section uses the following typographical conventions to highlight the various elements that make up each statement and function.

• CAPS designate CBASIC Compiler keywords.

• Lower-case letters indicate variables.

• Italics identify syntactic items, such as expressions.

• Items enclosed in square brackets [ ] are optional.

• Items enclosed in braces { } are optional and can be repeated.

All other punctuation, such as delimiters and parentheses, must be included. The glossary in Appendix D contains general definitions of syntactic items such as expres-sion, file specification, and label.

'

~,,"

0:,

-, '

. "/", 0 '

ABS Function CBASIC Compiler Reference Manual

ABS Function

The ABS function returns the absolute value of a number.

Syntax:

x = ABS(numeric expression) Explanation:

The ABS function returns a real number. Integer expressions convert to real numbers.

Examples:

X = ABSC1S0) Y

=

ABSC-1S0)

IF ABSCTEMP.A-TEMP.B) < SAFE.LIMIT THEN CALL WARN.MSG

CBASIC Compiler Reference Manual ASC Function

ASC Function

The ASC function returns the ASCII decimal value of the first character in a string.

Syntax:

iO/o = ASC(string expression) Explanation:

ASC returns an integer between 0 and 255. The string must contain at least one character. An execution error occurs if the string expression evaluates to a null string.

Refer to Appendix B for a listing of ASCII symbols and corresponding numeric values. The inverse function of ASC is CHR$.

Examples:

PRINT ASC(A$

+

B$) SEND% = ASC(LAST.NAME$)

IF ASC(DIGIT$) > 47 AND ASC(DIGIT$) < 58 \

THEN PRINT "l)ALID DIGITS"

A TN Function CBASIC Compiler Reference Manual

A TN Function

The A TN function returns the arctangent of a number.

Syntax:

x

=

ATN(numeric expression) Explanation:

The A TN function is the inverse of the TAN function. A TN returns the angle, expressed in radians, whose tangent is the expression. A TN returns a real number.

Examples:

x = ATN(.S484) PI

=

3.14158

IF ATN(N) < PI/Z.O THEN \

PRINT "ANGLE LESS THAN 80 DEGREES"

PI = 3.14158 RADIANS

=

ATN(X)

DEGREES = RADIANS * lS0/PI

CBASIC Compiler Reference Manual ATTACH Function

ATTACH Function

The A IT ACH function returns a Boolean integer value indicating whether or not a specified printer is available for program use. If the printer is available, the function attaches it to the program.

Syntax:

i% = ATIACH(printer number) Explanation:

Use A IT ACH with concurrent or multiuser operating systems. The ATI ACH func-tion returns the value that the operating system returns after attempting to attach a specified printer. A logical false, 0, indicates that the printer is attached for program use. A IT ACH returns a logical false in systems that do not support multiple printers.

Once A IT ACH attaches a printer to a program, no other program can use that printer.

Examples:

IX.

=

ATTACH(Ll)

JX

=

ATTACH(PRINTER.NOX)

IF ATTACH(PRINTER.NOX) = TRUEX THEN GOTO MESSAGE LPRINTER

CALL PRINT.TABLE.OF.VALUES CALL PRINT.BAR.CHART

DETACH

CALL Statement CBASIC Compiler Reference Manual

CALL Statement

The CALL statement transfers program control to a multiple-line function.

Syntax:

CALL function name {(parameter list)}

Explanation:

The CALL statement passes parameters to and starts execution of the specified function. The address of the statement following the CALL statement is placed on a stack. A RETURN or FEND statement in the function sends control back to the statement following the CALL statement.

The parameter list is a list of expressions, variables, or constants. You must separate the expressions with commas. The number of parameters specified in a CALL statement must match the number of formal parameters in the function definition. Parameter data types in the CALL statement and function definition must also match. Numeric parameters convert from integer to real, or real to integer, as required.

The CALL statement cannot reference a single-line function or a program label.

Section 4 explains how to define and use functions.

Examples:

CALL CLEAR.SCREEN

CALL FN.CALC.TOTAL(SUB%)

CALL GET.REC(FILE.NM$, REC.NOk, AMOUNT)

CBASIC Compiler Reference Manual CHAIN Statement

CHAIN Statement

The CHAIN statement loads another program into memory and starts execution.

Syntax:

CHAIN filespec Explanation:

The CHAIN statement can load two types of programs: an overlay program gen-erated by the linker, or a directly executable file. CHAIN can load files gengen-erated by languages other than CBASIC. However, before you chain to an overlay file, the linker must create that overlay and the root program at the same time.

The filespec can be a string expression, a variable, or a constant. The compiler assumes a filetype of .OVL if not specified otherwise in the filespec. Refer to the Programming Guide for more information on chaining modules and programs.

Examples:

CHA I N 116: AtJERAGES II

CHAIN NEW.PROG$

TOTALS$

=

II ACCOUNTS. OtJL II

CHAIN CDRIVE$ + TOTAL$

CHR$ Function CBASIC Compiler Reference Manual

CHR$ Function

The CHR$ function returns a one character string. The string is a single ASCII character that has the specified ASCII decimal value.

Syntax:

a$

=

CHR$(numeric expression) Explanation:

The expression contains the ASCII decimal value of the character. If the expression is real, CHR$ converts it to an integer.

Refer to Appendix B for a listing of ASCII symbols and corresponding numeric values. The ASC function is the inverse function of CHR$.

Examples:

REM BEEP THE TERMINAL PRINT CHR$(7)

LINEFEED% = 10

PRINT CHR$(LINEFEED%)

IF CHR$(INP(IN.PORT%» = IIAII THEN GOSUB 100

CBASIC Compiler Reference Manual CLOSE Statement

CLOSE Statement

The CLOSE statement closes disk files.

Syntax:

CLOSE file number{,file number}

Explanation:

The CLOSE statement closes the files, releases the file numbers, and frees all buffer space that the files used. A file must first be activated with a CREATE or OPEN statement before using a CLOSE statement. An IF END statement assigned to a closed file has no further effect unless you reassign the file number in a CREATE or OPEN statement.

The file number is a unique identification number you assign to a file with the CREATE or OPEN statement. File numbers can be any numeric expression. If file numbers evaluate to real values, they convert to integers.

STOP statements automatically close all active files. A run-time error does not close files.

Examples:

CLOSE 2

CLOSE 5 t 12 t 20

CLOSE UPDATE.FILE%t OLD.MASTER.FILE%t NEW.MASTER.FILE%

COMMAND$ Function CBASIC Compiler Reference Manual

A command line is the line that you enter at the keyboard telling the operating system to run a program. A command line consists of a command keyword and an optional command tail. The command keyword identifies the program to execute. The command tail can contain extra information for the program such as a filename, option, or parameter.

The COMMAND$ function does not return the command keyword. COMMAND$

eliminates all blanks preceding the first character in the command tail and converts all characters to upper-case. For the following command lines,

PAYROLL no checKs totals PAYROLL NOCHECKS TOTALS ACCOUNTS nochecKs TOTALS

COMMAND$ returns the string:

NOCHECKS TOTALS

CBASIC Compiler Reference Manual COMMON Statement

COMMON Statement

The COMMON statement specifies variables to retain in memory for use by chained programs.

Syntax:

COMMON variable{,variable}

Explanation:

Only blank lines, REM statements, and data type declaration statements can precede COMMON statements.

The compiler treats all COMMON statements in a program as one consecutive list of variables. Therefore, a program can contain any number of COMMON statements.

All COMMON statements taken as a group must have the same number of variables in each chained program. Each COMMON statement in a chained program can contain a different number of variables if the total number of variables matches for all chained programs. The position of each variable and data type must match in each chained program. Dimensioned variables must have the same number of subscripts.

For array variables, place the number of subscripts in parentheses after the array name. The COMMON statement does not indicate the size of the subscript. Be sure to allocate array space with a DIM statement before referencing an array variable in COMMON. The first program requiring access to the array must contain the DIM statement. Subsequent programs can access the array without affecting the data.

If a DIM statement executes a second time for the same array, the original data is lost. However, elements in a string array are not released from memory. Set string array elements to null strings before reexecuting a DIM statement for the same string array. Refer to the DIM statement for information on setting array elements to null.

Examples:

COMMON X

CONCHAR % Function CBASIC Compiler Reference Manual

CONCHAR

%

Function

The CONCHAR % function reads one character from the console keyboard and returns the decimal ASCII representation of that character.

Syntax:

i%

=

CONCHAR % Explanation:

CONCHAR % waits for a character to be entered at the console keyboard, then displays the character on the console screen before returning the ASCII decimal value.

However, if the ASCII decimal value is less than 32, CONCHAR % does not display the character.

The low-order eight bits of the returned value comprise the binary ASCII represen-tation. The high-order eight bits are always zeros. The value returned is a decimal integer. Refer to Appendix B for a listing of ASCII symbols and corresponding numeric values. The INKEY function performs the same task that CONCHAR % performs except INKEY does not display the character on the console screen.

Examples:

ALPHA% = CONCHAR%

IF CONCHAR%

=

48 THEN GOSUB 1000

PRINT CHR$(CONCHAR%)

CBASIC Compiler Reference Manual CONSOLE Statement

CONSOLE Statement

The CONSOLE statement directs program output to the console screen.

Syntax:

CONSOLE Explanation:

CBASIC maintains a special print control flag to determine whether output from a PRINT statement is displayed on a console screen or printer. The CONSOLE and LPRINTER statements set and reset the flag. You cannot access the print control flag directly.

Initially, the flag is set to logical false and output from PRINT statements displays on the console screen. LPRINTER sets the flag to logical true so information can be printed on a list device. The CONSOLE statement resets the flag to false and redirects output to the console screen.

The print control flag does not affect INPUT statement prompt strings. Prompt strings always print on the console screen.

If the current output column is not 1, both CONSOLE and LPRINTER send a carriage return line-feed prior to changing the print control flag. Refer to the LPRINTER . statement for more information.

Examples:

CONSOLE 500 CONSOLE

IF LST.REQUEST THEN LPRINTER \

ELSE CONSOLE

CONSTAT% Function CBASIC Compiler Reference Manual

CONST A TO/o Function

The CONST ATO/o function returns a logical value signifying console status.

Syntax:

iO/o = CONSTAT%

Explanation:

Use CONST AT% to determine if the console has a ready status. Ready status means a character has been entered at the console keyboard but has not been read by the program. CONSTATO/o returns a -1 or logical true if the console is ready. Otherwise, CONST AT% returns a zero or logical false.

Examples:

IF CONSTAT%

GOSU6 95

THEN \

REM PROCESS OPERATOR INTERRUPT PRINT "PRESS ANY KEY TO CONTINUE"

WHILE NOT CONSTAT%

WEND

CBASIC Compiler Reference Manual COS Function

COS Function

The COS function returns the cosine of a number.

Syntax:

x = COS (numeric expression) Explanation:

All CBASIC trigonometric functions require that you specify the numeric expression in radians. Integers convert to real numbers. The cosine value returned is a real number.

Examples:

1% = COSC3.14159)

IF COSCANGLE) = 0.0 THEN VERTICAL% = TRUE%

PI = 3.14159

INPUT "ENTER DEGREE l)ALUE ••

til;

DEGREES RADIANS = DEGREES * PI/1S0.0

X = COS(RADIANS)

CREATE Statement CBASIC Compiler Reference Manual

Use CREATE statements to create either stream or fixed disk files.

To make a stream file, CREATE requires a filespec and a file number. The filespec can be a string expression, variable, or constant. The file number is a unique integer identification number ranging from 1 to the current implementation limit for the num-ber of files accessible at one time. Refer to Appendix A of the Programming Guide for the current limit. Place the file number in a CREATE statement after the keyword AS.

To create a fixed file, specify the record length with the RECL parameter in addition to the filespec and file number.

The BUFF option assigns additional internal buffers. CREATE assumes a default value of 1 buffer if not specified otherwise. The BUFF parameter must specify 1 if you access the file randomly.

CBASIC supports three different modes for accessing files: LOCKED, UNLOCKED, and READONL Y. Use the mode parameter under multiuser or concurrent operating systems. If you CREATE a file in LOCKED mode, no other program or user can access that file. UNLOCKED mode allows more than one program or user to access the file. READONL Y files allow more than one program or user to read the file. An-other program or user cannot modify the data inside a READONL Y file. You cannot CREATE a file in READONL Y mode. Use READONL Y with the OPEN statement.

CBASIC Compiler Reference Manual CREATE Statement Examples:

CREATE IISALES.FEBII AS 1

CREATE IIB:TEST.DAT II RECL 250 AS 20

CREATE ACCDUNT.MASTER$ RECL 500 AS 12 BUFF

a

CREATE liB: II + NAME$ + II II + LEFT$ (STR$ (CURR. WDRK'X,) t 3) \ AS CURR. WORK'X,

CREATE II FILE. OAT II AS NUM'X, BUFF (MFREI 128)

DATA Statement CBASIC Compiler Reference Manual

The constant list in a DATA statement can be any combination of integer, real, and string constants. However, data types for the constants in the DATA statements and the corresponding variables in the READ statements must match. Real constants assigned to integer variables by a READ statement are truncated to the integer portion of the real number.

DATA statements can span more than one physical line using the backslash contin-uation character, but cannot appear on the same line with other statements. The continuation character can appear in string constants enclosed in quotation marks.

However, string constants do not require quotation marks. Delimit each constant with a comma or a carriage return line-feed.

DATA statements are nonexecutable statements that can appear anywhere in a program. CBASIC treats all DATA statements in a program as one consecutive list of constants.

See the READ and RESTORE statements for additional information.

Examples:

DATA 3,25,

la,

8, S8, 181,

a

DATA one, two, three,

a,

5, 8.0

DATA 7.0, ei9'htf 8,10

DATA 331.5 f IIVIOLET", a58.2, IIBLUE II , \ 583.7, IIYELLOW", 81a.8, IIREDII DATA IIABC\DEF II

CBASIC Compiler Reference Manual DEF Statement

DEF Statement

The DEF statement defines both single-line and multiple-line functions.

Syntax:

Single-line:

DEF function name[(formal parameters)]

=

expression Multiple-line:

DEF function name[(formal parameters)]\

[EXTERNAL or PUBLIC]

[declaration statements]

CBASIC statements

RETURN FEND

Explanation:

A function definition must occur in a program before using the function. To define a function, the word DEF must precede the function name.

Single-line function definitions use an equal sign followed by an expression. The expression contains the actual process that the single-line function performs. The data types used in the expression must correspond to the data type used in the function name.

Multiple-line function definitions include optional data declarations and any number of statements. A DEF statement precedes the declaration group, and a FEND statement

DEF Statement CBASIC Compiler Reference Manual In both cases, formal parameters hold a place for actual parameters specified in the function reference. A formal parameter is either a string variable or numeric variable;

it is never a constant. If a formal parameter is a string variable, the actual parameter must be a string expression. If the formal parameter is numeric, the actual parameter must be numeric. However, real numbers convert to integers and integers convert to real as required.

All formal parameters and any variables declared in the declaration group are local to the function. Labels defined in a multiple-line function are local to that function.

Refer to Section 4 for complete information on defining and using functions.

Examples:

CBASIC Compiler Reference Manual DELETE Statement

DELETE Statement

DELETE Statement

Dans le document Compiler Language (Page 20-0)

Documents relatifs