Started adding support for floating-point type, floating-point literals, and logging of floats. Other operations on float probably don't work yet.

This commit is contained in:
Tim Chevalier 2011-03-21 17:12:05 -07:00 committed by Graydon Hoare
parent 35951c92db
commit caa22c9341
9 changed files with 87 additions and 18 deletions

View File

@ -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;

View File

@ -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 {

View File

@ -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);

View File

@ -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 + "\"";

View File

@ -211,6 +211,11 @@ fn T_int() -> TypeRef {
ret T_i32();
}
fn T_float() -> TypeRef {
// FIXME: switch on target type.
ret T_f64();
}
fn T_char() -> TypeRef {
ret T_i32();
}
@ -360,10 +365,6 @@ fn T_crate(type_names tn) -> TypeRef {
ret t;
}
fn T_double() -> TypeRef {
ret llvm.LLVMDoubleType();
}
fn T_taskptr(type_names tn) -> TypeRef {
ret T_ptr(T_task(tn));
}
@ -590,6 +591,7 @@ fn type_of_inner(@crate_ctxt cx, @ty.t t, bool boxed) -> TypeRef {
case (ty.ty_nil) { llty = T_nil(); }
case (ty.ty_bool) { llty = T_bool(); }
case (ty.ty_int) { llty = T_int(); }
case (ty.ty_float) { llty = T_float(); }
case (ty.ty_uint) { llty = T_int(); }
case (ty.ty_machine(?tm)) {
alt (tm) {
@ -743,6 +745,10 @@ fn C_integral(int i, TypeRef t) -> ValueRef {
ret llvm.LLVMConstIntOfString(t, _str.buf(istr(i)), 10);
}
fn C_float(str s) -> ValueRef {
ret llvm.LLVMConstRealOfString(T_float(), _str.buf(s));
}
fn C_nil() -> ValueRef {
// NB: See comment above in T_void().
ret C_integral(0, T_i1());
@ -879,6 +885,7 @@ fn trans_upcall2(builder b, @glue_fns glues, ValueRef lltaskptr,
llglue = glues.upcall_glues_cdecl.(n);
}
let vec[ValueRef] call_args = vec(llupcall);
if (!pass_task) {
call_args += vec(lltaskptr);
}
@ -2290,6 +2297,9 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
}
ret C_integral(i, t);
}
case(ast.lit_float(?fs)) {
ret C_float(fs);
}
case (ast.lit_char(?c)) {
ret C_integral(c as int, T_char());
}
@ -4476,6 +4486,15 @@ fn trans_log(@block_ctxt cx, @ast.expr e) -> result {
auto sub = trans_expr(cx, e);
auto e_ty = ty.expr_ty(e);
alt (e_ty.struct) {
case(ty.ty_float) {
auto tmp = sub.bcx.build.Alloca(T_float());
sub.bcx.build.Store(sub.val, tmp);
sub = res(sub.bcx, tmp);
}
case(_) { }
}
alt (e_ty.struct) {
case (ty.ty_str) {
auto v = vp2i(sub.bcx, sub.val);
@ -4483,6 +4502,12 @@ fn trans_log(@block_ctxt cx, @ast.expr e) -> result {
"upcall_log_str",
vec(v));
}
case (ty.ty_float) {
auto v = vp2i(sub.bcx, sub.val);
ret trans_upcall(sub.bcx,
"upcall_log_float",
vec(v));
}
case (_) {
ret trans_upcall(sub.bcx,
"upcall_log_int",
@ -6247,7 +6272,6 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
collect_items(cx, crate);
collect_tag_ctors(cx, crate);
trans_constants(cx, crate);
trans_mod(cx, crate.node.module);
trans_vec_append_glue(cx);
if (!shared) {

View File

@ -32,6 +32,7 @@ tag sty {
ty_nil;
ty_bool;
ty_int;
ty_float;
ty_uint;
ty_machine(util.common.ty_mach);
ty_char;
@ -162,6 +163,7 @@ fn ty_to_str(&@t typ) -> str {
case (ty_nil) { s += "()"; }
case (ty_bool) { s += "bool"; }
case (ty_int) { s += "int"; }
case (ty_float) { s += "float"; }
case (ty_uint) { s += "uint"; }
case (ty_machine(?tm)) { s += common.ty_mach_to_str(tm); }
case (ty_char) { s += "char"; }
@ -418,6 +420,7 @@ fn type_is_scalar(@t ty) -> bool {
case (ty_nil) { ret true; }
case (ty_bool) { ret true; }
case (ty_int) { ret true; }
case (ty_float) { ret true; }
case (ty_uint) { ret true; }
case (ty_machine(_)) { ret true; }
case (ty_char) { ret true; }

View File

@ -1493,6 +1493,7 @@ fn check_lit(@ast.lit lit) -> @ty.t {
case (ast.lit_str(_)) { sty = ty.ty_str; }
case (ast.lit_char(_)) { sty = ty.ty_char; }
case (ast.lit_int(_)) { sty = ty.ty_int; }
case (ast.lit_float(_)) { sty = ty.ty_float; }
case (ast.lit_uint(_)) { sty = ty.ty_uint; }
case (ast.lit_mach_int(?tm, _)) { sty = ty.ty_machine(tm); }
case (ast.lit_nil) { sty = ty.ty_nil; }

View File

@ -283,6 +283,9 @@ impure fn print_literal(ps s, @ast.lit lit) {
case (ast.lit_uint(?val)) { // TODO clipping? uistr?
wrd(s, util.common.istr(val as int) + "u");
}
case (ast.lit_float(?fstr)) {
wrd(s, fstr);
}
case (ast.lit_mach_int(?mach,?val)) {
wrd(s, util.common.istr(val as int));
wrd(s, util.common.ty_mach_to_str(mach));

View File

@ -39,6 +39,13 @@ void upcall_log_int(rust_task *task, int32_t i) {
"rust: %" PRId32 " (0x%" PRIx32 ")", i, i);
}
extern "C" CDECL
void upcall_log_float(rust_task *task, double *f) {
LOG_UPCALL_ENTRY(task);
task->log(rust_log::UPCALL | rust_log::ULOG,
"rust: %12.12f", *f);
}
extern "C" CDECL void
upcall_log_str(rust_task *task, rust_str *str) {
LOG_UPCALL_ENTRY(task);