blob: d52ebab88e8d0654c474724eb6a103061f5aac0d (
plain) (
blame)
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
|
(define (spreadsheet)
(init-array)
(set-selection-cell-id! (make-id 1 1))
(set-screen-corner-cell-id! (make-id 1 1))
(command-loop))
(define (command-loop)
(print-screen)
(let ((command-or-formula (read)))
(if (equal? command-or-formula 'exit)
"Bye!"
(begin (process-command command-or-formula)
(command-loop)))))
(define (process-command command-or-formula)
(cond ((and (list? command-or-formula)
(command? (car command-or-formula)))
(execute-command command-or-formula))
((command? command-or-formula)
(execute-command (list command-or-formula 1)))
(else (exhibit (ss-eval (pin-down command-or-formula
(selection-cell-id)))))))
(define (execute-command command)
(apply (get-command (car command))
(cdr command)))
(define (exhibit val)
(show val)
(show "Type RETURN to redraw screen")
(read-line)
(read-line))
;;; Commands
;; Cell selection commands: F, B, N, P, and SELECT
(define (prev-row delta)
(let ((row (id-row (selection-cell-id))))
(if (< (- row delta) 1)
(error "Already at top.")
(set-selected-row! (- row delta)))))
(define (next-row delta)
(let ((row (id-row (selection-cell-id))))
(if (> (+ row delta) 30)
(error "Already at bottom.")
(set-selected-row! (+ row delta)))))
(define (prev-col delta)
(let ((col (id-column (selection-cell-id))))
(if (< (- col delta) 1)
(error "Already at left.")
(set-selected-column! (- col delta)))))
(define (next-col delta)
(let ((col (id-column (selection-cell-id))))
(if (> (+ col delta) 26)
(error "Already at right.")
(set-selected-column! (+ col delta)))))
(define (set-selected-row! new-row)
(select-id! (make-id (id-column (selection-cell-id)) new-row)))
(define (set-selected-column! new-column)
(select-id! (make-id new-column (id-row (selection-cell-id)))))
(define (select-id! id)
(set-selection-cell-id! id)
(adjust-screen-boundaries))
(define (select cell-name)
(select-id! (cell-name->id cell-name)))
(define (adjust-screen-boundaries)
(let ((row (id-row (selection-cell-id)))
(col (id-column (selection-cell-id))))
(if (< row (id-row (screen-corner-cell-id)))
(set-corner-row! row)
'do-nothing)
(if (>= row (+ (id-row (screen-corner-cell-id)) 20))
(set-corner-row! (- row 19))
'do-nothing)
(if (< col (id-column (screen-corner-cell-id)))
(set-corner-column! col)
'do-nothing)
(if (>= col (+ (id-column (screen-corner-cell-id)) 6))
(set-corner-column! (- col 5))
'do-nothing)))
(define (set-corner-row! new-row)
(set-screen-corner-cell-id!
(make-id (id-column (screen-corner-cell-id)) new-row)))
(define (set-corner-column! new-column)
(set-screen-corner-cell-id!
(make-id new-column (id-row (screen-corner-cell-id)))))
;; LOAD
(define (spreadsheet-load filename)
(let ((port (open-input-file filename)))
(sl-helper port)
(close-input-port port)))
(define (sl-helper port)
(let ((command (read port)))
(if (eof-object? command)
'done
(begin (show command)
(process-command command)
(sl-helper port)))))
;; PUT
(define (put formula . where)
(cond ((null? where)
(put-formula-in-cell formula (selection-cell-id)))
((cell-name? (car where))
(put-formula-in-cell formula (cell-name->id (car where))))
((number? (car where))
(put-all-cells-in-row formula (car where)))
((letter? (car where))
(put-all-cells-in-col formula (letter->number (car where))))
(else (error "Put it where?"))))
(define (put-all-cells-in-row formula row)
(put-all-helper formula (lambda (col) (make-id col row)) 1 26))
(define (put-all-cells-in-col formula col)
(put-all-helper formula (lambda (row) (make-id col row)) 1 30))
(define (put-all-helper formula id-maker this max)
(if (> this max)
'done
(begin (try-putting formula (id-maker this))
(put-all-helper formula id-maker (+ 1 this) max))))
(define (try-putting formula id)
(if (or (null? (cell-value id)) (null? formula))
(put-formula-in-cell formula id)
'do-nothing))
(define (put-formula-in-cell formula id)
(put-expr (pin-down formula id) id))
;;; The Association List of Commands
(define (command? name)
(assoc name *the-commands*))
(define (get-command name)
(let ((result (assoc name *the-commands*)))
(if (not result)
#f
(cadr result))))
(define *the-commands*
(list (list 'p prev-row)
(list 'n next-row)
(list 'b prev-col)
(list 'f next-col)
(list 'select select)
(list 'put put)
(list 'load spreadsheet-load)))
;;; Pinning Down Formulas Into Expressions
(define (pin-down formula id)
(cond ((cell-name? formula) (cell-name->id formula))
((word? formula) formula)
((null? formula) '())
((equal? (car formula) 'cell)
(pin-down-cell (cdr formula) id))
(else (bound-check
(map (lambda (subformula) (pin-down subformula id))
formula)))))
(define (bound-check form)
(if (member 'out-of-bounds form)
'out-of-bounds
form))
(define (pin-down-cell args reference-id)
(cond ((null? args)
(error "Bad cell specification: (cell)"))
((null? (cdr args))
(cond ((number? (car args)) ; they chose a row
(make-id (id-column reference-id) (car args)))
((letter? (car args)) ; they chose a column
(make-id (letter->number (car args))
(id-row reference-id)))
(else (error "Bad cell specification:"
(cons 'cell args)))))
(else
(let ((col (pin-down-col (car args) (id-column reference-id)))
(row (pin-down-row (cadr args) (id-row reference-id))))
(if (and (>= col 1) (<= col 26) (>= row 1) (<= row 30))
(make-id col row)
'out-of-bounds)))))
(define (pin-down-col new old)
(cond ((equal? new '*) old)
((equal? (first new) '>) (+ old (bf new)))
((equal? (first new) '<) (- old (bf new)))
((letter? new) (letter->number new))
(else (error "What column?"))))
(define (pin-down-row new old)
(cond ((number? new) new)
((equal? new '*) old)
((equal? (first new) '>) (+ old (bf new)))
((equal? (first new) '<) (- old (bf new)))
(else (error "What row?"))))
;;; Dependency Management
(define (put-expr expr-or-out-of-bounds id)
(let ((expr (if (equal? expr-or-out-of-bounds 'out-of-bounds)
'()
expr-or-out-of-bounds)))
(for-each (lambda (old-parent)
(set-cell-children!
old-parent
(remove id (cell-children old-parent))))
(cell-parents id))
(set-cell-expr! id expr)
(set-cell-parents! id (remdup (extract-ids expr)))
(for-each (lambda (new-parent)
(set-cell-children!
new-parent
(cons id (cell-children new-parent))))
(cell-parents id))
(figure id)))
(define (extract-ids expr)
(cond ((id? expr) (list expr))
((word? expr) '())
((null? expr) '())
(else (append (extract-ids (car expr))
(extract-ids (cdr expr))))))
(define (figure id)
(cond ((null? (cell-expr id)) (setvalue id '()))
((all-evaluated? (cell-parents id))
(setvalue id (ss-eval (cell-expr id))))
(else (setvalue id '()))))
(define (all-evaluated? ids)
(cond ((null? ids) #t)
((not (number? (cell-value (car ids)))) #f)
(else (all-evaluated? (cdr ids)))))
(define (setvalue id value)
(let ((old (cell-value id)))
(set-cell-value! id value)
(if (not (equal? old value))
(for-each figure (cell-children id))
'do-nothing)))
;;; Evaluating Expressions
(define (ss-eval expr)
(cond ((number? expr) expr)
((quoted? expr) (quoted-value expr))
((id? expr) (cell-value expr))
((invocation? expr)
(apply (get-function (car expr))
(map ss-eval (cdr expr))))
(else (error "Invalid expression:" expr))))
(define (quoted? expr)
(or (string? expr)
(and (list? expr) (equal? (car expr) 'quote))))
(define (quoted-value expr)
(if (string? expr)
expr
(cadr expr)))
(define (invocation? expr)
(list? expr))
(define (get-function name)
(let ((result (assoc name *the-functions*)))
(if (not result)
(error "No such function: " name)
(cadr result))))
(define *the-functions*
(list (list '* *)
(list '+ +)
(list '- -)
(list '/ /)
(list 'abs abs)
(list 'acos acos)
(list 'asin asin)
(list 'atan atan)
(list 'ceiling ceiling)
(list 'cos cos)
(list 'count count)
(list 'exp exp)
(list 'expt expt)
(list 'floor floor)
(list 'gcd gcd)
(list 'lcm lcm)
(list 'log log)
(list 'max max)
(list 'min min)
(list 'modulo modulo)
(list 'quotient quotient)
(list 'remainder remainder)
(list 'round round)
(list 'sin sin)
(list 'sqrt sqrt)
(list 'tan tan)
(list 'truncate truncate)))
;;; Printing the Screen
(define (print-screen)
(newline)
(newline)
(newline)
(show-column-labels (id-column (screen-corner-cell-id)))
(show-rows 20
(id-column (screen-corner-cell-id))
(id-row (screen-corner-cell-id)))
(display-cell-name (selection-cell-id))
(display ": ")
(show (cell-value (selection-cell-id)))
(display-expression (cell-expr (selection-cell-id)))
(newline)
(display "?? "))
(define (display-cell-name id)
(display (number->letter (id-column id)))
(display (id-row id)))
(define (show-column-labels col-number)
(display " ")
(show-label 6 col-number)
(newline))
(define (show-label to-go this-col-number)
(cond ((= to-go 0) '())
(else
(display " -----")
(display (number->letter this-col-number))
(display "----")
(show-label (- to-go 1) (+ 1 this-col-number)))))
(define (show-rows to-go col row)
(cond ((= to-go 0) 'done)
(else
(display (align row 2 0))
(display " ")
(show-row 6 col row)
(newline)
(show-rows (- to-go 1) col (+ row 1)))))
(define (show-row to-go col row)
(cond ((= to-go 0) 'done)
(else
(display (if (selected-indices? col row) ">" " "))
(display-value (cell-value-from-indices col row))
(display (if (selected-indices? col row) "<" " "))
(show-row (- to-go 1) (+ 1 col) row))))
(define (selected-indices? col row)
(and (= col (id-column (selection-cell-id)))
(= row (id-row (selection-cell-id)))))
(define (display-value val)
(display (align (if (null? val) "" val) 10 2)))
(define (display-expression expr)
(cond ((null? expr) (display '()))
((quoted? expr) (display (quoted-value expr)))
((word? expr) (display expr))
((id? expr)
(display-cell-name expr))
(else (display-invocation expr))))
(define (display-invocation expr)
(display "(")
(display-expression (car expr))
(for-each (lambda (subexpr)
(display " ")
(display-expression subexpr))
(cdr expr))
(display ")"))
;;; Abstract Data Types
;; Special cells: the selected cell and the screen corner
(define *special-cells* (make-vector 2))
(define (selection-cell-id)
(vector-ref *special-cells* 0))
(define (set-selection-cell-id! new-id)
(vector-set! *special-cells* 0 new-id))
(define (screen-corner-cell-id)
(vector-ref *special-cells* 1))
(define (set-screen-corner-cell-id! new-id)
(vector-set! *special-cells* 1 new-id))
;; Cell names
(define (cell-name? expr)
(and (word? expr)
(letter? (first expr))
(number? (bf expr))))
(define (cell-name-column cell-name)
(letter->number (first cell-name)))
(define (cell-name-row cell-name)
(bf cell-name))
(define (cell-name->id cell-name)
(make-id (cell-name-column cell-name)
(cell-name-row cell-name)))
;; Cell IDs
(define (make-id col row)
(list 'id col row))
(define (id-column id)
(cadr id))
(define (id-row id)
(caddr id))
(define (id? x)
(and (list? x)
(not (null? x))
(equal? 'id (car x))))
;; Cells
(define (make-cell)
(vector '() '() '() '()))
(define (cell-value id)
(vector-ref (cell-structure id) 0))
(define (cell-value-from-indices col row)
(vector-ref (cell-structure-from-indices col row) 0))
(define (cell-expr id)
(vector-ref (cell-structure id) 1))
(define (cell-parents id)
(vector-ref (cell-structure id) 2))
(define (cell-children id)
(vector-ref (cell-structure id) 3))
(define (set-cell-value! id val)
(vector-set! (cell-structure id) 0 val))
(define (set-cell-expr! id val)
(vector-set! (cell-structure id) 1 val))
(define (set-cell-parents! id val)
(vector-set! (cell-structure id) 2 val))
(define (set-cell-children! id val)
(vector-set! (cell-structure id) 3 val))
(define (cell-structure id)
(global-array-lookup (id-column id)
(id-row id)))
(define (cell-structure-from-indices col row)
(global-array-lookup col row))
(define *the-spreadsheet-array* (make-vector 30))
(define (global-array-lookup col row)
(if (and (<= row 30) (<= col 26))
(vector-ref (vector-ref *the-spreadsheet-array* (- row 1))
(- col 1))
(error "Out of bounds")))
(define (init-array)
(fill-array-with-rows 29))
(define (fill-array-with-rows n)
(if (< n 0)
'done
(begin (vector-set! *the-spreadsheet-array* n (make-vector 26))
(fill-row-with-cells
(vector-ref *the-spreadsheet-array* n) 25)
(fill-array-with-rows (- n 1)))))
(define (fill-row-with-cells vec n)
(if (< n 0)
'done
(begin (vector-set! vec n (make-cell))
(fill-row-with-cells vec (- n 1)))))
;;; Utility Functions
(define alphabet
'#(a b c d e f g h i j k l m n o p q r s t u v w x y z))
(define (letter? something)
(and (word? something)
(= 1 (count something))
(vector-member something alphabet)))
(define (number->letter num)
(vector-ref alphabet (- num 1)))
(define (letter->number letter)
(+ (vector-member letter alphabet) 1))
(define (vector-member thing vector)
(vector-member-helper thing vector 0))
(define (vector-member-helper thing vector index)
(cond ((= index (vector-length vector)) #f)
((equal? thing (vector-ref vector index)) index)
(else (vector-member-helper thing vector (+ 1 index)))))
(define (remdup lst)
(cond ((null? lst) '())
((member (car lst) (cdr lst))
(remdup (cdr lst)))
(else (cons (car lst) (remdup (cdr lst))))))
(define (remove bad-item lst)
(filter (lambda (item) (not (equal? item bad-item)))
lst))
|