File: txtin_parser.mly

package info (click to toggle)
orpie 1.5.2-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,924 kB
  • ctags: 2,720
  • sloc: ml: 13,872; ansic: 3,754; makefile: 310; sh: 11; python: 11
file content (376 lines) | stat: -rw-r--r-- 11,161 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
%{
(*  Orpie -- a fullscreen RPN calculator for the console
 *  Copyright (C) 2003-2004, 2005, 2006-2007, 2010 Paul Pelzl
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, Version 2,
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Please send bug reports, patches, etc. to Paul Pelzl at 
 *  <pelzlpj@gmail.com>.
 *)

(* txtinr_parser.mly
 *
 * Orpie can handle input of data from a textfile created by an external editor.
 * editor_parser.mly generates a parser that works in conjunction with
 * editor_lexer.mll to enter this data on the stack.
 *)


let pi = 3.14159265358979323846;;

type f_or_c = | F of float 
              | C of Complex.t

(* decode a matrix of type CF and decide
 * whether it has float elements or complex elements,
 * then create the appropriate orpie_data_t type. *)
let decode_float_complex_matrix mat u_str =
   let num_rows = Array.length mat
   and num_cols = Array.length mat.(0) in
   let flt_array = Array.make_matrix num_rows num_cols 0.0
   and cpx_array = Array.make_matrix num_rows num_cols Complex.zero in
   let has_complex = ref false in
   for i = 0 to pred num_rows do
      if Array.length mat.(i) != num_cols then
         raise (Utility.Txtin_error "inconsistent number of columns in input matrix")
      else
         begin
            for j = 0 to pred num_cols do
               begin match mat.(i).(j) with
               |F el ->
                  flt_array.(i).(j) <- el;
                  cpx_array.(i).(j) <- {Complex.re = el; Complex.im = 0.0}
               |C el -> 
                  has_complex := true;
                  cpx_array.(i).(j) <- el
               end
            done;
         end
   done;
   if !has_complex then
      Rpc_stack.RpcComplexMatrixUnit (Gsl_matrix_complex.of_arrays cpx_array,
      Units.units_of_string (Str.string_after u_str 1) !Rcfile.unit_table)
   else
      Rpc_stack.RpcFloatMatrixUnit (Gsl_matrix.of_arrays flt_array,
      Units.units_of_string (Str.string_after u_str 1) !Rcfile.unit_table)


let rect_of_polar_rad r theta = 
   let real = r *. (cos theta)
   and imag = r *. (sin theta) in
   {Complex.re = real; Complex.im = imag}

let rect_of_polar_deg r theta = 
   let rad_theta = theta /. 180.0 *. pi in
   rect_of_polar_rad r rad_theta


(* convert an integer string to an RpcInt *)
let decode_integer i_str =
   let int_str = i_str in
   let str_len = String.length int_str in
   let digits  = Str.string_before int_str (str_len - 2) in
   let int_val =
     let base_char = int_str.[pred str_len] in
     if base_char = 'b' then 
        Big_int_str.big_int_of_string_base digits 2
     else if base_char = 'o' then
        Big_int_str.big_int_of_string_base digits 8
     else if base_char = 'd' then
        Big_int_str.big_int_of_string_base digits 10
     else if base_char = 'h' then
        Big_int_str.big_int_of_string_base digits 16
     else 
        let base_string = String.make 1 base_char in
        raise (Utility.Txtin_error ("illegal base character " ^ base_string))
   in
   Rpc_stack.RpcInt int_val


(* convert a floating point string and a unit string to an RpcFloatUnit *)
let decode_float_units f_str u_str =
   Rpc_stack.RpcFloatUnit ((float_of_string f_str),
      Units.units_of_string (Str.string_after u_str 1) !Rcfile.unit_table)

(* convert a cartesian complex number string and a unit string to an 
 * RpcComplexUnit *)
let decode_complex_rect_units re_str im_str u_str =
   let f1 = float_of_string re_str
   and f2 = float_of_string im_str in
   Rpc_stack.RpcComplexUnit ({Complex.re = f1; Complex.im = f2},
   Units.units_of_string (Str.string_after u_str 1) !Rcfile.unit_table)

(* convert a polar representation complex number string to an
 * RpcComplex.  The rect_of_polar argument should take care of
 * any necessary degrees/radians conversion. *)
let decode_complex_polar_units rect_of_polar mag_str ang_str u_str = 
   let mag = float_of_string mag_str
   and ang = float_of_string ang_str in
   Rpc_stack.RpcComplexUnit ((rect_of_polar mag ang),
   Units.units_of_string (Str.string_after u_str 1) !Rcfile.unit_table)

(* convert a polar representation complex number string to an
 * RpcComplex.  Assumes radian representation of the angle. *)
let decode_complex_polar_rad_units mag_str ang_str u_str =
   decode_complex_polar_units rect_of_polar_rad mag_str ang_str u_str

(* convert a polar representation complex number string to an
 * RpcComplex.  Assumes degree representation of the angle. *)
let decode_complex_polar_deg_units mag_str ang_str u_str =
   decode_complex_polar_units rect_of_polar_deg mag_str ang_str u_str

(* convert a matrix to an RpcFloatMatrixUnit or an RpcComplexMatrix. *)
let decode_matrix_units mat_rows u_str =
   (* matrix_rows is a list of rows, each of which
    * is a list of elements; create a 2d array 
    * from these lists, and generate the appropriate
    * orpie_data_t from the 2d array. *)
   let num_rows = List.length mat_rows in
   let num_cols = List.length (List.hd mat_rows) in
   let mat = Array.make_matrix num_rows num_cols (F 0.0) in
   let temp_arr = Array.of_list (List.rev mat_rows) in
   for i = 0 to pred num_rows do
      mat.(i) <- Array.of_list (List.rev temp_arr.(i))
   done;
   (* create a float array or a complex array, depending
    * on whether or not any complex types are present
    * in this array. *)
   decode_float_complex_matrix mat u_str

(* convert a variable string to an RpcVariable *)
let decode_variable v_str =
   Rpc_stack.RpcVariable v_str
%}


