Avoid having (read) as an atom in Cvar language
This commit is contained in:
parent
ec8e75232e
commit
44a453a062
|
|
@ -6,10 +6,10 @@
|
||||||
|
|
||||||
; remove complex sub-expression
|
; remove complex sub-expression
|
||||||
; the resulting code is either
|
; the resulting code is either
|
||||||
; - (read)
|
|
||||||
; - a number literal
|
; - a number literal
|
||||||
; - a symbol
|
; - a symbol
|
||||||
; the three above are called in the following "simple terms"
|
; the three above are called in the following "simple terms"
|
||||||
|
; - (read)
|
||||||
; - (- x) where x is a simple term
|
; - (- x) where x is a simple term
|
||||||
; - (+ x y) where x and y are simple terms
|
; - (+ x y) where x and y are simple terms
|
||||||
; - (let ([var y]) z) where y and z are expressions
|
; - (let ([var y]) z) where y and z are expressions
|
||||||
|
|
@ -61,7 +61,13 @@
|
||||||
(match exp
|
(match exp
|
||||||
[(? fixnum?) (values exp '() tmpcount)]
|
[(? fixnum?) (values exp '() tmpcount)]
|
||||||
[(? symbol?) (values exp '() tmpcount)]
|
[(? symbol?) (values exp '() tmpcount)]
|
||||||
[`(read) (values exp '() tmpcount)]
|
[`(read)
|
||||||
|
(begin
|
||||||
|
(define new-tmpcount (+ tmpcount 1))
|
||||||
|
(define tmpname (get-unique-symbol new-tmpcount))
|
||||||
|
(values tmpname
|
||||||
|
(list `(,tmpname (read)))
|
||||||
|
new-tmpcount))]
|
||||||
[`(- ,e)
|
[`(- ,e)
|
||||||
(begin
|
(begin
|
||||||
(define-values (new-exp assoc-list exp-tmpcount) (rco-arg e tmpcount))
|
(define-values (new-exp assoc-list exp-tmpcount) (rco-arg e tmpcount))
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,6 @@
|
||||||
|
|
||||||
`(program () (+ (let ([x (+ (- 1) 2)]) (+ x 2)) (+ 4 5)))))
|
`(program () (+ (let ([x (+ (- 1) 2)]) (+ x 2)) (+ 4 5)))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define (pass program) (explicate-control (remove-complex-opera* program)))
|
(define (pass program) (explicate-control (remove-complex-opera* program)))
|
||||||
|
|
||||||
(test-eq
|
(test-eq
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,21 @@
|
||||||
`(program () (let ([x (let ([x 1]) x)]) (+ 2 x)))
|
`(program () (let ([x (let ([x 1]) x)]) (+ 2 x)))
|
||||||
`(program ()
|
`(program ()
|
||||||
(let ([y (let ([x 20])
|
(let ([y (let ([x 20])
|
||||||
(+ x (let ([x 22]) x)))]) y))))
|
(+ x (let ([x 22]) x)))]) y))
|
||||||
|
`(program ()
|
||||||
|
(+ (read) (read)))))
|
||||||
|
|
||||||
(for ([program programs] [env (build-list (length programs) (lambda (_) '()))])
|
(define inputs
|
||||||
(test-eq ((interp-R1 env) program)
|
(let ([empty-inputs (build-list (length programs) (lambda (_) ""))])
|
||||||
((interp-R1 env) (remove-complex-opera* program))))
|
(list-set empty-inputs 13 "2\n3")))
|
||||||
|
|
||||||
|
(for ([program programs]
|
||||||
|
[env (build-list (length programs) (lambda (_) '()))]
|
||||||
|
[input-string inputs])
|
||||||
|
(test-eq (with-input-from-string input-string
|
||||||
|
(lambda () ((interp-R1 env) program)))
|
||||||
|
(with-input-from-string input-string
|
||||||
|
(lambda () ((interp-R1 env) (remove-complex-opera* program))))))
|
||||||
|
|
||||||
(test-eq
|
(test-eq
|
||||||
(remove-complex-opera* (list-ref programs 0))
|
(remove-complex-opera* (list-ref programs 0))
|
||||||
|
|
@ -95,3 +105,7 @@
|
||||||
(test-eq
|
(test-eq
|
||||||
(remove-complex-opera* (list-ref programs 12))
|
(remove-complex-opera* (list-ref programs 12))
|
||||||
`(program () (let ((y.1 (let ((x.1 20)) (let ((x.2 22)) (+ x.1 x.2))))) y.1)))
|
`(program () (let ((y.1 (let ((x.1 20)) (let ((x.2 22)) (+ x.1 x.2))))) y.1)))
|
||||||
|
|
||||||
|
(test-eq
|
||||||
|
(remove-complex-opera* (list-ref programs 13))
|
||||||
|
`(program () (let ((tmp.1 (read))) (let ((tmp.2 (read))) (+ tmp.1 tmp.2)))))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue