Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/talogo.py
blob: 145d3aaee1746dbc3daede348f3ce1729ac05aab (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
#Copyright (c) 2007-8, Playful Invention Company.
#Copyright (c) 2008-9, Walter Bender

#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 re
from time import *
import gobject
from operator import isNumberType
import random
import audioop
from math import *
import subprocess
from UserDict import UserDict
try:
    from sugar.datastore import datastore
except:
    pass

class noKeyError(UserDict):
    __missing__=lambda x,y: 0

class taLogo: pass

from taturtle import *
from tagplay import *
from tajail import *

procstop = False

class symbol:
    def __init__(self, name):
        self.name = name
        self.nargs = None
        self.fcn = None

    def __str__(self): return self.name
    def __repr__(self): return '#'+self.name

class logoerror(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

def run_blocks(lc, spr, blocks, run_flag):
    # user-defined stacks
    for x in lc.stacks.keys():
        lc.stacks[x]= None
    # two built-in stacks
    lc.stacks['stack1'] = None
    lc.stacks['stack2'] = None
    for i in blocks:
        if i.proto.name=='hat1':
            lc.stacks['stack1']= readline(lc,blocks_to_code(lc,i))
        if i.proto.name=='hat2':
            lc.stacks['stack2']= readline(lc,blocks_to_code(lc,i))
        if i.proto.name=='hat':
            if (i.connections[1]!=None):
                text=i.connections[1].label
                lc.stacks['stack3'+text]= readline(lc,blocks_to_code(lc,i))
    code = blocks_to_code(lc,spr)
    if run_flag == True:
        print code
        setup_cmd(lc, code)
    else: return code

def blocks_to_code(lc,spr):
    if spr==None: return ['%nothing%']
    code = []
    dock = spr.proto.docks[0]
    if len(dock)>4: code.append(dock[4])
    if spr.proto.primname != '': code.append(spr.proto.primname)
    else:
        if spr.proto.name=='number':
            try:
                code.append(float(spr.label))
            except:
                code.append(float(ord(spr.label[0])))
        elif spr.proto.name=='string' or spr.proto.name=='title':
            if type(spr.label) == float or type(spr.label) == int:
                if int(spr.label) == spr.label:
                    spr.label = int(spr.label)
                code.append('#s'+str(spr.label))
            else:
                code.append('#s'+spr.label)
        elif spr.proto.name=='journal':
            if spr.ds_id != None:
                code.append('#smedia_'+str(spr.ds_id))
            else:
                code.append('#smedia_None')
        elif spr.proto.name=='descriptionoff' or \
             spr.proto.name=='descriptionon':
            if spr.ds_id != None:
                code.append('#sdescr_'+str(spr.ds_id))
            else:
                code.append('#sdescr_None')
        elif spr.proto.name=='audiooff' or spr.proto.name=='audio':
            if spr.ds_id != None:
                code.append('#saudio_'+str(spr.ds_id))
            else:
                code.append('#saudio_None')
        else:
            return ['%nothing%']
    for i in range(1,len(spr.connections)):
        b = spr.connections[i]
        dock = spr.proto.docks[i]
        if len(dock)>4:
            for c in dock[4]: code.append(c)
        if b!=None: code.extend(blocks_to_code(lc,b))
        elif spr.proto.docks[i][0] not in \
            ['flow', 'numend', 'stringend', 'mediaend', \
            'audioend', 'unavailable', 'logi-']:
            code.append('%nothing%')
    return code

def intern(lc, str):
    if str in lc.oblist: return lc.oblist[str]
    sym = symbol(str)
    lc.oblist[str] = sym
    return sym

def parseline(str):
    split = re.split(r"\s|([\[\]()])", str)
    return [x for x in split if x and x != ""]

def readline(lc, line):
    res = []
    while line:
        token = line.pop(0)
        if isNumberType(token): res.append(token)
        elif token.isdigit(): res.append(float(token))
        elif token[0]=='-' and token[1:].isdigit():
            res.append(-float(token[1:]))
        elif token[0] == '"': res.append(token[1:])
        elif token[0:2] == "#s": res.append(token[2:])
        elif token == '[': res.append(readline(lc,line))
        elif token == ']': return res
        else: res.append(intern(lc, token))
    return res

def setup_cmd(lc, str):
    setlayer(lc.tw.turtle.spr,100) 
    lc.procstop=False
    list = readline(lc, str)
    lc.step = start_eval(lc, list)

def start_eval(lc, list):
    icall(lc, evline, list); yield True
    yield False

def evline(lc, list):
    oldiline = lc.iline
    lc.iline = list[:]
    lc.arglist = None
    while lc.iline:
        if lc.tw.step_time > 0:
            setlayer(lc.tw.turtle.spr,630)
            endtime = millis()+an_int(lc,lc.tw.step_time)*100
            while millis()<endtime:
                yield True
            setlayer(lc.tw.turtle.spr,100)
        token = lc.iline[0]
        if token==lc.symopar: token=lc.iline[1]
        icall(lc, eval); yield True
        if lc.procstop: break
        if lc.iresult==None: continue
        raise logoerror(str(lc.iresult))
    lc.iline = oldiline
    ireturn(lc); yield True

def eval(lc, infixarg=False):
    token = lc.iline.pop(0)
    if type(token) == lc.symtype:
        icall(lc, evalsym, token); yield True
        res = lc.iresult
    else: res = token
    if not infixarg:
        while infixnext(lc):
            icall(lc, evalinfix, res); yield True
            res = lc.iresult
    ireturn(lc, res); yield True

def evalsym(lc, token):
    debug_trace(lc, token)
    undefined_check(lc, token)
    oldcfun, oldarglist = lc.cfun, lc.arglist
    lc.cfun, lc.arglist = token, []
    if token.nargs==None: raise logoerror("#noinput")
    for i in range(token.nargs):
        no_args_check(lc)
        icall(lc, eval); yield True
        lc.arglist.append(lc.iresult)
    if lc.cfun.rprim:
        if type(lc.cfun.fcn)==lc.listtype:
            icall(lc, ufuncall, cfun.fcn); yield True
        else:
            icall(lc, lc.cfun.fcn, *lc.arglist); yield True
        result = None
    else: 
        result = lc.cfun.fcn(lc, *lc.arglist)
    lc.cfun, lc.arglist = oldcfun, oldarglist
    if lc.arglist!=None and result==None:
        raise logoerror("%s didn't output to %s" % \
            (oldcfun.name, lc.cfun.name))
    ireturn(lc, result); yield True

def evalinfix(lc, firstarg):
    token = lc.iline.pop(0)
    oldcfun, oldarglist = lc.cfun, lc.arglist
    lc.cfun, lc.arglist = token, [firstarg]
    no_args_check(lc)
    icall(lc, eval,True); yield True
    lc.arglist.append(lc.iresult)
    result = lc.cfun.fcn(lc,*lc.arglist)
    lc.cfun, lc.arglist = oldcfun, oldarglist
    ireturn (lc,result); yield True

def infixnext(lc):
    if len(lc.iline)==0: return False
    if type(lc.iline[0])!=lc.symtype: return False
    return lc.iline[0].name in ['+', '-', '*', '/','%','and','or']

def debug_trace(lc, token):
    if lc.trace:
        showlabel(lc, token)

def undefined_check(lc, token):
    if token.fcn != None: return False
    raise logoerror("I don't know how to %s" % token.name)

def no_args_check(lc):
    if lc.iline and lc.iline[0]!=lc.symnothing : return
    raise logoerror("#noinput")

def prim_wait(lc,time):
    setlayer(lc.tw.turtle.spr,630)
    endtime = millis()+an_int(lc,time)*100
    while millis()<endtime:
        yield True
    setlayer(lc.tw.turtle.spr,100)
    ireturn(lc); yield True

def prim_repeat(lc, num, list):
    num = an_int(lc, num)
    for i in range(num):
        icall(lc, evline, list[:]); yield True
        if lc.procstop: break
    ireturn(lc); yield True

def prim_forever(lc, list):
    while True:
        icall(lc,evline, list[:]); yield True
        if lc.procstop: break
    ireturn(lc); yield True

def prim_if(lc, bool, list):
    if bool: icall(lc, evline, list[:]); yield True
    ireturn(lc); yield True

def prim_ifelse(lc, bool, list1,list2):
    if bool: ijmp(lc, evline, list1[:]); yield True
    else: ijmp(lc, evline, list2[:]); yield True

def prim_opar(lc,val):
    lc.iline.pop(0)
    return val

def prim_define(name, body):
    if type(name) != symtype: name = intern(name)
    name.nargs, name.fcn = 0, body
    name.rprim = True

def prim_stack(lc,stri):
    if (not lc.stacks.has_key('stack3'+stri)) or \
        lc.stacks['stack3'+stri]==None: raise logoerror("#nostack")
    icall(lc, evline, lc.stacks['stack3'+stri][:]); yield True
    lc.procstop = False
    ireturn(lc); yield True

def prim_stack1(lc):
    if lc.stacks['stack1']==None: raise logoerror("#nostack")
    icall(lc, evline, lc.stacks['stack1'][:]); yield True
    lc.procstop = False
    ireturn(lc); yield True

def prim_stack2(lc):
    if lc.stacks['stack2']==None: raise logoerror("#nostack")
    icall(lc, evline, lc.stacks['stack2'][:]); yield True
    lc.procstop = False
    ireturn(lc); yield True

def prim_stopstack(lc):
    lc.procstop = True

def careful_divide(x,y):
    try:
        if y==0: return 0
        return x/y
    except:
        return 0

def ufuncall(body):
    ijmp(evline, body); yield True

def an_int(lc, n):
    if type(n) == int:
        return n
    elif type(n) == float:
        return int(n)
    elif type(n) == str:
        return int(ord(n[0]))
    else:
        raise logoerror("%s doesn't like %s as input" \
            % (lc.cfun.name, str(n)))

def defprim(lc, name, args, fcn, rprim=False):
    sym = intern(lc, name)
    sym.nargs, sym.fcn = args,fcn
    sym.rprim = rprim

def taequal(x,y):
    try:
        return float(x)==float(y)
    except:
        if type(x) == str or type(x) == unicode:
            xx = ord(x[0])
        else:
            xx = x
        if type(y) == str or type(y) == unicode:
            yy = ord(y[0])
        else:
            yy = y
        return xx==yy

def taless(x,y):
    try:
        return(x<y)
    except:
        raise logoerror("#syntaxerror")

def tamore(x,y):
    return taless(y,x)

def taplus(x,y):
    if (type(x) == int or type(x) == float) and \
        (type(y) == int or type(y) == float):
        return(x+y)
    else:
        return(str(x) + str(y))

def taminus(x,y):
    try:
        return(x-y)
    except:
        raise logoerror("#syntaxerror")

def taproduct(x,y):
    try:
        return(x*y)
    except:
        raise logoerror("#syntaxerror")

def tamod(x,y):
    try:
        return(x%y)
    except:
        raise logoerror("#syntaxerror")

def tasqrt(x):
    try:
        return sqrt(x)
    except:
        raise logoerror("#syntaxerror")

def identity(x):
    return(x)

# recenter the canvas when the start block is clicked
def start_stack(lc):
    if hasattr(lc.tw,'activity'):
        lc.tw.activity.recenter()

def lcNew(tw):
    lc = taLogo()
    lc.tw = tw
    lc.oblist = {}

    # math primitives
    defprim(lc,'print', 1, lambda lc,x: status_print(lc,x))
    defprim(lc,'+', None, lambda lc,x,y:x+y)
    defprim(lc,'plus', 2, lambda lc,x,y:taplus(x,y))
    defprim(lc,'-', None, lambda lc,x,y:x-y)
    defprim(lc,'minus', 2, lambda lc,x,y:taminus(x,y))
    defprim(lc,'*', None, lambda lc,x,y:x*y)
    defprim(lc,'product', 2, lambda lc,x,y:taproduct(x,y))
    defprim(lc,'/', None, lambda lc,x,y:careful_divide(x,y))
    defprim(lc,'division', 2, lambda lc,x,y:careful_divide(x,y))
    defprim(lc,'random', 2, lambda lc,x,y: int(random.uniform(x,y)))
    defprim(lc,'greater?', 2, lambda lc,x,y: tamore(x,y))
    defprim(lc,'less?', 2, lambda lc,x,y: taless(x,y))
    defprim(lc,'equal?', 2, lambda lc,x,y: taequal(x,y))
    defprim(lc,'and', None, lambda lc,x,y:x&y)
    defprim(lc,'or', None, lambda lc,x,y:x|y)
    defprim(lc,'not', 1, lambda lc,x:not x)
    defprim(lc,'%', None, lambda lc,x,y:x%y)
    defprim(lc,'mod', 2, lambda lc,x,y:tamod(x,y))
    defprim(lc,'sqrt', 1, lambda lc,x: sqrt(x))
    defprim(lc,'id',1, lambda lc,x: identity(x))
    
    # keyboard, sensor, and misc. primitives
    defprim(lc,'kbinput', 0, lambda lc: kbinput(lc))
    defprim(lc,'keyboard', 0, lambda lc: lc.keyboard)
    defprim(lc,'userdefined', 1, lambda lc,x: loadmyblock(lc,x))
    defprim(lc,'myfunc', 2, lambda lc,f,x: callmyfunc(lc, f, x))
    defprim(lc,'hres', 0, lambda lc: lc.tw.turtle.width) # canvas width
    defprim(lc,'vres', 0, lambda lc: lc.tw.turtle.height) # canvas height
    defprim(lc,'leftpos', 0, lambda lc: -(lc.tw.turtle.width/2)) 
    defprim(lc,'toppos', 0, lambda lc: lc.tw.turtle.height/2)
    defprim(lc,'rightpos', 0, lambda lc: lc.tw.turtle.width/2) 
    defprim(lc,'bottompos', 0, lambda lc: -(lc.tw.turtle.height/2))

    # turtle primitives
    defprim(lc,'clean', 0, lambda lc: clear(lc))
    defprim(lc,'forward', 1, lambda lc, x: forward(lc.tw.turtle, x))
    defprim(lc,'back', 1, lambda lc,x: forward(lc.tw.turtle,-x))
    defprim(lc,'seth', 1, lambda lc, x: seth(lc.tw.turtle, x))
    defprim(lc,'right', 1, lambda lc, x: right(lc.tw.turtle, x))
    defprim(lc,'left', 1, lambda lc,x: right(lc.tw.turtle,-x))
    defprim(lc,'heading', 0, lambda lc: lc.tw.turtle.heading)
    defprim(lc,'setxy', 2, lambda lc, x, y: setxy(lc.tw.turtle, x, y))
    defprim(lc,'show',1,lambda lc, x: show(lc, x))
    defprim(lc,'setscale', 1, lambda lc,x: set_scale(lc, x))
    defprim(lc,'scale', 0, lambda lc: lc.scale)
    defprim(lc,'write',2,lambda lc, x,y: write(lc, x,y))
    defprim(lc,'insertimage', 1, lambda lc,x: insert_image(lc, x))
    defprim(lc,'arc', 2, lambda lc, x, y: arc(lc.tw.turtle, x, y))
    defprim(lc,'xcor', 0, lambda lc: lc.tw.turtle.xcor)
    defprim(lc,'ycor', 0, lambda lc: lc.tw.turtle.ycor)

    # pen primitives
    defprim(lc,'pendown', 0, lambda lc: setpen(lc.tw.turtle, True))
    defprim(lc,'penup', 0, lambda lc: setpen(lc.tw.turtle, False))
    defprim(lc,'(', 1, lambda lc, x: prim_opar(lc,x))
    defprim(lc,'setcolor', 1, lambda lc, x: setcolor(lc.tw.turtle, x))
    defprim(lc,'settextcolor', 1, lambda lc, x: settextcolor(lc.tw.turtle, x))
    defprim(lc,'settextsize', 1, lambda lc, x: settextsize(lc.tw.turtle, x))
    defprim(lc,'setshade', 1, lambda lc, x: setshade(lc.tw.turtle, x))
    defprim(lc,'setpensize', 1, lambda lc, x: setpensize(lc.tw.turtle, x))
    defprim(lc,'fillscreen', 2, lambda lc, x, y: \
        fillscreen(lc.tw.turtle, x, y))
    defprim(lc,'color', 0, lambda lc: lc.tw.turtle.color)
    defprim(lc,'shade', 0, lambda lc: lc.tw.turtle.shade)
    defprim(lc,'pensize', 0, lambda lc: lc.tw.turtle.pensize)
    defprim(lc,'textcolor', 0, lambda lc: lc.tw.turtle.textcolor)
    defprim(lc,'textsize', 0, lambda lc: lc.tw.textsize)

    # flow primitives
    defprim(lc,'wait', 1, prim_wait, True)
    defprim(lc,'repeat', 2, prim_repeat, True)
    defprim(lc,'forever', 1, prim_forever, True)
    defprim(lc,'if', 2, prim_if, True)
    defprim(lc,'ifelse', 3, prim_ifelse, True)
    defprim(lc,'stopstack', 0, prim_stopstack)

    # blocks primitives
    defprim(lc,'stack1', 0, prim_stack1, True)
    defprim(lc,'stack2', 0, prim_stack2, True)
    defprim(lc,'stack', 1, prim_stack, True)
    defprim(lc,'box1', 0, lambda lc: lc.boxes['box1'])
    defprim(lc,'box2', 0, lambda lc: lc.boxes['box2'])
    defprim(lc,'box', 1, lambda lc,x: box(lc,x))
    defprim(lc,'storeinbox1', 1, lambda lc,x: setbox(lc, 'box1',x))
    defprim(lc,'storeinbox2', 1, lambda lc,x: setbox(lc, 'box2',x))
    defprim(lc,'storeinbox', 2, lambda lc,x,y: setbox(lc, 'box3'+str(x),y))
    defprim(lc,'push', 1, lambda lc,x: push_heap(lc,x))
    defprim(lc,'pop', 0, lambda lc: pop_heap(lc))
    defprim(lc,'heap', 0, lambda lc: heap_print(lc))
    defprim(lc,'emptyheap', 0, lambda lc: empty_heap(lc))
    defprim(lc,'start', 0, lambda lc: start_stack(lc))
    defprim(lc,'define', 2, prim_define)
    defprim(lc,'nop', 0, lambda lc: None)
    defprim(lc,'nop1', 0, lambda lc: None)
    defprim(lc,'nop2', 0, lambda lc: None)
    defprim(lc,'nop3', 1, lambda lc,x: None)

    # templates primitives
    defprim(lc,'container', 1, lambda lc,x: x)
    defprim(lc,'tp1', 2, lambda lc,x,y: show_template1(lc, x, y))
    defprim(lc,'tp8', 2, lambda lc,x,y: show_template8(lc, x, y))
    defprim(lc,'tp6', 3, lambda lc,x,y,z: show_template6(lc, x, y, z))
    defprim(lc,'tp3', 8, lambda lc,x,y,z,a,b,c,d,e: \
        show_template3(lc, x, y, z, a, b, c, d, e))
    defprim(lc,'sound', 1, lambda lc,x: play_sound(lc, x))
    defprim(lc,'video', 1, lambda lc,x: play_movie(lc, x))
    defprim(lc,'tp2', 3, lambda lc,x,y,z: \
        show_template2(lc, x, y, z))
    defprim(lc,'tp7', 5, lambda lc,x,y,z,a,b: \
        show_template7(lc, x, y, z, a, b))
    defprim(lc,'hideblocks', 0, lambda lc: hideblocks(lc))

    lc.symtype = type(intern(lc, 'print'))
    lc.listtype = type([])
    lc.symnothing = intern(lc, '%nothing%')
    lc.symopar = intern(lc, '(')

    lc.istack = []
    lc.stacks = {}
    # lc.boxes = noKeyError({'box1': 0, 'box2': 0})
    lc.boxes = {'box1': 0, 'box2': 0}
    lc.heap = []
    lc.keyboard = 0
    lc.trace = 0 # flag for enabling debug output via showlabel
    lc.gplay = None
    lc.ag = None
    lc.nobox = ""
    lc.title_height = int((tw.turtle.height/30)*tw.scale)
    lc.body_height = int((tw.turtle.height/60)*tw.scale)
    lc.bullet_height = int((tw.turtle.height/45)*tw.scale)

    lc.iline, lc.cfun, lc.arglist, lc.ufun = None, None, None, None

    # this dictionary is used to define the relative size and postion of 
    # template elements (w, h, x, y, dx, dy, dx1, dy1...)
    lc.templates = {
             'tp1': (0.5, 0.5, 0.0625, 0.125, 1.05, 0),
             'tp2': (0.5, 0.5, 0.0625, 0.125, 1.05, 1.05),
             'tp3': (1, 1, 0.0625, 0.125, 0, 0.1),
             'tp6': (0.45, 0.45, 0.0625, 0.125, 1.05, 1.05),
             'tp7': (0.45, 0.45, 0.0625, 0.125, 1.05, 1.05),
             'tp8': (0.9, 0.9, 0.0625, 0.125, 0, 0),
             'insertimage': (0.333, 0.333)
            }
    lc.scale = 33

    return lc

def box(lc,x):
    try:
        return lc.boxes['box3'+str(x)]
    except:
        lc.nobox = str(x)
        raise logoerror("#emptybox")

def loadmyblock(lc,x):
    # execute code inported from the Journal
    if lc.tw.myblock != None:
        y = myfunc_import(lc, lc.tw.myblock, x)
    else:
        raise logoerror("#nocode")
    return

def callmyfunc(lc, f, x):
    y = myfunc(lc, f, x)
    if y is None:
        raise logoerror("#syntaxerror")
        stop_logo(lc.tw)
    else:
        return y

def show_picture(lc, media, x, y, w, h):
    if media == "" or media[6:] == "":
#        raise logoerror("#nomedia")
         print "no media"
    elif media[6:] != "None":
        try:
            dsobject = datastore.get(media[6:])
        except:
            raise logoerror("#nomedia")
        # check to see if it is a movie
        print dsobject.file_path
        if dsobject.file_path[-4:] == '.ogv' or \
           dsobject.file_path[-4:] == '.vob' or \
           dsobject.file_path[-4:] == '.mp4' or \
           dsobject.file_path[-4:] == '.mov':
            print "playing movie x:" + str(x) + " y:" + str(y) + " w:" \
                + str(w) + " h:" + str(h)
            play_dsobject(lc, dsobject, int(x), int(y), int(w), int(h))
        else:
            pixbuf = get_pixbuf_from_journal(dsobject, int(w), int(h))
            if pixbuf != None:
                draw_pixbuf(lc.tw.turtle, pixbuf, 0, 0, int(x), int(y), \
                            int(w), int(h))
        dsobject.destroy()

def get_pixbuf_from_journal(dsobject,w,h):
    try:
        pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(dsobject.file_path, \
                                                      int(w),int(h))
    except:
        try:
            # print "Trying preview..."
            pixbufloader = gtk.gdk.pixbuf_loader_new_with_mime_type \
                ('image/png')
            pixbufloader.set_size(min(300,int(w)),min(225,int(h)))
            pixbufloader.write(dsobject.metadata['preview'])
            pixbufloader.close()
#            gtk.gdk_pixbuf_loader_close(pixbufloader)
            pixbuf = pixbufloader.get_pixbuf()
        except:
            # print "No preview"
            pixbuf = None
    return pixbuf

def show_description(lc, media, x, y, w, h):
    if media == "" or media[6:] == "":
#        raise logoerror("#nomedia")
        print "no media"
    elif media[6:] != "None":
        try:
            dsobject = datastore.get(media[6:])
            draw_text(lc.tw.turtle, \
                      dsobject.metadata['description'],int(x),int(y), \
                      lc.body_height, int(w))
            dsobject.destroy()
        except:
            print "no description?"

def draw_title(lc,title,x,y):
    draw_text(lc.tw.turtle,title,int(x),0,lc.title_height, \
        lc.tw.turtle.width-x)

def calc_position(lc,t):
    w,h,x,y,dx,dy = lc.templates[t]
    x *= lc.tw.turtle.width
    y *= lc.tw.turtle.height
    w *= (lc.tw.turtle.width-x)
    h *= (lc.tw.turtle.height-y)
    dx *= w
    dy *= h
    return(w,h,x,y,dx,dy)

# title, one image, and description
def show_template1(lc, title, media):
    w,h,xo,yo,dx,dy = calc_position(lc,'tp1')
    x = -(lc.tw.turtle.width/2)+xo
    y = lc.tw.turtle.height/2
    setxy(lc.tw.turtle, x, y)
    # save the text size so we can restore it later
    save_text_size = lc.tw.textsize
    # set title text
    settextsize(lc.tw.turtle, lc.title_height)
    show(lc,title)
    # calculate and set scale for media blocks
    myscale = 45 * (lc.tw.turtle.height - lc.title_height*2) \
                  / lc.tw.turtle.height
    set_scale(lc,myscale)
    # set body text size
    settextsize(lc.tw.turtle, lc.body_height)
    # render media object
    y -= (lc.title_height*2) # leave some space below the title
    setxy(lc.tw.turtle, x, y)
    show(lc, media)
    x = 0
    setxy(lc.tw.turtle, x, y)
    show(lc, media.replace("media_","descr_"))
    # restore text size
    settextsize(lc.tw.turtle, save_text_size)

# title, two images (horizontal), two descriptions
def show_template2(lc, title, media1, media2):
    w,h,xo,yo,dx,dy = calc_position(lc,'tp2')
    x = -(lc.tw.turtle.width/2)+xo
    y = lc.tw.turtle.height/2
    setxy(lc.tw.turtle, x, y)
    # save the text size so we can restore it later
    save_text_size = lc.tw.textsize
    # set title text
    settextsize(lc.tw.turtle, lc.title_height)
    show(lc,title)
    # calculate and set scale for media blocks
    myscale = 45 * (lc.tw.turtle.height - lc.title_height*2)/lc.tw.turtle.height
    set_scale(lc,myscale)
    # set body text size
    settextsize(lc.tw.turtle, lc.body_height)
    # render four quadrents
    y -= (lc.title_height*2) # leave some space below the title
    setxy(lc.tw.turtle, x, y)
    show(lc, media1)
    x = 0
    setxy(lc.tw.turtle, x, y)
    show(lc, media2)
    y = -lc.title_height
    setxy(lc.tw.turtle, x, y)
    show(lc, media2.replace("media_","descr_"))
    x = -(lc.tw.turtle.width/2)+xo
    setxy(lc.tw.turtle, x, y)
    show(lc, media1.replace("media_","descr_"))
    # restore text size
    settextsize(lc.tw.turtle, save_text_size)

# title and seven bullets
def show_template3(lc, title, s1, s2, s3, s4, s5, s6, s7):
    w,h,xo,yo,dx,dy = calc_position(lc,'tp3')
    x = -(lc.tw.turtle.width/2)+xo
    y = lc.tw.turtle.height/2
    setxy(lc.tw.turtle, x, y)
    # save the text size so we can restore it later
    save_text_size = lc.tw.textsize
    # set title text
    settextsize(lc.tw.turtle, lc.title_height)
    show(lc,title)
    # set body text size
    settextsize(lc.tw.turtle, lc.bullet_height)
    y -= (lc.title_height*2) # leave some space below the title
    setxy(lc.tw.turtle, x, y)
    show(lc, s1)
    y -= (lc.bullet_height*2)
    setxy(lc.tw.turtle, x, y)
    show(lc, s2)
    y -= (lc.bullet_height*2)
    setxy(lc.tw.turtle, x, y)
    show(lc, s3)
    y -= (lc.bullet_height*2)
    setxy(lc.tw.turtle, x, y)
    show(lc, s4)
    y -= (lc.bullet_height*2)
    setxy(lc.tw.turtle, x, y)
    show(lc, s5)
    y -= (lc.bullet_height*2)
    setxy(lc.tw.turtle, x, y)
    show(lc, s6)
    y -= (lc.bullet_height*2)
    setxy(lc.tw.turtle, x, y)
    show(lc, s7)
    # restore text size
    settextsize(lc.tw.turtle, save_text_size)

# title, two images (vertical), two desciptions
def show_template6(lc, title, media1, media2):
    w,h,xo,yo,dx,dy = calc_position(lc,'tp6')
    x = -(lc.tw.turtle.width/2)+xo
    y = lc.tw.turtle.height/2
    setxy(lc.tw.turtle, x, y)
    # save the text size so we can restore it later
    save_text_size = lc.tw.textsize
    # set title text
    settextsize(lc.tw.turtle, lc.title_height)
    show(lc,title)
    # calculate and set scale for media blocks
    myscale = 45 * (lc.tw.turtle.height - lc.title_height*2)/lc.tw.turtle.height
    set_scale(lc,myscale)
    # set body text size
    settextsize(lc.tw.turtle, lc.body_height)
    # render four quadrents
    y -= (lc.title_height*2) # leave some space below the title
    setxy(lc.tw.turtle, x, y)
    show(lc, media1)
    x = 0
    setxy(lc.tw.turtle, x, y)
    show(lc, media1.replace("media_","descr_"))
    y = -lc.title_height
    setxy(lc.tw.turtle, x, y)
    show(lc, media2.replace("media_","descr_"))
    x = -(lc.tw.turtle.width/2)+xo
    setxy(lc.tw.turtle, x, y)
    show(lc, media2)
    # restore text size
    settextsize(lc.tw.turtle, save_text_size)

# title and four images
def show_template7(lc, title, media1, media2, media3, media4):
    w,h,xo,yo,dx,dy = calc_position(lc,'tp7')
    x = -(lc.tw.turtle.width/2)+xo
    y = lc.tw.turtle.height/2
    setxy(lc.tw.turtle, x, y)
    # save the text size so we can restore it later
    save_text_size = lc.tw.textsize
    # set title text
    settextsize(lc.tw.turtle, lc.title_height)
    show(lc,title)
    # calculate and set scale for media blocks
    myscale = 45 * (lc.tw.turtle.height - lc.title_height*2)/lc.tw.turtle.height
    set_scale(lc,myscale)
    # set body text size
    settextsize(lc.tw.turtle, lc.body_height)
    # render four quadrents
    y -= (lc.title_height*2) # leave some space below the title
    setxy(lc.tw.turtle, x, y)
    show(lc, media1)
    x = 0
    setxy(lc.tw.turtle, x, y)
    show(lc, media2)
    y = -lc.title_height
    setxy(lc.tw.turtle, x, y)
    show(lc, media4)
    x = -(lc.tw.turtle.width/2)+xo
    setxy(lc.tw.turtle, x, y)
    show(lc, media3)
    # restore text size
    settextsize(lc.tw.turtle, save_text_size)

# title, one media object
def show_template8(lc, title, media1):
    w,h,xo,yo,dx,dy = calc_position(lc,'tp7')
    x = -(lc.tw.turtle.width/2)+xo
    y = lc.tw.turtle.height/2
    setxy(lc.tw.turtle, x, y)
    # save the text size so we can restore it later
    save_text_size = lc.tw.textsize
    # set title text
    settextsize(lc.tw.turtle, lc.title_height)
    show(lc,title)
    # calculate and set scale for media blocks
    myscale = 90 * (lc.tw.turtle.height - lc.title_height*2) \
                  / lc.tw.turtle.height
    set_scale(lc,myscale)
    # set body text size
    settextsize(lc.tw.turtle, lc.body_height)
    # render media object
    y -= (lc.title_height*2) # leave some space below the title
    setxy(lc.tw.turtle, x, y)
    show(lc, media1)
    # restore text size
    settextsize(lc.tw.turtle, save_text_size)

# image only (at current x,y)
def insert_image(lc, media):
    w = (lc.tw.turtle.width * lc.scale)/100
    h = (lc.tw.turtle.height * lc.scale)/100
    # convert from Turtle coordinates to screen coordinates
    x = lc.tw.turtle.width/2+int(lc.tw.turtle.xcor)
    y = lc.tw.turtle.height/2-int(lc.tw.turtle.ycor)
    if media[0:5] == 'media':
        show_picture(lc, media, x, y, w, h)

# description text only (at current x,y)
def insert_desc(lc, media):
    w = (lc.tw.turtle.width * lc.scale)/100
    h = (lc.tw.turtle.height * lc.scale)/100
    # convert from Turtle coordinates to screen coordinates
    x = lc.tw.turtle.width/2+int(lc.tw.turtle.xcor)
    y = lc.tw.turtle.height/2-int(lc.tw.turtle.ycor)
    if media[0:5] == 'descr':
        show_description(lc, media, x, y, w, h)

def set_scale(lc, x):
    lc.scale = x

# need to fix export logo to map show to write
def show(lc, string):
    # convert from Turtle coordinates to screen coordinates
    x = lc.tw.turtle.width/2+int(lc.tw.turtle.xcor)
    y = lc.tw.turtle.height/2-int(lc.tw.turtle.ycor)
    if type(string) == str or type(string) == unicode:
        if string == "media_None":
            pass
        elif string[0:6] == 'media_':
            insert_image(lc, string)
        elif string[0:6] == 'descr_':
            insert_desc(lc, string)
        elif string[0:6] == 'audio_':
            play_sound(lc, string)
        else:
            draw_text(lc.tw.turtle,string,x,y,lc.tw.textsize,lc.tw.turtle.width)
    elif type(string) == float or type(string) == int:
        if int(string) == string:
            string = int(string)
        else:
            string = float(string*10.0/10.0)
        draw_text(lc.tw.turtle,string,x,y,lc.tw.textsize,lc.tw.turtle.width)

# audio only
def play_sound(lc, audio):
    play_audio(lc, audio)

def clear(lc):
    stop_media(lc)
    clearscreen(lc.tw.turtle)

def write(lc, string, fsize):
    # convert from Turtle coordinates to screen coordinates
    x = lc.tw.turtle.width/2+int(lc.tw.turtle.xcor)
    y = lc.tw.turtle.height/2-int(lc.tw.turtle.ycor)
    draw_text(lc.tw.turtle,string,x,y,int(fsize),lc.tw.turtle.width)

def hideblocks(lc):
    from tawindow import hideshow_button
    lc.tw.hide = False # force hide
    hideshow_button(lc.tw)
    for i in lc.tw.selbuttons:
        hide(i)
    lc.tw.activity.projectToolbar.do_hide()

def doevalstep(lc):
    starttime = millis()
    try:
        while (millis()-starttime)<120:
            try:
                lc.step.next()
            except StopIteration:
                setlayer(lc.tw.turtle.spr,630)
                return False
    except logoerror, e:
        showlabel(lc, str(e)[1:-1])
        setlayer(lc.tw.turtle.spr,630)
        return False
    return True

def icall(lc, fcn, *args):
    lc.istack.append(lc.step)
    lc.step = fcn(lc, *(args))

def ireturn(lc, res=None):
    lc.step = lc.istack.pop()
    lc.iresult = res

def ijmp(lc, fcn, *args):
    lc.step = fcn(lc,*(args))

def heap_print(lc):
    showlabel(lc,lc.heap)

def status_print(lc,n):
    if type(n) == str or type(n) == unicode:
        # show title for Journal entries
        if n[0:6] == 'media_':
            try:
                dsobject = datastore.get(n[6:])
                showlabel(lc, dsobject.metadata['title'])
                dsobject.destroy()
            except:
                showlabel(lc,n)
        else:
            showlabel(lc,n)
    elif type(n) == int:
        showlabel(lc,n)
    else:
        if int(float(n)) == n:
            # show no decimal for ints
            showlabel(lc, int(n))
        else:
            # show one decimal for floats
            showlabel(lc, int(float(n)*10)/10.)

def kbinput(lc):
    if len(lc.tw.keypress) == 1:
        lc.keyboard = ord(lc.tw.keypress[0])
    else:
        try:
            lc.keyboard = {'Escape': 27, 'space': 32, ' ': 32, 'Return': 13, \
                'KP_Up': 2, 'KP_Down': 4, 'KP_Left': 1, 'KP_Right': 3,} \
                [lc.tw.keypress]
        except:
            lc.keyboard = 0
    lc.tw.keypress = ""

def showlabel(lc,label):
    if label=='#nostack': shp = 'nostack'; label=''
    elif label=='#noinput': shp = 'noinput'; label=''
    elif label=='#emptyheap': shp = 'emptyheap'; label=''
    elif label=='#emptybox': shp = 'emptybox'; label='                    '+lc.nobox
    elif label=='#nomedia': shp = 'nomedia'; label=''
    elif label=='#nocode': shp = 'nocode'; label=''
    elif label=='#syntaxerror': shp = 'syntaxerror'; label=''
    else: shp = 'status'
    setshape(lc.tw.status_spr, lc.tw.status_shapes[shp])
    setlabel(lc.tw.status_spr, label)
    setlayer(lc.tw.status_spr, 710)

def stop_logo(tw):
    tw.step_time = 0
    tw.lc.step = just_stop()

def just_stop(): yield False

def setbox(lc, name,val): lc.boxes[name]=val

def push_heap(lc,val): lc.heap.append(val)

def pop_heap(lc):
    try: return lc.heap.pop(-1)
    except: raise logoerror ("#emptyheap")

def empty_heap(lc): lc.heap = []

def tyo(n): print n

def millis(): return int(clock()*1000)