diff --git a/src/lib/bitv.rs b/src/lib/bitv.rs index 851a26237ee89..0f39a345b3946 100644 --- a/src/lib/bitv.rs +++ b/src/lib/bitv.rs @@ -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 @@ -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::(elt, nbits / uint_bits() + 1u); + let storage = vec::init_elt_mut::(elt, nbits / uint_bits + 1u); ret @{storage: storage, nbits: nbits}; } @@ -118,7 +117,7 @@ Function: clone Makes a copy of a bitvector */ fn clone(v: t) -> t { - let storage = vec::init_elt_mut::(0u, v.nbits / uint_bits() + 1u); + let storage = vec::init_elt_mut::(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}; @@ -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; @@ -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; diff --git a/src/lib/float.rs b/src/lib/float.rs index faa71b4ed56f3..30781592b7153 100644 --- a/src/lib/float.rs +++ b/src/lib/float.rs @@ -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. @@ -137,7 +137,7 @@ fn from_str(num: str) -> float { break; } _ { - ret NaN(); + ret NaN; } } } @@ -157,7 +157,7 @@ fn from_str(num: str) -> float { break; } _ { - ret NaN(); + ret NaN; } } } @@ -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; @@ -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.; } @@ -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; } @@ -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 @@ -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; } /* @@ -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; } // diff --git a/src/lib/int.rs b/src/lib/int.rs index 65edaf75dc312..621477ebca72d 100644 --- a/src/lib/int.rs +++ b/src/lib/int.rs @@ -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::() * 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; } diff --git a/src/lib/io.rs b/src/lib/io.rs index 016c6381a7d2c..afba61423775b 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -282,12 +282,12 @@ obj fd_buf_writer(fd: int, res: option::t<@fd_res>) { fn file_buf_writer(path: str, flags: [fileflag]) -> result::t { 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. { } } } @@ -295,8 +295,8 @@ fn file_buf_writer(path: str, 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(); @@ -384,6 +384,7 @@ fn buffered_file_buf_writer(path: str) -> result::t { // 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)); } diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs index 8d56aeade639f..7a0e097696072 100644 --- a/src/lib/linux_os.rs +++ b/src/lib/linux_os.rs @@ -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"; } diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs index 2d23b20355261..9bc1fab3c0e72 100644 --- a/src/lib/macos_os.rs +++ b/src/lib/macos_os.rs @@ -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"; } diff --git a/src/lib/rand.rs b/src/lib/rand.rs index e28260003d609..777fdb027bfa4 100644 --- a/src/lib/rand.rs +++ b/src/lib/rand.rs @@ -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; } } diff --git a/src/lib/u32.rs b/src/lib/u32.rs index 626ca8b6fbdc0..69811dc7a9f1a 100644 --- a/src/lib/u32.rs +++ b/src/lib/u32.rs @@ -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: diff --git a/src/lib/u64.rs b/src/lib/u64.rs index 3109e5dc50ffe..aacc35a827d97 100644 --- a/src/lib/u64.rs +++ b/src/lib/u64.rs @@ -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 diff --git a/src/lib/u8.rs b/src/lib/u8.rs index c2f7aba7ce70d..76e2272b43320 100644 --- a/src/lib/u8.rs +++ b/src/lib/u8.rs @@ -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; } diff --git a/src/lib/uint.rs b/src/lib/uint.rs index 6d904d33d7a8f..897c59e4273e9 100644 --- a/src/lib/uint.rs +++ b/src/lib/uint.rs @@ -3,24 +3,22 @@ Module: uint */ /* -Function: min_value +Const: min_value Return the minimal value for an uint. This is always 0 */ -pure fn min_value() -> uint { ret 0u; } +const min_value: uint = 0u; /* -Function: max_value +Const: max_value Return the maximal value for an uint. This is 2^wordsize - 1 */ -pure fn max_value() -> uint { - ret 0u - 1u; -} +const max_value: uint = 0u - 1u; /* Function: add */ pure fn add(x: uint, y: uint) -> uint { ret x + y; } diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index 6ffa2bc25a494..c870938d4d5cb 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -19,24 +19,18 @@ 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 256; } - fn O_EXCL() -> int { ret 1024; } - fn O_TRUNC() -> int { ret 512; } - fn O_TEXT() -> int { ret 16384; } - fn O_BINARY() -> int { ret 32768; } - fn O_NOINHERIT() -> int { ret 128; } - fn S_IRUSR() -> uint { - ret 256u; // really _S_IREAD in win32 - - } - fn S_IWUSR() -> uint { - ret 128u; // really _S_IWRITE in win32 - - } + const O_RDONLY: int = 0; + const O_WRONLY: int = 1; + const O_RDWR: int = 2; + const O_APPEND: int = 8; + const O_CREAT: int = 256; + const O_EXCL: int = 1024; + const O_TRUNC: int = 512; + const O_TEXT: int = 16384; + const O_BINARY: int = 32768; + const O_NOINHERIT: int = 128; + const S_IRUSR: uint = 256u; // really _S_IREAD in win32 + const S_IWUSR: uint = 128u; // really _S_IWRITE in win32 } type DWORD = u32; @@ -52,8 +46,8 @@ native "stdcall" mod kernel32 { nSize: DWORD) -> DWORD; } +// FIXME turn into constants fn exec_suffix() -> str { ret ".exe"; } - fn target_os() -> str { ret "win32"; } fn dylib_filename(base: str) -> str { ret base + ".dll"; } diff --git a/src/test/run-pass/float-nan.rs b/src/test/run-pass/float-nan.rs index d0ec3d4f9949c..c27b97d81da3e 100644 --- a/src/test/run-pass/float-nan.rs +++ b/src/test/run-pass/float-nan.rs @@ -2,11 +2,11 @@ use std; import std::float; fn main() { - let nan = float::NaN(); + let nan = float::NaN; assert(float::isNaN(nan)); - let inf = float::infinity(); - assert(-inf == float::neg_infinity()); + let inf = float::infinity; + assert(-inf == float::neg_infinity); assert( nan != nan); assert( nan != -nan); diff --git a/src/test/stdtest/float.rs b/src/test/stdtest/float.rs index 04e16648b7d16..8720e455586f3 100644 --- a/src/test/stdtest/float.rs +++ b/src/test/stdtest/float.rs @@ -40,44 +40,44 @@ fn test_from_str() { #[test] fn test_positive() { - assert(float::positive(float::infinity())); + assert(float::positive(float::infinity)); assert(float::positive(1.)); assert(float::positive(0.)); assert(!float::positive(-1.)); - assert(!float::positive(float::neg_infinity())); - assert(!float::positive(1./float::neg_infinity())); - assert(!float::positive(float::NaN())); + assert(!float::positive(float::neg_infinity)); + assert(!float::positive(1./float::neg_infinity)); + assert(!float::positive(float::NaN)); } #[test] fn test_negative() { - assert(!float::negative(float::infinity())); + assert(!float::negative(float::infinity)); assert(!float::negative(1.)); assert(!float::negative(0.)); assert(float::negative(-1.)); - assert(float::negative(float::neg_infinity())); - assert(float::negative(1./float::neg_infinity())); - assert(!float::negative(float::NaN())); + assert(float::negative(float::neg_infinity)); + assert(float::negative(1./float::neg_infinity)); + assert(!float::negative(float::NaN)); } #[test] fn test_nonpositive() { - assert(!float::nonpositive(float::infinity())); + assert(!float::nonpositive(float::infinity)); assert(!float::nonpositive(1.)); assert(!float::nonpositive(0.)); assert(float::nonpositive(-1.)); - assert(float::nonpositive(float::neg_infinity())); - assert(float::nonpositive(1./float::neg_infinity())); - assert(!float::nonpositive(float::NaN())); + assert(float::nonpositive(float::neg_infinity)); + assert(float::nonpositive(1./float::neg_infinity)); + assert(!float::nonpositive(float::NaN)); } #[test] fn test_nonnegative() { - assert(float::nonnegative(float::infinity())); + assert(float::nonnegative(float::infinity)); assert(float::nonnegative(1.)); assert(float::nonnegative(0.)); assert(!float::nonnegative(-1.)); - assert(!float::nonnegative(float::neg_infinity())); - assert(!float::nonnegative(1./float::neg_infinity())); - assert(!float::nonnegative(float::NaN())); + assert(!float::nonnegative(float::neg_infinity)); + assert(!float::nonnegative(1./float::neg_infinity)); + assert(!float::nonnegative(float::NaN)); } diff --git a/src/test/stdtest/int.rs b/src/test/stdtest/int.rs index 9136289e93605..43bdf39ee004d 100644 --- a/src/test/stdtest/int.rs +++ b/src/test/stdtest/int.rs @@ -91,7 +91,7 @@ fn test_pow() { #[test] fn test_overflows() { - assert (int::max_value() > 0); - assert (int::min_value() <= 0); - assert (int::min_value() + int::max_value() + 1 == 0); + assert (int::max_value > 0); + assert (int::min_value <= 0); + assert (int::min_value + int::max_value + 1 == 0); } diff --git a/src/test/stdtest/uint.rs b/src/test/stdtest/uint.rs index 28546f4a6f127..adb1cde32a93b 100644 --- a/src/test/stdtest/uint.rs +++ b/src/test/stdtest/uint.rs @@ -98,9 +98,9 @@ fn test_next_power_of_two() { #[test] fn test_overflows() { - assert (uint::max_value() > 0u); - assert (uint::min_value() <= 0u); - assert (uint::min_value() + uint::max_value() + 1u == 0u); + assert (uint::max_value > 0u); + assert (uint::min_value <= 0u); + assert (uint::min_value + uint::max_value + 1u == 0u); } #[test]