~ubuntu-branches/ubuntu/utopic/dicelab/utopic

« back to all changes in this revision

Viewing changes to tree.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Lemmen
  • Date: 2009-11-25 12:30:05 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091125123005-ylg50em2n998a232
Tags: 0.7-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
};
31
31
#line 23 "eval.tc"
32
32
 
 
33
struct val_list *list_new(size_t count, double prob) {
 
34
        struct val_list *ret = (struct val_list*)malloc(sizeof(struct val_list));
 
35
        ret->next = NULL;
 
36
        ret->prob = prob;
 
37
        ret->count = count;
 
38
        ret->values = count ? (int*)malloc(count * sizeof(int)): NULL;
 
39
    memset(ret->values, 0, count * sizeof(int));
 
40
        return ret;
 
41
}
 
42
 
33
43
// add cret to ret
34
44
void list_add(struct val_list **ret, struct val_list *cret, ordering_type
35
45
ordering) {
63
73
}
64
74
 
65
75
struct val_list *error_val() {
66
 
        struct val_list *ret = (struct val_list*)malloc(sizeof(struct val_list));
67
 
        ret->values = (int*)malloc(sizeof(int));
 
76
        struct val_list *ret = list_new(1, 1.0);
68
77
        ret->values[0] = 0;
69
 
        ret->count = 1;
70
 
        ret->prob = 1.0;
71
 
        ret->next = NULL;
72
78
        return ret;
73
79
}
74
80
 
80
86
                free(cur);
81
87
        }
82
88
}
83
 
#line 786 "eval.tc"
 
89
#line 770 "eval.tc"
84
90
 
85
91
void rec_whiledo(struct val_list *v, char *varname, struct symtab *symtab, 
86
92
                expression *expr, int *data, int count, struct val_list **ret,
113
119
                        }
114
120
                        // recursive call
115
121
                        rec_whiledo(x, varname, symtab, expr, cdata, ccount, ret, ordering);
116
 
                        // free the symtab
 
122
                        // free the data, symtab and list
 
123
                        free(cdata);
117
124
                        free_roll(nst->rvalue);
118
125
                        free(nst);
 
126
                        list_free(x);
119
127
                }
120
128
                else {
121
129
                        // add to final list
122
 
                        struct val_list *cur = (struct val_list*)malloc(sizeof(struct val_list));
123
 
                        cur->next = NULL;
124
 
                        cur->prob = cv->prob;
125
 
                        cur->count = ccount;
 
130
                        struct val_list *cur = list_new(ccount, cv->prob);
 
131
                        free(cur->values);
126
132
                        cur->values = cdata;
127
133
                        list_add(ret, cur, ordering);
128
134
                }
130
136
                cv = cv->next;
131
137
        }
132
138
}
133
 
#line 972 "eval.tc"
 
139
#line 937 "eval.tc"
134
140
 
135
141
void cb_perm(int *data, int count, void *arg, float farg) {
136
142
        struct val_list **pret = (struct val_list**)arg;
137
 
        struct val_list *current = (struct val_list*)malloc(sizeof(struct val_list));
138
 
        current->count = count;
139
 
        current->values = (int*)malloc(current->count * sizeof(int));
 
143
        struct val_list *current = list_new(count, farg);
140
144
        memcpy(current->values, data, current->count * sizeof(int));
141
 
        current->next = NULL;
142
 
        current->prob = farg;
143
145
        // XXX pass this from the tree instead of using caring all the time
144
146
        // or even better, don't do the permutations if we don't care about the
145
147
        // ordering
146
148
        list_add(pret, current, caring);
147
149
}
148
 
#line 149 "tree.c"
 
150
#line 3 "optimize.tc"
 
151
 
 
152
 
 
153
#include <values.h>
 
154
 
 
155
#ifndef MINFLOAT
 
156
#define MINFLOAT        0.0000001
 
157
#endif
 
158
 
 
159
// this is taken from http://en.wikipedia.org/wiki/Dice
 
160
// calculates the probability for value "pos" in the sum of "num" 
 
161
// dice with "sides" faces
 
162
double Fsumdice(double *cache, int mnum, int num, int sides, int pos) {
 
163
        if (pos > ((num*sides-num)/2)+num) {
 
164
                pos = num*sides - pos + num;
 
165
        }
 
166
 
 
167
        if (num == 1) {
 
168
                if ((pos < 1) || (pos > sides)) {
 
169
                        return 0.0;
 
170
                }
 
171
                return 1.0/sides;
 
172
        }
 
173
        else {
 
174
                if (pos < num) {
 
175
                        return 0.0;
 
176
                }
 
177
                int i;
 
178
                if (cache[mnum*(pos-num)+num] < -0.5) {
 
179
                        double ret = 0;
 
180
                        for (i = 1; i < min(pos, sides+1); i++) {
 
181
                                ret += Fsumdice(cache, mnum, 1, sides, i) 
 
182
                                        * Fsumdice(cache, mnum, num-1, sides, pos-i); 
 
183
                        }
 
184
                        cache[mnum*(pos-num)+num] = ret;
 
185
                        return ret;
 
186
                }
 
187
                return cache[mnum*(pos-num)+num];
 
188
        }
 
189
}
 
190
 
 
191
// calculates the probability that the sum of "num" rolls of 
 
192
// distribution "dist" have the value "pos"
 
193
double Fsumany(double *cache, int mnum, int minval, int maxval, int num,
 
194
        struct val_list *dist, int pos) {
 
195
    struct val_list *cdist = dist;
 
196
    if (num == 1) {
 
197
        while (cdist) {
 
198
            if (cdist->values[0] == pos) {
 
199
                return cdist->prob;
 
200
            }
 
201
            cdist = cdist->next;
 
202
        }
 
203
        return 0.0;
 
204
    }
 
205
    else {
 
206
        int cpos = pos*mnum+num;
 
207
        if ((pos < minval * num) || (pos > maxval * pos)) {
 
208
            return 0.0;
 
209
        }
 
210
        if (cache[cpos] < -0.5) {
 
211
                    double ret = 0.0;
 
212
            while (cdist) {
 
213
                ret += cdist->prob * Fsumany(cache, mnum, minval, maxval,
 
214
                    num - 1, dist, pos - cdist->values[0]);
 
215
                cdist = cdist->next;
 
216
            }
 
217
            cache[cpos] = ret;
 
218
            return ret;
 
219
        }
 
220
        return cache[cpos];
 
221
    }
 
222
}
 
223
 
 
224
#line 225 "tree.c"
149
225
 
150
226
#define YYNODESTATE_TRACK_LINES 1
151
227
#line 1 "c_skel.c"
413
489
        state__->push_stack__ = 0;
414
490
        state__->used__ = 0;
415
491
}
416
 
#line 417 "tree.c"
 
492
#line 493 "tree.c"
417
493
void expression_set_ordering__(expression *this, ordering_type ordering)
418
494
#line 7 "ordering.tc"
419
495
{
420
496
        this->ordering = ordering;
421
497
}
422
 
#line 423 "tree.c"
 
498
#line 499 "tree.c"
 
499
 
 
500
expression * expression_optimize__(expression *this)
 
501
#line 225 "optimize.tc"
 
502
{
 
503
        return this;
 
504
}
 
505
#line 506 "tree.c"
423
506
 
424
507
struct expression_vtable__ const expression_vt__ = {
425
508
        0,
430
513
        (void (*)(expression *this__, struct symtab * st))0,
431
514
        (void (*)(expression *this__, ordering_type ordering))expression_set_ordering__,
432
515
        (struct val_list * (*)(expression *this__))0,
 
516
        (expression * (*)(expression *this__))expression_optimize__,
433
517
};
434
518
 
435
519
void elist_printtree__(elist *this, int depth)
438
522
        indent(depth);
439
523
        printf("empty list\n");
440
524
}
441
 
#line 442 "tree.c"
 
525
#line 526 "tree.c"
442
526
 
443
527
struct roll_value * elist_roll__(elist *this)
444
528
#line 4 "roll.tc"
447
531
        rv->count = 0;
448
532
        return rv;
449
533
}
450
 
#line 451 "tree.c"
 
534
#line 535 "tree.c"
451
535
 
452
536
void elist_set_symtab__(elist *this, struct symtab * st)
453
537
#line 20 "symtab.tc"
454
538
{
455
539
}
456
 
#line 457 "tree.c"
 
540
#line 541 "tree.c"
457
541
 
458
542
struct val_list * elist_eval__(elist *this)
459
 
#line 77 "eval.tc"
 
543
#line 83 "eval.tc"
460
544
{
461
 
        struct val_list *cret = (struct val_list*)
462
 
                malloc(sizeof(struct val_list));
463
 
        cret->next = NULL;
464
 
        cret->values = NULL;
465
 
        cret->count = 0;
466
 
        cret->prob = 1;
467
 
        return cret;
 
545
        return list_new(0, 1.0);
468
546
}
469
 
#line 470 "tree.c"
 
547
#line 548 "tree.c"
470
548
 
471
549
struct elist_vtable__ const elist_vt__ = {
472
550
        &expression_vt__,
477
555
        (void (*)(expression *this__, struct symtab * st))elist_set_symtab__,
478
556
        (void (*)(expression *this__, ordering_type ordering))expression_set_ordering__,
479
557
        (struct val_list * (*)(expression *this__))elist_eval__,
 
558
        (expression * (*)(expression *this__))expression_optimize__,
480
559
};
481
560
 
482
561
void binary_set_symtab__(binary *this, struct symtab * st)
486
565
        set_symtab(this->expr1, st);
487
566
        set_symtab(this->expr2, st);
488
567
}
489
 
#line 490 "tree.c"
 
568
#line 569 "tree.c"
490
569
 
491
570
void binary_set_ordering__(binary *this, ordering_type ordering)
492
571
#line 18 "ordering.tc"
495
574
        set_ordering(this->expr1, ordering);
496
575
        set_ordering(this->expr2, ordering);
497
576
}
498
 
#line 499 "tree.c"
 
577
#line 578 "tree.c"
 
578
 
 
579
expression * binary_optimize__(binary *this)
 
580
#line 230 "optimize.tc"
 
581
{
 
582
        this->expr1 = optimize(this->expr1);
 
583
        this->expr2 = optimize(this->expr2);
 
584
        return (expression*)this;
 
585
}
 
586
#line 587 "tree.c"
499
587
 
500
588
struct binary_vtable__ const binary_vt__ = {
501
589
        &expression_vt__,
506
594
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
507
595
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
508
596
        (struct val_list * (*)(expression *this__))0,
 
597
        (expression * (*)(expression *this__))binary_optimize__,
509
598
};
510
599
 
511
600
void unary_set_symtab__(unary *this, struct symtab * st)
514
603
        this->symtab = st;
515
604
        set_symtab(this->expr, st);
516
605
}
517
 
#line 518 "tree.c"
 
