1 module List = ListLabels
13 | CompoundStm of stm * stm
14 | AssignStm of id * exp
15 | PrintStm of exp list
19 | OpExp of exp * binop * exp
20 | EseqExp of stm * exp
22 val maxargs : stm -> int option
23 (** Option because a program may not have any print statements at all. *)
34 | CompoundStm of stm * stm
35 | AssignStm of id * exp
36 | PrintStm of exp list
40 | OpExp of exp * binop * exp
41 | EseqExp of stm * exp
43 (* 01.p.1: Write ML function (maxargs : stm -> int) that tells the
44 * maximum number of arguments of any print statement within any
45 * subexpression of a given statement. For example, maxargs(prog)
49 let opt_max_update opt n =
52 | Some m -> Some (max m n)
54 let opt_max_merge a b =
59 | Some _, Some n -> opt_max_update a n
61 let rec check_stm max_opt stm =
65 ~init:(opt_max_update max_opt (List.length exps))
69 | CompoundStm (s1, s2) ->
70 opt_max_merge (check_stm max_opt s1) (check_stm max_opt s2)
71 and check_exp max_opt exp =
73 | IdExp _ | NumExp _ -> max_opt
74 | OpExp (e1, _, e2) ->
75 opt_max_merge (check_exp max_opt e1) (check_exp max_opt e2)
77 opt_max_merge (check_stm max_opt s) (check_exp max_opt e)
84 * b := (print(a, a - 1), 10 * a);
88 ( Spl.AssignStm ("a", Spl.OpExp (Spl.NumExp 5, Spl.Plus, Spl.NumExp 3))
95 ; Spl.OpExp (Spl.IdExp "a", Spl.Minus, Spl.NumExp 1)
97 , Spl.OpExp (Spl.NumExp 10, Spl.Times, Spl.IdExp "a")
100 , Spl.PrintStm [Spl.IdExp "b"]
104 let spl_prog_noprint =
110 ("a", Spl.OpExp (Spl.NumExp 5, Spl.Plus, Spl.NumExp 3))
112 ("b", Spl.OpExp (Spl.NumExp 10, Spl.Times, Spl.IdExp "a"))
116 let string_of_maxargs int_opt =
118 | Some n -> string_of_int n
121 Printf.printf "maxargs : spl_prog_orig -> %s\n"
122 (string_of_maxargs (Spl.maxargs spl_prog_orig));
123 Printf.printf "maxargs : spl_prog_noprint -> %s\n"
124 (string_of_maxargs (Spl.maxargs spl_prog_noprint))