From ed8b20c824248f09840de30e1d0149d5cd938310 Mon Sep 17 00:00:00 2001 From: Rahul Arya Date: Fri, 5 Apr 2019 23:47:53 -0700 Subject: [PATCH] Implemented java-style autoformatting. (#133) * Implemented java-style autoformatting. * special case #[symbol]. This is a hack and needs to be fixed * make sure to pass javastyle in debug as well as in reformat * fix wording * Made cache work properly with javastyle. --- all_tests.scm | 1605 +++++++++++------ editor/format_parser.py | 6 +- editor/formatter.py | 23 +- editor/local_server.py | 7 +- editor/static/index.html | 6 + editor/static/scripts/editor.js | 2 + editor/static/scripts/settings.js | 10 +- .../scripts/substitution_tree_worker.js | 7 +- 8 files changed, 1139 insertions(+), 527 deletions(-) diff --git a/all_tests.scm b/all_tests.scm index 3d7ba70f..36d55c57 100644 --- a/all_tests.scm +++ b/all_tests.scm @@ -2,7 +2,7 @@ display ; expect #[display] -hello ; expect SchemeError +; hello ; expect SchemeError 2 ; expect 2 @@ -25,49 +25,61 @@ hello ; expect SchemeError (+ 2 3) ; expect 5 -(* (+ 3 2) (+ 1 7)) ; expect 40 +; expect 40 +(* (+ 3 2) (+ 1 7)) (1 2) ; expect SchemeError -(1 (print 0)) ; expect SchemeError +; expect SchemeError +(1 (print 0)) (+) ; expect 0 (odd? 13) ; expect #t -(car (list 1 2 3 4)) ; expect 1 +; expect 1 +(car (list 1 2 3 4)) (car car) ; expect SchemeError -(odd? 1 2 3) ; expect SchemeError +; expect SchemeError +(odd? 1 2 3) ; expect 25 (+ (+ 1) (* 2 3) (+ 5) - (+ 6 (+ 7))) + (+ 6 (+ 7)) +) (*) ; expect 1 (-) ; expect SchemeError ; expect 3 -(car (cdr (cdr (list 1 2 3 4)))) +(car (cdr (cdr (list 1 2 3 4))) +) ; expect SchemeError (car cdr (list 1)) ; expect 6 -(* (car (cdr (cdr (list 1 2 3 4)))) - (car (cdr (list 1 2 3 4)))) +(* (car (cdr (cdr (list 1 2 3 4))) + ) + (car (cdr (list 1 2 3 4))) +) ; expect SchemeError -(* (car (cdr (cdr (list 1 2 3 4)))) - (cdr (cdr (list 1 2 3 4)))) +(* (car (cdr (cdr (list 1 2 3 4))) + ) + (cdr (cdr (list 1 2 3 4))) +) -(+ (/ 1 0)) ; expect SchemeError +; expect SchemeError +(+ (/ 1 0)) -((/ 1 0) (print 5)) ; expect SchemeError +; expect SchemeError +((/ 1 0) (print 5)) (null? (print 5)) ; expect #f @@ -86,25 +98,31 @@ x ; expect 9 ; expect 6.28 (eval (define tau 6.28)) -(define pi 3.14159) ; expect pi +; expect pi +(define pi 3.14159) -(define radius 10) ; expect radius +; expect radius +(define radius 10) ; expect area (define area - (* pi (* radius radius))) + (* pi (* radius radius)) +) area ; expect 314.159 -(define radius 100) ; expect radius +; expect radius +(define radius 100) radius ; expect 100 area ; expect 314.159 -(define 0 1) ; expect SchemeError +; expect SchemeError +(define 0 1) -(define error (/ 1 0)) ; expect SchemeError +; expect SchemeError +(define error (/ 1 0)) (quote hello) ; expect hello @@ -114,27 +132,35 @@ area ; expect 314.159 (quote (1 2)) ; expect (1 2) -(quote (1 . 2)) ; expect (1 . 2) +; expect (1 . 2) +(quote (1 . 2)) (quote (1 2)) ; expect (1 2) -(car (quote (1 2 3))) ; expect 1 +; expect 1 +(car (quote (1 2 3))) -(cdr (quote (1 2))) ; expect (2) +; expect (2) +(cdr (quote (1 2))) -(car (car (quote ((1))))) ; expect 1 +; expect 1 +(car (car (quote ((1))))) (quote 3) ; expect 3 ; expect 4 -(eval - (cons (quote car) (quote ((quote (4 2)))))) +(eval (cons (quote car) + (quote ((quote (4 2)))) + ) +) -(quasiquote (1 2 3)) ; expect (1 2 3) +; expect (1 2 3) +(quasiquote (1 2 3)) (define a 2) ; expect a -(quasiquote (unquote a)) ; expect 2 +; expect 2 +(quasiquote (unquote a)) ; expect (a b c) (quasiquote (a b c)) @@ -143,26 +169,34 @@ area ; expect 314.159 (quasiquote ((unquote a) b c)) ; expect SchemeError -(quasiquote - ((unquote a) (unquote b) c)) +(quasiquote ((unquote a) (unquote b) c) +) ; expect SchemeError -(quasiquote ((unquote - (+ (unquote a) (unquote a))) - b)) +(quasiquote ((unquote (+ (unquote a) (unquote a)) + ) + b + ) +) ; expect (quasiquote (1 2 (+ 3 4))) -(quasiquote (quasiquote (1 2 (+ 3 4)))) +(quasiquote (quasiquote (1 2 (+ 3 4))) +) ; expect (1 (2 b 3)) -(quasiquote - (1 (unquote - (cons a - (quasiquote (b (unquote (+ 1 2)))))))) +(quasiquote (1 (unquote (cons a + (quasiquote (b (unquote (+ 1 2))) + ) + ) + ) + ) +) -(begin (+ 2 3) (+ 5 6)) ; expect 11 +; expect 11 +(begin (+ 2 3) (+ 5 6)) -(begin (define x 3) x) ; expect 3 +; expect 3 +(begin (define x 3) x) ; expect (+ 2 2) (begin 30 (quote (+ 2 2))) @@ -179,11 +213,13 @@ x ; expect 1 ; expect (3 y z) (begin (define x 3) - (cons x (quote (y z)))) + (cons x (quote (y z))) +) ; expect (3 x z) (begin (define x 3) - (cons x (quote (x z)))) + (cons x (quote (x z))) +) (define x 0) ; expect x @@ -191,24 +227,30 @@ x ; expect 1 (begin (define x (+ x 1)) (define x (+ x 10)) (define x (+ x 100)) - (define x (+ x 1000))) + (define x (+ x 1000)) +) x ; expect 1111 ; expect (lambda (x y) (+ x y)) -(lambda (x y) (+ x y)) +(lambda (x y) + (+ x y) +) ; expect (lambda (x) (+ x) (+ x x)) -(lambda - (x) - (+ x) - (+ x x)) +(lambda (x) + (+ x) + (+ x x) +) -(lambda (x)) ; expect SchemeError +; expect SchemeError +(lambda (x)) -(lambda nil 2) ; expect (lambda () 2) +; expect (lambda () 2) +(lambda nil 2) -(define (f) (+ 2 2)) ; expect f +; expect f +(define (f) (+ 2 2)) f ; expect (lambda () (+ 2 2)) @@ -217,11 +259,13 @@ f ; expect (lambda () (+ 2 2)) f ; expect (lambda (x) (* x x)) -(define (foo x) 1 2 3 4 5) ; expect foo +; expect foo +(define (foo x) 1 2 3 4 5) foo ; expect (lambda (x) 1 2 3 4 5) -(define (foo) (/ 1 0)) ; expect foo +; expect foo +(define (foo) (/ 1 0)) foo ; expect (lambda () (/ 1 0)) @@ -235,59 +279,80 @@ square ; expect (lambda (x) (* x x)) square ; expect (lambda (x) (* x x)) ; expect square -(define square (lambda (x) (* x x))) +(define square + (lambda (x) (* x x)) +) -(square (square 21)) ; expect 194481 +; expect 194481 +(square (square 21)) ; expect ((lambda (x) (list x (list (quote quote) x))) (quote (lambda (x) (list x (list (quote quote) x))))) ((lambda (x) - (list x - (list (quote quote) x))) + (list x + (list (quote quote) x) + ) + ) (quote (lambda (x) - (list x - (list (quote quote) x))))) + (list x + (list (quote quote) x) + ) + ) + ) +) ; expect outer (define (outer x y) (define (inner z x) - (+ x - (* y 2) - (* z 3))) - (inner x 10)) + (+ x (* y 2) (* z 3)) + ) + (inner x 10) +) (outer 1 2) ; expect 17 ; expect outer-func (define (outer-func x y) (define (inner z x) - (+ x - (* y 2) - (* z 3))) - inner) + (+ x (* y 2) (* z 3)) + ) + inner +) -((outer-func 1 2) 1 10) ; expect 17 +; expect 17 +((outer-func 1 2) 1 10) ; expect square -(define square (lambda (x) (* x x))) +(define square + (lambda (x) (* x x)) +) ; expect sum-of-squares (define (sum-of-squares x y) - (+ (square x) (square y))) + (+ (square x) (square y)) +) -(sum-of-squares 3 4) ; expect 25 +; expect 25 +(sum-of-squares 3 4) ; expect double -(define double (lambda (x) (* 2 x))) +(define double + (lambda (x) (* 2 x)) +) ; expect compose (define compose (lambda (f g) - (lambda (x) (f (g x))))) + (lambda (x) (f (g x))) + ) +) ; expect apply-twice -(define apply-twice (lambda (f) (compose f f))) +(define apply-twice + (lambda (f) (compose f f)) +) -((apply-twice double) 5) ; expect 20 +; expect 20 +((apply-twice double) 5) (and) ; expect #t @@ -313,7 +378,8 @@ square ; expect (lambda (x) (* x x)) (and 0 1 2 3) ; expect 3 -(define (true-fn) #t) ; expect true-fn +; expect true-fn +(define (true-fn) #t) (and (true-fn)) ; expect #t @@ -328,7 +394,8 @@ square ; expect (lambda (x) (* x x)) (and (define x (+ x 1)) (define x (+ x 10)) (define x (+ x 100)) - (define x (+ x 1000))) + (define x (+ x 1000)) +) x ; expect 1111 @@ -339,13 +406,15 @@ x ; expect 1111 (define x (+ x 10)) #f (define x (+ x 100)) - (define x (+ x 1000))) + (define x (+ x 1000)) +) x ; expect 11 ; expect no-mutation (define (no-mutation) - (and #t #t #t #t)) + (and #t #t #t #t) +) no-mutation ; expect (lambda () (and #t #t #t #t)) @@ -376,11 +445,16 @@ no-mutation ; expect (lambda () (and #t #t #t #t)) (or (quote a) #f) ; expect #t -(or (< 2 3) (> 2 3) 2 (quote a)) +(or (< 2 3) + (> 2 3) + 2 + (quote a) +) (or (< 2 3) 2) ; expect #t -(define (false-fn) #f) ; expect false-fn +; expect false-fn +(define (false-fn) #f) ; expect yay (or (false-fn) (quote yay)) @@ -388,29 +462,38 @@ no-mutation ; expect (lambda () (and #t #t #t #t)) (define x 0) ; expect x ; expect #f -(or - (begin (define x (+ x 1)) #f) - (begin (define x (+ x 10)) #f) - (begin (define x (+ x 100)) #f) - (begin (define x (+ x 1000)) #f)) +(or (begin (define x (+ x 1)) #f) + (begin (define x (+ x 10)) #f) + (begin (define x (+ x 100)) + #f + ) + (begin (define x (+ x 1000)) + #f + ) +) x ; expect 1111 (define x 0) ; expect x ; expect #t -(or - (begin (define x (+ x 1)) #f) - (begin (define x (+ x 10)) #f) - #t - (begin (define x (+ x 100)) #f) - (begin (define x (+ x 1000)) #f)) +(or (begin (define x (+ x 1)) #f) + (begin (define x (+ x 10)) #f) + #t + (begin (define x (+ x 100)) + #f + ) + (begin (define x (+ x 1000)) + #f + ) +) x ; expect 11 ; expect no-mutation (define (no-mutation) - (or #f #f #f #f)) + (or #f #f #f #f) +) no-mutation ; expect (lambda () (or #f #f #f #f)) @@ -420,11 +503,16 @@ no-mutation ; expect (lambda () (or #f #f #f #f)) ; expect greater-than-5 (define (greater-than-5 x) - (if (> x 5) #t #f)) + (if (> x 5) + #t + #f + ) +) ; expect other (define (other y) - (or (greater-than-5 y) #f)) + (or (greater-than-5 y) #f) +) (other 2) ; expect #f @@ -432,7 +520,8 @@ no-mutation ; expect (lambda () (or #f #f #f #f)) ; expect other (define (other y) - (and (greater-than-5 y) #t)) + (and (greater-than-5 y) #t) +) (other 2) ; expect #f @@ -443,33 +532,51 @@ no-mutation ; expect (lambda () (or #f #f #f #f)) ((> 2 3) 5) ((> 2 4) 6) ((< 2 5) 7) - (else 8)) + (else 8) +) ; expect 8 (cond ((> 2 3) 5) ((> 2 4) 6) - (else 8)) - -(if 0 1 2) ; expect 1 - -(if #f 1 (if #t 2 3)) ; expect 2 + (else 8) +) + +; expect 1 +(if 0 + 1 + 2 +) + +; expect 2 +(if #f + 1 + (if #t + 2 + 3 + ) +) ; expect a -(if (= 1 2) (/ 1 0) (quote a)) +(if (= 1 2) + (/ 1 0) + (quote a) +) ; expect 7 (cond ((> 2 3) 5) ((> 2 4) 6) - ((< 2 5) 7)) + ((< 2 5) 7) +) (cond ((> 2 3) (display (quote oops)) (newline)) (else - 9)) + 9) +) (cond ((< 2 1) @@ -477,17 +584,20 @@ no-mutation ; expect (lambda () (or #f #f #f #f)) ((> 3 2) ) (else - 5)) + 5) +) ; expect (cond - (#f 1)) + (#f 1) +) ; expect hi (cond ((= 4 3) (quote nope)) ((= 4 4) (quote hi)) - (else (quote wat))) + (else (quote wat)) +) (cond ((= 4 3) @@ -495,36 +605,43 @@ no-mutation ; expect (lambda () (or #f #f #f #f)) ((= 4 4) ) (else - (quote hm))) + (quote hm)) +) (cond ((= 4 4) (+ 40 2)) (else (quote wat) - 0)) + 0) +) ; expect 12 (cond (12 - )) + ) +) ; expect hi (cond ((= 4 3) ) ((quote hi) - )) + ) +) ; expect (eval (cond (#f 1) - (#f 2))) + (#f 2) + ) +) ; expect yea (cond (0 (quote yea)) - (else (quote nay))) + (else (quote nay)) +) (define x 0) ; expect x @@ -540,57 +657,81 @@ no-mutation ; expect (lambda () (or #f #f #f #f)) (else (define x (- x 5)) (define y (- y 5)) - (define z (- z 5)))) + (define z (- z 5))) +) ; expect (1 1 1) (list x y z) ; expect print-and-false -(define (print-and-false val) (print val) #f) +(define (print-and-false val) + (print val) + #f +) ; expect (cond - ((print-and-false (quote cond1)) + ((print-and-false (quote cond1) + ) + ) + ((print-and-false (quote cond2) + ) + ) + ((print-and-false (quote cond3) ) - ((print-and-false (quote cond2)) ) - ((print-and-false (quote cond3)) + ((print-and-false (quote cond4) ) - ((print-and-false (quote cond4)) - )) + ) +) ; expect print-and-true -(define (print-and-true val) (print val) #t) +(define (print-and-true val) + (print val) + #t +) ; expect #t (cond - ((print-and-false (quote cond1)) + ((print-and-false (quote cond1) + ) + ) + ((print-and-false (quote cond2) ) - ((print-and-false (quote cond2)) ) ((print-and-true (quote cond3)) ) - ((print-and-false (quote cond4)) - )) + ((print-and-false (quote cond4) + ) + ) +) (define x 1) ; expect x ; expect 8 -(let ((x 5)) - (+ x 3)) +(let ((x 5) + ) + (+ x 3) +) x ; expect 1 ; expect SchemeError (let ((a 1) - (b a)) - b) + (b a) + ) + b +) ; expect 9 -(let ((x 5)) +(let ((x 5) + ) (let ((x 2) - (y x)) - (+ y (* x 2)))) + (y x) + ) + (+ y (* x 2)) + ) +) ; expect square (define (square x) (* x x)) @@ -598,10 +739,14 @@ x ; expect 1 ; expect f (define (f x y) (let ((a (+ 1 (* x y))) - (b (- 1 y))) + (b (- 1 y)) + ) (+ (* x (square a)) (* y b) - (* a b)))) + (* a b) + ) + ) +) (f 3 4) ; expect 456 @@ -611,39 +756,52 @@ x ; expect 1 ; expect (6 . 5) (let ((x (+ y 2)) - (y (+ x 2))) - (cons x y)) + (y (+ x 2)) + ) + (cons x y) +) ; expect hello -(let ((x (quote hello))) - x) +(let ((x (quote hello)) + ) + x +) ; expect 6 (let ((a 1) (b 2) - (c 3)) - (+ a b c)) + (c 3) + ) + (+ a b c) +) (define z 0) ; expect z ; expect 1 -(let ((a (define z (+ z 1)))) - z) +(let ((a (define z (+ z 1))) + ) + z +) ; expect (2 . 3) (let ((x 1) - (y 3)) + (y 3) + ) (define x (+ x 1)) - (cons x y)) + (cons x y) +) ; expect SchemeError (let ((a 1 1)) - a) + a +) ; expect SchemeError (let ((a 1) - (2 2)) - a) + (2 2) + ) + a +) (define y 1) ; expect y @@ -652,8 +810,8 @@ x ; expect 1 ; expect g (define g - (lambda (x y) - (f (+ x x)))) + (lambda (x y) (f (+ x x))) +) (g 3 7) ; expect 13 @@ -667,11 +825,13 @@ x ; expect 1 ; expect f (define (f x) - (mu nil (lambda (y) (+ x y)))) + (mu nil (lambda (y) (+ x y))) +) ; expect g (define (g x) - (((f (+ x 1))) (+ x 2))) + (((f (+ x 1))) (+ x 2)) +) (g 3) ; expect 8 @@ -681,34 +841,43 @@ x ; expect 1 (load (quote questions)) ; expect ((0 3) (1 4) (2 5) (3 6)) -(enumerate (quote (3 4 5 6))) +(enumerate (quote (3 4 5 6)) +) ; expect ((0 9) (1 8) (2 7) (3 6) (4 5) (5 4)) -(enumerate (quote (9 8 7 6 5 4))) +(enumerate (quote (9 8 7 6 5 4)) +) ; expect None (load (quote questions)) ; expect ((0 a) (1 b) (2 c) (3 d)) -(enumerate - (quote (a b c d))) +(enumerate (quote (a b c d)) +) -(enumerate (quote nil)) ; expect () +; expect () +(enumerate (quote nil)) ; expect None (load (quote questions)) ; expect ((10) (5 5) (5 1 1 1 1 1) (1 1 1 1 1 1 1 1 1 1)) -(list-change 10 (quote (25 10 5 1))) +(list-change 10 + (quote (25 10 5 1)) +) ; expect ((4 1) (3 2) (3 1 1) (2 2 1) (2 1 1 1) (1 1 1 1 1)) -(list-change 5 (quote (4 3 2 1))) +(list-change 5 + (quote (4 3 2 1)) +) ; expect None (load (quote questions)) ; expect ((5 2) (5 1 1) (4 3) (4 2 1) (4 1 1 1) (3 3 1) (3 2 2) (3 2 1 1) (3 1 1 1 1) (2 2 2 1) (2 2 1 1 1) (2 1 1 1 1 1) (1 1 1 1 1 1 1)) -(list-change 7 (quote (5 4 3 2 1))) +(list-change 7 + (quote (5 4 3 2 1)) +) ; expect None (load (quote questions)) @@ -719,114 +888,174 @@ x ; expect 1 (let-to-lambda (quote a)) ; expect (+ 1 2) -(let-to-lambda (quote (+ 1 2))) +(let-to-lambda (quote (+ 1 2)) +) ; expect ((lambda (a b) (+ a b)) 1 2) -(let-to-lambda - (quote (let ((a 1) - (b 2)) - (+ a b)))) +(let-to-lambda (quote (let ((a 1) + (b 2) + ) + (+ a b) + ) + ) +) ; expect None (load (quote questions)) ; expect (quoted expressions remain the same) -(quote - (quoted expressions remain the same)) +(quote (quoted expressions + remain + the + same + ) +) ; expect (quote (let ((a 1) (b 2)) (+ a b))) -(let-to-lambda - (quote (quote (let ((a 1) - (b 2)) - (+ a b))))) +(let-to-lambda (quote (quote (let ((a 1) + (b 2) + ) + (+ a b) + ) + ) + ) +) ; expect None (load (quote questions)) ; expect (lambda parameters not affected but body affected) -(quote - (lambda parameters +(quote (lambda parameters not affected but body - affected)) + affected + ) +) ; expect (lambda (let a b) (+ let a b)) -(let-to-lambda - (quote (lambda (let a - b) - (+ let - a - b)))) +(let-to-lambda (quote (lambda (let a + b + ) + (+ let + a + b + ) + ) + ) +) ; expect (lambda (x) a ((lambda (a) a) x)) -(let-to-lambda - (quote (lambda (x) - a - (let ((a x)) - a)))) +(let-to-lambda (quote (lambda (x) + a + (let ((a x) + ) + a + ) + ) + ) +) ; expect None (load (quote questions)) ; expect ((lambda (a b) (+ a b)) ((lambda (a) a) 2) 2) -(let-to-lambda - (quote (let ((a (let ((a 2)) - a)) - (b 2)) - (+ a b)))) +(let-to-lambda (quote (let ((a (let ((a 2) + ) + a + ) + ) + (b 2) + ) + (+ a b) + ) + ) +) ; expect ((lambda (a) ((lambda (b) b) a)) 1) -(let-to-lambda - (quote (let ((a 1)) - (let ((b a)) - b)))) +(let-to-lambda (quote (let ((a 1) + ) + (let ((b a) + ) + b + ) + ) + ) +) ; expect (+ 1 ((lambda (a) a) 1)) (let-to-lambda (quote (+ 1 - (let ((a 1)) - a)))) + (let ((a 1) + ) + a + ) + ) + ) +) ; expect map (define (map f lst) (if (null? lst) nil (cons (f (car lst)) - (map f (cdr lst))))) + (map f (cdr lst)) + ) + ) +) ; expect for (define-macro (for formal iterable body) (list (quote map) - (list (quote lambda) (list formal) body) - iterable)) + (list (quote lambda) + (list formal) + body + ) + iterable + ) +) ; expect (0 2 3) (for i (quote (1 2 3)) - (if (= i 1) 0 i)) + (if (= i 1) + 0 + i + ) +) ; expect map (define (map f lst) (if (null? lst) nil (cons (f (car lst)) - (map f (cdr lst))))) + (map f (cdr lst)) + ) + ) +) ; expect cadr -(define (cadr s) (car (cdr s))) +(define (cadr s) + (car (cdr s)) +) ; expect cars (define (cars s) (map car s)) ; expect cadrs -(define (cadrs s) (map cadr s)) +(define (cadrs s) + (map cadr s) +) ; expect leet (define-macro (leet bindings expr) - (cons - (list (quote lambda) (cars bindings) expr) - (cadrs bindings))) + (cons (list (quote lambda) + (cars bindings) + expr + ) + (cadrs bindings) + ) +) ; expect square (define (square x) (* x x)) @@ -834,8 +1063,11 @@ x ; expect 1 ; expect hyp (define (hyp a b) (leet ((a2 (square a)) - (b2 (square b))) - (sqrt (+ a2 b2)))) + (b2 (square b)) + ) + (sqrt (+ a2 b2)) + ) +) (hyp 3 4) ; expect 5.0 @@ -844,27 +1076,37 @@ x ; expect 1 (if (null? lst) nil (cons (f (car lst)) - (map f (cdr lst))))) + (map f (cdr lst)) + ) + ) +) -(define-macro wat?) ; expect SchemeError +; expect SchemeError +(define-macro wat?) -(define-macro woah okay) ; expect SchemeError +; expect SchemeError +(define-macro woah okay) -(define-macro (hello world)) ; expect SchemeError +; expect SchemeError +(define-macro (hello world)) -(define-macro (5) (cons 1 2)) ; expect SchemeError +; expect SchemeError +(define-macro (5) (cons 1 2)) -(define-macro (name) (body)) ; expect name +; expect name +(define-macro (name) (body)) name ; expect (lambda () (body)) (name) ; expect SchemeError -(quote (1 2 3 4 5 . 6)) ; expect (1 2 3 4 5 . 6) +; expect (1 2 3 4 5 . 6) +(quote (1 2 3 4 5 . 6)) (quote (1 2)) ; expect (1 2) -(quote (1 . 2)) ; expect (1 . 2) +; expect (1 . 2) +(quote (1 . 2)) + ; expect #[+] @@ -897,7 +1139,8 @@ begin ; expect SchemeError ; expect (if (quote #f) (print "err") - (print "success")) + (print "success") +) (= (sqrt 9) 3) ; expect #t @@ -926,9 +1169,11 @@ nil ; expect () (floor 2.5) ; expect 2 -(define cat dog) ; expect SchemeError +; expect SchemeError +(define cat dog) -(define cat cat) ; expect SchemeError +; expect SchemeError +(define cat cat) ; expect SchemeError (define a b c) @@ -938,7 +1183,8 @@ nil ; expect () (a) ; expect SchemeError -(define a) ; expect SchemeError +; expect SchemeError +(define a) (define) ; expect SchemeError @@ -947,18 +1193,21 @@ define ; expect SchemeError ; expect scope (define (scope) (define define 5) - (+ define define)) + (+ define define) +) (scope) ; expect 10 ; expect scope (define (scope) (define (unquote cat) 5) - (unquote 6)) + (unquote 6) +) (scope) ; expect 5 -(unquote cat) ; expect SchemeError +; expect SchemeError +(unquote cat) ; expect cat (define cat (quote cat)) @@ -976,29 +1225,34 @@ define ; expect SchemeError (define dog 5) ; expect dog ; expect (quote dog) -(apply id (quote ((quote dog)))) +(apply id + (quote ((quote dog))) +) ; expect dog (apply id (quote (dog))) ; expect (quote (dog)) (apply eval - (quote ((quote (quote (dog)))))) + (quote ((quote (quote (dog)))) + ) +) ; expect (dog) (apply eval - (list (quote (quote (dog))))) + (list (quote (quote (dog)))) +) ; expect (quote dog) (apply eval - (quote ((quote (quote dog))))) + (quote ((quote (quote dog)))) +) ; expect 5 (apply eval (quote (dog))) ; expect x -(define (x a b c) - (+ a b c)) +(define (x a b c) (+ a b c)) (define y +) ; expect y @@ -1014,7 +1268,11 @@ define ; expect SchemeError (eq? y z) ; expect 9 -((begin (print "cat") +) 2 3 4) +((begin (print "cat") +) + 2 + 3 + 4 +) ; expect SchemeError ((print "1") (print "2")) @@ -1026,9 +1284,11 @@ define ; expect SchemeError x ; expect 2 -(define 2 x) ; expect SchemeError +; expect SchemeError +(define 2 x) -(define #t #f) ; expect SchemeError +; expect SchemeError +(define #t #f) (define cat 5) ; expect cat @@ -1036,13 +1296,15 @@ cat ; expect 5 cat ; expect 5 -(quote (car 5)) ; expect (car 5) +; expect (car 5) +(quote (car 5)) ; expect (print "cat") (quote (print "cat")) ; expect (quote (print (quote cat))) -(quote (quote (print (quote cat)))) +(quote (quote (print (quote cat))) +) ; expect (eval (quote (print "cat"))) @@ -1050,24 +1312,36 @@ cat ; expect 5 ; expect SchemeError (eval (quote ((print "cat")))) -(quasiquote (+ 1 2 3 4)) ; expect (+ 1 2 3 4) +; expect (+ 1 2 3 4) +(quasiquote (+ 1 2 3 4)) ; expect (1 2 3 9) -(quasiquote (1 2 3 (unquote (+ 4 5)))) +(quasiquote (1 2 3 (unquote (+ 4 5))) +) ; expect (1 2 3 . 9) -(quasiquote (1 2 3 unquote (+ 4 5))) +(quasiquote (1 2 3 unquote (+ 4 5)) +) ; expect SchemeError -(quasiquote (1 2 3 unquote + 4 5)) +(quasiquote (1 2 3 unquote + 4 5) +) ; expect (1 2 3 (quasiquote (4 5 (unquote (+ 6 7 8))))) -(quasiquote - (1 2 3 (quasiquote (4 5 (unquote (+ 6 7 8)))))) +(quasiquote (1 2 + 3 + (quasiquote (4 5 (unquote (+ 6 7 8))) + ) + ) +) ; expect (1 2 3 (quasiquote (4 5 unquote + 6 7 8))) -(quasiquote - (1 2 3 (quasiquote (4 5 unquote + 6 7 8)))) +(quasiquote (1 2 + 3 + (quasiquote (4 5 unquote + 6 7 8) + ) + ) +) (begin) ; expect SchemeError @@ -1077,31 +1351,30 @@ cat ; expect 5 (define x 6) ; expect x ; expect 12 -((lambda (y) - (print y) - (* x y)) - 2) +((lambda (y) (print y) (* x y)) + 2 +) (define x 7) ; expect x ; expect 14 -((lambda (y) - (print y) - (* x y)) - 2) +((lambda (y) (print y) (* x y)) + 2 +) -(lambda (y)) ; expect SchemeError +; expect SchemeError +(lambda (y)) ; expect f -(define (f x) - (+ a b c)) +(define (f x) (+ a b c)) f ; expect (lambda (x) (+ a b c)) ; expect f (define (f x) (define (g y) (+ x y)) - g) + g +) g ; expect SchemeError @@ -1122,37 +1395,48 @@ g ; expect SchemeError (or #f #f) ; expect -(and (print 5) (print 6) (print 7)) +(and (print 5) + (print 6) + (print 7) +) ; expect -(or (print 5) (print 6) (print 7)) +(or (print 5) + (print 6) + (print 7) +) ; expect 11 (cond ((+ 5 6) - )) + ) +) ; expect #t (cond (else - )) + ) +) (cond (else 5 6 - 7)) + 7) +) ; expect SchemeError (cond ((else 5 6 7) - )) + ) +) ; expect (cond (#t (print "5")) (#t (print "6")) - (else (print "7"))) + (else (print "7")) +) ; expect (cond @@ -1161,21 +1445,26 @@ g ; expect SchemeError ((print "5") ) ((print "6") - )) + ) +) ; expect 6 (let ((cat 1) (dog 2) - (elephant 3)) - (+ cat dog elephant)) + (elephant 3) + ) + (+ cat dog elephant) +) elephant ; expect SchemeError (define cat 1) ; expect cat ; expect 3 -(let ((cat 2)) - (+ 1 cat)) +(let ((cat 2) + ) + (+ 1 cat) +) cat ; expect 1 @@ -1183,18 +1472,24 @@ cat ; expect 1 ; expect 2 (let ((cat 2) - (dog (+ 1 cat))) - dog) + (dog (+ 1 cat)) + ) + dog +) ; expect f (define f (mu (x) - (print x) - (if (> x 0) - (begin y - (define y x) - (f (- x 1))) - 1))) + (print x) + (if (> x 0) + (begin y + (define y x) + (f (- x 1)) + ) + 1 + ) + ) +) (define y 5) ; expect y @@ -1204,20 +1499,26 @@ cat ; expect 1 (define (tailcall x) (if (> x 0) (tailcall (- x 1)) - (print "wow"))) + (print "wow") + ) +) ; (tailcall 10000) ; expect ; expect dotwice (define-macro (dotwice expr) - (list (quote begin) expr expr)) + (list (quote begin) expr expr) +) (dotwice (print 5)) ; expect ; expect alwaysprint5 -(define-macro (alwaysprint5 expr) (list print 5)) +(define-macro (alwaysprint5 expr) + (list print 5) +) -(alwaysprint5 (print (/ 1 0))) ; expect +; expect +(alwaysprint5 (print (/ 1 0))) 10 ; expect 10 @@ -1235,15 +1536,18 @@ cat ; expect 1 (* 25 4 12) ; expect 1200 -(+ (* 3 5) (- 10 6)) ; expect 19 +; expect 19 +(+ (* 3 5) (- 10 6)) ; expect 57 (+ (* 3 (+ (* 2 4) (+ 3 5))) - (+ (- 10 7) 6)) + (+ (- 10 7) 6) +) ; expect 57 (+ (* 3 (+ (* 2 4) (+ 3 5))) - (+ (- 10 7) 6)) + (+ (- 10 7) 6) +) (define size 2) ; expect size @@ -1251,15 +1555,19 @@ size ; expect 2 (* 5 size) ; expect 10 -(define pi 3.14159) ; expect pi +; expect pi +(define pi 3.14159) -(define radius 10) ; expect radius +; expect radius +(define radius 10) ; expect 314.159 (* pi (* radius radius)) ; expect circumference -(define circumference (* 2 pi radius)) +(define circumference + (* 2 pi radius) +) circumference ; expect 62.8318 @@ -1269,23 +1577,31 @@ circumference ; expect 62.8318 (square 21) ; expect 441 ; expect square -(define square (lambda (x) (* x x))) +(define square + (lambda (x) (* x x)) +) (square 21) ; expect 441 (square (+ 2 5)) ; expect 49 -(square (square 3)) ; expect 81 +; expect 81 +(square (square 3)) ; expect sum-of-squares (define (sum-of-squares x y) - (+ (square x) (square y))) + (+ (square x) (square y)) +) -(sum-of-squares 3 4) ; expect 25 +; expect 25 +(sum-of-squares 3 4) ; expect f (define (f a) - (sum-of-squares (+ a 1) (* a 2))) + (sum-of-squares (+ a 1) + (* a 2) + ) +) (f 5) ; expect 136 @@ -1294,7 +1610,9 @@ circumference ; expect 62.8318 (cond ((> x 0) x) ((= x 0) 0) - ((< x 0) (- x)))) + ((< x 0) (- x)) + ) +) (abs -3) ; expect 3 @@ -1304,9 +1622,8 @@ circumference ; expect 62.8318 ; expect a-plus-abs-b (define (a-plus-abs-b a b) - ((if (> b 0) + -) - a - b)) + ((if (> b 0) + -) a b) +) (a-plus-abs-b 3 -2) ; expect 5 @@ -1314,77 +1631,97 @@ circumference ; expect 62.8318 (define (sqrt-iter guess x) (if (good-enough? guess x) guess - (sqrt-iter (improve guess x) x))) + (sqrt-iter (improve guess x) + x + ) + ) +) ; expect improve (define (improve guess x) - (average guess (/ x guess))) + (average guess (/ x guess)) +) ; expect average (define (average x y) - (/ (+ x y) 2)) + (/ (+ x y) 2) +) ; expect good-enough? (define (good-enough? guess x) - (< - (abs (- (square guess) x)) - 0.001)) + (< (abs (- (square guess) x)) + 0.001 + ) +) ; expect sqrt -(define (sqrt x) (sqrt-iter 1 x)) +(define (sqrt x) + (sqrt-iter 1 x) +) -(sqrt 9) ; expect 3.00009155413138 +; expect 3.00009155413138 +(sqrt 9) -(sqrt (+ 100 37)) ; expect 11.704699917758145 +; expect 11.704699917758145 +(sqrt (+ 100 37)) ; expect 1.7739279023207892 (sqrt (+ (sqrt 2) (sqrt 3))) -(square (sqrt 1000)) ; expect 1000.000369924366 +; expect 1000.000369924366 +(square (sqrt 1000)) ; expect sqrt (define (sqrt x) (define (good-enough? guess) - (< - (abs (- (square guess) x)) - 0.001)) + (< (abs (- (square guess) x)) + 0.001 + ) + ) (define (improve guess) - (average guess (/ x guess))) + (average guess (/ x guess)) + ) (define (sqrt-iter guess) (if (good-enough? guess) guess - (sqrt-iter (improve guess)))) - (sqrt-iter 1)) + (sqrt-iter (improve guess)) + ) + ) + (sqrt-iter 1) +) -(sqrt 9) ; expect 3.00009155413138 +; expect 3.00009155413138 +(sqrt 9) -(sqrt (+ 100 37)) ; expect 11.704699917758145 +; expect 11.704699917758145 +(sqrt (+ 100 37)) ; expect 1.7739279023207892 (sqrt (+ (sqrt 2) (sqrt 3))) -(square (sqrt 1000)) ; expect 1000.000369924366 +; expect 1000.000369924366 +(square (sqrt 1000)) ; expect cube -(define (cube x) - (* x x x)) +(define (cube x) (* x x x)) ; expect sum (define (sum term a next b) (if (> a b) 0 (+ (term a) - (sum term - (next a) - next - b)))) + (sum term (next a) next b) + ) + ) +) ; expect inc (define (inc n) (+ n 1)) ; expect sum-cubes (define (sum-cubes a b) - (sum cube a inc b)) + (sum cube a inc b) +) (sum-cubes 1 10) ; expect 3025 @@ -1393,80 +1730,103 @@ circumference ; expect 62.8318 ; expect sum-integers (define (sum-integers a b) - (sum identity a inc b)) + (sum identity a inc b) +) -(sum-integers 1 10) ; expect 55 +; expect 55 +(sum-integers 1 10) ; expect 12 ((lambda (x y z) - (+ x y (square z))) + (+ x y (square z)) + ) 1 2 - 3) + 3 +) ; expect f (define (f x y) (let ((a (+ 1 (* x y))) - (b (- 1 y))) + (b (- 1 y)) + ) (+ (* x (square a)) (* y b) - (* a b)))) + (* a b) + ) + ) +) (f 3 4) ; expect 456 (define x 5) ; expect x ; expect 38 -(+ (let ((x 3)) - (+ x (* x 10))) - x) +(+ (let ((x 3) + ) + (+ x (* x 10)) + ) + x +) ; expect 21 (let ((x 3) - (y (+ x 2))) - (* x y)) + (y (+ x 2)) + ) + (* x y) +) ; expect add-rat (define (add-rat x y) - (make-rat (+ - (* (numer x) (denom y)) - (* (numer y) (denom x))) - (* (denom x) (denom y)))) + (make-rat (+ (* (numer x) (denom y)) + (* (numer y) (denom x)) + ) + (* (denom x) (denom y)) + ) +) ; expect sub-rat (define (sub-rat x y) - (make-rat (- - (* (numer x) (denom y)) - (* (numer y) (denom x))) - (* (denom x) (denom y)))) + (make-rat (- (* (numer x) (denom y)) + (* (numer y) (denom x)) + ) + (* (denom x) (denom y)) + ) +) ; expect mul-rat (define (mul-rat x y) - (make-rat - (* (numer x) (numer y)) - (* (denom x) (denom y)))) + (make-rat (* (numer x) (numer y)) + (* (denom x) (denom y)) + ) +) ; expect div-rat (define (div-rat x y) - (make-rat - (* (numer x) (denom y)) - (* (denom x) (numer y)))) + (make-rat (* (numer x) (denom y)) + (* (denom x) (numer y)) + ) +) ; expect equal-rat? (define (equal-rat? x y) - (= - (* (numer x) (denom y)) - (* (numer y) (denom x)))) + (= (* (numer x) (denom y)) + (* (numer y) (denom x)) + ) +) -(define x (cons 1 2)) ; expect x +; expect x +(define x (cons 1 2)) (car x) ; expect 1 (cdr x) ; expect 2 -(define x (cons 1 2)) ; expect x +; expect x +(define x (cons 1 2)) -(define y (cons 3 4)) ; expect y +; expect y +(define y (cons 3 4)) ; expect z (define z (cons x y)) @@ -1479,7 +1839,8 @@ z ; expect ((1 . 2) 3 . 4) ; expect make-rat (define (make-rat n d) - (cons n d)) + (cons n d) +) ; expect numer (define (numer x) (car x)) @@ -1492,76 +1853,106 @@ z ; expect ((1 . 2) 3 . 4) (display (numer x)) (display (quote /)) (display (denom x)) - (newline)) + (newline) +) -(define one-half (make-rat 1 2)) ; expect one-half +; expect one-half +(define one-half + (make-rat 1 2) +) (print-rat one-half) ; expect ; expect one-third -(define one-third (make-rat 1 3)) +(define one-third + (make-rat 1 3) +) ; expect -(print-rat (add-rat one-half one-third)) +(print-rat (add-rat one-half one-third) +) ; expect -(print-rat (mul-rat one-half one-third)) +(print-rat (mul-rat one-half one-third) +) ; expect -(print-rat (add-rat one-third one-third)) +(print-rat (add-rat one-third one-third) +) ; expect gcd (define (gcd a b) (if (= b 0) a - (gcd b (remainder a b)))) + (gcd b (remainder a b)) + ) +) ; expect make-rat (define (make-rat n d) - (let ((g (gcd n d))) - (cons (/ n g) - (/ d g)))) + (let ((g (gcd n d)) + ) + (cons (/ n g) (/ d g)) + ) +) ; expect -(print-rat (add-rat one-third one-third)) +(print-rat (add-rat one-third one-third) +) ; expect one-through-four -(define one-through-four (list 1 2 3 4)) +(define one-through-four + (list 1 2 3 4) +) one-through-four ; expect (1 2 3 4) -(car one-through-four) ; expect 1 +; expect 1 +(car one-through-four) -(cdr one-through-four) ; expect (2 3 4) +; expect (2 3 4) +(cdr one-through-four) -(car (cdr one-through-four)) ; expect 2 +; expect 2 +(car (cdr one-through-four)) -(cons 10 one-through-four) ; expect (10 1 2 3 4) +; expect (10 1 2 3 4) +(cons 10 one-through-four) -(cons 5 one-through-four) ; expect (5 1 2 3 4) +; expect (5 1 2 3 4) +(cons 5 one-through-four) ; expect map (define (map proc items) (if (null? items) nil (cons (proc (car items)) - (map proc (cdr items))))) + (map proc (cdr items)) + ) + ) +) ; expect (10 2.5 11.6 17) (map abs - (list -10 2.5 -11.6 17)) + (list -10 2.5 -11.6 17) +) ; expect (1 4 9 16) (map (lambda (x) (* x x)) - (list 1 2 3 4)) + (list 1 2 3 4) +) ; expect scale-list (define (scale-list items factor) (map (lambda (x) (* x factor)) - items)) + items + ) +) ; expect (10 20 30 40 50) -(scale-list (list 1 2 3 4 5) 10) +(scale-list (list 1 2 3 4 5) + 10 +) ; expect count-leaves (define (count-leaves x) @@ -1572,10 +1963,15 @@ one-through-four ; expect (1 2 3 4) 1) (else (+ (count-leaves (car x)) - (count-leaves (cdr x)))))) + (count-leaves (cdr x)) + )) + ) +) ; expect x -(define x (cons (list 1 2) (list 3 4))) +(define x + (cons (list 1 2) (list 3 4)) +) (count-leaves x) ; expect 4 @@ -1583,7 +1979,9 @@ one-through-four ; expect (1 2 3 4) (count-leaves (list x x)) ; expect odd? -(define (odd? x) (= 1 (remainder x 2))) +(define (odd? x) + (= 1 (remainder x 2)) +) ; expect filter (define (filter predicate sequence) @@ -1592,34 +1990,68 @@ one-through-four ; expect (1 2 3 4) nil) ((predicate (car sequence)) (cons (car sequence) - (filter predicate (cdr sequence)))) + (filter predicate + (cdr sequence) + ) + )) (else - (filter predicate (cdr sequence))))) + (filter predicate + (cdr sequence) + )) + ) +) -(filter odd? (list 1 2 3 4 5)) ; expect (1 3 5) +; expect (1 3 5) +(filter odd? (list 1 2 3 4 5)) ; expect accumulate -(define (accumulate op initial sequence) +(define (accumulate op + initial + sequence + ) (if (null? sequence) initial (op (car sequence) - (accumulate op initial (cdr sequence))))) + (accumulate op + initial + (cdr sequence) + ) + ) + ) +) -(accumulate + 0 (list 1 2 3 4 5)) ; expect 15 +; expect 15 +(accumulate + + 0 + (list 1 2 3 4 5) +) -(accumulate * 1 (list 1 2 3 4 5)) ; expect 120 +; expect 120 +(accumulate * + 1 + (list 1 2 3 4 5) +) ; expect (1 2 3 4 5) -(accumulate cons nil (list 1 2 3 4 5)) +(accumulate cons + nil + (list 1 2 3 4 5) +) ; expect enumerate-interval (define (enumerate-interval low high) (if (> low high) nil (cons low - (enumerate-interval (+ low 1) high)))) + (enumerate-interval (+ low 1) + high + ) + ) + ) +) -(enumerate-interval 2 7) ; expect (2 3 4 5 6 7) +; expect (2 3 4 5 6 7) +(enumerate-interval 2 7) ; expect enumerate-tree (define (enumerate-tree tree) @@ -1630,10 +2062,14 @@ one-through-four ; expect (1 2 3 4) (list tree)) (else (append (enumerate-tree (car tree)) - (enumerate-tree (cdr tree)))))) + (enumerate-tree (cdr tree)) + )) + ) +) ; expect (1 2 3 4 5) -(enumerate-tree (list 1 (list 2 (list 3 4)) 5)) +(enumerate-tree (list 1 (list 2 (list 3 4)) 5) +) (define a 1) ; expect a @@ -1656,20 +2092,25 @@ one-through-four ; expect (1 2 3 4) ; expect memq (define (memq item x) (cond - ((null? x) #f) - ((equal? item (car x)) x) - (else (memq item (cdr x))))) + ((null? x) + #f) + ((equal? item (car x)) + x) + (else + (memq item (cdr x))) + ) +) ; expect #f (memq (quote apple) - (quote (pear banana prune))) + (quote (pear banana prune)) +) ; expect (apple pear) (memq (quote apple) - (quote (x (apple sauce) - y - apple - pear))) + (quote (x (apple sauce) y apple pear) + ) +) ; expect my-equal? (define (my-equal? x y) @@ -1677,115 +2118,171 @@ one-through-four ; expect (1 2 3 4) ((pair? x) (and (pair? y) (my-equal? (car x) (car y)) - (my-equal? (cdr x) (cdr y)))) + (my-equal? (cdr x) (cdr y)) + )) ((null? x) (null? y)) (else - (equal? x y)))) + (equal? x y)) + ) +) ; expect #t (my-equal? (quote (1 2 (three))) - (quote (1 2 (three)))) + (quote (1 2 (three))) +) ; expect #f (my-equal? (quote (1 2 (three))) - (quote (1 2 three))) + (quote (1 2 three)) +) ; expect #f (my-equal? (quote (1 2 three)) - (quote (1 2 (three)))) + (quote (1 2 (three))) +) ; expect double -(define double (lambda (x) (* 2 x))) +(define double + (lambda (x) (* 2 x)) +) (double 5) ; expect 10 ; expect compose (define compose (lambda (f g) - (lambda (x) (f (g x))))) + (lambda (x) (f (g x))) + ) +) -((compose list double) 5) ; expect (10) +; expect (10) +((compose list double) 5) ; expect apply-twice -(define apply-twice (lambda (f) (compose f f))) +(define apply-twice + (lambda (f) (compose f f)) +) -((apply-twice double) 5) ; expect 20 +; expect 20 +((apply-twice double) 5) -((apply-twice (apply-twice double)) 5) ; expect 80 +; expect 80 +((apply-twice (apply-twice double) + ) + 5 +) ; expect fact (define fact (lambda (n) - (if (<= n 1) - 1 - (* n (fact (- n 1)))))) + (if (<= n 1) + 1 + (* n (fact (- n 1))) + ) + ) +) (fact 3) ; expect 6 ; expect 30414093201713378043612608166064768844377641568960512000000000000 -(fact 50) +(fact 50 +) ; expect combine (define (combine f) (lambda (x y) - (if (null? x) - nil - (f - (list (car x) (car y)) - ((combine f) - (cdr x) - (cdr y)))))) + (if (null? x) + nil + (f (list (car x) (car y)) + ((combine f) (cdr x) (cdr y)) + ) + ) + ) +) ; expect zip (define zip (combine cons)) ; expect ((1 5) (2 6) (3 7) (4 8)) -(zip (list 1 2 3 4) (list 5 6 7 8)) +(zip (list 1 2 3 4) + (list 5 6 7 8) +) ; expect riff-shuffle (define riff-shuffle - (lambda - (deck) - (begin - (define take - (lambda (n seq) - (if (<= n 0) - (quote nil) - (cons (car seq) - (take (- n 1) (cdr seq)))))) - (define drop - (lambda (n seq) - (if (<= n 0) - seq - (drop (- n 1) (cdr seq))))) - (define mid (lambda (seq) (/ (length seq) 2))) - ((combine append) - (take (mid deck) deck) - (drop (mid deck) deck))))) + (lambda (deck) + (begin (define take + (lambda (n seq) + (if (<= n 0) + (quote nil) + (cons (car seq) + (take (- n 1) (cdr seq)) + ) + ) + ) + ) + (define drop + (lambda (n seq) + (if (<= n 0) + seq + (drop (- n 1) (cdr seq)) + ) + ) + ) + (define mid + (lambda (seq) + (/ (length seq) 2) + ) + ) + ((combine append) + (take (mid deck) deck) + (drop (mid deck) deck) + ) + ) + ) +) ; expect (1 5 2 6 3 7 4 8) -(riff-shuffle (list 1 2 3 4 5 6 7 8)) +(riff-shuffle (list 1 2 3 4 5 6 7 8) +) ; expect (1 3 5 7 2 4 6 8) ((apply-twice riff-shuffle) - (list 1 2 3 4 5 6 7 8)) + (list 1 2 3 4 5 6 7 8) +) ; expect (1 2 3 4 5 6 7 8) -(riff-shuffle - (riff-shuffle - (riff-shuffle (list 1 2 3 4 5 6 7 8)))) +(riff-shuffle (riff-shuffle (riff-shuffle (list 1 2 3 4 5 6 7 8) + ) + ) +) -(apply square (quote (2))) ; expect 4 +; expect 4 +(apply square (quote (2))) -(apply + (quote (1 2 3 4))) ; expect 10 +; expect 10 +(apply + (quote (1 2 3 4))) ; expect (1 2 3 4) -(apply (if #f + append) (quote ((1 2) (3 4)))) - -(if 0 1 2) ; expect 1 - -(if (quote nil) 1 2) ; expect 1 +(apply (if #f + + + append + ) + (quote ((1 2) (3 4))) +) + +; expect 1 +(if 0 + 1 + 2 +) + +; expect 1 +(if (quote nil) + 1 + 2 +) ; expect #t (or #f #t) @@ -1810,25 +2307,46 @@ one-through-four ; expect (1 2 3 4) (or #f (/ 1 0)) ; expect hello -(or (quote hello) (quote world)) - -(if nil 1 2) ; expect 1 - -(if 0 1 2) ; expect 1 +(or (quote hello) + (quote world) +) + +; expect 1 +(if nil + 1 + 2 +) + +; expect 1 +(if 0 + 1 + 2 +) ; expect 2 -(if (or #f #f #f) 1 2) +(if (or #f #f #f) + 1 + 2 +) -(define (loop) (loop)) ; expect loop +; expect loop +(define (loop) (loop)) (cond (#f (loop)) (12 - )) + ) +) ; expect 2 -((lambda (x) (display x) (newline) x) 2) +((lambda (x) + (display x) + (newline) + x + ) + 2 +) ; expect g (define g (mu nil x)) @@ -1841,9 +2359,11 @@ one-through-four ; expect (1 2 3 4) ; expect print-and-square (define (print-and-square x) (print x) - (square x)) + (square x) +) -(print-and-square 12) ; expect 144 +; expect 144 +(print-and-square 12) (/ 1 0) ; expect SchemeError @@ -1852,31 +2372,38 @@ one-through-four ; expect (1 2 3 4) ; expect add2xy (define add2xy - (lambda (x y) - (addx (+ x x)))) + (lambda (x y) (addx (+ x x))) +) (add2xy 3 7) ; expect 13 ; expect 7 -(let ((x 2)) +(let ((x 2) + ) ((begin (define x (+ x 1)) +) 3 - (begin (define x (+ x 1)) x))) + (begin (define x (+ x 1)) x) + ) +) ; expect len (define (len s) (if (eq? s (quote nil)) 0 - (+ 1 (len (cdr s))))) + (+ 1 (len (cdr s))) + ) +) -(len (quote (1 2 3 4))) ; expect 4 +; expect 4 +(len (quote (1 2 3 4))) ; expect sum (define (sum n total) (if (zero? n) total - (sum (- n 1) - (+ n total)))) + (sum (- n 1) (+ n total)) + ) +) ; (sum 1001 0) ; expect 501501 @@ -1886,26 +2413,33 @@ one-through-four ; expect (1 2 3 4) ((zero? n) total) (else - (sum (- n 1) - (+ n total))))) + (sum (- n 1) (+ n total))) + ) +) ; (sum 1001 0) ; expect 501501 ; expect sum (define (sum n total) - (begin - 2 - 3 - (if (zero? n) - total - (and 2 - 3 - (or #f - (begin 2 - 3 - (let ((m n)) - (sum (- m 1) - (+ m total))))))))) + (begin 2 + 3 + (if (zero? n) + total + (and 2 + 3 + (or #f + (begin 2 + 3 + (let ((m n) + ) + (sum (- m 1) (+ m total)) + ) + ) + ) + ) + ) + ) +) ; (sum 1001 0) ; expect 501501 @@ -1917,33 +2451,53 @@ one-through-four ; expect (1 2 3 4) (if (null? lst) nil (cons (f (car lst)) - (map f (cdr lst))))) + (map f (cdr lst)) + ) + ) +) ; expect for (define-macro (for formal iterable body) (list (quote map) - (list (quote lambda) (list formal) body) - iterable)) + (list (quote lambda) + (list formal) + body + ) + iterable + ) +) ; expect (0 2 3) (for i (quote (1 2 3)) - (if (= i 1) 0 i)) + (if (= i 1) + 0 + i + ) +) ; expect cadr -(define (cadr s) (car (cdr s))) +(define (cadr s) + (car (cdr s)) +) ; expect cars (define (cars s) (map car s)) ; expect cadrs -(define (cadrs s) (map cadr s)) +(define (cadrs s) + (map cadr s) +) ; expect leet (define-macro (leet bindings expr) - (cons - (list (quote lambda) (cars bindings) expr) - (cadrs bindings))) + (cons (list (quote lambda) + (cars bindings) + expr + ) + (cadrs bindings) + ) +) ; expect square (define (square x) (* x x)) @@ -1951,17 +2505,22 @@ one-through-four ; expect (1 2 3 4) ; expect hyp (define (hyp a b) (leet ((a2 (square a)) - (b2 (square b))) - (sqrt (+ a2 b2)))) + (b2 (square b)) + ) + (sqrt (+ a2 b2)) + ) +) -(hyp 3 4) ; expect 5.000023178253949 +; expect 5.000023178253949 +(hyp 3 4) ; expect sum (define (sum n total) (if (zero? n) total - (sum (- n 1) - (+ n total)))) + (sum (- n 1) (+ n total)) + ) +) ; (sum 1001 0) ; expect 501501 @@ -1971,8 +2530,10 @@ one-through-four ; expect (1 2 3 4) total (if #f 42 - (sum (- n 1) - (+ n total))))) + (sum (- n 1) (+ n total)) + ) + ) +) ; (sum 1001 0) ; expect 501501 @@ -1982,10 +2543,11 @@ one-through-four ; expect (1 2 3 4) ((zero? n) total) ((zero? 0) - (sum (- n 1) - (+ n total))) + (sum (- n 1) (+ n total))) (else - 42))) + 42) + ) +) ; (sum 1001 0) ; expect 501501 @@ -1993,12 +2555,16 @@ one-through-four ; expect (1 2 3 4) (define (sum n total) (if (zero? n) total - (add n (+ n total)))) + (add n (+ n total)) + ) +) ; expect add (define add (lambda (x+1 y) - (sum (- x+1 1) y))) + (sum (- x+1 1) y) + ) +) ; (sum 1001 0) ; expect 501501 @@ -2006,20 +2572,28 @@ one-through-four ; expect (1 2 3 4) (define (sum n total) (if (zero? n) total - (let ((n-1 (- n 1))) - (sum n-1 (+ n total))))) + (let ((n-1 (- n 1)) + ) + (sum n-1 (+ n total)) + ) + ) +) ; (sum 1001 0) ; expect 501501 ; expect sum (define (sum n total) (or (and (zero? n) total) - (add n (+ n total)))) + (add n (+ n total)) + ) +) ; expect add (define add (lambda (x+1 y) - (sum (- x+1 1) y))) + (sum (- x+1 1) y) + ) +) ; (sum 1001 0) ; expect 501501 @@ -2027,9 +2601,13 @@ one-through-four ; expect (1 2 3 4) (define (sum n total) (define add (lambda (x+1 y) - (sum (- x+1 1) y))) + (sum (- x+1 1) y) + ) + ) (or (and (zero? n) total) - (add n (+ n total)))) + (add n (+ n total)) + ) +) ; (sum 1001 0) ; expect 501501 @@ -2039,6 +2617,11 @@ one-through-four ; expect (1 2 3 4) (define (sum n total) (begin (define add (lambda (x+1 y) - (sum (- x+1 1) y))) + (sum (- x+1 1) y) + ) + ) (or (and (zero? n) total) - (add n (+ n total))))) \ No newline at end of file + (add n (+ n total)) + ) + ) +) diff --git a/editor/format_parser.py b/editor/format_parser.py index ceb594d0..83cfdf97 100644 --- a/editor/format_parser.py +++ b/editor/format_parser.py @@ -43,7 +43,11 @@ def __repr__(self): def get_expression(buffer: TokenBuffer) -> Formatted: token = buffer.pop_next_token() comments = [] - if token in SPECIALS: + if token == "#" and not buffer.done and buffer.get_next_token() == "[": + buffer.pop_next_token() + out = FormatAtom("#[" + buffer.pop_next_token().value + "]") + buffer.pop_next_token() + elif token in SPECIALS: comments = token.comments if token in ("(", "["): out = get_rest_of_list(buffer, ")" if token == "(" else "]") diff --git a/editor/formatter.py b/editor/formatter.py index c24d3c7d..143faf62 100644 --- a/editor/formatter.py +++ b/editor/formatter.py @@ -21,19 +21,23 @@ CACHE_SIZE = 2 ** 8 +java_newline = "" -def prettify(strings: List[str]) -> str: +def prettify(strings: List[str], javastyle: bool=False) -> str: out = [] for i, string in enumerate(strings): if not string.strip(): continue - out.extend(prettify_single(string)) + out.extend(prettify_single(string, javastyle)) return "\n\n".join(out) @lru_cache(CACHE_SIZE) -def prettify_single(string: str) -> List[str]: +def prettify_single(string: str, javastyle: bool) -> List[str]: + if javastyle: + global java_newline + java_newline = "\n" out = [] buff = lexer.TokenBuffer([string], True) while not buff.done: @@ -149,7 +153,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]: len(f"({operator} ")) body_str = indent("\n".join(body), INDENT // 2) out_str = expr.open_paren + operator + " " + name_str.lstrip() + "\n" \ - + body_str + expr.close_paren + + body_str + java_newline + expr.close_paren return verify(make_comments(expr.comments, 0, True) + out_str, remaining) if operator == "let": @@ -175,7 +179,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]: out_str = expr.open_paren + "let " + binding_str.lstrip() + "\n" + body_string if expr.contents[-1].comments: out_str += "\n" - out_str += expr.close_paren + out_str += java_newline + expr.close_paren return verify(make_comments(expr.comments, 0, True) + out_str, remaining) if operator == "cond": @@ -220,7 +224,8 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]: else: # success! out_str = make_comments(expr.comments, 0, True) + expr.open_paren + \ - "cond\n" + indent("\n".join(formatted_clauses), 1) + expr.close_paren + "cond\n" + indent("\n".join(formatted_clauses), 1) + \ + java_newline + expr.close_paren out = verify(out_str, remaining) if out[1]: return out @@ -240,7 +245,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]: formatted_clauses.append(clause_str) out_str = expr.open_paren + "cond\n" + indent("\n".join(formatted_clauses), 1) \ - + expr.close_paren + + java_newline + expr.close_paren return verify(make_comments(expr.comments, 0, True) + out_str, remaining) # assume no special forms @@ -256,7 +261,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]: out_str = expr.open_paren + operator + " " + operand_string.lstrip() if expr.contents[-1].comments: out_str += "\n" - out_str += expr.close_paren + out_str += java_newline + expr.close_paren out = verify(make_comments(expr.comments, 0, True) + out_str, remaining) return out # but may have to go here anyway, if inlining takes up too much space @@ -304,7 +309,7 @@ def prettify_data(expr: Formatted, remaining: int, is_data: bool, force_multilin out_str = expr.open_paren + elem_string if expr.contents and expr.contents[-1].comments: out_str += "\n" - out_str += expr.close_paren + out_str += java_newline + expr.close_paren return verify(make_comments(expr.comments, 0, True) + out_str, remaining) diff --git a/editor/local_server.py b/editor/local_server.py index fa086e25..cc5ae140 100644 --- a/editor/local_server.py +++ b/editor/local_server.py @@ -26,8 +26,6 @@ state = {} -import ctypes - class Handler(server.BaseHTTPRequestHandler): cancellation_event = threading.Event() # Shared across all instances, because the threading mixin creates a new instance every time... @@ -83,10 +81,11 @@ def handle_post_thread(self, data, path): elif path == "/reformat": code = [x.decode("utf-8") for x in data[b"code[]"]] + javastyle = data[b"javastyle"][0] == b"true" self.send_response(HTTPStatus.OK, 'test') self.send_header("Content-type", "application/JSON") self.end_headers() - self.wfile.write(bytes(json.dumps({"result": "success", "formatted": prettify(code)}), "utf-8")) + self.wfile.write(bytes(json.dumps({"result": "success", "formatted": prettify(code, javastyle)}), "utf-8")) elif path == "/test": self.cancellation_event.clear() # Make sure we don't have lingering cancellation requests from before @@ -275,9 +274,11 @@ def supports_color(): return False return True + class ThreadedHTTPServer(socketserver.ThreadingMixIn, server.HTTPServer): daemon_threads = True + def start(file_args, port, open_browser): global main_files main_files = file_args diff --git a/editor/static/index.html b/editor/static/index.html index 6f841a1c..ecc3a384 100644 --- a/editor/static/index.html +++ b/editor/static/index.html @@ -148,6 +148,12 @@ Hide frames after return. Note that frames that continue to be referenced from closures will also be hidden. +
+ + +
diff --git a/editor/static/scripts/editor.js b/editor/static/scripts/editor.js index 823c96fc..0d7da603 100644 --- a/editor/static/scripts/editor.js +++ b/editor/static/scripts/editor.js @@ -4,6 +4,7 @@ import {open} from "./layout"; import {make, request_update} from "./event_handler"; import {terminable_command} from "./canceller"; import {registerEditor, removeEditor, notify_changed} from "./test_results"; +import {javastyle} from "./settings"; export {register}; @@ -288,6 +289,7 @@ function register(layout) { let code = [editor.getValue()]; $.post("./reformat", { code: code, + javastyle: javastyle(), }).done(function (data) { if (data) { data = $.parseJSON(data); diff --git a/editor/static/scripts/settings.js b/editor/static/scripts/settings.js index 8a845f0a..f47a7ee3 100644 --- a/editor/static/scripts/settings.js +++ b/editor/static/scripts/settings.js @@ -1,6 +1,6 @@ import {saveState} from "./state_handler"; -export {init, hide_return_frames, getAllSettings, setAllSettings}; +export {init, hide_return_frames, javastyle, getAllSettings, setAllSettings}; function init() { $("#settings-btn").click(function () { @@ -18,12 +18,18 @@ function hide_return_frames() { return $("#hideReturnFramesCheckbox").prop('checked'); } +function javastyle() { + return $("#javastyleCheckbox").prop('checked'); +} + function getAllSettings() { return { - "return_frames": hide_return_frames() + "return_frames": hide_return_frames(), + "javastyle": javastyle(), } } function setAllSettings(data) { $("#hideReturnFramesCheckbox").prop('checked', data["return_frames"]); + $("#javastyleCheckbox").prop('checked', data["javastyle"]); } diff --git a/editor/static/scripts/substitution_tree_worker.js b/editor/static/scripts/substitution_tree_worker.js index 8662829b..c2c6d21c 100644 --- a/editor/static/scripts/substitution_tree_worker.js +++ b/editor/static/scripts/substitution_tree_worker.js @@ -9,6 +9,8 @@ import { } from "./measure"; import {get_active_node} from "./navigation"; +import {javastyle} from "./settings"; + export { display_tree, get_i @@ -40,7 +42,10 @@ function display_str(elem) { async function locate(data) { await $.post("./reformat", - {code: [display_str(data)]}).done( + { + code: [display_str(data)], + javastyle: javastyle() + }).done( (response) => { response = $.parseJSON(response); data["str"] = response["formatted"];