Skip to content

Commit

Permalink
tie up a few loose ends in keyword args, add a couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Apr 2, 2013
1 parent e7acd70 commit 0782b39
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 21 deletions.
4 changes: 3 additions & 1 deletion base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,9 @@ function abstract_interpret(e::Expr, vtypes, sv::StaticVarInfo)
elseif is(e.head,:gotoifnot)
abstract_eval(e.args[1], vtypes, sv)
elseif is(e.head,:method)
return StateUpdate(e.args[1], Function, vtypes)
fname = e.args[1]
if isa(fname,Expr); fname = fname.args[1]; end
return StateUpdate(fname, Function, vtypes)
end
return vtypes
end
Expand Down
2 changes: 1 addition & 1 deletion src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jl_sym_t *null_sym; jl_sym_t *body_sym;
jl_sym_t *macro_sym; jl_sym_t *method_sym;
jl_sym_t *enter_sym; jl_sym_t *leave_sym;
jl_sym_t *exc_sym; jl_sym_t *error_sym;
jl_sym_t *static_typeof_sym;
jl_sym_t *static_typeof_sym; jl_sym_t *kw_sym;
jl_sym_t *new_sym; jl_sym_t *using_sym;
jl_sym_t *const_sym; jl_sym_t *thunk_sym;
jl_sym_t *anonymous_sym; jl_sym_t *underscore_sym;
Expand Down
29 changes: 20 additions & 9 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1854,24 +1854,35 @@ static Value *emit_expr(jl_value_t *expr, jl_codectx_t *ctx, bool isboxed,
}
}
else if (head == method_sym) {
jl_value_t *mn;
if (jl_is_symbolnode(args[0])) {
mn = (jl_value_t*)jl_symbolnode_sym(args[0]);
jl_value_t *mn = args[0];
bool iskw = false;
if (jl_is_expr(mn) && ((jl_expr_t*)mn)->head == kw_sym) {
iskw = true;
mn = jl_exprarg(mn,0);
}
else {
mn = args[0];
if (jl_is_symbolnode(mn)) {
mn = (jl_value_t*)jl_symbolnode_sym(mn);
}
assert(jl_is_symbol(mn));
int last_depth = ctx->argDepth;
Value *name = literal_pointer_val(mn);
jl_binding_t *bnd = NULL;
Value *bp;
if (is_global((jl_sym_t*)mn, ctx)) {
bnd = jl_get_binding_for_method_def(ctx->module, (jl_sym_t*)mn);
bp = literal_pointer_val(&bnd->value, jl_ppvalue_llvmt);
if (iskw) {
Value *theF = emit_expr(mn, ctx);
// fenv = theF->env
Value *fenv = emit_nthptr(theF, 2);
// bp = &((jl_methtable_t*)fenv)->kwsorter
bp = emit_nthptr_addr(fenv, 7);
}
else {
bp = var_binding_pointer((jl_sym_t*)mn, &bnd, false, ctx);
if (is_global((jl_sym_t*)mn, ctx)) {
bnd = jl_get_binding_for_method_def(ctx->module, (jl_sym_t*)mn);
bp = literal_pointer_val(&bnd->value, jl_ppvalue_llvmt);
}
else {
bp = var_binding_pointer((jl_sym_t*)mn, &bnd, false, ctx);
}
}
Value *a1 = emit_expr(args[1], ctx);
make_gcroot(boxed(a1), ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static jl_value_t *eval(jl_value_t *e, jl_value_t **locals, size_t nl)
jl_sym_t *fname = (jl_sym_t*)args[0];
jl_value_t **bp=NULL;
jl_binding_t *b=NULL;
if (jl_is_expr(fname) && ((jl_expr_t*)fname)->head == jl_symbol("kw")) {
if (jl_is_expr(fname) && ((jl_expr_t*)fname)->head == kw_sym) {
fname = (jl_sym_t*)jl_exprarg(fname, 0);
assert(jl_is_symbol(fname));
jl_function_t *gf = (jl_function_t*)eval((jl_value_t*)fname, locals, nl);
Expand Down
1 change: 1 addition & 0 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2762,4 +2762,5 @@ void jl_init_types(void)
type_goto_sym = jl_symbol("type_goto");
toplevel_sym = jl_symbol("toplevel");
tuple_sym = jl_symbol("tuple");
kw_sym = jl_symbol("kw");
}
4 changes: 2 additions & 2 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
(define (assignment? e)
(and (pair? e) (eq? (car e) '=)))

(define (assertion? e)
(define (typedecl? e)
(and (length= e 3) (eq? (car e) |::|) (symbol? (cadr e))))

(define unary-ops '(+ - ! ~ |<:| |>:|))
Expand Down Expand Up @@ -1146,7 +1146,7 @@
(kws args) (separate (lambda (x)
(and (assignment? x)
(or (symbol? (cadr x))
(assertion? (cadr x)))))
(typedecl? (cadr x)))))
argl)
(if (null? kws)
args
Expand Down
3 changes: 1 addition & 2 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,7 @@
(receive
(vararg req) (separate vararg? argl)
(optional-positional-defs name sparams req opt dfl body
(cons kw
(append req opt vararg))
(cons kw (append req opt vararg))
`(parameters (... ,(gensy))))))
;; optional positional only
(receive
Expand Down
2 changes: 1 addition & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ extern jl_sym_t *null_sym; extern jl_sym_t *body_sym;
extern jl_sym_t *macro_sym; extern jl_sym_t *method_sym;
extern jl_sym_t *enter_sym; extern jl_sym_t *leave_sym;
extern jl_sym_t *exc_sym; extern jl_sym_t *new_sym;
extern jl_sym_t *static_typeof_sym;
extern jl_sym_t *static_typeof_sym; extern jl_sym_t *kw_sym;
extern jl_sym_t *const_sym; extern jl_sym_t *thunk_sym;
extern jl_sym_t *anonymous_sym; extern jl_sym_t *underscore_sym;
extern jl_sym_t *abstracttype_sym; extern jl_sym_t *bitstype_sym;
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
JULIAHOME = $(abspath ..)
include ../Make.inc

TESTS = core numbers strings unicode corelib hashing remote iostring \
TESTS = core keywordargs numbers strings unicode corelib hashing remote iostring \
arrayops linalg blas fft dct sparse bitarray random math functional bigint \
sorting statistics spawn parallel suitesparse arpack bigfloat file zlib image \
all git pkg
Expand Down
20 changes: 20 additions & 0 deletions test/keywordargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,23 @@ opkwf1(a=0,b=1;k=2) = (a,b,k)

# dictionaries as keywords
@test kwf1(4; {:hundreds=>9, :tens=>5}...) == 954

# with inner function
let
function kwf_maker()
f(;k=0) = 2k+1
end
kwf5 = kwf_maker()
@test kwf5() == 1
@test kwf5(k=2) == 5
@test_fails kwf5(1)
end

# with every feature!
extravagant_args(x,y=0,rest...;color="blue",kw...) =
(x,y,rest,color,kwf1(6;tens=8,kw...))

@test isequal(extravagant_args(1), (1,0,(),"blue",86))
@test isequal(extravagant_args(1;hundreds=7), (1,0,(),"blue",786))
@test isequal(extravagant_args(1,2,3;{:color=>"red", :hundreds=>3}...),
(1,2,(3,),"red",386))
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
testnames = ["core", "numbers", "strings", "unicode", "corelib", "hashing",
"remote", "iostring", "arrayops", "linalg", "blas", "fft",
"dct", "sparse", "bitarray", "random", "math", "functional",
testnames = ["core", "keywordargs", "numbers", "strings", "unicode", "corelib",
"hashing", "remote", "iostring", "arrayops", "linalg", "blas",
"fft", "dct", "sparse", "bitarray", "random", "math", "functional",
"bigint", "sorting", "statistics", "spawn", "parallel",
"suitesparse", "arpack", "bigfloat", "file", "zlib", "image",
"perf"]
Expand Down

0 comments on commit 0782b39

Please sign in to comment.