Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt/taprimitive.py
blob: 197a8d821e3ecb80470111dcfcf6e3854d8348d4 (plain)
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
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
#Copyright (c) 2013 Marion Zepf

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import ast
from gettext import gettext as _
from math import sqrt
from random import uniform
import traceback

#from ast_pprint import * # only used for debugging, safe to comment out

from tablock import Media
from tacanvas import TurtleGraphics
from taconstants import (Color, CONSTANTS)
from talogo import (LogoCode, logoerror, NegativeRootError)
from taturtle import (Turtle, Turtles)
from TurtleArt.tatype import (TYPE_CHAR, TYPE_INT, TYPE_FLOAT, TYPE_OBJECT,
                              TYPE_MEDIA, TYPE_COLOR, BOX_AST, ACTION_AST,
                              Type, TypeDisjunction, TATypeError, get_type,
                              TypedSubscript, TypedName, is_bound_method,
                              is_instancemethod, is_staticmethod,
                              identity, get_converter, convert, get_call_ast)
from tautils import debug_output
from tawindow import (TurtleArtWindow, global_objects, plugins_in_use)
from util import ast_extensions


class PyExportError(BaseException):
    """ Error that is raised when something goes wrong while converting the
    blocks to python code """

    def __init__(self, message, block=None):
        """ message -- the error message
        block -- the block where the error occurred """
        self.message = message
        self.block = block

    def __str__(self):
        if self.block is not None:
            return _("error in highlighted block") + ": " + str(self.message)
        else:
            return _("error") + ": " + str(self.message)


