海野がはじめて書いた SML プログラムです。信用しないでください(しないか)。

fun maxargs(stm) =
    let
	fun max(a, b) =
	    if a > b then a else b

	fun maxargs_loop_stm(m, CompoundStm(left, right)) =
	    max(maxargs_loop_stm(m, left), maxargs_loop_stm(m, right))

	  | maxargs_loop_stm(m, AssignStm(id, exp)) = maxargs_loop_exp(m, exp)

	  | maxargs_loop_stm(m, PrintStm(l)) = 
	    max(length l, maxargs_loop_explist(m, l))

	and maxargs_loop_exp(m, IdExp(id)) = m

	  | maxargs_loop_exp(m, NumExp(i)) = m

	  | maxargs_loop_exp(m, OpExp(lhs, bop, rhs)) =
	    max(maxargs_loop_exp(m, lhs), maxargs_loop_exp(m, rhs))

	  | maxargs_loop_exp(m, EseqExp(s, e)) =
	    max(maxargs_loop_stm(m, s), maxargs_loop_exp(m, e))

	and maxargs_loop_explist(m, l) = 
	    if length l > 1 then
		max(maxargs_loop_exp(m, hd(l)), 
		    maxargs_loop_explist(m, List.drop(l, 1)))
	    else
		maxargs_loop_exp(m, hd(l))
    in
	maxargs_loop_stm(0, stm)
    end