4

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

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

问题是如何保持语言文档、实现和语言的内联帮助(有一种 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 *)

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

我想在不使用任何外部处理工具的情况下做到这一点,这就是为什么我问是否有东西能够在编译阶段处理评论或类似的东西。

提前致谢

4

2 回答 2

3

你看过ocamldoc吗?

不过,通常情况下,接收注释的更多的是 .mli 文件。就您而言,您介意在 type 的定义处编写文档instruction吗?像:

(** Comment for type weather  *)
type weather =
| Rain of int (** The comment for construtor Rain *)
| Sun (** The comment for constructor Sun *)
于 2010-10-31T12:21:09.310 回答
1

你正在尝试做的事情听起来很像文学编程,所以我会建议ocamlweb,即使它是一个外部工具。

在标准发行版中,有 ocamldoc,正如 Pascal 建议的那样,但您对源语法或输出的外观没有太多控制权。

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 -> ....

于 2010-11-01T19:08:12.617 回答