class Primitive(object):
    """ Something that can be called when the block code is executed in TA,
    but that can also be transformed into a Python AST."""

    _DEBUG = False

    STANDARD_OPERATORS = {'plus': (ast.UAdd, ast.Add),
                          'minus': (ast.USub, ast.Sub),
                          'multiply': ast.Mult,
                          'divide': ast.Div,
                          'modulo': ast.Mod,
                          'power': ast.Pow,
                          'and_': ast.And,
                          'or_': ast.Or,
                          'not_': ast.Not,
                          'equals': ast.Eq,
                          'less': ast.Lt,
                          'greater': ast.Gt}

    def __init__(self, func, return_type=TYPE_OBJECT, arg_descs=None,
                 kwarg_descs=None, call_afterwards=None, export_me=True):
        """ return_type -- the type (from the type hierarchy) that this
            Primitive will return
        arg_descs, kwarg_descs -- a list of argument descriptions and
            a dictionary of keyword argument descriptions. An argument
            description can be either an ArgSlot or a ConstantArg.
        call_afterwards -- Code to call after this Primitive has been called
            (e.g., for updating labels in LogoCode) (not used for creating
            AST)
        export_me -- True iff this Primitive should be exported to Python
            code (the default case) """
        self.func = func
        self.return_type = return_type

        if arg_descs is None:
            self.arg_descs = []
        else:
            self.arg_descs = arg_descs

        if kwarg_descs is None:
            self.kwarg_descs = {}
        else:
            self.kwarg_descs = kwarg_descs

        self.call_afterwards = call_afterwards
        self.export_me = export_me

    def copy(self):
        """ Return a Primitive object with the same attributes as this one.
        Shallow-copy the arg_descs and kwarg_descs attributes. """
        arg_descs_copy = self.arg_descs[:]
        if isinstance(self.arg_descs, ArgListDisjunction):
            arg_descs_copy = ArgListDisjunction(arg_descs_copy)
        return Primitive(self.func,
                         return_type=self.return_type,
                         arg_descs=arg_descs_copy,
                         kwarg_descs=self.kwarg_descs.copy(),
                         call_afterwards=self.call_afterwards,
                         export_me=self.export_me)

    def __repr__(self):
        return "Primitive(%s -> %s)" % (repr(self.func), str(self.return_type))

    @property
    def __name__(self):
        return self.func.__name__

    def get_name_for_export(self):
        """ Return the expression (as a string) that represents this Primitive
        in the exported Python code, e.g., 'turtle.forward'. """
        func_name = ""
        if self.wants_turtle():
            func_name = "turtle."
        elif self.wants_turtles():
            func_name = "turtles."
        elif self.wants_canvas():
            func_name = "canvas."
        elif self.wants_logocode():
            func_name = "logo."
        elif self.wants_heap():
            func_name = "logo.heap."
        elif self.wants_tawindow():
            func_name = "tw."
        else:
            results, plugin = self.wants_plugin()
            if results:
                for k in global_objects.keys():
                    if k == plugin:
                        if k not in plugins_in_use:
                            plugins_in_use.append(k)
                        func_name = k.lower() + '.'
                        break

        # get the name of the function directly from the function itself
        func_name += self.func.__name__
        return func_name

    def are_slots_filled(self):
        """ Return True iff none of the arg_descs or kwarg_descs is an
        ArgSlot. """
        for arg_desc in self.arg_descs:
            if isinstance(arg_desc, ArgSlot):
                return False
        for key in self.kwarg_descs:
            if isinstance(self.kwarg_descs[key], ArgSlot):
                return False
        return True

    def fill_slots(self, arguments=None, keywords=None, convert_to_ast=False,
                   call_my_args=True):
        """ Return a copy of this Primitive whose ArgSlots are filled with
        the given arguments, turned into ConstantArgs. Call the arguments,
        apply their wrappers, and check their types as appropriate. """
        if arguments is None:
            arguments = []
        if keywords is None:
            keywords = {}

        new_prim = self.copy()

        if isinstance(new_prim.arg_descs, ArgListDisjunction):
            slot_list_alternatives = list(new_prim.arg_descs)
        else:
            slot_list_alternatives = [new_prim.arg_descs]

        # arguments
        error = None
        filler = None
        for slot_list in slot_list_alternatives:
            error = None
            new_slot_list = []
            filler_list = list(arguments[:])
            for slot in slot_list:
                if isinstance(slot, ArgSlot):
                    filler = filler_list.pop(0)
                    try:
                        const = slot.fill(filler,
                                          convert_to_ast=convert_to_ast,
                                          call_my_args=call_my_args)
                    except TATypeError as error:
                        if Primitive._DEBUG:
                            traceback.print_exc()
                        break
                    else:
                        new_slot_list.append(const)
                else:
                    new_slot_list.append(slot)
            if error is None:
                new_prim.arg_descs = new_slot_list
                break
        if error is not None:
            raise error

        # keyword arguments
        for key in keywords:
            kwarg_desc = new_prim.kwarg_descs[key]
            if isinstance(kwarg_desc, ArgSlot):
                const = kwarg_desc.fill(keywords[key],
                                        convert_to_ast=convert_to_ast,
                                        call_my_args=call_my_args)
                new_prim.kwarg_descs[key] = const

        return new_prim

    def get_values_of_filled_slots(self, exportable_only=False):
        """ Return the values of all filled argument slots as a list, and
        the values of all filled keyword argument slots as a dictionary.
        Ignore all un-filled (keyword) argument slots.
        exportable_only -- return only exportable values and convert values
            to ASTs instead of calling them """
        new_args = []
        for c_arg in self.arg_descs:
            if (isinstance(c_arg, ConstantArg)
                and (not exportable_only
                     or export_me(c_arg.value))):
                new_args.append(c_arg.get(convert_to_ast=exportable_only))
        new_kwargs = {}
        for key in self.kwarg_descs:
            if (isinstance(self.kwarg_descs[key], ConstantArg)
                and (not exportable_only
                     or export_me(self.kwarg_descs[key].value))):
                new_kwargs[key] = self.kwarg_descs[key].get(
                    convert_to_ast=exportable_only)
        return (new_args, new_kwargs)

    def allow_call_args(self, recursive=False):
        """ Set call_args attribute of all argument descriptions to True
        recursive -- recursively call allow_call_args on all constant args
            that are Primitives """
        for arg_desc in self.arg_descs:
            arg_desc.call_arg = True
            if (recursive and isinstance(arg_desc, ConstantArg) and
                    isinstance(arg_desc.value, Primitive)):
                arg_desc.value.allow_call_args(recursive=True)
        for kwarg_desc in self.kwarg_descs:
            kwarg_desc.call_arg = True
            if (recursive and isinstance(kwarg_desc, ConstantArg) and
                    isinstance(kwarg_desc.value, Primitive)):
                kwarg_desc.value.allow_call_args(recursive=True)

    def __call__(self, *runtime_args, **runtime_kwargs):
        """ Execute the function, passing it the arguments received at
        runtime. Also call the function in self.call_afterwards and pass it
        all runtime_args and runtime_kwargs.
        If the very first argument is a LogoCode instance, it is removed.
        The active turtle, the Turtles object, the canvas, the LogoCode
        object, or the TurtleArtWindow object will be prepended to the
        arguments (depending on what this Primitive wants). """

        # remove the first argument if it is a LogoCode instance
        if runtime_args and isinstance(runtime_args[0], LogoCode):
            runtime_args = runtime_args[1:]

        if Primitive._DEBUG:
            debug_output(repr(self))
            debug_output("  runtime_args: " + repr(runtime_args))
        # fill the ArgSlots with the runtime arguments
        new_prim = self.fill_slots(runtime_args, runtime_kwargs,
                                   convert_to_ast=False)
        if not new_prim.are_slots_filled():
            raise logoerror("#syntaxerror")
        if Primitive._DEBUG:
            debug_output("  new_prim.arg_descs: " + repr(new_prim.arg_descs))

        # extract the actual values from the (now constant) arguments
        (new_args, new_kwargs) = new_prim.get_values_of_filled_slots()
        if Primitive._DEBUG:
            debug_output("  new_args: " + repr(new_args))
            debug_output("end " + repr(self))

        # what does this primitive want as its first argument?
        first_arg = None
        if not is_bound_method(new_prim.func):
            if new_prim.wants_turtle():
                first_arg = global_objects["turtles"].get_active_turtle()
            elif new_prim.wants_turtles():
                first_arg = global_objects["turtles"]
            elif new_prim.wants_canvas():
                first_arg = global_objects["canvas"]
            elif new_prim.wants_logocode():
                first_arg = global_objects["logo"]
            elif new_prim.wants_heap():
                first_arg = global_objects["logo"].heap
            elif new_prim.wants_tawindow():
                first_arg = global_objects["window"]
            else:
                result, plugin = new_prim.wants_plugin()
                if result:
                    first_arg = plugin

        # execute the actual function
        if first_arg is None:
            return_value = new_prim.func(*new_args, **new_kwargs)
        else:
            return_value = new_prim.func(first_arg, *new_args, **new_kwargs)

        if new_prim.call_afterwards is not None:
            new_prim.call_afterwards(*new_args, **new_kwargs)

        return return_value

    def get_ast(self, *arg_asts, **kwarg_asts):
        """Transform this object into a Python AST. When serialized and
        executed, the AST will do exactly the same as calling this
        object."""

        if Primitive._DEBUG:
            debug_output(repr(self))
            debug_output("  arg_asts: " + repr(arg_asts))
        new_prim = self.fill_slots(arg_asts, kwarg_asts, convert_to_ast=True)
        if not new_prim.are_slots_filled():
            raise PyExportError("not enough arguments")
        if Primitive._DEBUG:
            debug_output("  new_prim.arg_descs: " + repr(new_prim.arg_descs))

        # extract the actual values from the (now constant) arguments
        (new_arg_asts, new_kwarg_asts) = new_prim.get_values_of_filled_slots(
            exportable_only=True)
        if Primitive._DEBUG:
            debug_output("  new_arg_asts: " + repr(new_arg_asts))
            debug_output("end " + repr(self))

        # SPECIAL HANDLING #

        # loops
        if self == LogoCode.prim_loop:
            controller = self._get_loop_controller()
            if controller == Primitive.controller_repeat:
                # 'repeat' loop
                num_repetitions = new_arg_asts[0]
                if num_repetitions.func.id == 'controller_repeat':
                    num_repetitions = num_repetitions.args[0]
                repeat_iter = get_call_ast("range", [num_repetitions])
                # TODO use new variable name in nested loops
                loop_ast = ast.For(target=ast.Name(id="i", ctx=ast.Store),
                                   iter=repeat_iter,
                                   body=new_arg_asts[1],
                                   orelse=[])
                return loop_ast
            else:
                if controller == Primitive.controller_forever:
                    condition_ast = ast.Name(id="True", ctx=ast.Load)
                elif controller == Primitive.controller_while:
                    condition_ast = new_arg_asts[0].args[0]
                elif controller == Primitive.controller_until:
                    pos_cond_ast = new_arg_asts[0].args[0]
                    condition_ast = ast.UnaryOp(op=ast.Not,
                                                operand=pos_cond_ast)
                else:
                    raise PyExportError("unknown loop controller: " +
                                        repr(controller))
                loop_ast = ast.While(test=condition_ast,
                                     body=new_arg_asts[1],
                                     orelse=[])
                # Until always executes its body once.
                if controller == Primitive.controller_until:
                    loop_list = []
                    for arg_ast in new_arg_asts[1]:
                        loop_list.append(arg_ast)
                    loop_list.append(loop_ast)
                    return loop_list
                else:
                    return loop_ast

        # conditionals
        elif self in (LogoCode.prim_if, LogoCode.prim_ifelse):
            test = new_arg_asts[0]
            body = new_arg_asts[1]
            if len(new_arg_asts) > 2:
                orelse = new_arg_asts[2]
            else:
                orelse = []
            if_ast = ast.If(test=test, body=body, orelse=orelse)
            return if_ast

        # boxes
        elif self == LogoCode.prim_set_box:
            target_ast = ast.Subscript(value=BOX_AST,
                                       slice=ast.Index(value=new_arg_asts[0]),
                                       ctx=ast.Store)
            return ast.Assign(targets=[target_ast], value=new_arg_asts[1])
        elif self == LogoCode.prim_get_box:
            return ast.Subscript(value=BOX_AST,
                                 slice=ast.Index(value=new_arg_asts[0]),
                                 ctx=ast.Load)

        # action stacks
        elif self == LogoCode.prim_define_stack:
            return
        elif self == LogoCode.prim_invoke_stack:
            stack_func = ast.Subscript(
                value=ACTION_AST,
                slice=ast.Index(value=new_arg_asts[0]), ctx=ast.Load)
            call_ast = get_call_ast('logo.icall', [stack_func])
            return [call_ast, ast_yield_true()]

        # stop stack
        elif self == LogoCode.prim_stop_stack:
            return ast.Return()

        # sleep/ wait
        elif self == LogoCode.prim_wait:
            return [get_call_ast('sleep', new_arg_asts), ast_yield_true()]

        # standard operators
        elif self.func.__name__ in Primitive.STANDARD_OPERATORS:
            op = Primitive.STANDARD_OPERATORS[self.func.__name__]
            # 'divide': prevent unwanted integer division
            if self == Primitive.divide:
                def _is_float(x):
                    return get_type(x)[0] == TYPE_FLOAT
                if (not _is_float(new_arg_asts[0]) and
                        not _is_float(new_arg_asts[1])):
                    new_arg_asts[0] = get_call_ast('float', [new_arg_asts[0]],
                                                   return_type=TYPE_FLOAT)
            if len(new_arg_asts) == 1:
                if isinstance(op, tuple):
                    op = op[0]
                return ast.UnaryOp(op=op, operand=new_arg_asts[0])
            elif len(new_arg_asts) == 2:
                if isinstance(op, tuple):
                    op = op[1]
                (left, right) = new_arg_asts
                if issubclass(op, ast.boolop):
                    return ast.BoolOp(op=op, values=[left, right])
                elif issubclass(op, ast.cmpop):
                    return ast.Compare(left=left, ops=[op],
                                       comparators=[right])
                else:
                    return ast.BinOp(op=op, left=left, right=right)

        # f(x)
        elif self == LogoCode.prim_myfunction:
            param_asts = []
            for id_ in ['x', 'y', 'z'][:len(new_arg_asts)-1]:
                param_asts.append(ast.Name(id=id_, ctx=ast.Param))
            func_ast = ast_extensions.LambdaWithStrBody(
                body_str=new_arg_asts[0].s, args=param_asts)
            return get_call_ast(func_ast, new_arg_asts[1:],
                                return_type=self.return_type)

        # square root
        elif self == Primitive.square_root:
            return get_call_ast('sqrt', new_arg_asts, new_kwarg_asts,
                                return_type=self.return_type)

        # random
        elif self in (Primitive.random_char, Primitive.random_int):
            uniform_ast = get_call_ast('uniform', new_arg_asts)
            round_ast = get_call_ast('round', [uniform_ast, ast.Num(n=0)])
            int_ast = get_call_ast('int', [round_ast], return_type=TYPE_INT)
            if self == Primitive.random_char:
                chr_ast = get_call_ast('chr', [int_ast], return_type=TYPE_CHAR)
                return chr_ast
            else:
                return int_ast

        # identity
        elif self == Primitive.identity:
            return new_arg_asts[0]

        # constant
        elif self == CONSTANTS.get:
            return TypedSubscript(value=ast.Name(id='CONSTANTS', ctx=ast.Load),
                                  slice_=ast.Index(value=new_arg_asts[0]),
                                  return_type=self.return_type)

        # group of Primitives or sandwich-clamp block
        elif self in (Primitive.group, LogoCode.prim_clamp):
            ast_list = []
            for prim in new_arg_asts[0]:
                if export_me(prim):
                    new_ast = value_to_ast(prim)
                    if isinstance(new_ast, ast.AST):
                        ast_list.append(new_ast)
            return ast_list

        # set turtle
        elif self == LogoCode.prim_turtle:
            text = 'turtle = turtles.get_active_turtle()'
            return [get_call_ast('logo.prim_turtle', new_arg_asts),
                    ast_extensions.ExtraCode(text)]

        elif self == LogoCode.active_turtle:
            text = 'turtle = turtles.get_active_turtle()'
            return ast_extensions.ExtraCode(text)

        # comment
        elif self == Primitive.comment:
            if isinstance(new_arg_asts[0], ast.Str):
                text = ' ' + str(new_arg_asts[0].s)
            else:
                text = ' ' + str(new_arg_asts[0])
            return ast_extensions.Comment(text)

        # print
        elif self == TurtleArtWindow.print_:
            func_name = self.get_name_for_export()
            call_ast = get_call_ast(func_name, new_arg_asts)
            print_ast = ast.Print(values=new_arg_asts[:1], dest=None, nl=True)
            return [call_ast, print_ast]

        # heap
        elif self == LogoCode.get_heap:
            return TypedName(id_='logo.heap', return_type=self.return_type)
        elif self == LogoCode.reset_heap:
            target_ast = ast.Name(id='logo.heap', ctx=ast.Store)
            value_ast = ast.List(elts=[], ctx=ast.Load)
            return ast.Assign(targets=[target_ast], value=value_ast)

        # NORMAL FUNCTION CALL #

        else:
            func_name = self.get_name_for_export()
            return get_call_ast(func_name, new_arg_asts, new_kwarg_asts,
                                return_type=self.return_type)

    def __eq__(self, other):
        """ Two Primitives are equal iff their all their properties are equal.
        Consider bound and unbound methods equal. """
        # other is a Primitive
        if isinstance(other, Primitive):
            return (self == other.func and
                    self.return_type == other.return_type and
                    self.arg_descs == other.arg_descs and
                    self.kwarg_descs == other.kwarg_descs and
                    self.call_afterwards == other.call_afterwards and
                    self.export_me == other.export_me)

        # other is a callable
        elif callable(other):
            if is_instancemethod(self.func) != is_instancemethod(other):
                return False
            elif is_instancemethod(self.func):  # and is_instancemethod(other):
                return (self.func.im_class == other.im_class and
                        self.func.im_func == other.im_func)
            else:
                return self.func == other

        elif is_staticmethod(other):
            return self.func == other.__func__

        # other is neither a Primitive nor a callable
        else:
            return False

    def wants_turtle(self):
        """Does this Primitive want to get the active turtle as its first
        argument?"""
        return self._wants(Turtle)

    def wants_turtles(self):
        """ Does this Primitive want to get the Turtles instance as its
        first argument? """
        return self._wants(Turtles)

    def wants_canvas(self):
        """ Does this Primitive want to get the canvas as its first
        argument? """
        return self._wants(TurtleGraphics)

    def wants_logocode(self):
        """ Does this Primitive want to get the LogoCode instance as its
        first argument? """
        return (self.func.__name__ == '<lambda>' or self._wants(LogoCode))

    def wants_heap(self):
        """ Does this Primitive want to get the heap as its first argument? """
        return ((hasattr(self.func, '__self__') and
                 isinstance(self.func.__self__, list)) or
                self.func in list.__dict__.values())

    def wants_tawindow(self):
        """ Does this Primitive want to get the TurtleArtWindow instance
        as its first argument? """
        return self._wants(TurtleArtWindow)

    def wants_plugin(self):
        """Does this Primitive want to get a plugin instance as its first
        argument? """
        for obj in global_objects.keys():
            if self._wants(global_objects[obj].__class__):
                return True, obj
        return False, None

    def wants_nothing(self):
        """Does this Primitive want nothing as its first argument? I.e. does
        it want to be passed all the arguments of the block and
        nothing else?"""
        return not is_instancemethod(self.func)

    def _wants(self, theClass):
        return is_instancemethod(self.func) and self.func.im_class == theClass

    # treat the following methods in a special way when converting the
    # Primitive to an AST

    @staticmethod
    def controller_repeat(num):
        """ Loop controller for the 'repeat' block """
        for i in range(num):
            yield True
        yield False

    @staticmethod
    def controller_forever():
        """ Loop controller for the 'forever' block """
        while True:
            yield True

    @staticmethod
    def controller_while(condition):
        """ Loop controller for the 'while' block
        condition -- Primitive that is evaluated every time through the
            loop """
        condition.allow_call_args(recursive=True)
        while condition():
            yield True
        yield False

    @staticmethod
    def controller_until(condition):
        """ Loop controller for the 'until' block
        condition -- Primitive that is evaluated every time through the
            loop """
        condition.allow_call_args(recursive=True)
        while not condition():
            yield True
        yield False

    LOOP_CONTROLLERS = [controller_repeat, controller_forever,
                        controller_while, controller_until]

    def _get_loop_controller(self):
        """ Return the controller for this loop Primitive. Raise a
        ValueError if no controller was found. """
        def _is_loop_controller(candidate):
            return (callable(candidate)
                    and candidate in Primitive.LOOP_CONTROLLERS)

        for desc in self.arg_descs:
            if isinstance(desc, ConstantArg):
                value = desc.value
                if _is_loop_controller(value):
                    return value
            elif isinstance(desc, ArgSlot):
                wrapper = desc.wrapper
                if _is_loop_controller(wrapper):
                    return wrapper

        # no controller found
        raise PyExportError("found no loop controller for " + repr(self))

    @staticmethod
    def do_nothing():
        pass

    @staticmethod
    def identity(arg):
        """ Return the argument unchanged """
        return arg

    @staticmethod
    def group(prim_list):
        """ Group together multiple Primitives into one. Treat each Primitive
        as a separate line of code. """
        return_val = None
        for prim in prim_list:
            return_val = prim()
        return return_val

    @staticmethod
    def plus(arg1, arg2=None):
        """ If only one argument is given, prefix it with '+'. If two
        arguments are given, add the second to the first. If the first
        argument is a tuple of length 2 and the second is None, use the
        values in the tuple as arg1 and arg2. """
        if isinstance(arg1, (list, tuple)) and len(arg1) == 2 and arg2 is None:
            (arg1, arg2) = arg1
        if arg2 is None:
            return + arg1
        else:
            return arg1 + arg2

    @staticmethod
    def minus(arg1, arg2=None):
        """ If only one argument is given, change its sign. If two
        arguments are given, subtract the second from the first. """
        if arg2 is None:
            return - arg1
        else:
            return arg1 - arg2

    @staticmethod
    def multiply(arg1, arg2):
        """ Multiply the two arguments """
        return arg1 * arg2

    @staticmethod
    def divide(arg1, arg2):
        """ Divide the first argument by the second """
        return float(arg1) / arg2

    @staticmethod
    def modulo(arg1, arg2):
        """ Return the remainder of dividing the first argument by the second.
        If the first argument is a string, format it with the value(s) in
        the second argument. """
        return arg1 % arg2

    @staticmethod
    def power(arg1, arg2):
        """ Raise the first argument to the power given by the second """
        return arg1 ** arg2

    @staticmethod
    def square_root(arg1):
        """ Return the square root of the argument. If it is a negative
        number, raise a NegativeRootError. """
        if arg1 < 0:
            raise NegativeRootError(neg_value=arg1)
        return sqrt(arg1)

    @staticmethod
    def and_(arg1, arg2):
        """ Logcially conjoin the two arguments (using short-circuting) """
        return arg1 and arg2

    @staticmethod
    def or_(arg1, arg2):
        """ Logically disjoin the two arguments (using short-circuting) """
        return arg1 or arg2

    @staticmethod
    def not_(arg):
        """ Return True if the argument evaluates to False, and False
        otherwise. """
        return not arg

    @staticmethod
    def equals(arg1, arg2):
        """ Return arg1 == arg2 """
        return arg1 == arg2

    @staticmethod
    def less(arg1, arg2):
        """ Return arg1 < arg2 """
        return arg1 < arg2

    @staticmethod
    def greater(arg1, arg2):
        """ Return arg1 > arg2 """
        return arg1 > arg2

    @staticmethod
    def comment(text):
        """In 'snail' execution mode, display the comment. Else, do
        nothing."""
        tw = global_objects["window"]
        if not tw.hide and tw.step_time != 0:
            tw.showlabel('print', text)

    @staticmethod
    def random_int(lower, upper):
        """ Choose a random integer between lower and upper, which must be
        integers """
        return int(round(uniform(lower, upper), 0))

    @staticmethod
    def random_char(lower, upper):
        """ Choose a random Unicode code point between lower and upper,
        which must be integers """
        return chr(Primitive.random_int(lower, upper))


