functional programming - In OCaml, I defined in AST file that stmt can be stmt or stmt List, but when used in function, it reports error -
i defined in ast file stmt=|stmt|stmt list. have function parameter of type stmt, cannot use stmt list? can tell me how make work?
the error when compiling is:
error: expression has type ast.stmt list expression expected of type ast.stmt
below code function:
let rec eval_stmt (stmt:stmt) (ps:proc_state) (env:environment) (store:store) : (stmtevalres*proc_state*store) = match stmt | printint e -> let r = eval_expr e ps env store in print_int r; (next, ps, store) | printstr s -> print_string (str.global_replace (str.regexp "\\\\n") "\n" s); (* escaping characters here because it's not done in parser *) (next, ps, store) | list (stmt1::stmts) -> eval_stmt stmt1 ps env store; eval_stmt stmts ps env store;(next,ps,store)
below ast file:
type stmt = (* statements in c-flat language *) empty | varass of expr * expr (* expressions can statements in themselves. useful when expression function call. calls built-in functions special cases of calls. *) | expr of expr | printstr of string (* string static! *) | printint of expr (* expr should int *) (* control flow *) | ifthen of cond * stmt | ifthenelse of cond * stmt * stmt | switch of expr * (int * stmt list) list * stmt list (* switch (cond, cases, default) *) | while of cond * stmt | of stmt * cond * stmt * stmt | break (* used in switch, while , *) | continue (* used in while , *) (* nested statements *) | list of stmt list ;;
your define stmt
variant type, i.e., finite collection of alternatives. 1 of alternatives list
indeed contains stmt list
. doesn't mean stmt list
same type stmt
. means value list ...
(with list of statements inside) of type stmt
.
Comments
Post a Comment