OCaml 中的注释

发布于 2024-09-30 03:49:48 字数 2109 浏览 7 评论 0原文

标题可能有点误导,所以让我解释一下我想要实现的目标。

我正在编写一种编程语言,它有大量的运算符,可以处理具有不同行为的多种类型。实施正在不断发展,操作员正在改变/适应我在尝试时发现更有用的东西。

问题是如何保持语言文档、实现和语言的内联帮助(具有某种 REPL)之间的一致性。

由于大多数行为是在大模式匹配块内定义的,我想知道是否可以以某种方式(也许使用 Camlp4)对代码进行注释,以便预处理运行可以提取列出的 txt 文件(或任何类似的 csv、html 等)所有运营商都实施了。

我的意思是,如果我有类似的东西

match instruction with
  Plus -> ...
  | Minus -> ...

,我希望

match instruction with
  (* Plus, +, int -> int -> int, computes the sum *)
  Plus -> ...
  (* Minus, -, int -> int -> int, computes the difference *)
  | Minus -> ...

在注释中包含类似的信息(我使用注释语法只是为了使用某些东西,我真的从未使用过 OCaml 预处理器,所以我不知道它是如何工作的)在我编译项目时被提取并保存在某处。

也许所要求的是不可能的,我必须使用与 ocaml 预处理器/编译器本身不同的东西单独处理源代码。

有什么线索吗?

编辑:我将给出一个具体的例子来展示我想要做什么...

例如,加号指令以这种方式编译用我的语言编写的程序:

| Plus -> (fun s ->
        let o2 = vm_pop s and o1 = vm_pop s in
          (match o1, o2 with
              Float f1, Float f2 -> vm_push s (Float (f1 +. f2))
            | Float f, Int i -> vm_push s (Float (f +. float i))
            | Int i, Float f -> vm_push s (Float (float i +. f))
            | Int i1, Int i2 -> vm_push s (Int (i1 + i2))
            | Complex c1, Complex c2 -> vm_push s (Complex (Complex.add c1 c2))
            | String str, v -> vm_push s (String (Printf.sprintf "%s%s" str (string_value_short v)))
            | List l, a -> l := a :: !l; vm_push s (Types.I.List l)
            | (Set c as set), a -> c := Types.ValueSet.add a !c; vm_push s set;
            | w, w2 -> throw_exc2 "+" w w2
          ); s
      )

我希望能够注释此模式匹配的每个子句通过某种

(* Plus, +, float -> float -> float, sum, computes the sum between two floats *)
(* Plus, +, string -> any -> string, append, appends the string representation of the value *)
(* etc *)

方式,我能够预处理我的源代码并构建所有已实现操作及其类型和描述的列表,这些操作仅从注释中获取。我不需要修改我的代码中的任何内容。这只是为了在一个地方保持一致性,而不必以单独的方式跟踪所有可用的指令(因为我也需要为文档和内联帮助对它们进行索引)。

我想在不使用任何外部处理工具的情况下完成此操作,这就是为什么我询问是否有一些东西能够在编译阶段处理注释或类似的东西。

提前致谢

the title could be somewhat misleading so let me explain what I'm trying to achieve.

I'm writing a programming language that has a plethora of operators which can work on multiple types with different behaviour. The implementation is evolving and operators are changing/adapting to what I find more useful while trying it.

The problem is how to keep consistency between the language documentation, the implementation and the inline help of the language (which has a sort of REPL).

Since most of the behaviour is defined inside big pattern matched blocks I was wondering if it's possible somehow (maybe with Camlp4) to annotate the code so that a preprocess run could extract a txt file (or anything similar csv, html, whatever) that lists all the operators implemented.

I mean, if I have something like

match instruction with
  Plus -> ...
  | Minus -> ...

I would like to have something like

match instruction with
  (* Plus, +, int -> int -> int, computes the sum *)
  Plus -> ...
  (* Minus, -, int -> int -> int, computes the difference *)
  | Minus -> ...

in which the informations in comments (I used comments syntax just to use something, I really haven't never used OCaml preprocessor so I don't know how it works yet) are extracted and saved somewhere when I compile my project.

Maybe what's asking is not possible and I have to process the source separately with something different than ocaml preprocessor/compiler by itself.

Any clues?

EDIT: I'll give a concrete example to show what I would like to do...

The plus instruction for example compiles the program written in my language in this way:

| Plus -> (fun s ->
        let o2 = vm_pop s and o1 = vm_pop s in
          (match o1, o2 with
              Float f1, Float f2 -> vm_push s (Float (f1 +. f2))
            | Float f, Int i -> vm_push s (Float (f +. float i))
            | Int i, Float f -> vm_push s (Float (float i +. f))
            | Int i1, Int i2 -> vm_push s (Int (i1 + i2))
            | Complex c1, Complex c2 -> vm_push s (Complex (Complex.add c1 c2))
            | String str, v -> vm_push s (String (Printf.sprintf "%s%s" str (string_value_short v)))
            | List l, a -> l := a :: !l; vm_push s (Types.I.List l)
            | (Set c as set), a -> c := Types.ValueSet.add a !c; vm_push s set;
            | w, w2 -> throw_exc2 "+" w w2
          ); s
      )

I would like to be able to annotate every clause of this pattern match with something like

(* Plus, +, float -> float -> float, sum, computes the sum between two floats *)
(* Plus, +, string -> any -> string, append, appends the string representation of the value *)
(* etc *)

in a way that I'm able to preprocess my source code and build a sort of list of all implemented operations with their types and description, just taken from the annotation. I don't need to modify anything in my code. It's just to keep consistency in just a place without having to keep track all the available instructions in a separate way (since I need to index them for the documentation and for the inline help too).

I would like to do it without using any external processing tool, that's why I asked if there's something that is able to process comments or something similar in the compile phase.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-10-07 03:49:48

您看过 ocamldoc 吗?

不过,通常情况下,更多的是 .mli 文件接收注释。在您的情况下,您介意在类型 instruction 的定义处编写文档吗?喜欢:

(** Comment for type weather  *)
type weather =
| Rain of int (** The comment for construtor Rain *)
| Sun (** The comment for constructor Sun *)

Did you take a look at ocamldoc ?

Typically it's more the .mli file that receives the annotations, though. In your case, would you mind writing the documentation at the definition of type instruction ? Like:

(** Comment for type weather  *)
type weather =
| Rain of int (** The comment for construtor Rain *)
| Sun (** The comment for constructor Sun *)
与他有关 2024-10-07 03:49:48

你想要做的事情听起来很像文学编程,所以我建议 ocamlweb,即使它是外部工具。

在标准发行版中,有 ocamldoc,如
Pascal 建议,但您对源语法或内容没有太多控制权输出看起来像。

使用 CamlP4(标准 Ocaml 预处理器),您可以更改词法分析器以访问注释,但我认为这并不容易。向包含字符串或带有字符串扩展器的引号的模式语法添加条目要容易得多,因此您可以编写类似 | 的内容。 <:casedoc<加号、+、int ->整数-> int,计算总和>>加号-> ...

What you're trying to do sounds a lot like literate programming, so I'm going to suggest ocamlweb, even if it's an external tool.

In the standard distribution, there is ocamldoc, as
Pascal suggested, but you don't have much control over the source syntax or what the output looks like.

With CamlP4 (the standard Ocaml preprocessor), you could change the lexer to get access to comments, but I don't think this is very easy. It's a lot easier to add an entry to the pattern syntax containing either a string or a quotation with a string expander, so you'd write something like | <:casedoc<Plus, +, int -> int -> int, computes the sum>> Plus -> ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文