3 Description: Faust expression evaluation
5 Created: 03/06/2013 Modified: 04/08/2013
14 exception NotYetDone;;
15 exception Dimension_error of string;;
16 exception Process_error of string;;
18 class dimension : int * int -> dimension_type =
19 fun (init : int * int) ->
21 val dim_input = fst init
22 val dim_output = snd init
24 method input = dim_input
25 method output = dim_output
27 method par : dimension_type -> dimension_type =
30 ((self#input + dim#input), (self#output + dim#output))
32 method seq : dimension_type -> dimension_type =
34 if self#output = dim#input then
35 new dimension (self#input, dim#output)
36 else raise (Dimension_error "seq dimension not matched.")
38 method split : dimension_type -> dimension_type =
40 if dim#input mod self#output = 0 then
41 new dimension (self#input, dim#output)
42 else raise (Dimension_error "split dimension not matched.")
44 method merge : dimension_type -> dimension_type =
46 if self#output mod dim#input = 0 then
47 new dimension (self#input, dim#output)
48 else raise (Dimension_error "merge dimension not matched.")
50 method _rec : dimension_type -> dimension_type =
52 if self#output >= dim#input && self#input >= dim#output then
53 new dimension (self#input - dim#output, self#output)
54 else raise (Dimension_error "rec dimension not matched.")
57 class process : faust_exp -> process_type =
58 fun (exp_init : faust_exp) ->
67 | Split (e1, e2) -> e1
68 | Merge (e1, e2) -> e1
77 | Split (e1, e2) -> e2
78 | Merge (e1, e2) -> e2
83 val dim = new dimension
87 method get_delay = delay
88 method to_string = "NotYetDone"
89 method virtual evaluate : beam_type -> beam_type
93 class proc_const : faust_exp -> process_type =
94 fun (exp_init : faust_exp) ->
97 val _dim = new dimension (0,1)
102 | _ -> raise (Process_error "const process constructor.")
106 method delay = _delay
107 method const = _const
109 method eval : beam_type -> beam_type =
110 fun (input : beam_type) ->
112 new beam [| new signal 0 (fun t -> new value self#const)|]
114 raise (Process_error "proc_const accepts no input.")
118 class exp_ident : faust_exp -> process_type =
119 fun (exp_init : faust_exp) ->
125 | _ -> raise (Process_error "ident process constructor.")
126 val _dim = dimension_of_symbol _symbol
132 method delay = _delay
133 method const = _const
135 method eval : beam_type -> beam_type =
136 fun (input : beam_type) ->
138 new beam [| new signal 0 (fun t -> new value self#const)|]
140 raise (Process_error "proc_const accepts no input.")
181 (* PROCESS DELAY ESTIMATION *)
183 (** val delay : faust_exp -> int, returns the number of delays estimated staticly.
184 Attention: delays of "@" is estimated as 10 constant,
185 delays of "vectorize" and "serialize" haven't been implemented,
186 delays of "rdtable" hasn't been implemented.*)
187 let rec delay exp_faust = match exp_faust with
215 |Par (e1, e2) -> max (delay e1) (delay e2)
216 |Seq (e1, e2) -> (delay e1) + (delay e2)
217 |Split (e1, e2) -> (delay e1) + (delay e2)
218 |Merge (e1, e2) -> (delay e1) + (delay e2)
219 |Rec (e1, e2) -> delay e1;;
224 (** val exp_of_string : string -> faust_exp, faust expression parser. *)
225 let exp_of_string s = (Parser.main Lexer.token (Lexing.from_string s));;
229 (* PROCESS DIMENSION ESTIMATION *)
230 (* process dimension := (size of input beam, size of output beam).*)
233 (** val get_root : dimension -> int * int, returns the root of dimension tree. *)
234 let get_root = fun d_tree -> match d_tree with
236 | Tree (d, branches) -> d;;
239 (** val subtree : dimention -> int -> dimension, returns a subtree of dimension tree.*)
240 let subtree = fun d_tree -> fun i ->
242 | End d -> raise (Beam_Matching_Error "Subtree left absent.")
243 | Tree (d, branches) -> (
245 (left, right) -> if i = 0 then left else right);;
247 (** val subtree_left : dimension -> dimension, returns the left subtree of dimension tree.*)
248 let subtree_left = fun d_tree -> subtree d_tree 0;;
251 (** val subtree_right : dimension -> dimension, returns the right subtree of dimension tree.*)
252 let subtree_right = fun d_tree -> subtree d_tree 1;;
254 (** val dim : faust_exp -> int * int, returns dimension for faust expression,
255 along with beam matching.*)
256 let rec dim exp_faust =
258 (** val dimension_constructor : ((int * int) -> (int * int) -> (int * int)) -> faust_exp
259 -> faust_exp -> dimension,
260 returns the dimension tree of constructor(e1, e2).*)
261 let dimension_constructor = fun constructor -> fun e1 -> fun e2 ->
262 let subtree1 = dim e1 in
263 let subtree2 = dim e2 in
264 let root = constructor (get_root subtree1) (get_root subtree2) in
265 Tree (root, (subtree1, subtree2)) in
268 |Const v -> End (0, 1)
283 |Rdtable -> End (3, 1)
285 |Vectorize -> End (2, 1)
286 |Concat -> End (2, 1)
288 |Serialize -> End (1, 1)
289 |Larger -> End (2, 1)
290 |Smaller -> End (2, 1)
291 |Prefix -> End (2, 1)
292 |Selecttwo -> End (3, 1)
293 |Selectthree -> End (4, 1)
296 |Par (e1, e2) -> dimension_constructor d_par e1 e2
297 |Seq (e1, e2) -> dimension_constructor d_seq e1 e2
298 |Split (e1, e2) -> dimension_constructor d_split e1 e2
299 |Merge (e1, e2) -> dimension_constructor d_merge e1 e2
300 |Rec (e1, e2) -> dimension_constructor d_rec e1 e2;;
304 (* AUXILIARY 'CONVERT_TO_STRING' FUNCTIONS *)
306 (** val print_exp : faust_exp -> unit, print to console the input faust expression.*)
308 let rec string_of_exp exp = match exp with
309 |Const v -> "Const" ^ " (" ^ (string_of_value v) ^ ")"
310 |Ident s -> "Ident" ^ " \"" ^ "s" ^ "\""
311 |Par (e1, e2) -> "Par" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
312 |Seq (e1, e2) -> "Seq" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
313 |Split (e1, e2) -> "Split" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
314 |Merge (e1, e2) -> "Merge" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
315 |Rec (e1, e2) -> "Rec" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
317 print_string("Parer : Types.faust_exp = "^ (string_of_exp exp));;