Skip to content

Instantly share code, notes, and snippets.

@sdevlin

sdevlin/out.log Secret

Created February 21, 2020 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdevlin/2d3e3b5350926c0fb78f4bff4f3a8899 to your computer and use it in GitHub Desktop.
Save sdevlin/2d3e3b5350926c0fb78f4bff4f3a8899 to your computer and use it in GitHub Desktop.
Low-level traps
% guile --debug -q ~/test.scm
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
return: cc (11 1)
apply: first-denomination (_)
return: cc (11 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (10 1)
apply: first-denomination (_)
return: cc (10 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (9 1)
apply: first-denomination (_)
return: cc (9 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (8 1)
apply: first-denomination (_)
return: cc (8 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (7 1)
apply: first-denomination (_)
return: cc (7 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (6 1)
apply: first-denomination (_)
return: cc (6 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (5 1)
apply: first-denomination (_)
return: cc (5 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (4 1)
apply: first-denomination (_)
return: cc (4 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (3 1)
apply: first-denomination (_)
return: cc (3 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (2 1)
apply: first-denomination (_)
return: cc (2 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (1 1)
apply: first-denomination (_)
return: cc (1 1)
apply: cc (_ _)
return: cc (1 1)
return: cc (2 1)
return: cc (3 1)
return: cc (4 1)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (11 2)
apply: first-denomination (_)
return: cc (11 2)
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
return: cc (6 1)
apply: first-denomination (_)
return: cc (6 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (5 1)
apply: first-denomination (_)
return: cc (5 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (4 1)
apply: first-denomination (_)
return: cc (4 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (3 1)
apply: first-denomination (_)
return: cc (3 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (2 1)
apply: first-denomination (_)
return: cc (2 1)
apply: cc (_ _)
apply: cc (_ _)
return: cc (1 1)
apply: first-denomination (_)
return: cc (1 1)
apply: cc (_ _)
return: cc (1 1)
return: cc (2 1)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (6 2)
apply: first-denomination (_)
return: cc (6 2)
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
return: cc (1 1)
apply: first-denomination (_)
return: cc (1 1)
apply: cc (_ _)
return: cc (1 1)
return: cc (1 2)
apply: first-denomination (_)
return: cc (1 2)
apply: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (_ _)
return: cc (11 3)
apply: first-denomination (_)
return: cc (11 3)
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
apply: cc (_ _)
return: cc (1 1)
apply: first-denomination (_)
return: cc (1 1)
apply: cc (_ _)
return: cc (1 1)
return: cc (1 2)
apply: first-denomination (_)
return: cc (1 2)
apply: cc (_ _)
return: cc (1 2)
return: cc (1 3)
apply: first-denomination (_)
return: cc (1 3)
apply: cc (_ _)
return: cc (1 3)
return: cc (11 3)
return: cc (11 4)
apply: first-denomination (_)
return: cc (11 4)
apply: cc (_ _)
return: cc (_ _)
return: cc (11 5)
apply: first-denomination (_)
return: cc (11 5)
apply: cc (_ _)
return: cc (11 5)
return: #f ()
(use-modules (system vm vm)
(system vm traps)
(system vm trap-state))
(define (count-change amount)
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
(define (apply-handler frame depth)
(format #t
"apply: ~a ~a\n"
(frame-procedure-name frame)
(frame-arguments frame)))
(define (return-handler frame . args)
(format #t
"return: ~a ~a\n"
(frame-procedure-name frame)
(frame-arguments frame)))
(define t
(trap-calls-in-dynamic-extent count-change apply-handler return-handler))
(add-trap! t "test-trap")
(set-vm-trace-level! 1)
(count-change 11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment