2 FISH REFERENCE
2.1 Introduction and Overview
简介和概述
This section contains a detailed reference to the FISH language. Following the introduction, Section2.2 describes the rules of the language and how variables and functions are used. Section 2.3explains FISH statements and Section 2.4 describes how the FISH language links with PFC2D. Pre-defined FISH variables, functions and arrays are described in Section 2.5.
这部分包含FISH语言的详细参考。接下来,2.2描述语言规则及如何运用变量和函数。2.3解释FISH 陈述。2.4描述FISH语言如何与PFC联系在一起。2.5讲述如何预定义FISH变量、函数和数列。
FISH is a programming language embedded within PFC2D that enables the user to define new variables and functions. These functions may be used to extend PFC2D’s usefulness or add use r defined features. For example, new variables may be plotted or printed, special particle generators may be implemented, servo-control may be applied to a numerical test, unusual distributions of properties may be specified, and parameter studies may be automated.
FISH是PFC内置的一种编程语言,用户可以自定义变量和函数。这些函数可用来扩展PFC的功能,或者增加用户定义的特征。例如:输出或者打印新的变量、实现特殊的颗粒生成、数值测试中中应用伺服控制、定义特殊的颗粒分布以及自动研究参数。
FISH is a “compiler” (rather than an “interpreter”). Programs entered via a PFC2D data file are translated into a list of instructions (in “pseudo-code”) stored in PFC2D’s memory space; the original source program is not retained by PFC2D. Whenever a FISH function is invoked, its compiled pseudo-code is executed. The use of compiled code — rather than interpreted source code —enables programs to run much faster. However, unlike a compiler, variable names and values are available for printing at any time; values may be modified by the user by using PFC2D’s SET command.
FISH是一个编程员,而不是一个翻译员。通过PFC数据文件输入的程序转化成一连串命令(以“pseudo 指令”的形式)储存在PFC的存储空间。PFC没有保留初始源程序。无论何时援引FISH函数,其编好的pseudo 指令执行。编好的指令—不是翻译好的源指令—的作用是使得程序更快的运行。然而,不像编程员那样,FISH在任何时间都可以打印变量的名字和数值,用户可以通过PFC中的SET命令修改数值。
FISH programs are simply embedded in a normal PFC2D data file — lines following the word DEFINE are processed as a FISH function; the function terminates when the word END is encountered. Functio
ns may invoke other functions, which may invoke others, and so on. The order in which functions are defined does not matter, as long as they are all defined before they are used (e.g., invoked by a PFC2D command). Since the compiled form of a FISH function is stored in PFC2D’s memory space, the SAVE command saves the function and the current values of associated variables.
FISH程序简单的包含在正常的PFC数据文件里—DIFINE 后边的命令行按FISH函数处理;函数以遇到END为结尾。函数也许会援引其他函数,其它函数亦会援引其他函数等等。函数定义的顺序没有影响。因为FISH函数的程序形式储存在PFC的存储空间,SAVE命令会存储函数及相关变量的当值。
A summary of FISH language and intrinsic names is provided in Section 2 in the Command and FISH Reference Summary.
在Command and FISH Reference Summary.的第二章提供FISH语言及其内在名字的总结。
2.2 FISH Language Rules, Variables and Functions
FISH语言规则,变量及函数
2.2.1 Lines 命令行
FISH programs can be embedded in a normal PFC2D data file or may be entered directly from the keyboard. Lines following the word DEFINE are taken to be statements of a FISH function; the function terminates when the word END is encountered. A valid line of FISH code must take one of the following forms.
FISH程序包含在普通的PFC数据文件中或者直接从键盘键入。DIFINE 后边的命令行按FISH函数处理;函数以遇到END为结尾。FISH的有效命令行必须是下述格式中的一种。
1. The line starts with a statement, such as IF, LOOP, etc. (see Section
2.3).
2. The line contains one or more names of user-defined FISH functions, separated
by spaces — e.g.,
fun 1 fun 2 fun 3
where the names correspond to functions written by the user. These functions
are executed in order. The functions need not be defined prior to their reference
on a line of FISH code (i.e., forward references are allowed).
3. The line consists of an assignment statement (i.e., the expression on the right
of the = sign is evaluated and the value given to the variable or function name
on the left of the = sign).
4. The line consists of a PFC2D command, provided that the line is embedded in a
section of FISH code delimited by the COMMAND –ENDCOMMAND statements
(see Section 2.3.3).
5. The line is blank or starts with a semicolon.
1.命令用IF,LOOP等开始。(见
2.3)
2.如果一行包含一个或更多用户定义FISH函数的名字,以空格号隔开。
例如fun_1 fun_2 fun_3
名字与用户所写的函数相对应,这些函数有序执行。一行FISH指令中,函数没有必要比其参考前定义。
(即允许参考在前面)
3.命令行包含指派声明(例如等号右边的数学式被运算且其值赋予给等号左边的函数名称或是变量)
4.命令行由PFC命令组成,通过COMMAND—ENDCOMMAND 分隔FISH 指令。命令行包含在FISH指令的一部分里面。
5.命令行以分号开始或者结束。
FISH variables, function names and statements must be spelled out in full; they cannot be truncated, as in PFC2D commands. No continuation lines are allowed; intermediate variables may be used to split complex expressions. FISH is “case-insensitive” by default — i.e., it makes no distinction between upper case and lowercase letters; all names are converted to lowercase letters. (Note that this can be changed with the command SET case sensitivity on.) Spaces are significant (unlike inFORTRAN) and serve to separate variables, keywords, and so on; no embedded blanks are allowed in variable or function names. Extra spaces may be used to improve readability—for example, by indenting loops an
d conditional clauses. Any characters following a semicolon ( ; ) are ignored; comments may be embedded in a FISH program by preceding them with a semicolon. Blank lines may be embedded in a FISH program.
函数的变量,函数名称或者声明必须完整拼写,在PFC命令中不能缩写。不允许有连续的命令行;可以利用中间变量分解复杂的表示式。FISH在默认状态下为“对情况不敏感”—即大写与小写字母之间没有区别,所有的名字都转换为小写字母(注意可以通过(SET case sensitivity on)命令改变)。有相当大的空间来存储各自的变量、关键词等等(与FORTRAN不同)。在变量或是函数名中不允许含有空格。还留有格外的空间来提高可读性--例如通过缩进循环或是假定子句。分号后面的所有字都被忽略;FISH程序中包含的注释放在分号后面;FISH程序中可以包含空白行。
2.2.2 Reserved Names for Functions and Variables
为函数和变量保留的名字
values翻译Variable or function names must start with a non-number and must not contain any of the following symbols:
. , * / + - ˆ = < > # ( ) [ ] @ ; ’ "
变量或函数名必须以非数字开始,并且不包含下列符号:. , * / + - ˆ = < > # ( ) [ ] @ ; ’ "
User-defined names can be any length, but they are truncated in printout and in plot captions, due to line-length limitations. In general, names may be chosen arbitrarily, although they must not be the same as a FISH statement (see Section 2.3) or a pre-defined variable or function (see Section 2.5). There are also many other words used in PFC2D input that should be avoided. The list contained in Table 2.1 shows all words that could give rise to a conflict if used to name a FISH variable or function. However, the potential conflict depends on the way in which the chosen name is used. For example, the word gravity could be used as a FISH variable, provided that it is simply referred to inside a FISH function; conflict would arise only if it is necessary to use the SET command to set its value, since gravity is a valid argument to the SET command. Similarly, it may be impossible to print the value of a FISH variable if its name is the same as a parameter for
the PRINT command. If in doubt, avoid any of the names listed in Table 2.1, or contractions of the names (since PFC2D allows of keywords and commands).
用户定义的名字可以在任何长度,但是由于行的长度限制,在打印或输出加标题时会被截断。一般说来,名字是任意选择的,只要与FISH参数或者预先定义的变量或函数不同就行,还应避免PFC输入时将
要用的的词。表2.1列出的词,如果用来命名FISH变量或函数,便会引起冲突。但是,潜在的冲突决定于如何利用所选中的名字。例如:gravity可以用作FISH变量,只是简单的在FISH函数内部涉及。只有当用SET命令去设置它的值,冲突才会发生,因为对于SET命令来说,gravity是个有效的词语。类似的,当一个FISH变量的名字与PRINT命令的参数一样时,也不可能把这个变量的值打印出来。如果有疑问,避免表2.1中的所有词,或者缩写名字。(因为PFC允许关键词或者命令缩写)
As an alternative strategy, the PFC2D command SET safe conversion can be used to force the explicit recognition of FISH variables in a command-line instruction. The @ symbol
pre-pended to any FISH variable identifies the object as such and forces the command-line parser to ignore any other possible interpretation. For example, suppose that a FISH function initial, as shown in Example 2.1, has been created. Attempting to run the function from the command prompt would ordinarily produce an error, since the command INITIAL would take precedence over the FISH initial. However, using the FISH identifier@in front of the variable gives us an unambiguous, correct interpretation.
作为一个相对的策略,可以用PFC中的命令SET safe conversion 强制FISH变量清晰的组织成为命令行说明。@置于任何FISH变量之前都表示照这样鉴定这个对象,强制命令行的语法分析器忽略任何其他可
能的解释。例如:假定例2.1中的FISH函数initial 已经创建。试图从命令提示中运行这个函数一般会引起错误。但是,在变量前面使用函数标识符@,便会得到一个明白的,正确的解释。
Example 2.1 Explicit identification of FISH variables
;fname: fishr1.dat
def initial
xvel = 0.0
yvel = 1.0
end
;
; these lines will both result 这两行均会引起错误
initial ; ... vainly hoping to 欲将参数初始化
ini xvel xvel yvel yvel ; ... 明显的混乱
;
; these lines would solve 这些命令行将解决问题
set safe_conversion on
@initial
ini xvel @xvel yvel @yvel
By default, user-defined variables represent single numbers or strings. Multi-dimensional arrays of numbers or strings may be stored if the ARRAY statement is used. Section 2.3.1 defines the way in which arrays are created and used. At present, there is no explicit printout or input facility
for arrays, but functions may be written in FISH to perform these operations. For example, the contents of a two-dimensional array (or matrix) may be initialized and printed, as shown in Example 2.2.
默认状态下,用户自定义的变量只能代表单个数字或是字符串。如果使用ARRAY才能储存数字或字符串的多维数组。2.3.1定义了数组的创建和使用方法。目前,没有明确的数组输入或输出设备,但是可以在FISH中写函数执行这些操作。例如:如例2.2所示,可以初始化和打印二位数组(或矩阵)的内容。
Example 2.2 Initializing and printing FISH arrays
;fname: fishr2.dat
new
def afill ; fill matrix with random numbers 用任意数字填满矩阵
array var(4,3)
loop m (1,4)
loop n (1,3)
var(m,n) = urand
end_loop
end_loop
end
def ashow ; display contents of matrix 展示矩阵内容
loop m (1,4)
hed = ’ ’
msg = ’ ’+string(m)
loop n (1,3)
hed = hed + ’ ’+string(n)
msg = msg + ’ ’+stri ng(var(m,n))
end_loop
if m = 1
dum = out(hed)
end_if
dum = out(msg)
end_loop
end
afill
ashow
Upon execution, the following output is produced:
Pfc2D>afill
Pfc2D>ashow
1 2 3
1 5.77131092280e-001 6.23065859697e-001 7.69736401269e-001
2 8.38073820221e-001 3.36399274746e-001 8.56969321222e-001
3 6.32138908185e-001 5.41650756113e-002 1.82265148152e-001
4 8.59740023776e-001 9.27965545082e-001 9.63324485278e-001
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论