about summary refs log tree commit diff
path: root/src/comp/front
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2011-03-21 17:12:05 -0700
committerGraydon Hoare <graydon@mozilla.com>2011-03-21 18:10:34 -0700
commitcaa22c93415bb106bced82bbabc3d9ffbef7e69c (patch)
tree2a77dc7cfb33c82dcdc83a69a423ebc4340e576e /src/comp/front
parent35951c92dbc1bb2eeb94f7951b6186ce8239fc41 (diff)
downloadrust-caa22c93415bb106bced82bbabc3d9ffbef7e69c.tar.gz
rust-caa22c93415bb106bced82bbabc3d9ffbef7e69c.zip
Started adding support for floating-point type, floating-point literals, and logging of floats. Other operations on float probably don't work yet.
Diffstat (limited to 'src/comp/front')
-rw-r--r--src/comp/front/ast.rs2
-rw-r--r--src/comp/front/lexer.rs48
-rw-r--r--src/comp/front/parser.rs4
-rw-r--r--src/comp/front/token.rs3
4 files changed, 44 insertions, 13 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs
index a007e76b058..cdeea241809 100644
--- a/src/comp/front/ast.rs
+++ b/src/comp/front/ast.rs
@@ -255,6 +255,7 @@ tag lit_ {
     lit_int(int);
     lit_uint(uint);
     lit_mach_int(ty_mach, int);
+    lit_float(str);
     lit_nil;
     lit_bool(bool);
 }
@@ -274,6 +275,7 @@ tag ty_ {
     ty_bool;
     ty_int;
     ty_uint;
+    ty_float;
     ty_machine(util.common.ty_mach);
     ty_char;
     ty_str;
diff --git a/src/comp/front/lexer.rs b/src/comp/front/lexer.rs
index 403558e2934..d4948503d59 100644
--- a/src/comp/front/lexer.rs
+++ b/src/comp/front/lexer.rs
@@ -1,5 +1,6 @@
 import std.io;
 import std._str;
+import std._int;
 import std.map;
 import std.map.hashmap;
 import util.common;
@@ -314,6 +315,24 @@ impure fn consume_block_comment(reader rdr) {
     be consume_any_whitespace(rdr);
 }
 
+impure fn scan_dec_digits(reader rdr) -> int {
+
+    auto c = rdr.curr();
+
+    let int accum_int = 0;
+
+    while (is_dec_digit(c) || c == '_') {
+            if (c != '_') {
+                accum_int *= 10;
+                accum_int += dec_digit_val(c);
+            }
+            rdr.bump();
+            c = rdr.curr();
+    }
+
+    ret accum_int;
+}
+
 impure fn scan_number(mutable char c, reader rdr) -> token.token {
     auto accum_int = 0;
     auto n = rdr.next();
@@ -330,9 +349,7 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
             rdr.bump();
             c = rdr.curr();
         }
-    }
-
-    if (c == '0' && n == 'b') {
+    } else if (c == '0' && n == 'b') {
         rdr.bump();
         rdr.bump();
         c = rdr.curr();
@@ -344,16 +361,12 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
             rdr.bump();
             c = rdr.curr();
         }
+    } else {
+        accum_int = scan_dec_digits(rdr);
     }
 
-    while (is_dec_digit(c) || c == '_') {
-        if (c != '_') {
-            accum_int *= 10;
-            accum_int += dec_digit_val(c);
-        }
-        rdr.bump();
-        c = rdr.curr();
-    }
+    c = rdr.curr();
+    n = rdr.next();
 
     if (c == 'u' || c == 'i') {
         let bool signed = (c == 'i');
@@ -405,7 +418,18 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
             ret token.LIT_UINT(accum_int as uint);
         }
     }
-    ret token.LIT_INT(accum_int);
+    n = rdr.curr();
+    if(n == '.') {
+        // Parse a floating-point number.
+        rdr.bump();
+        auto accum_int1 = scan_dec_digits(rdr);
+        ret token.LIT_FLOAT(_int.to_str(accum_int, 10u) + "."
+                          + _int.to_str(accum_int1, 10u));
+        // FIXME: Parse exponent.
+    }
+    else {
+        ret token.LIT_INT(accum_int);
+    }
 }
 
 impure fn next_token(reader rdr) -> token.token {
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs
index 9d3724f9de1..f1f8a91887f 100644
--- a/src/comp/front/parser.rs
+++ b/src/comp/front/parser.rs
@@ -537,6 +537,10 @@ impure fn parse_lit(parser p) -> ast.lit {
             p.bump();
             lit = ast.lit_uint(u);
         }
+        case (token.LIT_FLOAT(?s)) {
+            p.bump();
+            lit = ast.lit_float(s);
+        }
         case (token.LIT_MACH_INT(?tm, ?i)) {
             p.bump();
             lit = ast.lit_mach_int(tm, i);
diff --git a/src/comp/front/token.rs b/src/comp/front/token.rs
index 62c6406a6b8..a1fb1cd0174 100644
--- a/src/comp/front/token.rs
+++ b/src/comp/front/token.rs
@@ -126,6 +126,7 @@ tag token {
     LIT_INT(int);
     LIT_UINT(uint);
     LIT_MACH_INT(ty_mach, int);
+    LIT_FLOAT(str);
     LIT_STR(str);
     LIT_CHAR(char);
     LIT_BOOL(bool);
@@ -295,7 +296,7 @@ fn to_str(token t) -> str {
             ret  _int.to_str(i, 10u)
                 + "_" + ty_mach_to_str(tm);
         }
-
+        case (LIT_FLOAT(?s)) { ret s; }
         case (LIT_STR(?s)) {
             // FIXME: escape.
             ret "\"" + s + "\"";