PFC手册fish翻译中英文.讲义
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
PFC手册fish翻译中英文.讲义
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 us efulness 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数据文件输入的程序转化成一连串命令(以“pse udo 指令”的形式)储存在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. Functions 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 and 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
为函数和变量保留的名字
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 in errors... 这两行均会引起错误
initial ; ... vainly hoping to initialize parameters... 欲将参数初始化
ini xvel xvel yvel yvel ; ...obviously ambiguous... 明显的混乱
;
; these lines would solve the problem... 这些命令行将解决问题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
2.2.3 Scope of Variables 变量范围
Variable and function names are recognized globally (as in the BASIC language). As soon as a name is mentioned in a valid FISH program line, it is thereafter recognized globally, both in FISH code and in PFC2D commands (for example, in place of a number); it also appears in the list of variables displayed when the PRINT fish command is given. A variable may be given a value in one FISH function and used in another function or in a PFC2D command. The value is retained until it is changed. The values of all variables are also saved by the SAVE command and restored by the RESTORE command.
变量和函数的名字在全球均是可识别的(用BASIC 语言)。
只要在一个有效的FISH程序命令行中提到一个名字,便能普遍被认可,无
论是以FISH指令形式还是PFC命令形式。
当给出PRINT fish 命令时就会输出一系列的变量。
在一个FISH函数中赋值的变量可用于其他函数或PFC命令。
变量的值如不改变就会一直保持。
所有变量的值可以用SAVE命令保存,用RESTORE命令存储。
2.2.4 Functions: Structure, Evaluation and Calling Scheme
函数:结构,评价和引入计划
The only object in the FISH language that can be executed is the “function.” Functions have no arguments; communication of parameters is through the setting of variables prior to invoking the function. (Recall that variables have global scope.) The name of a function follows the DEFINE statement, and its scope terminates with the END statement. The END statement also serves to return control to the caller when the function is executed. (Note that the EXIT statement also returns control —see Section 2.3.2.) Consider Example 2.3, which shows function construction and use.
在FISH语言中唯一能够执行的对象便是函数。
函数没有参数。
在援引函数前设置变量(记得变量具有广泛范围),完成参数之间的通信。
函数名放在在DINIFE后边,遇见END时表示函数范围结束。
END 在函数执行过程中起返回控制的作用。
(EXIT起相同作用)。
参考例2.3,展示函数如何构建和使用。
Example 2.3 Construction of a function
;fname: fishr3.dat
new
def xxx
aa = 2 * 3
xxx = aa + bb
end
The value of xxx is changed when the function is executed. The variable aa is computed locally, but the existing value of bb
is used in the computation of xxx. If values are not explicitly given to variables, they default to zero (integer). It is not necessary for a function to assign a value to the variable corresponding to its name. The function xxx may be invoked in one of the following ways:
(1) as a single word xxx on a FISH input line;
(2) as the variable xxx in a FISH formula — e.g.,
new var = (sqrt(xxx) / 5.6)?4;
(3) as a single word xxx on a PFC2D input line;
(4) as a symbolic replacement for a number on an input line (see Section 2.4.1);
and
(5) as a parameter to the SET, PRINT or HISTORY commands of PFC2D.
执行函数的时候,XXX的值被改变。
变量aa在函数内部运算,但是XXX 运算过程中用到bb已经存在的值。
如果该变量的值没有明确给定,那么结果为零(整数)。
一个函数没有必要赋值给相同名字的变量。
可通过以下几种方法援引函数XXX。
(1)在FISH的输入命令行中直接简单的写XXX。
(2)在FISH方程中作为一个变量XXX—例如new var = (sqrt(xxx) / 5.6)?4;
(3)在PFC命令行中直接简单写XXX。
(4)在输入行中作为一个数的替代标志。
(5)作为PFC命令SET,PRINT或HISTORY的参数。
A function may be referred to in another function before it is defined; the FISH compiler simply creates a symbol at the time of first mention and then links all references to the function when it is defined by a DEFINE command. A function can be deleted or redefined — see Section 2.2.8.
一个函数在定义之前可能被其他函数援引;FISH编程员在首次提
到的时候先简单的创建一个符号,用DINIFE 命令定义之后再引入整个函数。
一个函数可以被删除或重定义。
--见2.2.8
Function calls may be nested to any level—i.e., functions may refer to other functions, which may refer to others, ad infinitum. However, recursive function calls are not allowed (i.e., execution of a function must not invoke that same function). Example 2.4 shows a recursive function call, which is not allowed, because the name of the defining function is used in such a way that the function will try to call itself. The example will produce an error on execution.
函数可以在任何程度上嵌入,函数可以援引其它函数,而其它函数也可以无限援引。
然而,不允许循环引入函数。
(即函数执行是不能引入相同的函数)例2.4展示循环引入函数的例子,这是不允许的,因为定义中的函数名称在函数引入自己的时候被使用了。
这个例子在执行时会引发错误。
Example 2.4 A recursive function call
;fname: fishr4.dat
def force_sum
force_sum = 0.0
cp = contact_head
loop while cp # null
force_sum = force_sum + c_nforce(cp)
cp = c_next(cp)
end_loop
end
The same function should be coded as shown in Example 2.5, below.
类似的函数应该像例2.5所示编程。
Example 2.5 Removing recursion from the function shown in Example 2.4
;fname: fishr5.dat
def force_sum
sum = 0.0
cp = contact_head
loop while cp # null
sum = sum + c_nforce(cp)
cp = c_next(cp)
end_loop
force_sum = sum
end
The difference between variables and functions is that functions are always executed whenever their name is mentioned; variables simply convey their current values. However, the execution of a function may cause other variables (as opposed to functions) to be evaluated. This effect is useful, for example, when several histories of FISH variables are required—only one function is necessary in order to evaluate several quantities, as in Example 2.6.
变量与函数不同的地方在于,只要提到名字,函数必须一直运行;变量只是简单的输送它们当前的值。
然而,函数运行时会评估其他变量(如果与函数相冲突)。
这种效果是很有用的,例如,当需要若干个FISH变量记录时,只需一个函数便可评估几个量,见例2.6.
Example 2.6 Evaluation of variables
;fname: fishr6.dat
new
def h_var_1
;
; bp22 = pointer to ball with ID = 22, set during creation...指针指向ID为22的球
; bp45 = pointer to ball with ID = 45, set during creation...指
针指向ID为45的球
;
xx = b_x(bp45)
h_var_1 = b_x(bp45)
h_var_2 = b_x(bp22)
h_var_3 = abs(h_var_2 - xx) ; use of xx here avoids recursion 使用xx以避免循环
h_var_4 = b_xvel(bp45)
h_var_5 = b_xvel(bp22)
h_var_6 = abs(h_var_5 - h_var_4)
end
The PFC2D commands to request histories might be:
hist h var 1
hist h var 2
hist h var 3
hist h var 4
hist h var 5
hist h var 6
The function h var 1 would be executed by the PFC2D’s history logic every few steps but, as a side effect, the values of h var 2 through h var 6 would also be computed and used as history variables.
函数h var 1每隔几时步便被PFC记录理念执行一次,但是,因为侧面影响,h var 2到h var 6的值也能计算且用作记录变量。
2.2.5 Data Types 数据类型
There are four data types used for FISH variables or function values:
1. Integer exact numbers in the range -2,147,483,648 to +2,147,483,647;
2. Floating-point approximate numbers with about 15
decimal digits of precision, with a
range of approximately 10?300 to 10300;
3. String packed sequence of any printable characters; the sequence may be any length,
but it will be truncated on the printout. Strings are denoted in FISH and PFC2D by a
sequence of characters enclosed by single quotes —e.g., ‘Have a nice day’; and
4. Pointer machine address —used for scanning through linked lists. They have an
associated type from the object to which the pointer refers, except for the null pointer.
FISH变量或函数值有四类数据类型:
1.Integer:(-2147483648,+2147483647)范围内的准确的数字
2.Floating-point:有精度为15位小数的大概数字,范围在(10-300,10,300)
3.String:任何可打印的符号的一串集合体,串可以有任意的长度,但是在输出时可能被截断。
在FISH
或PFC中的字符串包含在单引号内—例如:‘Have a nice day’
4.Pointer:机器地址—用来扫描相连的一系列符号。
指针所指的符号,除了null 以外,都拥有一个相
同的形式。
A variable in FISH can change its type dynamically, depending on the type of the expression to which it is set. To make this clear, consider the assignment statement
var1 = var2
If var1 and var2 are of different types, then two things are done: first, var1’s type is converted to var2’s type; second, var2’s data are t ransferred to var1. In other languages, such as
FORTRAN or C, the type of var1 is not changed, although data conversion is done. By default, all variables in FISH start their life as integers. However, a statement such as
var1 = 3.4
causes var1 to become a floating-point variable when it is executed. The current type of all
variables may be determined by giving the PFC2D command PRINT fish — the types are denoted in the printout.
FISH中的变量可以动态的改变它的类型,取决于表达类型和设置类型。
为了更易懂,以如下指派命令为例:var1 = var2
如果var1和var2属不同类型,则会发生两件事情:一, var1 的类型转化为var2的类型;二,var2的值赋予给 var1。
在其他语言中,例如FORTRAN或C语言,var1的类型没有改变,虽然数值已经改变。
默认状态下,所有FISH变量的初始类型为整数。
但是,命令如var1=3.4
在运行时,使得var1的类型变为Floating-point。
所有变量的当前类型可由PFC的命令PRINT fish 确定—变量类型将会在输出中说明。
The dynamic typing mechanism in FISH was devised to make programming easier for nonprogrammers. In languages such as BASIC, numbers are stored in floating-point format, which can cause difficulties when integers are needed for, say, loop counters. In FISH, the type of the variable adjusts naturally to the context in which it is used. For example, in the following code fragment, n = n + 2
xx = xx + 3.5
the variable n will be an integer and will be incremented by exactly 2, and the variable xx will be a floating-point number, subject to the usual truncation error but capable of handling a much bigger dynamic range. The rules governing type
conversion in arithmetic operations are explained in Section 2.2.6. The type of a variable is determined by the type of the object on the right-hand side of an assignment statement. This applies both to FISH statements and to assignments done with the PFC2D SET command. Both types of assignment may be used to change the type of a variable according to the value specified, as follows.
1. An integer assignment (digits 0-9 only) will cause the variable to become an integer (e.g., var1 = 334).
2. If the assigned number has a decimal point or an exponent denoted by “e”or “E,” then the variable will become a floating-point number (e.g., var1 =3e5; var2 = -1.2).
3. If the assignment is delimited by single quotes, the variable becomes a string, with the “value” taken to be the list of characters inside the quotes (e.g., var1 = ‘Have a nice day’).
FISH中动态分类原理是为了在没有程序员的情况下使程序更简易而设计的。
像BASIC等语言中,数字都是以floating-point方式储存的,这样就会在循环计算等需要整数时造成困难。
在FISH中,变量的类型根据需要自然调整。
例如,在下面的指令碎片中,
n = n + 2
xx = xx + 3.5
变量n将变为一个整数而且加上整数2,而变量xx则是一个小数,易出现普通的缩写错误,但是操作能力却大大提高。
2.2.6解释在算术操作中管理类型转化的规则。
变量的类型是由指派命令右边的对象类型决定的。
这些规则应用在FISH函数和PFC中SET命令的指派过程中。
根据如下数值的指定,两种类型的指派可用来改变变量的类型。
1.一个整数指派(只有0-9之间的数字)将会使变量变成整数。
(例var 1=334)
2.如果指派的数字带有小数点或者用e或E表示的指数,则这个变量将变成floating-point形式。
3.如果指派被单引号包含,变量就变成字符串,“值”就是单引号里边的一串字符。
Type conversion is also done in assignments involving pre-defined variables or functions. These rules are presented in Section 2.5. Note that pointer variables may not be converted to or from other variable types.
引入预定义变量或函数时的指派也会引起类型转换。
2.5讲述这些规则。
注意指针变量不能转化为别的变量类型,或者从别的变量类型转化而来。
2.2.6 Arithmetic: Expressions and Type Conversions
算术:表示及类型转化
Arithmetic follows the conventions used in most languages. The symbols
/ * - +
denote exponentiation, division, multiplication, subtraction and addition, respectively, and are applied in the order of precedence given. Arbitrary numbers of parentheses may be used to render explicit the order of evaluation; expressions within parentheses are evaluated before anything else. Inner parentheses are evaluated first. As an example, FISH evaluates the following variable xx as 133:
xx = 6/3*4?3+5
The expression is equivalent to
xx = ( (6/3) * (4?3) ) + 5
If there is any doubt about the order in which arithmetic operators are applied, then parentheses should be used for clarification.
算术遵循这些在大部分语言中转化。
符号? / * - + 表示幂、除、乘、减和加,与此对应的,按照给定的优先顺次应用。
加上任意数量的圆括号以明确运算的顺序。
圆括号内的表达式比其他的先运算,最
里面的圆括号最先运算。
例如:FISH运算变量xx的值为133:xx = 6/3*4?3+5
这个表达式与下面的一样
xx = ( (6/3) * (4?3) ) + 5
如果对算术操作的顺序还有任何含糊,则加上圆括号以明示。
If either of the two arguments in an arithmetic operation are of floating-point type, then the result will be floating-point. If both of the arguments are integers, then the result will be integer. It is important to note that the division of one integer by another causes truncation of the result — for example, 5/2 produces the result 2, and 5/6 produces the result 0. Arithmetic operations are not allowed for pointer variables except for the addition of an integer (see Section 2.5.2.8). However, two pointers may be tested for equality in an IF statement — e.g.,
if zp # null
如果算术操作中的任何一个参数是floating-point 类型,那么运算结果是floating-point。
如果两个参数都是整数,结果才能是整数。
注意一个整数除以另一个整数,会使得结果四舍五入—例5/2的结果是2,5/6的
结果是0。
除非加上一个整数,算术操作不允许指针变量。
但是,在IF命令中,两个指针可以在量上进行比较,例if zp # null
2.2.7 Strings 字符串
There are three main FISH intrinsic functions that are available to manipulate strings
FISH内部主要有三种操纵字符串的函数。
in( s )
prints out variable s if it is a string, or the message “Input?” if it is not, and then waits for input from the keyboard. (The returned value depends on the characters that are typed. FISH tries to decode the input first as an integer and then as a floating-
point number — the returned value
will be of type int or float if a single number has been typed in that can be decoded as integer or floating-point, respectively. The number should be the only thing on the line. However, if it is followed by a space, comma or parenthesis, then any other characters on the line are ignored. If the characters typed in by the user cannot be interpreted as a single number, then the returned value will be a string containing the sequence of characters. The user’s FISH function can determine what has been returned by using the function type( ).)
如果是字符串则输出变量s,如果不是则给出信息“INPUT ?”,然后等待键盘输入。
(返回值决定于键入的字符。
FISH尝试将输入先翻译为整数,然后小数—如果键入简单的数字,翻译为整数或者小数,与之相对应,返回值是整数或是小数的形式。
命令行应该只包含数字。
但是,如果接着有空格、逗号或括弧,则命令行中所有其他字符都可以忽略。
如果用户输入的字符不能解释为简单数字,则返回值是包含一串字符的字符串。
用户的FISH函数可以利用函数type()决定返回什么。
out( s )
prints the message contained in string-variable s to the screen and to the log file, if it is open. (The variable s must be of type string. The returned value of the function is zero if no error is detected, and 1 if there is an error in the argument — e.g., if s is not a string.)
输出字符串变量s包含的内容到屏幕上或是打开的记录文件。
(变量s必须是字符串类型,如果没有发现错误,函数的返回值是0.如果参数中有错误比如s不是字符串,则返回值为1.
string( g )
converts g to a string. (If g is already of type string, then the function simply returns g as its value. If g is int or float, then a
character string will be returned that corresponds to the number as it would be printed out. However, no blanks are included in the string.)
将g转化为字符串形式。
(如果g已经是字符串类型,则函数只返回g的值。
如果g是整数或是小数,则返回与打印的数字相对应的的字符串。
但是,字符串中没有空格。
One use of these functions is to control interactive input and output. Example 2.7 demonstrates for user-supplied input parameters for Young’s modulus and Poisson’s ratio.
某人用这些函数进行计算机输入和输出交互。
例2.7说明如何输入参数为杨氏模量和泊松比赋值。
Example 2.7 Control of interactive input
;fname: fishr7.dat
new
def in_def
xx = in(msg+’(’+’default:’+string(default)+’):’)
if type(xx) = 3
in_def = default
else
in_def = xx
endif
end
def moduli_data
default = 1.0e9
msg=’Input Young‘s modulus ’
y_mod = in_def
default = 0.25
msg=’Input Poisson‘s ratio ’
p_ratio = in_def
if p_ratio = 0.5 then
ii = out(’ Bulk mod is undefined at Poisson‘s ratio = 0.5’) ii = out(’ Select a different value --’)
p_ratio = in_def
endif
s_mod = y_mod / (2.0 * (1.0 + p_ratio))
b_mod = y_mod / (3.0 * (1.0 - 2.0 * p_ratio))
end
moduli_data
print y_mod p_ratio
print s_mod b_mod
The only arithmetic operation that is valid for string variables is addition. As demonstrated in Example 2.7, this causes two strings to be concatenated. Table 2.2 identifies special characters available in string manipulation.
唯一对字符串有效的运算操作就是加法。
例2.7所示,加法使得两个字符串相连。
表2.2定义在字符串操纵过程中有效的特殊字符。
Table 2.2 String manipulation characters
\’ place single quote in string在字符串中加单引号
\" place double quote in string 在字符串中加双引号
\ \ place backslash in string 在字符串中加反斜杠
\b backspace 退格
\t tab
\r carriage return
\n CR/LF
It is invalid for only one argument in an arithmetic operation to be a string variable. The intrinsic function string( ) must be used if a number is to be included as part of a string variable (see variable。
(see variable x in Example 2.7). Also note the use of intrinsic function type( ), which identifies the type of argument.
字符串变量的算术操纵中若只有一个参数,便是无效的。
利用内
在函数string使得数字成为字符串变量的一部分(见例2.7中的变量x)。
注意内在函数type()如何定义参数类型。
2.2.8 Redefining FISH Functions 重新定义FISH函数
A FISH function can be redefined. If the same name as an existing function is given on a DEFINE line, the code corresponding to the old function is first deleted (and a warning printed), and the new code substituted. The following are some notes of caution.
1. The variables that are used in a function still exist even if the function is redefined; only the code is deleted. Since variables are global, it is likely that they are used elsewhere.
2. If a function is replaced by another of the same name, all calls to the old function from existing functions are removed. All fishcall links to the replaced function are removed.
FISH函数可以重新定义。
如果在DINIFE命令行中出现与已有函数相同的名字,则旧函数首先被删除(出现警告信息),以新指令代替。
下面是一些注意事项。
1.即使函数重新定义,函数中所使用的变量依然存在。
只有指令被删除。
变量是具有普遍意义的,
很可能在别的地方用到。
2.如果函数被其他同名函数替代,不能从存在的函数中引用旧函数。
所有与被替代函数相连接的
fishcall 将消失。
2.3 FISH Statements FISH函数
There are a number of reserved words in the FISH language; they must not be used for userdefined variable or function names. The reserved words, or statements, fall into three categories, as explained below.
在FISH 语言中有很多服务性的词语。
它们不会用来用户定义变量或函数名称。
服务性语言或陈述分为三类,如下所述。
2.3.1Specification Statements 详细陈述
The following words are normally placed at the beginning of a FISH function. They alter the characteristics of the function or its variables but do not affect the flow of control within the function. They are only interpreted during compilation.
下列词语一般放在FISH函数开始部分。
它们改变函数的特征或者函数的变量,但是不影响函数内部的控制流。
它们只是在编辑的时候翻译出来。
ARRAY var1(n1, n2 . . . ) . . .
This statement permits arrays of any dimension and size to be included in FISH code.
这条陈述保证FISH指令可以包含任何维数任何大小的数列。
In the above specification, var1 is any valid variable name and n1, n2 . . . are either actual integers, or single user-defined variables (not expressions) that have integer values at the time the ARRAY statement is processed. There may be several arrays specified on the same line (e.g., var2, above); the number of dimensions may be different for each array. The ARRAY statement is a specification and is acted on during compilation, not execution (it is ignored during execution). Note the following.
1. The given name may be an existing single variable. If so, it is converted to an array and its value is lost. If the name does not already exist, it is created.
2. The given name may not be that of a function or the name of an existing array (i.e., arrays cannot be redefined).
3. The given dimensions (n1, n2, . . . ) must be positive integers or evaluate to positive integers
(i.e., indices start at 1, not 0), or be the scalar dim.
4. There is no limit to the number and size of the array dimensions, except memory capacity and the maximum line。