Constructori:
* stack -> list
* push : expVal x list -> list
Observatori:
* top : list -> expVal
* pop : list -> list
* size : list -> num-val
> lang.scm_________________________________________________
;; the grammar
(expression ("stack") stack-exp)
(expression ("push" expression expression) push-exp)
(expression ("top" expression) top-exp)
(expression ("pop" expression) pop-exp)
(expression ("size" "(" expression ")") size-exp)
> interp.scm_______________________________________________
;; value-of
(stack-exp ()
'())
(push-exp (exp1 exp2)
(cons (value-of exp1 env) (value-of exp2 env)))
(top-exp (exp1)
(car (value-of exp1 env))
)
(pop-exp (exp1)
(cdr (value-of exp1 env))
)
(size-exp (exp1)
(num-val (length (value-of exp1 env)))
)
> tests.scm__________________________________________________
(stack-1 "top stack" error)
(stack-2 "top push 3 push 4 stack" 3)
(stack-3 "pop stack" error)
(stack-4 "pop push 1 push 2 push zero?(0) stack" (2 #t))
(stack-5 "size(stack)" 0)
(stack-6 "size(push 100 stack)" 1)
(stack-7 "let x=1 in let y=2 in top pop push x push -(x,y) push y stack" -1)
_____________________________________________________________
* stack -> list
* push : expVal x list -> list
Observatori:
* top : list -> expVal
* pop : list -> list
* size : list -> num-val
> lang.scm_________________________________________________
;; the grammar
(expression ("stack") stack-exp)
(expression ("push" expression expression) push-exp)
(expression ("top" expression) top-exp)
(expression ("pop" expression) pop-exp)
(expression ("size" "(" expression ")") size-exp)
> interp.scm_______________________________________________
;; value-of
(stack-exp ()
'())
(push-exp (exp1 exp2)
(cons (value-of exp1 env) (value-of exp2 env)))
(top-exp (exp1)
(car (value-of exp1 env))
)
(pop-exp (exp1)
(cdr (value-of exp1 env))
)
(size-exp (exp1)
(num-val (length (value-of exp1 env)))
)
> tests.scm__________________________________________________
(stack-1 "top stack" error)
(stack-2 "top push 3 push 4 stack" 3)
(stack-3 "pop stack" error)
(stack-4 "pop push 1 push 2 push zero?(0) stack" (2 #t))
(stack-5 "size(stack)" 0)
(stack-6 "size(push 100 stack)" 1)
(stack-7 "let x=1 in let y=2 in top pop push x push -(x,y) push y stack" -1)
_____________________________________________________________
Niciun comentariu:
Trimiteți un comentariu