我有一个ocaml函数,其中我模式匹配几个案例。我一直遇到类型错误,我无法看到如何处理它。任何帮助,将不胜感激。这是我收到的错误消息:“文件”eval.ml“,第40行,字符18-35: 错误:此表达式的类型为Env.symEntry“。第40行是”let l = binding_of env x in“
的行(* eval_expr: expr -> proc_state -> env -> store -> value *)
(* eval_expr: expr -> proc_state -> env -> store -> int *)
let rec eval_expr (expr:expr) (ps:proc_state) (env:environment) (store:store) : int = match expr with
Add (e1, e2) ->
let r1 = eval_expr e1 ps env store in
let r2 = eval_expr e2 ps env store in
r1 + r2
| Sub (e1, e2) ->
let r1 = eval_expr e1 ps env store in
let r2 = eval_expr e2 ps env store in
r1 - r2
| Mul (e1, e2) ->
let r1 = eval_expr e1 ps env store in
let r2 = eval_expr e2 ps env store in
r1 * r2
| Div (e1, e2) ->
let r1 = eval_expr e1 ps env store in
let r2 = eval_expr e2 ps env store in
r1 / r2
| Neg (e1) ->
let r1 = eval_expr e1 ps env store in
r1 * -1
| IntConst i -> i
(* - variable reads: handle the Id constructor from expr.
use binding_of and
value_at *)
| Id(x) ->
let l = binding_of env x in
let i = value_at store l in i;;
以下是我使用的一些功能和类型的代码:
module IntMap = Map.Make(struct type t = int let compare = compare end);;
type store = int IntMap.t;;
let value_at store loc = IntMap.find loc store;;
let insert_value store loc v = IntMap.add loc v store;;
let enter_scope env scope = scope::env;;
let exit_scope env = List.tl env;;
let current_scope env = List.hd env;;
type expr = (* Expressions in C-flat *)
(* int operations *)
Add of expr * expr
| Sub of expr * expr
| Mul of expr * expr
| Div of expr * expr
| Neg of expr
(* constants/variables *)
| Id of string
| IntConst of int
(* array/pointer operations *)
| At of expr * expr
| Deref of expr
| AddressOf of expr
(* function *)
| Call of string * expr list
(* pre- & post-increment *)
| Pre of expr
| Post of expr
;;
let binding_of env name =
let lookup name symTab = List.assoc name symTab in
try
lookup name (List.hd env)
with Not_found ->
lookup name (List.hd (List.tl env))
;;
答案 0 :(得分:0)
你没有展示足够的定义,看看你提到的线路上发生了什么。
我的猜测是binding_of_env
没有返回位置,但value_at
需要一个位置。可能您需要从绑定中提取位置。
答案 1 :(得分:0)
如果您需要CSE 307项目的帮助,请到办公时间。
你的TA; - )