Forth
パラダイム 手続き型、スタック指向
登場時期 1970年代
設計者 チャールズ・ムーア
型付け なし
主な処理系 Forth, Inc., GNU Forth, MPE
影響を受けた言語 バロースの大型システムLISP, APL
影響を与えた言語 PostScript[1]FactorJoy, Cat
テンプレートを表示

Forth Forth 

  Forth () Forth  threaded code 

 Forth Forth  Open Firmware [2]使GNUGforth2008121994 Forth 200x [3]

概要

編集

Forth (words)()Forth CUnixForth Forth  BBC Micro  Apple II 使CForth 

スタック

編集

(stack)(C call-by-value )Forth call-by-value (lincage)(return)rstack  rstack 使使Forth

FORTH

使

保守

編集

Forth CAD Forth 使[4] Forth [5]

使Forth ()使RISCForth [6]  Forth AppleIBMOLPC XO-1使Open Firmware FreeBSDFICL-based first stage boot controller

歴史

編集

Forth 1958[7] 1968FORTRAN使ForthForth 1970 Elizabeth Rather [7]1971FORTHNRAOElizabeth RatherFORTH, Inc. 197310 Forth 

1968"[t]he file holding the interpreter was labeled FOURTH, for 4th (next) generation software  but the IBM 1130 operating system restricted file names to 5 characters."[8] Forth  compile-link-go  Forth 使FORTRANBASIC4fourth5FORTH

Forth 使Forth1978 Intel 8086 MacFORTH1984 Apple Macintosh[7]

Forth, Inc  microFORTH 1976Intel 8080, Motorola 6800, and Zilog Z80 MicroFORTH   1978 6502  Forth 使 FORTH-79[9]  FORTH-83[10] 197919831994 ANSI ANS Forth[11] (ANSI INCITS 215-1994 (R2001)ISO/IEC 15145:1997(E))

Forth 1980使[12]Jupiter ACE ROM Forth Canon Cat   Forth 使 Rockwell  Forth  R65F11  R65F12 

標準規格

編集
  • Forth-77 Standard
  • Forth-78 Standard
  • Forth-79 Standard
  • Forth-83 Standard
  • ISO/IEC 15145:1997(E) - Information technology - Programming languages - Forth (First edition: 1997-04-15)

プログラマの観点

編集

Forth  使()Hewlett-Packard 使Forth 使

 (25 * 10 + 50) 
25 10 * 50 + . <cr> 300 ok
 

2510

 

 * 

 

50

 

 + . [13]

 4 5 + .

452


 4 5 + . 9 ok