class Disjunction(tuple):
    """ Abstract disjunction class (not to be instantiated directly) """

    def __init__(self, iterable):
        self = tuple(iterable)

    def __repr__(self):
        s = ["("]
        for disj in self:
            s.append(repr(disj))
            s.append(" or ")
        s.pop()
        s.append(")")
        return "".join(s)

    def get_alternatives(self):
        """ Return a tuple of alternatives, i.e. self """
        return self


class PrimitiveDisjunction(Disjunction, Primitive):
    """ Disjunction of two or more Primitives. PrimitiveDisjunctions may not
    be nested. """

    @property
    def return_type(self):
        """ Tuple of the return_types of all disjuncts """
        return TypeDisjunction((prim.return_type for prim in self))

    def __call__(self, *runtime_args, **runtime_kwargs):
        """ Loop over the disjunct Primitives and try to fill their slots
        with the given args and kwargs. Call the first Primitives whose
        slots could be filled successfully. If all disjunct Primitives
        fail, raise the last error that occurred. """

        # remove the first argument if it is a LogoCode instance
        if runtime_args and isinstance(runtime_args[0], LogoCode):
            runtime_args = runtime_args[1:]

        error = None
        for prim in self:
            try:
                new_prim = prim.fill_slots(runtime_args, runtime_kwargs,
                                           convert_to_ast=False)
            except TATypeError as error:
                # on failure, try the next one
                continue
            else:
                # on success, call this Primitive
                return new_prim()

        # if we get here, all disjuncts failed
        if error is not None:
            raise error