606
#line 607 "tree.c"
518
607
 
519
608
void unary_set_ordering__(unary *this, ordering_type ordering)
520
609
#line 12 "ordering.tc"
522
611
        this->ordering = ordering;
523
612
        set_ordering(this->expr, ordering);
524
613
}
525
 
#line 526 "tree.c"
 
614
#line 615 "tree.c"
 
615
 
 
616
expression * unary_optimize__(unary *this)
 
617
#line 237 "optimize.tc"
 
618
{
 
619
        this->expr = optimize(this->expr);
 
620
        return (expression*)this;
 
621
}
 
622
#line 623 "tree.c"
526
623
 
527
624
struct unary_vtable__ const unary_vt__ = {
528
625
        &expression_vt__,
533
630
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
534
631
        (void (*)(expression *this__, ordering_type ordering))unary_set_ordering__,
535
632
        (struct val_list * (*)(expression *this__))0,
 
633
        (expression * (*)(expression *this__))unary_optimize__,
536
634
};
537
635
 
538
636
void number_printtree__(number *this, int depth)
541
639
        indent(depth);
542
640
        printf("%i\n", this->num);
543
641
}
544
 
#line 545 "tree.c"
 
642
#line 643 "tree.c"
545
643
 
546
644
struct roll_value * number_roll__(number *this)
547
645
#line 11 "roll.tc"
549
647
        struct roll_value *rv = new_roll_single(this->num);
550
648
        return rv;
551
649
}
552
 
#line 553 "tree.c"
 
650
#line 651 "tree.c"
553
651
 
554
652
void number_set_symtab__(number *this, struct symtab * st)
555
653
#line 24 "symtab.tc"
556
654
{
557
655
}
558
 
#line 559 "tree.c"
 
656
#line 657 "tree.c"
559
657
 
560
658
struct val_list * number_eval__(number *this)
561
 
#line 1044 "eval.tc"
 
659
#line 1005 "eval.tc"
562
660
{
563
 
        struct val_list *ret = (struct val_list*)malloc(sizeof(struct val_list));
564
 
        ret->next = NULL;
565
 
        ret->values = (int*)malloc(sizeof(int));
 
661
        struct val_list *ret = list_new(1, 1.0);
566
662
        ret->values[0] = this->num;
567
 
        ret->count = 1;
568
 
        ret->prob = 1;
569
663
        return ret;
570
664
}
571
 
#line 572 "tree.c"
 
665
#line 666 "tree.c"
572
666
 
573
667
struct number_vtable__ const number_vt__ = {
574
668
        &expression_vt__,
579
673
        (void (*)(expression *this__, struct symtab * st))number_set_symtab__,
580
674
        (void (*)(expression *this__, ordering_type ordering))expression_set_ordering__,
581
675
        (struct val_list * (*)(expression *this__))number_eval__,
 
676
        (expression * (*)(expression *this__))expression_optimize__,
582
677
};
583
678
 
584
679
void ifthenelse_printtree__(ifthenelse *this, int depth)
594
689
        printf("else\n");
595
690
        printtree(this->else_expr, depth+1);
596
691
}
597
 
#line 598 "tree.c"
 
692
#line 693 "tree.c"
598
693
 
599
694
struct roll_value * ifthenelse_roll__(ifthenelse *this)
600
 
#line 499 "roll.tc"
 
695
#line 486 "roll.tc"
601
696
{
602
697
        struct roll_value *if_expr = roll(this->if_expr);
603
698
        if (if_expr->count > 0) {
609
704
                return roll(this->else_expr);
610
705
        }
611
706
}
612
 
#line 613 "tree.c"
 
707
#line 708 "tree.c"
613
708
 
614
709
void ifthenelse_set_symtab__(ifthenelse *this, struct symtab * st)
615
710
#line 46 "symtab.tc"
619
714
        set_symtab(this->then_expr, st);
620
715
        set_symtab(this->else_expr, st);
621
716
}
622
 
#line 623 "tree.c"
 
717
#line 718 "tree.c"
623
718
 
624
719
void ifthenelse_set_ordering__(ifthenelse *this, ordering_type ordering)
625
720
#line 69 "ordering.tc"
629
724
    set_ordering(this->then_expr, ordering);
630
725
        set_ordering(this->else_expr, ordering);
631
726
}
632
 
#line 633 "tree.c"
 
727
#line 728 "tree.c"
633
728
 
634
729
struct val_list * ifthenelse_eval__(ifthenelse *this)
635
 
#line 1055 "eval.tc"
 
730
#line 1012 "eval.tc"
636
731
{
637
732
        struct val_list *cnt = eval(this->if_expr);
638
733
        struct val_list *ccnt = cnt;
656
751
                }
657
752
                ccnt = ccnt->next;
658
753
        }
 
754
        list_free(cnt);
659
755
        return ret;
660
756
}
661
 
#line 662 "tree.c"
 
757
#line 758 "tree.c"
 
758
 
 
759
expression * ifthenelse_optimize__(ifthenelse *this)
 
760
#line 243 "optimize.tc"
 
761
{
 
762
        this->if_expr = optimize(this->if_expr);
 
763
        this->then_expr = optimize(this->then_expr);
 
764
        this->else_expr = optimize(this->else_expr);
 
765
        return (expression*)this;
 
766
}
 
767
#line 768 "tree.c"
662
768
 
663
769
struct ifthenelse_vtable__ const ifthenelse_vt__ = {
664
770
        &expression_vt__,
669
775
        (void (*)(expression *this__, struct symtab * st))ifthenelse_set_symtab__,
670
776
        (void (*)(expression *this__, ordering_type ordering))ifthenelse_set_ordering__,
671
777
        (struct val_list * (*)(expression *this__))ifthenelse_eval__,
 
778
        (expression * (*)(expression *this__))ifthenelse_optimize__,
672
779
};
673
780
 
674
781
void variable_printtree__(variable *this, int depth)
677
784
        indent(depth);
678
785
        printf("%s\n", this->varname);
679
786
}
680
 
#line 681 "tree.c"
 
787
#line 788 "tree.c"
681
788
 
682
789
struct roll_value * variable_roll__(variable *this)
683
 
#line 527 "roll.tc"
 
790
#line 514 "roll.tc"
684
791
{
685
792
        struct symtab *nst = this->symtab;
686
793
        struct roll_value *ret = NULL;
708
815
        }
709
816
        return ret;
710
817
}
711
 
#line 712 "tree.c"
 
818
#line 819 "tree.c"
712
819
 
713
820
void variable_set_symtab__(variable *this, struct symtab * st)
714
821
#line 28 "symtab.tc"
715
822
{
716
823
        this->symtab = st;
717
824
}
718
 
#line 719 "tree.c"
 
825
#line 826 "tree.c"
719
826
 
720
827
void variable_set_ordering__(variable *this, ordering_type ordering)
721
828
#line 77 "ordering.tc"
730
837
                nst = nst->next;
731
838
        }
732
839
 
733
 
        if (nst) {
 
840
        if (nst && nst->ordering == agnostic) {
734
841
                nst->ordering = ordering;
735
842
        }
736
843
}
737
 
#line 738 "tree.c"
 
844
#line 845 "tree.c"
738
845
 
739
846
struct val_list * variable_eval__(variable *this)
740
 
#line 1082 "eval.tc"
 
847
#line 1040 "eval.tc"
741
848
{
742
849
        struct symtab *nst = this->symtab;
743
850
        struct val_list *ret = NULL;
744
851
        while (nst != NULL) {
745
852
                if (strcmp(this->varname, nst->name) == 0) {
746
 
                        ret = (struct val_list*)malloc(sizeof(struct val_list));
747
 
                        ret->next = NULL;
748
 
                        ret->values = (int*)malloc(sizeof(int) * nst->rvalue->count);
749
 
                        ret->count = nst->rvalue->count;
 
853
                        ret = list_new(nst->rvalue->count, 1.0);
750
854
                        memcpy(ret->values, nst->rvalue->values, sizeof(int) * nst->rvalue->count);
751
 
                        ret->prob = 1;
752
855
                        break;
753
856
                }
754
857
                nst = nst->next;
762
865
        }
763
866
        return ret;
764
867
}
765
 
#line 766 "tree.c"
 
868
#line 869 "tree.c"
766
869
 
767
870
struct variable_vtable__ const variable_vt__ = {
768
871
        &expression_vt__,
773
876
        (void (*)(expression *this__, struct symtab * st))variable_set_symtab__,
774
877
        (void (*)(expression *this__, ordering_type ordering))variable_set_ordering__,
775
878
        (struct val_list * (*)(expression *this__))variable_eval__,
 
879
        (expression * (*)(expression *this__))expression_optimize__,
 
880
};
 
881
 
 
882
void sumrepdice_printtree__(sumrepdice *this, int depth)
 
883
#line 89 "optimize.tc"
 
884
{
 
885
        // unsupported, only used in eval
 
886
}
 
887
#line 888 "tree.c"
 
888
 
 
889
struct roll_value * sumrepdice_roll__(sumrepdice *this)
 
890
#line 85 "optimize.tc"
 
891
{
 
892
        // unsupported, only used in eval
 
893
}
 
894
#line 895 "tree.c"
 
895
 
 
896
void sumrepdice_set_symtab__(sumrepdice *this, struct symtab * st)
 
897
#line 93 "optimize.tc"
 
898
{
 
899
        // not needed, don't use symtab
 
900
}
 
901
#line 902 "tree.c"
 
902
 
 
903
struct val_list * sumrepdice_eval__(sumrepdice *this)
 
904
#line 97 "optimize.tc"
 
905
{
 
906
        int n;
 
907
        struct val_list *ret = NULL;
 
908
        struct val_list *cnum = this->num_dice;
 
909
        while (cnum) {
 
910
                if (cnum->count != 1) {
 
911
                        yyerror("Argument 1 to sumrep operator is not scalar");
 
912
                        list_free(this->num_dice);
 
913
                        list_free(ret);
 
914
                        return error_val();
 
915
                }
 
916
                int num_dice_val = cnum->values[0];
 
917
        if (num_dice_val > 0) {
 
918
            int expected = num_dice_val*
 
919
                ((num_dice_val*this->num_sides-num_dice_val)/2)
 
920
                +num_dice_val+1;
 
921
            double *cache = malloc(sizeof(double) * expected);
 
922
            for (n = 0; n < expected; n++) {
 
923
                cache[n] = -1.0;
 
924
            }
 
925
            for (n = num_dice_val * this->num_sides; 
 
926
                    n >= num_dice_val; n--) {
 
927
                struct val_list *cret = 
 
928
                    (struct val_list*)malloc(sizeof(struct val_list));
 
929
                cret->next = NULL;
 
930
                cret->count = 1;
 
931
                cret->prob = Fsumdice(cache, num_dice_val, num_dice_val, 
 
932
                    this->num_sides, n) * cnum->prob;
 
933
                cret->values = (int*)malloc(sizeof(int));
 
934
                cret->values[0] = n;
 
935
                list_add(&ret, cret, this->ordering);
 
936
            } 
 
937
            free(cache);
 
938
        }
 
939
                cnum = cnum->next;
 
940
        }
 
941
        if (ret == NULL) {
 
942
                return list_new(1, 1.0);
 
943
        }
 
944
        return ret;
 
945
}
 
946
#line 947 "tree.c"
 
947
 
 
948
struct sumrepdice_vtable__ const sumrepdice_vt__ = {
 
949
        &expression_vt__,
 
950
        sumrepdice_kind,
 
951
        "sumrepdice",
 
952
        (void (*)(expression *this__, int depth))sumrepdice_printtree__,
 
953
        (struct roll_value * (*)(expression *this__))sumrepdice_roll__,
 
954
        (void (*)(expression *this__, struct symtab * st))sumrepdice_set_symtab__,
 
955
        (void (*)(expression *this__, ordering_type ordering))expression_set_ordering__,
 
956
        (struct val_list * (*)(expression *this__))sumrepdice_eval__,
 
957
        (expression * (*)(expression *this__))expression_optimize__,
 
958
};
 
959
 
 
960
void sumrepany_printtree__(sumrepany *this, int depth)
 
961
#line 149 "optimize.tc"
 
962
{
 
963
        // unsupported, only used in eval
 
964
}
 
965
#line 966 "tree.c"
 
966
 
 
967
struct roll_value * sumrepany_roll__(sumrepany *this)
 
968
#line 145 "optimize.tc"
 
969
{
 
970
        // unsupported, only used in eval
 
971
}
 
972
#line 973 "tree.c"
 
973
 
 
974
void sumrepany_set_symtab__(sumrepany *this, struct symtab * st)
 
975
#line 153 "optimize.tc"
 
976
{
 
977
        // not needed, don't use symtab
 
978
}
 
979
#line 980 "tree.c"
 
980
 
 
981
struct val_list * sumrepany_eval__(sumrepany *this)
 
982
#line 157 "optimize.tc"
 
983
{
 
984
        int n;
 
985
        struct val_list *ret = NULL;
 
986
        struct val_list *cnum = this->number;
 
987
    int minval = -1;
 
988
    int maxval = 0;
 
989
    struct val_list *cval = this->data;
 
990
    while (cval) {
 
991
        if ((minval == -1) || (cval->values[0] < minval)) {
 
992
            minval = cval->values[0];
 
993
        }
 
994
        if (cval->values[0] > maxval) {
 
995
            maxval = cval->values[0];
 
996
        }
 
997
        cval = cval->next;
 
998
    }
 
999
        while (cnum) {
 
1000
                if (cnum->count != 1) {
 
1001
                        yyerror("Argument 1 to sumrep operator is not scalar");
 
1002
                        list_free(this->number);
 
1003
                        list_free(this->data);
 
1004
                        list_free(ret);
 
1005
                        return error_val();
 
1006
                }
 
1007
        if (cnum->values[0] == 0) {
 
1008
            struct val_list *cret = 
 
1009
                (struct val_list*)malloc(sizeof(struct val_list));
 
1010
            cret->next = NULL;
 
1011
            cret->count = 1;
 
1012
            cret->prob = cnum->prob;
 
1013
            cret->values = (int*)malloc(sizeof(int));
 
1014
            cret->values[0] = 0;
 
1015
            list_add(&ret, cret, this->ordering);
 
1016
        }
 
1017
        else {
 
1018
            int expected = (cnum->values[0] * maxval + 1) * cnum->values[0] + 1;
 
1019
            double *cache = malloc(sizeof(double) * expected);
 
1020
            for (n = 0; n < expected; n++) {
 
1021
                 cache[n] = -1.0;
 
1022
            }
 
1023
            for (n = cnum->values[0] * minval; n <= cnum->values[0] * maxval; n++) {
 
1024
                struct val_list *cret = 
 
1025
                    (struct val_list*)malloc(sizeof(struct val_list));
 
1026
                cret->next = NULL;
 
1027
                cret->count = 1;
 
1028
                cret->prob = Fsumany(cache, cnum->values[0], minval, maxval,
 
1029
                    cnum->values[0], this->data, n) * cnum->prob;
 
1030
                cret->values = (int*)malloc(sizeof(int));
 
1031
                cret->values[0] = n;
 
1032
                if (cret->prob > MINFLOAT) { 
 
1033
                    list_add(&ret, cret, this->ordering);
 
1034
                }
 
1035
                else {
 
1036
                    list_free(cret);    
 
1037
                }
 
1038
            }
 
1039
            free(cache);
 
1040
        }
 
1041
                cnum = cnum->next;
 
1042
    }
 
1043
        if (ret == NULL) {
 
1044
                return list_new(1, 1.0);
 
1045
        }
 
1046
        return ret;
 
1047
}
 
1048
#line 1049 "tree.c"
 
1049
 
 
1050
struct sumrepany_vtable__ const sumrepany_vt__ = {
 
1051
        &expression_vt__,
 
1052
        sumrepany_kind,
 
1053
        "sumrepany",
 
1054
        (void (*)(expression *this__, int depth))sumrepany_printtree__,
 
1055
        (struct roll_value * (*)(expression *this__))sumrepany_roll__,
 
1056
        (void (*)(expression *this__, struct symtab * st))sumrepany_set_symtab__,
 
1057
        (void (*)(expression *this__, ordering_type ordering))expression_set_ordering__,
 
1058
        (struct val_list * (*)(expression *this__))sumrepany_eval__,
 
1059
        (expression * (*)(expression *this__))expression_optimize__,
776
1060
};
777
1061
 
778
1062
void mathop_printtree__(mathop *this, int depth)
805
1089
        printtree(this->expr1, depth+1);
806
1090
        printtree(this->expr2, depth+1);
807
1091
}
808
 
#line 809 "tree.c"
 
1092
#line 1093 "tree.c"
809
1093
 
810
1094
struct roll_value * mathop_roll__(mathop *this)
811
1095
#line 17 "roll.tc"
853
1137
        free_roll(rv2);
854
1138
        return rv1;
855
1139
}
856
 
#line 857 "tree.c"
 
1140
#line 1141 "tree.c"
857
1141
 
858
1142
struct val_list * mathop_eval__(mathop *this)
859
1143
#line 88 "eval.tc"
879
1163
                                list_free(e2);
880
1164
                                return error_val();
881
1165
                        }
882
 
                        struct val_list *cret = (struct val_list*)
883
 
                                malloc(sizeof(struct val_list));
884
 
                        cret->next = NULL;
885
 
                        cret->values = (int*)malloc(sizeof(int));
 
1166
                        struct val_list *cret = list_new(1, ce1->prob * ce2->prob);
886
1167
                        switch (yykind(this)) {
887
1168
                                case plus_kind:
888
1169
                                        cret->values[0] = ce1->values[0] + ce2->values[0];
908
1189
                                        list_free(e2);
909
1190
                                        return error_val();
910
1191
                        }
911
 
                        cret->count = 1;
912
 
                        cret->prob = ce1->prob * ce2->prob;
913
1192
                        list_add(&ret, cret, this->ordering);
914
1193
                        ce2 = ce2->next;
915
1194
                }
919
1198
        list_free(e2);
920
1199
        return ret;
921
1200
}
922
 
#line 923 "tree.c"
 
1201
#line 1202 "tree.c"
923
1202
 
924
1203
struct mathop_vtable__ const mathop_vt__ = {
925
1204
        &binary_vt__,
930
1209
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
931
1210
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
932
1211
        (struct val_list * (*)(expression *this__))mathop_eval__,
 
1212
        (expression * (*)(expression *this__))binary_optimize__,
933
1213
};
934
1214
 
935
1215
void scat_printtree__(scat *this, int depth)
940
1220
        printtree(this->expr1, depth+1);
941
1221
        printtree(this->expr2, depth+1);
942
1222
}
943
 
#line 944 "tree.c"
 
1223
#line 1224 "tree.c"
944
1224
 
945
1225
struct roll_value * scat_roll__(scat *this)
946
1226
#line 64 "roll.tc"
970
1250
        free_roll(rv2);
971
1251
        return rv1;
972
1252
}
973
 
#line 974 "tree.c"
 
1253
#line 1254 "tree.c"
974
1254
 
975
1255
struct val_list * scat_eval__(scat *this)
976
 
#line 153 "eval.tc"
 
1256
#line 148 "eval.tc"
977
1257
{
978
1258
        struct val_list *e1 = eval(this->expr1);
979
1259
        struct val_list *e2 = eval(this->expr2);
996
1276
                                list_free(e2);
997
1277
                                return error_val();
998
1278
                        }
999
 
                        struct val_list *cret = (struct val_list*)
1000
 
                                malloc(sizeof(struct val_list));
1001
 
                        cret->next = NULL;
1002
 
                        cret->values = (int*)malloc(sizeof(int));
 
1279
                        struct val_list *cret = list_new(1, ce1->prob * ce2->prob);
1003
1280
                        int i = 10;
1004
1281
                        while (i < ce2->values[0]) {
1005
1282
                                i *= 10;
1007
1284
                        cret->values[0] = ce1->values[0];
1008
1285
                        cret->values[0] *= i;
1009
1286
                        cret->values[0] += ce2->values[0];
1010
 
                        cret->count = 1;
1011
 
                        cret->prob = ce1->prob * ce2->prob;
1012
1287
                        list_add(&ret, cret, this->ordering);
1013
1288
                        ce2 = ce2->next;
1014
1289
                }
1018
1293
        list_free(e2);
1019
1294
        return ret;
1020
1295
}
1021
 
#line 1022 "tree.c"
 
1296
#line 1297 "tree.c"
1022
1297
 
1023
1298
struct scat_vtable__ const scat_vt__ = {
1024
1299
        &binary_vt__,
1029
1304
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1030
1305
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1031
1306
        (struct val_list * (*)(expression *this__))scat_eval__,
 
1307
        (expression * (*)(expression *this__))binary_optimize__,
1032
1308
};
1033
1309
 
1034
1310
void rep_printtree__(rep *this, int depth)
1039
1315
        printtree(this->expr1, depth+1);
1040
1316
        printtree(this->expr2, depth+1);
1041
1317
}
1042
 
#line 1043 "tree.c"
 
1318
#line 1319 "tree.c"
1043
1319
 
1044
1320
struct roll_value * rep_roll__(rep *this)
1045
1321
#line 132 "roll.tc"
1067
1343
 
1068
1344
        return rv;
1069
1345
}
1070
 
#line 1071 "tree.c"
 
1346
#line 1347 "tree.c"
1071
1347
 
1072
1348
struct val_list * rep_eval__(rep *this)
1073
 
#line 200 "eval.tc"
 