/* declarations */
%token <string> INTEGER
%token <string> FLOAT
%token <string> VARIABLE
%token <string> UNITS
%token BEGINCOMPLEX
%token ENDCOMPLEX
%token SEPARATOR
%token ANGLE
%token BEGINMATRIX
%token ENDMATRIX
%token EOF

/* parse the input under the assumption that angles
 * are provided using radian measure */
%start decode_data_rad
%type <Rpc_stack.orpie_data_t list> decode_data_rad

/* parse the input under the assumption that angles
 * are provided using degree measure */
%start decode_data_deg
%type <Rpc_stack.orpie_data_t list> decode_data_deg

%%
/* rules */

/****************************************
 * ANGLES PROVIDED USING RADIAN MEASURE *
 ****************************************/
decode_data_rad:
   datalist_rad EOF 
      { List.rev $1 }
;


datalist_rad:
   datalist_rad datagroup_rad
      { $2 :: $1 }
   |  /* empty */
      { [] }
;


datagroup_rad:
   data_rad
      { $1 }
;


data_rad:
   INTEGER 
      { decode_integer $1 }

   | VARIABLE
      { decode_variable $1}

   | FLOAT UNITS
      { decode_float_units $1 $2 }

   | FLOAT
      { decode_float_units $1 "_" } 

   | BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX UNITS
      { decode_complex_rect_units $2 $4 $6 }

   | BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX
      { decode_complex_rect_units $2 $4 "_" }

   | BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX UNITS
      { decode_complex_polar_rad_units $2 $4 $6 }

   | BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX
      { decode_complex_polar_rad_units $2 $4 "_" }

   | BEGINMATRIX matrix_rows_rad ENDMATRIX UNITS
      { decode_matrix_units $2 $4 }

   | BEGINMATRIX matrix_rows_rad ENDMATRIX
      { decode_matrix_units $2 "_" }
