open Imptypes (* The 'store' models the machine's memory as a mapping from * variable names to integers. *) type store = varname -> int let init_store (l : (varname*int) list) : store = (* YOUR CODE GOES HERE * Insert code here that takes a list of (variable,integer) * pairs and returns a store that maps each variable to * each corresponding integer. If your store function is * applied to a variable not in the list, it may return any * integer or it may raise an exception. *) (fun _ -> 0);; let rec eval_arith (s:store) (a:iarith) : int = (* YOUR CODE GOES HERE * Insert code here that evaluates iarith expression 'a' * evaluated in memory state 's' and returns an integer. * The type 'iarith' is defined in imptypes.ml. *) 0;; let rec eval_bool (s:store) (b:ibool) : bool = (* YOUR CODE GOES HERE * Insert code here that evaluates ibool expression 'b' * evaluated in memory state 's' and returns a bool. * The type 'ibool' is defined in imptypes.ml. *) false;; let rec exec_cmd (s:store) (c:icmd) : store = (* YOUR CODE GOES HERE * Insert code here that executes icmd 'c' evaluated * in memory state 's' and returns the new memory state * s that results. Type 'icmd' is defined in imptypes.ml. *) s;; (* This is the main entrypoint of the code. You don't need to understand * how it works to complete the assignment, but here's a short explanation * for those interested: * * The first 'let' statement reads the .imp file and invokes an external * parser (defined in impparser.mly) to convert the file into an icmd * structure. * * The next 'let' statement calls your init_store code to create a * store s from the rest of the command-line arguments. The first * command-line argument gets assigned to variable "arg0", the next to * "arg1", etc. * * The third 'let' statement calls your exec_cmd code to execute the icmd * starting in the initial store. * * If the IMP program terminates (and your code is correct) then s2 will * eventually hold the final store that results from executing the * program. We then print out the value of variable "ret" as the result * of the computation. *) let main () = let c = (Impparser.parse_cmd Implexer.token (Lexing.from_channel (open_in Sys.argv.(1)))) in let s = init_store (List.tl (List.tl (Array.to_list (Array.mapi (fun i a -> ("arg"^(string_of_int (i-2)), if i>=2 then (int_of_string a) else 0)) Sys.argv)))) in let s2 = exec_cmd s c in print_int (s2 "ret");; main ();;