1349
#line 190 "eval.tc"
1074
1350
{
1075
1351
        struct val_list *cnt = eval(this->expr1);
1076
1352
        struct val_list *exp = eval(this->expr2);
1113
1389
                                        len += index[pos[i]]->count;
1114
1390
                                        prob *= index[pos[i]]->prob;
1115
1391
                                }
1116
 
                                struct val_list *cret = (struct val_list*)
1117
 
                                        malloc(sizeof(struct val_list));
1118
 
                                cret->next = NULL;
1119
 
                                cret->count = len;
1120
 
                                cret->prob = prob;
1121
 
                                cret->values = (int*)malloc(sizeof(int) * cret->count);
 
1392
                                struct val_list *cret = list_new(len, prob);
1122
1393
                                int cp = 0;
1123
1394
                                for (i = 0; i < icnt; i++) {
1124
1395
                                        memcpy(&cret->values[cp], index[pos[i]]->values, 
1136
1407
                                        pos[i]++;
1137
1408
                                }
1138
1409
                        } while (pos[icnt-1] < expcnt);
 
1410
                } else {
 
1411
                        list_add(&ret, list_new(0, ccnt->prob), this->ordering);
1139
1412
                }
1140
1413
                // next iteration
1141
1414
                ccnt=ccnt->next;
1142
1415
        }
1143
1416
        // if we have nothing, return an empty list     
1144
1417
        if (ret == NULL) {
1145
 
                struct val_list *cret = (struct val_list*)
1146
 
                        malloc(sizeof(struct val_list));
1147
 
                cret->next = NULL;
1148
 
                cret->values = NULL;
1149
 
                cret->count = 0;
1150
 
                cret->prob = 1;
 
1418
                struct val_list *cret = list_new(0, 1.0);
1151
1419
                list_add(&ret, cret, this->ordering);
1152
1420
        }
 
1421
        list_free(cnt);
 
1422
        list_free(exp);
1153
1423
        return ret;
1154
1424
}
1155
 
#line 1156 "tree.c"
 
1425
#line 1426 "tree.c"
1156
1426
 
1157
1427
struct rep_vtable__ const rep_vt__ = {
1158
1428
        &binary_vt__,
1163
1433
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1164
1434
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1165
1435
        (struct val_list * (*)(expression *this__))rep_eval__,
 
1436
        (expression * (*)(expression *this__))binary_optimize__,
1166
1437
};
1167
1438
 
1168
1439
void range_printtree__(range *this, int depth)
1173
1444
        printtree(this->expr1, depth+1);
1174
1445
        printtree(this->expr2, depth+1);
1175
1446
}
1176
 
#line 1177 "tree.c"
 
1447
#line 1448 "tree.c"
1177
1448
 
1178
1449
struct roll_value * range_roll__(range *this)
1179
1450
#line 92 "roll.tc"
1215
1486
        free_roll(rv2);
1216
1487
        return rv;
1217
1488
}
1218
 
#line 1219 "tree.c"
 
1489
#line 1490 "tree.c"
1219
1490
 
1220
1491
struct val_list * range_eval__(range *this)
1221
 
#line 283 "eval.tc"
 
1492
#line 267 "eval.tc"
1222
1493
{
1223
1494
        struct val_list *from = eval(this->expr1);
1224
1495
        struct val_list *to = eval(this->expr2);
1248
1519
                        if (count < 0) {
1249
1520
                                count = 0;
1250
1521
                        }
1251
 
                        struct val_list *cret = (struct val_list*)
1252
 
                                malloc(sizeof(struct val_list));
1253
 
                        cret->next = NULL;
1254
 
                        cret->values = (int*)malloc(sizeof(int)*count);
1255
 
                        int i, c = 0;
1256
 
                        for (i = ifrom; i < ito; i++ ) {
 
1522
                        struct val_list *cret = list_new(count, cfrom->prob * cto->prob);
 
1523
                        int i, c;
 
1524
                        for (i = ifrom, c = 0; i <= ito; ++i, ++c) {
1257
1525
                                cret->values[c] = i;
1258
 
                                c++;
1259
1526
                        }
1260
 
                        cret->values[c] = i;
1261
 
                        cret->count = count;
1262
 
                        cret->prob = cfrom->prob * cto->prob;
1263
1527
                        list_add(&ret, cret, this->ordering);
1264
1528
 
1265
1529
                        cto = cto->next;
1270
1534
        list_free(to);
1271
1535
        return ret;
1272
1536
}
1273
 
#line 1274 "tree.c"
 
1537
#line 1538 "tree.c"
1274
1538
 
1275
1539
struct range_vtable__ const range_vt__ = {
1276
1540
        &binary_vt__,
1281
1545
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1282
1546
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1283
1547
        (struct val_list * (*)(expression *this__))range_eval__,
 
1548
        (expression * (*)(expression *this__))binary_optimize__,
1284
1549
};
1285
1550
 
1286
1551
void lcat_printtree__(lcat *this, int depth)
1291
1556
        printtree(this->expr1, depth+1);
1292
1557
        printtree(this->expr2, depth+1);
1293
1558
}
1294
 
#line 1295 "tree.c"
 
1559
#line 1560 "tree.c"
1295
1560
 
1296
1561
struct roll_value * lcat_roll__(lcat *this)
1297
1562
#line 158 "roll.tc"
1306
1571
        free_roll(rv2);
1307
1572
        return rv1;
1308
1573
}
1309
 
#line 1310 "tree.c"
 
1574
#line 1575 "tree.c"
1310
1575
 
1311
1576
struct val_list * lcat_eval__(lcat *this)
1312
 
#line 336 "eval.tc"
 
1577
#line 313 "eval.tc"
1313
1578
{
1314
1579
        struct val_list *e1 = eval(this->expr1);
1315
1580
        struct val_list *e2 = eval(this->expr2);
1320
1585
        while (ce1) {
1321
1586
                ce2 = e2;
1322
1587
                while (ce2) {
1323
 
                        struct val_list *cret = (struct val_list*)
1324
 
                                malloc(sizeof(struct val_list));
1325
 
                        cret->next = NULL;
1326
 
                        cret->values = (int*)malloc(sizeof(int) 
1327
 
                                * (ce1->count + ce2->count));
1328
 
                        cret->count = ce1->count + ce2->count;
 
1588
                        struct val_list *cret = list_new
 
1589
                                (ce1->count + ce2->count, ce1->prob * ce2->prob);
1329
1590
                        memcpy(cret->values, ce1->values, ce1->count * sizeof(int));
1330
1591
                        memcpy(&cret->values[ce1->count], ce2->values, 
1331
1592
                                ce2->count * sizeof(int));
1332
 
                        cret->prob = ce1->prob * ce2->prob;
1333
1593
                        list_add(&ret, cret, this->ordering);
1334
1594
                        ce2 = ce2->next;
1335
1595
                }
1339
1599
        list_free(e2);
1340
1600
        return ret;
1341
1601
}
1342
 
#line 1343 "tree.c"
 
1602
#line 1603 "tree.c"
1343
1603
 
1344
1604
struct lcat_vtable__ const lcat_vt__ = {
1345
1605
        &binary_vt__,
1350
1610
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1351
1611
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1352
1612
        (struct val_list * (*)(expression *this__))lcat_eval__,
 
1613
        (expression * (*)(expression *this__))binary_optimize__,
1353
1614
};
1354
1615
 
1355
1616
struct filter_vtable__ const filter_vt__ = {
1361
1622
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1362
1623
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1363
1624
        (struct val_list * (*)(expression *this__))0,
 
1625
        (expression * (*)(expression *this__))binary_optimize__,
1364
1626
};
1365
1627
 
1366
1628
void let_printtree__(let *this, int depth)
1371
1633
        printtree(this->expr1, depth+1);
1372
1634
        printtree(this->expr2, depth+1);
1373
1635
}
1374
 
#line 1375 "tree.c"
 
1636
#line 1637 "tree.c"
1375
1637
 
1376
1638
struct roll_value * let_roll__(let *this)
1377
 
#line 512 "roll.tc"
 
1639
#line 499 "roll.tc"
1378
1640
{
1379
1641
        // variable setzen und in symtab
1380
1642
        struct symtab *nst = (struct symtab*)malloc(sizeof(struct symtab));
1388
1650
        free(nst);
1389
1651
        return ret;
1390
1652
}
1391
 
#line 1392 "tree.c"
 
1653
#line 1654 "tree.c"
1392
1654
 
1393
1655
void let_set_ordering__(let *this, ordering_type ordering)
1394
1656
#line 94 "ordering.tc"
1403
1665
        set_ordering(this->expr1, nst->ordering);
1404
1666
        free(nst);
1405
1667
}
1406
 
#line 1407 "tree.c"
 
1668
#line 1669 "tree.c"
1407
1669
 
1408
1670
struct val_list * let_eval__(let *this)
1409
 
#line 681 "eval.tc"
 
1671
#line 672 "eval.tc"
1410
1672
{
1411
1673
        struct val_list *cnt = eval(this->expr1);
1412
1674
        struct val_list *ret = NULL;
1439
1701
        list_free(cnt);
1440
1702
        return ret;
1441
1703
}
1442
 
#line 1443 "tree.c"
 
1704
#line 1705 "tree.c"
1443
1705
 
1444
1706
struct let_vtable__ const let_vt__ = {
1445
1707
        &binary_vt__,
1450
1712
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1451
1713
        (void (*)(expression *this__, ordering_type ordering))let_set_ordering__,
1452
1714
        (struct val_list * (*)(expression *this__))let_eval__,
 
1715
        (expression * (*)(expression *this__))binary_optimize__,
1453
1716
};
1454
1717
 
1455
1718
void foreach_printtree__(foreach *this, int depth)
1460
1723
        printtree(this->expr1, depth+1);
1461
1724
        printtree(this->expr2, depth+1);
1462
1725
}
1463
 
#line 1464 "tree.c"
 
1726
#line 1727 "tree.c"
1464
1727
 
1465
1728
struct roll_value * foreach_roll__(foreach *this)
1466
 
#line 556 "roll.tc"
 
1729
#line 543 "roll.tc"
1467
1730
{
1468
1731
        struct roll_value *list = roll(this->expr1);
1469
1732
        struct roll_value *ret = (struct roll_value*)
1494
1757
        free(nst);
1495
1758
        return ret;
1496
1759
}
1497
 
#line 1498 "tree.c"
 
1760
#line 1761 "tree.c"
1498
1761
 
1499
1762
struct val_list * foreach_eval__(foreach *this)
1500
 
#line 715 "eval.tc"
 
