代码之家  ›  专栏  ›  技术社区  ›  Jack

OCAML中的注释

  •  4
  • Jack  · 技术社区  · 15 年前

    标题可能有点误导性,所以让我解释一下我正在努力实现的目标。

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

    问题在于如何保持语言文档、实现和语言的内联帮助(具有一种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指令以这种方式编译用我的语言编写的程序:

    | 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 *)
    

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

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

    提前谢谢

    2 回复  |  直到 15 年前
        1
  •  1
  •   Community Mohan Dere    8 年前

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

    在标准发行版中,有ocamldoc,as Pascal suggested 但您对源语法或输出的外观没有太大的控制权。

    使用camlp4(标准的ocaml预处理器),您可以更改lexer以获取评论,但我认为这并不容易。在模式语法中添加包含字符串或带字符串扩展器的引号的条目要容易得多,因此您可以编写 | <:casedoc<Plus, +, int -> int -> int, computes the sum>> Plus -> ... .

        2
  •  3
  •   Pascal Cuoq    15 年前

    你看了吗 ocamldoc ?

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

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