Skip to content

Beginnings of support for magical self prefix; nothing profound happening #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/comp/front/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ tag expr_ {
expr_tup(vec[elt], ann);
expr_rec(vec[field], option.t[@expr], ann);
expr_call(@expr, vec[@expr], ann);
expr_call_self(@expr, vec[@expr], ann);
expr_bind(@expr, vec[option.t[@expr]], ann);
expr_spawn(spawn_dom, option.t[str], @expr, vec[@expr], ann);
expr_binary(binop, @expr, @expr, ann);
Expand Down
1 change: 1 addition & 0 deletions src/comp/front/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impure fn new_reader(io.reader rdr, str filename) -> reader
keywords.insert("any", token.ANY);

keywords.insert("obj", token.OBJ);
keywords.insert("self", token.SELF);

keywords.insert("port", token.PORT);
keywords.insert("chan", token.CHAN);
Expand Down
15 changes: 15 additions & 0 deletions src/comp/front/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,20 @@ impure fn parse_bottom_expr(parser p) -> @ast.expr {
ex = ast.expr_chan(e, ast.ann_none);
}

case (token.SELF) {
p.bump();
expect(p, token.DOT);
// The rest is a call expression.
auto e = parse_bottom_expr(p);
auto pf = parse_expr;
auto es = parse_seq[@ast.expr](token.LPAREN,
token.RPAREN,
some(token.COMMA),
pf, p);
hi = es.span;
auto ex = ast.expr_call_self(e, es.node, ast.ann_none);
}

case (_) {
auto lit = parse_lit(p);
hi = lit.span;
Expand Down Expand Up @@ -1646,6 +1660,7 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
case (ast.expr_tup(_,_)) { ret true; }
case (ast.expr_rec(_,_,_)) { ret true; }
case (ast.expr_call(_,_,_)) { ret true; }
case (ast.expr_call_self(_,_,_)){ ret true; }
case (ast.expr_binary(_,_,_,_)) { ret true; }
case (ast.expr_unary(_,_,_)) { ret true; }
case (ast.expr_lit(_,_)) { ret true; }
Expand Down
3 changes: 2 additions & 1 deletion src/comp/front/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ tag token {
FN;
ITER;

/* Object type */
/* Object type and related keywords */
OBJ;
SELF;

/* Comm and task types */
CHAN;
Expand Down
1 change: 1 addition & 0 deletions src/comp/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ fn expr_ty(@ast.expr expr) -> @t {
case (ast.expr_rec(_, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_bind(_, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_call(_, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_call_self(_, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_spawn(_, _, _, _, ?ann))
{ ret ann_to_type(ann); }
case (ast.expr_binary(_, _, _, ?ann)) { ret ann_to_type(ann); }
Expand Down
7 changes: 7 additions & 0 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,13 @@ fn demand_expr_full(&@fn_ctxt fcx, @ty.t expected, @ast.expr e,
ann_to_type(ann), adk);
e_1 = ast.expr_call(sube, es, ast.ann_type(t, none[vec[@ty.t]]));
}
case (ast.expr_call_self(?sube, ?es, ?ann)) {
auto t = demand_full(fcx, e.span, expected,
ann_to_type(ann), adk);
e_1 = ast.expr_call_self(sube,
es,
ast.ann_type(t, none[vec[@ty.t]]));
}
case (ast.expr_binary(?bop, ?lhs, ?rhs, ?ann)) {
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
e_1 = ast.expr_binary(bop, lhs, rhs,
Expand Down
13 changes: 13 additions & 0 deletions src/test/compile-fail/self-missing-method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// xfail-boot
// error-pattern:expecting ., found (
fn main() {

obj foo() {
fn m() {
self();
}
}

auto a = foo;
a.m();
}
16 changes: 16 additions & 0 deletions src/test/run-pass/obj-self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// xfail-boot
fn main() {

obj foo() {
fn m1() {
log "hi!";
}
fn m2() {
self.m1();
}
}

auto a = foo();
a.m1();
a.m2();
}