File: ast.cpp

package info (click to toggle)
contextfree 3.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,432 kB
  • sloc: cpp: 34,857; lex: 416; makefile: 124; sh: 42; python: 41
file content (1079 lines) | stat: -rw-r--r-- 46,595 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
// ast.cpp
// this file is part of Context Free
// ---------------------
// Copyright (C) 2012-2014 John Horigan - john@glyphic.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//

#define _USE_MATH_DEFINES 1
#include "ast.h"
#include "astexpression.h"
#include "astreplacement.h"
#include "builder.h"
#include <cmath>
#include <cassert>
#include "rendererAST.h"
#include "attributes.h"
#include <cstdlib>
#include <cstring>

namespace AST {
    const std::map<expType, std::string> ASTparameter::typeNames =
    {
        {NoType, "no type"},
        {NumericType, "numeric type"},
        {ModType, "adjustment type"},
        {RuleType, "rule type"},
        {FlagType, "flag type"}
    };
    
    const std::map<Locality_t, std::string> ASTparameter::localityNames =
    {
        {UnknownLocal, "unknown locality"},
        {ImpureNonlocal, "impure non-local"},
        {PureNonlocal, "pure non-local"},
        {PureLocal, "pure local"}
    };


    void
    ASTparameter::init(int nameIndex, ASTdefine* def)
    {
        mType = def->mType;
        mLocality = def->mExpression ? def->mExpression->mLocality : PureLocal;
        mTuplesize = def->mTuplesize;
        
        if (mType == AST::NumericType) {
            isNatural = def->mExpression && def->mExpression->isNatural;
            if (mTuplesize == 0) mTuplesize = 1;    // loop index
            if (mTuplesize < 1 || mTuplesize > MaxVectorSize)
                CfdgError::Error(mLocation, "Illegal vector size (<1 or >99)");
        }
        
        mName = nameIndex;
        if (def->mDefineType == ASTdefine::ConstDefine)
            mDefinition = def;
    }
    
    expType
    decodeType(const std::string& typeName, int& mTuplesize,
               bool& isNatural, const yy::location& mLocation)
    {
        expType type = NoType;
        mTuplesize = 1;
        isNatural = false;
        
        if (typeName == "number") {
            type = AST::NumericType;
        } else if (typeName == "natural") {
            type = AST::NumericType;
            isNatural = true;
        } else if (typeName == "adjustment") {
            mTuplesize = ModificationSize;
            type = AST::ModType;
        } else if (typeName == "shape") {
            type = AST::RuleType;
        } else if (typeName.find("vector") == 0) {
            // Would have used a regex but gcc does not have support yet
            if (typeName.length() < 7 || !std::strchr("123456789", typeName[6]))
                CfdgError::Error(mLocation, "Illegal vector type specification");
            else {
                // Must make sure next char is not +/-, whitespace or 0 before
                // we can use strtol()
                char* tail = nullptr;
                errno = 0;
                long sz = std::strtol(typeName.c_str() + 6, &tail, 10);
                if ((tail && *tail != '\0') || errno == ERANGE) {
                    CfdgError::Error(mLocation, "Illegal vector type specification");
                } else if (sz <= 1 || sz > MaxVectorSize) {
                    CfdgError::Error(mLocation, "Illegal vector size (<=1 or >99)");
                } else {
                    type = AST::NumericType;
                    mTuplesize = static_cast<int>(sz);
                }
            }
        } else {
            type = AST::NoType;
            CfdgError::Error(mLocation, "Unrecognized type name");
        }
        
        return type;
    }
    
    void
    ASTparameter::init(const std::string& typeName, int nameIndex)
    {
        mLocality = PureNonlocal;
        mType = decodeType(typeName, mTuplesize, isNatural, mLocation);
        mName = nameIndex;
        mDefinition = nullptr;
    }
    
    ASTparameter::ASTparameter(const std::string& typeName, int nameIndex,
                 const yy::location& where)
    : mLocality(PureNonlocal), mLocation(where)
    { init(typeName, nameIndex); }
    
    ASTparameter::ASTparameter(int nameIndex, ASTdefine* def, const yy::location& where)
    : mLocation(where)
    { init(nameIndex, def); }
    
    ASTparameter::ASTparameter(int nameIndex, const yy::location& where)
    : mType(NumericType), isLoopIndex(true), mName(nameIndex), mLocation(where)
    { }     // ctor for loop variables
    
    ASTparameter::ASTparameter(const ASTparameter& from)
    : mType(from.mType), isParameter(from.isParameter), isLoopIndex(from.isLoopIndex),
      isNatural(from.isNatural), mLocality(from.mLocality), mName(from.mName),
      mLocation(from.mLocation), mDefinition(nullptr), mStackIndex(from.mStackIndex),
      mTuplesize(from.mTuplesize)
    {}
    
