unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] for strports.c: scm_c_eval_string_from_file_line
@ 2003-05-24 20:47 Bruce Korb
  2003-06-01 20:00 ` Marius Vollmer
  0 siblings, 1 reply; 4+ messages in thread
From: Bruce Korb @ 2003-05-24 20:47 UTC (permalink / raw)
  Cc: bkorb

Hi all,

I established my own private MTA, so I can now once again get email
through to gnu.org.  Some of you have already seen this, so here it
is again.  Sorry.

BTW, "inner_eval_string" is local to strports.c.


2003-05-24  Bruce Korb  <bkorb@gnu.org>

	* guile-core/libguile/strports.c(scm_c_eval_string_from_file_line):
	new procedure.  Facilitate error messages for applications that
	extract scheme code from their input files.
	* guile-core/libguile/strports.h: declare the procedure.

Index: strports.c
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/libguile/strports.c,v
retrieving revision 1.98
diff -u -p -r1.98 strports.c
--- strports.c  24 Apr 2003 16:02:04 -0000      1.98
+++ strports.c  24 May 2003 20:29:38 -0000
@@ -474,6 +474,37 @@ scm_eval_string (SCM string)
   return scm_eval_string_in_module (string, SCM_UNDEFINED);
 }

+/* Given a NUL-terminated string EXPR containing Scheme program text,
+   a NUL terminated source file name and a line number,
+   evaluate it, and return the result of the last expression evaluated.  */
+SCM
+scm_c_eval_string_from_file_line (const char *pzexpr, const char *pzfile,
+                                  int line)
+{
+  SCM port;
+
+  {
+    static const char z_ex[] = "c-eval-string-from-file-line";
+    SCM expr = scm_makfrom0str (pzexpr);
+    port = scm_mkstrport (SCM_INUM0, expr, SCM_OPN | SCM_RDNG, z_ex);
+  }
+
+  {
+    static SCM file = SCM_UNDEFINED;
+    scm_t_port* pt  = SCM_PTAB_ENTRY (port);
+
+    if (  (file == SCM_UNDEFINED)
+       || (strcmp (SCM_CHARS (file), pzfile) != 0) )
+      file = scm_makfrom0str (pzfile); /* only do this if name changes */
+
+    pt->line_number = line - 1; /* zero-based line numbering */
+    pt->file_name   = file;
+  }
+
+  return scm_c_call_with_current_module(
+            SCM_UNDEFINED, inner_eval_string, (void*)port );
+}
+
 static scm_t_bits
 scm_make_stptob ()
 {
Index: strports.h
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/libguile/strports.h,v
retrieving revision 1.26
diff -u -p -r1.26 strports.h
--- strports.h  5 Apr 2003 19:10:22 -0000       1.26
+++ strports.h  24 May 2003 20:29:38 -0000
@@ -56,6 +56,8 @@ SCM_API SCM scm_c_eval_string_in_module
 SCM_API SCM scm_eval_string (SCM string);
 SCM_API SCM scm_eval_string_in_module (SCM string, SCM module);
 SCM_API void scm_init_strports (void);
+SCM_API SCM scm_c_eval_string_from_file_line (const char *expr,
+                       const char *file, int lineno);

 #endif  /* SCM_STRPORTS_H */


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] for strports.c: scm_c_eval_string_from_file_line
  2003-05-24 20:47 [PATCH] for strports.c: scm_c_eval_string_from_file_line Bruce Korb
@ 2003-06-01 20:00 ` Marius Vollmer
  2003-06-01 20:38   ` Bruce Korb
  0 siblings, 1 reply; 4+ messages in thread
From: Marius Vollmer @ 2003-06-01 20:00 UTC (permalink / raw)
  Cc: guile development

Bruce Korb <bkorb@veritas.com> writes:

> 2003-05-24  Bruce Korb  <bkorb@gnu.org>
> 
> 	* guile-core/libguile/strports.c(scm_c_eval_string_from_file_line):
> 	new procedure.  Facilitate error messages for applications that
> 	extract scheme code from their input files.

Hmm, I'm not sure whether we should provide such a function as a ready
made unit.  Evaluation from strings (maybe with embedded new lines,
maybe more than one string, etc) while maintaining the line number,
the current module, etc, might come in several variations, I think.

We should provide the building blocks so that people can easily
implement what they want cleanly so that
scm_c_eval_string_from_file_line is very easy to implement.

What you can do right now is, for example (and untested, sorry):

  void
  scm_c_primitive_load_from_string (const char *str,
                                    const char *filename, int line)
  {  
    SCM port, exp;

    port = scm_open_input_string (scm_str2string (str));
    scm_set_port_file_name_x (port, scm_str2string (filename));
    scm_set_port_line_x (port, scm_int2num (line));

    while (!SCM_EOF_OBJECT_P (exp = scm_read (port)))
      scm_primitive_eval_x (exp);
  }

I think this quite straightforward, no?  But we might want to offer it
ready-made, anyway.  Opinions?

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] for strports.c: scm_c_eval_string_from_file_line
  2003-06-01 20:00 ` Marius Vollmer
@ 2003-06-01 20:38   ` Bruce Korb
  2003-06-09 19:53     ` Marius Vollmer
  0 siblings, 1 reply; 4+ messages in thread
