內容列表(Table of Contents)


13. 選用的區域變數詞集(The optional Locals word set)


13.1 簡介(Introduction)

參閱(See): Annex A.13 區域變數詞集(The Locals Word Set)


13.2 附加的專有名詞和記號(Additional terms and notation)

無(None).


13.3 附加的使用需求(Additional usage requirements)

參閱(See): A.13.3 附加的使用需求(Additional usage requirements)


13.3.1 區域變數(Locals)

一個區域變數是一個執行語義是傳回它的值的資料物件, 它的範圍必須被限制於它被宣告的定義中, 並且它的在一個定義中的使用必須不能排除重入或遞迴.

A local is a data object whose execution semantics shall return its value, whose scope shall be limited to the definition in which it is declared, and whose use in a definition shall not preclude reentrancy or recursion.


13.3.2 環境的詢問(Environmental queries)

將表格 13.1 加進表格 3.5.

Append table 13.1 to table 3.5.

參閱(See): 3.2.6 環境的詢問(Environmental queries)

表格 13.1 - 環境詢問字串

字串            值     資料型別  常數?      意義
------          ---------------  ---------  -------
#LOCALS         n                yes        在一個定義中最大的區域變數數目
LOCALS          flag             no         區域變數詞集存在
LOCALS-EXT      flag             no         區域變數延伸詞集存在

Table 13.1 - Environmental query strings

String          Value data type  Constant?  Meaning
------          ---------------  ---------  -------
#LOCALS         n                yes        maximum number of local variables in a definition
LOCALS          flag             no         locals word set present
LOCALS-EXT      flag             no         locals extensions word set present


13.3.3 區域變數處理(Processing locals)

要支援區域變數詞集, 一個系統必須提供一個機制來接收由 (LOCAL) 定義的訊息, 並以下列方式回應.

To support the locals word set, a system shall provide a mechanism to receive the messages defined by (LOCAL) and respond as described here.

在一個定義的編譯中途的 : (colon), :NONAME, 或 DOES> 之後, 一個程式可以開始送出區域識別訊息給系統. 這個程序必須在第一個訊息被送出時開始. 這個程序必須在 最後的區域變數 訊息被送出時終止. 系統必須保持追蹤在完全的順序中的名稱、次序和識別訊息的數目.

During the compilation of a definition after : (colon), :NONAME, or DOES>, a program may begin sending local identifier messages to the system. The process shall begin when the first message is sent. The process shall end when the last local message is sent. The system shall keep track of the names, order, and number of identifiers contained in the complete sequence.


13.3.3.1 編譯語義(Compilation semantics)

在接收到一串區域識別訊息的系統之上, 必須在編譯時期進行下列的動作:

The system, upon receipt of a sequence of local-identifier messages, shall take the following actions at compile time:

a) 對於每一個傳給 (LOCAL) 的識別符號創造暫時的詞典記錄, 這樣的每一個識別符號將會表現得像是一個區域變數. 這些暫時的詞典記錄必須在定義的結束時消失, 定義的結束由 ; (分號), ;CODE, 或 DOES> 表示. 系統不須以它維護其他詞典記錄的方式來維護這些識別符號, 只要它們能被以正常的詞典搜尋程序找到即可. 而且如果搜尋順序詞集存在, 區域識別符號必須總是在任何可定義的搜尋順序的詞列表之前被搜尋, 並且沒有任何搜尋順序詞集可以改變區域變數在搜尋順序上的特權位置. 區域識別符號可以保存於大量儲存媒體之中.

a) Create temporary dictionary entries for each of the identifiers passed to (LOCAL), such that each identifier will behave as a local. These temporary dictionary entries shall vanish at the end of the definition, denoted by ; (semicolon), ;CODE, or DOES>. The system need not maintain these identifiers in the same way it does other dictionary entries as long as they can be found by normal dictionary searching processes. Furthermore, if the Search-Order word set is present, local identifiers shall always be searched before any of the word lists in any definable search order, and none of the Search-Order words shall change the locals' privileged position in the search order. Local identifiers may reside in mass storage.

b) 對於任何傳給 (LOCAL) 的識別符號, 系統必須產生一個適當的碼序列來在執行時期做下列動作:

  1. 配置一個儲存資源足夠用來包含區域變數的值. 這個儲存必須被以一種不排除使用這個區域變數的定義重入或遞迴的方式來配置.
  2. 用在資料堆疊上方的元素來初始化這些值. 如果超過一個區域變數被宣告, 在最上方的元素將會被移進第一個識別的區域變數, 下一個元素將會被移進第二個, 諸如此類地運作.