Forth 
: FLOOR5 ( n -- n' )   DUP 6 < IF DROP 5 ELSE 1 - THEN ;

 使 FLOOR5 (使)DUP66 < (6 DUP )IFELSEDROPTHEN FLOOR5 C
int floor5(int v) { return v < 6 ? 5 : v - 1; }


: FLOOR5 ( n -- n' ) 1- 5 MAX ;


1 FLOOR5 . <cr> 5 ok
8 FLOOR5 . <cr> 7 ok

(18) FLOOR5 .

機能

編集

Forth 使(dictionary)[14]

:;()
: X DUP 1+ . . ;

 X10 X 11 10 [15]

foo
 : foo 4 5 + . ;

FORTHfoo

FORTH+.2FORTHFORTH

使


 Forth Forth 使Forth Forth (0..n, 使)使使S [16]

オペレーティングシステムとファイル、マルチタスク

編集

 Forth 使 BLOCK 1Forth 使

 cooperative ( ANSI Forth ) PAUSE  使Forth  Intel 8051, Atmel AVR, and TI MSP430[17]

Microsoft WindowsLinuxUnix Forth ANSI Forth  I/O 使OSForth PAUSE 

セルフコンパイルとクロスコンパイル

編集

すべてのソースコードとともに十分な機能を有する Forth システムは自身をコンパイルすることができ、Forth プログラマはこのようなテクニックを普通メタ・コンピレーション(meta-compilation)と呼ぶ(ただし、この用語は普通の定義であるメタコンピレーションとは厳密には一致しない)。通常の方法はコンパイルされたビットをメモリにを配置する一握りのワードの再定義である。コンパイラのワードはメモリ内のバッファエリアにリダイレクトされることができるフェッチとストアの、特別に命名されたバージョンを使う。このバッファエリアはコードバッファというより異なるアドレスから始まるメモリ領域へのシミュレートやアクセスをする。このコンパイラは対象のコンピュータのメモリとホストの(コンパイルする)コンピュータのメモリの両方にアクセスするワードを定義する。[18]

フェッチやストア操作がコード空間に再定義されたあと、コンパイラやアセンブラなどはフェッチやストアの新たな定義を使って再コンパイルされる。これはコンパイラとインタプリタのすべてのコードの効果的な再利用である。それから、Forth システムのコードはコンパイルされるが、このバージョンはバッファに格納される。このメモリ内のバッファはディスクに書きこまれ、これをテストのために一時的にメモリにロードする方法が提供される。新たなバージョンがきちんと機能するようなら、これは以前のバージョンに上書きされる。

異なる環境のためのバリエーションが多数存在する。組み込みシステム向けには、代わりに他のコンピュータのためにコードが書かれることになるが、このテクニックはクロスコンピレーションとして知られ、シリアルポートや単独の TTL ビット越しでさえ、その上オリジナルのコンパイルするコンピュータのワード名やディクショナリの他の実行されない部分も維持する。このようなForthコンパイラのための最小の定義は バイト単位のフェッチやストアをするワードと、実行される Forth ワード を命令するワードである。しばしばもっとも多くの時間のかかるリモートのポートへの書き込みの部分は、フェッチやストア、実行を実装するための初期化プログラムの構築であるが、多くの現代的なマイクロプロセッサ(Motorola CPU32など)は、このタスクを排除する統合されたデバッグ機能を持っている[19]

言語の構造

編集

Forth( NULL )

(NF) (LF)(CF)(PF)

Forth Forth[20]

ディクショナリのエントリ

編集

[21]



structure
  byte:       flag           \ 3bit flags + length of word's name
  char-array: name           \ name's runtime length isn't known at compile time
  address:    previous       \ link field, backward ptr to previous word
  address:    codeword       \ ptr to the code to execute this word
  any-array:  parameterfield \ unknown length of data, words, or opcodes
end-structure forthword

(32)Forth NUL('\0')



  Forth (instruction pointer, IP) IP call/return 

コンパイラの構造

編集

  Forth 

threaded [15]

 IFand WHILE Forth  Forth BRANCH?BRANCH (false)使
... DUP 6 < IF DROP 5 ELSE 1 - THEN ...


... DUP LIT 6 < ?BRANCH 5  DROP LIT 5  BRANCH 3  LIT 1 - ...

BRANCHLIT

コンピレーションステートとインタープリテーションステート

編集

 : () (, colon definition) (interpretation semantics)(compilation semantics)[15]

;();  ANS Forth 使使[15]

 [ ()  ] () LITERAL 使LITERAL 

ANS Forth STATE true false (state-smart words)

イミディエイトワード

編集

IMMEDIATE (immediate word)[22] ; ANS Forth  POSTPONE Forth-83  COMPILE  [COMPILE] 

無名ワードと実行トークン

編集

ANS Forth  :NONAME ; ()(execution token)C

 EXECUTE  COMPILE,(COMPILE )

 ' (tick)' RANDOM-WORD EXECUTE  RANDOM-WORD [23]

ワードの構文解析とコメント

編集

The words : (colon)POSTPONE ' (tick) :NONAME (parsing words)使 ( () \ ()使( ()  \ ()

コードの構造

編集

 Forth  threaded code  FIG (Forth Interest Gruop) Forth TIL (Threaded Interpretive Language) indirect-threaded code direct-threaded  subroutine threaded Forths 使 Forth  subroutine threading 使 peephole optimization [24]

データオブジェクト

編集

CP"defining behavior"()

VARIABLE

one-cell memory location VARIABLE 

CONSTANT

(CONSTANT)

CREATE



Forth I/O

Forth (Forth )Forth 使()使使[25]

Forth 使使

プログラミング

編集

Forth で書かれたワードは実行可能な形式にコンパイルされる。古典的な indirect threaded 実装は、順に実行されるワードのアドレスのリストをコンパイルする。多くの現代的なシステムは実際のマシンコードを生成する(いくつかの外部ワードの呼び出しと、適当な場所に展開された他者のためのコードを含む)。最適化コンパイラをもつシステムもある。一般的に言えば、Forth プログラムはコンパイルされたバージョンがロードされたとき実行される単独のコマンド(e.g., RUN)でコンパイルされたプログラムのメモリイメージとして保存される。

開発中、プログラマは小さなコード片をそれが開発されたときに実行およびテストするのにインタプリタを使う。そのためほとんどの Forth プログラマは 緩やかなトップダウン設計と、絶え間ないテストと結合によるボトムアップ開発を支持する。[26]

トップダウン設計はたいてい最終的なプログラムを書くためのツールの高レベルなセットとして使われる、「語彙」(vocabularies)へのプログラムの分割である。よく設計された Forth プログラムは自然言語のように読め、ただひとつではない解決策だが、しかし関係する問題を解決するためのツールのセットもまた実装する[27]。  

コード例

編集

Hello world

編集

For an explanation of the tradition of programming "Hello World", see Hello world program.


: HELLO  ( -- )  CR ." Hello, world!" ; HELLO <cr>
HELLO

 CR(Carriage Return)  ." (dot-quote)  Hello, world!  ."  ."  Forth 

 Forth  Forth 
CR .( Hello, world!)

.( (dot-paren) ."Hello, world! .( 

 CRForth okForth  CR

コンピレーションステートとインタープリテーションステートの混用

編集

  Q EMIT-Q 
: EMIT-Q   81 ( the ASCII value for the character 'Q' ) EMIT ;

 QASCII(81)使 EMIT 

 EMIT-Q [ ()] (), CHAR LITERAL 使 QAscii
: EMIT-Q   [ CHAR Q ]  LITERAL  EMIT ;

 CHAR  [CHAR]  CHAR  [CHAR]使 EMIT-Q 
: EMIT-Q   [CHAR] Q  EMIT ; \ Emit the single character 'Q'

 \ () 使 

CHAR  [CHAR] ANS Forth IMMEDIATE  POSTPONE 使[CHAR] 
: [CHAR]   CHAR  POSTPONE LITERAL ; IMMEDIATE

完全な RC4 暗号プログラム

編集

1987Ron Rivest  RC4  RSA Data Security, Inc. 

 256 (:) 使  i j 0   i1  ji i  j (:XOR使)i  j  XOR   0  255 (:) i  j使i jji  j i  j 0   256 

http://ciphersaber.gurus.com 

 Forth 使
0 VALUE ii
0 VALUE jj
CREATE S[] 256 CHARS ALLOT
: ARCFOUR  ( c -- x )
 ii 1+ DUP TO ii 255 AND  ( -- i )
 S[] +  DUP C@   ( -- 'S[i] S[i] ) 
 DUP jj +  255 AND DUP TO jj ( -- 'S[i] S[i] j )
 S[] +  DUP C@ >R   ( -- 'S[i] S[i] 'S[j] )
 OVER SWAP C!        ( -- 'S[i] S[i] )
 R@ ROT C!   ( -- S[i] )
 R> +    ( -- S[i]+S[j] )
 255 AND S[] + C@   ( -- c x )
 XOR ;
: ARCFOUR-INIT ( key len -- )
 256 MIN LOCALS| len key |
 256 0 DO  I  S[] I + C!  LOOP
 0 TO jj 
 256 0  DO   ( key len -- )
    key I len MOD + C@  
    S[] I  + C@  + jj + 255 AND TO jj
    S[] I  + DUP C@  SWAP ( c1 addr1 )
    S[] jj + DUP C@  ( c1 addr1 addr2 c2 ) ROT C! C!
      LOOP
 0 TO ii  0 TO jj ;


CREATE KEY: 64 CHARS ALLOT
: !KEY ( c1 c2 ... cn n—store the specified key of length n )
	DUP 63 U> ABORT" key too long (<64)"
	DUP KEY: C! KEY: + KEY: 1+ SWAP ?DO  I C!  -1 +LOOP ;
 
	HEX  61 8A 63 D2 FB  5 !KEY
	KEY: COUNT ARCFOUR-INIT
	CR 
	   DC ARCFOUR 2 .R SPACE
	   EE ARCFOUR 2 .R SPACE
	   4C ARCFOUR 2 .R SPACE
	   F9 ARCFOUR 2 .R SPACE
	   2C ARCFOUR 2 .R
	CR .( Should be: F1 38 29 C9 DE )

実装

編集

Forth 仮想マシンは実装が単純で規格のリファレンス実装を持たないため、大量の言語実装が存在する。標準的な各種デスクトップコンピュータシステム(POSIX, Microsoft Windows, Mac OS X)をサポートしていることに加え、これらの多くの Forth システムは各種の組み込みシステムもまた対象としている。1994年の ANS Forth 規格に準拠するさらに有名ないくつかのシステムが列挙する。

FORTHに類するもの

編集

PostScriptForthForthAdobe Illustrator使

JavaForth

ForthMind

ForthMac OSMopsPalo Alto Shipping Co.Mach1

ForthForth

Open FirmwareForthOpenBootROMMacintoshOpen Firmware

ForthFiclFreeBSD使

TeleScript General MagicForth

 80x87 Forth

1985JUN4D-Box0DLForth



関連項目

編集

脚注

編集


(一)^ First (English) (paperback). PostScript Language - Tutorial and Cookbook. Adobe Systems Inc. (First ed.). Addison-Wesley. (1985). pp. 243. ISBN 0-201-10179-3. "The PostScript language contains elements of many programming languages, but most closely resembles the Forth language." 

(二)^ NASA applications of Forth

(三)^ Forth 200x standards effort

(四)^ Forth Success Stories. 200669

(五)^ Space Related Applications of Forth. 200794

(六)^ Forth Chips Page. pp. 54. 200669

(七)^ abcThe Evolution of Forth. ACM SIGPLAN Notices, Volume 28, No. 3. March, 1993. ACM SIGPLAN History of Programming Languages Conference (19934). Template:Cite web accessdate 

(八)^ Moore, Charles H (1991). Forth - The Early Years. 200663

(九)^ The Forth-79 Standard (PDF). Template:Cite web accessdate 

(十)^ The Forth-83 Standard. Template:Cite web accessdate 

(11)^ Programming Languages: Forth. ANSI technical committee X3J14 (1994324). 200663

(12)^ The Forth Language, BYTE Magazine 5(8), (1980) 

(13)^ Brodie, Leo (1987) (paperback). Starting Forth (Second ed.). Prentice-Hall. pp. 20. ISBN 0-13-843079-9 

(14)^ Brodie, Leo (1987) (paperback). Starting Forth (Second ed.). Prentice-Hall. pp. 14. ISBN 0-13-843079-9 

(15)^ abcdBrodie, Leo (1987) (paperback). Starting Forth (Second ed.). Prentice-Hall. pp. 16. ISBN 0-13-843079-9 

(16)^ Rodriguez, Brad. B.Y.O.ASSEMBLER. 2006619

(17)^ Rodriguez, Brad. MULTITASKING 8051 CAMELFORTH (PDF). 2006619

(18)^ Rodriguez, Brad (19957). MOVING FORTH. 2006619

(19)^ Shoebridge, Peter (19981221). Motorola Background Debugging Mode Driver for Windows NT. 2006619

(20)^ Martin, Harold M. (19913). Developing a tethered Forth model. ACM Press. 2006619

(21)^ Brodie, Leo (1987) (paperback). Starting Forth (Second ed.). Prentice-Hall. pp. 200202. ISBN 0-13-843079-9 

(22)^ Brodie, Leo (1987) (paperback). Starting Forth (Second ed.). Prentice-Hall. pp. 273. ISBN 0-13-843079-9 

(23)^ Brodie, Leo (1987) (paperback). Starting Forth (Second ed.). Prentice-Hall. pp. 199. ISBN 0-13-843079-9 

(24)^ Ertl, M. Anton; Gregg, David. Implementation Issues for Superinstructions in Gforth (PDF). 2006619

(25)^ Brodie, Leo (1987). Under The Hood (paperback). Starting Forth (2nd ed.). Prentice-Hall. pp. 241. ISBN 0-13-843079-9. "To summarize, there are three kinds of variables: System variables contain values used by the entire Forth system. User variables contain values that are unique for each task, even though the definitions can be used by all tasks in the system. Regular variables can be accessible either system-wide or within a single task only, depending upon whether they are defined within OPERATOR or within a private task." 

(26)^ Brodie, Leo (1984) (paperback). Thinking Forth. Prentice-Hall. ISBN 0-13-917568-7 

(27)^ The classic washing machine example describes the process of creating a vocabulary to naturally represent the problem domain in a readable way.

参考文献

編集

外部リンク

編集