Summary

problems with building json egg in c5 egg repo

Metadata

Description

Building the json egg results in:

 Error: (json.scm:109) - toplevel definition of `white' in non-toplevel context

The code looks like this:

       (define parser
 	(packrat-parser (begin
 			  (define (white results) ...

Changing the `begin` to `let ()` results in another, more obscure error. Apparently the proper treatment of `define` inside `begin` inside `let` is not correctly expanded (`packrat-parser` seems to expand into a let body).

Changes and comments

[2017-10-19 16:02:08 UTC] sjamaan wrote:

I believe this is a bug in how packrat works:

 (import packrat)
 
 (define plus-or-minus
   (packrat-parser
    (begin
      (print "here be a define")
      expr)
    (expr ((a <- '+)
           (print "plus: " a))
          ((a <- '-)
           (print "minus: " a)))))

Compiling this with csc -debug 2 produces:

 [canonicalized]
 (##core#callunit library)
 
 (##core#callunit eval)
 
 (##core#undefined)
 
 (##core#undefined)
 
 (##core#undefined)
 
 (chicken.load#load-extension 'packrat '(packrat#) 'require)
 
 (set! minus
   (let ()
     (let ((expr31 (##core#undefined)))
       (let ((t58 (set! expr31
                    (##core#lambda
                      (results2632)
                      (packrat#results->result
                        results2632
                        'expr
                        (##core#lambda
                          ()
                          (let ((g3334 (packrat#packrat-or
                                         (packrat#packrat-check-base
                                           '+
                                           (##core#lambda
                                             (a42)
                                             (##core#lambda
                                               (results4446)
                                               (packrat#make-result
                                                 (chicken.base#print '"plus: " a42)
                                                 results4446))))
                                         (packrat#packrat-check-base
                                           '-
                                           (##core#lambda
                                             (a53)
                                             (##core#lambda
                                               (results5557)
                                               (packrat#make-result
                                                 (chicken.base#print '"minus: " a53)
                                                 results5557)))))))
                            (g3334 results2632))))))))
         (let ((t59 (chicken.base#print '"here be a define"))) expr31)))))
 
 ((##sys#implicit-exit-handler))

As you can see, the expansion of the internal define of json would look something like:

 (set! parser
   (let ()
      (let (...) ;; parser definitions used by begin
        (let ((t59 (begin (define (white results) ...)))) ... expr))))

This is clearly incorrect: you can't put define in the value expression of a let-variable.

[2017-10-19 16:10:07 UTC] sjamaan wrote:

On second thought, it looks like this inner let that is the source of t59 is introduced by the compiler (through ##sys#canonicalize-body), so I'm not 100% sure it is packrat doing things wrong...

[2017-10-19 16:27:38 UTC] sjamaan changed status from new to closed

[2017-10-19 16:27:38 UTC] sjamaan set resolution to invalid

[2017-10-19 16:27:38 UTC] sjamaan wrote:

OK never mind, this was a complete red herring. The json egg didn't work because it contained use instead of import expressions, causing the packrat-parser call to be seen not as a module but as a procedure call, which means the defines were indeed "misplaced".