class ArgListDisjunction(Disjunction):
    """ Disjunction of two or more argument lists """
    pass


class ArgSlot(object):
    """ Description of the requirements that a Primitive demands from an
    argument or keyword argument. An ArgSlot is filled at runtime, based
    on the block program structure. """

    def __init__(self, type_, call_arg=True, wrapper=None):
        """
        type_ -- what type of the type hierarchy the argument should have
            (after the wrapper has been applied)
        call_arg -- if this argument is callable, should it be called and
            its return value passed to the parent Primitive (True, the
            default), or should it be passed as it is (False)?
        wrapper -- a Primitive that is 'wrapped around' the argument before
            it gets passed to its parent Primitive. Wrappers can be nested
            infinitely. """
        self.type = type_
        self.call_arg = call_arg
        self.wrapper = wrapper

    def __repr__(self):
        s = ["ArgSlot(type="]
        s.append(repr(self.type))
        if not self.call_arg:
            s.append(", call=")
            s.append(repr(self.call_arg))
        if self.wrapper is not None:
            s.append(", wrapper=")
            s.append(repr(self.wrapper))
        s.append(")")
        return "".join(s)

    def get_alternatives(self):
        """ Return a tuple of slot alternatives, i.e. (self, ) """
        return (self, )

    def fill(self, argument, convert_to_ast=False, call_my_args=True):
        """ Try to fill this argument slot with the given argument. Return
        a ConstantArg containing the result. If there is a type problem,
        raise a TATypeError. """
        if isinstance(argument, ast.AST):
            convert_to_ast = True

        # 1. can the argument be called?
        (func_disjunction, args) = (None, [])
        if (isinstance(argument, tuple) and argument
                and callable(argument[0])):
            func_disjunction = argument[0]
            if len(argument) >= 2 and isinstance(argument[1], LogoCode):
                args = argument[2:]
            else:
                args = argument[1:]
        elif callable(argument):
            func_disjunction = argument

        # make sure we can loop over func_disjunction
        if not isinstance(func_disjunction, PrimitiveDisjunction):
            func_disjunction = PrimitiveDisjunction((func_disjunction, ))

        error = None
        bad_value = argument  # the value that caused the TATypeError
        for func in func_disjunction:
            error = None
            for slot in self.get_alternatives():

                if isinstance(slot.wrapper, PrimitiveDisjunction):
                    wrapper_disjunction = slot.wrapper
                else:
                    wrapper_disjunction = PrimitiveDisjunction((slot.wrapper,))

                for wrapper in wrapper_disjunction:

                    # check if the argument can fill this slot (type-wise)
                    # (lambda functions are always accepted)
                    if getattr(func, '__name__', None) == '<lambda>':
                        converter = identity
                        old_type = TYPE_OBJECT
                        new_type = slot.type
                    else:
                        if wrapper is not None:
                            arg_types = get_type(wrapper)[0]
                            bad_value = wrapper
                        elif func is not None:
                            arg_types = get_type(func)[0]
                            bad_value = func
                        else:
                            arg_types = get_type(argument)[0]
                            bad_value = argument
                        converter = None
                        if not isinstance(arg_types, TypeDisjunction):
                            arg_types = TypeDisjunction((arg_types, ))
                        if isinstance(slot.type, TypeDisjunction):
                            slot_types = slot.type
                        else:
                            slot_types = TypeDisjunction((slot.type, ))
                        for old_type in arg_types:
                            for new_type in slot_types:
                                converter = get_converter(old_type, new_type)
                                if converter is not None:
                                    break
                            if converter is not None:
                                break
                        # unable to convert, try next wrapper/ slot/ func
                        if converter is None:
                            continue

                    # 1. (cont'd) call the argument or pass it on as a callable
                    called_argument = argument
                    if func is not None:
                        func_prim = func
                        if not isinstance(func_prim, Primitive):
                            func_prim = Primitive(
                                func_prim,
                                [ArgSlot(TYPE_OBJECT)] * len(args))
                        try:
                            func_prim = func_prim.fill_slots(
                                args,
                                convert_to_ast=convert_to_ast,
                                call_my_args=(slot.call_arg and call_my_args))
                        except TATypeError as error:
                            if Primitive._DEBUG:
                                traceback.print_exc()
                            # on failure, try next wrapper/ slot/ func
                            bad_value = error.bad_value
                            continue
                        if convert_to_ast:
                            called_argument = func_prim.get_ast()
                        else:
                            if slot.call_arg and call_my_args:
                                # call and pass on the return value
                                called_argument = func_prim()
                            else:
                                # don't call and pass on the callable
                                called_argument = func_prim

                    # 2. apply any wrappers
                    wrapped_argument = called_argument
                    if wrapper is not None:
                        if convert_to_ast:
                            if not hasattr(wrapper, "get_ast"):
                                raise PyExportError(
                                    ("cannot convert callable"
                                     " %s to an AST") % (repr(wrapper)))
                            wrapped_argument = wrapper.get_ast(
                                called_argument)
                        else:
                            if slot.call_arg and call_my_args:
                                wrapped_argument = wrapper(called_argument)
                            else:
                                wrapped_argument = wrapper.fill_slots(
                                    [called_argument], call_my_args=False)

                    # last chance to convert raw values to ASTs
                    # (but not lists of ASTs)
                    if (convert_to_ast and
                            not isinstance(wrapped_argument, ast.AST) and
                            not (isinstance(wrapped_argument, list) and
                                 wrapped_argument and
                                 isinstance(wrapped_argument[0], ast.AST))):
                        wrapped_argument = value_to_ast(wrapped_argument)

                    # 3. check the type and convert the argument if necessary
                    converted_argument = wrapped_argument
                    if slot.call_arg and call_my_args:
                        try:
                            converted_argument = convert(
                                wrapped_argument,
                                new_type, old_type=old_type,
                                converter=converter)
                        except TATypeError as error:
                            if Primitive._DEBUG:
                                traceback.print_exc()
                            # on failure, try next wrapper/ slot/ func
                            bad_value = wrapped_argument
                            continue
                    elif converter != identity:
                        converted_argument = Primitive(
                            converter,
                            return_type=new_type,
                            arg_descs=[ConstantArg(wrapped_argument,
                                                   value_type=old_type,
                                                   call_arg=False)])
                    # on success, return the result
                    return ConstantArg(
                        converted_argument,
                        value_type=new_type,
                        call_arg=(slot.call_arg and call_my_args))

        # if we haven't returned anything yet, then all alternatives failed
        if error is not None:
            raise error
        else:
            raise TATypeError(bad_value=bad_value, bad_type=old_type,
                              req_type=new_type)