From: Bruce Korb @ 2003-06-01 20:38 UTC (permalink / raw)
  Cc: guile development

Marius Vollmer wrote:

>   void
>   scm_c_primitive_load_from_string (const char *str,
>                                     const char *filename, int line)
>   {
>     SCM port, exp;
> 
>     port = scm_open_input_string (scm_str2string (str));
>     scm_set_port_file_name_x (port, scm_str2string (filename));
>     scm_set_port_line_x (port, scm_int2num (line));
> 
>     while (!SCM_EOF_OBJECT_P (exp = scm_read (port)))
>       scm_primitive_eval_x (exp);
>   }
> 
> I think this quite straightforward, no?

No.

> But we might want to offer it ready-made, anyway.  Opinions?

Yes.  It is pretty close to what I wound up with, but I don't
track the preferred way to do things.  For Guile 1.4,
``scm_primitive_eval_x'' gets #define-d into ``scm_eval_x''
and it works.  I also try to avoid gratuitous creations of
string SCM's since the file name is generally invariant.
You omitted returning the final value, which is important :-).
It's easier to ignore if the caller doesn't want it.  And, no,
I didn't find that the research required to do this was obvious.
In fact, there is no ``scm_set_port_file_name_x'' in 1.6.3, so
you must be talking 1.7 here.  I'm still working with 1.4.
Therefore, I must directly assign to ``file_name''.  I may as
well assign the line number, too.

I'd rather it was ready made so I can just #define
my ``ag_scm_c_eval_string_from_file_line'' into
``scm_c_primitive_load_from_string''.  You might tweak it
a bit so a NULL file pointer bypasses the scm_set_port_file_name_x
call.  I'd also use "file_line" in the name, but that is your call.


EXPORT SCM
ag_scm_c_eval_string_from_file_line( tCC* pzExpr, tCC* pzFile, int line )
{
    SCM port;

    {
        tSCC zEx[] = "eval-string-from-file-line";
        SCM  expr  = scm_makfrom0str( pzExpr );
        port = scm_mkstrport( SCM_INUM0, expr, SCM_OPN | SCM_RDNG, zEx );
    }

    {
        static SCM file = SCM_UNDEFINED;
        scm_t_port* pt;

        if (  (file == SCM_UNDEFINED)
           || (strcmp( SCM_CHARS( file ), pzFile ) != 0) )
            file = scm_makfrom0str( pzFile );

        pt = SCM_PTAB_ENTRY( port );
        pt->line_number = line - 1;
        pt->file_name   = file;
    }

    {
        SCM ans = SCM_UNSPECIFIED;

        for (;;) {
            SCM form = scm_read( port );
            if (SCM_EOF_OBJECT_P( form ))
                break;
            ans = scm_primitive_eval_x( form );
        }

        return ans; // return the last answer
    }
}


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] for strports.c: scm_c_eval_string_from_file_line
  2003-06-01 20:38   ` Bruce Korb
@ 2003-06-09 19:53     ` Marius Vollmer
  0 siblings, 0 replies; 4+ messages in thread
From: Marius Vollmer @ 2003-06-09 19:53 UTC (permalink / raw)
  Cc: guile development

Bruce Korb <bkorb@veritas.com> writes:

> Marius Vollmer wrote:
> 
> >   void
> >   scm_c_primitive_load_from_string (const char *str,
> >                                     const char *filename, int line)
> >   {
> >     SCM port, exp;
> > 
> >     port = scm_open_input_string (scm_str2string (str));
> >     scm_set_port_file_name_x (port, scm_str2string (filename));
> >     scm_set_port_line_x (port, scm_int2num (line));
> > 
> >     while (!SCM_EOF_OBJECT_P (exp = scm_read (port)))
> >       scm_primitive_eval_x (exp);
> >   }
> > 
> > I think this quite straightforward, no?
> 
> No.

Err, I mean, once you see how it is done.  The manual can be improved,
I'm sure.

> [...]  I also try to avoid gratuitous creations of
> string SCM's since the file name is generally invariant.
> You omitted returning the final value, which is important :-).

As you see, there are quite some variants on this theme, and I think
the above code is clean enough to allow for easy modification.  There
is no black magic in it.

> It's easier to ignore if the caller doesn't want it.  And, no,
> I didn't find that the research required to do this was obvious.
> In fact, there is no ``scm_set_port_file_name_x'' in 1.6.3, so
> you must be talking 1.7 here.

Oops, it is "scm_set_port_filename_x".

> I'm still working with 1.4.  Therefore, I must directly assign to
> ``file_name''.  I may as well assign the line number, too.

One more reason to join the happy Guile 1.6 club...

> I'd rather it was ready made so I can just #define my
> ``ag_scm_c_eval_string_from_file_line'' into
> ``scm_c_primitive_load_from_string''.  You might tweak it a bit so a
> NULL file pointer bypasses the scm_set_port_file_name_x call.  I'd
> also use "file_line" in the name, but that is your call.

Given these minor variations, I suggest you just use the function from
above.  That way, you get exactly what you want.

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-06-09 19:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-24 20:47 [PATCH] for strports.c: scm_c_eval_string_from_file_line Bruce Korb
2003-06-01 20:00 ` Marius Vollmer
2003-06-01 20:38   ` Bruce Korb
2003-06-09 19:53     ` Marius Vollmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).