Skip to content

Commit

Permalink
work on macro changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jul 22, 2012
1 parent c2cbee0 commit 605bc65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ macro gensym(names...)
return blk
end

syntax_escape(e) = expr(:escape, {e})

## expressions ##

expr(hd::Symbol, args::ANY...) = Expr(hd, {args...}, Any)
Expand Down
19 changes: 16 additions & 3 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static char flisp_system_image[] = {
};

extern fltype_t *iostreamtype;
static fltype_t *jvtype;

static jl_value_t *scm_to_julia(value_t e);
static value_t julia_to_scm(jl_value_t *v);
Expand Down Expand Up @@ -71,8 +72,21 @@ value_t fl_invoke_julia_macro(value_t *args, uint32_t nargs)
// so the preserved value stack is popped there.
jl_gc_preserve(result);
value_t scm = julia_to_scm(result);
fl_gc_handle(&scm);
value_t scmresult;
jl_module_t *defmod = f->linfo->module;
if (defmod == jl_current_module) {
scmresult = fl_cons(scm, FL_F);
}
else {
value_t opaque = cvalue(jvtype, sizeof(void*));
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)defmod;
scmresult = fl_cons(scm, opaque);
}
fl_free_gc_handles(1);

JL_GC_POP();
return scm;
return scmresult;
}

static builtinspec_t julia_flisp_ast_ext[] = {
Expand All @@ -81,8 +95,6 @@ static builtinspec_t julia_flisp_ast_ext[] = {
{ NULL, NULL }
};

static fltype_t *jvtype;

DLLEXPORT void jl_init_frontend(void)
{
fl_init(2*512*1024);
Expand Down Expand Up @@ -433,6 +445,7 @@ jl_value_t *jl_parse_next(int *plineno)
value_t a = car_(c);
if (isfixnum(a)) {
*plineno = numval(a);
//ios_printf(ios_stderr, " line %d\n", *plineno);
return scm_to_julia(cdr_(c));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ jl_value_t *jl_toplevel_eval(jl_value_t *v)
// repeatedly call jl_parse_next and eval everything
void jl_parse_eval_all(char *fname)
{
//ios_printf(ios_stderr, "***** loading %s\n", fname);
int lineno=0;
jl_value_t *fn=NULL, *ln=NULL, *form=NULL;
JL_GC_PUSH(&fn, &ln, &form);
Expand Down

4 comments on commit 605bc65

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting! (Probably only to me though.)

@JeffBezanson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is going to be ready real soon. My biggest concern is what to call syntax_escape. It will be used like quote $syntax_escape(var)(x) end but that is really too verbose. We could pick a short name like esc or even a single letter, and arrange it to be a local function within macro definitions to avoid polluting the global namespace.

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like esc but I worry about having it local to macro definitions since it's pretty standard to define the macro transformation as a function for debugging purposes and then call that function from the macro. Some need to do that could be alleviated by providing a way to call macro transformers directly, but there are still cases where one wants a separate function, e.g. when there are additional arguments that modify the way the code is generated (e.g. _jl_interp_parse). Of course, that's relatively rare, so maybe it's ok to have to explicitly import/prefix esc in those cases. A sensible way to do this might be to have a Macro module that provides macro-related tools like esc and just have that be automatically imported in the body of macros. Speaking of which, can one import into any scope or only top-level? Importing into a function body seems like a useful thing to be able to do.

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, while you're at it, can I make a plea for changing macro functions to being generic. This would make me much happier.

Please sign in to comment.