标题可能有些误导,所以让我解释一下我试图达到的目标。OCaml中的注释
我正在编写一个编程语言,它有许多运算符可以处理具有不同行为的多种类型。实施正在发展,运营商正在改变/适应我在尝试使用时发现更有用的东西。
问题是如何保持语言文档,实现和语言的内联帮助(具有某种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 *)
在我能够预处理我的源代码的方式
,并建立一种Ø f所有已实施操作的列表及其类型和说明,仅从注释中获取。我不需要修改代码中的任何内容。只需要在一个地方保持一致性,而无需以单独的方式跟踪所有可用的指令(因为我需要为文档和联机帮助编制索引)。
我想在不使用任何外部处理工具的情况下做到这一点,这就是为什么我会问在编译阶段是否有能够处理注释或类似内容的东西。
在此先感谢
你能给我们可以编译和运行更详细的例子,这将重现你的问题? – gasche 2010-10-31 15:27:11
你的例子中“+”和“ - ”的状态是什么?你打算注释somewhow隐含声明中缀运算符'(+)'和'( - )'在模式条款的休息吗? – gasche 2010-10-31 15:27:47