guile-devel
[Top][All Lists]
Advanced

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

proposal: scm_c_public_ref et al


From: Andy Wingo
Subject: proposal: scm_c_public_ref et al
Date: Sun, 06 Mar 2011 12:24:11 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Hey all,

As we move more and more to writing code in Scheme and not in C, it
becomes apparent that it is more cumbersome to reference Scheme
values than it should be.

I propose that we add helper C APIs like these:

    SCM scm_public_lookup (SCM module_name, SCM sym);
    SCM scm_private_lookup (SCM module_name, SCM sym);

    Look up a variable bound to SYM in the module named MODULE_NAME.  If
    the module does not exist or the symbol is unbound, signal an
    error.  The "public" variant looks in the public interface of the
    module, while scm_private_lookup looks into the module itself.

Then also:

    SCM scm_public_ref (SCM module_name, SCM sym);

    Equivalent to scm_variable_ref (scm_public_ref (module_name, sym)).

    SCM scm_private_ref (SCM module_name, SCM sym);

    Equivalent to scm_variable_ref (scm_private_ref (module_name, sym)).

And then:

    SCM scm_c_public_lookup (const char *module_name, const char *name);
    SCM scm_c_private_lookup (const char *module_name, const char *name);
    SCM scm_c_public_ref (const char *module_name, const char *name);
    SCM scm_c_private_ref (const char *module_name, const char *name);

    Like the above, but with locale-encoded C strings, for convenience.
    Module names are encoded as for `scm_c_resolve_module'.

With all these, we can happily implement Bruce Korb's
`eval-string-from-file-line' using the new `(ice-9 eval-string)', and
our code becomes:

    SCM scm_c_eval_string_from_file_line (const char *str, const char *file, 
int line)
    {
      return scm_call_5 (scm_c_public_ref ("ice-9 eval-string", "eval-string"),
                         scm_from_locale_string (the_string),
                         scm_from_locale_keyword ("file"),
                         scm_from_locale_string (file_name),
                         scm_from_locale_keyword ("line"),
                         scm_from_int (line));
     }

scm_call_N doesn't go up to 5 yet, but it probably should.  We can cache
the var if we want:

    SCM scm_c_eval_string_from_file_line (const char *str, const char *file, 
int line)
    {
      static SCM eval_string_var = SCM_BOOL_F;

      if (scm_is_false (eval_string_var))
        eval_string_var = scm_c_public_lookup ("ice-9 eval-string", 
"eval-string");

      return scm_call_5 (scm_variable_ref (eval_string_var),
                         scm_from_locale_string (the_string),
                         scm_from_locale_keyword ("file"),
                         scm_from_locale_string (file_name),
                         scm_from_locale_keyword ("line"),
                         scm_from_int (line));
     }

And we can use this strategy for easily moving over more C code to
Scheme modules as time goes on.

(If we make this convenient enough, we might be able to avoid defining
scm_c_eval_string_from_file_line ourselves, and just say to call the
function from ice-9 eval-string; but that's another discussion.)

Any thoughts?

Andy
-- 
http://wingolog.org/



reply via email to

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