pForth 參考手冊(pForth Reference Manual -- with Chinese Translation)


pForth - 一套用 ANSI C 寫的、移植性高、符合 ANSI 規格的 Forth.
pForth - a Portable ANSI style Forth written in ANSI 'C'. 
Last updated: August 20th, 1998 V20


中譯版本: 0.01.00
中譯者: Cnoize Chuang

系統主要由 Phil Burk 及 Larry Polansky, David Rosenboom 開發. 特別感謝下列的貢獻者: Darren Gibbs, Herb Maeder, Gary Arakaki, Mike Haas.

by Phil Burk with Larry Polansky, David Rosenboom. Special thanks to contributors Darren Gibbs, Herb Maeder, Gary Arakaki, Mike Haas.

PForth 原始碼是可以自由地使用的. 作者也提供定製 pForth 系統的服務: 移植到新平台, 或是在有契約的型式下發展 pForth 的應用程式. 如果有需要, 可以 從 philburk@softsynth.com 連絡 Phil Burk.

PForth source code is freely available.  The author is available for customization of pForth, porting to new platforms, or developing pForth applications on a contractual basis.  If interested, contact  Phil Burk at philburk@softsynth.com

回到(Back to) pForth 首頁(pForth Home Page)

版權宣告(LEGAL NOTICE)

pforth 軟體的程式碼開放為公眾的財產, 任何人都可以重製、散布和修改 pforth 軟體的程式碼和任何衍生的成果. 並且不需付出任何的報酬及受限於軟體的授權. pforth 的程式碼是在沒有任何的擔保及限制的條件下開放的. (後面的條文我無法 理解)

The pForth software code is dedicated to the public domain, and any third party may reproduce, distribute and modify the pForth software code or any derivative works thereof without any compensation or license. The pForth software code is provided on an "as is" basis without any warranty of any kind, including, without limitation, the implied warranties of merchantability and fitness for a particular purpose and their equivalents under the laws of any jurisdiction.

pforth 相關文件的中譯文採用 BSD 風格的授權, 任何人都可以重製、散布和修改, 但必須保留文件的出處及相關作者和譯者的姓名. 使用時不需付出任何的報酬, 也可 以只引用部分的內容, 但是不可以宣稱這些內容完全是自己創作的.


內容列表(Table of Contents)


pForth 是什麼(What is pForth)?

pForth 是一個 ANSI 型式的 Forth, 它設計用來易於移植於各種不同的平台. 在 pForth 中的 'P' 字是表現出 "可移植性". pForth 是採用一個用 ANSI 標準 C 所寫的 forth kernel.

PForth is an ANSI style Forth designed to be portable across many platforms. The 'P' in pForth stands for "Portable". PForth is based on a Forth kernel written in ANSI standard 'C'.

Forth 是什麼(What is Forth)?

Forth 是一個建立於堆疊的語言. 它最初由天文學家 Charles Moore 發明, 目的是 為了控制天文望遠鏡. Forth 是一個交談式的語言. 你可以直接在鍵盤上直接輸入 命令, 而它們會立即被執行, 就像是 BASIC 或是 LISP 一樣. Forth 有一組詞的 詞典, 可以用來執行或是將新的詞加入到詞典中. Forth 的詞是在一個資料堆疊中 運作, 而這一個堆疊包含了數目字和位置.

Forth is a stack based language invented by astronomer Charles Moore for controlling telescopes. Forth is an interactive language. You can enter commands at the keyboard and have them be immediately executed, similar to BASIC or LISP. Forth has a dictionary of words that can be executed or used to construct new words that are then added to the dictionary. Forth words operate on a data stack that contains numbers and addresses.

要了解更多有關於 Forth 的資訊, 請閱讀 Forth 教學.

To learn more about Forth, see the Forth Tutorial.

pForth 的起源(The Origins of pForth)

pforth 開始於一個稱為 HForth 的 JSR 系列的 68000 Forth. 而最初用來支援 HMSL, 一種階層的音樂規格語言. HMSL 是一個由 Phil Burk, Larry Polansky 和 David Rosenboom 在 Mills 當代音樂教育中心工作時所發展出來的音樂實驗 性語言. Phil 從 Mills 換工作到 3DO 公司時, 他將 Forth 的核心移植成 C 語言. 而這套系統在 3DO 中是作為一項檢驗 ASIC 設計及移植到新的硬體平台 的工具. 在 3DO 中, 這個 Forth 必須在許多系統上運作, 包括了 SUN, SGI, Macintosh, PC, Amiga, 和使用 3DO ARM 的 Opera 系統及使用 3DO PowerPC 的 M2 系統. pforth 現在也被 3DO 的子公司 CagEnt 發展使用中.

PForth began as a JSR threaded 68000 Forth called HForth that was used to support HMSL, the Hierarchical Music Specification Language. HMSL was a music experimentation language developed by Phil Burk, Larry Polansky and David Rosenboom while working at the Mills College Center for Contemporary Music. Phil moved from Mills to the 3DO Company where he ported the Forth kernel to 'C'. It was used at 3DO as a tool for verifying ASIC design and for bringing up new hardware platforms. At 3DO, the Forth had to run on many systems including SUN, SGI, Macintosh, PC, Amiga, the 3DO ARM based Opera system, and the 3DO PowerPC based M2 system. PForth is now being developed for use at CagEnt, a spinoff of 3DO.

