about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2012-11-07 18:40:34 -0800
committerPatrick Walton <pcwalton@mimiga.net>2012-11-12 10:39:08 -0800
commitfe02814a63bd759f6727c7479fc4aeb04f0be9b4 (patch)
tree7b372b7829c23190c1e6f1d187e1b316f9b67a3e /src/libsyntax/parse
parentf05e2da709cca3b20e560eaf2e05d73c0ca5d91b (diff)
downloadrust-fe02814a63bd759f6727c7479fc4aeb04f0be9b4.tar.gz
rust-fe02814a63bd759f6727c7479fc4aeb04f0be9b4.zip
rustc: Implement floating point literal inference. r=nmatsakis
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/lexer.rs8
-rw-r--r--src/libsyntax/parse/parser.rs8
-rw-r--r--src/libsyntax/parse/token.rs16
3 files changed, 28 insertions, 4 deletions
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,