3 Description: Faust expression evaluation
5 Created: 03/06/2013 Modified: 04/08/2013
13 exception NotYetDone;;
14 exception Dimension_error of string;;
16 class dimension : int * int -> dimension_type =
17 fun (init : int * int) ->
19 val dim_input = fst init
20 val dim_output = snd init
22 method input = dim_input
23 method output = dim_output
25 method par : dimension_type -> dimension_type =
28 ((self#input + dim#input), (self#output + dim#output))
30 method seq : dimension_type -> dimension_type =
32 if self#output = dim#input then
33 new dimension (self#input, dim#output)
34 else raise (Dimension_error "seq dimension not matched.")
36 method split : dimension_type -> dimension_type =
38 if dim#input mod self#output = 0 then
39 new dimension (self#input, dim#output)
40 else raise (Dimension_error "split dimension not matched.")
42 method merge : dimension_type -> dimension_type =
44 if self#output mod dim#input = 0 then
45 new dimension (self#input, dim#output)
46 else raise (Dimension_error "merge dimension not matched.")
48 method _rec : dimension_type -> dimension_type =
50 if self#output >= dim#input && self#input >= dim#output then
51 new dimension (self#input - dim#output, self#output)
52 else raise (Dimension_error "rec dimension not matched.")
55 class process : faust_exp -> process_type =
56 fun (exp_init : faust_exp) ->
65 | Split (e1, e2) -> e1
66 | Merge (e1, e2) -> e1
75 | Split (e1, e2) -> e2
76 | Merge (e1, e2) -> e2
81 val dim = new dimension
85 method get_delay = delay
86 method to_string = "NotYetDone"
87 method virtual evaluate : beam_type -> beam_type
91 class proc_const : faust_exp -> process_type =
92 fun (exp_init : faust_exp) ->
96 method evaluate = fun b1 ->
145 (* PROCESS DELAY ESTIMATION *)
147 (** val delay : faust_exp -> int, returns the number of delays estimated staticly.
148 Attention: delays of "@" is estimated as 10 constant,
149 delays of "vectorize" and "serialize" haven't been implemented,
150 delays of "rdtable" hasn't been implemented.*)
151 let rec delay exp_faust = match exp_faust with
179 |Par (e1, e2) -> max (delay e1) (delay e2)
180 |Seq (e1, e2) -> (delay e1) + (delay e2)
181 |Split (e1, e2) -> (delay e1) + (delay e2)
182 |Merge (e1, e2) -> (delay e1) + (delay e2)
183 |Rec (e1, e2) -> delay e1;;
188 (** val exp_of_string : string -> faust_exp, faust expression parser. *)
189 let exp_of_string s = (Parser.main Lexer.token (Lexing.from_string s));;
193 (* PROCESS DIMENSION ESTIMATION *)
194 (* process dimension := (size of input beam, size of output beam).*)
197 (** val get_root : dimension -> int * int, returns the root of dimension tree. *)
198 let get_root = fun d_tree -> match d_tree with
200 | Tree (d, branches) -> d;;
203 (** val subtree : dimention -> int -> dimension, returns a subtree of dimension tree.*)
204 let subtree = fun d_tree -> fun i ->
206 | End d -> raise (Beam_Matching_Error "Subtree left absent.")
207 | Tree (d, branches) -> (
209 (left, right) -> if i = 0 then left else right);;
211 (** val subtree_left : dimension -> dimension, returns the left subtree of dimension tree.*)
212 let subtree_left = fun d_tree -> subtree d_tree 0;;
215 (** val subtree_right : dimension -> dimension, returns the right subtree of dimension tree.*)
216 let subtree_right = fun d_tree -> subtree d_tree 1;;
218 (** val dim : faust_exp -> int * int, returns dimension for faust expression,
219 along with beam matching.*)
220 let rec dim exp_faust =
222 (** val dimension_constructor : ((int * int) -> (int * int) -> (int * int)) -> faust_exp
223 -> faust_exp -> dimension,
224 returns the dimension tree of constructor(e1, e2).*)
225 let dimension_constructor = fun constructor -> fun e1 -> fun e2 ->
226 let subtree1 = dim e1 in
227 let subtree2 = dim e2 in
228 let root = constructor (get_root subtree1) (get_root subtree2) in
229 Tree (root, (subtree1, subtree2)) in
232 |Const v -> End (0, 1)
247 |Rdtable -> End (3, 1)
249 |Vectorize -> End (2, 1)
250 |Concat -> End (2, 1)
252 |Serialize -> End (1, 1)
253 |Larger -> End (2, 1)
254 |Smaller -> End (2, 1)
255 |Prefix -> End (2, 1)
256 |Selecttwo -> End (3, 1)
257 |Selectthree -> End (4, 1)
260 |Par (e1, e2) -> dimension_constructor d_par e1 e2
261 |Seq (e1, e2) -> dimension_constructor d_seq e1 e2
262 |Split (e1, e2) -> dimension_constructor d_split e1 e2
263 |Merge (e1, e2) -> dimension_constructor d_merge e1 e2
264 |Rec (e1, e2) -> dimension_constructor d_rec e1 e2;;
268 (* AUXILIARY 'CONVERT_TO_STRING' FUNCTIONS *)
270 (** val print_exp : faust_exp -> unit, print to console the input faust expression.*)
272 let rec string_of_exp exp = match exp with
273 |Const v -> "Const" ^ " (" ^ (string_of_value v) ^ ")"
274 |Ident s -> "Ident" ^ " \"" ^ "s" ^ "\""
275 |Par (e1, e2) -> "Par" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
276 |Seq (e1, e2) -> "Seq" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
277 |Split (e1, e2) -> "Split" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
278 |Merge (e1, e2) -> "Merge" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
279 |Rec (e1, e2) -> "Rec" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
281 print_string("Parer : Types.faust_exp = "^ (string_of_exp exp));;