Write errors to stderr/stdout when reading a Scheme file from C

47 views
Skip to first unread message

Ben Card

unread,
Oct 24, 2020, 6:58:05 PM10/24/20
to chibi-scheme
I'm trying to load a Scheme file into my C source file, but if my Scheme file has syntax errors or undefined variables, nothing happens. The program just returns nothing.

I'm using the code from the example in the manual:
obj1 = sexp_c_string(ctx, "file.scm", -1);
sexp_load(ctx, obj1, NULL);

The entire file is as follows:

#include <chibi/eval.h>

void dostuff(sexp ctx) {
  sexp_gc_var2(obj1, obj2);
  sexp_gc_preserve2(ctx, obj1, obj2);
  obj1 = sexp_c_string(ctx, "file.scm", -1);
  sexp_load(ctx, obj1, NULL);
  sexp_eval_string(ctx, "(+ 2 2)", -1, NULL);
  obj1 = sexp_intern(ctx, "my-pro", -1);
  obj2 = sexp_cons(ctx, obj1, SEXP_NULL);
  sexp_eval(ctx, obj2, NULL);
  sexp_gc_release2(ctx);
}

int main(int argc, char** argv) {
  sexp ctx;
  sexp_scheme_init();
  ctx = sexp_make_eval_context(NULL, NULL, NULL, 0, 0);
  sexp_load_standard_env(ctx, NULL, SEXP_SEVEN);
  sexp_load_standard_ports(ctx, NULL, stdin, stdout, stderr, 1);
  dostuff(ctx);
  sexp_destroy_context(ctx);
}

For example, if 'file.scm' holds
  (display "hello")
The program will print "hello", however, if the file holds:
  (display x)
The program will not print anything at all, and will exit gracefully. I would like to display something like 'Undefined variable x' to the output so users know what's wrong.

I didn't see something about it in the manual, but I might have missed / misunderstood it. I couldn't find the answer in a websearch, but I don't know what keywords I should use.

Alex Shinn

unread,
Oct 24, 2020, 7:01:05 PM10/24/20
to chibi-...@googlegroups.com
Hi Ben,

This is something of a FAQ so I'll try to make it more clear in the docs.

C has no exceptions, so you need to check the result of each operation to see if it is a Scheme exception:

  res = sexp_load(ctx, file, NULL);
  if (sexp_exceptionp(res)) {
    sexp_print_exception(ctx, res), sexp_current_error_port(ctx);
  }

--
Alex


--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chibi-scheme/6555bd96-3833-4a5f-a77b-f7b58c7d8067n%40googlegroups.com.

Ben Card

unread,
Oct 27, 2020, 6:15:21 PM10/27/20
to chibi-...@googlegroups.com
Thanks a bunch, this works rather well! Sorry for the late reply, I
was experimenting with it the past few days. Is there a way to print
the entirety of the debug message (that would usually be displayed by
running a Scheme file directly)?
sexp_debug() just returns an #<exception> object, and displaying it
prints the regular exception again (with no debugger)
> You received this message because you are subscribed to a topic in the Google Groups "chibi-scheme" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/chibi-scheme/0AG1XQHERUQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to chibi-scheme...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/chibi-scheme/CAMMPzYOCSUP9rU1%3DkRqwgPbB7puT21V58aBF02o0WGk3LVJFMA%40mail.gmail.com.



--
Regards, Ben Card.
https://muto.ca

Alex Shinn

unread,
Oct 31, 2020, 7:32:40 PM10/31/20
to chibi-...@googlegroups.com
On Wed, Oct 28, 2020 at 7:15 AM Ben Card <benben....@gmail.com> wrote:
Thanks a bunch, this works rather well! Sorry for the late reply, I
was experimenting with it the past few days. Is there a way to print
the entirety of the debug message (that would usually be displayed by
running a Scheme file directly)?
sexp_debug() just returns an #<exception> object, and displaying it
prints the regular exception again (with no debugger)

I'm afraid there's currently no debugger.
I rely on tracing and print debugging.

You can print an exception with sexp_print_exception(ctx, err, out),
and print the stack trace with sexp_stack_trace(ctx, err).

This can get tedious working from C.  I can add a runtime option
to automatically print uncaught exceptions returned to C.

[Note a debugger is not hard to implement, but I
have very little time these days for new features.]

--
Alex
 
Reply all
Reply to author
Forward
0 new messages