Skip to content

Commit

Permalink
Fix Splatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed Aug 23, 2014
1 parent 50fa094 commit 478b0cd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jl_sym_t *global_sym; jl_sym_t *tuple_sym;
jl_sym_t *dot_sym; jl_sym_t *newvar_sym;
jl_sym_t *boundscheck_sym; jl_sym_t *copyast_sym;
jl_sym_t *simdloop_sym; jl_sym_t *arrow_sym;
jl_sym_t *ldots_sym;

typedef struct {
int64_t a;
Expand Down
19 changes: 15 additions & 4 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,11 +944,22 @@ DLLEXPORT jl_function_t *jl_instantiate_staged(jl_methlist_t *m, jl_tuple_t *tt)
oldast = (jl_expr_t*)jl_uncompress_ast(m->func->linfo, m->func->linfo->ast);
assert(oldast->head == lambda_sym);
ex = jl_exprn(arrow_sym, 2);
jl_expr_t *argnames = jl_exprn(tuple_sym, jl_tuple_len(tt));
jl_cellset(ex->args, 0, argnames);
jl_array_t *oldargnames = (jl_array_t*)jl_cellref(oldast->args,0);
for (size_t i = 0; i < jl_tuple_len(tt); ++i) {
jl_cellset(argnames->args,i,jl_cellref(oldargnames,i));
jl_expr_t *argnames = jl_exprn(tuple_sym, jl_array_len(oldargnames));
jl_cellset(ex->args, 0, argnames);
for (size_t i = 0; i < jl_array_len(oldargnames); ++i) {
jl_value_t *arg = jl_cellref(oldargnames,i);
if (jl_is_expr(arg)) {
assert(((jl_expr_t*)arg)->head == colons_sym);
arg = jl_cellref(((jl_expr_t*)arg)->args,0);
assert(jl_is_symbol(arg));
jl_expr_t *dd_expr = jl_exprn(ldots_sym,1);
jl_cellset(dd_expr->args,0,arg);
jl_cellset(argnames->args,i,dd_expr);
} else {
assert(jl_is_symbol(arg));
jl_cellset(argnames->args,i,arg);
}
}
jl_cellset(ex->args, 1, jl_apply(m->func, tt->data, jl_tuple_len(tt)));
func = (jl_function_t*)jl_toplevel_eval(jl_expand((jl_value_t*)ex));
Expand Down
1 change: 1 addition & 0 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3258,6 +3258,7 @@ void jl_init_types(void)
copyast_sym = jl_symbol("copyast");
simdloop_sym = jl_symbol("simdloop");
arrow_sym = jl_symbol("->");
ldots_sym = jl_symbol("...");
}

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ extern jl_sym_t *compositetype_sym; extern jl_sym_t *type_goto_sym;
extern jl_sym_t *global_sym; extern jl_sym_t *tuple_sym;
extern jl_sym_t *boundscheck_sym; extern jl_sym_t *copyast_sym;
extern jl_sym_t *simdloop_sym; extern jl_sym_t *arrow_sym;

extern jl_sym_t *ldots_sym;

// object accessors -----------------------------------------------------------

Expand Down
31 changes: 30 additions & 1 deletion test/staged.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,33 @@ end
tinline(a,b) = t1(a,b)

@test !isa(tinline(1,2),Expr)
@test tinline(1,0.5) == 1.5
@test tinline(1,0.5) == 1.5

stagedfunction splat(a,b...)
:( ($a,$b,a,b) )
end

@test splat(1,2,3) == (Int64,(Int64,Int64),1,(2,3))

A = rand(5,5,3);
B = slice(A, 1:3, 2, 1:3);
stagedfunction mygetindex(S::SubArray, indexes::Real...)
T, N, A, I = S.parameters
if N != length(indexes)
error("Wrong number of indexes supplied")
end
NP = length(I)
indexexprs = Array(Expr, NP)
j = 1
for i = 1:NP
if I[i] == Int
indexexprs[i] = :(S.indexes[$i])
else
indexexprs[i] = :(S.indexes[$i][indexes[$j]])
j += 1
end
end
ex = :(S.parent[$(indexexprs...)])
ex
end
@test mygetindex(B,2,2) == A[2,2,2]

0 comments on commit 478b0cd

Please sign in to comment.