1763
#line 706 "eval.tc"
1501
1764
{
1502
1765
        struct val_list *cnt = eval(this->expr1);
1503
1766
        struct val_list *ccnt = cnt;
1504
1767
        int i;
1505
1768
        
1506
1769
        struct val_list *tret = NULL;
1507
 
        struct val_list *ret = (struct val_list*)malloc(sizeof(struct val_list));
1508
 
        ret->count = 0;
1509
 
        ret->prob = 1;
1510
 
        ret->values = NULL;
1511
1770
 
1512
1771
        struct symtab *nst = (struct symtab*)malloc(sizeof(struct symtab));
1513
1772
        nst->name = this->varname;
1519
1778
 
1520
1779
        // each possibility
1521
1780
        while (ccnt) {
1522
 
                ret = (struct val_list*)malloc(sizeof(struct val_list));
1523
 
                ret->count = 0;
1524
 
                ret->prob = 1;
1525
 
                ret->values = NULL;
 
1781
                struct val_list *ret = list_new(0, 1.0);
1526
1782
                for (i = 0; i < ccnt->count; i++) {
1527
1783
                        nst->rvalue->values[0] = ccnt->values[i];
1528
1784
                        struct val_list *cur = eval(this->expr2);
1534
1790
                        while (cret) {
1535
1791
                                struct val_list *ccur = cur;
1536
1792
                                while (ccur) {
1537
 
                                        struct val_list *item = (struct val_list*)
1538
 
                                                malloc(sizeof(struct val_list));
1539
 
                                        item->prob = ccnt->prob * ccur->prob * cret->prob;
1540
 
                                        item->count = ccur->count + cret->count;
1541
 
                                        item->values = (int*)malloc(sizeof(int) * item->count);
 
1793
                                        struct val_list *item = list_new(
 
1794
                                                ccur->count + cret->count,
 
1795
                                                ccur->prob * cret->prob);
1542
1796
                                        memcpy(item->values, cret->values, 
1543
1797
                                                sizeof(int) * cret->count);
1544
1798
                                        memcpy(&item->values[cret->count], ccur->values, 
1550
1804
                                }
1551
1805
                                cret = cret->next;
1552
1806
                        }
 
1807
                        list_free(cur);
1553
1808
                        list_free(ret);
1554
1809
                        ret = nret;
1555
1810
                        nret = NULL;
1556
1811
                }
1557
1812
                struct val_list *cret = ret;
1558
1813
                while (cret) {
 
1814
            cret->prob *= ccnt->prob;
1559
1815
                        struct val_list *temp = cret;
1560
1816
                        cret = cret->next;
1561
1817
                        temp->next = NULL;
1568
1824
        list_free(cnt);
1569
1825
        return tret;
1570
1826
}
1571
 
#line 1572 "tree.c"
 
1827
#line 1828 "tree.c"
1572
1828
 
1573
1829
struct foreach_vtable__ const foreach_vt__ = {
1574
1830
        &binary_vt__,
1579
1835
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1580
1836
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1581
1837
        (struct val_list * (*)(expression *this__))foreach_eval__,
 
1838
        (expression * (*)(expression *this__))binary_optimize__,
1582
1839
};
1583
1840
 
1584
1841
void whiledo_printtree__(whiledo *this, int depth)
1589
1846
        printtree(this->expr1, depth+1);
1590
1847
        printtree(this->expr2, depth+1);
1591
1848
}
1592
 
#line 1593 "tree.c"
 
1849
#line 1850 "tree.c"
1593
1850
 
1594
1851
struct roll_value * whiledo_roll__(whiledo *this)
1595
 
#line 588 "roll.tc"
 
1852
#line 575 "roll.tc"
1596
1853
{
1597
1854
        struct roll_value *cur;
1598
1855
        struct roll_value *ret = (struct roll_value*)malloc(
1627
1884
        free(nst);
1628
1885
        return ret;
1629
1886
}
1630
 
#line 1631 "tree.c"
 
1887
#line 1888 "tree.c"
1631
1888
 
1632
1889
struct val_list * whiledo_eval__(whiledo *this)
1633
 
#line 838 "eval.tc"
 
1890
#line 822 "eval.tc"
1634
1891
{
1635
1892
        struct val_list *expe = eval(this->expr1);
1636
1893
        struct val_list *ret = NULL;
1637
1894
        rec_whiledo(expe, this->varname, this->symtab, this->expr2, NULL, 0, &ret,
1638
1895
                this->ordering);
 
1896
        list_free(expe);
1639
1897
        return ret;
1640
1898
}
1641
 
#line 1642 "tree.c"
 
1899
#line 1900 "tree.c"
1642
1900
 
1643
1901
struct whiledo_vtable__ const whiledo_vt__ = {
1644
1902
        &binary_vt__,
1649
1907
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1650
1908
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1651
1909
        (struct val_list * (*)(expression *this__))whiledo_eval__,
 
1910
        (expression * (*)(expression *this__))binary_optimize__,
1652
1911
};
1653
1912
 
1654
1913
struct plus_vtable__ const plus_vt__ = {
1660
1919
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1661
1920
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1662
1921
        (struct val_list * (*)(expression *this__))mathop_eval__,
 
1922
        (expression * (*)(expression *this__))binary_optimize__,
1663
1923
};
1664
1924
 
1665
1925
struct minus_vtable__ const minus_vt__ = {
1671
1931
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1672
1932
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1673
1933
        (struct val_list * (*)(expression *this__))mathop_eval__,
 
1934
        (expression * (*)(expression *this__))binary_optimize__,
1674
1935
};
1675
1936
 
1676
1937
struct multi_vtable__ const multi_vt__ = {
1682
1943
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1683
1944
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1684
1945
        (struct val_list * (*)(expression *this__))mathop_eval__,
 
1946
        (expression * (*)(expression *this__))binary_optimize__,
1685
1947
};
1686
1948
 
1687
1949
struct divi_vtable__ const divi_vt__ = {
1693
1955
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1694
1956
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1695
1957
        (struct val_list * (*)(expression *this__))mathop_eval__,
 
1958
        (expression * (*)(expression *this__))binary_optimize__,
1696
1959
};
1697
1960
 
1698
1961
struct mod_vtable__ const mod_vt__ = {
1704
1967
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1705
1968
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1706
1969
        (struct val_list * (*)(expression *this__))mathop_eval__,
 
1970
        (expression * (*)(expression *this__))binary_optimize__,
1707
1971
};
1708
1972
 
1709
1973
struct expo_vtable__ const expo_vt__ = {
1715
1979
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1716
1980
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
1717
1981
        (struct val_list * (*)(expression *this__))mathop_eval__,
 
1982
        (expression * (*)(expression *this__))binary_optimize__,
1718
1983
};
1719
1984
 
1720
1985
void first_printtree__(first *this, int depth)
1731
1996
        printtree(this->expr1, depth+1);
1732
1997
        printtree(this->expr2, depth+1);
1733
1998
}
1734
 
#line 1735 "tree.c"
 
1999
#line 2000 "tree.c"
1735
2000
 
1736
2001
struct roll_value * first_roll__(first *this)
1737
2002
#line 266 "roll.tc"
1763
2028
        free_roll(num);
1764
2029
        return exp;
1765
2030
}
1766
 
#line 1767 "tree.c"
 
2031
#line 2032 "tree.c"
1767
2032
 
1768
2033
void first_set_ordering__(first *this, ordering_type ordering)
1769
2034
#line 55 "ordering.tc"
1772
2037
        set_ordering(this->expr1, agnostic);
1773
2038
        set_ordering(this->expr2, caring);
1774
2039
}
1775
 
#line 1776 "tree.c"
 
2040
#line 2041 "tree.c"
1776
2041
 
1777
2042
struct val_list * first_eval__(first *this)
1778
 
#line 367 "eval.tc"
 
2043
#line 339 "eval.tc"
1779
2044
{
1780
2045
        struct val_list *num = eval(this->expr1);
1781
2046
        struct val_list *exp = eval(this->expr2);
1820
2085
        list_free(num);
1821
2086
        return ret;
1822
2087
}
1823
 
#line 1824 "tree.c"
 
2088
#line 2089 "tree.c"
1824
2089
 
1825
2090
struct first_vtable__ const first_vt__ = {
1826
2091
        &filter_vt__,
1831
2096
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1832
2097
        (void (*)(expression *this__, ordering_type ordering))first_set_ordering__,
1833
2098
        (struct val_list * (*)(expression *this__))first_eval__,
 
2099
        (expression * (*)(expression *this__))binary_optimize__,
1834
2100
};
1835
2101
 
1836
2102
void last_printtree__(last *this, int depth)
1847
2113
        printtree(this->expr1, depth+1);
1848
2114
        printtree(this->expr2, depth+1);
1849
2115
}
1850
 
#line 1851 "tree.c"
 
2116
#line 2117 "tree.c"
1851
2117
 
1852
2118
struct roll_value * last_roll__(last *this)
1853
2119
#line 295 "roll.tc"
1877
2143
        free_roll(num);
1878
2144
        return exp;
1879
2145
}
1880
 
#line 1881 "tree.c"
 
2146
#line 2147 "tree.c"
1881
2147
 
1882
2148
void last_set_ordering__(last *this, ordering_type ordering)
1883
2149
#line 62 "ordering.tc"
1886
2152
        set_ordering(this->expr1, agnostic);
1887
2153
        set_ordering(this->expr2, caring);
1888
2154
}
1889
 
#line 1890 "tree.c"
 
2155
#line 2156 "tree.c"
1890
2156
 
1891
2157
struct val_list * last_eval__(last *this)
1892
 
#line 413 "eval.tc"
 
2158
#line 385 "eval.tc"
1893
2159
{
1894
2160
        struct val_list *num = eval(this->expr1);
1895
2161
        struct val_list *exp = eval(this->expr2);
1938
2204
        list_free(num);
1939
2205
        return ret;
1940
2206
}
1941
 
#line 1942 "tree.c"
 
2207
#line 2208 "tree.c"
1942
2208
 
1943
2209
struct last_vtable__ const last_vt__ = {
1944
2210
        &filter_vt__,
1949
2215
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
1950
2216
        (void (*)(expression *this__, ordering_type ordering))last_set_ordering__,
1951
2217
        (struct val_list * (*)(expression *this__))last_eval__,
 
2218
        (expression * (*)(expression *this__))binary_optimize__,
1952
2219
};
1953
2220
 
1954
2221
void high_printtree__(high *this, int depth)
1965
2232
        printtree(this->expr1, depth+1);
1966
2233
        printtree(this->expr2, depth+1);
1967
2234
}
1968
 
#line 1969 "tree.c"
 
2235
#line 2236 "tree.c"
1969
2236
 
1970
2237
struct roll_value * high_roll__(high *this)
1971
 
#line 396 "roll.tc"
 
