chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] newbie questions about macros in modules


From: Imran Rafique
Subject: [Chicken-users] newbie questions about macros in modules
Date: Thu, 22 Jul 2010 16:46:39 -0700

Rank newbie here, so bear with me. I know these examples are somewhat contrived. I'm just trying to explore what I can (and can't do). I have a couple of questions regarding macro's inside a module.

1) how to define something so that its available within either a macro or a function

(define-for-syntax) doesn't make the binding available for use in a function. And (define ...) has the opposite problem.

So far, the only way I've found to do this (thanks to the VERY helpful and patient guys on #chicken) is to create a secondary module, move my bindings into that, and then import it twice into the module where the macros and functions - which use those variables - are themselves defined.

Is there a better way?

2) module io defines a macro (debug-info) and a function (print-info). debug-info calls print-info. If module io is imported without a prefix, then it works. If module io is imported WITH a prefix, then the macro (debug-info) is trying to call a function (print-info) which isn't available (only <prefix>print-info is). Is there any way to account for this when creating the macro?

Example code below

;;; ---- repl ----
> (define _debug #t)
> (load-relative "/home/imran/src/scheme/lib/io.scm")
> (import (prefix io io/))
> (io/print-info "blah")
[lib/io 15:42:43] blah
> (io/debug-info "blah")
Error: unbound variable: print-info

> (import io)
> (debug-info "blah")
[lib/io 15:47:00] (debugging) blah


;;; ---- file: /home/imran/src/scheme/lib/io.scm ----
; this module define's all variables which I want available in module io for macro's and functions
(module io_private *
  (import scheme chicken)
  (define _colours-on "\x1b[1;44;36m")
  (define _colours-off "\x1b[0m")
  (define _debugging-colours-on "\x1b[1;31m")
)

(module io (sn print-info debug-info debug-info2)
  (import scheme chicken)
  (use srfi-1 extras posix data-structures srfi-69 srfi-13)
  ; import local module from above
  (import-for-syntax io_private)
  (import io_private)

  ;; resetting indentation for all bindings below (easier to work with)

(define sn (make-parameter "lib/io"))

(define (print-info msg . data)
  (apply format #t (string-append "~a[~a ~a]~a " msg "\n")
         _colours-on
         (sn)
         (time->string (seconds->local-time) "%H:%M:%S")
         _colours-off
         data))

(define-syntax debug-info
  (syntax-rules (debug)
    ((_ debug msg val ...)
     '(and _debug (print-info (string-append  _debugging-colours-on "(debugging)" _colours-off " " msg) val ...)) )
    ((_ msg val ...)
     (and _debug (print-info (string-append  _debugging-colours-on "(debugging)" _colours-off " " msg) val ...)) )
    ))

(define-syntax debug-info2
  (syntax-rules (debug)
    ((_ debug msg . vals)
     '(and _debug (print-info (string-append _debugging-colours-on "(debugging)" _colours-off " " msg) . vals)) )
    ((_ msg . vals)
     (and _debug (print-info (string-append _debugging-colours-on "(debugging)" _colours-off " " msg) . vals)) )
    ))

) ; end of module


Regards,
             Imran Rafique

reply via email to

[Prev in Thread] Current Thread [Next in Thread]