解释器
3.5.7 PRINT语句
在BASIC语言中,PRINT语句不但功能强大,而且使用灵活。理论上可以编写一个完善的方法来支持PRINT语句的所有功能,但是这超出了本章的范围。因此Small BASIC中实现的PRINT方法只能够支持PRINT语句的绝大部分特性。PRINT语句的一般形式如下:
PRINT 参数列表
这里,参数列表是由逗号或者分号分隔的表达式,或者带引号字符串的列表。
PRINT语句由print()方法负责解释。这里给出其详细代码:
// Execute a simple version of the PRINT statement.
private void print() throws InterpreterException
{
double result;
int len=0, spaces;
String lastDelim = "";
do {
getToken(); // get next list item
if(kwToken==EOL || token.equals(EOP)) break;
if(tokType==QUOTEDSTR) { // is string
System.out.print(token);
len += token.length();
getToken();
}
else { // is expression
putBack();
result = evaluate();
getToken();
System.out.print(result);
// Add length of output to running total.
Double t = new Double(result);
len += t.toString().length(); // save length
}
lastDelim = token;
// If comma, move to next tab stop.
if(lastDelim.equals(",")) {
// compute number of spaces to move to next tab
spaces = 8 - (len % 8);
len += spaces; // add in the tabbing position
while(spaces != 0) {
System.out.print(" ");
spaces--;
}
}
else if(token.equals(";")) {
System.out.print(" ");
len++;
}
else if(kwToken != EOL && !token.equals(EOP))
handleErr(SYNTAX);
} while (lastDelim.equals(";") ||
lastDelim.equals(","));
if(kwToken==EOL || token.equals(EOP)) {
if(!lastDelim.equals(";") && !lastDelim.equals(","))
System.out.println();
}
else handleErr(SYNTAX);
}
PRINT语句可用于在屏幕上显示一组变量和引号中的字符串。如果列表中的某一项与下一项用分号隔开,那么在它们之间将输出一个空格。如果其中两项被逗号分隔,那么在第一项之后将先输出一个制表符,然后再显示第二项。即使该列表由逗号或者分号结尾,也不再开始新的一行。
下面是一些有效的PRINT语句的实例:
PRINT X; Y; "THIS IS A STRING"
PRINT 10 / 4
PRINT
最后一个语句只是简单的换行。
大体来说,print()方法是顺序执行的。但是请注意,print()方法使用putBack()方法向输入流返回一个标识符。这样做是因为print()方法必须前向搜索,以判断下一输出项是被引用的字符串还是数值表达式。如果是表达式,那么这个表达式的第一项必须返回输入流,只有这样表达式解析器才能够正确地计算出该表达式的值。
3.5.8 INPUT语句
BASIC语言使用INPUT语句从键盘读入一个数值,并将该值赋给某个变量。INPUT语句有两种一般形式。第一种形式是:
INPUT 变量名
系统将显示一个问号并等待用户输入。第二种形式是:
INPUT "提示字符串",变量名
系统将显示字符串提示信息,然后等待用户输入。在这两种情况下,用户的输入值都保存在指定的变量中。例如,对于语句:
INPUT "Enter width: ", w
系统将先显示“Enter width:”,然后将用户输入的数值保存在变量w中。
input() 方法具体实现了INPUT语句的解释过程,其代码如下所示:
// Execute a simple form of INPUT.
private void input() throws InterpreterException
{
int var;
double val = 0.0;
String str;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
getToken(); // see if prompt string is present
if(tokType == QUOTEDSTR) {
// if so, print it and check for comma
System.out.print(token);
getToken();
if(!token.equals(",")) handleErr(SYNTAX);
getToken();
}
else System.out.print("? "); // otherwise, prompt with ?
// get the input var
var = Character.toUpperCase(token.charAt(0)) - 'A';
try {
str = br.readLine(); // read the value
val = Double.parseDouble(str);
} catch (IOException exc) {
handleErr(INPUTIOERROR);
} catch (NumberFormatException exc) {
/* You might want to handle this error
differently than the other interpreter
errors. */
System.out.println("Invalid input.");
}
vars[var] = val; // store it
}
由于input() 方法是顺序执行的,并且代码中标有比较详细的注释,因此阅读起来非常清晰。在此简要介绍一下操作过程。解释器先创建一个BufferedReader以读取键盘的标准输入。然后获取下一个标识符,这个标识符有两种可能:要么是提示字符串,要么是接收输入值的变量名。如果是提示字符串,那么将在在屏幕上显示,然后再次调用getToken()方法获得该字符串之后的变量名。接下来读入一个数值输入,并将其转换为doable类型最后将这个值赋给变量。
|