2238
#line 381 "roll.tc"
1972
2239
{
1973
2240
        struct roll_value *num = roll(this->expr1);
1974
2241
        struct roll_value *exp = roll(this->expr2);
1990
2257
                for (i = 0; (i < exp->count) && (c < num->values[0]); i++) {
1991
2258
                        int j, found = 0;
1992
2259
                        for (j = exp->count - 1; j >= exp->count - num->values[0]; j--) {
1993
 
                                if (exp->values[i] == slist[j]) {
 
2260
                                if ((exp->values[i] == slist[j]) && (!mask[j])) {
1994
2261
                                        found = 1;
 
2262
                    mask[j] = 1;
1995
2263
                                        break;
1996
2264
                                }
1997
2265
                        }
2019
2287
                }
2020
2288
        }
2021
2289
        exp->count = c;
 
2290
        free_roll(num);
2022
2291
        free(slist);
2023
2292
        free(mask);
2024
2293
        return exp;
2025
2294
}
2026
 
#line 2027 "tree.c"
 
2295
#line 2296 "tree.c"
2027
2296
 
2028
2297
struct val_list * high_eval__(high *this)
2029
 
#line 463 "eval.tc"
 
2298
#line 435 "eval.tc"
2030
2299
{
2031
2300
        struct val_list *num = eval(this->expr1);
2032
2301
        struct val_list *exp = eval(this->expr2);
2047
2316
                        struct val_list *cret = (struct val_list*)
2048
2317
                                malloc(sizeof(struct val_list));
2049
2318
                        cret->next = NULL;
 
2319
 
 
2320
            // ready, start the actual filtering:
 
2321
            // we want up to cnum->values[0] items from cexp in cret
 
2322
 
2050
2323
                        int *slist = (int*)malloc(sizeof(int) * cexp->count);
 
2324
                        int *mask = (int*)malloc(sizeof(int) * cexp->count);
2051
2325
                        memcpy(slist, cexp->values, sizeof(int) * cexp->count);
 
2326
                        memset(mask, 0, sizeof(int) * cexp->count);
2052
2327
                        quicksort(slist, 0, cexp->count - 1);
2053
 
                        int p = 0, i;
 
2328
            reverse(slist, 0, cexp->count - 1);
 
2329
                
 
2330
                int p = 0, i;
2054
2331
                        if (this->type == keep) {
 
2332
                // how many do we want?
2055
2333
                                cret->count = min(cexp->count, cnum->values[0]);
2056
2334
                                cret->values = (int*)malloc(sizeof(int) * cret->count);
 
2335
 
2057
2336
                                for (i = 0; (i < cexp->count) && (p < cnum->values[0]); i++) {
2058
2337
                                        int j, found = 0;
2059
 
                                        for (j = cexp->count - 1; 
2060
 
                                                j >= cexp->count - cnum->values[0]; j--) {
2061
 
                                                if (cexp->values[i] == slist[j]) {
 
2338
                                        for (j = 0; j < cnum->values[0]; j++) {
 
2339
                                                if ((cexp->values[i] == slist[j]) && (!mask[j])) {
2062
2340
                                                        found = 1;
 
2341
                            mask[j] = 1;
2063
2342
                                                        break;
2064
2343
                                                }
2065
2344
                                        }
2075
2354
                                        && (p < (cexp->count - cnum->values[0])); 
2076
2355
                                        i++) {
2077
2356
                                        int j, found = 0;
2078
 
                                        for (j = 0; j < cexp->count - cnum->values[0]; j++) {
2079
 
                                                if (cexp->values[i] == slist[j]) {
 
2357
                                        for (j = cexp->count - 1; j >= cnum->values[0]; j--) {
 
2358
                                                if ((cexp->values[i] == slist[j]) && (!mask[j])) {
2080
2359
                                                        found = 1;
 
2360
                                                        // XXX der mask kram auch bei high, und auch im
 
2361
                                                        // roll.tc
 
2362
                                                        mask[j] = 1;
2081
2363
                                                        break;
2082
2364
                                                }
2083
2365
                                        }
2087
2369
                                }
2088
2370
                        }
2089
2371
                        free(slist);
 
2372
                        free(mask);
2090
2373
                        cret->prob = cexp->prob * cnum->prob;   
2091
2374
                        list_add(&ret, cret, this->ordering);
2092
2375
                        cexp = cexp->next;
2097
2380
        list_free(num);
2098
2381
        return ret;
2099
2382
}
2100
 
#line 2101 "tree.c"
 
2383
#line 2384 "tree.c"
2101
2384
 
2102
2385
struct high_vtable__ const high_vt__ = {
2103
2386
        &filter_vt__,
2108
2391
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
2109
2392
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
2110
2393
        (struct val_list * (*)(expression *this__))high_eval__,
 
2394
        (expression * (*)(expression *this__))binary_optimize__,
2111
2395
};
2112
2396
 
2113
2397
void low_printtree__(low *this, int depth)
2124
2408
        printtree(this->expr1, depth+1);
2125
2409
        printtree(this->expr2, depth+1);
2126
2410
}
2127
 
#line 2128 "tree.c"
 
2411
#line 2412 "tree.c"
2128
2412
 
2129
2413
struct roll_value * low_roll__(low *this)
2130
2414
#line 322 "roll.tc"
2145
2429
        quicksort(slist, 0, exp->count -1);
2146
2430
        int p = 0, i, c = 0;
2147
2431
 
2148
 
/*      printf("# (");
2149
 
        for (i = 0; i < exp->count; i++) {
2150
 
                printf("%i", exp->values[i]);
2151
 
                if (i < (exp->count-1)) {
2152
 
                        printf(", ");
2153
 
                }
2154
 
        }
2155
 
        printf(") -> (");*/
2156
 
 
2157
2432
        if (this->type == keep) {
2158
2433
                for (i = 0; (i < exp->count) && (c < num->values[0]); i++) {
2159
2434
                        int j, found = 0;
2160
2435
                        for (j = 0; j < num->values[0]; j++) {
2161
 
                                if (exp->values[i] == slist[j]) {
 
2436
                                if ((exp->values[i] == slist[j]) && (!mask[j])) {
2162
2437
                                        found = 1;
 
2438
                    mask[j] = 1;
2163
2439
                                        break;
2164
2440
                                }
2165
2441
                        }
2170
2446
                }
2171
2447
        }
2172
2448
        else {
2173
 
                // XXX something wrong here
2174
2449
                for (i = 0; (i < exp->count) && (c < (exp->count - num->values[0])); 
2175
2450
                        i++) {
2176
2451
                        int j, found = 0;
2190
2465
                }
2191
2466
        }
2192
2467
        exp->count = c;
2193
 
/*      for (i = 0; i < exp->count; i++) {
2194
 
                printf("%i", exp->values[i]);
2195
 
                if (i < (exp->count-1)) {
2196
 
                        printf(", ");
2197
 
                }
2198
 
        }
2199
 
        printf(")\n");*/
 
2468
        free_roll(num);
2200
2469
        free(slist);
2201
2470
        free(mask);
2202
2471
        return exp;
2203
2472
}
2204
 
#line 2205 "tree.c"
 
2473
#line 2474 "tree.c"
2205
2474
 
2206
2475
struct val_list * low_eval__(low *this)
2207
 
#line 535 "eval.tc"
 
2476
#line 521 "eval.tc"
2208
2477
{
2209
2478
        struct val_list *num = eval(this->expr1);
2210
2479
        struct val_list *exp = eval(this->expr2);
2225
2494
                        struct val_list *cret = (struct val_list*)
2226
2495
                                malloc(sizeof(struct val_list));
2227
2496
                        cret->next = NULL;
 
2497
 
 
2498
            // ready, start the actual filtering:
 
2499
            // we want up to cnum->values[0] items from cexp in cret
 
2500
 
2228
2501
                        int *slist = (int*)malloc(sizeof(int) * cexp->count);
2229
2502
                        int *mask = (int*)malloc(sizeof(int) * cexp->count);
2230
2503
                        memcpy(slist, cexp->values, sizeof(int) * cexp->count);
2231
2504
                        memset(mask, 0, sizeof(int) * cexp->count);
2232
2505
                        quicksort(slist, 0, cexp->count - 1);
2233
 
                        int p = 0, i;
 
2506
                
 
2507
                int p = 0, i;
2234
2508
                        if (this->type == keep) {
 
2509
                // how many do we want?
2235
2510
                                cret->count = min(cexp->count, cnum->values[0]);
2236
2511
                                cret->values = (int*)malloc(sizeof(int) * cret->count);
 
2512
 
2237
2513
                                for (i = 0; (i < cexp->count) && (p < cnum->values[0]); i++) {
2238
2514
                                        int j, found = 0;
2239
2515
                                        for (j = 0; j < cnum->values[0]; j++) {
2240
 
                                                if (cexp->values[i] == slist[j]) {
 
2516
                                                if ((cexp->values[i] == slist[j]) && (!mask[j])) {
2241
2517
                                                        found = 1;
 
2518
                            mask[j] = 1;
2242
2519
                                                        break;
2243
2520
                                                }
2244
2521
                                        }
2280
2557
        list_free(num);
2281
2558
        return ret;
2282
2559
}
2283
 
#line 2284 "tree.c"
 
2560
#line 2561 "tree.c"
2284
2561
 
2285
2562
struct low_vtable__ const low_vt__ = {
2286
2563
        &filter_vt__,
2291
2568
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
2292
2569
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
2293
2570
        (struct val_list * (*)(expression *this__))low_eval__,
 
2571
        (expression * (*)(expression *this__))binary_optimize__,
2294
2572
};
2295
2573
 
2296
2574
void comparison_printtree__(comparison *this, int depth)
2329
2607
        printtree(this->expr1, depth+1);
2330
2608
        printtree(this->expr2, depth+1);
2331
2609
}
2332
 
#line 2333 "tree.c"
 
2610
#line 2611 "tree.c"
2333
2611
 
2334
2612
struct roll_value * comparison_roll__(comparison *this)
2335
 
#line 451 "roll.tc"
 
2613
#line 438 "roll.tc"
2336
2614
{
2337
2615
        struct roll_value *num = roll(this->expr1);
2338
2616
        struct roll_value *exp = roll(this->expr2);
2379
2657
        free_roll(num);
2380
2658
        return exp;
2381
2659
}
2382
 
#line 2383 "tree.c"
 
2660
#line 2661 "tree.c"
2383
2661
 
2384
2662
struct val_list * comparison_eval__(comparison *this)
2385
 
#line 612 "eval.tc"
 
2663
#line 606 "eval.tc"
2386
2664
{
2387
2665
        struct val_list *num = eval(this->expr1);
2388
2666
        struct val_list *exp = eval(this->expr2);
2400
2678
                }
2401
2679
                cexp = exp;
2402
2680
                while (cexp) {
2403
 
                        struct val_list *cret = (struct val_list*)
2404
 
                                malloc(sizeof(struct val_list));
2405
 
                        cret->next = NULL;
2406
 
                        cret->values = (int*)malloc(cexp->count * sizeof(int));
2407
 
                        cret->count = 0;
 
2681
                        struct val_list *cret = list_new
 
2682
                                (cexp->count, cexp->prob * cnum->prob);
 
2683
                        cret->count = 0; // updated below
2408
2684
                        int o;
2409
2685
                        for (o = 0; o < cexp->count; o++) {
2410
2686
                                int c = 0;
2440
2716
                                        cret->values[cret->count++] = cexp->values[o];
2441
2717
                                }
2442
2718
                        }
2443
 
                        cret->prob = cexp->prob * cnum->prob;
2444
2719
                        list_add(&ret, cret, this->ordering);
2445
2720
                        cexp = cexp->next;
2446
2721
                }
2450
2725
        list_free(num);
2451
2726
        return ret;
2452
2727
}
2453
 
#line 2454 "tree.c"
 
2728
#line 2729 "tree.c"
2454
2729
 
2455
2730
struct comparison_vtable__ const comparison_vt__ = {
2456
2731
        &filter_vt__,
2461
2736
        (void (*)(expression *this__, struct symtab * st))binary_set_symtab__,
2462
2737
        (void (*)(expression *this__, ordering_type ordering))binary_set_ordering__,
2463
2738
        (struct val_list * (*)(expression *this__))comparison_eval__,
 
2739
        (expression * (*)(expression *this__))binary_optimize__,
2464
2740
};
2465
2741
 
2466
2742
void negate_printtree__(negate *this, int depth)
2470
2746
        printf("-\n");
2471
2747
        printtree(this->expr, depth+1);
2472
2748
}
2473
 
#line 2474 "tree.c"
 
2749
#line 2750 "tree.c"
2474
2750
 
2475
2751
struct roll_value * negate_roll__(negate *this)
2476
2752
#line 171 "roll.tc"
2485
2761
        rv->values[0] *= -1;
2486
2762
        return rv;
2487
2763
}
2488
 
#line 2489 "tree.c"
 
2764
#line 2765 "tree.c"
2489
2765
 
2490
2766
struct val_list * negate_eval__(negate *this)
2491
 
#line 847 "eval.tc"
 
2767
#line 832 "eval.tc"
2492
2768
{
2493
2769
        struct val_list *sides = eval(this->expr);
2494
2770
        struct val_list *cside = sides;
2504
2780
        }
2505
2781
        return sides;
2506
2782
}
2507
 
