(**
 	Module: Faustexp	
	Description: dimension estimation and delay estimation of faust expressions.
	@author WANG Haisheng	
	Created: 03/06/2013	Modified: 04/06/2013
*)

open Types;;
open Value;;

(* EXCEPTIONS *)

(** Exception raised in beam matching of faust expressions.*)
exception Beam_Matching_Error of string;;

(** Exception raised in case that the branch under call hasn't yet been programed.*)
exception NotYetDone;;


(* PROCESS DELAY ESTIMATION *)

(** val delay : faust_exp -> int, returns the number of delays estimated staticly.
Attention: delays of "@" is estimated as 10 constant, 
delays of "vectorize" and "serialize" haven't been implemented, 
delays of "rdtable" hasn't been implemented.*)
let rec delay exp_faust = match exp_faust with
	|Const v	->	0
	|Ident s	->
		(
			match s with
			|Add			->	0
			|Sup			->	0		
			|Mul			->	0
			|Div			->	0
			|Pass			->	0
			|Stop			->	0
			|Mem			->	1
			|Delay			->	100000 (* danger! *)
			|Floor		        ->	0
			|Int			->	0
			|Sin			->	0
			|Cos			->	0
			|Atan			->	0
			|Atantwo                ->      0
			|Sqrt			->	0
			|Rdtable		->	100000 (* danger! *)
			|Mod			->	0
			|Larger			->	0
			|Smaller		->	0
			|Vectorize		->	100 (* danger! *)
			|Concat			->	0
			|Nth			->	0
			|Serialize		->	0
			|Prefix	 	        ->	1
			|Selecttwo		->	0
			|Selectthree		->	0
		)
	|Par (e1, e2)	->	max (delay e1) (delay e2)
	|Seq (e1, e2)	->	(delay e1) + (delay e2)
	|Split (e1, e2)	->	(delay e1) + (delay e2)
	|Merge (e1, e2)	->	(delay e1) + (delay e2)
	|Rec (e1, e2)	->	delay e1;;	


(* PARSER *)

(** val exp_of_string : string -> faust_exp, faust expression parser. *)
let exp_of_string s = (Parser.main Lexer.token (Lexing.from_string s));;



(* PROCESS DIMENSION ESTIMATION *)
(* process dimension := (size of input beam, size of output beam).*)


(** val get_root : dimension -> int * int, returns the root of dimension tree. *)
let get_root = fun d_tree -> match d_tree with
                        | End d -> d
			| Tree (d, branches) -> d;;


(** val subtree : dimention -> int -> dimension, returns a subtree of dimension tree.*)
let subtree = fun d_tree -> fun i ->
  match d_tree with
  | End d -> raise (Beam_Matching_Error "Subtree left absent.")
  | Tree (d, branches) -> (
      match branches with 
	(left, right) -> if i = 0 then left else right);;

(** val subtree_left : dimension -> dimension, returns the left subtree of dimension tree.*)
let subtree_left = fun d_tree -> subtree d_tree 0;;


(** val subtree_right : dimension -> dimension, returns the right subtree of dimension tree.*)
let subtree_right = fun d_tree -> subtree d_tree 1;;


(** val d_par : int * int -> int * int -> int * int, process dimension for constructor "par(,)", 
which is the addition of two dimensions.*)
let d_par a b = (((fst a) + (fst b)), ((snd a) + (snd b)));;


(** val d_seq : int * int -> int * int -> int * int, process dimension for constructor "seq(:)", 
which is (size of input beam of first exp, size of output beam of second exp) 
along with beam matching.*)
let d_seq a b = if (snd a) = (fst b) then (fst a, snd b) else raise (Beam_Matching_Error "seq");;


(** val d_split : int * int -> int * int -> int * int, process dimension for constructor "split(<:)", 
which is (size of input beam of first exp, size of output beam of second exp) 
along with beam matching.*)
let d_split a b = 
  if ((fst b) mod (snd a)) = 0 then 
    (fst a, snd b) 
  else raise (Beam_Matching_Error "split");;


(** val d_merge : int * int -> int * int -> int * int, process dimension for constructor "merge(:>)", 
which is (size of input beam of first exp, size of output beam of second exp) 
along with beam matching. *)
let d_merge a b = 
  if ((snd a) mod (fst b)) = 0 then 
    (fst a, snd b) 
  else raise (Beam_Matching_Error "merge");;


(** val d_rec : int * int -> int * int -> int * int, process dimension for constructor "rec(~)", 
which is (size of input beam of first exp - size of output beam of second exp, 
size of output beam of first exp) 
along with beam matching.*)
let d_rec a b = 
  if (fst a) >= (snd b) && (snd a) >= (fst b) then 
    ((fst a) - (snd b), snd a) 
  else raise (Beam_Matching_Error "rec");;


(** val dim : faust_exp -> int * int, returns dimension for faust expression, 
along with beam matching.*)
let rec dim exp_faust = 

(** val dimension_constructor : ((int * int) -> (int * int) -> (int * int)) -> faust_exp 
-> faust_exp -> dimension,
returns the dimension tree of constructor(e1, e2).*)
        let dimension_constructor = fun constructor -> fun e1 -> fun e2 ->
	    let subtree1 = dim e1 in
	    let subtree2 = dim e2 in
	    let root = constructor (get_root subtree1) (get_root subtree2) in
	    Tree (root, (subtree1, subtree2)) in

        match exp_faust with
	|Const v -> End (0, 1)
	|Ident s -> 
		(
			match s with
			|Add			->	End (2, 1)
			|Sup			->	End (2, 1)		
			|Mul			->	End (2, 1)
			|Div			->	End (2, 1)
			|Pass			->	End (1, 1)
			|Stop			->	End (1, 0)
			|Mem			->	End (1, 1)
			|Delay			->	End (2, 1)
			|Floor	    	        ->      End (1, 1)
			|Int			->	End (1, 1)
			|Sin			->	End (1, 1)
			|Cos			->	End (1, 1)
			|Atan			->	End (1, 1)
			|Atantwo                ->      End (2, 1)
			|Sqrt			->	End (1, 1)
			|Rdtable		->	End (3, 1)
			|Mod  		        ->	End (2, 1)
			|Vectorize		->	End (2, 1)
			|Concat			->	End (2, 1)
			|Nth			->	End (2, 1)
			|Serialize		->	End (1, 1)
			|Larger			->	End (2, 1)
			|Smaller		->	End (2, 1)
			|Prefix		        ->	End (2, 1)
			|Selecttwo		->	End (3, 1)
			|Selectthree		->	End (4, 1)
		)

	|Par (e1, e2)           ->      dimension_constructor d_par e1 e2
	|Seq (e1, e2)		->	dimension_constructor d_seq e1 e2
	|Split (e1, e2)		->	dimension_constructor d_split e1 e2
	|Merge (e1, e2)		->	dimension_constructor d_merge e1 e2
	|Rec (e1, e2)		->	dimension_constructor d_rec e1 e2;;



(* AUXILIARY 'CONVERT_TO_STRING' FUNCTIONS *)

(** val print_exp : faust_exp -> unit, print to console the input faust expression.*)
let print_exp exp = 
  let rec string_of_exp exp = match exp with
    |Const v		->	"Const" ^ " (" ^ (string_of_value v) ^ ")"
    |Ident s		->	"Ident" ^ " \"" ^ "s" ^ "\""
    |Par (e1, e2)	->	"Par" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
    |Seq (e1, e2)	->	"Seq" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
    |Split (e1, e2)	->	"Split" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
    |Merge (e1, e2)	->	"Merge" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
    |Rec (e1, e2)	->	"Rec" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
  in
    print_string("Parer : Types.faust_exp = "^ (string_of_exp exp));;