Skip to content

Commit caa22c9

Browse files
catamorphismgraydon
authored andcommitted
Started adding support for floating-point type, floating-point literals, and logging of floats. Other operations on float probably don't work yet.
1 parent 35951c9 commit caa22c9

File tree

9 files changed

+87
-18
lines changed

9 files changed

+87
-18
lines changed

src/comp/front/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ tag lit_ {
255255
lit_int(int);
256256
lit_uint(uint);
257257
lit_mach_int(ty_mach, int);
258+
lit_float(str);
258259
lit_nil;
259260
lit_bool(bool);
260261
}
@@ -274,6 +275,7 @@ tag ty_ {
274275
ty_bool;
275276
ty_int;
276277
ty_uint;
278+
ty_float;
277279
ty_machine(util.common.ty_mach);
278280
ty_char;
279281
ty_str;

src/comp/front/lexer.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import std.io;
22
import std._str;
3+
import std._int;
34
import std.map;
45
import std.map.hashmap;
56
import util.common;
@@ -314,6 +315,24 @@ impure fn consume_block_comment(reader rdr) {
314315
be consume_any_whitespace(rdr);
315316
}
316317

318+
impure fn scan_dec_digits(reader rdr) -> int {
319+
320+
auto c = rdr.curr();
321+
322+
let int accum_int = 0;
323+
324+
while (is_dec_digit(c) || c == '_') {
325+
if (c != '_') {
326+
accum_int *= 10;
327+
accum_int += dec_digit_val(c);
328+
}
329+
rdr.bump();
330+
c = rdr.curr();
331+
}
332+
333+
ret accum_int;
334+
}
335+
317336
impure fn scan_number(mutable char c, reader rdr) -> token.token {
318337
auto accum_int = 0;
319338
auto n = rdr.next();
@@ -330,9 +349,7 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
330349
rdr.bump();
331350
c = rdr.curr();
332351
}
333-
}
334-
335-
if (c == '0' && n == 'b') {
352+
} else if (c == '0' && n == 'b') {
336353
rdr.bump();
337354
rdr.bump();
338355
c = rdr.curr();
@@ -344,16 +361,12 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
344361
rdr.bump();
345362
c = rdr.curr();
346363
}
364+
} else {
365+
accum_int = scan_dec_digits(rdr);
347366
}
348367

349-
while (is_dec_digit(c) || c == '_') {
350-
if (c != '_') {
351-
accum_int *= 10;
352-
accum_int += dec_digit_val(c);
353-
}
354-
rdr.bump();
355-
c = rdr.curr();
356-
}
368+
c = rdr.curr();
369+
n = rdr.next();
357370

358371
if (c == 'u' || c == 'i') {
359372
let bool signed = (c == 'i');
@@ -405,7 +418,18 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
405418
ret token.LIT_UINT(accum_int as uint);
406419
}
407420
}
408-
ret token.LIT_INT(accum_int);
421+
n = rdr.curr();
422+
if(n == '.') {
423+
// Parse a floating-point number.
424+
rdr.bump();
425+
auto accum_int1 = scan_dec_digits(rdr);
426+
ret token.LIT_FLOAT(_int.to_str(accum_int, 10u) + "."
427+
+ _int.to_str(accum_int1, 10u));
428+
// FIXME: Parse exponent.
429+
}
430+
else {
431+
ret token.LIT_INT(accum_int);
432+
}
409433
}
410434