    ASTparameter&
    ASTparameter::operator=(const ASTparameter& from)
    {
        if (this == &from) return *this;
        mType = from.mType;
        isParameter = from.isParameter;
        isLoopIndex = from.isLoopIndex;
        isNatural = from.isNatural;
        mLocality = from.mLocality;
        mName = from.mName;
        mLocation = from.mLocation;
        mStackIndex = from.mStackIndex;
        mTuplesize = from.mTuplesize;
        mDefinition = from.mDefinition;
        return *this;
    }
    
    
    void
    ASTparameter::checkParam(const yy::location&, const yy::location& nameLoc)
    {
        if (mName == -1)
            CfdgError::Error(nameLoc, "Reserved keyword used for parameter name");
    }
    
    bool
    ASTparameter::operator!=(const ASTparameter& p) const
    {
        if (mType != p.mType) return true;
        if (mType == AST::NumericType &&
            mTuplesize != p.mTuplesize) return true;
        return false;
    }
    
    bool
    ASTparameter::operator!=(const ASTexpression& e) const
    {
        if (mType != e.mType) return true;
        if (mType == AST::NumericType &&
            mTuplesize != e.evaluate()) return true;
        return false;
    }
    
    int
    ASTparameter::CheckType(const ASTparameters* types, const ASTexpression* args,
                            const yy::location& where, bool checkNumber, Builder* b)
    {
        // Walks down the right edge of an expression tree checking that the types
        // of the children match the specified argument types
        if ((types == nullptr || types->empty()) && (args == nullptr)) return 0;

        if (types == nullptr || types->empty()) {
            CfdgError::Error(args->where, "Arguments are not expected.", b);
            return -1;
        }
        if (args == nullptr) {
            CfdgError::Error(where, "Arguments are expected.", b);
            return -1;
        }
        bool justCount = args->mType == AST::NoType;
        
        size_t size = 0;
        ASTparameters::const_iterator param_it = types->begin(),
                                      param_end = types->end();
        
        for (auto&& arg: *args) {
            if (param_it == param_end) {
                CfdgError::Error(args->where, "Too many arguments", b);
                return -1;
            }
            
            if (!justCount) {
                if (param_it->mType != arg.mType) {
                    CfdgError::Error(arg.where, "Incorrect argument type.", b);
                    CfdgError::Error(param_it->mLocation, "This is the expected type.", b);
                    return -1;
                }
                if (param_it->isNatural && !arg.isNatural && !b->impure()) {
                    CfdgError::Error(arg.where, "this expression does not satisfy the natural number requirement", b);
                    return -1;
                }
                if (param_it->mType == AST::NumericType &&
                    param_it->mTuplesize != arg.evaluate())
                {
                    if (param_it->mTuplesize == 1)
                        CfdgError::Error(arg.where, "This argument should be scalar", b);
                    else
                        CfdgError::Error(arg.where, "This argument should be a vector", b);
                    CfdgError::Error(param_it->mLocation, "This is the expected type.", b);
                    return -1;
                }
                if (arg.mLocality != PureLocal && arg.mLocality != PureNonlocal &&
                    param_it->mType == AST::NumericType && !param_it->isNatural &&
                    !b->impure() && checkNumber)
                {
                    CfdgError::Error(arg.where, "This expression does not satisfy the number parameter requirement", b);
                    return -1;
                }
            }
            size += param_it->mTuplesize;
            ++param_it;
        }
        
        if (param_it != param_end) {
            CfdgError::Error(args->where, "Not enough arguments.", b);
            CfdgError::Error(param_it->mLocation, "Expecting this argument.", b);
            return -1;
        }

        return static_cast<int>(size);
    }
    
    ASTexpression*
    ASTparameter::constCopy(const yy::location& where, const std::string& entropy) const
    {
        switch (mType) {
            case AST::NumericType: {
                std::vector<double> data(mTuplesize);
                bool natural = isNatural;
                int valCount = mDefinition->mExpression->evaluate(data.data(), mTuplesize);
                if (valCount != mTuplesize || valCount == 0)
                    CfdgError::Error(where, "Unexpected compile error.");                   // this also shouldn't happen
                if (valCount < 1 || data.empty())
                    return new ASTreal(0.0, where);     // shouldn't happen, but we don't want to crash if it does
                
                // Create a new cons-list based on the evaluated variable's expression
                ASTexpression* list = nullptr;
                for (auto value: data) {
                    ASTreal* r = new ASTreal(value, where);
                    if (!list) r->text = entropy;
                    list = list ? list->append(r) : r;
                }
                list->isNatural = natural;
                list->mLocality = mLocality;
                return list;
            }
            case AST::ModType: {
                ASTmodification* ret = nullptr;
                if (const ASTmodification* mod = dynamic_cast<const ASTmodification*>(mDefinition->mExpression.get()))
                    ret = new ASTmodification(*mod, where);
                else
                    ret = new ASTmodification(mDefinition->mChildChange, where);
                ret->mLocality = mLocality;
                return ret;
            }
            case AST::RuleType: {
                // This must be bound to an ASTruleSpecifier, otherwise it would not be constant
                if (const ASTruleSpecifier* r = dynamic_cast<const ASTruleSpecifier*> (mDefinition->mExpression.get())) {
                    ASTruleSpecifier* ret = new ASTruleSpecifier(r->shapeType, entropy, nullptr, where, nullptr);
                    ret->grab(r);
                    ret->mLocality = mLocality;
                    return ret;
                } else {
                    CfdgError::Error(where, "Internal error computing bound rule specifier");
                }
                break;
            }
            default:
                break;
        }
        return nullptr;
    }
    
