-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSS_interp.java
More file actions
1409 lines (1243 loc) · 40.2 KB
/
Copy pathSS_interp.java
File metadata and controls
1409 lines (1243 loc) · 40.2 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SaySentence Engine -- An open source SayScript Interpreter and Environment.
*
* Copyright (C) 2013, ParseTree Corporation.
*
* Steve Murphy <murf@parsetree.com>
*
* This program is free software, distributed under the terms of
* the Lesser GNU General Public License Version 2.1. See the LICENSE file
* at the top of the source tree.
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.ListIterator;
import java.util.Enumeration;
import java.util.regex.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
class SS_interp
{
ArrayList<SS_vardef> vars; /* var defs for a script are here. */
long level;
/* these two are the current value of the num, if the sayscript is
pronouncing them */
long num; /* in the number/enumeration scripts, this and str will
hold two diff reps of the number, both are updated with
each CUT(). */
String str; /* in general, most sayscripts will use this to pass data */
String options; /* these will be available to the interpreter */
String pref_lang_locale; /* what lang/locale/sublocale/etc do we want to use? */
/* the current time? */
Date timeval; /* the time for date/time related sayscripts */
SS_interp(long depth)
{
vars = new ArrayList<SS_vardef>();
timeval = new Date();
level = depth;
}
boolean ops_are_true(SS_statement statement, ArrayList<SS_op> oplist, ArrayList<SS_log> log_list)
{
ListIterator<SS_op> op_it = oplist.listIterator();
long low, high;
boolean found;
while (op_it.hasNext())
{
SS_op op = (SS_op)op_it.next();
switch (op.type)
{
case SS_op.SS_OP_TYPE_RANGE:
if (op.arglist.size() == 2)
{
low = ((SS_op_arg)op.arglist.get(0)).num;
high = ((SS_op_arg)op.arglist.get(1)).num;
if (num >= low && num <= high)
continue; /* test the next op */
}
return false;
case SS_op.SS_OP_TYPE_PATTERN:
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
if (arg.pattern != null && arg.preg != null)
{
Matcher z = arg.preg.matcher(str);
if (!z.find(0))
return false;
break; /* matched, so go on... */
}
return false; /* wrong # of args? */
}
return false;
case SS_op.SS_OP_TYPE_GREATER:
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
if (num > arg.num)
break;
}
return false;
case SS_op.SS_OP_TYPE_LESS:
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
if (num < arg.num)
break;
}
return false;
case SS_op.SS_OP_TYPE_VAR:
found = false;
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
String varname = arg.str;
ListIterator<SS_vardef> var_it = vars.listIterator();
while (var_it.hasNext())
{
SS_vardef var = (SS_vardef)var_it.next();
if (varname.equals(var.varname))
{
if( var.varval != null && !var.varval.equals("0"))
{
found = true;
break; /* true -- the varval is not 0 */
}
return false;
}
}
if (!found)
return false; /* no var matches? then this var is not defined */
else
break;
}
return false;
case SS_op.SS_OP_TYPE_NOTVAR:
found = false;
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
String varname = arg.str;
ListIterator<SS_vardef> var_it = vars.listIterator();
while (var_it.hasNext())
{
SS_vardef var = (SS_vardef)var_it.next();
if (varname.equals(var.varname))
{
found = true;
if( var.varval != null && var.varval.equals("0"))
break; /* true -- the varval is not 0 */
return false;
}
}
if (!found)
return false; /* no var matches? then this var is not defined */
else
break;
}
return false;
case SS_op.SS_OP_TYPE_NUMLEN: /* complicated by ODD and EVEN */
if (op.arglist.size() == 1)
{
int len;
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
if (arg.str != null && arg.str.length() != 0)
{
if (arg.str.equals("EVEN"))
{
if((str.length() % 2) == 0)
{
break;
}
return false;
}
if (arg.str.equals("ODD"))
{
if((str.length() % 2) == 1)
{
break;
}
return false;
}
Long x = new Long(arg.str);
if (str.length() == x) /* length matches */
break;
}
else
{
if (arg.num == str.length())
break;
}
return false;
}
return false;
case SS_op.SS_OP_TYPE_ATEXIT: /* ATEXIT in wrong place, or at wrong time - the statement should not be eval'd */
return false;
case SS_op.SS_OP_TYPE_DATEPAST_RANGE:
if (op.arglist.size() == 2)
{
Date now = new Date();
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
SS_op_arg arg2 = (SS_op_arg)op.arglist.get(1);
if (arg1.num < arg2.num) /* make sure the bigger number is first, back further in time */
{
long sw = arg2.num;
arg2.num = arg1.num;
arg1.num = sw;
}
if (timeval.before(now))
{
/* the range numbers are in days */
Date range_start = new Date(now.getTime() - arg1.num * 86400000); /* in days, converted to milliseconds */
Date range_end = new Date(now.getTime() - arg2.num * 86400000);
Calendar cal =new GregorianCalendar();
cal.setTime(timeval);
int hour = cal.get(Calendar.HOUR_OF_DAY);
range_start.setTime(range_start.getTime() - hour * 3600000); /* the range of day 0 (or any day) is from midnight */
range_end.setTime(range_end.getTime() + (24-hour) * 3600000); /* the range of a day is till midnight */
if (range_start.before(timeval) && range_end.after(timeval))
break; /* success, the time val falls within the specified ranges */
return false; /* it didn't fall in the range! */
}
return false;
}
return false;
case SS_op.SS_OP_TYPE_DATEPAST_GREATER:
if (op.arglist.size() == 1)
{
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
Date now = new Date();
if (timeval.before(now))
{
/* the range numbers are in days */
Date range_start = new Date(now.getTime() - arg1.num * 86400000); /* in days, converted to milliseconds */
Calendar cal = new GregorianCalendar();
cal.setTime(timeval);
int hour = cal.get(Calendar.HOUR_OF_DAY);
range_start.setTime(range_start.getTime() - hour * 3600000); /* the range of day 0 (or any day) is from midnight */
if (timeval.before(range_start))
break; /* success, the time val is more than x days in the past! */
return false; /* it didn't fall in the range! */
}
return false;
}
return false;
case SS_op.SS_OP_TYPE_DATEFUT_RANGE:
if (op.arglist.size() == 2)
{
Date now = new Date();
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
SS_op_arg arg2 = (SS_op_arg)op.arglist.get(1);
if (arg1.num > arg2.num) /* make sure the bigger number is last, further in time */
{
long sw = arg2.num;
arg2.num = arg1.num;
arg1.num = sw;
}
if (timeval.after(now))
{
/* the range numbers are in days */
Date range_start = new Date(now.getTime() + arg1.num * 86400000); /* in days, converted to milliseconds */
Date range_end = new Date(now.getTime() + arg2.num * 86400000);
Calendar cal = new GregorianCalendar();
cal.setTime(timeval);
int hour = cal.get(Calendar.HOUR_OF_DAY);
range_start.setTime(range_start.getTime() - hour * 3600000); /* the range of day 0 (or any day) is from midnight */
range_end.setTime(range_end.getTime() + (24-hour) * 3600000); /* the range of a day is till midnight */
if (range_start.before(timeval) && range_end.after(timeval))
break; /* success, the time val falls within the specified ranges */
return false; /* it didn't fall in the range! */
}
return false;
}
return false;
case SS_op.SS_OP_TYPE_DATEFUT_GREATER:
if (op.arglist.size() == 1)
{
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
Date now = new Date();
if (timeval.after(now))
{
/* the range numbers are in days */
Date range_start = new Date(now.getTime() + arg1.num * 86400000); /* in days, converted to milliseconds */
Calendar cal = new GregorianCalendar();
cal.setTime(timeval);
int hour = cal.get(Calendar.HOUR_OF_DAY);
range_start.setTime(range_start.getTime() - hour * 3600000); /* the range of day 0 (or any day) is from midnight */
if (timeval.after(range_start))
break; /* success, the time val is more than x days in the past! */
return false; /* it didn't fall in the range! */
}
return false;
}
return false;
case SS_op.SS_OP_TYPE_ANYDATE:
break; /* this is going to true always */
case SS_op.SS_OP_TYPE_SECOND_RANGE:
if (op.arglist.size() == 2)
{
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
SS_op_arg arg2 = (SS_op_arg)op.arglist.get(1);
Calendar cal = new GregorianCalendar();
cal.setTime(timeval);
int range_start = (int)arg1.num;
int range_end = (int)arg2.num;
int seconds = cal.get(Calendar.SECOND);
if (seconds >= range_start && seconds <= range_end)
break; /* success */
return false;
}
return false;
case SS_op.SS_OP_TYPE_MINUTE_RANGE:
if (op.arglist.size() == 2)
{
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
SS_op_arg arg2 = (SS_op_arg)op.arglist.get(1);
Calendar cal = new GregorianCalendar();
cal.setTime(timeval);
int range_start = (int)arg1.num;
int range_end = (int)arg2.num;
int min = cal.get(Calendar.MINUTE);
if (min >= range_start && min <= range_end)
break; /* success */
return false;
}
return false;
case SS_op.SS_OP_TYPE_HOUR_RANGE:
if (op.arglist.size() == 2)
{
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
SS_op_arg arg2 = (SS_op_arg)op.arglist.get(1);
Calendar cal = new GregorianCalendar();
// System.out.println("Set cal time to "+ timeval);
cal.setTime(timeval);
int range_start = (int)arg1.num;
int range_end = (int)arg2.num;
int hr = cal.get(Calendar.HOUR_OF_DAY);
// System.out.println("HOUR RANGE: result of cal.get=" + hr + " where timeval = " + timeval + " where range = " + range_start + ", " + range_end);
if (hr >= range_start && hr <= range_end)
break; /* success */
return false;
}
else
{
// System.out.println("HOUR_RANGE: arglist of wrong size; expecting 2, but there are " + op.arglist.size() + " instead!");
}
return false;
case SS_op.SS_OP_TYPE_YEAR_RANGE:
if (op.arglist.size() == 2)
{
SS_op_arg arg1 = (SS_op_arg)op.arglist.get(0);
SS_op_arg arg2 = (SS_op_arg)op.arglist.get(1);
Calendar cal = new GregorianCalendar();
cal.setTime(timeval);
int range_start = (int)arg1.num;
int range_end = (int)arg2.num;
int yr = cal.get(Calendar.YEAR);
if (yr >= range_start && yr <= range_end)
break; /* success */
return false;
}
return false;
case SS_op.SS_OP_TYPE_OPT:
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
int interesting_op = arg.str.charAt(0);
if (options.indexOf(interesting_op) >= 0)
break;
return false;
}
return false;
case SS_op.SS_OP_TYPE_NOTOPT:
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
int interesting_op = arg.str.charAt(0);
if (options.indexOf(interesting_op) == -1)
break;
return false;
}
return false;
case SS_op.SS_OP_TYPE_ATBEGIN: /* ATEXIT in wrong place, or at wrong time - the statement should not be eval'd */
return false;
case SS_op.SS_OP_TYPE_YEAR_PATTERN:
if (op.arglist.size() == 1)
{
SS_op_arg arg = (SS_op_arg)op.arglist.get(0);
if (arg.pattern != null && arg.preg != null)
{
Matcher z = arg.preg.matcher(str);
if (!z.find(0))
return false;
break; /* matched, so go on... */
}
return false; /* wrong # of args? */
}
return false;
}
}
return true;
}
void add_silence_files(char script_char, SS_engine engine, ArrayList<SS_playlist> flist, ArrayList<SS_log> loglist)
{
SS_silence_def itsdef = engine.find_silence(pref_lang_locale, '\0', script_char);
if (itsdef != null)
{
ListIterator<SS_playlist> pfile = itsdef.playfiles.listIterator();
while (pfile.hasNext())
{
SS_playlist pf = (SS_playlist)pfile.next();
SS_playlist play = new SS_playlist();
play.str = pf.str;
play.interrupt = pf.interrupt;
flist.add(play);
}
}
else
{
/* LOG THIS?? Is it necessary to define all the silence types?
They can silently be filled in with nothing. */
}
}
void move_filename_to_playlist(StringBuffer filename, ArrayList<SS_playlist> flist, String interrupts)
{
SS_playlist play = new SS_playlist();
play.str = filename.toString();
play.interrupt = interrupts;
/* fill in the interrupt keys */
/* reset the filename */
filename.delete(0,filename.length());
flist.add(play);
}
static boolean isNumeric(String str)
{
for (char c : str.toCharArray())
{
if(!Character.isDigit(c)) return false; // works for int strings only
}
return true;
}
String eval_play_expr(SS_play_expr expr, long num, String str, Date timeval, SS_engine engine, ListIterator<SS_play_expr> itplay, StringBuffer filename, ArrayList<SS_playlist> flist, ArrayList<SS_log> log_list, SS_script script, SS_statement stat, String interrupts)
{
GregorianCalendar cal;
String l1, r1;
int sec;
switch(expr.type)
{
case SS_play_expr.SS_EXPR_CONDITIONAL:
String cond = eval_play_expr(expr.cond, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if ( cond == null || cond.equals("0"))
{
String f1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
return f1;
}
else
{
String t1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
return t1;
}
case SS_play_expr.SS_EXPR_LOGOR:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if ( l1 != null && !l1.equals("0"))
{
return "1";
}
else
{
l1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if ( l1 != null && !l1.equals("0"))
{
return "1";
}
else
{
return "0";
}
}
case SS_play_expr.SS_EXPR_LOGAND:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if ( l1 == null || l1.equals("0"))
{
return "0";
}
else
{
l1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if ( l1 == null || l1.equals("0"))
{
return "0";
}
else
{
return "1";
}
}
case SS_play_expr.SS_EXPR_EQ:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
System.out.println("EQEQ: L="+l1+" R="+r1+" and lr eq r1 = "+l1.equals(r1));
if (l1.equals(r1))
return "1";
else
return "0";
case SS_play_expr.SS_EXPR_NEQ:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (!l1.equals(r1))
return "1";
else
return "0";
case SS_play_expr.SS_EXPR_LT:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
if (a<b)
return "1";
else
return "0";
}
else
{
int d = l1.compareTo(r1);
if (d<0)
return "1";
else
return "0";
}
case SS_play_expr.SS_EXPR_GT:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
if (a>b)
return "1";
else
return "0";
}
else
{
int d = l1.compareTo(r1);
if (d>0)
return "1";
else
return "0";
}
case SS_play_expr.SS_EXPR_LE:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
if (a<=b)
return "1";
else
return "0";
}
else
{
int d = l1.compareTo(r1);
if (d<1)
return "1";
else
return "0";
}
case SS_play_expr.SS_EXPR_GE:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
if (a>=b)
return "1";
else
return "0";
}
else
{
int d = l1.compareTo(r1);
if (d>-1)
return "1";
else
return "0";
}
case SS_play_expr.SS_EXPR_PLUS:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
long c = a+b;
System.out.println("PLUS: L="+l1+"("+a+") R="+r1+"("+b+") => "+Long.toString(c)+"("+c+")");
return Long.toString(c);
}
else
{
System.out.println("PLUS: L="+l1+" R="+r1+ " => "+ l1+r1);
return l1 + r1; // Sounds slightly better than "error" in this case!
}
case SS_play_expr.SS_EXPR_MINUS:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
long c = a-b;
return Long.toString(c);
}
else
{
return "error";
}
case SS_play_expr.SS_EXPR_MULT:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
long c = a*b;
return Long.toString(c);
}
else
{
return "error";
}
case SS_play_expr.SS_EXPR_DIV:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
long c = a/b;
return Long.toString(c);
}
else
{
return "error";
}
case SS_play_expr.SS_EXPR_MOD:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
r1 = eval_play_expr(expr.Right, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if (isNumeric(l1) && isNumeric(r1))
{
long a = Long.parseLong(l1);
long b = Long.parseLong(r1);
long c = a%b;
return Long.toString(c);
}
else
{
return "error";
}
case SS_play_expr.SS_EXPR_NOT:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
if ( l1 == null || l1.equals("0"))
{
return "1";
}
else
{
return "0";
}
case SS_play_expr.SS_EXPR_PAREN:
l1 = eval_play_expr(expr.Left, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
System.out.println("PAREN: evaled: "+l1);
if( expr.Left != null) System.out.println("PAREN: Left type="+expr.Left.type);
return l1;
case SS_play_expr.SS_EXPR_STR_CONST:
return expr.str;
case SS_play_expr.SS_EXPR_CONCAT: /* I may remove this concept. A evolutionary left-over */
break;
case SS_play_expr.SS_EXPR_LEN:
return Integer.toString(str.length());
case SS_play_expr.SS_EXPR_NUM:
if (expr.range_type == SS_play_expr.SS_EXPR_RANGE)
{
String r1s = eval_play_expr(expr.range_start, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
String r2s = eval_play_expr(expr.range_end, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
// System.out.println("range start="+expr.range_start.type+" ("+r1s+"), and end ("+r2s+") is "+expr.range_end.type);
// System.out.flush();
int i1 = Integer.parseInt(r1s);
int i2 = Integer.parseInt(r2s);
System.out.println("substr('"+str+"'.substring("+i1+","+(i2+1)+") equeals: "+ str.substring(i1,i2+1));
return str.substring(i1,i2+1);
}
else if (expr.range_type == SS_play_expr.SS_EXPR_RANGE_START)
{
String r1s = eval_play_expr(expr.range_start, num, str, timeval, engine, itplay, filename, flist, log_list, script, stat, interrupts);
int ir1 = Integer.parseInt(r1s);
return str.substring(ir1);
}
else
{
return str;
}
case SS_play_expr.SS_EXPR_OPT:
// System.out.print("eval EXPR_OPT "+pexp.str + "and options="+options+"\n");
for (int z2 = 0; z2 < expr.str.length(); z2++)
{
char interesting_op = expr.str.charAt(z2);
if (options.indexOf(interesting_op) >= 0)
{
return Character.toString(interesting_op);
}
}
case SS_play_expr.SS_EXPR_TIME_SEC:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.SECOND);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_TIME_MIN:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.MINUTE);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_TIME_12HOUR:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.HOUR);
// System.out.println("**** 12HR Calendar.HOUR is: " + sec);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_TIME_24HOUR:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.HOUR_OF_DAY);
// System.out.println("**** 24HR Calendar.HOUR_OF_DAY is: " + sec);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_TIME_12HOUR2D:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.HOUR);
if (sec < 10)
return "0"+Integer.toString(sec);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_TIME_24HOUR2D:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.HOUR_OF_DAY);
if (sec < 10)
return "0"+Integer.toString(sec);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_TIME_AMPM:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.AM_PM);
if (sec == Calendar.AM)
return "a-m";
else
return "p-m";
case SS_play_expr.SS_EXPR_TIME_XM:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.AM_PM);
if (sec == Calendar.AM)
return "am";
else
return "pm";
case SS_play_expr.SS_EXPR_TIME_CM:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.AM_PM);
if (sec == Calendar.AM)
return "A";
else
return "P";
case SS_play_expr.SS_EXPR_TIME_TZ:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.ZONE_OFFSET);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_DATE_DOM:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.DAY_OF_MONTH);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_DATE_DOW:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.DAY_OF_WEEK) - 1; /* unix & java weeks start with sunday, but java is 1-7, unix is 0-6 */
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_DATE_MONTH:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.MONTH);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_DATE_DOWSTR:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.DAY_OF_WEEK);
switch(sec)
{
case Calendar.SUNDAY:
return "sun";
case Calendar.MONDAY:
return "mon";
case Calendar.TUESDAY:
return "tues";
case Calendar.WEDNESDAY:
return "wed";
case Calendar.THURSDAY:
return "thurs";
case Calendar.FRIDAY:
return "fri";
case Calendar.SATURDAY:
return "sat";
}
case SS_play_expr.SS_EXPR_DATE_MONTHSTR:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.MONTH);
switch(sec)
{
case Calendar.JANUARY:
return "jan";
case Calendar.FEBRUARY:
return "feb";
case Calendar.MARCH:
return "mar";
case Calendar.APRIL:
return "apr";
case Calendar.MAY:
return "may";
case Calendar.JUNE:
return "june";
case Calendar.JULY:
return "july";
case Calendar.AUGUST:
return "aug";
case Calendar.SEPTEMBER:
return "sep";
case Calendar.OCTOBER:
return "oct";
case Calendar.NOVEMBER:
return "nov";
case Calendar.DECEMBER:
return "dec";
}
case SS_play_expr.SS_EXPR_DATE_YEAR:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.YEAR);
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_DATE_CENT:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.YEAR) / 100;
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_DATE_DECADE:
cal = new GregorianCalendar();
cal.setTime(timeval);
sec = cal.get(Calendar.YEAR) % 100;
return Integer.toString(sec);
case SS_play_expr.SS_EXPR_TIMEVAL:
cal = new GregorianCalendar();
cal.setTime(timeval);
Date t = cal.getTime();
long t2 = t.getTime();
return Long.toString(t2);
case SS_play_expr.SS_EXPR_SUBCALL:
/* we could push the filename out to the flist right now? It'd be foolish
to have a filename get cut in half by a subcall. On the other hand,
users need to put some kind of silence between to flush the filename */
SS_script scr = engine.find_script(pref_lang_locale, expr.subcall_script_name);
/* put together a new context based on this one */
if (level > 150) // Highly arbitrary, but better than nothing!
{
SS_log log1 = new SS_log(-1, -1, "Recursion too deep! (150 levels!); aborting at script "+script.names.get(0).name, ", line "+ stat.lineno);
log_list.add(log1);
break;
}
SS_interp sub = new SS_interp(level+1);
/* should I copy the vardefs from this interp context into the sub-context? Do we want scripts
to have all the vardefs of the parent? */
if (options == null) options = "";
/* The new str value for the new subcall should be composed of subexpr's evaluated
in the current context, in a list from the subcallexprlist. */
ListIterator<SS_play_expr> itplay2 = expr.subcall_exprlist.listIterator();
StringBuffer filename2 = new StringBuffer();
play_exprs(engine, itplay2, filename2, flist, log_list, interrupts, script, stat);
String x = new String(filename2); /* copy of the current string; do we want the sub to hack the current str? */
String y = new String(options);
/* override options with the ones on the pexp */
if( expr.subcall_options != null )
y = expr.subcall_options;
Date z = new Date(timeval.getTime());