pForth 設計的目的(pForth Design Goals)

pForth 主要的設計目的是為了提供可移植性. 因而 pforth 避免使用任何特別的 UNIX 系統呼叫. pForth 也避免使用任何聰明和傳統的建造詞典方法. 它只是從 用 ANSI C 寫的程式碼編譯出核心後再載入 ANS 相容的 Forth 程式碼來建造詞 典. 非常令人厭煩, 但是很有可能幾乎能移植到任何平台.

PForth has been designed with portability as the primary design goal. As a result, pForth avoids any fancy UNIX calls. pForth also avoids using any clever and original ways of constructing the Forth dictionary. It just compiles its kernel from ANSI compatible 'C' code then loads ANS compatible Forth code to build the dictionary. Very boring but very likely to work on almost any platform.

pForth 可以儲存的這一個詞典檔案幾乎是與平台無關的. 它們可以在一個處理 器上編譯, 並且在另一個處理器上執行. 只要兩個機器上的 endian 是相同的就不 會有問題. 換句話說, 在 PC 上建造的詞典只適用於 PC. 在大部分的其他機型的 電腦上建造的詞典幾乎適用於大部分的其他機型的電腦.

The dictionary files that can be saved from pForth are almost host independant. They can be compiled on one processor, and then run on another processor. as long as the endian-ness is the same. In other words, dictionaries built on a PC will only work on a PC. Dictionaries built on almost any other computer will work on almost any other computer.

pForth 可以運用於只實作非常少量系統服務的硬體系統. 有可能編譯 pforth 系統來用於只支援收發一個單一字元的系統. 如果沒有 malloc() 和 free() 函式, 系統也有用標準 C 程式碼寫的相等函式. 如果沒有檔案的 I/O 功能, 詞典也可以 用 C 的靜態陣列原始碼的方式來儲存在機器裡. 用 C 寫的詞典格式可以稍後編譯 成特製的 pForth 核心來避免必須從磁碟上讀取詞典的問題.

PForth can be used to bring up minimal hardware systems that have very few system services implemented. It is possible to compile pForth for systems that only support routines to send and receive a single character. If malloc() and free() are not available, equivalent functions are available in standard 'C' code. If file I/O is not available, the dictionary can be saved as a static data array in 'C' source format on a host system. The dictionary in 'C' source form is then compiled with a custom pForth kernel to avoid having to read the dictionary from disk.


為你的系統編譯 pForth(Compiling pForth for your System)

建造 pforth 的過程包含了數個步驟. 這一個過程通常是自動地由 Makefile 或 是 IDE Project 自動完成的.

The process of building pForth involves several steps. This process is typically handled automatically by the Makefile or IDE Project.

  1. 編譯出用 C 寫的 pforth 核心: "pforth".
  2. 執行 "pforth -i" 選項來從頭開始建造出詞典.
  3. 編譯 "system.fth" 檔來加進所有高層的 Forth 詞.
  4. 將編譯好的詞典儲存成 "pforth.dic"
  5. 下一次執行 pforth 時, 已經預先編譯好的 pforth.dic 將會自動被載入.

  1. Compile the 'C' based pForth kernel called "pforth".
  2. Execute "pforth" with the -i option to build the dictionary from scratch.
  3. Compile the "system.fth" file which will add all the top level Forth words.
  4. Save the compiled dictionary as "pforth.dic".
  5. The next time you run pforth, the precompiled pforth.dic file will be loaded automatically.

UNIX

提供的 Makefile 應該在大多數的 UNIX 平台上能正常地使用.

A Makefile has been provided that should work on most UNIX platforms.

  1. 切換目錄到 pForth 裡面的最上層目錄.

    cd to top directory of pForth

  2. 輸入: make all

    Enter: make all

麥金塔(Macintosh)

一個預先編譯完全的 pForth PPC 執行檔已經包含了. 如果你需要重建 PPC 版本的 pForth, 一個 Code Warrior 計劃檔也包含在套件中. 另外你也可以使用 MPW 來將 pForth 建造成一個 MPW 的工具. 你需要確定預留了 1 M 的堆積空間. 如果你是建 造給 68K 使用的話, 要確定你使用了 32 bit 的整數, 並且連結了適當的函式庫. 要在 PPC 中重建 pForth:

A precompiled PPC binary for pForth is provided. A Code Warrior Project has been provided that will rebuild pForth for PPC if desired. Alternatively you could use MPW to make pForth as an MPW Tool.  Make sure that you provide at least 1 Meg of heap space. If you build for 68K, make sure you use 32 bit integers, and select the appropriate libraries.  To rebuild pForth for PPC:

  1. 打開 pForthCW.

    Open pForthCW

  2. 建造目標 "pForthApp".

    Make target "pForthApp"

  3. 執行 pForthApp.

    Run pForthApp

  4. 在開啟的對話窗內輸入參數 "-i".

    Enter "-i" as Argumant in starting dialog to initialize dictionary.

  5. 為了編譯 system.fth, 輸入 "loadsys".

    To compile system.fth, enter "loadsys".

  6. 用檔案選單離開 pForth.

    Quit pForth using File menu.

  7. 從現在開始, 只需雙擊 pForthApp 圖像來執行 pForth.

    From now on, just double click pForthApp icon to run pForth.