class ArgSlotDisjunction(Disjunction, ArgSlot):
    """ Disjunction of two or more argument slots """
    pass


class ConstantArg(object):
    """ A constant argument or keyword argument to a Primitive. It is
    independent of the block program structure. """

    def __init__(self, value, call_arg=True, value_type=None):
        """ call_arg -- call the value before returning it?
        value_type -- the type of the value (from the TA type system). This
            is useful to store e.g., the return type of call ASTs. """
        self.value = value
        self.call_arg = call_arg
        self.value_type = value_type

    def get(self, convert_to_ast=False):
        """ If call_arg is True and the value is callable, call the value
        and return its return value. Else, return the value unchanged.
        convert_to_ast -- return the equivalent AST instead of a raw value """
        if self.call_arg and callable(self.value):
            if convert_to_ast:
                return value_to_ast(self.value)
            else:
                return self.value()
        else:
            if convert_to_ast and not isinstance(self.value, list):
                return value_to_ast(self.value)
            else:
                return self.value

    def get_value_type(self):
        """ If this ConstantArg has stored the type of its value, return
        that. Else, use get_type(...) to guess the type of the value. """
        if self.value_type is None:
            return get_type(self.value)[0]
        else:
            return self.value_type

    def __repr__(self):
        s = ["ConstantArg("]
        s.append(repr(self.value))
        if not self.call_arg:
            s.append(", call=")
            s.append(repr(self.call_arg))
        s.append(")")
        return "".join(s)


