This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

guile, flex, bison, c++, WinNT: want to link c++ streambuf to a scheme port


I am running guile on NT4.0 and using flex & bison as a
front end scanner and parser for a programming language that
glues together some machine learning code.  Currenly, I have
the parser to set up to read from strings or files that are
represented directly as c++ objects.  The c++ objects are
created from guile strings or a guile string representing a
filename.  Once control is turned over to my function, I
instantiate a ifstream or istrstream and use the resulting
istream for the lexer.  I am not yet currently wrapping the
c++ streams in SMOBs (I'll need to worry about finalization
a bit better than I am now at some point).  I have not found
the ctax parser to be stable or understandable (by myself)
so my approach for using flex and bison is pragmatic.

I wish to, however, hook up the same machinery using ports,
so I can leverage scheme ports.  I anticipate that this will
make my scheme-based REPL a bit easier to write.  To do
this, it seems appropriate to hook a port up to a subclass
of a streambuf so I can instantiate an using
istream(port_streambuf*).   I am able to read from generic
ports using the scm functions and determine when I have hit
a EOF.

Unfortunately, my code to link a port to a streambuf is not
working.  Although this is more of a c++ question, I am
hoping this problem has come up before and that someone
already has a solution specific to the use of ports and the
specific way that I want to use them.  Perhaps the code
would work but perhaps my virtual function for getting more
data from the data source (the input port) is not being
called.  I am using the latest cygwin release (egcs-1.1.1
g++ compiler).

cheers,
gregory

------------first try at getting this to work--my methods
are never called--------------
class scm_port_streambuf : public streambuf
{
protected:
  SCM input_port;
public:
  scm_port_streambuf(SCM port) : input_port(port) { }
  streamsize xsgetn(char *s, streamsize n)
    { return my_getn(s, n); }
  streamsize sys_read(char *buf, streamsize size)
    {
      cout << __PRETTY_FUNCTION__ << "::sys_read()" << endl;

      return 0;
    }
protected:
  streamsize my_getn(char *s, streamsize n)
    {
      cout << __PRETTY_FUNCTION__ << ": " << n << '\n';
      cout << "PORT: "; gh_display(input_port);
gh_newline();

      streamsize count = 0;
      SCM rchar;
      rchar = scm_read_char(input_port);
      while(count < n && SCM_EOF_OBJECT_P(rchar))
        {
          *s++ = gh_scm2char(rchar);
          rchar = scm_read_char(input_port);
          count++;
        }
      return count;
    }
};