    Locality_t
    CombineLocality(Locality_t first, Locality_t second)
    {
        return static_cast<Locality_t>(first & second);
    }

    double
    CFatof(const char* s)
    {
        double ret = atof(s);
        return strchr(s, '%') ? ret / 100.0 : ret;
    }
    
    void
    addUnique(SymmList& syms, agg::trans_affine& tr)
    {
        if (std::find(syms.begin(), syms.end(), tr) == syms.end())
            syms.push_back(tr);
    }

    void
    processDihedral(SymmList& syms, double order, double x, double y,
                    bool dihedral, double angle, const yy::location& where)
    {
        if (order < 1.0)
            CfdgError::Error(where, "Rotational symmetry order must be one or larger");
        agg::trans_affine reg;
        agg::trans_affine_reflection mirror(angle);
        reg.translate(-x, -y);
        int num = static_cast<int>(order);
        order = 2.0 * M_PI / order;
        for (int i = 0; i < num; ++i) {
            agg::trans_affine tr(reg);
            if (i) tr.rotate(i * order);
            agg::trans_affine tr2(tr);
            tr2 *= mirror;
            tr.translate(x, y);
            tr2.translate(x, y);
            addUnique(syms, tr);
            if (dihedral) addUnique(syms, tr2);
        }
    }