def or_(*disjuncts):
    """ Return a disjunction object of the same type as the disjuncts. If
    the item type cannot be linked to a Disjunction class, return a tuple
    of the disjuncts. """
    if isinstance(disjuncts[0], Primitive):
        return PrimitiveDisjunction(disjuncts)
    elif isinstance(disjuncts[0], (list, ArgListDisjunction)):
        return ArgListDisjunction(disjuncts)
    elif isinstance(disjuncts[0], ArgSlot):
        return ArgSlotDisjunction(disjuncts)
    elif isinstance(disjuncts[0], Type):
        return TypeDisjunction(disjuncts)
    else:
        return tuple(disjuncts)


def value_to_ast(value, *args_for_prim, **kwargs_for_prim):
    """ Turn a value into an AST. Supported types: Primitive, int, float,
    bool, basestring, list
    If the value is already an AST, return it unchanged.
    If the value is a non-exportable Primitive, return None. """
    # already an AST
    if isinstance(value, ast.AST):
        return value
    # Primitive
    elif isinstance(value, Primitive):
        if value.export_me:
            return value.get_ast(*args_for_prim, **kwargs_for_prim)
        else:
            return None
    # boolean
    elif isinstance(value, bool):
        return ast.Name(id=str(value), ctx=ast.Load)
    # number
    elif isinstance(value, (int, float)):
        return ast.Num(n=value)
    # string
    elif isinstance(value, basestring):
        return ast.Str(value)
    # list (recursively transform to an AST)
    elif isinstance(value, list):
        ast_list = []
        for item in value:
            item_ast = value_to_ast(item)
            if item_ast is not None:
                ast_list.append(item_ast)
        return ast.List(elts=ast_list, ctx=ast.Load)
    # color
    elif isinstance(value, Color):
        # call to the Color constructor with this object's values,
        # e.g., Color('red', 0, 50, 100)
        return get_call_ast('Color', [value.name, value.color,
                                      value.shade, value.gray],
                            return_type=TYPE_COLOR)
    # media
    elif isinstance(value, Media):
        args = [value_to_ast(value.type), value_to_ast(value.value)]
        return get_call_ast('Media', args, return_type=TYPE_MEDIA)
    # unknown
    else:
        raise PyExportError("unknown type of raw value: " + repr(type(value)))


def ast_yield_true():
    return ast.Yield(value=ast.Name(id='True', ctx=ast.Load))


def export_me(something):
    """ Return True iff this is not a Primitive or its export_me attribute
    is True, i.e. everything is exportable except for Primitives with
    export_me == False """
    return not isinstance(something, Primitive) or something.export_me