PC 相容機型(PC Compatible)

已經提供了編譯完成的 pForth 執行檔了. 如果要在 NT 或是 95 下重新 編譯則需要使用 Microsoft Visual C++.

A precompiled binary for pForth is provided. To rebuild under Windows NT or Win95 using Microsoft Visual C++:

  1. 用滑鼠雙擊在 "pForth\pcbuild" 中的 pForth.dsw 圖像.

    Double click on the pForth.dsw icon in "pForth\pcbuild".

  2. 選擇 "MakeDic" 的組態設定.

    Select the "MakeDic" configuration.

  3. 在建造選單中選擇 "Rebuild All", 這將會建造出 pForth.exe 檔.

    Select "Rebuild All" from the Build menu.This will build the pForth.exe file.

  4. 用 CTRL-F5 來執行這一個程式將會建造出 pforth.dic 檔.

    Run the app with CTRL-F5 which will build the pforth.dic file.

  5. 選擇 "Release" 組態設定.

    Select the "Release" configuration.

  6. 用 CTRL-F5 來執行這一個應用程式將會把你帶進 Forth 系統中.

    Run the app with CTRL-F5 which will drop you into Forth.

  7. 從現在開始, 只需用滑鼠雙擊 pforth.exe 執行檔即可執行 pForth 系統.

    From now on, to run pForth, just double click on the pforth.exe file.

原始碼檔案的描述(Description of Source Files)

Forth 原始碼(Forth Source)

ansilocs.fth    = 支援 ANSI (LOCAL) 詞.
c_struct.fth    = 似 'C' 的資料結構格式.
case.fth        = CASE OF ENDOF ENDCASE
catch.fth       = CATCH 和 THROW
condcomp.fth    = [IF] [ELSE] [THEN] 條件式編譯系統
filefind.fth    = FILE?
floats.fth      = 浮點運算支援
forget.fth      = FORGET [FORGET] IF.FORGOTTEN
loadp4th.fth    = 載入基本的詞典
locals.fth      = { } 風格的區域變數, 使用了 (LOCAL)
math.fth        = 許多數學運算詞
member.fth      = 更多的似 'C' 資料結構支援
misc1.fth       = 各式各樣的詞
misc2.fth       = 各式各樣的詞
numberio.fth    = 格式化的數字輸出入
private.fth     = 隱藏低階的詞
quit.fth        = 高階的 QUIT EVALUATE INTERPRET
smart_if.fth    = 允許條件式子在冒號定義之外
see.fth         = Forth "反組譯器". 例如: SEE SPACES
strings.fth     = 字串支援
system.fth      = Bootstrap 載入 pForth 詞典
trace.fth       = 除錯用的單步追蹤系統

ansilocs.fth    = support for ANSI (LOCAL) word
c_struct.fth    = 'C' like data structures
case.fth        = CASE OF ENDOF ENDCASE
catch.fth       = CATCH and THROW
condcomp.fth    = [IF] [ELSE] [THEN] conditional compiler
filefind.fth    = FILE?
floats.fth      = floating point support
forget.fth      = FORGET [FORGET] IF.FORGOTTEN
loadp4th.fth    = loads basic dictionary
locals.fth      = { } style locals using (LOCAL)
math.fth        = misc math words
member.fth      = additional 'C' like data structure support
misc1.fth       = miscellaneous words
misc2.fth       = miscellaneous words
numberio.fth    = formatted numeric input/output
private.fth     = hide low level words
quit.fth        = QUIT EVALUATE INTERPRET in high level
smart_if.fth    = allows conditionals outside colon definition
see.fth         = Forth "disassembler".  Eg.  SEE SPACES
strings.fth     = string support
system.fth      = bootstraps pForth dictionary
trace.fth       = single step trace for debugging

C 原始碼('C' Source)

csrc/pfcompil.c = pForth 編譯器支援
csrc/pfcustom.c = 可從 pForth 中呼叫的 C 函式範例
csrc/pfinnrfp.h = 給直譯器用的浮點運算支援
csrc/pforth.h = 在內嵌 pForth 系統的應用程式中 include 此檔
csrc/pf_cglue.c = pForth 系統呼叫 C 的黏膠
csrc/pf_clib.c = C stdlib 的替換函式
csrc/pf_core.c = 內嵌 pForth 系統的應用程式中主要呼叫的詞
csrc/pf_float.h = 定義 PF_FLOAT, 並包含如 fp_sin 的浮點運算函式
csrc/pf_inner.c = 內部的解譯器
csrc/pf_guts.h = 主要的 include 檔, 定義各種結構
csrc/pf_io.c = 輸入/輸出
csrc/pf_main.c = 獨立的基本 pForth 應用程式
csrc/pf_mem.c = 選用的 malloc() 實作
csrc/pf_save.c = 儲存和載入詞典
csrc/pf_text.c = 字串工具, 錯誤訊息文字
csrc/pf_words.c = 各式各樣的 pForth 詞實作

