Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue1165 #1180

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/lib/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ The bitvector type.
type t = @{storage: [mutable uint], nbits: uint};


// FIXME: this should be a constant once they work
fn uint_bits() -> uint { ret 32u + (1u << 32u >> 27u); }
const uint_bits: uint = 32u + (1u << 32u >> 27u);

/*
Function: create
Expand All @@ -50,7 +49,7 @@ init - If true then the bits are initialized to 1, otherwise 0
*/
fn create(nbits: uint, init: bool) -> t {
let elt = if init { !0u } else { 0u };
let storage = vec::init_elt_mut::<uint>(elt, nbits / uint_bits() + 1u);
let storage = vec::init_elt_mut::<uint>(elt, nbits / uint_bits + 1u);
ret @{storage: storage, nbits: nbits};
}

Expand Down Expand Up @@ -118,7 +117,7 @@ Function: clone
Makes a copy of a bitvector
*/
fn clone(v: t) -> t {
let storage = vec::init_elt_mut::<uint>(0u, v.nbits / uint_bits() + 1u);
let storage = vec::init_elt_mut::<uint>(0u, v.nbits / uint_bits + 1u);
let len = vec::len(v.storage);
uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
ret @{storage: storage, nbits: v.nbits};
Expand All @@ -131,7 +130,7 @@ Retreive the value at index `i`
*/
fn get(v: t, i: uint) -> bool {
assert (i < v.nbits);
let bits = uint_bits();
let bits = uint_bits;
let w = i / bits;
let b = i % bits;
let x = 1u & v.storage[w] >> b;
Expand Down Expand Up @@ -229,7 +228,7 @@ Preconditions:
*/
fn set(v: t, i: uint, x: bool) {
assert (i < v.nbits);
let bits = uint_bits();
let bits = uint_bits;
let w = i / bits;
let b = i % bits;
let flag = 1u << b;
Expand Down
38 changes: 16 additions & 22 deletions src/lib/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn from_str(num: str) -> float {
//The string must start with one of the following characters.
alt str::char_at(num, 0u) {
'-' | '+' | '0' to '9' | '.' {}
_ { ret NaN(); }
_ { ret NaN; }
}

//Determine if first char is '-'/'+'. Set [pos] and [neg] accordingly.
Expand Down Expand Up @@ -137,7 +137,7 @@ fn from_str(num: str) -> float {
break;
}
_ {
ret NaN();
ret NaN;
}
}
}
Expand All @@ -157,7 +157,7 @@ fn from_str(num: str) -> float {
break;
}
_ {
ret NaN();
ret NaN;
}
}
}
Expand Down Expand Up @@ -202,12 +202,12 @@ fn from_str(num: str) -> float {
total = total * multiplier;
}
} else {
ret NaN();
ret NaN;
}
}