這個儲存資源可以是返回堆疊或可以以其他的方式實作, 像是在暫存器中. 這個儲存資源必須不在資料堆疊中. 使用區域變數必須不限制在宣告點之前或之後的資料堆疊的使用.

b) For each identifier passed to (LOCAL), the system shall generate an appropriate code sequence that does the following at execution time:

  1. Allocate a storage resource adequate to contain the value of a local. The storage shall be allocated in a way that does not preclude re-entrancy or recursion in the definition using the local.
  2. Initialize the value using the top item on the data stack. If more than one local is declared, the top item on the stack shall be moved into the first local identified, the next item shall be moved into the second, and so on.
The storage resource may be the return stack or may be implemented in other ways, such as in registers. The storage resource shall not be the data stack. Use of locals shall not restrict use of the data stack before or after the point of declaration.

c) 安排任何合法的終結一個定義方法, 特別是 ; (分號), ;CODE, DOES> 或 EXIT, 將會釋放任何在定義中為區域變數所配置的資源. ABORT 必須釋放所有的區域變數儲存資源, 並且 (如果有實作) CATCH / THROW 必須釋放被終止的所有的定義的這類資源.

c) Arrange that any of the legitimate methods of terminating execution of a definition, specifically ; (semicolon), ;CODE, DOES> or EXIT, will release the storage resource allocated for the locals, if any, declared in that definition. ABORT shall release all local storage resources, and CATCH / THROW (if implemented) shall release such resources for all definitions whose execution is being terminated.

d) 分開的區域變數集合可以被宣告於定義詞中使用, 在 DOES> 之前是給定義詞使用的, 在 DOES> 之後是給被定義的詞使用的.

d) Separate sets of locals may be declared in defining words before DOES> for use by the defining word, and after DOES> for use by the word defined.

一個實作區域變數詞的系統必須支援在一個定義中至少能宣告八個區域變數.

A system implementing the Locals word set shall support the declaration of at least eight locals in a definition.


13.3.3.2 語法的限制(Syntax restrictions)

在一個程式中的立即詞可以在以下的限制下使用 (LOCAL) 來實作給區域變數宣告的語法.

Immediate words in a program may use (LOCAL) to implement syntaxes for local declarations with the following restrictions:

a) 一個程式必須不在下列兩個時間中編譯任何可執行的程式碼到目前的定義中: (LOCAL) 被執行來識別那個定義的第一個區域變數, 和送出單一需要 最後區域變數 訊息;

a) A program shall not compile any executable code into the current definition between the time (LOCAL) is executed to identify the first local for that definition and the time of sending the single required last local message;

b) 在 (LOCAL) 訊息被送出的程序的程式原始碼的位置, 參考到區域變數被宣告的這一點的這裡, 必須不位置任何控制結構的範圍中;

b) The position in program source at which the sequence of (LOCAL) messages is sent, referred to here as the point at which locals are declared, shall not lie within the scope of any control structure;

c) 區域變數必須不在定義中的之前放置於返回堆疊中的值被移除之前宣告;

c) Locals shall not be declared until values previously placed on the return stack within the definition have been removed;

d) 在一個定義的區域變數被宣告之後, 一個程式可以放置資料於返回堆疊中. 然而, 如果做出了這種事, 區域變數必須在那些值被從返回堆疊中移除後才可以存取.

d) After a definition's locals have been declared, a program may place data on the return stack. However, if this is done, locals shall not be accessed until those values have been removed from the return stack;

e) 傳回執行記號的詞, 像是 ' (tick), ['], 或 FIND, 必須不使用於區域變數名稱中;

e) Words that return execution tokens, such as ' (tick), ['], or FIND, shall not be used with local names;

f) 一個在一個單一定義中宣告了超過八個區域變數的程式有環境相依性;

f) A program that declares more than eight locals in a single definition has an environmental dependency;

g) 區域變數可以在控制結構中被存取或更新, 包括 do-loops;

g) Locals may be accessed or updated within control structures, including do-loops;

h) 區域變數名稱必須不被由 POSTPONE[COMPILE] 參考.

h) Local names shall not be referenced by POSTPONE and [COMPILE].

參閱(See): 3.4 Forth 文字解譯器(The Forth text interpreter)


13.4 附加的文件需求(Additional documentation requirements)


13.4.1 系統文件(System documentation)


13.4.1.1 實作自訂選項(Implementation-defined options)


13.4.1.2 模稜兩可的情況(Ambiguous conditions)


13.4.1.3 其他的系統文件(Other system documentation)