411435
impure fn next_token(reader rdr) -> token.token {

src/comp/front/parser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ impure fn parse_lit(parser p) -> ast.lit {
537537
p.bump();
538538
lit = ast.lit_uint(u);
539539
}
540+
case (token.LIT_FLOAT(?s)) {
541+
p.bump();
542+
lit = ast.lit_float(s);
543+
}
540544
case (token.LIT_MACH_INT(?tm, ?i)) {
541545
p.bump();
542546
lit = ast.lit_mach_int(tm, i);

src/comp/front/token.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ tag token {
126126
LIT_INT(int);
127127
LIT_UINT(uint);
128128
LIT_MACH_INT(ty_mach, int);
129+
LIT_FLOAT(str);
129130
LIT_STR(str);
130131
LIT_CHAR(char);
131132
LIT_BOOL(bool);
@@ -295,7 +296,7 @@ fn to_str(token t) -> str {
295296
ret _int.to_str(i, 10u)
296297
+ "_" + ty_mach_to_str(tm);
297298
}
298-
299+
case (LIT_FLOAT(?s)) { ret s; }
299300
case (LIT_STR(?s)) {
300301
// FIXME: escape.
301302
ret "\"" + s + "\"";

src/comp/middle/trans.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ fn T_int() -> TypeRef {
211211
ret T_i32();
212212
}
213213

214+
fn T_float() -> TypeRef {
215+
// FIXME: switch on target type.
216+
ret T_f64();
217+
}
218+
214219
fn T_char() -> TypeRef {
215220
ret T_i32();
216221
}
@@ -360,10 +365,6 @@ fn T_crate(type_names tn) -> TypeRef {
360365
ret t;
361366
}
362367

363-
fn T_double() -> TypeRef {
364-
ret llvm.LLVMDoubleType();
365-
}
366-
367368
fn T_taskptr(type_names tn) -> TypeRef {
368369
ret T_ptr(T_task(tn));
369370
}
@@ -590,6 +591,7 @@ fn type_of_inner(@crate_ctxt cx, @ty.t t, bool boxed) -> TypeRef {
590591
case (ty.ty_nil) { llty = T_nil(); }
591592
case (ty.ty_bool) { llty = T_bool(); }
592593
case (ty.ty_int) { llty = T_int(); }
594+
case (ty.ty_float) { llty = T_float(); }
593595
case (ty.ty_uint) { llty = T_int(); }
594596
case (ty.ty_machine(?tm)) {
595597
alt (tm) {
@@ -743,6 +745,10 @@ fn C_integral(int i, TypeRef t) -> ValueRef {
743745
ret llvm.LLVMConstIntOfString(t, _str.buf(istr(i)), 10);
744746
}
745747

748+
fn C_float(str s) -> ValueRef {
749+
ret llvm.LLVMConstRealOfString(T_float(), _str.buf(s));
750+
}
751+
746752
fn C_nil() -> ValueRef {
747753
// NB: See comment above in T_void().
748754
ret C_integral(0, T_i1());
@@ -879,6 +885,7 @@ fn trans_upcall2(builder b, @glue_fns glues, ValueRef lltaskptr,
879885
llglue = glues.upcall_glues_cdecl.(n);
880886
}
881887
let vec[ValueRef] call_args = vec(llupcall);
888+
882889
if (!pass_task) {
883890
call_args += vec(lltaskptr);
884891
}
@@ -2290,6 +2297,9 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
22902297
}
22912298
ret C_integral(i, t);
22922299
}
2300+
case(ast.lit_float(?fs)) {
2301+
ret C_float(fs);
2302+
}
22932303
case (ast.lit_char(?c)) {
22942304
ret C_integral(c as int, T_char());
22952305
}
@@ -4476,13 +4486,28 @@ fn trans_log(@block_ctxt cx, @ast.expr e) -> result {
44764486

44774487
auto sub = trans_expr(cx, e);
44784488
auto e_ty = ty.expr_ty(e);
4489+
alt (e_ty.struct) {
4490+
case(ty.ty_float) {
4491+
auto tmp = sub.bcx.build.Alloca(T_float());
4492+
sub.bcx.build.Store(sub.val, tmp);
4493+
sub = res(sub.bcx, tmp);
4494+
}
4495+
case(_) { }
4496+
}
4497+
44794498
alt (e_ty.struct) {
44804499
case (ty.ty_str) {
44814500
auto v = vp2i(sub.bcx, sub.val);
44824501
ret trans_upcall(sub.bcx,
44834502
"upcall_log_str",
44844503
vec(v));
44854504
}
4505+
case (ty.ty_float) {
4506+
auto v = vp2i(sub.bcx, sub.val);
4507+
ret trans_upcall(sub.bcx,
4508+
"upcall_log_float",
4509+
vec(v));
4510+
}
44864511
case (_) {
44874512
ret trans_upcall(sub.bcx,
44884513
"upcall_log_int",
@@ -6247,7 +6272,6 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
62476272
collect_items(cx, crate);
62486273
collect_tag_ctors(cx, crate);
62496274
trans_constants(cx, crate);
6250-
62516275
trans_mod(cx, crate.node.module);
62526276
trans_vec_append_glue(cx);
62536277
if (!shared) {

src/comp/middle/ty.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tag sty {
3232
ty_nil;
3333
ty_bool;
3434
ty_int;
35+
ty_float;
3536
ty_uint;
3637
ty_machine(util.common.ty_mach);
3738
ty_char;
@@ -162,6 +163,7 @@ fn ty_to_str(&@t typ) -> str {
162163
case (ty_nil) { s += "()"; }
163164
case (ty_bool) { s += "bool"; }
164165
case (ty_int) { s += "int"; }
166+
case (ty_float) { s += "float"; }
165167
case (ty_uint) { s += "uint"; }
166168
case (ty_machine(?tm)) { s += common.ty_mach_to_str(tm); }
167169
case (ty_char) { s += "char"; }
@@ -418,6 +420,7 @@ fn type_is_scalar(@t ty) -> bool {
418420
case (ty_nil) { ret true; }
419421
case (ty_bool) { ret true; }
420422
case (ty_int) { ret true; }
423+
case (ty_float) { ret true; }
421424
case (ty_uint) { ret true; }
422425
case (ty_machine(_)) { ret true; }
423426
case (ty_char) { ret true; }

src/comp/middle/typeck.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,7 @@ fn check_lit(@ast.lit lit) -> @ty.t {
14931493
case (ast.lit_str(_)) { sty = ty.ty_str; }
14941494
case (ast.lit_char(_)) { sty = ty.ty_char; }
14951495
case (ast.lit_int(_)) { sty = ty.ty_int; }
1496+
case (ast.lit_float(_)) { sty = ty.ty_float; }
14961497
case (ast.lit_uint(_)) { sty = ty.ty_uint; }
14971498
case (ast.lit_mach_int(?tm, _)) { sty = ty.ty_machine(tm); }
14981499
case (ast.lit_nil) { sty = ty.ty_nil; }

src/comp/pretty/pprust.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ impure fn print_literal(ps s, @ast.lit lit) {
283283
case (ast.lit_uint(?val)) { // TODO clipping? uistr?
284284
wrd(s, util.common.istr(val as int) + "u");
285285
}
286+
case (ast.lit_float(?fstr)) {
287+
wrd(s, fstr);
288+
}
286289
case (ast.lit_mach_int(?mach,?val)) {
287290
wrd(s, util.common.istr(val as int));
288291
wrd(s, util.common.ty_mach_to_str(mach));

src/rt/rust_upcall.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ void upcall_log_int(rust_task *task, int32_t i) {
3939
"rust: %" PRId32 " (0x%" PRIx32 ")", i, i);
4040
}
4141

42+
extern "C" CDECL
43+
void upcall_log_float(rust_task *task, double *f) {
44+
LOG_UPCALL_ENTRY(task);
45+
task->log(rust_log::UPCALL | rust_log::ULOG,
46+
"rust: %12.12f", *f);
47+
}
48+
4249
extern "C" CDECL void
4350
upcall_log_str(rust_task *task, rust_str *str) {
4451
LOG_UPCALL_ENTRY(task);

0 commit comments

Comments
 (0)