diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2012-11-07 18:40:34 -0800 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2012-11-12 10:39:08 -0800 |
| commit | fe02814a63bd759f6727c7479fc4aeb04f0be9b4 (patch) | |
| tree | 7b372b7829c23190c1e6f1d187e1b316f9b67a3e /src/libsyntax | |
| parent | f05e2da709cca3b20e560eaf2e05d73c0ca5d91b (diff) | |
| download | rust-fe02814a63bd759f6727c7479fc4aeb04f0be9b4.tar.gz rust-fe02814a63bd759f6727c7479fc4aeb04f0be9b4.zip | |
rustc: Implement floating point literal inference. r=nmatsakis
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/parse/lexer.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 16 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 1 |
5 files changed, 32 insertions, 4 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 7452e41fac3..dd90dfee683 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -862,6 +862,7 @@ enum lit_ { lit_uint(u64, uint_ty), lit_int_unsuffixed(i64), lit_float(@~str, float_ty), + lit_float_unsuffixed(@~str), lit_nil, lit_bool(bool), } @@ -880,6 +881,7 @@ impl ast::lit_: cmp::Eq { (lit_float(val_a, ty_a), lit_float(val_b, ty_b)) => { val_a == val_b && ty_a == ty_b } + (lit_float_unsuffixed(a), lit_float_unsuffixed(b)) => a == b, (lit_nil, lit_nil) => true, (lit_bool(a), lit_bool(b)) => a == b, (lit_str(_), _) => false, @@ -887,6 +889,7 @@ impl ast::lit_: cmp::Eq { (lit_uint(*), _) => false, (lit_int_unsuffixed(*), _) => false, (lit_float(*), _) => false, + (lit_float_unsuffixed(*), _) => false, (lit_nil, _) => false, (lit_bool(_), _) => false } diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 8f57d733eb5..482813f3fd0 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -385,6 +385,8 @@ fn scan_number(c: char, rdr: string_reader) -> token::Token { } None => () } + + let mut is_machine_float = false; if rdr.curr == 'f' { bump(rdr); c = rdr.curr; @@ -404,10 +406,14 @@ fn scan_number(c: char, rdr: string_reader) -> token::Token { back-end. */ } else { is_float = true; + is_machine_float = true; } } if is_float { - return token::LIT_FLOAT(rdr.interner.intern(@num_str), ast::ty_f); + if is_machine_float { + return token::LIT_FLOAT(rdr.interner.intern(@num_str), ast::ty_f); + } + return token::LIT_FLOAT_UNSUFFIXED(rdr.interner.intern(@num_str)); } else { if str::len(num_str) == 0u { rdr.fatal(~"no valid digits found for number"); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 2b42dcc0ed0..f1f49c63a7d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -43,9 +43,9 @@ use ast::{_mod, add, arg, arm, attribute, ident, impure_fn, infer, inherited, item, item_, item_class, item_const, item_enum, item_fn, item_foreign_mod, item_impl, item_mac, item_mod, item_trait, - item_ty, lit, lit_, lit_bool, lit_float, lit_int, - lit_int_unsuffixed, lit_nil, lit_str, lit_uint, local, m_const, - m_imm, m_mutbl, mac_, mac_aq, mac_ellipsis, mac_invoc, + item_ty, lit, lit_, lit_bool, lit_float, lit_float_unsuffixed, + lit_int, lit_int_unsuffixed, lit_nil, lit_str, lit_uint, local, + m_const, m_imm, m_mutbl, mac_, mac_aq, mac_ellipsis, mac_invoc, mac_invoc_tt, mac_var, matcher, match_nonterminal, match_seq, match_tok, method, mode, module_ns, mt, mul, mutability, named_field, neg, noreturn, not, pat, pat_box, pat_enum, @@ -787,6 +787,8 @@ impl Parser { token::LIT_UINT(u, ut) => lit_uint(u, ut), token::LIT_INT_UNSUFFIXED(i) => lit_int_unsuffixed(i), token::LIT_FLOAT(s, ft) => lit_float(self.id_to_str(s), ft), + token::LIT_FLOAT_UNSUFFIXED(s) => + lit_float_unsuffixed(self.id_to_str(s)), token::LIT_STR(s) => lit_str(self.id_to_str(s)), token::LPAREN => { self.expect(token::RPAREN); lit_nil }, _ => { self.unexpected_last(tok); } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 0d139b101d8..baf963942e2 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -62,6 +62,7 @@ enum Token { LIT_UINT(u64, ast::uint_ty), LIT_INT_UNSUFFIXED(i64), LIT_FLOAT(ast::ident, ast::float_ty), + LIT_FLOAT_UNSUFFIXED(ast::ident), LIT_STR(ast::ident), /* Name components */ @@ -164,6 +165,13 @@ fn to_str(in: @ident_interner, t: Token) -> ~str { } body + ast_util::float_ty_to_str(t) } + LIT_FLOAT_UNSUFFIXED(s) => { + let mut body = *in.get(s); + if body.ends_with(~".") { + body = body + ~"0"; // `10.f` is not a float literal + } + body + } LIT_STR(s) => { ~"\"" + str::escape_default(*in.get(s)) + ~"\"" } /* Name components */ @@ -204,6 +212,7 @@ pure fn can_begin_expr(t: Token) -> bool { LIT_UINT(_, _) => true, LIT_INT_UNSUFFIXED(_) => true, LIT_FLOAT(_, _) => true, + LIT_FLOAT_UNSUFFIXED(_) => true, LIT_STR(_) => true, POUND => true, AT => true, @@ -243,6 +252,7 @@ fn is_lit(t: Token) -> bool { LIT_UINT(_, _) => true, LIT_INT_UNSUFFIXED(_) => true, LIT_FLOAT(_, _) => true, + LIT_FLOAT_UNSUFFIXED(_) => true, LIT_STR(_) => true, _ => false } @@ -684,6 +694,12 @@ impl Token : cmp::Eq { _ => false } } + LIT_FLOAT_UNSUFFIXED(e0a) => { + match (*other) { + LIT_FLOAT_UNSUFFIXED(e0b) => e0a == e0b, + _ => false + } + } LIT_STR(e0a) => { match (*other) { LIT_STR(e0b) => e0a == e0b, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 0418f6776de..54ed5085435 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1970,6 +1970,7 @@ fn print_literal(s: ps, &&lit: @ast::lit) { ast::lit_float(f, t) => { word(s.s, *f + ast_util::float_ty_to_str(t)); } + ast::lit_float_unsuffixed(f) => word(s.s, *f), ast::lit_nil => word(s.s, ~"()"), ast::lit_bool(val) => { if val { word(s.s, ~"true"); } else { word(s.s, ~"false"); } |
