2014-08-06 19:16:50 +00:00
|
|
|
#lang racket/base
|
2013-04-14 19:33:07 +00:00
|
|
|
|
|
|
|
(require racket/match)
|
|
|
|
|
|
|
|
(require (prefix-in core: "../main.rkt"))
|
2014-08-06 19:16:50 +00:00
|
|
|
(require "../sugar.rkt")
|
2013-04-14 19:33:07 +00:00
|
|
|
(require "../vm.rkt")
|
|
|
|
(require "../process.rkt")
|
|
|
|
(require "../quasiqueue.rkt")
|
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
(require "gui.rkt")
|
2013-04-14 23:00:54 +00:00
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; (define-type Debugger (All (S) (S -> S)))
|
2013-04-14 19:33:07 +00:00
|
|
|
|
2013-04-23 20:26:10 +00:00
|
|
|
(provide debug)
|
2013-04-14 23:00:54 +00:00
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; debug : (All (ParentState) (Spawn ParentState) -> (Spawn ParentState))
|
2013-04-14 19:33:07 +00:00
|
|
|
(define (debug spawn-child)
|
|
|
|
(match-define (core:spawn child-spec parent-k debug-name) spawn-child)
|
|
|
|
(core:spawn
|
2014-08-06 19:16:50 +00:00
|
|
|
(core:process-spec
|
|
|
|
(lambda (pid) ;; TODO: exploit this more in messages etc.
|
|
|
|
(define original-cotransition ((core:process-spec-boot child-spec) pid))
|
|
|
|
;; wrapped-cotransition : (All (R) (All (S) (Transition S) -> R) -> R)
|
2013-04-14 19:33:07 +00:00
|
|
|
(define (wrapped-cotransition k)
|
2014-08-06 19:16:50 +00:00
|
|
|
;; receiver : (All (S) (Transition S) -> R)
|
2013-04-14 19:33:07 +00:00
|
|
|
(define (receiver child-transition)
|
2013-04-23 20:26:10 +00:00
|
|
|
(define d (open-debugger debug-name))
|
2014-08-06 19:16:50 +00:00
|
|
|
(k (wrap-transition d child-transition)))
|
|
|
|
(original-cotransition receiver))
|
2013-04-14 19:33:07 +00:00
|
|
|
wrapped-cotransition))
|
|
|
|
parent-k
|
|
|
|
(list 'debug debug-name)))
|
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; wrap-transition : (All (ChildState)
|
|
|
|
;; Debugger
|
|
|
|
;; (Transition ChildState)
|
|
|
|
;; -> (Transition ChildState))
|
2013-04-23 20:26:10 +00:00
|
|
|
(define (wrap-transition d child-transition0)
|
2014-08-06 19:16:50 +00:00
|
|
|
(define child-transition (d child-transition0))
|
2013-04-14 19:33:07 +00:00
|
|
|
(match-define (core:transition child-state child-actions) child-transition)
|
2014-08-06 19:16:50 +00:00
|
|
|
(core:transition child-state (action-tree-map (wrap-action d)
|
|
|
|
child-actions)))
|
2013-04-14 19:33:07 +00:00
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; action-tree-map : (All (State) ((Action State) -> (Action State))
|
|
|
|
;; (ActionTree State)
|
|
|
|
;; -> (ActionTree State))
|
2013-04-14 19:33:07 +00:00
|
|
|
(define (action-tree-map f actions)
|
2014-08-06 19:16:50 +00:00
|
|
|
(map f (quasiqueue->list (action-tree->quasiqueue actions))))
|
2013-04-14 19:33:07 +00:00
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; wrap-action : (All (ChildState)
|
|
|
|
;; Debugger
|
|
|
|
;; -> ((Action ChildState) -> (Action ChildState)))
|
2013-04-14 23:00:54 +00:00
|
|
|
(define ((wrap-action d) action)
|
2013-04-14 19:33:07 +00:00
|
|
|
(cond
|
|
|
|
[(core:yield? action)
|
2013-04-14 23:00:54 +00:00
|
|
|
(core:yield (wrap-interruptk d (core:yield-k action)))]
|
2013-04-14 19:33:07 +00:00
|
|
|
[(core:at-meta-level? action)
|
2013-04-14 23:00:54 +00:00
|
|
|
(core:at-meta-level (wrap-preaction #t d (core:at-meta-level-preaction action)))]
|
2013-04-14 19:33:07 +00:00
|
|
|
[else
|
2013-04-14 23:00:54 +00:00
|
|
|
(wrap-preaction #f d action)]))
|
2013-04-14 19:33:07 +00:00
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; wrap-preaction : (All (ChildState)
|
|
|
|
;; Boolean
|
|
|
|
;; Debugger
|
|
|
|
;; (PreAction ChildState)
|
|
|
|
;; -> (PreAction ChildState))
|
2013-04-14 23:00:54 +00:00
|
|
|
(define (wrap-preaction meta? d preaction)
|
2013-04-14 19:33:07 +00:00
|
|
|
(match preaction
|
|
|
|
[(core:add-endpoint pre-eid role handler)
|
2013-04-14 23:00:54 +00:00
|
|
|
(core:add-endpoint pre-eid role (wrap-handler meta? d handler))]
|
2013-04-14 19:33:07 +00:00
|
|
|
[(core:delete-endpoint pre-eid reason)
|
|
|
|
preaction]
|
|
|
|
[(core:send-message body orientation)
|
|
|
|
preaction]
|
|
|
|
[(core:spawn spec maybe-k child-debug-name)
|
2013-04-14 23:00:54 +00:00
|
|
|
(core:spawn spec (wrap-spawnk d maybe-k) child-debug-name)]
|
2013-04-14 19:33:07 +00:00
|
|
|
[(core:quit pid reason)
|
|
|
|
preaction]))
|
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; wrap-interruptk : (All (ChildState)
|
|
|
|
;; Debugger
|
|
|
|
;; (InterruptK ChildState)
|
|
|
|
;; -> (InterruptK ChildState))
|
2013-04-14 23:00:54 +00:00
|
|
|
(define (wrap-interruptk d ik)
|
2013-04-14 19:33:07 +00:00
|
|
|
(lambda (state)
|
2013-04-14 23:00:54 +00:00
|
|
|
(wrap-transition d (ik state))))
|
2013-04-14 19:33:07 +00:00
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; wrap-spawnk : (All (ChildState)
|
|
|
|
;; Debugger
|
|
|
|
;; (Option (PID -> (InterruptK ChildState)))
|
|
|
|
;; -> (Option (PID -> (InterruptK ChildState))))
|
2013-04-14 23:00:54 +00:00
|
|
|
(define (wrap-spawnk d maybe-k)
|
2013-04-14 19:33:07 +00:00
|
|
|
(and maybe-k
|
2014-08-06 19:16:50 +00:00
|
|
|
(lambda (child-pid) (wrap-interruptk d (maybe-k child-pid)))))
|
2013-04-14 19:33:07 +00:00
|
|
|
|
2014-08-06 19:16:50 +00:00
|
|
|
;; wrap-handler : (All (ChildState)
|
|
|
|
;; Boolean
|
|
|
|
;; Debugger
|
|
|
|
;; (Handler ChildState)
|
|
|
|
;; -> (Handler ChildState))
|
2013-04-23 20:26:10 +00:00
|
|
|
(define (wrap-handler meta?0 d h)
|
|
|
|
(lambda (event0)
|
2014-08-06 19:16:50 +00:00
|
|
|
(match-define (cons meta? event) (d (cons meta?0 event0)))
|
2013-04-14 23:00:54 +00:00
|
|
|
(wrap-interruptk d (h event))))
|