csrc/pfcompil.c = pForth compiler support
csrc/pfcustom.c = example of 'C' functions callable from pForth
csrc/pfinnrfp.h = float extensions to interpreter
csrc/pforth.h = include this in app that embeds pForth
csrc/pf_cglue.c = glue for pForth calling 'C'
csrc/pf_clib.c = replacement routines for 'C' stdlib
csrc/pf_core.c = primary words called from 'C' app that embeds pForth
csrc/pf_float.h = defines PF_FLOAT, and the floating point math functions such as fp_sin
csrc/pf_inner.c = inner interpreter
csrc/pf_guts.h = primary include file, define structures
csrc/pf_io.c = input/output
csrc/pf_main.c = basic application for standalone pForth
csrc/pf_mem.c = optional malloc() implementation
csrc/pf_save.c = save and load dictionaries
csrc/pf_text.c = string tools, error message text
csrc/pf_words.c = miscellaneous pForth words implemented


執行 pForth 系統(Running pForth)

依照作業系統的不同, pforth 可以從 shell 中執行或是用滑鼠雙擊它的圖像 來啟動. pforth 的執行選項以假定讀者從 shell 中執行它的方式來描述的.

PForth can be run from a shell or by double clicking on its icon, depending on the system you are using. The execution options for pForth are described assuming that you are running it from a shell.

使用(Usage):

-i

從頭開始建造 pForth 詞典. 在建造 pForth 系統及在新的系統上除錯 pForth 時 使用.
Initialize pForth by building dictionary from scratch. Used when building pForth or when debugging pForth on new systems.
-dDictionaryFilename

指定一個自訂的詞典來取代原本預設的 "pforth.dic" 被載入. 例如:
Specify a custom dictionary to be loaded in place of the default "pforth.dic". For example:
原始檔檔名(SourceFilename)

pForth 會取得傳給它的 Forth 原始檔檔名之後, 自動地編譯執行. 這在將 Forth 作為組譯或是自動化的硬體測試時十分有用. 記住原始碼需要能夠在整個檔案內 編譯及執行完成. (譯註: 這是指使用者需要傳給 pForth 最主要的原始檔, 而這一 個檔案除了系統提供的函式之外, 其他需要的函式庫都要在其中 INCLUDE 進來, 由於 Forth 並不提供像是 C 一樣編譯出 object 檔案的功能, 所以每次提供給 Forth 的檔案均需完整.)

A Forth source file can be automatically compiled by passing its name to pForth. This is useful when using Forth as an assembler or for automated hardware testing. Remember that the source file can compile code and execute it all in the same file.

快速地檢查 pForth 系統(Quick Verification of pForth)

要檢查 pForth 系統是否正常運作, 輸入:

To verify that PForth is working, enter:

它現在應該會顯示 "7 ok". 現在輸入:

It should print "7 ok". Now enter:

你應該可以看到一長串的 pForth 詞典中的指令列表. 別擔心, 你不必將它們全部 學會才能工作. 更多的測試描述於 README.txt 檔案中.

You should see a long list of all the words in the pForth dictionary. Don't worry. You won't need to learn all of these.  More tests are described in the README.txt file.


相容於 ANSI 標準(ANSI Compliance)

這一個 Forth 系統是設計用來與 ANS 標準相容. 除非更多的使用者能確認系統 確實相容於標準, 否則作者將不會宣稱它相容. 如果讀者發現有哪些地方偏離了 標準, 請讓作者知道.

This Forth is intended to be ANS compatible. I will not claim that it is compatible until more people bang on it. If you find areas where it deviates from the standard, please let me know.

支援的詞集包括(Word sets supported include):

下面是我知道不相容的區域:

Here are the areas that I know are not compatible:

環境變數的查詢未實作.

The ENVIRONMENT queries are not implemented.

不支援的詞集包括:

Word sets NOT supported include:


pForth 的特色(pForth Special Features)

下列的特性並不是 ANS 標準的一部分, 它只是為了幫助開發者所加進來的.

These features are not part of the ANS standard for Forth.  They have been added to assist developers.

編譯一個檔案(Compiling from a File)

使用 INCLUDE 來從一個原始碼檔編譯程式碼:

Use INCLUDE to compile source code from a file:

你可以巢狀地呼叫 INCLUDE. INCLUDE 只是簡單地將 Forth 系統原本的鍵盤輸入方式 重新導向至檔案. 所以使用者可以在原始程式碼中放入任何合法的 Forth 程式碼.

You can nest calls to INCLUDE. INCLUDE simply redirects Forth to takes its input from the file instead of the keyboard so you can place any legal Forth code in the source code file.

儲存預先編譯好的詞典(Saving Precompiled Dictionaries)

使用 SAVE-FORTH 來將使用者編譯好的程式碼存入檔案. 如果要將目前的詞典存成 一個名為 "custom.dic" 的檔案, 就輸入:

Use SAVE-FORTH save your precompiled code to a file. To save the current dictionary to a file called "custom.dic", enter:

使用者可以離開 pForth, 並且改用使用者自設的詞典. 只需輸入下列的指令:

You can then leave pForth and use your custom dictionary by enterring:

在使用圖像的系統, 使用者可能希望將他自己的詞典存成 "pforth.dic", 以便 下次進入系統時, 這一個檔案將會自動被載入.

On icon based systems, you may wish to name your custom dictionary "pforth.dic" so that it will be loaded automatically.

小心不要在詞典中存入絕對的位置, 因為當使用者重新載入 pForth 時, 系統的位置將會改變. 使用 A! 來將位置用一種可以重新定位的方式來存進變 數中, 並且使用 A@ 來取回使用者所需的位置.

Be careful that you do not leave absolute addresses stored in the dictionary because they will not work when you reload pForth at a different address. Use A! to store an address in a variable in a relocatable form and A@ to get it back if you need to.

重新編譯程式碼(Recompiling Code) - ANEW INCLUDE?

當你測試一個充滿了程式碼的檔案時, 你也許需要重新編譯許多次. 而這將會需要 使用 FORGET 指令來清除舊的程式碼, 再來載入新的程式碼. 可以在檔案之前放入 下列的一行指令:

When you are testing a file full of code, you will probably recompile many times. You will probably want to FORGET the old code before loading the new code. You could put a line at the beginning of your file like this:

這將會讓系統自動地 "FORGET" 使用者每次載入的程式碼. 不幸地, 使用者必須先 定義 XXXX_MINE 之後, 才能載入這一個檔案. 我們製造一個詞來自動地為使用者 定義這一個必須存在的字. 它叫做 ANEW, 能夠在幾乎每一個 Forth 原始碼中找到. 作者們使用一個字首 TASK- 並接著檔案名稱來達成一致性. 這一個 TASK-name 的 單字對於配合 INCLUDE? 詞使用時也很方便. 下面有一個例子:

This would automatically FORGET for you every time you load. Unfortunately, you must define XXXX-MINE before you can ever load this file. We have a word that will automatically define a word for you the first time, then FORGET and redefine it each time after that. It is called ANEW and can be found at the beginning of most Forth source files. We use a prefix of TASK- followed by the filename just to be consistent. This TASK-name word is handy when working with INCLUDE? as well. Here is an example:

注意這一個 INCLUDE? 在呼叫 ANEW 之前, 所以我們將不會在每次重新編譯時 FORGET MYTHING.FTH.

Notice that the INCLUDE? comes before the call to ANEW so that we don't FORGET MYTHING.FTH every time we recompile.

FORGET 允許使用者清除已經編譯過的程式碼. 這是一個在程式語言中不平常的 特色. 這在 Forth 中是一種十分便利的特性, 但是卻可能導致問題. 大部分的問題 是某些位置仍指向 FORGET 已經清除的程式碼. 這有可能發生在使用者設定了一個 deferred 系統詞指到他所創造的詞, 接著用 FORGET 將那一個詞清除. 這一個系統 詞指向的程式碼不存在了之後, 如果有其他的程式碼使用到這一個詞, 就可能會造 成系統毀壞. (請參考後面的 DEFER 討論). 另一個問題是如果你的程式碼配置了 記憶體、開啟了檔案、或是開了視窗, 程式碼一被忘記之後, 就無法釋放或是關上 這些事物了. 使用者也可能遇到另一種問題就是將這些程式碼的位置加進跳躍表或 是資料表格中.

FORGET allows you to get rid of code that you have already compiled. This is an unusual feature in a programming language. It is very convenient in Forth but can cause problems. Most problems with FORGET involve leaving addresses that point to the forgotten code that are not themselves forgotten. This can occur if you set a deferred system word to your word then FORGET your word. The system word which is below your word in the dictionary is pointing up to code that no longer exists. It will probably crash if called. (See discussion of DEFER below.) Another problem is if your code allocates memory, opens files, or opens windows. If your code is forgotten you may have no way to free or close these thing. You could also have a problems if you add addresses from your code to a table that is below your code. This might be a jump table or data table.

由於這是很常見的問題, 所以作者們提供了一套工具來處理它. 如果你有一些 程式碼如果使用 FORGET 指令會有潛在性的問題, 就可以寫一個清除用的詞來消除 這一個問題. 這一個詞可以 UNdefer 詞, 釋放記憶體等等. 然後告訴系統如果需 要 FORGET 程式碼時就呼叫這一個詞. 下面是它的用法:

Since this is a common problem we have provided a tool for handling it. If you have some code that you know could potentially cause a problem if forgotten, then write a cleanup word that will eliminate the problem. This word could UNdefer words, free memory, etc. Then tell the system to call this word if the code is forgotten. Here is how:

IF.FORGOTTEN 創造了一個鍵結串列, 其中包含了所有設定過的 CFA, 並且在使用 FORGET 指令時將會被檢查. 每一個結束於 FORGET 執行後的 HERE 位置(這是 Forth 指向詞典最上層的指標) 之前的節點都會被執行. (譯註: 應該是從連結的最後開始 執行, 直到最後的位置落在要 FORGET 的指令之前的位置為止, 然後再執行 FORGET 的動作.)

IF.FORGOTTEN creates a linked list node containing your CFA that is checked by FORGET. Any nodes that end up above HERE (the Forth pointer to the top of the dictionary) after FORGET is done are executed.

用 [FORGET] 來自訂 FORGET 指令 (Customising FORGET with [FORGET])

有時候你可能需要延展 FORGET 運作的方式. 由於提供 defer 功能給 FORGET 可能 會造成一些問題. 所以系統讓使用者可以定義一個新版的 [FORGET] 指令, 它將會被 FORGET 指令呼叫. 使用者必須在 [FORGET] 指令的程式碼中再一次呼叫 [FORGET] 指令, 否則將不會真正做出 FORGET 的動作. 下面是一個例子:

Sometimes, you may need to extend the way that FORGET works. FORGET is not deferred, however, because that could cause some real problems. Instead, you can define a new version of [FORGET] which is searched for and executed by FORGET. You MUST call [FORGET] from your program or FORGET will not actually FORGET. Here is an example.

這種方式比重新定義 FORGET 指令更值得推薦的原因是像是 ANEW 的指令中會呼叫 到 FORGET 指令, 而採用這種做法將可以真正使用到使用者自訂的 FORGET 動作.

This is recommended over redefining FORGET because words like ANEW that call FORGET will now pick up your changes.

聰明的流程控制(Smart Conditionals)

在 pForth 中, 你可以在冒號定義指令外使用 IF THEN DO LOOP 和其他的流程控制 指令. pForth 將會暫時地切換到編譯模式, 然後自動地執行這些流程控制指令. (感謝 Mitch Bradley). 例如, 可以直接輸入下列程式碼:

In pForth, you can use IF THEN DO LOOP and other conditionals outside of colon definitions. PForth will switch temporarily into the compile state, then automatically execute the conditional code. (Thank you Mitch Bradley) For example, just enter this at the keyboard.

發展工具(Development Tools)

WORDS.LIKE

如果你不能記住一個詞的正確名字, 你可以用 WORDS.LIKE 來在整個詞典中找出 包含一個子字串的詞. 例如:

If you cannot remember the exact name of a word, you can use WORDS.LIKE to search the dictionary for all words that contain a substring. For an example, enter:

FILE?

你可以使用 FILE? 指令來找出哪一個詞是從哪一個檔案中編譯而成的. 如果一個詞 在多個檔案中都有定義, 則這一個指令會列出每一個檔案. 一個詞中的每一個定義的 執行單元將會被列在同一行.

You can use FILE? to find out what file a word was compiled from. If a word was defined in multiple files then it will list each file. The execution token of each definition of the word is listed on the same line.

SEE

你可以用 SEE 來反組譯一個在 pForth 中的詞. SEE 將會嘗試以 Forth 原始碼的 格式來印出程式碼. SEE 將會讓你不完全地了解一個詞是如何定義的. 當然有些編 譯用字, 像是 BEGIN 和 LITERAL 是很難正確地反組譯再列印出來的. 例如:

You can use SEE to "disassemble" a word in the pForth dictionary. SEE will attempt to print out Forth source in a form that is similar to the source code. SEE will give you some idea of how the word was defined but is not perfect. Certain compiler words, like BEGIN and LITERAL, are difficult to disassemble and may not print properly. For an example, enter:

單步追蹤和除錯(Single Step Trace and Debug)

在除錯時單步追蹤程式碼是有用的. 為了這一項目的, PForth 提供了一個簡單的 單步追蹤機制來達成這一項需求. 下面有一個用 TRACE 指令來對於一個簡單的程式 除錯的範例. 先輸入下列的程式:

It is often useful to proceed step by step through your code when debugging.  PForth provides a simple single step trace facility for this purpose.  Here is an example of using TRACE to debug a simple program.  Enter the following program:
 

雖然這一個程式是正確的, 讓我們先假裝它有問題, 並且對它進行除錯. 輸入:

Even though this program should work, let's pretend it doesn't and try to debug it.  Enter:

你應該能看到:

You should see:

這個 "TSQ +0" 是表示你正在執行從 TSQ 開始的第 "+0" 個位置的程式碼. <10:1> 是指我們正在使用十進位, 並且在堆疊上有一個元素, 並且它的數值是 7. (.") 是 準備要被執行的詞. (.") 是使用 ." 指令時會被編譯的詞. 現在進行單步追蹤, 輸入:

The "TSQ +0" means that you are about to execute code at an offset of "+0" from the beginning of TSQ.  The <10:1> means that we are in base 10, and that there is 1 item on the stack, which is shown to be "7". The (.") is the word that is about to be executed.  (.") is the word that is compiled when use use .".  Now to single step, enter:

你將會看到:

You should see:

