-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.rkt
668 lines (570 loc) · 24.7 KB
/
project.rkt
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
;; PL Project - Fall 2018
;; NUMEX interpreter
#lang racket
(provide (all-defined-out)) ;; so we can put tests in a second file
(require racket/set)
;; definition of structures for NUMEX programs
;; CHANGE add the missing ones
(struct var (string) #:transparent) ;; a variable, e.g., (var "foo")
(struct num (int) #:transparent) ;; a constant number, e.g., (num 17)
(struct bool (bit) #:transparent) ;; a boolean, e.g., (bool false)
(struct plus (e1 e2) #:transparent) ;; add two expressions
(struct minus (e1 e2) #:transparent) ;; minus two expressions
(struct mult (e1 e2) #:transparent) ;; multiple two expressions
(struct div (e1 e2) #:transparent) ;; divide two expressions
(struct neg (e1) #:transparent) ;; negation of an expression
(struct andalso (e1 e2) #:transparent) ;; logical conjunction
(struct orelse (e1 e2) #:transparent) ;; logical disjunction
(struct cnd (e1 e2 e3) #:transparent) ;; conditions
(struct iseq (e1 e2) #:transparent) ;; add two expressions
(struct ifnzero (e1 e2 e3) #:transparent) ;; add two expressions
(struct ifleq (e1 e2 e3 e4) #:transparent) ;; add two expressions
(struct with (s e1 e2) #:transparent) ;; add two expressions
(struct apair (e1 e2) #:transparent) ;; add two expressions
(struct 1st (e1) #:transparent) ;; add two expressions
(struct 2nd (e1) #:transparent) ;; add two expressions
(struct lam (nameopt formal body) #:transparent) ;; a recursive(?) 1-argument function
(struct apply (funexp actual) #:transparent) ;; function application
(struct munit () #:transparent) ;; unit value -- good for ending a list
(struct ismunit (e) #:transparent) ;; if e1 is unit then true else false
(struct isnumexlist (e) #:transparent) ;; if e1 is list then true else false
;; a closure is not in "source" programs; it is what functions evaluate to
(struct closure (env f) #:transparent)
;; Problem 1
(define (racketlist->numexlist xs) (cond [(eq? xs null) (munit)]
[(list? xs) (apair (car xs)
(racketlist->numexlist (cdr xs)))]
[#t xs]
))
(define (numexlist->racketlist xs) (cond [(munit? xs) null]
[(apair? xs) (cons (numexlist->racketlist (apair-e1 xs))
(numexlist->racketlist (apair-e2 xs))
)]
[#t xs]
))
;; Problem 2
;; lookup a variable in an environment
;; Complete this function
(define (envlookup env str)
(cond [(null? env) (error "unbound variable during evaluation" str)]
[(eq? str (car (car env))) (cdr (car env))]
[#t (envlookup (cdr env) str)]
)
)
;; Complete more cases for other kinds of NUMEX expressions.
;; We will test eval-under-env by calling it directly even though
;; "in real life" it would be a helper function of eval-exp.
(define (eval-under-env e env)
(cond [(var? e) ;; Variables
(let ([v1 (eval-under-env (var-string e) env)])
(if (string? (var-string e))
(envlookup env (var-string e))
(error "NUMEX Var applied to non-string")))]
;;
;; Other
;;
[(lam? e)
(if (and (or (string? (lam-nameopt e)) (null? (lam-nameopt e))) (string? (lam-formal e)))
(closure env e)
(error "NUMEX function name and parameter name must be string"))]
[(with? e)
(eval-under-env (with-e2 e) (cons (cons (with-s e)(eval-under-env (with-e1 e) env)) env))]
[(apply? e)
(let ([v1 (eval-under-env (apply-funexp e) env)]
[v2 (eval-under-env (apply-actual e) env)])
(if (closure? v1)
(if (null? (lam-nameopt (closure-f v1)))
(eval-under-env (lam-body (closure-f v1)) (cons (cons (lam-formal (closure-f v1)) v2) (closure-env v1)))
(eval-under-env (lam-body (closure-f v1)) (cons (cons (lam-nameopt (closure-f v1)) v1) (cons (cons (lam-formal (closure-f v1)) v2) (closure-env v1)))))
(error "NUMUX apply applied to non-closure" v1 (apply-funexp e))
))]
;;
;; Pairs
;;
[(apair? e)
(let ([v1 (eval-under-env (apair-e1 e) env)]
[v2 (eval-under-env (apair-e2 e) env)])
(apair v1 v2))]
[(1st? e)
(let ([v1 (eval-under-env (1st-e1 e) env)])
(if (apair? v1)
(apair-e1 v1)
(error "NUMUX 1st applied to non-apair")))]
[(2nd? e)
(let ([v1 (eval-under-env (2nd-e1 e) env)])
(if (apair? v1)
(apair-e2 v1)
(error "NUMUX 1st applied to non-apair")))]
;;
;; Conditions
;;
[(cnd? e)
(let ([v1 (eval-under-env (cnd-e1 e) env)])
(if (bool? v1)
(if (bool-bit v1)
(eval-under-env (cnd-e2 e) env)
(eval-under-env (cnd-e3 e) env)
)
(error "NUMUX cnd guard applied to non-boolean"))
)]
[(iseq? e)
(let ([v1 (eval-under-env (iseq-e1 e) env)]
[v2 (eval-under-env (iseq-e2 e) env)])
(cond
[(and (num? v1)(num? v2))
(bool (eq? (num-int v1) (num-int v2)))]
[(and (bool? v1)(bool? v2))
(bool (eq? (bool-bit v1)(bool-bit v2)))]
[#t (bool #f)]
))]
[(ifnzero? e)
(let ([v1 (eval-under-env (ifnzero-e1 e) env)])
(if (num? v1)
(if (eq? (num-int v1) 0)
(eval-under-env (ifnzero-e3 e) env)
(eval-under-env (ifnzero-e2 e) env))
(error "NUMUX ifnzero applied to a non-number")
))]
[(ifleq? e)
(let ([v1 (eval-under-env (ifleq-e1 e) env)]
[v2 (eval-under-env (ifleq-e2 e) env)])
(if (and
(num? v2)
(num? v1))
(if (<= (num-int v1)(num-int v2))
(eval-under-env (ifleq-e3 e) env)
(eval-under-env (ifleq-e4 e) env))
(error "NUMUX ifleq applied to a non-number")
))]
[(ismunit? e)
(let ([v1 (eval-under-env (ismunit-e e) env)])
(bool (munit? v1))
)]
[(isnumexlist? e)
(let ([v1 (eval-under-env (isnumexlist-e e) env)])
(cond[(munit? v1) (bool #t)]
[(apair? v1) (eval-under-env (isnumexlist (2nd v1)) env)]
[#t (bool #f)])
)]
;;
;; Logical Operations
;;
[(andalso? e)
(let ([v1 (eval-under-env (andalso-e1 e) env)])
(let ([v3 (cond[(bool? v1) v1]
; [(num? v1) (if (eq? (num-int v1) 0) (bool #f) (bool #t))]
[#t error "NUMEX and-also applied to non-number or non-boolean"])])
(if (and (bool? v3) (eq? (bool-bit v3) #f))
(bool #f)
(let ([v2 (eval-under-env (andalso-e2 e) env)])
(let ([v4 (cond[(bool? v2) v2]
; [(num? v2) (if (eq? (num-int v2) 0) (bool #f) (bool #t))]
[#t error "NUMEX and-also applied to non-number or non-boolean"])])
v4
)))
))]
[(orelse? e)
(let ([v1 (eval-under-env (orelse-e1 e) env)])
(let ([v3 (cond[(bool? v1) v1]
[(num? v1) (if (eq? (num-int v1) 0) (bool #f) (bool #t))]
[#t error "NUMEX orelse applied to non-number or non-boolean"])])
(if (and (bool? v3) (eq? (bool-bit v3) #t))
(bool #t)
(let ([v2 (eval-under-env (orelse-e2 e) env)])
(let ([v4 (cond[(bool? v2) v2]
[(num? v2) (if (eq? (num-int v2) 0) (bool #f) (bool #t))]
[#t error "NUMEX orelse applied to non-number or non-boolean"])])
v4
)))
))]
;;
;; Arithmetic Operations
;;
[(plus? e)
(let ([v1 (eval-under-env (plus-e1 e) env)]
[v2 (eval-under-env (plus-e2 e) env)])
(if (and (num? v1)
(num? v2))
(num (+ (num-int v1)
(num-int v2)))
(error "NUMEX addition applied to non-number")))]
[(minus? e)
(let ([v1 (eval-under-env (minus-e1 e) env)]
[v2 (eval-under-env (minus-e2 e) env)])
(if (and (num? v1)
(num? v2))
(num (- (num-int v1)
(num-int v2)))
(error "NUMEX subtraction applied to non-number")))]
[(mult? e)
(let ([v1 (eval-under-env (mult-e1 e) env)]
[v2 (eval-under-env (mult-e2 e) env)])
(if (and (num? v1)
(num? v2))
(num (* (num-int v1)
(num-int v2)))
(error "NUMEX multiply applied to non-number")))]
[(div? e)
(let ([v1 (eval-under-env (div-e1 e) env)]
[v2 (eval-under-env (div-e2 e) env)])
(if (and (num? v1)
(num? v2))
(if (eq? (num-int v2) 0)
(error "NUMEX division applied to Zero" v2)
(num (quotient (num-int v1)
(num-int v2))))
(error "NUMEX division applied to non-number")))]
[(neg? e)
(let ([v1 (eval-under-env (neg-e1 e) env)])
(if (num? v1)
(num (- (num-int v1)))
(if (bool? v1)
(bool (if (bool-bit v1) #f #t))
(error "NUMEX Nagation applied to non-number or non-boolean"))
))]
[(num? e)
(let ([v1 (eval-under-env (num-int e) env)])
(if (integer? v1) (num v1) (error "NUMEX num applied to non-integer")))]
[(bool? e)
(let ([v1 (eval-under-env (bool-bit e) env)])
(if (boolean? v1) (bool v1) (error "NUMEX bool appllied to non-boolean")))]
[(closure? e) e]
[(number? e) e]
[(boolean? e) e]
[(string? e) e]
[(munit? e) e]
[#t (error (format "bad NUMEX expression: ~v" e))]))
;; Do NOT change
(define (eval-exp e)
(eval-under-env e null))
;; Problem 3
(define (ifmunit e1 e2 e3)
(cnd (ismunit e1) e2 e3)
)
(define (with* bs e2)
(if (null? bs)
e2
(with (car (car bs)) (cdr (car bs)) (with* (cdr bs) e2))))
(define (ifneq e1 e2 e3 e4)
(with* (cons (cons "_x" e1) (cons (cons "_y" e2) null))
(cnd (iseq (var "_x") (var "_y")) e4 e3))
)
;; Problem 4
(define numex-filter (lam null "f"
(
lam "map" "list"
(ifmunit (var "list")
(munit)
(ifnzero (apply (var "f") (1st (var "list")))
(apair (apply (var "f") (1st (var "list"))) (apply (var "map") (2nd (var "list"))))
(apply (var "map") (2nd (var "list")))
)
)
)
)
)
(define numex-all-gt
(with "filter" numex-filter
(lam null "i"
(lam null "list"
(apply
(apply
(var "filter")
(lam "gt" "num"
(ifleq (var "num") (var "i")
(num 0) ;; Whatever
(var "num")
)
)
)
(var "list")
)
)
)
)
)
;; Challenge Problem
(struct fun-challenge (nameopt formal body freevars) #:transparent) ;; a recursive(?) 1-argument function
;; We will test this function directly, so it must do
;; as described in the assignment
(define (compute-free-vars e)
(cond[(var? e) e]
;;
;; Other
;;
[(lam? e) (fun-challenge (lam-nameopt e) (lam-formal e) (compute-free-vars (lam-body e)) (cfv e))]
[(with? e) (with (with-s e) (compute-free-vars (with-e1 e)) (compute-free-vars (with-e2 e)))]
[(apply? e) (apply (compute-free-vars (apply-funexp e)) (compute-free-vars (apply-actual e)))]
;;
;; Pairs
;;
[(apair? e) (apair (compute-free-vars (apair-e1 e)) (compute-free-vars (apair-e2 e)))]
[(1st? e) (1st (compute-free-vars (1st-e1 e)))]
[(2nd? e) (2nd (compute-free-vars (2nd-e1 e)))]
;;
;; Conditions
;;
[(cnd? e) (cnd (compute-free-vars (cnd-e1 e)) (compute-free-vars (cnd-e2 e)) (compute-free-vars (cnd-e3 e)))]
[(iseq? e) (iseq (compute-free-vars (iseq-e1 e)) (compute-free-vars (iseq-e2 e)))]
[(ifnzero? e) (ifnzero (compute-free-vars (ifnzero-e1 e)) (compute-free-vars (ifnzero-e2 e)) (compute-free-vars (ifnzero-e3 e)))]
[(ifleq? e) (ifleq (compute-free-vars (ifleq-e1 e)) (compute-free-vars (ifleq-e2 e)) (compute-free-vars (ifleq-e3 e)) (compute-free-vars (ifleq-e4 e)))]
[(ismunit? e) (ismunit (compute-free-vars (ismunit-e e)))]
;;
;; Logical Operations
;;
[(andalso? e) (andalso (compute-free-vars (andalso-e1 e)) (compute-free-vars (andalso-e2 e)))]
[(orelse? e) (orelse (compute-free-vars (orelse-e1 e)) (compute-free-vars (orelse-e2 e)))]
;;
;; Arithmetic Operations
;;
[(plus? e) (plus (compute-free-vars (plus-e1 e)) (compute-free-vars (plus-e2 e)))]
[(minus? e) (minus (compute-free-vars (minus-e1 e)) (compute-free-vars (minus-e2 e)))]
[(mult? e) (mult (compute-free-vars (mult-e1 e)) (compute-free-vars (mult-e2 e)))]
[(div? e) (div (compute-free-vars (div-e1 e)) (compute-free-vars (div-e2 e)))]
[(neg? e) (neg (compute-free-vars (neg-e1 e)))]
[(num? e) e]
[(bool? e) e]
[(closure? e) e]
[(string? e) e]
[(munit? e) e]
[#t (error "NUMEX Compute free vars wrong type" e)]
)
)
(define (cfv e)
(cond[(var? e) ;; Variables
(set (var-string e))]
;;
;; Other
;;
[(lam? e) (set-remove (set-remove (cfv (lam-body e)) (lam-nameopt e)) (lam-formal e))]
[(with? e) (set-union (set-remove (cfv (with-e2 e)) (with-s e)) (cfv (with-e1 e)) )]
[(apply? e) (set-union (cfv (apply-funexp e))(cfv (apply-actual e)))]
;;
;; Pairs
;;
[(apair? e) (set-union (cfv (apair-e1 e)) (cfv (apair-e2 e)))]
[(1st? e) (cfv (1st-e1 e))]
[(2nd? e) (cfv (2nd-e1 e))]
;;
;; Conditions
;;
[(cnd? e) (set-union (cfv (cnd-e1 e)) (cfv (cnd-e2 e)) (cfv (cnd-e3 e)))]
[(iseq? e) (set-union (cfv (iseq-e1 e)) (cfv (iseq-e2 e)))]
[(ifnzero? e) (set-union (cfv (ifnzero-e1 e)) (cfv (ifnzero-e2 e)) (cfv (ifnzero-e3 e)))]
[(ifleq? e) (set-union (cfv (ifleq-e1 e)) (cfv (ifleq-e2 e)) (cfv (ifleq-e3 e)) (cfv (ifleq-e4 e)))]
[(ismunit? e) (cfv (ismunit-e e))]
;;
;; Logical Operations
;;
[(andalso? e) (set-union (cfv (andalso-e1 e)) (cfv (andalso-e2 e)))]
[(orelse? e) (set-union (cfv (orelse-e1 e)) (cfv (orelse-e2 e)))]
;;
;; Arithmetic Operations
;;
[(plus? e) (set-union (cfv (plus-e1 e)) (cfv (plus-e2 e)))]
[(minus? e) (set-union (cfv (minus-e1 e)) (cfv (minus-e2 e)))]
[(mult? e) (set-union (cfv (mult-e1 e)) (cfv (mult-e2 e)))]
[(div? e) (set-union (cfv (div-e1 e)) (cfv (div-e2 e)))]
[(neg? e) (cfv (neg-e1 e))]
[(num? e) (set)]
[(bool? e) (set)]
[(closure? e) (set)]
[(munit? e) (set)]
[(string? e) (set)]
[#t (error (format "bad NUMEX expression: ~v" e))]
)
)
(define (new-env env free)
(cond[(null? env) null]
[(set-member? free (car (car env))) (cons (car env) (new-env (cdr env) (set-remove free (car (car env)))))]
[#t (new-env (cdr env) free)]
)
)
;; Do NOT share code with eval-under-env because that will make grading
;; more difficult, so copy most of your interpreter here and make minor changes
(define (eval-under-env-c e env)
(cond [(var? e) ;; Variables
(let ([v1 (eval-under-env-c (var-string e) env)])
(if (string? (var-string e))
(envlookup env (var-string e))
(error "NUMEX Var applied to non-string")))]
;;
;; Other
;;
[(fun-challenge? e)
(if (and (or (string? (fun-challenge-nameopt e)) (null? (fun-challenge-nameopt e))) (string? (fun-challenge-formal e)))
(closure (new-env env (fun-challenge-freevars e)) e)
(error "NUMEX function name and parameter name must be string"))]
[(with? e)
(eval-under-env-c (with-e2 e) (cons (cons (with-s e)(eval-under-env-c (with-e1 e) env)) env))]
[(apply? e)
(let ([v1 (eval-under-env-c (apply-funexp e) env)]
[v2 (eval-under-env-c (apply-actual e) env)])
(if (closure? v1)
(if (null? (fun-challenge-nameopt (closure-f v1)))
(eval-under-env-c (fun-challenge-body (closure-f v1)) (cons (cons (fun-challenge-formal (closure-f v1)) v2) (closure-env v1)))
(eval-under-env-c (fun-challenge-body (closure-f v1)) (cons (cons (fun-challenge-nameopt (closure-f v1)) v1) (cons (cons (fun-challenge-formal (closure-f v1)) v2) (closure-env v1)))))
(error "NUMUX apply applied to non-closure" v1 (apply-funexp e))
))]
; [(apply? e)
; (let ([v1 (eval-under-env-c (apply-funexp e) env)]
; )
; (if (closure? v1)
; (if (null? (fun-challenge-nameopt (closure-f v1)))
; (eval-under-env-c (fun-challenge-body (closure-f v1)) (cons (cons (fun-challenge-formal (closure-f v1)) v2) (closure-env v1)))
; (eval-under-env-c (fun-challenge-body (closure-f v1)) (cons (cons (fun-challenge-nameopt (closure-f v1)) v1) (cons (cons (fun-challenge-formal (closure-f v1)) v2) (closure-env v1)))))
; (error "NUMUX apply applied to non-closure" v1 (apply-funexp e))
; ))]
;;
;; Pairs
;;
[(apair? e)
(let ([v1 (eval-under-env-c (apair-e1 e) env)]
[v2 (eval-under-env-c (apair-e2 e) env)])
(apair v1 v2))]
[(1st? e)
(let ([v1 (eval-under-env-c (1st-e1 e) env)])
(if (apair? v1)
(apair-e1 v1)
(error "NUMUX 1st applied to non-apair")))]
[(2nd? e)
(let ([v1 (eval-under-env-c (2nd-e1 e) env)])
(if (apair? v1)
(apair-e2 v1)
(error "NUMUX 1st applied to non-apair")))]
;;
;; Conditions
;;
[(cnd? e)
(let ([v1 (eval-under-env-c (cnd-e1 e) env)])
(if (bool? v1)
(if (bool-bit v1)
(eval-under-env-c (cnd-e2 e) env)
(eval-under-env-c (cnd-e3 e) env)
)
(error "NUMUX cnd guard applied to non-boolean"))
)]
[(iseq? e)
(let ([v1 (eval-under-env-c (iseq-e1 e) env)]
[v2 (eval-under-env-c (iseq-e2 e) env)])
(cond
[(and (num? v1)(num? v2))
(bool (eq? (num-int v1) (num-int v2)))]
[(and (bool? v1)(bool? v2))
(bool (eq? (bool-bit v1)(bool-bit v2)))]
[#t (bool #f)]
))]
[(ifnzero? e)
(let ([v1 (eval-under-env-c (ifnzero-e1 e) env)])
(if (num? v1)
(if (eq? (num-int v1) 0)
(eval-under-env-c (ifnzero-e3 e) env)
(eval-under-env-c (ifnzero-e2 e) env))
(error "NUMUX ifnzero applied to a non-number")
))]
[(ifleq? e)
(let ([v1 (eval-under-env-c (ifleq-e1 e) env)]
[v2 (eval-under-env-c (ifleq-e2 e) env)])
(if (and
(num? v2)
(num? v1))
(if (<= (num-int v1)(num-int v2))
(eval-under-env-c (ifleq-e3 e) env)
(eval-under-env-c (ifleq-e4 e) env))
(error "NUMUX ifleq applied to a non-number")
))]
[(ismunit? e)
(let ([v1 (eval-under-env-c (ismunit-e e) env)])
(bool (munit? v1))
)]
;;
;; Logical Operations
;;
[(andalso? e)
(let ([v1 (eval-under-env-c (andalso-e1 e) env)])
(let ([v3 (cond[(bool? v1) v1]
[(num? v1) (if (eq? (num-int v1) 0) (bool #f) (bool #t))]
[#t error "NUMEX and-also applied to non-number or non-boolean"])])
(if (and (bool? v3) (eq? (bool-bit v3) #f))
(bool #f)
(let ([v2 (eval-under-env-c (andalso-e2 e) env)])
(let ([v4 (cond[(bool? v2) v2]
[(num? v2) (if (eq? (num-int v2) 0) (bool #f) (bool #t))]
[#t error "NUMEX and-also applied to non-number or non-boolean"])])
v4
)))
))]
[(orelse? e)
(let ([v1 (eval-under-env-c (orelse-e1 e) env)])
(let ([v3 (cond[(bool? v1) v1]
[(num? v1) (if (eq? (num-int v1) 0) (bool #f) (bool #t))]
[#t error "NUMEX orelse applied to non-number or non-boolean"])])
(if (and (bool? v3) (eq? (bool-bit v3) #t))
(bool #t)
(let ([v2 (eval-under-env-c (orelse-e2 e) env)])
(let ([v4 (cond[(bool? v2) v2]
[(num? v2) (if (eq? (num-int v2) 0) (bool #f) (bool #t))]
[#t error "NUMEX orelse applied to non-number or non-boolean"])])
v4
)))
))]
;;
;; Arithmetic Operations
;;
[(plus? e)
(let ([v1 (eval-under-env-c (plus-e1 e) env)]
[v2 (eval-under-env-c (plus-e2 e) env)])
(if (and (num? v1)
(num? v2))
(num (+ (num-int v1)
(num-int v2)))
(error "NUMEX addition applied to non-number")))]
[(minus? e)
(let ([v1 (eval-under-env-c (minus-e1 e) env)]
[v2 (eval-under-env-c (minus-e2 e) env)])
(if (and (num? v1)
(num? v2))
(num (- (num-int v1)
(num-int v2)))
(error "NUMEX subtraction applied to non-number")))]
[(mult? e)
(let ([v1 (eval-under-env-c (mult-e1 e) env)]
[v2 (eval-under-env-c (mult-e2 e) env)])
(if (and (num? v1)
(num? v2))
(num (* (num-int v1)
(num-int v2)))
(error "NUMEX multiply applied to non-number")))]
[(div? e)
(let ([v1 (eval-under-env-c (div-e1 e) env)]
[v2 (eval-under-env-c (div-e2 e) env)])
(if (and (num? v1)
(num? v2))
(if (eq? (num-int v2) 0)
(error "NUMEX division applied to Zero" v2)
(num (quotient (num-int v1)
(num-int v2))))
(error "NUMEX division applied to non-number")))]
[(neg? e)
(let ([v1 (eval-under-env-c (neg-e1 e) env)])
(if (num? v1)
(num (- (num-int v1)))
(if (bool? v1)
(bool (if (bool-bit v1) #f #t))
(error "NUMEX Nagation applied to non-number or non-boolean"))
))]
[(num? e)
(let ([v1 (eval-under-env-c (num-int e) env)])
(if (integer? v1) (num v1) (error "NUMEX num applied to non-integer")))]
[(bool? e)
(let ([v1 (eval-under-env-c (bool-bit e) env)])
(if (boolean? v1) (bool v1) (error "NUMEX bool appllied to non-boolean")))]
[(closure? e) e]
[(number? e) e]
[(boolean? e) e]
[(string? e) e]
[(munit? e) e]
[#t (error (format "bad NUMEX expression: ~v" e))]))
;; Do NOT change this
(define (eval-exp-c e)
(eval-under-env-c (compute-free-vars e) null))