13.4.2 程式文件(Program documentation)


13.4.2.1 環境相依性(Environmental dependencies)


13.4.2.2 其他程式文件(Other program documentation)


13.5 遵守標準和命名(Compliance and labeling)


13.5.1 ANS Forth 系統(ANS Forth systems)

措辭 提供區域變數詞集 必須被加到任何提供區域變數詞集的標準系統的描述之中.

The phrase Providing the Locals word set shall be appended to the label of any Standard System that provides all of the Locals word set.

措辭 從區域變數延伸詞集提供 name(s) 必須被加到任何提供部分區域變數延伸詞集的標準系統的描述之中.

The phrase Providing name(s) from the Locals Extensions word set shall be appended to the label of any Standard System that provides portions of the Locals Extensions word set.

措辭 提供區域變數延伸詞集 必須被加到任何提供所有區域變數詞集和區域變數延伸詞集的標準系統的描述之中.

The phrase Providing the Locals Extensions word set shall be appended to the label of any Standard System that provides all of the Locals and Locals Extensions word sets.


13.5.2 ANS Forth 程式(ANS Forth programs)

措辭 需要區域變數詞集 必須被加到任何需要系統提供區域變數詞集的標準程式的描述之中.

The phrase Requiring the Locals word set shall be appended to the label of Standard Programs that require the system to provide the Locals word set.

措辭 需要區域變數延伸詞集中的 name(s) 必須被加到任何需要系統提供部分區域變數延伸詞集的標準程式的描述之中.

The phrase Requiring name(s) from the Locals Extensions word set shall be appended to the label of Standard Programs that require the system to provide portions of the Locals Extensions word set.

措辭 需要區域變數延伸詞集 必須被加到任何需要系統提供所有區域變數和區域變數延伸詞集的標準程式的描述之中.

The phrase Requiring the Locals Extensions word set shall be appended to the label of Standard Programs that require the system to provide all of the Locals and Locals Extensions word sets.


13.6 詞彙表(Glossary)


13.6.1 區域變數詞(Locals words)


13.6.1.0086 (LOCAL)
paren-local-paren LOCAL

        解譯: 這個詞的解譯語義是未定義的.

	執行: ( c-addr u -- )

當在編譯時執行, (LOCAL) 傳送一個有兩種意義的其中一種的訊息給系統. 如果 u 是非零時, 這個訊息加入了一個新的區域變數, 它的定義名稱是由字元字串 c-addr u 識別的. 如果 u 是零, 這個訊息是 最後的區域變數, 而 c-addr 沒有意義.

在編譯一個定義時執行 (LOCAL) 的結果是創造了一個有名字的區域變數識別記號集合, 每一個都是一個定義名稱, 只在那一個定義的原始碼中的範圍有執行語義.

        local 執行: ( -- x )

將 local 的值 x 堆放於堆疊上. local 之值的初始化方式描述於 13.3.3 區域變數處理, 並且可以透過在 local 的名稱之前放置一個 TO 來改變. 如果 local 在解譯模式執行時, 將會有一個模稜兩可的情況存在.

注意: 這個詞沒有一般感覺上的特別編譯語義, 因為它提供了給其他有特別編譯語義的使用者自訂詞存取系統性能的能力, 然而區域變數機能作為一個完整和有牽涉到語義的訊息序列定義了傳遞的特定使用規則, 它的細節描述於章節 13.3.3 區域變數處理.

注意: 這個詞不是用來直接在一個定義中使用來宣告定義的區域變數. 它是用來給系統或使用編譯詞來使用. 這些編譯詞依序地定義它們自己的語法, 並且可以直接在定義中使用來宣告區域變數. 在這個情況中, (LOCAL) 的語法是以一串的編譯時期訊息的表示來定義, 並且細節描述於章節 13.3.3 區域變數處理.

注意: 區域變數詞集修改了定義於核心延伸詞集的 6.2.2295 TO 的語法和語義.

參閱: 3.4 文字解譯器


13.6.1.0086 (LOCAL)
paren-local-paren LOCAL

	Interpretation: Interpretation semantics for this word are undefined.

	Execution: ( c-addr u -- )

When executed during compilation, (LOCAL) passes a message to the system that has one of two meanings. If u is non-zero, the message identifies a new local whose definition name is given by the string of characters identified by c-addr u. If u is zero, the message is last local and c-addr has no significance.

The result of executing (LOCAL) during compilation of a definition is to create a set of named local identifiers, each of which is a definition name, that only have execution semantics within the scope of that definition's source.

        local Execution: ( -- x )