"Square os" 已經被 (.") 指令印出來了. 我們可以用 sm 指令來進行多步的追蹤. 輸入:

The "Square os" was printed by (."). We can step multiple times using the "sm" command. Enter:

你可以看到:

You should see:

在 ">>" 後面的 "7" 是由 "." 詞所印出來的. 如果我們再輸入 "s", 就會直接一步 執行完 SQUARE 詞. 如果我們需要追蹤進 SQUARE 詞, 我們可以輸入:

The "7" after the ">>" was printed by the . word. If we entered "s", we would step over the SQUARE word. If we want to dive down into SQUARE, we can enter:

你將會看到:

You should see:

要在 SQUARE 中單步執行, 輸入:

To step once in SQUARE, enter:

你應該可以看見:

You should see:

要直接結束目前的詞, 輸入:

To go to the end of the current word, enter:

你應該可以看見:

You should see:

EXIT 是在每一個編譯過的 Forth 詞的結束位置. 要得到有關 TRACE 的更多的資訊, 可以輸入 TRACE.HELP:

EXIT is compiled at the end of every Forth word. For more information on TRACE, enter TRACE.HELP:

有條件的編譯(Conditional Compilation) [IF] [ELSE] [THEN]

PForth 支援了如同 C 語言的 #if, #else 和 #endif 一樣的條件式編譯指令:

PForth supports conditional compilation words similar to 'C''s #if, #else, and #endif.

[IF] ( flag -- , 若真, 跳到 [ELSE] 或是 [THEN] )
[ELSE] ( -- , 跳到 [THEN] )
[THEN] ( -- , 不做任何時, 用來結束 [IF] 和 [ELSE] 區段)

[IF] ( flag -- , if true, skip to [ELSE] or [THEN] )
[ELSE] ( -- , skip to [THEN] )
[THEN] ( -- , noop, used to terminate [IF] and [ELSE] section )

 
例如:
For example:

這裡是如何在冒號定義中利用 [ 和 ] 來有條件的地編譯.

Here is how to conditionally compile within a colon definition by using [ and ].

各種方便的函式(Miscellaneous Handy Words)

.HEX ( n -- , 用 16 進位來顯示 N(print N as hex number) )
CHOOSE ( n -- 亂數, 在 0 和 N 之間任取一個數值 (rand , select random number between 0 and N) )
MAP ( -- , 印出詞典的資訊(print dictionary information) )

區域變數(Local Variables) { foo --}

在一個編譯好的 Forth 中, 有時很難追蹤到底有哪些東西在堆疊上. 如果你發現你 做了一大堆像是 DUP SWAP ROT PICK 等等的堆疊操作時, 你可能需要區域變數. 它們 將會大量地簡化你的程式碼. 你可以用很像堆疊的語法的方式來定義區域變數. 這些 區域變數只能在這一個詞中被存取. 因此它們是屬於 "區域" 的, 相反地, 一般的 變數是屬於 "全域" 的. 區域變數是自我讀取的, 如果你使用它們的名字, 它們將會 自動地把數值放進堆疊中. 如果需要保留它們的數值時, 它們將會停留在返回堆疊中. 用它們所寫的詞是可重入、且可遞回的.

In a complicated Forth word it is sometimes hard to keep track of where things are on the stack. If you find you are doing a lot of stack operations like DUP SWAP ROT PICK etc. then you may want to use local variables. They can greatly simplify your code. You can declare local variables for a word using a syntax similar to the stack diagram. These variables will only be accessible within that word. Thus they are "local" as opposed to "global" like regular variables. Local variables are self-fetching. They automatically put their values on the stack when you give their name. You don't need to @ the contents. Local variables do not take up space in the dictionary. They reside on the return stack where space is made for them as needed. Words written with them can be reentrant and recursive.

假設要寫一個計算兩個平方數差值的詞, 下面有兩種方式可以寫出相同作用的詞.

Consider a word that calculates the difference of two squares, Here are two ways of writing the same word.

在第二個定義中, DIFF.SQUARES 用大括弧來告訴編譯器開始定義區域變數. 目前定義兩個區域變數, A 及 B. 它們的名字可以跟一般的 Forth 詞一樣長. "--" 符號代表了區域變數列表的結束. 當執行這一個詞時, 這些數值將會自動地 從堆疊中取出, 然後放進區域變數中. 當區域變數被執行時, 它將會直接把它的 數值放進堆疊, 而不是放進它的位置. 這就叫做是自我讀取. 由於沒有使用到位置 , 你可能會懷疑要如何將數值存入. 有一個特別的運算符號可以做這種儲存的動作, 它看起來像是 "->", 並且發音成 "to".

In the second definition of DIFF.SQUARES the curly bracket '{' told the compiler to start declaring local variables. Two locals were defined, A and B. The names could be as long as regular Forth words if desired. The "--" marked the end of the local variable list. When the word is executed, the values will automatically be pulled from the stack and placed in the local variables. When a local variable is executed it places its value on the stack instead of its address. This is called self-fetching. Since there is no address, you may wonder how you can store into a local variable. There is a special operator for local variables that does a store. It looks like -> and is pronounced "to".

區域變數不需要全部由堆疊傳入. 你可以自己在 "|" 符號後宣告它們. 它們 在被創造出來時會被設成 0. 下面有一個運用 "->" 和 "|" 詞的範例.

Local variables need not be passed on the stack. You can declare a local variable by placing it after a "vertical bar" ( | )character. These are automatically set to zero when created. Here is a simple example that uses -> and | in a word:

: SHOW2*  
        { loc1 | unvar --  , 一個是規律的, 一個是未初始化的 }
        LOC1  2*  ->  UNVAR 
                ( 將 UNVER 設成 2*LOC1 )
        UNVAR   .   ( 顯示 UNVAR )
;
3 SHOW2*   ( 只傳一個參數, 顯示 6 )

由於區域變數通常是用來作為一個計數器或是累加器, 我們有一個特別的運算符號來 在一個區域變數中增加數值. 它是 "+->", 發音成 "plus to". 下面兩行的功能是相等 的, 但是第二行會較快而且較小.

Since local variable often used as counters or accumulators, we have a special operator for adding to a local variable It is +-> which is pronounced "plus to". These next two lines are functionally equivalent but the second line is faster and smaller:

如果你將一個區域變數命令成跟詞典中的 Forth 詞的名字相同. 例如: INDEX 或是 COUNT, 你將會得到一個警告訊息. 這一個區域變數仍然能工作, 但是其他人可能為 因此而遇到困擾, 所以我們為此向你提出警告. 其他可能出現的錯誤包括: 少了一個 "}" 符號, 少了 "--", 或是使用了過多的區域變數.

If you name a local variable the same as a Forth word in the dictionary, eg. INDEX or COUNT, you will be given a warning message. The local variable will still work but one could easily get confused so we warn you about this. Other errors that can occur include, missing a closing '}', missing '--', or having too many local variables.

C 格式的結構('C' like Structures.) :STRUCT

你可以用 :STRUCT 指令來設計 C 格式的結構, 例如:

You can define 'C' like data structures in pForth using :STRUCT. For example:

閱讀 "c_struct.fth" 來取得更多的資訊.

See the file "c_struct.fth" for more information.

向量的執行(Vectorred Execution) - DEFER

使用 DEFER 來處理向量字. 在 Forth 和其他的語言中, 你可以將一個函式的位置 儲存於一個變數中. 你可以稍後從這一個變數中取出這個位置, 並且執行這一個 位置所指向的函式. 這就叫做向量的執行. PForth 提供了一項工具來簡化這一個過程. 你可以用 DEFER 來定義一個詞. 這一個詞將會包含另一個 Forth 函式的執行符號, 當你執行這一個詞時, 它將會自動執行它所指向的詞. 只需改變這一個詞的內容, 你就可以改變它所執行的動作. 有數個詞可以支援這樣的語法.

Using DEFER for vectored words. In Forth and other languages you can save the address of a function in a variable. You can later fetch from that variable and execute the function it points to.This is called vectored execution. PForth provides a tool that simplifies this process. You can define a word using DEFER. This word will contain the execution token of another Forth function. When you execute the deferred word, it will execute the function it points to. By changing the contents of this deferred word, you can change what it will do. There are several words that support this process.

DEFER ( <name> -- , 定義一個 deferred 詞 )
IS ( CFA <name> -- , 設定一個函式對應至 deferred 詞 )
WHAT'S ( <name> -- CFA , 傳回由 IS 設定的 CFA )

DEFER ( <name> -- , define a deferred word )
IS ( CFA <name> -- , set the function for a deferred word )
WHAT'S ( <name> -- CFA , return the CFA set by IS )
要看到這種詞實際呼叫函式的名字的方式如下:

Simple way to see the name of what's in a deferred word:

應該會印出目前在 EMIT 中的詞的名字.

should print name of current word that's in EMIT.


 
這裡有一個使用 deferred 詞的範例.


Here is an example that uses a deferred word.

許多在系統中的詞使用 DEFER 來定義, 因此我們可以改變他們如何工作的方式, 而不需重新編譯系統. 這裡是這些詞中的部分列表.

Many words in the system have been defined using DEFER which means that we can change how they work without recompiling the entire system. Here is a partial list of those words

Defer 的潛在的問題(Potential Problems with Defer)

Defer 的詞是非常便於使用的, 然而你們必須小心使用它們. 一個問題是如果 你重複初始化一個 deferred 系統. 在下面的例子中, 假設我們呼叫 STUTTER 兩次. 首先我們先將原始的 EMIT 向量存進 OLD-EMIT 中, 並且放進一個新的. 第二次我們呼叫到這部分時, 我們將會從 EMIT 拿到新的函式位置, 並且存到 OLD-EMIT 中, 因而覆蓋到之前存放的內容. 然後我們就失去了原本 EMIT 的 位置, 你可以先檢查是否已經做過了 defer 來避免這種事情. 下面是這種技巧 的一個範例.

Deferred words are very handy to use, however, you must be careful with them. One problem that can occur is if you initialize a deferred system more than once. In the below example, suppose we called STUTTER twice. The first time we would save the original EMIT vector in OLD-EMIT and put in a new one. The second time we called it we would take our new function from EMIT and save it in OLD-EMIT overwriting what we had saved previously. Thus we would lose the original vector for EMIT . You can avoid this if you check to see whether you have already done the defer. Here's an example of this technique.