    // Analyze the symmetry spec accumulated in the data vector and add the 
    // appropriate affine transforms to the SymmList. Avoid adding the identity
    // transform if it is already present in the SymmList.
    void
    processSymmSpec(SymmList& syms, agg::trans_affine& tile, bool tiled,
                    std::vector<double>& data, const yy::location& where)
    {
        if (data.empty()) return;
        AST::FlagTypes t = static_cast<AST::FlagTypes>(static_cast<int>(data[0]));
        bool frieze = (tile.sx != 0.0 || tile.sy != 0.0) && (tile.sx * tile.sy == 0.0);
        bool rhombic = tiled && ((fabs(tile.shy) <= 0.0000001 && fabs(tile.shx/tile.sx - 0.5) < 0.0000001) ||
                                 (fabs(tile.shx) <= 0.0000001 && fabs(tile.shy/tile.sy - 0.5) < 0.0000001));
        bool rectangular = tiled && tile.shx == 0.0 && tile.shy == 0.0;
        bool square = rectangular && tile.sx == tile.sy;
        bool hexagonal = false;
        bool square45 = false;
        double size45 = tile.sx;
        if (rhombic) {
            double x1 = 1.0, y1 = 0.0;
            tile.transform(&x1, &y1);
            double dist10 = sqrt(x1 * x1 + y1 * y1);
            double x2 = 0.0, y2 = 1.0;
            tile.transform(&x2, &y2);
            double dist01 = sqrt(x2 * x2 + y2 * y2);
            hexagonal = fabs(dist10/dist01 - 1.0) < 0.0000001;
            square45 = fabs(dist01/dist10 - M_SQRT2) < 0.0000001 ||
            fabs(dist10/dist01 - M_SQRT2) < 0.0000001;
            size45 = fmin(dist01, dist10);
        }
        static const agg::trans_affine_reflection ref45(M_PI_4);
        static const agg::trans_affine_reflection ref135(-M_PI_4);
        
        if (t >= AST::CF_P11G && t <= AST::CF_P2MM && !frieze)
            CfdgError::Error(where, "Frieze symmetry only works in frieze designs");
        if (t >= AST::CF_PM && t <= AST::CF_P6M && !tiled)
            CfdgError::Error(where, "Wallpaper symmetry only works in tiled designs");
        if (t == AST::CF_P2 && !frieze && !tiled)
            CfdgError::Error(where, "p2 symmetry only works in frieze or tiled designs");
        switch (t) {
            case AST::CF_CYCLIC: {
                double order, x = 0.0, y = 0.0;
                switch (data.size()) {
                    case 4:
                        x = data[2];
                        y = data[3];
			FALLTHROUGH;
                    case 2:
                        order = data[1];
                        break;
                    default:
                        CfdgError::Error(where, "Cyclic symmetry requires an order argument and an optional center of rotation");
                        order = 1.0;    // suppress warning, never executed
                        break;  // never gets here
                }
                processDihedral(syms, order, x, y, false, 0.0, where);
                break;
            }
            case AST::CF_DIHEDRAL: {
                double order, angle = 0.0, x = 0.0, y = 0.0;
                switch (data.size()) {
                    case 5:
                        x = data[3];
                        y = data[4];
			FALLTHROUGH;
                    case 3:
                        order = data[1];
                        angle = data[2] * M_PI / 180.0;
                        break;
                    case 4:
                        x = data[2];
                        y = data[3];
			FALLTHROUGH;
                    case 2:
                        order = data[1];
                        break;
                    default:
                        CfdgError::Error(where, "Dihedral symmetry requires an order argument, an optional mirror angle, and an optional center of rotation");
                        order = 1.0;    // suppress warning, never executed
                        break;  // never gets here
                }
                processDihedral(syms, order, x, y, true, angle, where);
                break;
            }
            case AST::CF_P11G: {
                double mirrorx = 0.0, mirrory = 0.0;
                if (data.size() == 2) {
                    if (tile.sx != 0.0)
                        mirrory = data[1];
                    else
                        mirrorx = data[1];
                } else if (data.size() > 2) {
                    CfdgError::Error(where, "p11g symmetry takes no arguments or an optional glide axis position argument");
                }
                agg::trans_affine tr;
                addUnique(syms, tr);
                tr.translate(-mirrorx, -mirrory);
                if (tile.sx != 0.0)
                    tr.flip_y();
                else
                    tr.flip_x();
                tr.translate(tile.sx * 0.5 + mirrorx, tile.sy * 0.5 + mirrory);
                addUnique(syms, tr);
                break;
            }
            case AST::CF_P11M: {
                double mirrorx = 0.0, mirrory = 0.0;
                if (data.size() == 2) {
                    if (tile.sx != 0.0)
                        mirrory = data[1];
                    else
                        mirrorx = data[1];
                } else if (data.size() > 2) {
                    CfdgError::Error(where, "p11m symmetry takes no arguments or an optional mirror axis position argument");
                }
                agg::trans_affine tr;
                addUnique(syms, tr);
                tr.translate(-mirrorx, -mirrory);
                if (tile.sx != 0.0)
                    tr.flip_y();
                else
                    tr.flip_x();
                tr.translate(mirrorx, mirrory);
                addUnique(syms, tr);
                break;
            }
            case AST::CF_P1M1: {
                double mirrorx = 0.0, mirrory = 0.0;
                if (data.size() == 2) {
                    if (tile.sx != 0.0)
                        mirrorx = data[1];
                    else
                        mirrory = data[1];
                } else if (data.size() > 2) {
                    CfdgError::Error(where, "p1m1 symmetry takes no arguments or an optional mirror axis position argument");
                }
                agg::trans_affine tr;
                addUnique(syms, tr);
                tr.translate(-mirrorx, -mirrory);
                if (tile.sx != 0.0)
                    tr.flip_x();
                else
                    tr.flip_y();
                tr.translate(mirrorx, mirrory);
                addUnique(syms, tr);
                break;
            }
            case AST::CF_P2: {
                double mirrorx = 0.0, mirrory = 0.0;
                if (data.size() == 3) {
                    mirrorx = data[1];
                    mirrory = data[2];
                } else if (data.size() != 1) {
                    CfdgError::Error(where, "p2 symmetry takes no arguments or a center of rotation");
                }
                processDihedral(syms, 2.0, mirrorx, mirrory, false, 0.0, where);
                break;
            }
            case AST::CF_P2MG: {
                double mirrorx = 0.0, mirrory = 0.0;
                if (data.size() == 3) {
                    mirrorx = data[1];
                    mirrory = data[2];
                } else if (data.size() != 1) {
                    CfdgError::Error(where, "p2mg symmetry takes no arguments or a center of rotation");
                }
                agg::trans_affine tr1;
                agg::trans_affine_translation tr2(-mirrorx, -mirrory);
                agg::trans_affine_translation tr3(-mirrorx, -mirrory);
                agg::trans_affine_translation tr4(-mirrorx, -mirrory);
                tr2.flip_x();
                tr3.flip_x();
                tr3.flip_y();
                tr4.flip_y();
                tr2.translate(tile.sx * 0.5 + mirrorx, tile.sy * 0.5 + mirrory);
                tr3.translate(mirrorx, mirrory);
                tr4.translate(tile.sx * 0.5 + mirrorx, tile.sy * 0.5 + mirrory);
                addUnique(syms, tr1);
                addUnique(syms, tr2);
                addUnique(syms, tr3);
                addUnique(syms, tr4);
                break;
            }
            case AST::CF_P2MM: {
                double mirrorx = 0.0, mirrory = 0.0;
                if (data.size() == 3) {
                    mirrorx = data[1];
                    mirrory = data[2];
                } else if (data.size() != 1) {
                    CfdgError::Error(where, "p2mm symmetry takes no arguments or a center of relection");
                }
                processDihedral(syms, 2.0, mirrorx, mirrory, true, 0.0, where);
                break;
            }
            case AST::CF_PM: {
                if (!rectangular && !square45) {
                    CfdgError::Error(where, "pm symmetry requires rectangular tiling");
                }
                double offset = 0.0;
                switch (data.size()) {
                    case 2:
                        break;
                    case 3:
                        offset = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "pm symmetry takes a mirror axis argument and an optional axis position argument");
                }
                agg::trans_affine tr;
                addUnique(syms, tr);
                int axis = static_cast<int>(data[1]);
                if (rectangular && (axis < 0 || axis > 1))
                    CfdgError(where, "pm symmetry mirror axis argument must be 0 or 1");
                else if (square45 && (axis < 2 || axis > 3))
                    CfdgError::Error(where, "pm symmetry mirror axis argument must be 2 or 3");
                switch (axis) {
                    case 0:         // mirror on x axis
                        tr.translate(0, -offset);
                        tr.flip_y();
                        tr.translate(0, offset);
                        break;
                    case 1:         // mirror on y axis
                        tr.translate(-offset, 0);
                        tr.flip_x();
                        tr.translate(offset, 0);
                        break;
                    case 2:         // mirror on x=y axis
                        tr.translate(-offset * M_SQRT1_2,  offset * M_SQRT1_2);
                        tr *= ref45;
                        tr.translate( offset * M_SQRT1_2, -offset * M_SQRT1_2);
                        break;
                    case 3:         // mirror on x=-y axis
                        tr.translate(-offset * M_SQRT1_2, -offset * M_SQRT1_2);
                        tr *= ref135;
                        tr.translate( offset * M_SQRT1_2,  offset * M_SQRT1_2);
                        break;
                    default:
                        CfdgError::Error(where, "pm symmetry mirror axis argument must be 0, 1, 2, or 3");
                        break;
                }
                addUnique(syms, tr);
                break;
            }
            case AST::CF_PG: {
                if (!rectangular && !square45) {
                    CfdgError::Error(where, "pg symmetry requires rectangular tiling");
                }
                double offset = 0.0;
                switch (data.size()) {
                    case 2:
                        break;
                    case 3:
                        offset = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "pg symmetry takes a glide axis argument and an optional axis position argument");
                }
                agg::trans_affine tr;
                addUnique(syms, tr);
                int axis = static_cast<int>(data[1]);
                if (rectangular && (axis < 0 || axis > 1))
                    CfdgError(where, "pg symmetry mirror axis argument must be 0 or 1");
                else if (square45 && (axis < 2 || axis > 3))
                    CfdgError::Error(where, "pg symmetry mirror axis argument must be 2 or 3");
                switch (axis) {
                    case 0:         // mirror on x axis
                        tr.translate(0, -offset);
                        tr.flip_y();
                        tr.translate(tile.sx * 0.5, offset);
                        break;
                    case 1:         // mirror on y axis
                        tr.translate(-offset, 0);
                        tr.flip_x();
                        tr.translate(offset, tile.sy * 0.5);
                        break;
                    case 2:         // mirror on x=y axis
                        tr.translate(-offset * M_SQRT1_2,  offset * M_SQRT1_2);
                        tr *= ref45;
                        tr.translate(( offset + size45 * 0.5) * M_SQRT1_2, (-offset + size45 * 0.5) * M_SQRT1_2);
                        break;
                    case 3:         // mirror on x=-y axis
                        tr.translate(-offset * M_SQRT1_2, -offset * M_SQRT1_2);
                        tr *= ref135;
                        tr.translate(( offset - size45 * 0.5) * M_SQRT1_2, ( offset + size45 * 0.5) * M_SQRT1_2);
                        break;
                    default:
                        CfdgError::Error(where, "pg symmetry glide axis argument must be 0, 1, 2, or 3");
                        break;
                }
                addUnique(syms, tr);
                break;
            }
            case AST::CF_CM: {
                if (!rhombic && !square) {
                    CfdgError::Error(where, "cm symmetry requires diamond tiling");
                }
                double offset = 0.0;
                switch (data.size()) {
                    case 2:
                        break;
                    case 3:
                        offset = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "cm symmetry takes a mirror axis argument and an optional axis position argument");
                }
                agg::trans_affine tr;
                addUnique(syms, tr);
                int axis = static_cast<int>(data[1]);
                if (rhombic && (axis < 0 || axis > 1))
                    CfdgError(where, "cm symmetry mirror axis argument must be 0 or 1");
                else if (square && (axis < 2 || axis > 3))
                    CfdgError::Error(where, "cm symmetry mirror axis argument must be 2 or 3");
                switch (axis) {
                    case 0:         // mirror on x axis
                        tr.translate(0, -offset);
                        tr.flip_y();
                        tr.translate(0, offset);
                        break;
                    case 1:         // mirror on y axis
                        tr.translate(-offset, 0);
                        tr.flip_x();
                        tr.translate(offset, 0);
                        break;
                    case 2:         // mirror on x=y axis
                        tr.translate( offset * M_SQRT1_2, -offset * M_SQRT1_2);
                        tr *= ref45;
                        tr.translate(-offset * M_SQRT1_2,  offset * M_SQRT1_2);
                        break;
                    case 3:         // mirror on x=-y axis
                        tr.translate(-offset * M_SQRT1_2, -offset * M_SQRT1_2);
                        tr *= ref135;
                        tr.translate( offset * M_SQRT1_2,  offset * M_SQRT1_2);
                        break;
                    default:
                        CfdgError::Error(where, "cm symmetry mirror axis argument must be 0, 1, 2, or 3");
                        break;
                }
                addUnique(syms, tr);
                break;
            }
            case AST::CF_PMM: {
                if (!rectangular && !square45) {
                    CfdgError::Error(where, "pmm symmetry requires rectangular tiling");
                }
                double centerx = 0.0, centery = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        centerx = data[1];
                        centery = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "pmm symmetry takes no arguments or a center of reflection");
                }
                processDihedral(syms, 2.0, centerx, centery, true, square45 ? M_PI_4 : 0.0, where);
                break;
            }
            case AST::CF_PMG: {
                if (!rectangular && !square45) {
                    CfdgError::Error(where, "pmg symmetry requires rectangular tiling");
                }
                double centerx = 0.0, centery = 0.0;
                switch (data.size()) {
                    case 2:
                        break;
                    case 4:
                        centerx = data[2];
                        centery = data[3];
                        break;
                    default:
                        CfdgError::Error(where, "pmg symmetry takes a mirror axis argument and an optional center of reflection");
                }
                agg::trans_affine tr, tr2;
                int axis = static_cast<int>(data[1]);
                if (rectangular && (axis < 0 || axis > 1))
                    CfdgError(where, "pmg symmetry mirror axis argument must be 0 or 1");
                else if (square45 && (axis < 2 || axis > 3))
                    CfdgError::Error(where, "pmg symmetry mirror axis argument must be 2 or 3");
                switch (axis) {
                    case 0: {       // mirror on x axis
                        double cy = fabs(centery + 0.25 * tile.sy) < fabs(centery - 0.25 * tile.sy) ?
                        centery + 0.25 * tile.sy : centery - 0.25 * tile.sy;
                        processDihedral(syms, 2.0, centerx, cy, false, 0.0, where);
                        tr.translate(-centerx, 0.0);
                        tr.flip_x();
                        tr.translate(centerx, 0.5 * tile.sy);
                        addUnique(syms, tr);
                        tr2.translate(0.0, -centery);
                        tr2.flip_y();
                        tr2.translate(0.0, centery);
                        addUnique(syms, tr2);
                        break;
                    }
                    case 1: {       // mirror on y axis
                        double cx = fabs(centerx + 0.25 * tile.sx) < fabs(centerx - 0.25 * tile.sx) ?
                        centerx + 0.25 * tile.sx : centerx - 0.25 * tile.sx;
                        processDihedral(syms, 2.0, cx, centery, false, 0.0, where);
                        tr.translate(-centerx, 0.0);
                        tr.flip_x();
                        tr.translate(centerx, 0.0);
                        addUnique(syms, tr);
                        tr2.translate(0.0, -centery);
                        tr2.flip_y();
                        tr2.translate(0.5 * tile.sx, centery);
                        addUnique(syms, tr2);
                        break;
                    }
                    case 2: {       // mirror on x=y axis
                        double cx  = centerx - 0.25 * M_SQRT1_2 * size45;
                        double cy  = centery + 0.25 * M_SQRT1_2 * size45;
                        double cx2 = centerx + 0.25 * M_SQRT1_2 * size45;
                        double cy2 = centery - 0.25 * M_SQRT1_2 * size45;
                        if (cx2 * cx2 + cy2 * cy2 < cx * cx + cy * cy) {
                            cx = cx2;
                            cy = cy2;
                        }
                        processDihedral(syms, 2.0, cx, cy, false, 0.0, where);
                        tr.translate(-centerx, -centery);   // mirror on x=y
                        tr *= ref45;
                        tr.translate( centerx,  centery);
                        addUnique(syms, tr);
                        tr2.translate(-centerx, -centery);   // glide on x=-y
                        tr2 *= ref135;
                        tr2.translate(centerx - size45 * M_SQRT1_2 * 0.5, centery + size45 * M_SQRT1_2 * 0.5);
                        addUnique(syms, tr2);
                        break;
                    }
                    case 3: {       // mirror on x=-y axis
                        double cx  = centerx + 0.25 * M_SQRT1_2 * size45;
                        double cy  = centery + 0.25 * M_SQRT1_2 * size45;
                        double cx2 = centerx - 0.25 * M_SQRT1_2 * size45;
                        double cy2 = centery - 0.25 * M_SQRT1_2 * size45;
                        if (cx2 * cx2 + cy2 * cy2 < cx * cx + cy * cy) {
                            cx = cx2;
                            cy = cy2;
                        }
                        processDihedral(syms, 2.0, cx, cy, false, 0.0, where);
                        tr.translate(-centerx, -centery);   // mirror on x=-y
                        tr *= ref135;
                        tr.translate( centerx,  centery);
                        addUnique(syms, tr);
                        tr2.translate(-centerx, -centery);   // glide on x=y
                        tr2 *= ref45;
                        tr2.translate(centerx + size45 * M_SQRT1_2 * 0.5, centery + size45 * M_SQRT1_2 * 0.5);
                        addUnique(syms, tr2);
                        break;
                    }
                    default:
                        CfdgError::Error(where, "pmg symmetry mirror axis argument must be 0, 1, 2, or 3");
                        break;
                }
                break;
            }
            case AST::CF_PGG: {
                if (!rectangular && !square45) {
                    CfdgError::Error(where, "pgg symmetry requires rectangular tiling");
                }
                double centerx = 0.0, centery = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        centerx = data[1];
                        centery = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "pgg symmetry takes no arguments or a center of glide axis intersection");
                }
                if (square45) {
                    double cx = centerx + 0.25 * M_SQRT2 * size45;
                    double cy = centery;
                    double cx2 = centerx - 0.25 * M_SQRT2 * size45;
                    double cy2 = centery;
                    if (cx2*cx2 + cy2*cy2 < cx*cx + cy*cy) {
                        cx = cx2;
                        cy = cy2;
                    }
                    cx2 = centerx;
                    cy2 = centery + 0.25 * M_SQRT2 * size45;
                    if (cx2*cx2 + cy2*cy2 < cx*cx + cy*cy) {
                        cx = cx2;
                        cy = cy2;
                    }
                    cx2 = centerx;
                    cy2 = centery - 0.25 * M_SQRT2 * size45;
                    if (cx2*cx2 + cy2*cy2 < cx*cx + cy*cy) {
                        cx = cx2;
                        cy = cy2;
                    }
                    processDihedral(syms, 2.0, cx, cy, false, 0.0, where);
                    agg::trans_affine tr, tr2;
                    tr.translate(-centerx, -centery);   // glide on x=y
                    tr *= ref45;
                    tr.translate(centerx + size45 * M_SQRT1_2 * 0.5, centery + size45 * M_SQRT1_2 * 0.5);
                    addUnique(syms, tr);
                    tr2.translate(-centerx, -centery);   // glide on x=-y
                    tr2 *= ref135;
                    tr2.translate(centerx - size45 * M_SQRT1_2 * 0.5, centery + size45 * M_SQRT1_2 * 0.5);
                    addUnique(syms, tr2);
                    break;
                }
                double cx = fabs(centerx + 0.25 * tile.sx) < fabs(centerx - 0.25 * tile.sx) ?
                centerx + 0.25 * tile.sx : centerx - 0.25 * tile.sx;
                double cy = fabs(centery + 0.25 * tile.sy) < fabs(centery - 0.25 * tile.sy) ?
                centery + 0.25 * tile.sy : centery - 0.25 * tile.sy;
                processDihedral(syms, 2.0, cx, cy, false, 0.0, where);
                agg::trans_affine tr, tr2;
                tr.translate(-centerx, 0.0);
                tr.flip_x();
                tr.translate(centerx, 0.5 * tile.sy);
                addUnique(syms, tr);
                tr2.translate(0.0, -centery);
                tr2.flip_y();
                tr2.translate(0.5 * tile.sx, centery);
                addUnique(syms, tr2);
                break;
            }
            case AST::CF_CMM: {
                if (!rhombic && !square) {
                    CfdgError::Error(where, "cmm symmetry requires diamond tiling");
                }
                double centerx = 0.0, centery = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        centerx = data[1];
                        centery = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "cmm symmetry takes no arguments or a center of reflection");
                }
                processDihedral(syms, 2.0, centerx, centery, true, square45 ? M_PI_4 : 0.0, where);
                break;
            }
            case AST::CF_P4:
            case AST::CF_P4M: {
                if (!square && !square45) {
                    CfdgError::Error(where, "p4 & p4m symmetry requires square tiling");
                }
                double x = 0.0, y = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        x = data[1];
                        y = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "p4 & p4m symmetry takes no arguments or a center of rotation");
                }
                processDihedral(syms, 4.0, x, y, t == AST::CF_P4M, square ? M_PI_4 : 0.0, where);
                break;
            }
            case AST::CF_P4G: {
                if (!square && !square45) {
                    CfdgError::Error(where, "p4g symmetry requires square tiling");
                }
                double centerx = 0.0, centery = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        centerx = data[1];
                        centery = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "p4g symmetry takes no arguments or a center of rotation");
                }
                agg::trans_affine reg;
                reg.translate(-centerx, -centery);
                agg::trans_affine glide(reg);
                if (square45) {
                    glide.translate(-size45 * 0.25 * M_SQRT1_2, -size45 * 0.25 * M_SQRT1_2);
                    glide *= ref135;
                    glide.translate(-size45 * 0.25 * M_SQRT1_2,  size45 * 0.75 * M_SQRT1_2);
                } else {
                    glide.translate(tile.sx * 0.25, 0.0);
                    glide.flip_x();
                    glide.translate(-tile.sx * 0.25, tile.sy * 0.5);
                }
                for (int i = 0; i < 4; ++i) {
                    agg::trans_affine tr(reg), tr2(glide);
                    if (i) {
                        tr.rotate(i * M_PI_2);
                        tr2.rotate(i * M_PI_2);
                    }
                    tr.translate(centerx, centery);
                    tr2.translate(centerx, centery);
                    addUnique(syms, tr);
                    addUnique(syms, tr2);
                }
                break;
            }
            case AST::CF_P3: {
                if (!hexagonal) {
                    CfdgError::Error(where, "p3 symmetry requires hexagonal tiling");
                }
                double x = 0.0, y = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        x = data[1];
                        y = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "p3 symmetry takes no arguments or a center of rotation");
                }
                processDihedral(syms, 3.0, x, y, false, 0.0, where);
                break;
            }
            case AST::CF_P3M1:
            case AST::CF_P31M: {
                if (!hexagonal) {
                    CfdgError::Error(where, "p3m1 & p31m symmetry requires hexagonal tiling");
                }
                double x = 0.0, y = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        x = data[1];
                        y = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "p3m1 & p31m symmetry takes no arguments or a center of rotation");
                }
                bool deg30 = (fabs(tile.shx) <= 0.000001) != (t == AST::CF_P3M1);
                double angle = M_PI / (deg30 ? 6.0 : 3.0);
                processDihedral(syms, 3.0, x, y, true, angle, where);
                break;
            }
            case AST::CF_P6:
            case AST::CF_P6M: {
                if (!hexagonal) {
                    CfdgError::Error(where, "p6 & p6m symmetry requires hexagonal tiling");
                }
                double x = 0.0, y = 0.0;
                switch (data.size()) {
                    case 1:
                        break;
                    case 3:
                        x = data[1];
                        y = data[2];
                        break;
                    default:
                        CfdgError::Error(where, "p6 & p6m symmetry takes no arguments or a center of rotation");
                }
                processDihedral(syms, 6.0, x, y, t == AST::CF_P6M, 0.0, where);
                break;
            }
            default:
                CfdgError::Error(where, "Unknown symmetry type");
                break;  // never gets here
        }
        data.clear();
    }
    
    std::vector<const ASTmodification*>
    getTransforms(const ASTexpression* e, SymmList& syms, RendererAST* r, 
                  bool tiled, agg::trans_affine& tile)
    {
        std::vector<const ASTmodification*> ret;
        syms.clear();
        if (e == nullptr) return ret;
        
        std::vector<double> symmSpec;
        yy::location where;
        bool snarfFlagOpts = false;
        for (auto&& kid: *e) {
            switch (kid.mType) {
                case FlagType:
                    if (snarfFlagOpts)
                        processSymmSpec(syms, tile, tiled, symmSpec, where);
                    // Snarf and process the numeric arguments for the symmetry spec
                    where = kid.where;
                    snarfFlagOpts = true;
                    FALLTHROUGH;
                case NumericType:
                    if (snarfFlagOpts) {
                        int sz = kid.evaluate();
                        if (sz < 1) {
                            CfdgError::Error(kid.where, "Could not evaluate this");
                        } else {
                            size_t oldsize = symmSpec.size();
                            symmSpec.resize(oldsize + sz);
                            if (kid.evaluate(symmSpec.data() + oldsize, sz, r) != sz)
                                CfdgError::Error(kid.where, "Could not evaluate this");
                        }
                        where = where + kid.where;
                    } else {
                        CfdgError::Error(kid.where, "Symmetry flag expected here");
                    }
                    break;
                case ModType:
                    if (snarfFlagOpts)
                        processSymmSpec(syms, tile, tiled, symmSpec, where);
                    snarfFlagOpts = false;
                    if (const ASTmodification* m = dynamic_cast<const ASTmodification*>(&kid)) {
                        if ((m->modClass &
                             (ASTmodification::GeomClass | ASTmodification::PathOpClass)) ==
                            m->modClass && (r || m->isConstant))
                        {
                            Modification mod;
                            kid.evaluate(mod, false, r);
                            addUnique(syms, mod.m_transform);
                        } else {
                            ret.push_back(m);
                        }
                    } else {
                        CfdgError::Error(kid.where, "Wrong type");
                    }
                    break;
                default:
                    CfdgError::Error(kid.where, "Wrong type");
                    break;
            }
        }
        if (snarfFlagOpts)
            processSymmSpec(syms, tile, tiled, symmSpec, where);
        return ret;
    }
}