2 <link rel=
"stylesheet" href=
"style.css" type=
"text/css">
3 <meta content=
"text/html; charset=iso-8859-1" http-equiv=
"Content-Type">
4 <link rel=
"Start" href=
"index.html">
5 <link title=
"Index of types" rel=Appendix
href=
"index_types.html">
6 <link title=
"Index of exceptions" rel=Appendix
href=
"index_exceptions.html">
7 <link title=
"Index of values" rel=Appendix
href=
"index_values.html">
8 <link title=
"Index of modules" rel=Appendix
href=
"index_modules.html">
9 <link title=
"Types" rel=
"Chapter" href=
"Types.html">
10 <link title=
"Parser" rel=
"Chapter" href=
"Parser.html">
11 <link title=
"Lexer" rel=
"Chapter" href=
"Lexer.html">
12 <link title=
"Value" rel=
"Chapter" href=
"Value.html">
13 <link title=
"Signal" rel=
"Chapter" href=
"Signal.html">
14 <link title=
"Faustexp" rel=
"Chapter" href=
"Faustexp.html">
15 <link title=
"Interpreter" rel=
"Chapter" href=
"Interpreter.html">
16 <link title=
"Preprocess" rel=
"Chapter" href=
"Preprocess.html">
17 <link title=
"Main" rel=
"Chapter" href=
"Main.html"><title>Interpreter.eval
</title>
20 <code class=
"code"><span class=
"keyword">let
</span> <span class=
"keyword">rec
</span> eval
exp_faust
dimension_tree
input_beam
=
<br>
23 </code><table><tr><td></td><td><span class=
"comment">(** val interpret_par : faust_exp -
> faust_exp -
> beam -
> beam,
24 interprets par(e1, e2) with input beam, produces output beam.*)
</span></td></tr></table><code class=
"code"><br>
25 <span class=
"keyword">let
</span> interpret_par
=
<span class=
"keyword">fun
</span> e1
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> e2
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> dimension_tree
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> input_beam
<span class=
"keywordsign">-
></span><br>
27 <span class=
"comment">(*
dimension
information
*)
</span><br>
28 <span class=
"keyword">let
</span> n
=
<span class=
"constructor">List
</span>.length
input_beam
<span class=
"keyword">in
</span><br>
29 <span class=
"keyword">let
</span> subtree1
=
subtree_left
dimension_tree
<span class=
"keyword">in
</span><br>
30 <span class=
"keyword">let
</span> subtree2
=
subtree_right
dimension_tree
<span class=
"keyword">in
</span><br>
31 <span class=
"keyword">let
</span> d1
=
get_root
subtree1
<span class=
"keyword">in
</span><br>
32 <span class=
"keyword">let
</span> d2
=
get_root
subtree2
<span class=
"keyword">in
</span><br>
34 <span class=
"keyword">if
</span> n
=
(fst
d1)
+
(fst
d2)
<span class=
"keyword">then
</span> <br>
35 (
<br>
36 <span class=
"comment">(*
segmentation
of
input
beam
*)
</span><br>
37 <span class=
"keyword">let
</span> input_beam1
=
sublist
input_beam
0 (fst
d1)
<span class=
"keyword">in
</span><br>
38 <span class=
"keyword">let
</span> input_beam2
=
sublist
input_beam
(fst
d1)
(fst
d2)
<span class=
"keyword">in
</span><br>
40 <span class=
"comment">(*
evaluate
two
expressions
respectively
*)
</span><br>
41 <span class=
"keyword">let
</span> output_beam1
=
eval
e1
subtree1
input_beam1
<span class=
"keyword">in
</span><br>
42 <span class=
"keyword">let
</span> output_beam2
=
eval
e2
subtree2
input_beam2
<span class=
"keyword">in
</span><br>
44 <span class=
"comment">(*
concat
two
output
beams
*)
</span><br>
45 <span class=
"keyword">if
</span> <span class=
"constructor">List
</span>.length
output_beam1
=
snd
d1
<span class=
"keywordsign">&&</span> <span class=
"constructor">List
</span>.length
output_beam2
=
snd
d2
<br>
46 <span class=
"keyword">then
</span> (output_beam1
@
output_beam2)
<br>
47 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Par"</span>)
<br>
48 )
<br>
49 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Par"</span>)
<span class=
"keyword">in
</span><br>
52 </code><table><tr><td></td><td><span class=
"comment">(** val interpret_seq : faust_exp -
> faust_exp -
> beam -
> beam,
53 interprets seq(e1, e2) with input beam, produces output beam.*)
</span></td></tr></table><code class=
"code"><br>
54 <span class=
"keyword">let
</span> interpret_seq
=
<span class=
"keyword">fun
</span> e1
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> e2
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> dimension_tree
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> input_beam
<span class=
"keywordsign">-
></span><br>
56 <span class=
"comment">(*
dimension
information
*)
</span><br>
57 <span class=
"keyword">let
</span> n
=
<span class=
"constructor">List
</span>.length
input_beam
<span class=
"keyword">in
</span><br>
58 <span class=
"keyword">let
</span> subtree1
=
subtree_left
dimension_tree
<span class=
"keyword">in
</span><br>
59 <span class=
"keyword">let
</span> subtree2
=
subtree_right
dimension_tree
<span class=
"keyword">in
</span><br>
60 <span class=
"keyword">let
</span> d1
=
get_root
subtree1
<span class=
"keyword">in
</span><br>
61 <span class=
"keyword">let
</span> d2
=
get_root
subtree2
<span class=
"keyword">in
</span><br>
64 <span class=
"keyword">if
</span> n
=
fst
d1
<span class=
"keyword">then
</span><br>
65 (
<br>
66 <span class=
"comment">(*
evaluate
the
first
expression
*)
</span><br>
67 <span class=
"keyword">let
</span> output_beam1
=
eval
e1
subtree1
input_beam
<span class=
"keyword">in
</span><br>
69 <span class=
"comment">(*
evaluate
the
second
expression
*)
</span><br>
70 <span class=
"keyword">if
</span> <span class=
"constructor">List
</span>.length
output_beam1
=
fst
d2
<br>
71 <span class=
"keyword">then
</span> eval
e2
subtree2
output_beam1
<br>
72 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Seq"</span>)
<br>
73 )
<br>
74 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Seq"</span>)
<span class=
"keyword">in
</span><br>
77 </code><table><tr><td></td><td><span class=
"comment">(** val interpret_split : faust_exp -
> faust_exp -
> beam -
> beam,
78 interprets split(e1, e2) with input beam, produces output beam.*)
</span></td></tr></table><code class=
"code"><br>
79 <span class=
"keyword">let
</span> interpret_split
=
<span class=
"keyword">fun
</span> e1
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> e2
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> dimension_tree
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> input_beam
<span class=
"keywordsign">-
></span><br>
81 <span class=
"comment">(*
dimension
information
*)
</span><br>
82 <span class=
"keyword">let
</span> n
=
<span class=
"constructor">List
</span>.length
input_beam
<span class=
"keyword">in
</span><br>
83 <span class=
"keyword">let
</span> subtree1
=
subtree_left
dimension_tree
<span class=
"keyword">in
</span><br>
84 <span class=
"keyword">let
</span> subtree2
=
subtree_right
dimension_tree
<span class=
"keyword">in
</span><br>
85 <span class=
"keyword">let
</span> d1
=
get_root
subtree1
<span class=
"keyword">in
</span><br>
86 <span class=
"keyword">let
</span> d2
=
get_root
subtree2
<span class=
"keyword">in
</span><br>
89 <span class=
"keyword">if
</span> n
=
fst
d1
<span class=
"keyword">then
</span><br>
90 (
<br>
91 <span class=
"comment">(*
evaluate
the
first
expression
*)
</span><br>
92 <span class=
"keyword">let
</span> output_beam1
=
eval
e1
subtree1
input_beam
<span class=
"keyword">in
</span><br>
94 <span class=
"comment">(*
beam
matching
*)
</span><br>
95 <span class=
"keyword">let
</span> ref_output_beam1
=
ref
(beam_add_one_memory
output_beam1)
<span class=
"keyword">in
</span><br>
96 <span class=
"keyword">let
</span> input_beam2
=
<span class=
"constructor">List
</span>.concat
<br>
97 (
<span class=
"constructor">Array
</span>.to_list
(
<span class=
"constructor">Array
</span>.make
((fst
d2)/(
<span class=
"constructor">List
</span>.length
output_beam1))
!ref_output_beam1))
<br>
98 <span class=
"keyword">in
</span><br>
100 <span class=
"comment">(*
evaluate
the
second
expression
*)
</span><br>
101 <span class=
"keyword">if
</span> <span class=
"constructor">List
</span>.length
input_beam2
=
fst
d2
<br>
102 <span class=
"keyword">then
</span> eval
e2
subtree2
input_beam2
<br>
103 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Split"</span>)
<br>
104 )
<br>
105 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Split"</span>)
<span class=
"keyword">in
</span><br>
108 </code><table><tr><td></td><td><span class=
"comment">(** val interpret_merge : faust_exp -
> faust_exp -
> beam -
> beam,
109 interprets merge(e1, e2) with input beam, produces output beam.*)
</span></td></tr></table><code class=
"code"><br>
110 <span class=
"keyword">let
</span> interpret_merge
=
<span class=
"keyword">fun
</span> e1
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> e2
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> dimension_tree
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> input_beam
<span class=
"keywordsign">-
></span><br>
112 <span class=
"comment">(*
dimension
information
*)
</span><br>
113 <span class=
"keyword">let
</span> n
=
<span class=
"constructor">List
</span>.length
input_beam
<span class=
"keyword">in
</span><br>
114 <span class=
"keyword">let
</span> subtree1
=
subtree_left
dimension_tree
<span class=
"keyword">in
</span><br>
115 <span class=
"keyword">let
</span> subtree2
=
subtree_right
dimension_tree
<span class=
"keyword">in
</span><br>
116 <span class=
"keyword">let
</span> d1
=
get_root
subtree1
<span class=
"keyword">in
</span><br>
117 <span class=
"keyword">let
</span> d2
=
get_root
subtree2
<span class=
"keyword">in
</span><br>
120 <span class=
"keyword">if
</span> n
=
fst
d1
<span class=
"keyword">then
</span><br>
121 (
<br>
122 <span class=
"comment">(*
evaluate
the
first
expression
*)
</span><br>
123 <span class=
"keyword">let
</span> output_beam1
=
eval
e1
subtree1
input_beam
<span class=
"keyword">in
</span><br>
125 <span class=
"comment">(*
beam
matching
*)
</span><br>
126 <span class=
"keyword">let
</span> input_beam2
=
<br>
127 (
<br>
128 <span class=
"keyword">let
</span> fois
=
(snd
d1)/(fst
d2)
<span class=
"keyword">in
</span><br>
129 <span class=
"keyword">let
</span> ref_beam
=
ref
(sublist
output_beam1
0 (fst
d2))
<span class=
"keyword">in
</span><br>
130 <span class=
"keyword">for
</span> i
=
1 <span class=
"keyword">to
</span> fois
-
1 <span class=
"keyword">do
</span><br>
131 <span class=
"keyword">let
</span> temp_beam
=
sublist
output_beam1
(i*(fst
d2))
(fst
d2)
<span class=
"keyword">in
</span><br>
132 ref_beam
:=
<span class=
"constructor">List
</span>.map2
signal_add
(!ref_beam)
temp_beam;
<br>
133 <span class=
"keyword">done
</span>;
<br>
134 !ref_beam
<br>
135 )
<br>
136 <span class=
"keyword">in
</span><br>
138 <span class=
"comment">(*
evaluate
the
second
expression
*)
</span><br>
139 <span class=
"keyword">if
</span> <span class=
"constructor">List
</span>.length
input_beam2
=
fst
d2
<br>
140 <span class=
"keyword">then
</span> eval
e2
subtree2
input_beam2
<br>
141 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Merge"</span>)
<br>
142 )
<br>
143 <span class=
"keyword">else
</span> raise
(
<span class=
"constructor">Evaluation_Error
</span> <span class=
"string">"Merge"</span>)
<span class=
"keyword">in
</span><br>
146 </code><table><tr><td></td><td><span class=
"comment">(** val interpret_rec : faust_exp -
> faust_exp -
> beam -
> beam,
147 interprets rec(e1, e2) with input beam, produces output beam.*)
</span></td></tr></table><code class=
"code"><br>
148 <span class=
"keyword">let
</span> interpret_rec
=
<span class=
"keyword">fun
</span> e1
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> e2
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> dimension_tree
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> input_beam
<span class=
"keywordsign">-
></span><br>
150 <span class=
"comment">(*
dimension
information
*)
</span><br>
151 <span class=
"keyword">let
</span> subtree1
=
subtree_left
dimension_tree
<span class=
"keyword">in
</span><br>
152 <span class=
"keyword">let
</span> subtree2
=
subtree_right
dimension_tree
<span class=
"keyword">in
</span><br>
153 <span class=
"keyword">let
</span> d1
=
get_root
subtree1
<span class=
"keyword">in
</span><br>
154 <span class=
"keyword">let
</span> d2
=
get_root
subtree2
<span class=
"keyword">in
</span><br>
156 <span class=
"comment">(*
estimate
stockage
size
for
delay
*)
</span><br>
157 <span class=
"keyword">let
</span> delay_int
=
1 +
delay
e2
+
delay
e1
<span class=
"keyword">in
</span><br>
159 <span class=
"comment">(*
prepare
stockage
*)
</span><br>
160 <span class=
"keyword">let
</span> memory_hashtbl
=
<span class=
"constructor">Hashtbl
</span>.create
delay_int
<span class=
"keyword">in
</span><br>
161 <span class=
"keyword">let
</span> rate_list
=
ref
(
<span class=
"constructor">Array
</span>.to_list
(
<span class=
"constructor">Array
</span>.make
(snd
d1)
0))
<span class=
"keyword">in
</span><br>
163 </code><table><tr><td> </td><td><span class=
"comment">(** val apply_to : 'a -
> ('a -
> 'b) -
> 'b *)
</span></td></tr></table><code class=
"code"><br>
164 <span class=
"keyword">let
</span> apply_to
=
<span class=
"keyword">fun
</span> t
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> f
<span class=
"keywordsign">-
></span> f
t
<span class=
"keyword">in
</span><br>
166 </code><table><tr><td> </td><td><span class=
"comment">(** val get_value_fun_list : (int -
> (int list) * (value list)) -
> (int -
> value) list *)
</span></td></tr></table><code class=
"code"> <br>
167 <span class=
"keyword">let
</span> get_value_fun_list
=
<span class=
"keyword">fun
</span> beam_fun
<span class=
"keywordsign">-
></span> <br>
168 <span class=
"keyword">let
</span> tmp
=
<span class=
"keyword">fun
</span> beam_fun
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> i
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> t
<span class=
"keywordsign">-
></span> <br>
169 <span class=
"constructor">List
</span>.nth
(snd
(beam_fun
t))
i
<span class=
"keyword">in
</span><br>
170 <span class=
"constructor">List
</span>.map
(tmp
beam_fun)
(
<span class=
"constructor">Array
</span>.to_list
(
<span class=
"constructor">Array
</span>.init
(snd
d1)
(
<span class=
"keyword">fun
</span> n
<span class=
"keywordsign">-
></span> n)))
<span class=
"keyword">in
</span><br>
172 </code><table><tr><td> </td><td><span class=
"comment">(** val make_signal : int -
> (int -
> value) -
> signal, combines rate and function. *)
</span></td></tr></table><code class=
"code"><br>
173 <span class=
"keyword">let
</span> make_signal
=
<span class=
"keyword">fun
</span> rate
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> f
<span class=
"keywordsign">-
></span> (rate,
f)
<span class=
"keyword">in
</span><br>
175 </code><table><tr><td> </td><td><span class=
"comment">(** val output_beam_fun : int -
> (int list) * (value list), with
177 output: rate list * value list *)
</span></td></tr></table><code class=
"code"><br>
178 <span class=
"keyword">let
</span> <span class=
"keyword">rec
</span> output_beam_fun
=
<span class=
"keyword">fun
</span> t
<span class=
"keywordsign">-
></span><br>
180 <span class=
"comment">(*
initial
value
in
constrctor
"rec '~'" *)
</span><br>
181 <span class=
"keyword">if
</span> t
< 0 <span class=
"keyword">then
</span><br>
182 <span class=
"keyword">let
</span> init_rate_list
=
<span class=
"constructor">Array
</span>.to_list
(
<span class=
"constructor">Array
</span>.make
(snd
d1)
0)
<span class=
"keyword">in
</span><br>
183 <span class=
"keyword">let
</span> value_list
=
<span class=
"constructor">Array
</span>.to_list
(
<span class=
"constructor">Array
</span>.make
(snd
d1)
<span class=
"constructor">Zero
</span>)
<span class=
"keyword">in
</span><br>
184 (init_rate_list,
value_list)
<br>
186 <span class=
"comment">(*
check
stockage
at
time
t
*)
</span><br>
187 <span class=
"keyword">else
</span> <span class=
"keyword">if
</span> <span class=
"constructor">Hashtbl
</span>.mem
memory_hashtbl
t
<span class=
"keyword">then
</span> <br>
188 (!rate_list,
<span class=
"constructor">Hashtbl
</span>.find
memory_hashtbl
t)
<br>
190 <span class=
"comment">(*
blocks
:
"a ~ b",
calculate
rate
list
and
value
list
at
time
t
*)
</span><br>
191 <span class=
"keyword">else
</span> <br>
192 <span class=
"comment">(*
mid_output_fun_list
:
(int
-
> value)
list
*)
</span><br>
193 <span class=
"keyword">let
</span> mid_output_fun_list
=
get_value_fun_list
output_beam_fun
<span class=
"keyword">in
</span><br>
195 <span class=
"comment">(*
b_input_fun_list
:
(int
-
> value)
list
*)
</span><br>
196 <span class=
"keyword">let
</span> b_input_fun_list
=
<span class=
"constructor">List
</span>.map
<br>
197 (
<span class=
"keyword">fun
</span> s
<span class=
"keywordsign">-
></span> <span class=
"keyword">fun
</span> t
<span class=
"keywordsign">-
></span> s
(t
-
1))
<br>
198 (sublist
mid_output_fun_list
0 (fst
d2))
<span class=
"keyword">in
</span><br>
200 <span class=
"comment">(*
b_input_beam
:
signal
list
*)
</span><br>
201 <span class=
"keyword">let
</span> b_input_beam
=
<span class=
"constructor">List
</span>.map2
make_signal
<br>
202 (sublist
!rate_list
0 (fst
d2))
<br>
203 b_input_fun_list
<span class=
"keyword">in
</span><br>
205 <span class=
"comment">(*
evaluation
of
block
"b" *)
</span><br>
206 <span class=
"keyword">let
</span> b_output_beam
=
(eval
e2
subtree2
b_input_beam)
<span class=
"keyword">in
</span><br>
208 <span class=
"comment">(*
evaluation
of
block
"a" *)
</span><br>
209 <span class=
"keyword">let
</span> a_input_beam
=
b_output_beam
@
input_beam
<span class=
"keyword">in
</span><br>
210 <span class=
"keyword">let
</span> mid_output_beam
=
eval
e1
subtree1
a_input_beam
<span class=
"keyword">in
</span><br>
212 <span class=
"comment">(*
calculate
rate
list
and
value
list
at
time
t
*)
</span><br>
213 <span class=
"keyword">let
</span> mid_output_rate_list
=
<span class=
"constructor">List
</span>.map
fst
mid_output_beam
<span class=
"keyword">in
</span><br>
214 <span class=
"keyword">let
</span> mid_output_value_list
=
<span class=
"constructor">List
</span>.map
(apply_to
t)
(
<span class=
"constructor">List
</span>.map
snd
mid_output_beam)
<span class=
"keyword">in
</span><br>
216 <span class=
"comment">(*
update
stockage
*)
</span><br>
217 <span class=
"keyword">let
</span> ()
=
(rate_list
:=
mid_output_rate_list)
<span class=
"keyword">in
</span><br>
218 <span class=
"keyword">let
</span> ()
=
<span class=
"constructor">Hashtbl
</span>.add
memory_hashtbl
t
mid_output_value_list
<span class=
"keyword">in
</span><br>
219 <span class=
"keyword">let
</span> ()
=
<span class=
"constructor">Hashtbl
</span>.remove
memory_hashtbl
(t
-
delay_int)
<span class=
"keyword">in
</span><br>
220 (mid_output_rate_list,
mid_output_value_list)
<span class=
"keyword">in
</span><br>
222 <span class=
"comment">(*
output_beam
:
signal
list
*)
</span><br>
223 <span class=
"keyword">let
</span> output_beam
=
<span class=
"constructor">List
</span>.map2
make_signal
!rate_list
(get_value_fun_list
output_beam_fun)
<span class=
"keyword">in
</span><br>
224 output_beam
<span class=
"keyword">in
</span><br>
227 </code><table><tr><td> </td><td><span class=
"comment">(** Call for previous functions *)
</span></td></tr></table><code class=
"code"><br>
228 <span class=
"keyword">match
</span> exp_faust
<span class=
"keyword">with
</span><br>
229 <span class=
"keywordsign">|
</span><span class=
"constructor">Const
</span> v
<span class=
"keywordsign">-
></span> interpret_const
v
input_beam
<br>
230 <span class=
"keywordsign">|
</span><span class=
"constructor">Ident
</span> s
<span class=
"keywordsign">-
></span> interpret_ident
s
input_beam
<br>
231 <span class=
"keywordsign">|
</span><span class=
"constructor">Par
</span> (e1,
e2)
<span class=
"keywordsign">-
></span> interpret_par
e1
e2
dimension_tree
input_beam
<br>
232 <span class=
"keywordsign">|
</span><span class=
"constructor">Seq
</span> (e1,
e2)
<span class=
"keywordsign">-
></span> interpret_seq
e1
e2
dimension_tree
input_beam
<br>
233 <span class=
"keywordsign">|
</span><span class=
"constructor">Split
</span> (e1,
e2)
<span class=
"keywordsign">-
></span> interpret_split
e1
e2
dimension_tree
input_beam
<br>
234 <span class=
"keywordsign">|
</span><span class=
"constructor">Merge
</span> (e1,
e2)
<span class=
"keywordsign">-
></span> interpret_merge
e1
e2
dimension_tree
input_beam
<br>
235 <span class=
"keywordsign">|
</span><span class=
"constructor">Rec
</span> (e1,
e2)
<span class=
"keywordsign">-
></span> interpret_rec
e1
e2
dimension_tree
input_beam
</code></body></html>