Skip to content

Commit

Permalink
Beginnings of support for magical self prefix; nothing profound happe…
Browse files Browse the repository at this point in the history
…ning yet.
  • Loading branch information
lkuper committed Mar 29, 2011
1 parent f6490a6 commit 55fbed3
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 1 deletion.
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();
}

0 comments on commit 55fbed3

Please sign in to comment.