Make 'lambda(...) -> ...' parse as a type
This commit is contained in:
parent
4e9b8a28f7
commit
eaf9e05611
@ -25,13 +25,10 @@ expected to return.
|
|||||||
|
|
||||||
## Closures
|
## Closures
|
||||||
|
|
||||||
FIXME Either move entirely to fn~/fn@ nomenclature, or fix compiler to
|
|
||||||
accept lambda as a type
|
|
||||||
|
|
||||||
Normal Rust functions (declared with `fn`) do not close over their
|
Normal Rust functions (declared with `fn`) do not close over their
|
||||||
environment. A `lambda` expression can be used to create a closure.
|
environment. A `lambda` expression can be used to create a closure.
|
||||||
|
|
||||||
fn make_plus_function(x: int) -> fn@(int) -> int {
|
fn make_plus_function(x: int) -> lambda(int) -> int {
|
||||||
lambda(y: int) -> int { x + y }
|
lambda(y: int) -> int { x + y }
|
||||||
}
|
}
|
||||||
let plus_two = make_plus_function(2);
|
let plus_two = make_plus_function(2);
|
||||||
|
@ -299,7 +299,7 @@ fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ {
|
|||||||
ret ast::ty_fn(proto, inputs.node, ret_ty, ret_style, constrs);
|
ret ast::ty_fn(proto, inputs.node, ret_ty, ret_style, constrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_ty_obj(p: parser, &hi: uint) -> ast::ty_ {
|
fn parse_ty_obj(p: parser) -> ast::ty_ {
|
||||||
fn parse_method_sig(p: parser) -> ast::ty_method {
|
fn parse_method_sig(p: parser) -> ast::ty_method {
|
||||||
let flo = p.get_lo_pos();
|
let flo = p.get_lo_pos();
|
||||||
let proto: ast::proto = parse_method_proto(p);
|
let proto: ast::proto = parse_method_proto(p);
|
||||||
@ -320,7 +320,6 @@ fn parse_ty_obj(p: parser, &hi: uint) -> ast::ty_ {
|
|||||||
}
|
}
|
||||||
let meths =
|
let meths =
|
||||||
parse_seq(token::LBRACE, token::RBRACE, none, parse_method_sig, p);
|
parse_seq(token::LBRACE, token::RBRACE, none, parse_method_sig, p);
|
||||||
hi = meths.span.hi;
|
|
||||||
ret ast::ty_obj(meths.node);
|
ret ast::ty_obj(meths.node);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,7 +474,6 @@ fn parse_ret_ty(p: parser, n_args: uint) -> (ast::ret_style, @ast::ty) {
|
|||||||
|
|
||||||
fn parse_ty(p: parser, colons_before_params: bool) -> @ast::ty {
|
fn parse_ty(p: parser, colons_before_params: bool) -> @ast::ty {
|
||||||
let lo = p.get_lo_pos();
|
let lo = p.get_lo_pos();
|
||||||
let hi = lo;
|
|
||||||
let t: ast::ty_;
|
let t: ast::ty_;
|
||||||
// FIXME: do something with this
|
// FIXME: do something with this
|
||||||
|
|
||||||
@ -518,7 +516,6 @@ fn parse_ty(p: parser, colons_before_params: bool) -> @ast::ty {
|
|||||||
} else if p.peek() == token::LPAREN {
|
} else if p.peek() == token::LPAREN {
|
||||||
p.bump();
|
p.bump();
|
||||||
if p.peek() == token::RPAREN {
|
if p.peek() == token::RPAREN {
|
||||||
hi = p.get_hi_pos();
|
|
||||||
p.bump();
|
p.bump();
|
||||||
t = ast::ty_nil;
|
t = ast::ty_nil;
|
||||||
} else {
|
} else {
|
||||||
@ -530,54 +527,44 @@ fn parse_ty(p: parser, colons_before_params: bool) -> @ast::ty {
|
|||||||
if vec::len(ts) == 1u {
|
if vec::len(ts) == 1u {
|
||||||
t = ts[0].node;
|
t = ts[0].node;
|
||||||
} else { t = ast::ty_tup(ts); }
|
} else { t = ast::ty_tup(ts); }
|
||||||
hi = p.get_hi_pos();
|
|
||||||
expect(p, token::RPAREN);
|
expect(p, token::RPAREN);
|
||||||
}
|
}
|
||||||
} else if p.peek() == token::AT {
|
} else if p.peek() == token::AT {
|
||||||
p.bump();
|
p.bump();
|
||||||
let mt = parse_mt(p);
|
t = ast::ty_box(parse_mt(p));
|
||||||
hi = mt.ty.span.hi;
|
|
||||||
t = ast::ty_box(mt);
|
|
||||||
} else if p.peek() == token::TILDE {
|
} else if p.peek() == token::TILDE {
|
||||||
p.bump();
|
p.bump();
|
||||||
let mt = parse_mt(p);
|
t = ast::ty_uniq(parse_mt(p));
|
||||||
hi = mt.ty.span.hi;
|
|
||||||
t = ast::ty_uniq(mt);
|
|
||||||
} else if p.peek() == token::BINOP(token::STAR) {
|
} else if p.peek() == token::BINOP(token::STAR) {
|
||||||
p.bump();
|
p.bump();
|
||||||
let mt = parse_mt(p);
|
t = ast::ty_ptr(parse_mt(p));
|
||||||
hi = mt.ty.span.hi;
|
|
||||||
t = ast::ty_ptr(mt);
|
|
||||||
} else if p.peek() == token::LBRACE {
|
} else if p.peek() == token::LBRACE {
|
||||||
let elems =
|
let elems =
|
||||||
parse_seq(token::LBRACE, token::RBRACE, some(token::COMMA),
|
parse_seq(token::LBRACE, token::RBRACE, some(token::COMMA),
|
||||||
parse_ty_field, p);
|
parse_ty_field, p);
|
||||||
hi = elems.span.hi;
|
let hi = elems.span.hi;
|
||||||
t = ast::ty_rec(elems.node);
|
t = ast::ty_rec(elems.node);
|
||||||
if p.peek() == token::COLON {
|
if p.peek() == token::COLON {
|
||||||
p.bump();
|
p.bump();
|
||||||
t =
|
t = ast::ty_constr(@spanned(lo, hi, t),
|
||||||
ast::ty_constr(@spanned(lo, hi, t),
|
|
||||||
parse_type_constraints(p));
|
parse_type_constraints(p));
|
||||||
}
|
}
|
||||||
} else if p.peek() == token::LBRACKET {
|
} else if p.peek() == token::LBRACKET {
|
||||||
expect(p, token::LBRACKET);
|
expect(p, token::LBRACKET);
|
||||||
t = ast::ty_vec(parse_mt(p));
|
t = ast::ty_vec(parse_mt(p));
|
||||||
hi = p.get_hi_pos();
|
|
||||||
expect(p, token::RBRACKET);
|
expect(p, token::RBRACKET);
|
||||||
} else if eat_word(p, "fn") {
|
} else if eat_word(p, "fn") {
|
||||||
let proto = parse_fn_ty_proto(p);
|
let proto = parse_fn_ty_proto(p);
|
||||||
t = parse_ty_fn(proto, p);
|
t = parse_ty_fn(proto, p);
|
||||||
alt t { ast::ty_fn(_, _, out, _, _) { hi = out.span.hi; } }
|
|
||||||
} else if eat_word(p, "block") {
|
} else if eat_word(p, "block") {
|
||||||
t = parse_ty_fn(ast::proto_block, p);
|
t = parse_ty_fn(ast::proto_block, p);
|
||||||
alt t { ast::ty_fn(_, _, out, _, _) { hi = out.span.hi; } }
|
} else if eat_word(p, "lambda") {
|
||||||
|
t = parse_ty_fn(ast::proto_shared(ast::sugar_sexy), p);
|
||||||
} else if eat_word(p, "obj") {
|
} else if eat_word(p, "obj") {
|
||||||
t = parse_ty_obj(p, hi);
|
t = parse_ty_obj(p);
|
||||||
} else if p.peek() == token::MOD_SEP || is_ident(p.peek()) {
|
} else if p.peek() == token::MOD_SEP || is_ident(p.peek()) {
|
||||||
let path = parse_path(p);
|
let path = parse_path(p);
|
||||||
t = ast::ty_path(path, p.get_id());
|
t = ast::ty_path(path, p.get_id());
|
||||||
hi = path.span.hi;
|
|
||||||
} else { p.fatal("expecting type"); }
|
} else { p.fatal("expecting type"); }
|
||||||
ret parse_ty_postfix(t, p, colons_before_params);
|
ret parse_ty_postfix(t, p, colons_before_params);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user