if(pos < len) {
ret NaN();
ret NaN;
} else {
if(neg) {
total *= -1f;
Expand Down Expand Up @@ -235,7 +235,7 @@ Returns:
fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float {
if x == 0u {
if pow == 0u {
ret NaN();
ret NaN;
}
ret 0.;
}
Expand All @@ -260,23 +260,17 @@ fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float {
//TODO: Once this is possible, replace the body of these functions
//by an actual constant.

/* Function: NaN */
fn NaN() -> float {
ret 0./0.;
}
/* Const: NaN */
const NaN: float = 0./0.;

/* Predicate: isNaN */
pure fn isNaN(f: float) -> bool { f != f }

/* Function: infinity */
pure fn infinity() -> float {
ret 1./0.;
}
/* Const: infinity */
const infinity: float = 1./0.;

/* Function: neg_infinity */
pure fn neg_infinity() -> float {
ret -1./0.;
}
/* Const: neg_infinity */
const neg_infinity: float = -1./0.;

/* Function: add */
pure fn add(x: float, y: float) -> float { ret x + y; }
Expand Down Expand Up @@ -316,14 +310,14 @@ Predicate: positive

Returns true if `x` is a positive number, including +0.0 and +Infinity.
*/
pure fn positive(x: float) -> bool { ret x > 0. || (1./x) == infinity(); }
pure fn positive(x: float) -> bool { ret x > 0. || (1./x) == infinity; }

/*
Predicate: negative

Returns true if `x` is a negative number, including -0.0 and -Infinity.
*/
pure fn negative(x: float) -> bool { ret x < 0. || (1./x) == neg_infinity(); }
pure fn negative(x: float) -> bool { ret x < 0. || (1./x) == neg_infinity; }

/*
Predicate: nonpositive
Expand All @@ -332,7 +326,7 @@ Returns true if `x` is a negative number, including -0.0 and -Infinity.
(This is the same as `float::negative`.)
*/
pure fn nonpositive(x: float) -> bool {
ret x < 0. || (1./x) == neg_infinity();
ret x < 0. || (1./x) == neg_infinity;
}

/*
Expand All @@ -342,7 +336,7 @@ Returns true if `x` is a positive number, including +0.0 and +Infinity.
(This is the same as `float::positive`.)
*/
pure fn nonnegative(x: float) -> bool {
ret x > 0. || (1./x) == infinity();
ret x > 0. || (1./x) == infinity;
}

//
Expand Down
21 changes: 13 additions & 8 deletions src/lib/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ Module: int
*/

/*
Function: max_value
Const: max_value

The maximum value of an integer
*/
fn max_value() -> int {
ret min_value() - 1;
}
// FIXME: Find another way to access the machine word size in a const expr
#[cfg(target_arch="x86")]
const max_value: int = (-1 << 31)-1;

#[cfg(target_arch="x86_64")]
const max_value: int = (-1 << 63)-1;

/*
Function: min_value
Const: min_value

The minumum value of an integer
*/
fn min_value() -> int {
ret (-1 << (sys::size_of::<int>() * 8u as int - 1)) as int;
}
#[cfg(target_arch="x86")]
const min_value: int = -1 << 31;

#[cfg(target_arch="x86_64")]
const min_value: int = -1 << 63;

/* Function: add */
pure fn add(x: int, y: int) -> int { ret x + y; }
Expand Down
13 changes: 7 additions & 6 deletions src/lib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,21 @@ obj fd_buf_writer(fd: int, res: option::t<@fd_res>) {
fn file_buf_writer(path: str,
flags: [fileflag]) -> result::t<buf_writer, str> {
let fflags: int =
os::libc_constants::O_WRONLY() | os::libc_constants::O_BINARY();
os::libc_constants::O_WRONLY | os::libc_constants::O_BINARY;
for f: fileflag in flags {
alt f {
append. { fflags |= os::libc_constants::O_APPEND(); }
create. { fflags |= os::libc_constants::O_CREAT(); }
truncate. { fflags |= os::libc_constants::O_TRUNC(); }
append. { fflags |= os::libc_constants::O_APPEND; }
create. { fflags |= os::libc_constants::O_CREAT; }
truncate. { fflags |= os::libc_constants::O_TRUNC; }
none. { }
}
}
let fd =
str::as_buf(path,
{|pathbuf|
os::libc::open(pathbuf, fflags,
os::libc_constants::S_IRUSR() |
os::libc_constants::S_IWUSR())
os::libc_constants::S_IRUSR |
os::libc_constants::S_IWUSR)
});
ret if fd < 0 {
log_err sys::last_os_error();
Expand Down Expand Up @@ -384,6 +384,7 @@ fn buffered_file_buf_writer(path: str) -> result::t<buf_writer, str> {


// FIXME it would be great if this could be a const
// Problem seems to be that new_writer is not pure
fn stdout() -> writer { ret new_writer(fd_buf_writer(1, option::none)); }
fn stderr() -> writer { ret new_writer(fd_buf_writer(2, option::none)); }

Expand Down
31 changes: 13 additions & 18 deletions src/lib/linux_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,22 @@ native "cdecl" mod libc = "" {
}

mod libc_constants {
fn O_RDONLY() -> int { ret 0; }
fn O_WRONLY() -> int { ret 1; }
fn O_RDWR() -> int { ret 2; }
fn O_APPEND() -> int { ret 1024; }
fn O_CREAT() -> int { ret 64; }
fn O_EXCL() -> int { ret 128; }
fn O_TRUNC() -> int { ret 512; }
fn O_TEXT() -> int {
ret 0; // nonexistent in linux libc

}
fn O_BINARY() -> int {
ret 0; // nonexistent in linux libc

}
fn S_IRUSR() -> uint { ret 256u; }
fn S_IWUSR() -> uint { ret 128u; }
const O_RDONLY: int = 0;
const O_WRONLY: int = 1;
const O_RDWR: int = 2;
const O_APPEND: int = 1024;
const O_CREAT: int = 64;
const O_EXCL: int = 128;
const O_TRUNC: int = 512;
const O_TEXT: int = 0; // nonexistent in linux libc
const O_BINARY: int = 0; // nonexistent in linux libc

const S_IRUSR: uint = 256u;
const S_IWUSR: uint = 128u;
}

// FIXME turn into constants
fn exec_suffix() -> str { ret ""; }

fn target_os() -> str { ret "linux"; }

fn dylib_filename(base: str) -> str { ret "lib" + base + ".so"; }
Expand Down
31 changes: 13 additions & 18 deletions src/lib/macos_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,22 @@ native "cdecl" mod libc = "" {
}

mod libc_constants {
fn O_RDONLY() -> int { ret 0; }
fn O_WRONLY() -> int { ret 1; }
fn O_RDWR() -> int { ret 2; }
fn O_APPEND() -> int { ret 8; }
fn O_CREAT() -> int { ret 512; }
fn O_EXCL() -> int { ret 2048; }
fn O_TRUNC() -> int { ret 1024; }
fn O_TEXT() -> int {
ret 0; // nonexistent in darwin libc

}
fn O_BINARY() -> int {
ret 0; // nonexistent in darwin libc

}
fn S_IRUSR() -> uint { ret 1024u; }
fn S_IWUSR() -> uint { ret 512u; }
const O_RDONLY: int = 0;
const O_WRONLY: int = 1;
const O_RDWR: int = 2;
const O_APPEND: int = 8;
const O_CREAT: int = 512;
const O_EXCL: int = 2048;
const O_TRUNC: int = 1024;
const O_TEXT: int = 0; // nonexistent in darwin libc
const O_BINARY: int = 0; // nonexistent in darwin libc

const S_IRUSR: uint = 1024u;
const S_IWUSR: uint = 512u;
}

// FIXME turn into constants
fn exec_suffix() -> str { ret ""; }

fn target_os() -> str { ret "macos"; }

fn dylib_filename(base: str) -> str { ret "lib" + base + ".dylib"; }
Expand Down
2 changes: 1 addition & 1 deletion src/lib/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn mk_rng() -> rng {
let u1 = rustrt::rand_next(**c) as float;
let u2 = rustrt::rand_next(**c) as float;
let u3 = rustrt::rand_next(**c) as float;
let scale = u32::max_value() as float;
let scale = u32::max_value as float;
ret ((u1 / scale + u2) / scale + u3) / scale;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/u32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ Module: u32
*/

/*
Function: min_value
Const: min_value

Return the minimal value for a u32
*/
pure fn min_value() -> u32 { ret 0u32; }
const min_value: u32 = 0u32;

/*
Function: max_value
Const: max_value

Return the maximal value for a u32
*/
pure fn max_value() -> u32 { ret 4294967296u32; }
const max_value: u32 = 4294967296u32;

//
// Local Variables:
Expand Down
8 changes: 4 additions & 4 deletions src/lib/u64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ Module: u64
*/

/*
Function: min_value
Const: min_value

Return the minimal value for a u64
*/
pure fn min_value() -> u64 { ret 0u64; }
const min_value: u64 = 0u64;

/*
Function: max_value
Const: max_value

Return the maximal value for a u64
*/
pure fn max_value() -> u64 { ret 18446744073709551615u64; }
const max_value: u64 = 18446744073709551615u64;

/*
Function: to_str
Expand Down
8 changes: 4 additions & 4 deletions src/lib/u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ Module: u8
*/

/*
Function: max_value
Const: max_value

The maximum value of a u8.
*/
pure fn max_value() -> u8 { ret 255u8; }
const max_value: u8 = 255u8;

/*
Function: min_value
Const: min_value

The minumum value of a u8.
*/
pure fn min_value() -> u8 { ret 0u8; }
const min_value: u8 = 0u8;

/* Function: add */
pure fn add(x: u8, y: u8) -> u8 { ret x + y; }
Expand Down
Loading