Hi Thomas,

I tried following Matthew's suggestions to attach my-lang and use
dynamic-require. Turns out that works pretty well! And then there's
even no need to setup collection path since everything loads from
compiled byte-code.
https://gist.github.com/shhyou/aa2adaf7e1b7d548783cee352c3230a9

The code is basically calling (dynamic-require `(file ,argv-1) #f) to
run the external script. At line 35, the code builds (list 'file
argv-1) and at line 41 the code dynamic-requires the script.

/* custom_dsl.cpp

$ raco ctool --cgc --c-mods base.c ++lib racket/base ++lib
racket/base/lang/reader ++lib racket/runtime-config ++lib my-lang
++lib my-lang/lang/reader

$ g++ -framework Foundation -I<RACKET-HEAD-REPO>/racket/include
-L<RACKET-HEAD-REPO>/racket/src/build/racket
-L<RACKET-HEAD-REPO>/racket/src/build/rktio custom_dsl.cpp -o
custom_dsl -lracket -lmzgc -lrktio -liconv

$ ./custom_dsl ./a-module.rkt

 */
#include <scheme.h>
#include <iostream>

#include "base.c"

static int run(Scheme_Env *e, int argc, char *argv[])
{
  Scheme_Object *curout;
  int i;
  Scheme_Thread *th;
  mz_jmp_buf * volatile save, fresh;

  /* Declare embedded modules in "base.c": */
  declare_modules(e);
  scheme_namespace_require(scheme_intern_symbol("my-lang"));

  curout = scheme_get_param(scheme_current_config(),
      MZCONFIG_OUTPUT_PORT);

  th = scheme_get_current_thread();

  save = th->error_buf;
  th->error_buf = &fresh;
  if (scheme_setjmp(*th->error_buf)) {
    th->error_buf = save;
    return -1; /* There was an error */
  } else {
    std::cout << "Trying to load file from \"" << argv[1] << "\"\n";
    Scheme_Object *custom_dsl_path[2];
    custom_dsl_path[0] = scheme_make_pair(
                           scheme_intern_symbol("file"),
                           scheme_make_pair(
                             scheme_make_utf8_string(argv[1]),
                             scheme_make_null()));
    custom_dsl_path[1] = scheme_make_false();
    Scheme_Object *custom_dsl = scheme_dynamic_require(2, custom_dsl_path);
    th->error_buf = save;
  }
  return 0;
}


int main(int argc, char **argv) {
  std::cout << "In cpp main\n";
  return scheme_main_setup(1, run, argc, argv);
}



;; a-module
#lang my-lang
some 'DSL that turns everything into "quotes"

Cheers,
Shu-Hung

On Wed, Jul 26, 2017 at 10:09 AM, Matthew Flatt <mfl...@cs.utah.edu> wrote:
> At Wed, 26 Jul 2017 07:54:32 -0700 (PDT), Thomas Dickerson wrote:
>> One more thing: in terms of repeatedly executing scripts, does it make sense
>> to set up and tear down the interpreter every time? Or just swap in a fresh
>> namespace?
>
> Between those two options, a fresh namespace is almost certainly
> better.
>
> Depending on how much you need to reset, you might be even better off
> with `dynamic-rerequire` from `racket/rerequire`.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to