[plt-scheme] MzScheme as embedded engine for LilyPond ?

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Feb 3 18:51:19 EST 2005

On Feb  4, Han-Wen Nienhuys wrote:
> 
> eli at barzilay.org writes:
> >   static Scheme_Type foo_tag;
> >   typedef struct foo_struct {
> >     Scheme_Object so;
> >     int x;
> >     void* p;
> >   } foo_struct;
> > 
> > and you need to initialize foo_tag like this:
> > 
> >   foo_tag = scheme_make_type("<foo>");
> 
> Does MzScheme know to mark SO because it's the first object, or
> because it's aligned?

I forgot to add that whenever you allocate foo_struct, you should set
`so' to `foo_tag'.


> Oh wait, MzScheme uses Boehm GC, and Boehm GC is fully conservative,
> so I shouldn't worry about marking.

Yes.


> OTOH, I've understood that the internal types of MzScheme apply
> FIXUP macros to their Scheme_Object, for moving about data in
> copying collectors.

There is a precise version, and macros that can be used to specify
which fields are pointers etc.  This is part of this.  You can ignore
it and be restricted to using only the conservative collector.


> Is there a reason for maintaining that difference? iow: why can't I
> define my own mark function so my types work exactly like the
> internal ones?

These annotations are not used by the conservative version.


> > > * Is there a possibility to read a single expression precisely
> > >   off an input stream?
> > 
> > You mean using `read'?  Of course.
> 
> OK. So I need to setup a port for every # expression I get.

You can use `open-input-string' to convert a string to a port.


> > >   In LilyPond input files, you can escape to Scheme, using # , for
> > >   example:
> > > 
> > >     \score { #(make-music 'NoteEvent) }
> > > 
> > >   here, the lexer gives control to the Scheme interpreter when
> > >   it reaches # . The Scheme interpreter reads up to the ) and
> > >   evals the result. The lexer then continues at the )
> > > 
> > >   Is this somehow possible with MzScheme? 
> > 
> > Yes -- there is a library (parser-tools) that provides you with
> > lex/yacc-like tools.  If you want something that makes heavy use
> > of
> 
> No, that's not what I need. We already have a fully fleshed Yacc/Lex
> system. We just need a Scheme system that jibes with it.

If you pass strings to Scheme, you can still use the preprocessor
thing to identify places where parsing should go back to your parser.


> > There is no real reader extension support, 
> 
> is it planned? 

[I don't know.]


> > but using the preprocessor it is easy to modify things when you
> > load modules (mztext which is one component has a similar
> > approach, where some command character is used to dispatch to
> > different handlers).  But if you only need to escape from LilyPond
> > to Scheme and back, then you don't need to change the syntax
> > dynamically...
> 
> Correct. So, I have to look at mztext.

But if you're talking about only escaping back to LilyPond then it
might be an overkill.


> > >     lilypond --debug foo.ly
> > > 
> > >   would use the MzScheme with locations, and 
> > > 
> > >     lilypond foo.ly
> > >  
> > >   would use the non-debugging version.  Is this possible at all?
> > 
> > mzscheme -M errortrace
> 
> This produces line/column numbers, but no stacktrace, for the above
> example, I now get 
> 
> 	byrd:~/usr/src/lilypond$ mzscheme -M errortrace -f b.scm
> 	Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc.
> 	procedure application: expected procedure, given: 0 (no arguments)
> 	/home/hanwen/usr/src/savannah/lily/lilypond/b.scm:3:2: (a)

My guess is that you have some simple code like:

  (define (foo x)
    (x))
  (define (bar x)
    (if (< x 1)
      (foo 1)
      (foo 2)))
  (bar 0)

and the failure is at a point where there is no stack to show, since
all calls are tail calls.  If you try this:

  (define (foo x)
    (list (x)))
  (define (bar x)
    (list (if (< x 1)
            (foo 1)
            (foo 2))))
  (bar 0)

then:

  mojave:~/mail eli> mzscheme -M errortrace -f ~/tmp/x.scm
  Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc.
  procedure application: expected procedure, given: 1 (no arguments)
  /usr/u/eli/tmp/x.scm:2:8: (x)
  /usr/u/eli/tmp/x.scm:2:2: (list (x))
  /usr/u/eli/tmp/x.scm:4:2: (list (if (< x 1) (foo 1) (foo 2)))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



Posted on the users mailing list.