;


matrix_rows_rad:
   matrix_rows_rad BEGINMATRIX matrix_row_elements_rad ENDMATRIX
      {$3 :: $1}
   | /* empty */
      {[]}
;


matrix_row_elements_rad:
   matrix_row_elements_rad SEPARATOR FLOAT
      { (F (float_of_string $3)) :: $1 }
   | matrix_row_elements_rad SEPARATOR BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX
      { 
         let f1 = float_of_string $4
         and f2 = float_of_string $6 in
         (C {Complex.re = f1; Complex.im = f2}) :: $1
      }
   | matrix_row_elements_rad SEPARATOR BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX
      { 
         let r  = float_of_string $4
         and th = float_of_string $6 in
         (C (rect_of_polar_rad r th)) :: $1
      }
   | FLOAT
      { (F (float_of_string $1)) :: [] }
   | BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX
      {
         let f1 = float_of_string $2
         and f2 = float_of_string $4 in
         (C {Complex.re = f1; Complex.im = f2}) :: []
      }
   | BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX
      {
         let r  = float_of_string $2
         and th = float_of_string $4 in
         (C (rect_of_polar_rad r th)) :: []
      }
;



/****************************************
 * ANGLES PROVIDED USING DEGREE MEASURE *
 ****************************************/
decode_data_deg:
   datalist_deg EOF 
      { List.rev $1 }
;


datalist_deg:
   datalist_deg datagroup_deg
      { $2 :: $1 }
   |  /* empty */
      { [] }
;


datagroup_deg:
   data_deg
      { $1 }
;


data_deg:
   INTEGER 
      { decode_integer $1 }

   | VARIABLE
      { decode_variable $1}

   | FLOAT UNITS
      { decode_float_units $1 $2 }

   | FLOAT
      { decode_float_units $1 "_" } 

   | BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX UNITS
      { decode_complex_rect_units $2 $4 $6 }

   | BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX
      { decode_complex_rect_units $2 $4 "_" }

   | BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX UNITS
      { decode_complex_polar_deg_units $2 $4 $6 }

   | BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX
      { decode_complex_polar_deg_units $2 $4 "_" }

   | BEGINMATRIX matrix_rows_deg ENDMATRIX UNITS
      { decode_matrix_units $2 $4 }

   | BEGINMATRIX matrix_rows_deg ENDMATRIX
      { decode_matrix_units $2 "_" }
;


matrix_rows_deg:
   matrix_rows_deg BEGINMATRIX matrix_row_elements_deg ENDMATRIX
      {$3 :: $1}
   | /* empty */
      {[]}
;


matrix_row_elements_deg:
   matrix_row_elements_deg SEPARATOR FLOAT
      { (F (float_of_string $3)) :: $1 }
   | matrix_row_elements_deg SEPARATOR BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX
      { 
         let f1 = float_of_string $4
         and f2 = float_of_string $6 in
         (C {Complex.re = f1; Complex.im = f2}) :: $1
      }
   | matrix_row_elements_deg SEPARATOR BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX
      { 
         let r  = float_of_string $4
         and th = float_of_string $6 in
         (C (rect_of_polar_deg r th)) :: $1
      }
   | FLOAT
      { (F (float_of_string $1)) :: [] }
   | BEGINCOMPLEX FLOAT SEPARATOR FLOAT ENDCOMPLEX
      {
         let f1 = float_of_string $2
         and f2 = float_of_string $4 in
         (C {Complex.re = f1; Complex.im = f2}) :: []
      }
   | BEGINCOMPLEX FLOAT ANGLE FLOAT ENDCOMPLEX
      {
         let r  = float_of_string $2
         and th = float_of_string $4 in
         (C (rect_of_polar_deg r th)) :: []
      }
;


/* arch-tag: DO_NOT_CHANGE_c65a2550-f00d-40f1-b51f-f5d654257785 */