1

我将一个 OCaml 项目移植到 ReasonML,但首先移植到 BuckleScript。我在项目目录中播种

bsb init .

并复制 *.ml 文件。编译

npm run build

给我错误:

[1/6] 搭建src/demo.cmj
文件“/d/ProgLang/reason/src/demo.ml”,第 2 行,字符 7-17:错误:未绑定值 Str.regexp
[2/6] 构建 src/lex.cmj
文件“/d/ProgLang/reason/src/lex.ml”,第 13 行,字符 26-44:错误:未绑定值 Str.search_forward

我的 package.json 有

 "devDependencies": {
    "bsb-native": "^4.0.7000"
  }

./node_modules/bsb-native/vendor/ocaml/lib/ocaml包含文件str.a str.cma str.cmi str.cmx str.cmxs str.mli,但没有像 str.ml 这样的源。此外,https://reasonml.github.io/api/Str.html记录了我需要的功能,但是在定位 Javascript(节点)时如何链接它们?

我可以同时使用 Javascript 或本机目标,但我想从 OCaml 语法升级到 ReasonML。如果你需要我的 demo.ml,这里是:

let qq=Str.regexp "/q/" and
() = Js.log "Hello, STR! BuckleScript"
4

1 回答 1

2

模块公开的功能Str实际上是用 C 实现的,因此不容易移植到 JavaScript。正则表达式的任何 JavaScript 实现都可能比内置的 JavaScript 实现慢得多。

Furthermore, providing a common interface to multiple implementations isn't trivial since Regular Expression implementations differ in more or less subtle ways and aren't fully compatible with each other. Most aren't even regular, despite the name.

There's been some discussion around how a common interface for JS and native regexes can be accomplished, but there's no obvious solution and I'm not aware that anything has been concluded. But I would think that at the very least you'd have to implement a parser that would only accept the common subset of regex syntax, before passing it on to the underlying regex engine to be parsed again, which would obviously have a notable performance impact and is non-trivial to implement.

For now, you should be able to use Js.Re for JavaScript regexes and conditional compilation in order to use Str or some other implementation natively.

Another alternative might be to compile ocaml-re to JavaScript using and then interface with that using externals. Or port the project to bsb if possible, to use it directly. That will likely increase the size of your code bundle significantly, however, and it's not particularly straightforward to accomplish.

于 2019-05-09T13:13:32.650 回答