| status | id | title | pages | estimate | actual | start | finish |
|--------|------------|------------------------------------------|-------|----------|--------|------------|------------|
-| [ ] | 0 | Modern Compiler Implementation in ML | 512 | 28-84 | -- | xxxx-xx-xx | xxxx-xx-xx |
+| [-] | 0 | Modern Compiler Implementation in ML | 512 | 28-84 | -- | 2018-04-16 | xxxx-xx-xx |
| ====== | ========== | ======================================== | ===== | ======== | ====== | ========== | ========== |
-| [ ] | 1 | - Fundamentals of Compilation | 264 | 14 | -- | ---------- | ---------- |
-| [ ] | 1.01 | -- Introduction | 011 | 01 | -- | ---------- | ---------- |
-| [ ] | 1.01.1 | --- Modules and interfaces | 001 | -- | -- | ---------- | ---------- |
-| [ ] | 1.01.2 | --- Tools and Software | 002 | -- | -- | ---------- | ---------- |
-| [ ] | 1.01.3 | --- Data structures for tree languages | 003 | -- | -- | ---------- | ---------- |
-| [ ] | 1.01.p | --- Program | 002 | -- | -- | ---------- | ---------- |
-| [ ] | 1.01.p.1 | ---- interpreter: maxargs | --- | -- | -- | ---------- | ---------- |
+| [-] | 1 | - Fundamentals of Compilation | 264 | 14 | -- | 2018-04-16 | ---------- |
+| [-] | 1.01 | -- Introduction | 011 | 01 | -- | 2018-04-16 | ---------- |
+| [x] | 1.01.1 | --- Modules and interfaces | 001 | -- | -- | 2018-04-16 | ---------- |
+| [x] | 1.01.2 | --- Tools and Software | 002 | -- | -- | 2018-04-16 | ---------- |
+| [x] | 1.01.3 | --- Data structures for tree languages | 003 | -- | -- | 2018-04-16 | ---------- |
+| [-] | 1.01.p | --- Program | 002 | -- | -- | 2018-04-16 | ---------- |
+| [x] | 1.01.p.1 | ---- interpreter: maxargs | --- | -- | -- | 2018-04-17 | 2018-04-17 |
| [ ] | 1.01.p.2 | ---- interpreter: interp | --- | -- | -- | ---------- | ---------- |
| [ ] | 1.01.e | --- Exercises | 002 | -- | -- | ---------- | ---------- |
| [ ] | 1.01.e.1.a | ---- tree member | --- | -- | -- | ---------- | ---------- |
+module List = ListLabels
+
+module Spl : sig
+ type id = string
+
+ type binop =
+ | Plus
+ | Minus
+ | Times
+ | Div
+
+ type stm =
+ | CompoundStm of stm * stm
+ | AssignStm of id * exp
+ | PrintStm of exp list
+ and exp =
+ | IdExp of id
+ | NumExp of int
+ | OpExp of exp * binop * exp
+ | EseqExp of stm * exp
+
+ val maxargs : stm -> int
+end = struct
+ type id = string
+
+ type binop =
+ | Plus
+ | Minus
+ | Times
+ | Div
+
+ type stm =
+ | CompoundStm of stm * stm
+ | AssignStm of id * exp
+ | PrintStm of exp list
+ and exp =
+ | IdExp of id
+ | NumExp of int
+ | OpExp of exp * binop * exp
+ | EseqExp of stm * exp
+
+ (* 01.p.1: Write ML function (maxargs : stm -> int) that tells the
+ * maximum number of arguments of any print statement within any
+ * subexpression of a given statement. For example, maxargs(prog)
+ * is 2.
+ *)
+ let maxargs stm =
+ let max = ref 0 in
+ let rec check_stm = function
+ | PrintStm exps ->
+ let exps_length = List.length exps in
+ if exps_length > !max then max := exps_length else ();
+ List.iter exps ~f:check_exp
+ | AssignStm (_, e) ->
+ check_exp e
+ | CompoundStm (s1, s2) ->
+ check_stm s1;
+ check_stm s2
+ and check_exp = function
+ | IdExp _ | NumExp _ -> ()
+ | OpExp (e1, _, e2) ->
+ check_exp e1;
+ check_exp e2
+ | EseqExp (s, e) ->
+ check_stm s;
+ check_exp e
+ in
+ check_stm stm;
+ !max
+end
+
+let spl_prog =
+ (* a := 5 + 3;
+ * b := (print(a, a - 1), 10 * a);
+ * print(b)
+ *)
+ Spl.CompoundStm
+ ( Spl.AssignStm ("a", Spl.OpExp (Spl.NumExp 5, Spl.Plus, Spl.NumExp 3))
+ , Spl.CompoundStm
+ ( Spl.AssignStm
+ ( "b"
+ , Spl.EseqExp
+ ( Spl.PrintStm
+ [ Spl.IdExp "a"
+ ; Spl.OpExp (Spl.IdExp "a", Spl.Minus, Spl.NumExp 1)
+ ]
+ , Spl.OpExp (Spl.NumExp 10, Spl.Times, Spl.IdExp "a")
+ )
+ )
+ , Spl.PrintStm [Spl.IdExp "b"]
+ )
+ )
+
+let () =
+ Printf.printf "maxargs: %d\n" (Spl.maxargs spl_prog)