-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathprint.coffee
1479 lines (1273 loc) · 37.8 KB
/
print.coffee
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
power_str = "^"
codeGen = false
# this is only invoked when user invokes
# "print" explicitly
Eval_print = ->
stringsEmittedByUserPrintouts += _print(cdr(p1), printMode)
push(symbol(NIL));
# this is only invoked when user invokes
# "print2dascii" explicitly
Eval_print2dascii = ->
stringsEmittedByUserPrintouts +=_print(cdr(p1),PRINTMODE_2DASCII)
push(symbol(NIL));
# this is only invoked when user invokes
# "printcomputer" explicitly
Eval_printcomputer = ->
stringsEmittedByUserPrintouts +=_print(cdr(p1),PRINTMODE_COMPUTER)
push(symbol(NIL));
# this is only invoked when user invokes
# "printlatex" explicitly
Eval_printlatex = ->
stringsEmittedByUserPrintouts +=_print(cdr(p1),PRINTMODE_LATEX)
push(symbol(NIL));
# this is only invoked when user invokes
# "printhuman" explicitly
Eval_printhuman = ->
# test flag needs to be suspended
# because otherwise "printcomputer" mode
# will happen.
original_test_flag = test_flag
test_flag = 0
stringsEmittedByUserPrintouts +=_print(cdr(p1),PRINTMODE_HUMAN)
test_flag = original_test_flag
push(symbol(NIL));
# this is only invoked when user invokes
# "printlist" explicitly
Eval_printlist = ->
beenPrinted = _print(cdr(p1),PRINTMODE_LIST)
stringsEmittedByUserPrintouts += beenPrinted
push(symbol(NIL))
_print = (p, passedPrintMode) ->
accumulator = ""
while (iscons(p))
push(car(p));
Eval();
p2 = pop();
# display single symbol as "symbol = result"
# but don't display "symbol = symbol"
###
if (issymbol(car(p)) && car(p) != p2)
push_symbol(SETQ);
push(car(p));
push(p2);
list(3);
p2 = pop();
###
origPrintMode = printMode
if passedPrintMode == PRINTMODE_COMPUTER
printMode = PRINTMODE_COMPUTER
accumulator = printline(p2);
rememberPrint(accumulator, LAST_FULL_PRINT)
else if passedPrintMode == PRINTMODE_HUMAN
printMode = PRINTMODE_HUMAN
accumulator = printline(p2);
rememberPrint(accumulator, LAST_PLAIN_PRINT)
else if passedPrintMode == PRINTMODE_2DASCII
printMode = PRINTMODE_2DASCII
accumulator = print2dascii(p2);
rememberPrint(accumulator, LAST_2DASCII_PRINT)
else if passedPrintMode == PRINTMODE_LATEX
printMode = PRINTMODE_LATEX
accumulator = printline(p2);
rememberPrint(accumulator, LAST_LATEX_PRINT)
else if passedPrintMode == PRINTMODE_LIST
printMode = PRINTMODE_LIST
accumulator = print_list(p2);
rememberPrint(accumulator, LAST_LIST_PRINT)
printMode = origPrintMode
p = cdr(p);
if DEBUG then console.log "emttedString from display: " + stringsEmittedByUserPrintouts
return accumulator
rememberPrint = (theString, theTypeOfPrint) ->
scan('"' + theString + '"')
parsedString = pop()
set_binding(symbol(theTypeOfPrint), parsedString)
print_str = (s) ->
if DEBUG then console.log "emttedString from print_str: " + stringsEmittedByUserPrintouts
return s
print_char = (c) ->
return c
collectLatexStringFromReturnValue = (p) ->
origPrintMode = printMode
printMode = PRINTMODE_LATEX
originalCodeGen = codeGen
codeGen = false
returnedString = print_expr(p)
# some variables might contain underscores, escape those
returnedString = returnedString.replace(/_/g, "\\_");
printMode = origPrintMode
codeGen = originalCodeGen
if DEBUG then console.log "emttedString from collectLatexStringFromReturnValue: " + stringsEmittedByUserPrintouts
return returnedString
printline = (p) ->
accumulator = ""
accumulator += print_expr(p)
return accumulator
print_base_of_denom = (p1) ->
accumulator = ""
if (isfraction(p1) || car(p1) == symbol(ADD) || car(p1) == symbol(MULTIPLY) || car(p1) == symbol(POWER) || lessp(p1, zero)) # p1 is BASE
accumulator += print_char('(')
accumulator += print_expr(p1); # p1 is BASE
accumulator += print_char(')')
else
accumulator += print_expr(p1); # p1 is BASE
return accumulator
print_expo_of_denom = (p2) ->
accumulator = ""
if (isfraction(p2) || car(p2) == symbol(ADD) || car(p2) == symbol(MULTIPLY) || car(p2) == symbol(POWER)) # p2 is EXPO
accumulator += print_char('(')
accumulator += print_expr(p2); # p2 is EXPO
accumulator += print_char(')')
else
accumulator += print_expr(p2); # p2 is EXPO
return accumulator
# prints stuff after the divide symbol "/"
# d is the number of denominators
#define BASE p1
#define EXPO p2
print_denom = (p, d) ->
accumulator = ""
save()
p1 = cadr(p); # p1 is BASE
p2 = caddr(p); # p2 is EXPO
# i.e. 1 / (2^(1/3))
# get the cases like BASE^(-1) out of
# the way, they just become 1/BASE
if (isminusone(p2)) # p2 is EXPO
accumulator += print_base_of_denom p1
restore()
return accumulator
if (d == 1) # p2 is EXPO
accumulator += print_char('(')
# prepare the exponent
# (needs to be negated)
# before printing it out
push(p2); # p2 is EXPO
negate()
p2 = pop(); # p2 is EXPO
accumulator += print_power(p1,p2)
if (d == 1)
accumulator += print_char(')')
restore()
return accumulator
#define A p3
#define B p4
print_a_over_b = (p) ->
accumulator = ""
flag = 0
n = 0
d = 0
save()
# count numerators and denominators
n = 0
d = 0
p1 = cdr(p)
p2 = car(p1)
if (isrational(p2))
push(p2)
mp_numerator()
absval()
p3 = pop(); # p3 is A
push(p2)
mp_denominator()
p4 = pop(); # p4 is B
if (!isplusone(p3)) # p3 is A
n++
if (!isplusone(p4)) # p4 is B
d++
p1 = cdr(p1)
else
p3 = one; # p3 is A
p4 = one; # p4 is B
while (iscons(p1))
p2 = car(p1)
if (is_denominator(p2))
d++
else
n++
p1 = cdr(p1)
#debugger
if printMode == PRINTMODE_LATEX
accumulator += print_str('\\frac{')
if (n == 0)
accumulator += print_char('1')
else
flag = 0
p1 = cdr(p)
if (isrational(car(p1)))
p1 = cdr(p1)
if (!isplusone(p3)) # p3 is A
accumulator += print_factor(p3); # p3 is A
flag = 1
while (iscons(p1))
p2 = car(p1)
if (is_denominator(p2))
doNothing = 1
else
if (flag)
accumulator += print_multiply_sign()
accumulator += print_factor(p2)
flag = 1
p1 = cdr(p1)
if printMode == PRINTMODE_LATEX
accumulator += print_str('}{')
else if printMode == PRINTMODE_HUMAN and !test_flag
accumulator += print_str(" / ")
else
accumulator += print_str("/")
if (d > 1 and printMode != PRINTMODE_LATEX)
accumulator += print_char('(')
flag = 0
p1 = cdr(p)
if (isrational(car(p1)))
p1 = cdr(p1)
if (!isplusone(p4)) # p4 is B
accumulator += print_factor(p4); # p4 is B
flag = 1
while (iscons(p1))
p2 = car(p1)
if (is_denominator(p2))
if (flag)
accumulator += print_multiply_sign()
accumulator += print_denom(p2, d)
flag = 1
p1 = cdr(p1)
if (d > 1 and printMode != PRINTMODE_LATEX)
accumulator += print_char(')')
if printMode == PRINTMODE_LATEX
accumulator += print_str('}')
restore()
return accumulator
print_expr = (p) ->
accumulator = ""
if (isadd(p))
p = cdr(p)
if (sign_of_term(car(p)) == '-')
accumulator += print_str("-")
accumulator += print_term(car(p))
p = cdr(p)
while (iscons(p))
if (sign_of_term(car(p)) == '+')
if printMode == PRINTMODE_HUMAN and !test_flag
accumulator += print_str(" + ")
else
accumulator += print_str("+")
else
if printMode == PRINTMODE_HUMAN and !test_flag
accumulator += print_str(" - ")
else
accumulator += print_str("-")
accumulator += print_term(car(p))
p = cdr(p)
else
if (sign_of_term(p) == '-')
accumulator += print_str("-")
accumulator += print_term(p)
return accumulator
sign_of_term = (p) ->
accumulator = ""
if (car(p) == symbol(MULTIPLY) && isNumericAtom(cadr(p)) && lessp(cadr(p), zero))
accumulator += '-'
else if (isNumericAtom(p) && lessp(p, zero))
accumulator += '-'
else
accumulator += '+'
return accumulator
print_term = (p) ->
accumulator = ""
if (car(p) == symbol(MULTIPLY) && any_denominators(p))
accumulator += print_a_over_b(p)
return accumulator
if (car(p) == symbol(MULTIPLY))
p = cdr(p)
# coeff -1?
if (isminusone(car(p)))
# print_char('-')
p = cdr(p)
previousFactorWasANumber = false
# print the first factor ------------
if isNumericAtom(car(p))
previousFactorWasANumber = true
# this numberOneOverSomething thing is so that
# we show things of the form
# numericFractionOfForm1/something * somethingElse
# as
# somethingElse / something
# so for example 1/2 * sqrt(2) is rendered as
# sqrt(2)/2
# rather than the first form, which looks confusing.
# NOTE that you might want to avoid this
# when printing polynomials, as it could be nicer
# to show the numeric coefficients well separated from
# the variable, but we'll see when we'll
# come to it if it's an issue.
numberOneOverSomething = false
if printMode == PRINTMODE_LATEX and iscons(cdr(p)) and isNumberOneOverSomething(car(p))
numberOneOverSomething = true
denom = car(p).q.b.toString()
if numberOneOverSomething
origAccumulator = accumulator
accumulator = ""
else
accumulator += print_factor(car(p))
p = cdr(p)
# print all the other factors -------
while (iscons(p))
# check if we end up having a case where two numbers
# are next to each other. In those cases, latex needs
# to insert a \cdot otherwise they end up
# right next to each other and read like one big number
if printMode == PRINTMODE_LATEX
if previousFactorWasANumber
# if what comes next is a power and the base
# is a number, then we are in the case
# of consecutive numbers.
# Note that sqrt() i.e when exponent is 1/2
# doesn't count because the radical gives
# a nice graphical separation already.
if caar(p) == symbol(POWER)
if isNumericAtom(car(cdr(car(p))))
# rule out square root
if !isfraction(car(cdr(cdr(car(p)))))
accumulator += " \\cdot "
accumulator += print_multiply_sign()
accumulator += print_factor(car(p))
previousFactorWasANumber = false
if isNumericAtom(car(p))
previousFactorWasANumber = true
p = cdr(p)
if numberOneOverSomething
accumulator = origAccumulator + "\\frac{" + accumulator + "}{" + denom + "}"
else
accumulator += print_factor(p)
return accumulator
print_subexpr = (p) ->
accumulator = ""
accumulator += print_char('(')
accumulator += print_expr(p)
accumulator += print_char(')')
return accumulator
print_factorial_function = (p) ->
accumulator = ""
p = cadr(p)
if (car(p) == symbol(ADD) || car(p) == symbol(MULTIPLY) || car(p) == symbol(POWER) || car(p) == symbol(FACTORIAL))
accumulator += print_subexpr(p)
else
accumulator += print_expr(p)
accumulator += print_char('!')
return accumulator
print_ABS_latex = (p) ->
accumulator = ""
accumulator += print_str("\\left |")
accumulator += print_expr(cadr(p))
accumulator += print_str(" \\right |")
return accumulator
print_BINOMIAL_latex = (p) ->
accumulator = ""
accumulator += print_str("\\binom{")
accumulator += print_expr(cadr(p))
accumulator += print_str("}{")
accumulator += print_expr(caddr(p))
accumulator += print_str("} ")
return accumulator
print_DOT_latex = (p) ->
accumulator = ""
accumulator += print_expr(cadr(p))
accumulator += print_str(" \\cdot ")
accumulator += print_expr(caddr(p))
return accumulator
print_DOT_codegen = (p) ->
accumulator = "dot("
accumulator += print_expr(cadr(p))
accumulator += ", "
accumulator += print_expr(caddr(p))
accumulator += ")"
return accumulator
print_SIN_codegen = (p) ->
accumulator = "Math.sin("
accumulator += print_expr(cadr(p))
accumulator += ")"
return accumulator
print_COS_codegen = (p) ->
accumulator = "Math.cos("
accumulator += print_expr(cadr(p))
accumulator += ")"
return accumulator
print_TAN_codegen = (p) ->
accumulator = "Math.tan("
accumulator += print_expr(cadr(p))
accumulator += ")"
return accumulator
print_ARCSIN_codegen = (p) ->
accumulator = "Math.asin("
accumulator += print_expr(cadr(p))
accumulator += ")"
return accumulator
print_ARCCOS_codegen = (p) ->
accumulator = "Math.acos("
accumulator += print_expr(cadr(p))
accumulator += ")"
return accumulator
print_ARCTAN_codegen = (p) ->
accumulator = "Math.atan("
accumulator += print_expr(cadr(p))
accumulator += ")"
return accumulator
print_SQRT_latex = (p) ->
accumulator = ""
accumulator += print_str("\\sqrt{")
accumulator += print_expr(cadr(p))
accumulator += print_str("} ")
return accumulator
print_TRANSPOSE_latex = (p) ->
accumulator = ""
accumulator += print_str("{")
if iscons(cadr(p))
accumulator += print_str('(')
accumulator += print_expr(cadr(p))
if iscons(cadr(p))
accumulator += print_str(')')
accumulator += print_str("}")
accumulator += print_str("^T")
return accumulator
print_TRANSPOSE_codegen = (p) ->
accumulator = ""
accumulator += print_str("transpose(")
accumulator += print_expr(cadr(p))
accumulator += print_str(')')
return accumulator
print_UNIT_codegen = (p) ->
accumulator = ""
accumulator += print_str("identity(")
accumulator += print_expr(cadr(p))
accumulator += print_str(')')
return accumulator
print_INV_latex = (p) ->
accumulator = ""
accumulator += print_str("{")
if iscons(cadr(p))
accumulator += print_str('(')
accumulator += print_expr(cadr(p))
if iscons(cadr(p))
accumulator += print_str(')')
accumulator += print_str("}")
accumulator += print_str("^{-1}")
return accumulator
print_INV_codegen = (p) ->
accumulator = ""
accumulator += print_str("inv(")
accumulator += print_expr(cadr(p))
accumulator += print_str(')')
return accumulator
print_DEFINT_latex = (p) ->
accumulator = ""
functionBody = car(cdr(p))
p = cdr(p)
originalIntegral = p
numberOfIntegrals = 0
while iscons(cdr(cdr(p)))
numberOfIntegrals++
theIntegral = cdr(cdr(p))
accumulator += print_str("\\int^{")
accumulator += print_expr(car(cdr(theIntegral)))
accumulator += print_str("}_{")
accumulator += print_expr(car(theIntegral))
accumulator += print_str("} \\! ")
p = cdr(theIntegral)
accumulator += print_expr(functionBody)
accumulator += print_str(" \\,")
p = originalIntegral
for i in [1..numberOfIntegrals]
theVariable = cdr(p)
accumulator += print_str(" \\mathrm{d} ")
accumulator += print_expr(car(theVariable))
if i < numberOfIntegrals
accumulator += print_str(" \\, ")
p = cdr(cdr(theVariable))
return accumulator
print_tensor = (p) ->
accumulator = ""
accumulator += print_tensor_inner(p, 0, 0)[1]
return accumulator
# j scans the dimensions
# k is an increment for all the printed elements
# since they are all together in sequence in one array
print_tensor_inner = (p, j, k) ->
accumulator = ""
accumulator += print_str("[")
# only the last dimension prints the actual elements
# e.g. in a matrix, the first dimension contains
# vectors, not elements, and the second dimension
# actually contains the elements
# if not the last dimension, we are just printing wrappers
# and recursing down i.e. we print the next dimension
if (j < p.tensor.ndim - 1)
for i in [0...p.tensor.dim[j]]
[k, retString] = print_tensor_inner(p, j + 1, k)
accumulator += retString
# add separator between elements dimensions
# "above" the inner-most dimension
if i != p.tensor.dim[j] - 1
accumulator += print_str(",")
# if we reached the last dimension, we print the actual
# elements
else
for i in [0...p.tensor.dim[j]]
accumulator += print_expr(p.tensor.elem[k])
# add separator between elements in the
# inner-most dimension
if i != p.tensor.dim[j] - 1
accumulator += print_str(",")
k++
accumulator += print_str("]")
return [k, accumulator]
print_tensor_latex = (p) ->
accumulator = ""
if p.tensor.ndim <= 2
accumulator += print_tensor_inner_latex(true, p, 0, 0)[1]
return accumulator
# firstLevel is needed because printing a matrix
# is not exactly an elegant recursive procedure:
# the vector on the first level prints the latex
# "wrap", while the vectors that make up the
# rows don't. so it's a bit asymmetric and this
# flag helps.
# j scans the dimensions
# k is an increment for all the printed elements
# since they are all together in sequence in one array
print_tensor_inner_latex = (firstLevel, p, j, k) ->
accumulator = ""
# open the outer latex wrap
if firstLevel
accumulator += "\\begin{bmatrix} "
# only the last dimension prints the actual elements
# e.g. in a matrix, the first dimension contains
# vectors, not elements, and the second dimension
# actually contains the elements
# if not the last dimension, we are just printing wrappers
# and recursing down i.e. we print the next dimension
if (j < p.tensor.ndim - 1)
for i in [0...p.tensor.dim[j]]
[k, retString] = print_tensor_inner_latex(0, p, j + 1, k)
accumulator += retString
if i != p.tensor.dim[j] - 1
# add separator between rows
accumulator += print_str(" \\\\ ")
# if we reached the last dimension, we print the actual
# elements
else
for i in [0...p.tensor.dim[j]]
accumulator += print_expr(p.tensor.elem[k])
# separator between elements in each row
if i != p.tensor.dim[j] - 1
accumulator += print_str(" & ")
k++
# close the outer latex wrap
if firstLevel
accumulator += " \\end{bmatrix}"
return [k, accumulator]
print_SUM_latex = (p) ->
accumulator = "\\sum_{"
accumulator += print_expr(caddr(p))
accumulator += "="
accumulator += print_expr(cadddr(p))
accumulator += "}^{"
accumulator += print_expr(caddddr(p))
accumulator += "}{"
accumulator += print_expr(cadr(p))
accumulator += "}"
return accumulator
print_SUM_codegen = (p) ->
body = cadr(p)
variable = caddr(p)
lowerlimit = cadddr(p)
upperlimit = caddddr(p)
accumulator =
"(function(){" +
" var " + variable + "; " +
" var holderSum = 0; " +
" var lowerlimit = " + print_expr(lowerlimit) + "; " +
" var upperlimit = " + print_expr(upperlimit) + "; " +
" for (" + variable + " = lowerlimit; " + variable + " < upperlimit; " + variable + "++) { " +
" holderSum += " + print_expr(body) + ";" +
" } "+
" return holderSum;" +
"})()"
return accumulator
print_TEST_latex = (p) ->
accumulator = "\\left\\{ \\begin{array}{ll}"
p = cdr(p)
while (iscons(p))
# odd number of parameters means that the
# last argument becomes the default case
# i.e. the one without a test.
if (cdr(p) == symbol(NIL))
accumulator += "{"
accumulator += print_expr(car(p))
accumulator += "} & otherwise "
accumulator += " \\\\\\\\"
break
accumulator += "{"
accumulator += print_expr(cadr(p))
accumulator += "} & if & "
accumulator += print_expr(car(p))
accumulator += " \\\\\\\\"
# test unsuccessful, continue to the
# next pair of test,value
p = cddr(p)
accumulator = accumulator.substring(0, accumulator.length - 4);
accumulator += "\\end{array} \\right."
print_TEST_codegen = (p) ->
accumulator = "(function(){"
p = cdr(p)
howManyIfs = 0
while (iscons(p))
# odd number of parameters means that the
# last argument becomes the default case
# i.e. the one without a test.
if (cdr(p) == symbol(NIL))
accumulator += "else {"
accumulator += "return (" + print_expr(car(p)) + ");"
accumulator += "}"
break
if howManyIfs
accumulator += " else "
accumulator += "if (" + print_expr(car(p)) + "){"
accumulator += "return (" + print_expr(cadr(p)) + ");"
accumulator += "}"
# test unsuccessful, continue to the
# next pair of test,value
howManyIfs++
p = cddr(p)
accumulator += "})()"
return accumulator
print_TESTLT_latex = (p) ->
accumulator = "{"
accumulator += print_expr(cadr(p))
accumulator += "}"
accumulator += " < "
accumulator += "{"
accumulator += print_expr(caddr(p))
accumulator += "}"
print_TESTLE_latex = (p) ->
accumulator = "{"
accumulator += print_expr(cadr(p))
accumulator += "}"
accumulator += " \\leq "
accumulator += "{"
accumulator += print_expr(caddr(p))
accumulator += "}"
print_TESTGT_latex = (p) ->
accumulator = "{"
accumulator += print_expr(cadr(p))
accumulator += "}"
accumulator += " > "
accumulator += "{"
accumulator += print_expr(caddr(p))
accumulator += "}"
print_TESTGE_latex = (p) ->
accumulator = "{"
accumulator += print_expr(cadr(p))
accumulator += "}"
accumulator += " \\geq "
accumulator += "{"
accumulator += print_expr(caddr(p))
accumulator += "}"
print_TESTEQ_latex = (p) ->
accumulator = "{"
accumulator += print_expr(cadr(p))
accumulator += "}"
accumulator += " = "
accumulator += "{"
accumulator += print_expr(caddr(p))
accumulator += "}"
print_FOR_codegen = (p) ->
body = cadr(p)
variable = caddr(p)
lowerlimit = cadddr(p)
upperlimit = caddddr(p)
accumulator =
"(function(){" +
" var " + variable + "; " +
" var lowerlimit = " + print_expr(lowerlimit) + "; " +
" var upperlimit = " + print_expr(upperlimit) + "; " +
" for (" + variable + " = lowerlimit; " + variable + " < upperlimit; " + variable + "++) { " +
" " + print_expr(body) +
" } "+
"})()"
return accumulator
print_DO_codegen = (p) ->
accumulator = ""
p = cdr(p)
while iscons(p)
accumulator += print_expr(car(p))
p = cdr(p)
return accumulator
print_SETQ_codegen = (p) ->
accumulator = ""
accumulator += print_expr(cadr(p))
accumulator += " = "
accumulator += print_expr(caddr(p))
accumulator += "; "
return accumulator
print_PRODUCT_latex = (p) ->
accumulator = "\\prod_{"
accumulator += print_expr(caddr(p))
accumulator += "="
accumulator += print_expr(cadddr(p))
accumulator += "}^{"
accumulator += print_expr(caddddr(p))
accumulator += "}{"
accumulator += print_expr(cadr(p))
accumulator += "}"
return accumulator
print_PRODUCT_codegen = (p) ->
body = cadr(p)
variable = caddr(p)
lowerlimit = cadddr(p)
upperlimit = caddddr(p)
accumulator =
"(function(){" +
" var " + variable + "; " +
" var holderProduct = 1; " +
" var lowerlimit = " + print_expr(lowerlimit) + "; " +
" var upperlimit = " + print_expr(upperlimit) + "; " +
" for (" + variable + " = lowerlimit; " + variable + " < upperlimit; " + variable + "++) { " +
" holderProduct *= " + print_expr(body) + ";" +
" } "+
" return holderProduct;" +
"})()"
return accumulator
print_base = (p) ->
accumulator = ""
if (isadd(cadr(p)) || caadr(p) == symbol(MULTIPLY) || caadr(p) == symbol(POWER) || isnegativenumber(cadr(p)))
accumulator += print_str('(')
accumulator += print_expr(cadr(p))
accumulator += print_str(')')
else if (isNumericAtom(cadr(p)) && (lessp(cadr(p), zero) || isfraction(cadr(p))))
accumulator += print_str('(')
accumulator += print_factor(cadr(p))
accumulator += print_str(')')
else
accumulator += print_factor(cadr(p))
return accumulator
print_exponent = (p) ->
accumulator = ""
if (iscons(caddr(p)) || isfraction(caddr(p)) || (isNumericAtom(caddr(p)) && lessp(caddr(p), zero)))
accumulator += print_str('(')
accumulator += print_expr(caddr(p))
accumulator += print_str(')')
else
accumulator += print_factor(caddr(p))
return accumulator
print_power = (base, exponent) ->
accumulator = ""
#debugger
if DEBUG then console.log "power base: " + base + " " + " exponent: " + exponent
# quick check is this is actually a square root.
if isoneovertwo(exponent)
if equaln(base, 2)
if codeGen
accumulator += print_str("Math.SQRT2")
return accumulator
else
if printMode == PRINTMODE_LATEX
accumulator += print_str("\\sqrt{")
accumulator += print_expr(base)
accumulator += print_str("}")
return accumulator
else if codeGen
accumulator += print_str("Math.sqrt(")
accumulator += print_expr(base)
accumulator += print_str(')')
return accumulator
if ((equaln(get_binding(symbol(PRINT_LEAVE_E_ALONE)), 1)) and base == symbol(E))
if codeGen
accumulator += print_str("Math.exp(")
accumulator += print_expo_of_denom exponent
accumulator += print_str(')')
return accumulator
if printMode == PRINTMODE_LATEX
accumulator += print_str("e^{")
accumulator += print_expr(exponent)
accumulator += print_str("}")
else
accumulator += print_str("exp(")
accumulator += print_expr(exponent)
accumulator += print_str(')')
return accumulator
if codeGen
accumulator += print_str("Math.pow(")
accumulator += print_base_of_denom base
accumulator += print_str(", ")
accumulator += print_expo_of_denom exponent
accumulator += print_str(')')
return accumulator
if ((equaln(get_binding(symbol(PRINT_LEAVE_X_ALONE)), 0)) or base.printname != "x")
# if the exponent is negative then
# we invert the base BUT we don't do
# that if the base is "e", because for
# example when trigonometric functions are
# expressed in terms of exponential functions
# that would be really confusing, one wants to
# keep "e" as the base and the negative exponent
if (base != symbol(E))
if (isminusone(exponent))
if printMode == PRINTMODE_LATEX
accumulator += print_str("\\frac{1}{")
else if printMode == PRINTMODE_HUMAN and !test_flag
accumulator += print_str("1 / ")
else
accumulator += print_str("1/")
if (iscons(base) and printMode != PRINTMODE_LATEX)
accumulator += print_str('(')
accumulator += print_expr(base)
accumulator += print_str(')')
else
accumulator += print_expr(base)
if printMode == PRINTMODE_LATEX
accumulator += print_str("}")
return accumulator
if (isnegativeterm(exponent))