在Ocaml中实现一个解释器

时间:2017-01-10 23:18:22

标签: ocaml interpreter caml

我正在尝试在Ocaml中编写一个解释器,我已经定义了语法和大多数操作的语义。 我正在尝试再实施两项操作:

Ntimes :这有两个argouments,一个Integer和一个函数。 N次必须应用该功能n次。

管道:这与linux bash中的管道相同。

Ntimes的例子:

Ntimes(Int(4),f);;

管道示例:

Pipe(f1,(f2,(f3,(f4,(f5,Nil)))));;

我拥有的那个是:

语法

type ide = string;;

    type operator = Plus | Minus | Mul | Div | And | Or | Eq;;

    type exp = Int of int
             | Bool of bool
             | Den of string
             | Op of exp * operator * exp
             | Let of ide * exp * exp
             | Fun of ide * exp
             | Apply of exp * exp
             | Ifz of exp * exp * exp
             | Etup of tuple
             | Pipe of tuple
             | ManyTimes of int * exp
                and tuple = Nil | Seq of exp * tuple;;



type dexp = Dint of int | Dbool of bool | Dstring of string | Unbound | Dtuple of dexp list | Funval of efun and efun = ide * exp * dexp env ;;

RUN-TIME SUPPORT

let rec eval ((e: exp), (r:dexp env)) = match e with
         Int i -> Dint i
       | Bool i -> Dbool i
       .
       .
       .
       .
       | Etup e1 -> let v = (evalList e1 r) in Dtuple v
       | Apply(e1, e2) -> (match eval(e1, r) with
                | Funval(i, a, r1) -> eval(a, bind(r1, i, eval(e2, r)))
                | _ -> failwith("no funct in apply"))
       | NTimes(i,e) -> *I have no idea*
       | Pipe(e) -> * I have no idea*
       ;;

感谢您给我的任何帮助!

0 个答案:

没有答案