Skip to content
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

Pseudomethods #341

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 10 additions & 1 deletion src/comp/front/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,6 @@ fn next_token(reader rdr) -> token.token {

alt (c) {
// One-byte tokens.
case (':') { rdr.bump(); ret token.COLON; }
case ('?') { rdr.bump(); ret token.QUES; }
case (';') { rdr.bump(); ret token.SEMI; }
case (',') { rdr.bump(); ret token.COMMA; }
Expand All @@ -628,6 +627,16 @@ fn next_token(reader rdr) -> token.token {


// Multi-byte tokens.
case (':') {
rdr.bump();
if (rdr.curr() == ':') {
rdr.bump();
ret token.COLONCOLON;
} else {
ret token.COLON;
}
}

case ('=') {
rdr.bump();
if (rdr.curr() == '=') {
Expand Down
23 changes: 23 additions & 0 deletions src/comp/front/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,29 @@ fn parse_dot_or_call_expr(parser p) -> @ast.expr {
}
}

case (token.COLONCOLON) {
// Pseudomethod calls: e::pm() is sugar for pm(e).
p.bump();

auto pm = parse_bottom_expr(p);

// Parse the rest of the arguments.
auto pf = parse_expr;
auto lo = p.get_lo_pos();
auto hi = p.get_hi_pos();
expect(p, token.LPAREN);
auto result = parse_seq_to_end[@ast.expr](token.RPAREN,
some(token.COMMA),
pf, hi, p);

// Get all the arguments together.
let vec[@ast.expr] args = vec(e) + result;
auto es = @spanned(lo, hi, args);

auto e_ = ast.expr_call(pm, es.node, ast.ann_none);
e = @spanned(lo, hi, e_);
}

case (token.DOT) {
p.bump();
alt (p.peek()) {
Expand Down
2 changes: 2 additions & 0 deletions src/comp/front/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tag token {
COMMA;
SEMI;
COLON;
COLONCOLON;
QUES;
RARROW;
SEND;
Expand Down Expand Up @@ -214,6 +215,7 @@ fn to_str(token t) -> str {
case (COMMA) { ret ","; }
case (SEMI) { ret ";"; }
case (COLON) { ret ":"; }
case (COLONCOLON) { ret "::"; }
case (QUES) { ret "?"; }
case (RARROW) { ret "->"; }
case (SEND) { ret "<|"; }
Expand Down
55 changes: 55 additions & 0 deletions src/test/run-pass/pseudomethod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// xfail-boot
use std;
import std._vec.len;
fn main() {

// Pseudomethod using a library function
let uint a = len[int](vec(1, 2, 3, 4));
log a;
let uint b = vec(1, 2, 3, 4)::len[int]();
log b;
check (a == b);

let vec[int] v = vec(1, 2, 3, 4);
let uint c = len[int](v);
log c;
let uint d = v::len[int]();
log d;
check (c == d);

// User-defined pseudomethods
fn exclaim(str s) -> str { ret s + "!"; }
let str e = exclaim("hello");
log e;
let str f = "hello"::exclaim();
log f;
check (e == f);

fn plus(int a, int b) -> int { ret a + b; }
let int m = 2 * 3::plus(4) + 5;
let int n = 2 * (3 + 4) + 5;
log m;
log n;
check (m == n);

// Multi-argument pseudomethod
fn bang_huh(str s1, str s2) -> str {
ret s1 + "!" + s2 + "?";
}
let str g = bang_huh("hello", "world");
log g;
let str h = "hello"::bang_huh("world");
log h;
check (g == h);

// Stacking pseudomethods
let str i = "hello"::exclaim()::bang_huh("world");
log i;
let str j = bang_huh(exclaim("hello"), "world");
log j;
check (i == j);

let int k = (vec("foo", "bar", "baz")::len[str]() as int)::plus(50);
log k;
check (k == 53);
}