#line 2508 "tree.c"
 
2783
#line 2784 "tree.c"
2508
2784
 
2509
2785
struct negate_vtable__ const negate_vt__ = {
2510
2786
        &unary_vt__,
2515
2791
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2516
2792
        (void (*)(expression *this__, ordering_type ordering))unary_set_ordering__,
2517
2793
        (struct val_list * (*)(expression *this__))negate_eval__,
 
2794
        (expression * (*)(expression *this__))unary_optimize__,
2518
2795
};
2519
2796
 
2520
2797
void dice_printtree__(dice *this, int depth)
2524
2801
        printf("d\n");
2525
2802
        printtree(this->expr, depth+1);
2526
2803
}
2527
 
#line 2528 "tree.c"
 
2804
#line 2805 "tree.c"
2528
2805
 
2529
2806
struct roll_value * dice_roll__(dice *this)
2530
2807
#line 184 "roll.tc"
2545
2822
        rv->values[0] = 1 + (int) (rv->values[0] * (rand() / (RAND_MAX + 1.0)));
2546
2823
        return rv;
2547
2824
}
2548
 
#line 2549 "tree.c"
 
2825
#line 2826 "tree.c"
2549
2826
 
2550
2827
struct val_list * dice_eval__(dice *this)
2551
 
#line 864 "eval.tc"
 
2828
#line 849 "eval.tc"
2552
2829
{
2553
2830
        struct val_list *sides = eval(this->expr);
2554
2831
        struct val_list *ret = NULL;
2569
2846
                int i;
2570
2847
                for (i = 1; i <= cside->values[0]; i++) {
2571
2848
                        // create a matching result
2572
 
                        struct val_list *cret = (struct val_list*)
2573
 
                                malloc(sizeof(struct val_list));
2574
 
                        cret->next = NULL;
2575
 
                        cret->values = (int*)malloc(sizeof(int));
 
2849
                        struct val_list *cret = list_new(1, cside->prob/cside->values[0]);
2576
2850
                        cret->values[0] = i;
2577
 
                        cret->count = 1;
2578
 
                        cret->prob = cside->prob/cside->values[0];
2579
2851
                        list_add(&ret, cret, this->ordering);
2580
2852
                }
2581
2853
                cside = cside->next;
2583
2855
        list_free(sides);
2584
2856
        return ret;
2585
2857
}
2586
 
#line 2587 "tree.c"
 
2858
#line 2859 "tree.c"
2587
2859
 
2588
2860
struct dice_vtable__ const dice_vt__ = {
2589
2861
        &unary_vt__,
2594
2866
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2595
2867
        (void (*)(expression *this__, ordering_type ordering))unary_set_ordering__,
2596
2868
        (struct val_list * (*)(expression *this__))dice_eval__,
 
2869
        (expression * (*)(expression *this__))unary_optimize__,
2597
2870
};
2598
2871
 
2599
2872
void sum_printtree__(sum *this, int depth)
2603
2876
        printf("sum\n");
2604
2877
        printtree(this->expr, depth+1);
2605
2878
}
2606
 
#line 2607 "tree.c"
 
2879
#line 2880 "tree.c"
2607
2880
 
2608
2881
struct roll_value * sum_roll__(sum *this)
2609
2882
#line 203 "roll.tc"
2619
2892
        rv->values[0] = s;
2620
2893
        return rv;
2621
2894
}
2622
 
#line 2623 "tree.c"
 
2895
#line 2896 "tree.c"
2623
2896
 
2624
2897
void sum_set_ordering__(sum *this, ordering_type ordering)
2625
2898
#line 25 "ordering.tc"
2627
2900
        this->ordering = ordering;
2628
2901
        set_ordering(this->expr, agnostic);
2629
2902
}
2630
 
#line 2631 "tree.c"
 
2903
#line 2904 "tree.c"
2631
2904
 
2632
2905
struct val_list * sum_eval__(sum *this)
2633
 
#line 900 "eval.tc"
 
2906
#line 880 "eval.tc"
2634
2907
{
2635
2908
        struct val_list *sides = eval(this->expr);
2636
2909
        struct val_list *ret = NULL;
2642
2915
                for (i = 0; i < cside->count; i++) {
2643
2916
                        sum += cside->values[i];
2644
2917
                }
2645
 
                struct val_list *cret = (struct val_list*)
2646
 
                        malloc(sizeof(struct val_list));
2647
 
                cret->next = NULL;
2648
 
                cret->values = (int*)malloc(sizeof(int));
 
2918
                struct val_list *cret = list_new(1, cside->prob);
2649
2919
                cret->values[0] = sum;
2650
 
                cret->count = 1;
2651
 
                cret->prob = cside->prob;
2652
2920
                list_add(&ret, cret, this->ordering);
2653
2921
                cside = cside->next;
2654
2922
        }
2655
2923
        list_free(sides);
2656
2924
        return ret;
2657
2925
}
2658
 
#line 2659 "tree.c"
 
2926
#line 2927 "tree.c"
 
2927
 
 
2928
expression * sum_optimize__(sum *this)
 
2929
#line 251 "optimize.tc"
 
2930
{
 
2931
        if (yykind(this->expr) == rep_kind) {
 
2932
                rep *crep = (rep*)this->expr;
 
2933
                if (yykind(crep->expr2) == dice_kind) {
 
2934
                        dice *cdice = (dice*)crep->expr2;
 
2935
                        if (yykind(cdice->expr) == number_kind) {
 
2936
// case: sum N#dM
 
2937
                                expression *replace = sumrepdice_create(
 
2938
                                        eval(crep->expr1), 
 
2939
                                        ((number*)cdice->expr)->num);
 
2940
                                return replace;
 
2941
                        }
 
2942
                }
 
2943
                else {
 
2944
// case: sum N#XXX
 
2945
                        expression *replace = sumrepany_create(
 
2946
                                eval(crep->expr1),
 
2947
                                eval(sum_create(crep->expr2))); 
 
2948
                        return replace;                 
 
2949
                }
 
2950
        }
 
2951
        return (expression*)this;
 
2952
}
 
2953
#line 2954 "tree.c"
2659
2954
 
2660
2955
struct sum_vtable__ const sum_vt__ = {
2661
2956
        &unary_vt__,
2666
2961
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2667
2962
        (void (*)(expression *this__, ordering_type ordering))sum_set_ordering__,
2668
2963
        (struct val_list * (*)(expression *this__))sum_eval__,
 
2964
        (expression * (*)(expression *this__))sum_optimize__,
2669
2965
};
2670
2966
 
2671
2967
void prod_printtree__(prod *this, int depth)
2675
2971
        printf("prod\n");
2676
2972
        printtree(this->expr, depth+1);
2677
2973
}
2678
 
#line 2679 "tree.c"
 
2974
#line 2975 "tree.c"
2679
2975
 
2680
2976
struct roll_value * prod_roll__(prod *this)
2681
2977
#line 217 "roll.tc"
2686
2982
        for (i = 0; i < rv->count; i++) {
2687
2983
                s *= rv->values[i];
2688
2984
        }
 
2985
        rv->count = 1;
 
2986
        rv->values = (int*)realloc(rv->values, sizeof(int) * rv->count);
2689
2987
        rv->values[0] = s;
2690
 
        rv->count = 1;
2691
2988
        return rv;
2692
2989
}
2693
 
#line 2694 "tree.c"
 
2990
#line 2991 "tree.c"
2694
2991
 
2695
2992
void prod_set_ordering__(prod *this, ordering_type ordering)
2696
2993
#line 31 "ordering.tc"
2698
2995
        this->ordering = ordering;
2699
2996
        set_ordering(this->expr, agnostic);
2700
2997
}
2701
 
#line 2702 "tree.c"
 
2998
#line 2999 "tree.c"
2702
2999
 
2703
3000
struct val_list * prod_eval__(prod *this)
2704
 
#line 926 "eval.tc"
 
3001
#line 901 "eval.tc"
2705
3002
{
2706
3003
        struct val_list *sides = eval(this->expr);
2707
3004
        struct val_list *ret = NULL;
2713
3010
                for (i = 0; i < cside->count; i++) {
2714
3011
                        prod *= cside->values[i];
2715
3012
                }
2716
 
                struct val_list *cret = (struct val_list*)
2717
 
                        malloc(sizeof(struct val_list));
2718
 
                cret->next = NULL;
2719
 
                cret->values = (int*)malloc(sizeof(int));
 
3013
                struct val_list *cret = list_new(1, cside->prob);
2720
3014
                cret->values[0] = prod;
2721
 
                cret->count = 1;
2722
 
                cret->prob = cside->prob;
2723
3015
                list_add(&ret, cret, this->ordering);
2724
3016
                cside = cside->next;
2725
3017
        }
