﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	difficulty
113	Crunch egg: bug in define-crunch-primitives / define-crunch-callback	Jeronimo Pellegrini		"In line 78 of crunch.scm , pair? is called with two arguments:
   
{{{
    (and (pair? (car p) (c (r '+>) (cadr p)))))))
}}}

The version of Crunch is svn trunk (but that line is the same in version 0.7.2). I tested it with Chicken from git, commit 43a11082ddc109bfe1b4fdb400b6079ca64d024a

The following is a report of my attempt to debug the problem; since I'm new to Chicken Scheme, I may have made several silly mistakes along the way...

The following code (crunch-bug.scm) does not compile (I suppose I undertood how to use define-crunch-primitives and define-crunch-callback):

{{{
(cond-expand
 (crunched (import crunch))
 (else
  (define-syntax crunch
    (syntax-rules ()
      ((_ body ...) (begin (display ""Crunch not working"") body ...) ) ) ) ) ) 

(define-crunch-primitives
  ((foo int) -> void))

(define-crunch-callback
  (foo (int i)) void
  (display (format ""Here's your int: ~a ~%"" i)))

(crunch
  (define work
   (lambda (max)
     (do ((k 0 (+ k 1)))
         ((>= k max))
       (foo k))))    )

(work 10) 
}}}

I tried fixing line 78 of crunch.scm, changing it to:

{{{
    (and (pair? (car p) (c (r '+>) (cadr p)))))))
}}}

But then the code and declarations for the function (foo in the example above) are not generated properly in the C++ file:

{{{
$ csc -c++ -D crunched crunch-bug.scm

Warning: reference to variable `unquote' possibly unintended crunch-bug.cpp: In function ‘type37 f36(type35)’:
crunch-bug.cpp:58: error: ‘crunch_callback14’ was not declared in this scope crunch-bug.cpp: In function ‘void C_toplevel(long int, long int, long int)’:
}}}

That function is used in line 58 of the C++ file (I know line numbers in generated C++ could perhaps vary from my setup to others, but this is just to show the relative order of lines):
{{{
type55 t56 = crunch_callback14(t54); 
}}}

but is only declared in line 76:
{{{
C_externexport  void  crunch_callback14(int t0);
}}}

And defined in line 135:
{{{
void  crunch_callback14(int t0){
...
}
}}}

OK, so I manually added a declaration at the top of the file:
{{{
C_externexport  void  crunch_callback14(int t0);
}}}

and got this error:

{{{
crunch-bug.cpp: In function ‘type37 f36(type35)’:
crunch-bug.cpp:62: error: conversion from ‘void’ to non-scalar type ‘type55’ requested
}}}

OK. It won't return anything, so I changed the function call from

{{{
type55 t56 = crunch_callback14(t54);
}}}

to:

{{{
crunch_callback14(t54);
}}}

(And verified that t56 is *not* referenced at all in the C++ code).

And, HEY, it compiles!!!

But the generated binary doesn't work:

{{{
$ g++ crunch-bug.o -lchicken 
$ ./crunch-bug
Error: unbound variable: r

	Call history:

	r			<--
}}}

I suppose this is the r here, in line 60 of crunch.scm:

{{{
(define-syntax (define-crunch-primitives x r c)
}}}

But I wasn't able to debug further.
"	defect	closed	minor		extensions	4.2.x	fixed	crunch		
