我有一个文件
foo.sml
,其中包含一行标准 ML 代码:
Int.toString 5
这在 SML/NJ 中运行良好,但在 MLton 中则不然:
$ cat foo.sml
Int.toString 5
$ sml < foo.sml
Standard ML of New Jersey (64-bit) v110.99.5 [built: Thu Mar 14 17:56:03 2024]
- = [autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[autoloading done]
val it = "5" : strin
$ mlton foo.sml
Error: foo.sml 3.0.
Syntax error found at EOF.
Error: foo.sml 3.0-3.0.
Parse error.
如果我添加分号,问题就解决了:
$ cat foo.sml
Int.toString 5;
$ sml < foo.sml
Standard ML of New Jersey (64-bit) v110.99.5 [built: Thu Mar 14 17:56:03 2024]
- [autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[autoloading done]
val it = "5" : string
-
$ mlton foo.sml
$ ./foo
$
为什么在这行代码中需要分号才能让 MLton 成功编译?
SML 程序的语法定义如下(标准 ML 的定义,第 64 页):
𝑝𝑟𝑜𝑔𝑟𝑎𝑚 ::= 𝑡𝑜𝑝𝑑𝑒𝑐 `;` 𝑝𝑟𝑜𝑔𝑟𝑎𝑚?
因此,程序必须以分号终止。所以 MLton 是正确的,而 SML/NJ 比标准更自由,允许省略文件结尾之前的最后一个分号。