Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Conklin E.K.Forth programmer's handbook.2000.pdf
Скачиваний:
321
Добавлен:
23.08.2013
Размер:
2.04 Mб
Скачать

Forth Programmer’s Handbook

\Rates are used as offsets into arrays 0 CELLS CONSTANT FULL

1 CELLS CONSTANT LOWER

2 CELLS CONSTANT LOWEST

\Array-defining word

: FOR CREATE DOES> ( rate - charge-per-minute) + @ ;

\Table comprises three arrays

\Charge-per-minute at FULL LOWER LOWEST rate

FOR FIRST

30

,

22

,

12

,

FOR

+MINUTES

20

,

15

,

9

,

FOR

DISTANCE

12

,

10

,

6

,

90 CONSTANT ASSISTANCE

\ Charge

for

operator assistance

: ?ASSISTANCE

( flag -

charge)

ASSISTANCE AND ;

: ADDITIONAL

( #minutes-1 rate -

charge)

+MINUTES * ;

: MINUTES

( #minutes rate -

charge)

 

 

DUP FIRST

ROT 1- ROT

ADDITIONAL

+ ;

 

: MILES

( distance #minutes

rate

- charge)

DISTANCE * * ;

: TOTAL

( distance #minutes

rate

assistance-flag - charge)

?ASSISTANCE >R 2DUP

MINUTES >R

MILES

2R> + + ;

Stack music. Unreadable?

Rule:

9. Feature the stack machine.

6.3.7 Summary

How do we feel about these rules? Are any of them helpful? Hurtful? Are there better rules? Do we want rules anyway? These are questions for you to answer, should you so choose.

6.4 NAMING CONVENTIONS

Table 10 presents some naming conventions that have been widely used in Forth for many years. These take advantage of Forth’s flexible naming rules to use special characters to convey additional meaning.

In this table, the word name refers to some word the programmer has chosen to

Programming Style 197

Forth Programmer’s Handbook

represent a Forth routine.

Where possible, a prefix before a name indicates the type or precision of the

!value being operated on, whereas a suffix after a name indicates what the value is or where it’s kept.

Table 10: Naming conventions

Prefixes

Meaning

Examples

!name

Store into name

!DATA

#name

Size or quantity

#PIXELS

 

Output numeric operator

#S

 

Buffer name

#I

'name

Address of name

'S

 

Address of pointer to name

'TYPE

(name)

Internal component of name, not normally

(IF)

 

user-accessible

(FIND)

 

Run-time procedure of name

(:)

 

File index

(PEOPLE)

*name

Multiplication

*DIGIT

 

Takes scaled input parameter

*DRAW

+name

Addition

+LOOP

 

Advance

+BUF

 

Enable

+CLOCK

 

More powerful

+INITIALIZE

 

Takes relative input parameters

+DRAW

-name

Subtract, remove

-TRAILING

 

Disable

-CLOCK

 

not name (opposite of name)

-DONE

 

Returns reversed truth flag

-MATCH

 

(1 is false, 0 is true)

 

 

Pointers, especially in files

-JOB

198 Programming Style

 

Forth Programmer’s Handbook

Table 10:

Naming conventions (continued)

 

 

 

 

Prefixes

Meaning

Examples

.name

Print named item

.S

 

Print from stack in named format

.R .$

 

Print following string

." string"

 

May be further prefixed with data type

D. U. U.R

/name

Division

/DIGIT

 

Initialize routine or device

/COUNTER

 

“per”

/SIDE

1name

First item of a group

1SWITCH

 

Integer 1

1+

 

One-byte size

1@

2name

Second item of a group

2SWITCH

 

Integer 2

2/

 

Two-cell size

2@

;name

End of something

;S

 

End of something, start of something else

;CODE

<name

Less than

<LIMIT

 

Open bracket

<#

 

From device name

<TAPE

<name>

Name of an internal part of a device driver

<TYPE>

 

routine

 

>name

Towards name

>R, >TAPE

 

Index pointer

>IN

 

Exchange, especially bytes

>< (swap bytes)

 

 

>MOVE< (move,

 

 

swapping bytes)

?name

Check condition, return true if yes

?TERMINAL

 

Conditional operator

?DUP

 

Check condition, abort if bad

?STACK

 

Fetch contents of name and display

?N

Programming Style 199

Forth Programmer’s Handbook

Table 10:

Naming conventions (continued)

 

 

 

 

Prefixes

Meaning

Examples

@name

Fetch from name

@INDEX

Cname

One-byte character size, integer

C@

Dname

Double-cell integer

D+

Mname

Mixed single and double operator

M*

Tname

Three-cell size

T*

Uname

Unsigned encoding

U.

[name]

Executes at compile time

[']

\name

Unsigned subtraction (ramp-down)

\LOOP

name!

Store into name

B!

name"

String follows, delimited by "

ABORT" xxx"

name,

Put something into dictionary

C,

name:

Start definition

CASE:

name>

Close bracket

#>

 

Away from name

R>

name?

Same as ?name

B?

name@

Fetch from name

B@

200 Programming Style

Соседние файлы в предмете Электротехника