Merry Christmas and Happy New Year!
Ting.
What is FORTH?
FORTH was invented by Chuck Moore in 1960's as a programming language.
Chuck was not impressed by programming languages, operating systems,
and computer hardware of his time. He sought the simplest and most
efficient way to control his computers. He used FORTH to program
every computer in his sight. And then, he found that he could design
better computers, because FORTH is much more than just a programming
language, it is an excellent computer architecture.
So what is FORTH?
Many books and many papers had been written about FORTH. However,
FORTH is still elusive because it has many features and
characteristics which are difficult to describe. Now that it has
erased the boundary between hardware and software, it is even more
difficult to accurately put it into words.
Let me try this way. Here it goes.
FORTH is a list processor.
FORTH has a set of commands, and an interpreter to process lists of commands.
FORTH commands are records stored in a memory area called dictionary.
A record of FORTH command has three fields: a link field linking
commands to form a dictionary, a name field containing name of this
command in ASCII string, and a code field containing executable code
and data to perform specific function for this command. It may have
an optional parameter field, which contains data needed by this
command. Link field and name field allows interpreter to look up a
command in dictionary, and code field provides executable code to
perform function assigned to this command.
A FORTH command has two representations: an external representation in
form of an ASCII name; and an internal representation in the form of a
token, which invokes executable code stored in code field. In many
FORTH systems, token is an address. However, token can take other
forms depending on implementation.
There are two types of FORTH commands: primitive FORTH commands having
machine code in their code fields, and compound FORTH commands having
token lists in their code fields.
FORTH interpreter processes two types of lists: text list and token
list. A text list contains a sequence of FORTH command names,
separated by white spaces and terminated by a carriage return. A
token list contains a sequence of tokens, which are internal
representations of FORTH commands.
FORTH has two interpreters: a text interpreter or out interpreter and
a token interpreter or inner interpreter.
Text interpreter processes lists of FORTH commands represented in
text, which consists of names of FORTH commands separated by white
spaces and terminated by carriage return. Number of commands in a
text list is not limited. A list may be in one line of text, or in a
huge text file.
Token interpreter processes lists of tokens contained in compound
commands. It is also called address interpreter, because in many
FORTH systems, tokens are addresses pointing to code fields.
Text interpreter operates in two modes: interpreting mode and
compiling mode. In interpreting mode, a list of commands names is
interpreted; i.e., commands are parsed and executed. In compiling
mode, a list of command names is compiled; i.e., commands are parsed
and corresponding tokens are compiled into a token list. This token
list can be given a name to form a new compound command, by creating a
new command record in dictionary.
FORTH compiler is FORTH text interpreter operating in compiling mode.
It compiles new compound commands, converting a text list of FORTH
commands into an equivalent token list. It builds nested token lists
one on top of others, until final solution is reached in the last
token list.
This is the most powerful feature of FORTH, in that you can compile
new compound commands, which replace lists of existing commands,
primitive and compound. Syntax of a new compound command is:
: <name> <list of existing commands> ;
FORTH compiler converts a text list of existing commands to a new
token list. Nested token lists are added until the final compound
command becomes the solution to your problem. Lists are built and
tested from bottom up. Solution space can be explored wider and
farther, and optimized solution can be found more quickly.
Following are some minor deviations in syntax of FORTH as a
programming language.
Text interpreter accepts numbers in lists. Numbers are ASCII strings
with valid numeric digits and an optional leading '-' sign. Text
interpreter pushes an integer number on data stack. FORTH compiler
compiles an integer literal in token list. Later, when token list is
interpreted, integer literal token pushes the integer on data stack.
Text interpreter accepts strings in lists. A string must follow a
string command which consumes the string. A string is a sequence of
ASCII characters terminated by a terminating character specified by
preceding string command. A string command may compile a string
literal in token list. In token list, a string literal consists of a
string token followed by string in compiled form. The string token
uses the compiled string, and passes control to next token after the
compiled string.
Lists are normally processed in consecutive sequence. However,
branches and loops are allowed, using control structure commands.
Control structure commands compile control structures in token lists.
Later, when token list is interpreted, branching and looping occurred
within control structures.
String commands and control structure commands change sequential flow
in lists. They are elements in FORTH language which require
additional grammatical rules in their usage. Otherwise, all lists are
simple, linear, sequential lists.
The preceding exposition describes what is FORTH in terms of
programming language and operating system. A complete specification
of a FORTH system must include a document on all commands; i.e., names
of commands, their effects on data and return stacks, and their
functional descriptions.
The fundamental reason that FORTH lists can be simple, linear
sequences of commands is that FORTH uses two stacks: a return stack to
stored nested return addresses, and a data stack to pass parameters
among nested commands. Parameters are passed implicitly on data
stack, and do not have to be explicitly invoked. Therefore, FORTH
commands can be interpreted in linear sequence, and tokens can be
stored in simple, linear lists. Language syntax is greatly
simplified, internal representation of tokens is greatly simplified,
and execution speed is greatly increased.
A FORTH CPU thus needs two stacks, efficient means to traverse nested
token lists, and an instruction set to support primitive commands.
This is what eP32 is designed for. It has two stacks. It has a small
instruction set, which is sufficient to code all primitive commands in
eForth. It has very efficient single cycle subroutine call and return
instructions. When we use Subroutine Threading Model and represent
tokens by subroutine call instructions, eP32 CPU itself becomes FORTH
inner interpreter. Nested token lists, as nested subroutine lists,
are traversed naturally with very little overhead in execution speed.
eP32 is the best list processor.
Forth是Chuck Moore在1960年代发明的一种编程语言,
Moore认为当时的编程语言、操作系统,以及电脑设计都不够好。他寻求最简单,最有效的方法来控制他的电脑。他用Forth在他所见到每一种电脑上编程。然后,他发现他可以设计出更好的电脑,因为Forth不仅仅是一种编程语言,它还是一个优异的计算机结构设计。
那么,什么是Forth呢?
许多书籍和许多论文已经描述Forth。然而,Forth仍然难以捉摸,因为它有许多功能和特点,很难形容。现在,它已去除了硬件和软件之间的界线,更是难以准确地用语言表达。
让我再来试一次。
Forth是一个列表处理器。
Forth有一套命令,并有一个解码器Interpreter来处理命令列表Command list。
Forth的命令是一系列的记录,存在电脑的记忆中,称为字典区。
每个Forth命令记录都有三个区段:连接区Link field用来连接字典中的命令,名称区Name
field包命令的ASCII名称;执行码区Code
field包含可执行的机器码或代码及数据,以便执行该命令的特定功能。连接区和名称区让解码器在字典中查寻命令,执行码区执行该命令并完成它的特定功能。
一个Forth命令有两种表示方法:以一个ASCII名称形式的对外表述;和以一个代码Token形式的内部表述,代码在执行码区中调用可执行的命令。在许多Forth系统中,代码是一个地址。不过,代码可以采取其他适当的形式。
Forth有两种命令:原始命令Primitive commands在执行码区中使用机器码列表,复合命令Compound
commands在执行码区中使用代码列表。
Forth解释器处理两种类型的列表:文本列表和代码列表。文本列表是Forth命令名称的序列,由空格隔开,并以回车结束。代码列表是一个代码的序列,是Forth命令内部表述的序列。
Forth有的两种解码器:一个文本解码器Text interpreter(或外部解码器Outer
interpreter)和一个代码解码器Token interpreter(或内部解码器Inner interpreter)。
文本解码器处理Forth命令的文本表述列表,其中每个Forth命令都使用空格隔开并由回车终止。在文本列表中命令的数量不限。一个列表可以是一个文本行,也可以是一个巨大的文本文件。
代码解码器处理复合命令中的代码列表。它也称为地址解码器Address interpreter,因为在许多Forth系统中,代码就是执行码区的地址。
文本解码器在两种模式下工作:编译模式和解码模式。在解码模式中,文本解码器顺序执行命令名称列表中的命令,它分解出个别命令并立即执行。在编译模式中,文本解码器将命令名称列表中的命令编译成为代码列表,也就是说,每个Forth命令相对应的代码加入到一个新的代码列表中。此代码列表可以取一个名字,形成了一个新的复合命令,同时在字典中创建一个新的命令记录。
Forth编码器是编译模式下运行的文本解码器。它编译新的复合命令,将Forth命令的文本列表转换成相当的代码列表。它用已有的Forth命令建立嵌套的代码列表,直到最后一个代码列表成为问题最终的答案。
这是Forth最强大的功能,在这里你可以编译新的复合命令,扩建原始和复合命令。创建新的复合命令语法为:
: <名称> <已有的命令列表> ;
Forth编码器将一个已有命令的文本列表转换为一个新的代码列表。不断的重复这项工作,直到最后变成了你的问题的答案。列表从底部建成并经过测试。解决方案的空间可以无限,可以更快找到优化的解决方案。
作为一种编程语言,Forth有一些简单的语法规则:
文本解码器允许在列表中出现数字。数字是一个只包括ASCII数字符的文字串。Forth解码器将数字的整数值推上数据堆栈。Forth编译器将整数值编译为整数代码,加入代码列表。之后,当执行代码列表时,整数代码会将这个整数推上数据堆栈。
文本解码器允许在列表中出现字符串。一个字符串必须跟随一个字符串命令,字符串命令读入并使用附属的宇符串。字符串由串ASCII字符组成,并用一个指定的字符结束。一个字符串命令可能在代码列表中编译一个字符串。在代码列表中,字符串代码之后加入一个编译后的字符串。字符串代码使用编译后字符串,并将控制传递给字符串以后的代码。(这样行吗?)
列表中的命令通常是顺序执行的。然而,我们可以用控制结构命令,建造分支结构和循环结构。控制结构命令在编译代码列表中建造控制结构。之后,当执行代码列表时,在控制结构内产生分支和循环。
字符串命令和控制结构命令会改变代码列表的执行顺序。他们是Forth语言中必要的元素,必须使用额外的语法规则。否则,所有的命令列表都是很简单的、线性的、连续的列表。
前面的论述,介绍了Forth的编程语言和操作系统。一个完整的Forth系统必须包括所有命令的说明文档,也就是命令的名称,功能和对数据的影响。
forth列表可以是简单的线性命令序列,其根本原因是Forth使用两个堆栈:返回堆栈嵌套存储返回地址以及数据堆栈嵌套命令之间传递的参数。数据栈隐含参数的传递,所以参数不必指明调用。因此,Forth命令可以排成线性序列来执行,也可以编译成简单的线性代码列表。语言的语法大大简化,内部表示的代码大大简化,运行速度大大提高。
因此一个Forth 电脑需要两个堆栈,执行嵌套代码列表的有效机构,以及能够支持Forth原始命令的指令集。这是eP32的基本设计。它有两个堆栈,一个小的指令集,但足以编写Forth所有原始命令。它有-个非常有效的单周期子程序调用指令和一个返回指令。当我们使用子程序调用指令作为代码,eP32本身就是一个内在解码器。嵌套代码列表就是嵌套子程序列表,可以很自然地高速度执行。
eP32是最好的列表处理器。
在 10-12-21,王晓杰<yoyof...@gmail.com> 写道:
> 英文好的就不用看了。看了也不要骂我,但欢迎纠错。
> 我英文很臭,读起来很费劲,所以,试着翻译一下啦。
> 下面是译文:
>
>
>
> ---------------------------------------------------------------------------------------------------------
>
>
>
> 我为LatticeXP2 Brevia
> Kit<http://www.latticesemi.com/products/developmenthardware/developmentkits/xp2breviadevelopmentkit.cfm>中的书
> 写了这一章,并在上周的SVFIG <http://www.forth.org/svfig/>会议上谈到过它。也许你们这些家伙会很感兴趣。
> --
> 您已訂閱「Google 網上論壇」的「符式協會」群組,因此我們特別傳送這封郵件通知您。
> 如要在此群組張貼留言,請傳送電子郵件至 figt...@googlegroups.com。
> 如要取消訂閱此群組,請傳送電子郵件至 figtaiwan+...@googlegroups.com。
> 如需更多選項,請造訪此群組:http://groups.google.com/group/figtaiwan?hl=zh-TW。
>
>