#lang scheme/base

(require (prefix-in log: (planet synx/log:1)))

(define procedures (make-immutable-hash null))

(define (register-entry-point! name procedure)
  (log:info "registering one for ~s ~s" name (hash-count procedures))
  (let ((procs (hash-ref procedures name (λ () null))))
    (set! procedures (hash-set procedures name (cons procedure procs)))))

(define-syntax-rule (define-entry-point name body ...)
  (register-entry-point! 'name (λ () body ...)))

(define-syntax-rule (main-begin body ...)
  (register-entry-point! 'main (λ () body ...)))

(define (enter root (name 'main))
  (dynamic-require root 0)
  (log:info "entering ~s ~s" name (hash-count procedures))
  (for-each
   (λ (proc)
     (proc))
   (hash-ref procedures name (λ () null))))

(define (entry-points)
  (hash-map procedures (λ (k v) k)))

(provide define-entry-point main-begin enter entry-points)