This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Loading macros from java


...I've defined a file prequel.scm with a simple function and a macro:

(define (% a b)
(if (= b 0) 0
(/ a b)))
(define-syntax iflte
(syntax-rules ()
((iflte a b c d) (if (<= a b) c d))))


The function % is a version of division where division by zero is allowed. The macro represents 'if less than or equal' - the call (iflte 10 10 (display "yes") (display "no")) produces a 'yes' on the output. It has to be defined as a macro because only one of the third or fourth arguments will be evaluated. Thus (iflte 10 10 (+ 3 4) (/ 10 0)) is meaningful and produces 7 rather than an exception.

If I run kawa by hand these both work perfectly. If I execute the following java fragment:

try {
     Scheme scm = new Scheme();
     Object x = scm.eval("(load \"prequel.scm\")");
     x = scm.eval("(% 10 0)");
     System.out.println("x = " + x);
   } catch (Throwable ex) {
     ex.printStackTrace();
   }

it works perfectly.

However

try {
     Scheme scm = new Scheme();
     Object x = scm.eval("(load \"prequel.scm\")");
     x = scm.eval("(iflte 10 0 (+ 3 4) (/ 10 0))");
     System.out.println("x = " + x);
   } catch (Throwable ex) {
     ex.printStackTrace();
   }

produces the exception:

java.lang.ClassCastException: kawa.lang.Macro
       at gnu.expr.ApplyExp.apply(ApplyExp.java:70)
       at gnu.expr.ModuleExp.evalModule(ModuleExp.java:156)
       at gnu.expr.Language.eval(Language.java:765)
       at gnu.expr.Language.eval(Language.java:704)
       at gnu.expr.Language.eval(Language.java:686)
       at net.dwelly.tpsoup0.Main.<init>(Main.java:64)
       at net.dwelly.tpsoup0.Main.main(Main.java:77)

Is this a bug or am I doing something stupid ? is there a workaround ? The actual prequel file will probably end up quite large so its not really practical to write out a set of strings from Java.

Just out of curiosity I also tried:

Object x = scm.eval("(define-syntax iflte (syntax-rules () ((iflte a b c d) (if (<= a b) c d)))) (iflte 10 10 (+ 3 4) (/ 10 0))");

as well, which just puts the macro definition and the call immediately after one another. Its kind of hard to read but this form does work.

Best

Andy Dwelly



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