2726
3018
        list_free(sides);
2727
3019
        return ret;
2728
3020
}
2729
 
#line 2730 "tree.c"
 
3021
#line 3022 "tree.c"
2730
3022
 
2731
3023
struct prod_vtable__ const prod_vt__ = {
2732
3024
        &unary_vt__,
2737
3029
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2738
3030
        (void (*)(expression *this__, ordering_type ordering))prod_set_ordering__,
2739
3031
        (struct val_list * (*)(expression *this__))prod_eval__,
 
3032
        (expression * (*)(expression *this__))unary_optimize__,
2740
3033
};
2741
3034
 
2742
3035
void count_printtree__(count *this, int depth)
2746
3039
        printf("count\n");
2747
3040
        printtree(this->expr, depth+1);
2748
3041
}
2749
 
#line 2750 "tree.c"
 
3042
#line 3043 "tree.c"
2750
3043
 
2751
3044
struct roll_value * count_roll__(count *this)
2752
 
#line 257 "roll.tc"
 
3045
#line 258 "roll.tc"
2753
3046
{
2754
3047
        struct roll_value *rv = roll(this->expr);
2755
3048
        rv->values = realloc(rv->values, sizeof(int));
2757
3050
        rv->count = 1;
2758
3051
        return rv;
2759
3052
}
2760
 
#line 2761 "tree.c"
 
3053
#line 3054 "tree.c"
2761
3054
 
2762
3055
void count_set_ordering__(count *this, ordering_type ordering)
2763
3056
#line 37 "ordering.tc"
2765
3058
        this->ordering = ordering;
2766
3059
        set_ordering(this->expr, agnostic);
2767
3060
}
2768
 
#line 2769 "tree.c"
 
3061
#line 3062 "tree.c"
2769
3062
 
2770
3063
struct val_list * count_eval__(count *this)
2771
 
#line 952 "eval.tc"
 
3064
#line 922 "eval.tc"
2772
3065
{
2773
3066
        struct val_list *sides = eval(this->expr);
2774
3067
        struct val_list *ret = NULL;
2775
3068
        struct val_list *cside = sides;
2776
3069
        
2777
3070
        while (cside) {
2778
 
                struct val_list *cret = (struct val_list*)
2779
 
                        malloc(sizeof(struct val_list));
2780
 
                cret->next = NULL;
2781
 
                cret->values = (int*)malloc(sizeof(int));
 
3071
                struct val_list *cret = list_new(1, cside->prob);
2782
3072
                cret->values[0] = cside->count;
2783
 
                cret->count = 1;
2784
 
                cret->prob = cside->prob;
2785
3073
                list_add(&ret, cret, this->ordering);
2786
3074
                cside = cside->next;
2787
3075
        }
2788
3076
        list_free(sides);
2789
3077
        return ret;
2790
3078
}
2791
 
#line 2792 "tree.c"
 
3079
#line 3080 "tree.c"
2792
3080
 
2793
3081
struct count_vtable__ const count_vt__ = {
2794
3082
        &unary_vt__,
2799
3087
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2800
3088
        (void (*)(expression *this__, ordering_type ordering))count_set_ordering__,
2801
3089
        (struct val_list * (*)(expression *this__))count_eval__,
 
3090
        (expression * (*)(expression *this__))unary_optimize__,
2802
3091
};
2803
3092
 
2804
3093
void perm_printtree__(perm *this, int depth)
2808
3097
        printf("perm\n");
2809
3098
        printtree(this->expr, depth+1);
2810
3099
}
2811
 
#line 2812 "tree.c"
 
3100
#line 3101 "tree.c"
2812
3101
 
2813
3102
struct roll_value * perm_roll__(perm *this)
2814
 
#line 230 "roll.tc"
 
3103
#line 231 "roll.tc"
2815
3104
{
2816
3105
        struct roll_value *rv = roll(this->expr);
2817
3106
        permute(rv->values, rv->count);
2818
3107
        return rv;
2819
3108
}
2820
 
#line 2821 "tree.c"
 
3109
#line 3110 "tree.c"
2821
3110
 
2822
3111
void perm_set_ordering__(perm *this, ordering_type ordering)
2823
3112
#line 49 "ordering.tc"
2825
3114
        this->ordering = ordering;
2826
3115
        set_ordering(this->expr, agnostic);
2827
3116
}
2828
 
#line 2829 "tree.c"
 
3117
#line 3118 "tree.c"
2829
3118
 
2830
3119
struct val_list * perm_eval__(perm *this)
2831
 
#line 989 "eval.tc"
 
3120
#line 950 "eval.tc"
2832
3121
{
2833
3122
        struct val_list *exp = eval(this->expr);
2834
3123
        struct val_list *cexp = exp;
2846
3135
        list_free(exp);
2847
3136
        return ret;
2848
3137
}
2849
 
#line 2850 "tree.c"
 
3138
#line 3139 "tree.c"
2850
3139
 
2851
3140
struct perm_vtable__ const perm_vt__ = {
2852
3141
        &unary_vt__,
2857
3146
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2858
3147
        (void (*)(expression *this__, ordering_type ordering))perm_set_ordering__,
2859
3148
        (struct val_list * (*)(expression *this__))perm_eval__,
 
3149
        (expression * (*)(expression *this__))unary_optimize__,
2860
3150
};
2861
3151
 
2862
3152
void sort_printtree__(sort *this, int depth)
2866
3156
        printf("sort\n");
2867
3157
        printtree(this->expr, depth+1);
2868
3158
}
2869
 
#line 2870 "tree.c"
 
3159
#line 3160 "tree.c"
2870
3160
 
2871
3161
struct roll_value * sort_roll__(sort *this)
2872
 
#line 237 "roll.tc"
 
3162
#line 238 "roll.tc"
2873
3163
{
2874
3164
        struct roll_value *rv = roll(this->expr);
2875
3165
        /* sortiere */
2876
3166
        quicksort(rv->values, 0, rv->count - 1);
2877
3167
        return rv;
2878
3168
}
2879
 
#line 2880 "tree.c"
 
3169
#line 3170 "tree.c"
2880
3170
 
2881
3171
void sort_set_ordering__(sort *this, ordering_type ordering)
2882
3172
#line 43 "ordering.tc"
2884
3174
        this->ordering = ordering;
2885
3175
        set_ordering(this->expr, agnostic);
2886
3176
}
2887
 
#line 2888 "tree.c"
 
3177
#line 3178 "tree.c"
2888
3178
 
2889
3179
struct val_list * sort_eval__(sort *this)
2890
 
#line 1008 "eval.tc"
 
3180
#line 969 "eval.tc"
2891
3181
{
2892
3182
        struct val_list *sides = eval(this->expr);
2893
3183
        struct val_list *cside = sides;
2900
3190
        }
2901
3191
        return sides;
2902
3192
}
2903
 
#line 2904 "tree.c"
 
3193
#line 3194 "tree.c"
2904
3194
 
2905
3195
struct sort_vtable__ const sort_vt__ = {
2906
3196
        &unary_vt__,
2911
3201
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2912
3202
        (void (*)(expression *this__, ordering_type ordering))sort_set_ordering__,
2913
3203
        (struct val_list * (*)(expression *this__))sort_eval__,
 
3204
        (expression * (*)(expression *this__))unary_optimize__,
2914
3205
};
2915
3206
 
2916
3207
void rev_printtree__(rev *this, int depth)
2920
3211
        printf("rev\n");
2921
3212
        printtree(this->expr, depth+1);
2922
3213
}
2923
 
#line 2924 "tree.c"
 
3214
#line 3215 "tree.c"
2924
3215
 
2925
3216
struct roll_value * rev_roll__(rev *this)
2926
 
#line 245 "roll.tc"
 
3217
#line 246 "roll.tc"
2927
3218
{
2928
3219
        int i;
2929
3220
        struct roll_value *rv = roll(this->expr);
2934
3225
        }
2935
3226
        return rv;
2936
3227
}
2937
 
#line 2938 "tree.c"
 
3228
#line 3229 "tree.c"
2938
3229
 
2939
3230
struct val_list * rev_eval__(rev *this)
2940
 
#line 1022 "eval.tc"
 
3231
#line 983 "eval.tc"
2941
3232
{
2942
3233
        struct val_list *sides = eval(this->expr);
2943
3234
        struct val_list *cside = sides;
2958
3249
 
2959
3250
        return sides;
2960
3251
}
2961
 
#line 2962 "tree.c"
 
3252
#line 3253 "tree.c"
2962
3253
 
2963
3254
struct rev_vtable__ const rev_vt__ = {
2964
3255
        &unary_vt__,
2969
3260
        (void (*)(expression *this__, struct symtab * st))unary_set_symtab__,
2970
3261
        (void (*)(expression *this__, ordering_type ordering))unary_set_ordering__,
2971
3262
        (struct val_list * (*)(expression *this__))rev_eval__,
 
3263
        (expression * (*)(expression *this__))unary_optimize__,
2972
3264
};
2973
3265
 
2974
3266
expression *elist_create(void)
3028
3320
        return (expression *)node__;
3029
3321
}
3030
3322
 
 
3323
expression *sumrepdice_create(struct val_list * num_dice, int num_sides)
 
3324
{
 
3325
        sumrepdice *node__ = (sumrepdice *)yynodealloc(sizeof(struct sumrepdice__));
 
3326
        if(node__ == 0) return 0;
 
3327
        node__->vtable__ = &sumrepdice_vt__;
 
3328
        node__->kind__ = sumrepdice_kind;
 
3329
        node__->filename__ = yycurrfilename();
 
3330
        node__->linenum__ = yycurrlinenum();
 
3331
        node__->symtab = NULL;
 
3332
        node__->ordering = caring;
 
3333
        node__->num_dice = num_dice;
 
3334
        node__->num_sides = num_sides;
 
3335
        return (expression *)node__;
 
3336
}
 
3337
 
 
3338
expression *sumrepany_create(struct val_list * number, struct val_list * data)
 
3339
{
 
3340
        sumrepany *node__ = (sumrepany *)yynodealloc(sizeof(struct sumrepany__));
 
3341
        if(node__ == 0) return 0;
 
3342
        node__->vtable__ = &sumrepany_vt__;
 
3343
        node__->kind__ = sumrepany_kind;
 
3344
        node__->filename__ = yycurrfilename();
 
3345
        node__->linenum__ = yycurrlinenum();
 
3346
        node__->symtab = NULL;
 
3347
        node__->ordering = caring;
 
3348
        node__->number = number;
 
3349
        node__->data = data;
 
3350
        return (expression *)node__;
 
3351
}
 
3352
 
3031
3353
expression *scat_create(expression * expr1, expression * expr2)
3032
3354
{
3033
3355
        scat *node__ = (scat *)yynodealloc(sizeof(struct scat__));