Push the local's value, x, onto the stack. The local's value is initialized as described in 13.3.3 Processing locals and may be changed by preceding the local's name with TO. An ambiguous condition exists when local is executed while in interpretation state.

Note: This word does not have special compilation semantics in the usual sense because it provides access to a system capability for use by other user-defined words that do have them. However, the locals facility as a whole and the sequence of messages passed defines specific usage rules with semantic implications that are described in detail in section 13.3.3 Processing locals.

Note: This word is not intended for direct use in a definition to declare that definition's locals. It is instead used by system or user compiling words. These compiling words in turn define their own syntax, and may be used directly in definitions to declare locals. In this context, the syntax for (LOCAL) is defined in terms of a sequence of compile-time messages and is described in detail in section 13.3.3 Processing locals.

Note: The Locals word set modifies the syntax and semantics of 6.2.2295 TO as defined in the Core Extensions word set.

See: 3.4 The Forth text interpreter


13.6.1.2295 TO
LOCAL

延伸 6.2.2295 TO 的語法成:

        解譯: ( x "<spaces>name" -- )

跳過開頭的空白並且分析由一個空白分界的 name. 將 x 存入 name. 如果 name 不是由 VALUE 定義時, 將會有模稜兩可的情況.

        編譯: ( "<spaces>name" -- )

跳過開頭的空白並且分析由一個空白分界的 name. 將下列給予的執行時期語義加進目前的定義. 如果 name 不是由 VALUE 或 (LOCAL) 定義時, 將會有模稜兩可的情況.

        執行時期:       ( x -- )

將 x 儲存於 name 中.

注意: 如果 POSTPONE[COMPILE] 用於 TO 時, 將會有一個模稜兩可的情況存在.

參閱: 3.4.1 分析, A.13.6.1.2295 TO


13.6.1.2295 TO
LOCAL

Extend the semantics of 6.2.2295 TO to be:

        Interpretation: ( x "<spaces>name" -- )

Skip leading spaces and parse name delimited by a space. Store x in name. An ambiguous condition exists if name was not defined by VALUE.

        Compilation: ( "<spaces>name" -- )

Skip leading spaces and parse name delimited by a space. Append the run-time semantics given below to the current definition. An ambiguous condition exists if name was not defined by either VALUE or (LOCAL).

        Run-time:       ( x -- )

Store x in name.

Note: An ambiguous condition exists if either POSTPONE or [COMPILE] is applied to TO.

See: 3.4.1 Parsing, A.13.6.1.2295 TO


13.6.2 區域變數延伸詞(Locals extension words)


13.6.2.1795 LOCALS|
locals-bar LOCAL EXT

	解譯: 這個詞的解譯語義是未定義的. 

	編譯: ( "<spaces>name1" "<spaces>name2" ... "<spaces>namen" | -- )

由重複地跳過開頭的空白, 分析名稱, 並且執行 13.6.1.0086 (LOCAL) 來創造最多八個區域變數識別碼. 這個被定義的區域變數的列表由 | 終止. 將下列給予的執行時期語義到目前的定義.

        執行時期:       ( xn ... x2 x1 -- )

初始化最多八個描述於 13.6.1.0086 (LOCAL) 的區域變數識別碼, 每一個從堆疊上方的元素取得它們的初始值, 而從堆疊中移除它們. 識別碼 name1 是由值 x1 初始化, 識別碼 name2 是由值 x2 初始化, 等等. 在被運用時, 每一個區域變數將傳回它的值. 區域變數的值可以用 13.6.1.2295 TO 來改變.

參閱: A.13.6.2.1795 LOCALS|


13.6.2.1795 LOCALS|
locals-bar LOCAL EXT

	Interpretation: Interpretation semantics for this word are undefined.

	Compilation: ( "<spaces>name1" "<spaces>name2" ... "<spaces>namen" | -- )

Create up to eight local identifiers by repeatedly skipping leading spaces, parsing name, and executing 13.6.1.0086 (LOCAL). The list of locals to be defined is terminated by |. Append the run-time semantics given below to the current definition.

        Run-time:       ( xn ... x2 x1 -- )

Initialize up to eight local identifiers as described in 13.6.1.0086 (LOCAL), each of which takes as its initial value the top stack item, removing it from the stack. Identifier name1 is initialized with x1, identifier name2 with x2, etc. When invoked, each local will return its value. The value of a local may be changed using 13.6.1.2295 TO.

See: A.13.6.2.1795 LOCALS|


內容列表(Table of Contents)
下一章(Next Section)