-
Notifications
You must be signed in to change notification settings - Fork 4
/
osc_expr_u.c
2139 lines (2017 loc) · 64.5 KB
/
osc_expr_u.c
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
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2011, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
/** \file osc_expr.c
\author John MacCallum
*/
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <inttypes.h>
#include <string.h>
#include <math.h>
#include "osc.h"
#include "osc_mem.h"
#include "osc_bundle_s.h"
#include "osc_bundle_iterator_u.h"
#include "osc_message_s.h"
#include "osc_message_u.h"
#include "osc_message_iterator_u.h"
#include "osc_atom_u.h"
#include "osc_atom_array_u.h"
#include "osc_hashtab.h"
#include "osc_util.h"
#include "osc_rset.h"
#include "osc_query.h"
#include "osc_typetag.h"
#include "osc_match.h"
#include "osc_expr_u.h"
#include "osc_expr.r"
#include "osc_expr_rec.h"
#include "osc_expr_rec.r"
#include "osc_expr_func.h"
#include "osc_expr_parser.h"
#include "osc_expr_scanner.h"
#include "osc_expr_privatedecls.h"
#include "osc_expr_u_privatedecls.h"
//#define __OSC_PROFILE__
#include "osc_profile.h"
static void osc_expr_err_badInfixArg(void *context, char *func, char typetag, int argnum, t_osc_atom_u *left, t_osc_atom_u *right)
{
long len = osc_atom_u_nformat(NULL, 0, left, 0);
char leftstr[len + 1];
osc_atom_u_nformat(leftstr, len + 1, left, 0);
len = osc_atom_u_nformat(NULL, 0, right, 0);
char rightstr[len + 1];
osc_atom_u_nformat(rightstr, len + 1, right, 0);
osc_error(context, OSC_ERR_EXPR_ARGCHK, "bad argument for expression %s %s %s. arg %d is a %s", leftstr, func, rightstr, argnum, osc_typetag_str(typetag));
}
static void osc_expr_err_unbound(void *context, char *address, char *func)
{
osc_error(context, OSC_ERR_EXPR_ADDRESSUNBOUND, "%s: address %s is unbound", func, address);
}
static void osc_expr_err_argnum(void *context, unsigned int expected, unsigned int found, unsigned int optional_args_allowed, char *func)
{
if(expected == found){
// come on
return;
}
char *errstr = NULL;
if(optional_args_allowed){
if(expected < found){
errstr = "%s: expected %d arguments but found %d\n";
}else{
errstr = "%s: expected at least %d arguments but found only %d\n";
}
}else{
errstr = "%s: expected %d arguments but found %d\n";
}
osc_error(context, OSC_ERR_EXPR_ARGCHK, errstr, func, expected, found);
}
t_osc_err osc_expr_u_lex(char *str, t_osc_atom_array_u **ar);
t_osc_hashtab *osc_expr_u_funcobj_ht;
void osc_expr_u_funcobj_dtor(char *key, void *val)
{
if(key){
osc_mem_free(key);
}
if(val){
osc_expr_free((t_osc_expr *)val);
}
}
int osc_expr_u_eval(t_osc_expr *f, t_osc_bndl_u *u_bndl, t_osc_atom_ar_u **out, void *context)
{
return osc_expr_u_evalInLexEnv(f, NULL, u_bndl, out, context);
}
int osc_expr_u_evalInLexEnv(t_osc_expr *f,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
// printf("%s context %p\n",__func__, context );
//////////////////////////////////////////////////
// Special functions
//////////////////////////////////////////////////
if(f->rec->func == osc_expr_apply){
return osc_expr_u_specFunc_apply(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_map){
return osc_expr_u_specFunc_map(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_lreduce || f->rec->func == osc_expr_rreduce){
return osc_expr_u_specFunc_reduce(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_assign){
return osc_expr_u_specFunc_assign(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_assign_to_index){
return osc_expr_u_specFunc_assigntoindex(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_if){
return osc_expr_u_specFunc_if(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_emptybundle){
return osc_expr_u_specFunc_emptybundle(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_bound){
return osc_expr_u_specFunc_bound(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_exists){
return osc_expr_u_specFunc_exists(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_getaddresses){
return osc_expr_u_specFunc_getaddresses(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_delete){
return osc_expr_u_specFunc_delete(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_getmsgcount){
return osc_expr_u_specFunc_getmsgcount(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_value){
return osc_expr_u_specFunc_value(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_quote){
return osc_expr_u_specFunc_quote(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_eval_call){
return osc_expr_u_specFunc_eval(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_tokenize){
return osc_expr_u_specFunc_tokenize(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_compile){
return osc_expr_u_specFunc_compile(f, lexenv, u_bndl, out, context);
/* }else if(f->rec->func == osc_expr_gettimetag){
return osc_expr_u_specFunc_gettimetag(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_settimetag){
return osc_expr_u_specFunc_settimetag(f, lexenv, u_bndl, out, context); */
}else if(f->rec->func == osc_expr_getbundlemember){
return osc_expr_u_specFunc_getBundleMember(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_assigntobundlemember){
return osc_expr_u_specFunc_assignToBundleMember(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_andalso){
return osc_expr_u_specFunc_andalso(f, lexenv, u_bndl, out, context);
}else if(f->rec->func == osc_expr_orelse){
return osc_expr_u_specFunc_orelse(f, lexenv, u_bndl, out, context);
}else{
//////////////////////////////////////////////////
// Call normal function
//////////////////////////////////////////////////
// printf("function call \n" );
long f_argc = osc_expr_getArgCount(f);
t_osc_expr_arg *f_argv = osc_expr_getArgs(f);
t_osc_atom_ar_u *argv[f_argc];
memset(argv, '\0', sizeof(argv));
int ret = 0;
int i = 0;
while(f_argv){
int ret = osc_expr_u_evalArgInLexEnv(f_argv, lexenv, u_bndl, argv + i, context);
if(ret){
if(ret == OSC_ERR_EXPR_ADDRESSUNBOUND){
// if the type arg type is something else, it will be an expression which means an
// error has already been posted
if(osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_OSCADDRESS){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv), osc_expr_rec_getName(osc_expr_getRec(f)));
}
}
int j;
for(j = 0; j < i + 1; j++){
if(argv[j]){
osc_atom_array_u_free(argv[j]);
}
}
return ret;
}
f_argv = f_argv->next;
i++;
}
ret = f->rec->func(f, f_argc, argv, out, context);
for(i = 0; i < f_argc; i++){
if(argv[i]){
osc_atom_array_u_free(argv[i]);
}
}
return ret;
}
return 1;
}
t_osc_err osc_expr_u_evalArgInLexEnv(t_osc_expr_arg *arg,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
if(!arg){
return 1;
}
switch(arg->type){
case OSC_EXPR_ARG_TYPE_ATOM:
{
if(lexenv && osc_atom_u_getTypetag(arg->arg.atom) == 's'){
t_osc_atom_ar_u *tmp = NULL;
if((tmp = osc_expr_lookupBindingInLexenv(lexenv, osc_atom_u_getStringPtr(arg->arg.atom)))){
*out = osc_atom_array_u_copy(tmp);
return 0;
}
}
*out = osc_atom_array_u_alloc(1);
t_osc_atom_u *a = osc_atom_array_u_get(*out, 0);
osc_atom_u_copyInto(&a, arg->arg.atom);
}
return 0;
case OSC_EXPR_ARG_TYPE_LIST:
{
*out = osc_atom_array_u_copy(arg->arg.list);
return 0;
}
case OSC_EXPR_ARG_TYPE_EXPR:
{
t_osc_err e = osc_expr_u_evalInLexEnv(arg->arg.expr, lexenv, u_bndl, out, context);
return e;
}
case OSC_EXPR_ARG_TYPE_OSCADDRESS:
{
*out = NULL;
if(!u_bndl){
return OSC_ERR_EXPR_ADDRESSUNBOUND;
}
t_osc_msg_u* msg = osc_bundle_u_getFirstFullMatch(u_bndl, arg->arg.osc_address);
if(msg){
*out = osc_message_u_getArgArrayCopy(msg);
return 0;
}
return OSC_ERR_EXPR_ADDRESSUNBOUND;
}
case OSC_EXPR_ARG_TYPE_FUNCTION:
*out = osc_atom_array_u_alloc(1);
osc_atom_u_setRec(osc_atom_array_u_get(*out, 0), (void *)osc_expr_arg_getFunction(arg));
return 0;
}
return OSC_ERR_INVAL; // this really shouldn't happen unless there's a bug somewhere
}
//////////////////////////////////////////////////
// Special functions
//////////////////////////////////////////////////
static int osc_expr_u_specFunc_apply(t_osc_expr *f,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
t_osc_expr_arg *f_argv = osc_expr_getArgs(f);
if((osc_expr_arg_getType(f_argv) != OSC_EXPR_ARG_TYPE_ATOM) &&
(osc_expr_arg_getType(f_argv) != OSC_EXPR_ARG_TYPE_FUNCTION) &&
(osc_expr_arg_getType(f_argv) != OSC_EXPR_ARG_TYPE_EXPR) &&
(osc_expr_arg_getType(f_argv) != OSC_EXPR_ARG_TYPE_OSCADDRESS)){
osc_error_handler(context,
basename(__FILE__),
__func__,
__LINE__,
OSC_ERR_EXPPARSE,
"the first argument to apply() should be a function, the name of a function, or an OSC address");
return 1;
}
if(osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_FUNCTION){
t_osc_expr_rec *r = osc_expr_arg_getFunction(f_argv);
if(!r){
osc_error_handler(context,
basename(__FILE__),
__func__,
__LINE__,
OSC_ERR_EXPPARSE,
"Error parsing the first argument to apply()");
return 1;
}
// arity check
t_osc_expr_lexenv *lexenv_copy = NULL;
if(lexenv){
osc_expr_copyLexenv(&lexenv_copy, lexenv);
}else{
lexenv_copy = osc_expr_makeLexenv();
}
int nparams = osc_expr_rec_getNumRequiredArgs(r);
char **params = osc_expr_rec_getRequiredArgsNames(r);
t_osc_expr_arg *arg = f_argv->next;
for(int i = 0; (i < nparams) && arg; i++){
t_osc_atom_ar_u *atoms = NULL;
t_osc_err e = osc_expr_u_evalArgInLexEnv(arg, lexenv, u_bndl, &atoms, context);
if(e == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(arg), "apply");
return e;
}
osc_expr_bindVarInLexenv(lexenv_copy, params[i], atoms);
arg = arg->next;
}
t_osc_expr *e = (t_osc_expr *)osc_expr_rec_getExtra(r);
while(e){
int ret = osc_expr_u_evalInLexEnv(e, lexenv_copy, u_bndl, out, context);
if(e->next){
osc_atom_array_u_free(*out);
*out = NULL;
}
if(ret){
osc_expr_destroyLexenv(lexenv_copy);
return ret;
}
e = osc_expr_next(e);
}
osc_expr_destroyLexenv(lexenv_copy);
}else if(osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_ATOM ||
osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_EXPR ||
osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_OSCADDRESS){
t_osc_expr_rec *r = NULL;
if(osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_ATOM){
if(osc_atom_u_getTypetag(osc_expr_arg_getOSCAtom(f_argv)) != 's'){
//error
return 1;
}
/*
t_osc_atom_ar_u *a = NULL;
osc_expr_u_evalArgInLexEnv(f_argv, lexenv, u_bndl, &a);
if(a){
t_osc_atom_u *aa = osc_atom_array_u_get(a, 0);
}
*/
r = osc_expr_lookupFunction(osc_atom_u_getStringPtr(osc_expr_arg_getOSCAtom(f_argv)));
if(!r){
// maybe we're in a lambda and should look it up in the lexenv
t_osc_atom_ar_u *xx = osc_expr_lookupBindingInLexenv(lexenv, osc_atom_u_getStringPtr(osc_expr_arg_getOSCAtom(f_argv)));
if(osc_atom_u_getTypetag(osc_atom_array_u_get(xx, 0)) == 'r'){
r = osc_atom_u_getRec(osc_atom_array_u_get(xx, 0));
t_osc_expr_arg *a = osc_expr_arg_alloc();
osc_expr_arg_setFunction(a, r);
osc_expr_arg_setNext(a, f_argv->next);
osc_expr_setArg(f, a);
int ret = osc_expr_u_specFunc_apply(f, lexenv, u_bndl, out, context);
//osc_expr_setArg(f, f_argv);
//osc_expr_arg_setNext(a, NULL);
//osc_expr_arg_free(a);
return ret;
}
//osc_atom_array_u_free(xx);
}
}else{
t_osc_atom_ar_u *ar = NULL;
t_osc_err e = osc_expr_u_evalArgInLexEnv(f_argv, lexenv, u_bndl, &ar, context);
if(e == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv), "apply");
return e;
}
if(!ar){
//error
return 1;
}
if(osc_atom_array_u_getLen(ar) != 1){
//error
return 1;
}
if(osc_atom_u_getTypetag(osc_atom_array_u_get(ar, 0)) != 's'){
//error
return 1;
}
char *stp = osc_atom_u_getStringPtr(osc_atom_array_u_get(ar, 0));
r = osc_expr_lookupFunction(stp);
if(!r){
// lookup didn't return a valid function, so let's see
// if we can parse this string.
t_osc_err err = osc_expr_parser_parseFunction(stp, &r, context);
if(!err && r){
t_osc_expr *e = NULL;
osc_expr_copy(&e, f);
t_osc_expr_arg *arg1 = osc_expr_getArgs(e);
t_osc_expr_arg *arg2 = osc_expr_arg_next(arg1);
switch(osc_expr_arg_getType(arg1)){
case OSC_EXPR_ARG_TYPE_ATOM:
osc_atom_u_free(osc_expr_arg_getOSCAtom(arg1));
break;
case OSC_EXPR_ARG_TYPE_EXPR:
osc_expr_free(osc_expr_arg_getExpr(arg1));
break;
case OSC_EXPR_ARG_TYPE_OSCADDRESS:
osc_mem_free(osc_expr_arg_getOSCAddress(arg1));
break;
}
osc_expr_arg_setFunction(arg1, r);
osc_expr_arg_setNext(arg1, arg2);
e->argv = arg1;
osc_expr_arg_setFunction(osc_expr_getArgs(e), r);
int ret = osc_expr_u_evalInLexEnv(e, lexenv, u_bndl, out, context);
osc_expr_free(e);
osc_atom_array_u_free(ar);
return ret;
}
}
osc_atom_array_u_free(ar);
}
if(!r){
osc_error_handler(context,
basename(__FILE__),
__func__,
__LINE__,
OSC_ERR_EXPPARSE,
"unrecognized function %s",
osc_atom_u_getStringPtr(osc_expr_arg_getOSCAtom(f_argv)));
return 1;
}
t_osc_expr *e = osc_expr_alloc();
osc_expr_setRec(e, r);
osc_expr_setArg(e, f_argv->next);
int ret = osc_expr_u_evalInLexEnv(e, lexenv, u_bndl, out, context);
e->argc = 0;
e->argv = NULL;
osc_expr_free(e);
return ret;
}else{
//error
return 1;
}
return 0;
}
static int osc_expr_u_specFunc_map(t_osc_expr *f,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
int f_argc = osc_expr_getArgCount(f);
if(f_argc < 2){
// error
return 1;
}
t_osc_expr_arg *f_argv = osc_expr_getArgs(f);
t_osc_expr_rec *r = osc_expr_lookupFunction("apply");
t_osc_expr *e = osc_expr_alloc();
osc_expr_setRec(e, r);
int ac = f_argc - 1;
t_osc_atom_ar_u *args[ac];
memset(args, '\0', ac * sizeof(t_osc_atom_ar_u *));
uint32_t min = ~0;
int i = 0;
t_osc_expr_arg *a = f_argv->next;
while(a){
t_osc_err err = osc_expr_u_evalArgInLexEnv(a, lexenv, u_bndl, args + i, context);
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(a), "map");
// assume whoever generated this will post an error
for(int j = 0; j < i; j++){
if(args[j]){
osc_atom_array_u_free(args[j]);
}
}
e->argv = NULL;
e->argc = 0;
osc_expr_free(e);
return err;
}
int count = osc_atom_array_u_getLen(args[i]);
if(count < min){
min = count;
}
i++;
a = a->next;
}
// arity check needs to go here
t_osc_expr_arg *func_args[ac + 1];
func_args[0] = NULL;
osc_expr_arg_copy(func_args, f_argv);
osc_expr_appendArg(e, *func_args);
for(i = 1; i < ac + 1; i++){
func_args[i] = osc_expr_arg_alloc();
osc_expr_appendArg(e, func_args[i]);
}
t_osc_atom_ar_u *output[min];
memset(output, '\0', min * sizeof(t_osc_atom_ar_u *));
int outcount = 0;
for(i = 0; i < min; i++){
int j;
for(j = 0; j < ac; j++){
osc_expr_arg_setOSCAtom(func_args[j + 1], osc_atom_array_u_get(args[j], i));
}
osc_expr_u_evalInLexEnv(e, lexenv, u_bndl, output + i, context);
outcount += osc_atom_array_u_getLen(output[i]);
}
*out = osc_atom_array_u_alloc(outcount);
int pos = 0;
for(i = 0; i < min; i++){
osc_atom_array_u_copyInto(out, output[i], pos);
pos += osc_atom_array_u_getLen(output[i]);
}
e->argv = NULL;
e->argc = 0;
osc_expr_free(e);
e = NULL;
if(func_args[0]){
osc_expr_arg_free(func_args[0]);
}
for(i = 0; i < ac; i++){
if(args[i]){
osc_atom_array_u_free(args[i]);
}
if(func_args[i + 1]){
// it looks like a bug to free a t_osc_expr_arg with osc_mem_free,
// but we don't want to free the underlying t_osc_atom_u because
// it will be freed when args is freed.
osc_mem_free(func_args[i + 1]);
}
}
for(i = 0; i < min; i++){
if(output[i]){
osc_atom_array_u_free(output[i]);
}
}
return 0;
}
static int osc_expr_u_specFunc_reduce(t_osc_expr *f,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
int f_argc = osc_expr_getArgCount(f);
if(f_argc < 2){
// error
return 1;
}
t_osc_expr_arg *f_argv = osc_expr_getArgs(f);
t_osc_expr_rec *r = osc_expr_lookupFunction("apply");
t_osc_expr *e = osc_expr_alloc();
osc_expr_setRec(e, r);
int ac = 3;
t_osc_expr_arg *func_args[ac]; // function, arg1, arg2
func_args[0] = NULL;
osc_expr_arg_copy(func_args, f_argv);
osc_expr_appendArg(e, *func_args);
for(int i = 1; i < ac; i++){
func_args[i] = osc_expr_arg_alloc();
osc_expr_appendArg(e, func_args[i]);
}
t_osc_atom_ar_u *args = NULL;
t_osc_expr_arg *a = f_argv->next;
int err = osc_expr_u_evalArgInLexEnv(a, lexenv, u_bndl, &args, context);
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(a),
f->rec->func == osc_expr_lreduce ? "lreduce" : "rreduce");
if(args){
osc_atom_array_u_free(args);
}
for(int i = 0; i < ac; i++){
if(func_args[i]){
osc_expr_arg_free(func_args[i]);
}
}
e->argv = NULL;
e->argc = 0;
osc_expr_free(e);
return err;
}
int count = osc_atom_array_u_getLen(args);
int arg0 = 1;
int arg1 = 2;
int delta = 1;
int start = 0;
if(f->rec->func == osc_expr_rreduce){
arg0 = 2;
arg1 = 1;
delta = -1;
start = count - 1;
}
if(count == 0){
osc_expr_u_evalInLexEnv(e, lexenv, u_bndl, out, context);
if(args){
osc_atom_array_u_free(args);
}
}else if(count == 1 && f_argc == 2){
*out = args;
}else{
t_osc_atom_ar_u *output = NULL;
if(f_argc == 3){
t_osc_atom_ar_u *sc = NULL;
t_osc_err err = osc_expr_u_evalArgInLexEnv(f_argv->next->next, lexenv, u_bndl, &sc, context);
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(a),
f->rec->func == osc_expr_lreduce ? "lreduce" : "rreduce");
if(args){
osc_atom_array_u_free(args);
}
for(int i = 0; i < ac; i++){
if(func_args[i]){
osc_expr_arg_free(func_args[i]);
}
}
e->argv = NULL;
e->argc = 0;
osc_expr_free(e);
return err;
}
if(sc){
t_osc_atom_u *acopy = NULL;
osc_atom_u_copyInto(&acopy, osc_atom_array_u_get(sc, 0));
osc_expr_arg_setOSCAtom(func_args[arg0], acopy);
if(sc){
osc_atom_array_u_free(sc);
}
}else{
//error
return 1;
}
osc_expr_arg_setOSCAtom(func_args[arg1], osc_atom_array_u_get(args, start));
start += delta;
}else{
osc_expr_arg_setOSCAtom(func_args[arg0], osc_atom_array_u_get(args, start));
start += delta;
osc_expr_arg_setOSCAtom(func_args[arg1], osc_atom_array_u_get(args, start));
start += delta;
}
osc_expr_u_evalInLexEnv(e, lexenv, u_bndl, &output, context);
for(int i = start; delta > 0 ? i < count : i >= 0; i += delta){
t_osc_atom_u *copy1 = NULL;
t_osc_atom_u *copy2 = NULL;
osc_atom_u_copyInto(©1, osc_atom_array_u_get(output, 0));
osc_atom_u_copyInto(©2, osc_atom_array_u_get(args, i));
osc_expr_arg_setOSCAtom(func_args[arg0], copy1);
osc_expr_arg_setOSCAtom(func_args[arg1], copy2);
osc_atom_array_u_free(output);
output = NULL;
osc_expr_u_evalInLexEnv(e, lexenv, u_bndl, &output, context);
osc_expr_arg_clear(func_args[arg0]);
osc_expr_arg_clear(func_args[arg1]);
}
*out = output;
if(args){
osc_atom_array_u_free(args);
}
}
e->argv = NULL;
e->argc = 0;
osc_expr_free(e);
if(func_args[0]){
osc_expr_arg_free(func_args[0]);
}
for(int i = 1; i < ac; i++){
if(func_args[i]){
// it looks like a bug to free a t_osc_expr_arg with osc_mem_free,
// but we don't want to free the underlying t_osc_atom_u because
// it will be freed when args is freed.
osc_mem_free(func_args[i]);
}
}
return 0;
}
static int osc_expr_u_specFunc_assign(t_osc_expr *f,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
if(!u_bndl){
return 1;
}
// printf("%s context %p\n",__func__, context );
t_osc_expr_arg *f_argv = osc_expr_getArgs(f);
t_osc_atom_ar_u *address_ar = NULL;
char *address = NULL;
if(osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_OSCADDRESS){
osc_util_strdup(&address, f_argv->arg.osc_address);
}else{
/*
assign() treats an address in its first argument slot differently in that it doesn't replace
it with its value---the address is the target of assignment. I.e., assign(/foo, 10) assigns
10 to /foo, not 10 to whatever /foo is bound to which is why assign() is a special function.
This means that the use of the value() function in the first slot of assign() function has
to be treated differently as well. Normally, value() would look up the value of the thing
bound to the address, i.e.,
/foo "/bar"
/bar 10
value(/foo) => 10
since /foo would be substituted for /bar before value() gets a hold of it. Here, we would want
one fewer level of resolution, so assign(value(/foo), ...) should assign something to /bar.
*/
t_osc_err err = OSC_ERR_NONE;
if(osc_expr_arg_getType(f_argv) == OSC_EXPR_ARG_TYPE_EXPR){
t_osc_expr *e = osc_expr_arg_getExpr(f_argv);
t_osc_expr_rec *r = osc_expr_getRec(e);
if(r){
if(!strcmp(osc_expr_rec_getName(r), "value")){
t_osc_expr_arg *value_args = osc_expr_getArgs(e);
if(value_args && osc_expr_arg_getType(value_args) == OSC_EXPR_ARG_TYPE_OSCADDRESS){
err = osc_expr_u_evalArgInLexEnv(value_args, lexenv, u_bndl, &address_ar, context);
// don't report error--we'll try again below
}
}
}
}
if(!address_ar){
err = osc_expr_u_evalArgInLexEnv(f_argv, lexenv, u_bndl, &address_ar, context);
}
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv), "=");
return err;
}
if(!address_ar){
return 1;
}
t_osc_atom_u *address_atom = osc_atom_array_u_get(address_ar, 0);
if(osc_atom_u_getTypetag(address_atom) != 's'){
osc_atom_array_u_free(address_ar);
osc_error(context, OSC_ERR_EXPR_ARGCHK, "the first argument to assign() should resolve to an OSC address");
return 1;
}
osc_atom_u_getString(address_atom, 0, &address);
osc_atom_array_u_free(address_ar);
}
t_osc_err err;
if((err = osc_error_validateAddress(address))){
return err;
}
t_osc_msg_u *mm = osc_message_u_alloc();
osc_message_u_setAddress(mm, address);
t_osc_err ret = osc_expr_u_evalArgInLexEnv(f_argv->next, lexenv, u_bndl, out, context);
if(ret){
if(ret == OSC_ERR_EXPR_ADDRESSUNBOUND)
{
int argtype = osc_expr_arg_getType(f_argv->next);
if( argtype == OSC_EXPR_ARG_TYPE_OSCADDRESS )
{
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv->next), "=");
}
/*
// we can assume that the inner expression printed an error
else if ( argtype == OSC_EXPR_ARG_TYPE_EXPR )
{
osc_error(context, OSC_ERR_EXPR_EVAL, "%s = %s : assignment from expression result is unbound", osc_expr_arg_getOSCAddress(f_argv), osc_expr_arg_getExpr(f_argv->next)->rec->name );
}*/
}
else
osc_error(context, OSC_ERR_EXPR_EVAL, NULL);
if(address){
osc_mem_free(address);
}
osc_message_u_free(mm);
return ret;
}
for(int i = 0; i < osc_atom_array_u_getLen(*out); i++){
t_osc_atom_u *cpy = NULL;
osc_atom_u_copyInto(&cpy, osc_atom_array_u_get(*out, i));
osc_message_u_appendAtom(mm, cpy);
}
ret = osc_bundle_u_replaceMessage( u_bndl, mm );
if(address){
osc_mem_free(address);
}
// message free is handled in replace message
// osc_message_u_free(mm);
return ret;
}
static int osc_expr_u_specFunc_assigntoindex(t_osc_expr *f,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
if(!u_bndl){
return 1;
}
int f_argc = osc_expr_getArgCount(f);
if(f_argc != 3){
return 1;
}
t_osc_expr_arg *f_argv = osc_expr_getArgs(f);
t_osc_msg_ar_u *msg_ar = osc_bundle_u_lookupAddress(u_bndl, f_argv->arg.osc_address, 1);
if(!msg_ar){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv), "assign to index =");
return 1;
}
t_osc_atom_ar_u *indexes = NULL;
t_osc_atom_ar_u *data = NULL;
t_osc_err err = 0;
// get data at target address
err = osc_expr_u_evalArgInLexEnv(f_argv, lexenv, u_bndl, out, context);
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv), "assign to index");
goto bail;
}
int outlen = osc_atom_array_u_getLen(*out);
err = osc_expr_u_evalArgInLexEnv(f_argv->next, lexenv, u_bndl, &indexes, context);
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND){
osc_error(context, OSC_ERR_EXPR_ADDRESSUNBOUND, "%s assign to index : address %s is unbound", osc_expr_arg_getOSCAddress(f_argv), osc_expr_arg_getOSCAddress(f_argv->next));
// osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv->next), "assign to index 2");
goto bail;
}
// get index(es)
int nindexes = osc_atom_array_u_getLen(indexes);
// get data
err = osc_expr_u_evalArgInLexEnv(f_argv->next->next, lexenv, u_bndl, &data, context);
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND){
//osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv->next->next), "assign to index 3");
osc_error(context, OSC_ERR_EXPR_ADDRESSUNBOUND, "%s assign to index : address %s is unbound", osc_expr_arg_getOSCAddress(f_argv), osc_expr_arg_getOSCAddress(f_argv->next->next));
goto bail;
}
int ndata = osc_atom_array_u_getLen(data);
if(nindexes == 0 || ndata == 0)
{
err = 1;
goto bail;
}
else if(nindexes == 1 && ndata > 1) // simple case, maybe try to optimize this
{
int idx = osc_atom_u_getInt(osc_atom_array_u_get(indexes, 0));
if(idx >= outlen || idx < 0)
{
osc_error(context, OSC_ERR_EXPR_EVAL, "%s : assign to index %d exceeds array length %d", osc_expr_arg_getOSCAddress(f_argv), idx, outlen);
err = 1;
goto bail;
}
t_osc_atom_u *dest = osc_atom_array_u_get(*out, idx);
osc_atom_u_copyInto( &dest, osc_atom_array_u_get(data, 0) );
}
else if(nindexes > 1 && ndata == 1)
{
int i, idx;
t_osc_atom_u *a = osc_atom_array_u_get(data, 0);
for(i = 0; i < nindexes; i++)
{
idx = osc_atom_u_getInt(osc_atom_array_u_get(indexes, i));
if(idx >= outlen || idx < 0)
{
osc_error(context, OSC_ERR_EXPR_EVAL, "%s : assign to index %d exceeds array length %d", osc_expr_arg_getOSCAddress(f_argv), idx, outlen);
continue;
}
t_osc_atom_u *dest = osc_atom_array_u_get(*out, idx);
osc_atom_u_copyInto(&dest, a);
}
}
else // nindexes > 1 && ndata > 1
{
int i, idx;
int n = osc_atom_array_u_getLen(indexes);
if(osc_atom_array_u_getLen(data) < n){
n = osc_atom_array_u_getLen(data);
}
for(i = 0; i < n; i++){
idx = osc_atom_u_getInt(osc_atom_array_u_get(indexes, i));
if(idx >= outlen || idx < 0){
osc_error(context, OSC_ERR_EXPR_EVAL, "%s : assign to index %d exceeds array length %d", osc_expr_arg_getOSCAddress(f_argv), idx, outlen);
continue;
}
t_osc_atom_u *dest = osc_atom_array_u_get(*out, idx);
osc_atom_u_copyInto(&dest, osc_atom_array_u_get(data, i));
}
}
// out now contains the atom array, but we need to copy it into the message
t_osc_msg_u *mm = osc_message_u_allocWithAddress( f_argv->arg.osc_address );
int i;
for(i = 0; i < osc_atom_array_u_getLen(*out); i++){
t_osc_atom_u *cpy = NULL;
osc_atom_u_copyInto(&cpy, osc_atom_array_u_get(*out, i));
osc_message_u_appendAtom(mm, cpy);
}
osc_bundle_u_replaceMessage(u_bndl, mm);
if (mm)
osc_message_u_free(mm);
err = 0;
bail:
if (indexes)
osc_atom_array_u_free(indexes);
if (data)
osc_atom_array_u_free(data);
return err;
}
static int osc_expr_u_specFunc_if(t_osc_expr *f,
t_osc_expr_lexenv *lexenv,
t_osc_bndl_u *u_bndl,
t_osc_atom_ar_u **out,
void *context)
{
int f_argc = osc_expr_getArgCount(f);
// printf("n args %d\n", f_argc);
if(f_argc < 2 || f_argc > 3){
osc_expr_err_argnum(context, 2, f_argc, 1, "osc_expr: if()");
return 1;
}
t_osc_expr_arg *f_argv = osc_expr_getArgs(f);
t_osc_atom_ar_u *argv = NULL;
t_osc_err err = osc_expr_u_evalArgInLexEnv(f_argv, lexenv, u_bndl, &argv, context);
if(err){
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND)
{
int argtype = osc_expr_arg_getType(f_argv);
if( argtype == OSC_EXPR_ARG_TYPE_OSCADDRESS )
{
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv), "if(): error evaluating test argument");
}
else
{
// we can assume that the inner expression printed an error
// printf("argytpe %i\n", argtype);
}
}
else
{
osc_error(context, OSC_ERR_EXPR_EVAL, "osc_expr if(): error evaluating test argument" );
}
//printf("err %llu %i %i\n", err, OSC_ERR_EXPR_ADDRESSUNBOUND, OSC_ERR_NOBUNDLE );
if(argv){
osc_atom_array_u_free(argv);
}
return err;
}
int test_result_len = osc_atom_array_u_getLen(argv);
t_osc_atom_ar_u *boolvec[test_result_len];
memset(boolvec, '\0', test_result_len * sizeof(t_osc_atom_ar_u *));
int outlen = 0;
int j = 0;
//for(int j = 0; j < osc_atom_array_u_getLen(argv); j++){
if(osc_atom_u_getInt32(osc_atom_array_u_get(argv, j))){
err = osc_expr_u_evalArgInLexEnv(f_argv->next, lexenv, u_bndl, boolvec + j, context);
if(err){
if(err == OSC_ERR_EXPR_ADDRESSUNBOUND)
{
int argtype = osc_expr_arg_getType(f_argv->next);
if( argtype == OSC_EXPR_ARG_TYPE_OSCADDRESS )
{
osc_expr_err_unbound(context, osc_expr_arg_getOSCAddress(f_argv->next), "if(): error evaluating \"then\" argument");