问题描述
我正在尝试在ocaml中编写一些代码,以解析以下c代码中的预定义函数
main {
x = function1(3);
x = function2(x,2);
}
这是我的ocaml代码:
match expr with
|Bool(b) ->( match b with
|true -> (*do things*)
|false -> (*do things*) )
|Call(f,args) ->( match f with
| function1(x) -> (*do things with x*)
| function2(x,n) -> (*do things with x and n*)
|_ -> failwith "not implemented yet " )
暂时,让我们假设我正在解析的语言只有两个函数,我想在C代码中检索参数以便在ocaml程序中使用它们,但是我在行中遇到语法错误包含与sth()的匹配项
删除括号和参数使程序运行,但这不是我想要的,我需要参数...
我不知道我的匹配有什么问题,有人可以向我解释正确的语法吗?
预先感谢
解决方法
模式匹配仅匹配类型构造函数。
要做的第一件事是写下您要匹配的类型:
type typ =
| Bool of bool
| Function1 of int
| Function2 of int * int
然后您的功能将类似于此(将所有不同的情况组合在一个单独的比赛情况中):
let eval
: typ -> unit (* The function signature *)
= fun t ->
match t with
| Bool true -> print_endline "true"
| Bool false -> print_endline "false"
| Function1 number -> print_endline (string_of_int number)
| Function2 (n1,n2) -> print_endline (string_of_int (n1 + n2))