diff options
| author | Marijn Haverbeke <marijnh@gmail.com> | 2012-01-26 12:26:14 +0100 |
|---|---|---|
| committer | Marijn Haverbeke <marijnh@gmail.com> | 2012-01-26 14:25:06 +0100 |
| commit | 87b064b249657c8e65079d01beb77409f69d49cd (patch) | |
| tree | cecb525f7ddf7312521d1a65e93ea8531f23426e /src/comp/syntax | |
| parent | 1792d9ec96d680cb3ec257bfef84baffea352d80 (diff) | |
| download | rust-87b064b249657c8e65079d01beb77409f69d49cd.tar.gz rust-87b064b249657c8e65079d01beb77409f69d49cd.zip | |
First stab at operator overloading
When no built-in interpretation is found for one of the operators
mentioned below, the typechecker will try to turn it into a method
call with the name written next to it. For binary operators, the
method will be called on the LHS with the RHS as only parameter.
Binary:
+ op_add
- op_sub
* op_mul
/ op_div
% op_rem
& op_and
| op_or
^ op_xor
<< op_shift_left
>> op_shift_right
>>> op_ashift_right
Unary:
- op_neg
! op_not
Overloading of the indexing ([]) operator isn't finished yet.
Issue #1520
Diffstat (limited to 'src/comp/syntax')
| -rw-r--r-- | src/comp/syntax/ast_util.rs | 5 | ||||
| -rw-r--r-- | src/comp/syntax/parse/parser.rs | 4 |
2 files changed, 9 insertions, 0 deletions
diff --git a/src/comp/syntax/ast_util.rs b/src/comp/syntax/ast_util.rs index cb0545c07b7..65c117cfa6b 100644 --- a/src/comp/syntax/ast_util.rs +++ b/src/comp/syntax/ast_util.rs @@ -326,6 +326,11 @@ fn ident_to_path(s: span, i: ident) -> @path { @respan(s, {global: false, idents: [i], types: []}) } +// Provides an extra node_id to hang callee information on, in case the +// operator is deferred to a user-supplied method. The parser is responsible +// for reserving this id. +fn op_expr_callee_id(e: @expr) -> node_id { e.id - 1 } + // Local Variables: // mode: rust // fill-column: 78; diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index a66b223175c..1e892eb43b1 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -1048,6 +1048,7 @@ fn parse_prefix_expr(p: parser) -> pexpr { p.bump(); let e = to_expr(parse_prefix_expr(p)); hi = e.span.hi; + p.get_id(); // see ast_util::op_expr_callee_id ex = ast::expr_unary(ast::not, e); } token::BINOP(b) { @@ -1056,6 +1057,7 @@ fn parse_prefix_expr(p: parser) -> pexpr { p.bump(); let e = to_expr(parse_prefix_expr(p)); hi = e.span.hi; + p.get_id(); // see ast_util::op_expr_callee_id ex = ast::expr_unary(ast::neg, e); } token::STAR { @@ -1146,6 +1148,7 @@ fn parse_more_binops(p: parser, plhs: pexpr, min_prec: int) -> p.bump(); let expr = parse_prefix_expr(p); let rhs = parse_more_binops(p, expr, cur.prec); + p.get_id(); // see ast_util::op_expr_callee_id let bin = mk_pexpr(p, lhs.span.lo, rhs.span.hi, ast::expr_binary(cur.op, lhs, rhs)); ret parse_more_binops(p, bin, min_prec); @@ -1186,6 +1189,7 @@ fn parse_assign_expr(p: parser) -> @ast::expr { token::LSR { aop = ast::lsr; } token::ASR { aop = ast::asr; } } + p.get_id(); // see ast_util::op_expr_callee_id ret mk_expr(p, lo, rhs.span.hi, ast::expr_assign_op(aop, lhs, rhs)); } token::LARROW { |
