提交 e1251f7b 编写于 作者: P Paul Woolcock 提交者: Marijn Haverbeke

Change all ternary ops to if/then/else

All the files below had at least one instance of the ternary operator
present in the source.  All have been changed to the equivalent
if/then/else expression.
上级 e1f15a71
......@@ -333,7 +333,11 @@ fn host_triple() -> str {
// grabbing (at compile time) the target triple that this rustc is
// built with and calling that (at runtime) the host triple.
let ht = #env("CFG_HOST_TRIPLE");
ret ht != "" ? ht : fail "rustc built without CFG_HOST_TRIPLE";
ret if ht != "" {
ht
} else {
fail "rustc built without CFG_HOST_TRIPLE"
};
}
fn build_session_options(match: getopts::match,
......
......@@ -244,7 +244,7 @@ fn mk_path(cx: test_ctxt, path: [ast::ident]) -> [ast::ident] {
_ { false }
}
};
(is_std ? [] : ["std"]) + path
(if is_std { [] } else { ["std"] }) + path
}
// The ast::ty of [std::test::test_desc]
......
......@@ -602,23 +602,26 @@ fn walk(tcx: ty::ctxt, mut: option::t<unsafe_ty>, pat: @ast::pat,
ast::pat_rec(fs, _) {
let ty = ty::node_id_to_type(tcx, pat.id);
for f in fs {
let m = ty::get_field(tcx, ty, f.ident).mt.mut != ast::imm;
walk(tcx, m ? some(contains(ty)) : mut, f.pat, set);
let m = ty::get_field(tcx, ty, f.ident).mt.mut != ast::imm,
c = if m { some(contains(ty)) } else { mut };
walk(tcx, c, f.pat, set);
}
}
ast::pat_box(p) {
let ty = ty::node_id_to_type(tcx, pat.id);
let m = alt ty::struct(tcx, ty) {
ty::ty_box(mt) { mt.mut != ast::imm }
};
walk(tcx, m ? some(contains(ty)) : mut, p, set);
},
c = if m {some(contains(ty)) } else { mut };
walk(tcx, c, p, set);
}
ast::pat_uniq(p) {
let ty = ty::node_id_to_type(tcx, pat.id);
let m = alt ty::struct(tcx, ty) {
ty::ty_uniq(mt) { mt.mut != ast::imm }
};
walk(tcx, m ? some(contains(ty)) : mut, p, set);
},
c = if m { some(contains(ty)) } else { mut };
walk(tcx, c, p, set);
}
}
}
......
......@@ -73,14 +73,14 @@ fn check_expr(sess: session, method_map: typeck::method_map, e: @expr,
expr_lit(@{node: lit_int(v, t), _}) {
if t != ty_char {
if (v as u64) > ast_util::int_ty_max(
t == ty_i ? sess.targ_cfg.int_type : t) {
if t == ty_i { sess.targ_cfg.int_type } else { t }) {
sess.span_err(e.span, "literal out of range for its type");
}
}
}
expr_lit(@{node: lit_uint(v, t), _}) {
if v > ast_util::uint_ty_max(
t == ty_u ? sess.targ_cfg.uint_type : t) {
if t == ty_u { sess.targ_cfg.uint_type } else { t }) {
sess.span_err(e.span, "literal out of range for its type");
}
}
......
......@@ -465,10 +465,16 @@ fn create_composite_type(type_tag: int, name: str, file: ValueRef, line: int,
lli64(align), // align
lli64(offset), // offset
lli32(0), // flags
option::is_none(derived) ? llnull() : // derived from
option::get(derived),
option::is_none(members) ? llnull() : // members
llmdnode(option::get(members)),
if option::is_none(derived) {
llnull()
} else { // derived from
option::get(derived)
},
if option::is_none(members) {
llnull()
} else { //members
llmdnode(option::get(members))
},
lli32(0), // runtime language
llnull()
];
......@@ -776,7 +782,7 @@ fn create_function(fcx: @fn_ctxt) -> @metadata<subprogram_md> {
let loc = codemap::lookup_char_pos(cx.sess.codemap,
sp.lo);
let file_node = create_file(cx, loc.filename).node;
let key = cx.item_symbols.contains_key(fcx.id) ? fcx.id : id;
let key = if cx.item_symbols.contains_key(fcx.id) { fcx.id } else { id };
let mangled = cx.item_symbols.get(key);
let ty_node = if cx.sess.opts.extra_debuginfo {
alt ret_ty.node {
......
......@@ -1986,7 +1986,7 @@ fn visit_block_with_impl_scope(e: @env, b: ast::blk, sc: iscopes,
_ {}
}
}
let sc = vec::len(impls) > 0u ? cons(@impls, @sc) : sc;
let sc = if vec::len(impls) > 0u { cons(@impls, @sc) } else { sc };
visit::visit_block(b, sc, v);
}
......@@ -1998,8 +1998,11 @@ fn visit_mod_with_impl_scope(e: @env, m: ast::_mod, s: span, id: node_id,
}
for i in m.items { find_impls_in_item(*e, i, impls, none, none); }
let impls = @impls;
visit::visit_mod(m, s, id,
vec::len(*impls) > 0u ? cons(impls, @sc) : sc, v);
visit::visit_mod(m, s, id, if vec::len(*impls) > 0u {
cons(impls, @sc)
} else {
sc
}, v);
e.impl_map.insert(id, cons(impls, @nil));
}
......
......@@ -402,8 +402,11 @@ fn compile_submatch(bcx: @block_ctxt, m: match, vals: [ValueRef], f: mk_fail,
let col = pick_col(m);
let val = vals[col];
let m = has_nested_bindings(m, col) ?
expand_nested_bindings(m, col, val) : m;
let m = if has_nested_bindings(m, col) {
expand_nested_bindings(m, col, val)
} else {
m
};
let vals_left =
vec::slice(vals, 0u, col) +
......@@ -493,7 +496,11 @@ enum branch_kind { no_branch, single, switch, compare, }
lit(l) {
test_val = Load(bcx, val);
let pty = ty::node_id_to_type(ccx.tcx, pat_id);
kind = ty::type_is_integral(ccx.tcx, pty) ? switch : compare;
kind = if ty::type_is_integral(ccx.tcx, pty) {
switch
} else {
compare
};
}
range(_, _) {
test_val = Load(bcx, val);
......
......@@ -68,7 +68,7 @@ fn type_of_explicit_args(cx: @crate_ctxt, inputs: [ty::arg]) ->
// that would obviate the need for this check
check non_ty_var(cx, arg_ty);
let llty = type_of_inner(cx, arg_ty);
atys += [arg.mode == ast::by_val ? llty : T_ptr(llty)];
atys += [if arg.mode == ast::by_val { llty } else { T_ptr(llty) }];
}
ret atys;
}
......@@ -2015,8 +2015,11 @@ fn store_temp_expr(cx: @block_ctxt, action: copy_action, dst: ValueRef,
-> @block_ctxt {
// Lvals in memory are not temporaries. Copy them.
if src.kind != temporary && !last_use {
let v = src.kind == owned ? load_if_immediate(cx, src.val, t)
: src.val;
let v = if src.kind == owned {
load_if_immediate(cx, src.val, t)
} else {
src.val
};
ret copy_val(cx, action, dst, v, t);
}
ret move_val(cx, action, dst, src, t);
......@@ -3417,7 +3420,7 @@ fn trans_expr_save_in(bcx: @block_ctxt, e: @ast::expr, dest: ValueRef)
-> @block_ctxt {
let tcx = bcx_tcx(bcx), t = ty::expr_ty(tcx, e);
let do_ignore = ty::type_is_bot(tcx, t) || ty::type_is_nil(tcx, t);
ret trans_expr(bcx, e, do_ignore ? ignore : save_in(dest));
ret trans_expr(bcx, e, if do_ignore { ignore } else { save_in(dest) });
}
// Call this to compile an expression that you need as an intermediate value,
......@@ -4256,7 +4259,7 @@ fn trans_block_dps(bcx: @block_ctxt, b: ast::blk, dest: dest)
some(e) {
let bt = ty::type_is_bot(bcx_tcx(bcx), ty::expr_ty(bcx_tcx(bcx), e));
debuginfo::update_source_pos(bcx, e.span);
bcx = trans_expr(bcx, e, bt ? ignore : dest);
bcx = trans_expr(bcx, e, if bt { ignore } else { dest });
}
_ { assert dest == ignore || bcx.unreachable; }
}
......@@ -4493,7 +4496,11 @@ fn trans_fn(cx: @local_ctxt, sp: span, decl: ast::fn_decl, body: ast::blk,
llfndecl: ValueRef, ty_self: self_arg, ty_params: [ast::ty_param],
id: ast::node_id) {
let do_time = cx.ccx.sess.opts.stats;
let start = do_time ? time::get_time() : {sec: 0u32, usec: 0u32};
let start = if do_time {
time::get_time()
} else {
{sec: 0u32, usec: 0u32}
};
let fcx = option::none;
trans_closure(cx, sp, decl, body, llfndecl, ty_self, ty_params, id,
{|new_fcx| fcx = option::some(new_fcx);});
......@@ -5396,7 +5403,7 @@ fn decl_crate_map(sess: session::session, mapname: str,
let n_subcrates = 1;
let cstore = sess.cstore;
while cstore::have_crate_data(cstore, n_subcrates) { n_subcrates += 1; }
let mapname = sess.building_library ? mapname : "toplevel";
let mapname = if sess.building_library { mapname } else { "toplevel" };
let sym_name = "_rust_crate_map_" + mapname;
let arrtype = T_array(int_type, n_subcrates as uint);
let maptype = T_struct([int_type, arrtype]);
......
......@@ -149,9 +149,13 @@ fn trans_append(cx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
let ccx = bcx_ccx(cx);
let unit_ty = ty::sequence_element_type(bcx_tcx(cx), vec_ty);
let dynamic = ty::type_has_dynamic_size(bcx_tcx(cx), unit_ty);
let (lhsptr, rhs) = !dynamic ? (lhsptr, rhs) :
(PointerCast(cx, lhsptr, T_ptr(T_ptr(ccx.opaque_vec_type))),
PointerCast(cx, rhs, T_ptr(ccx.opaque_vec_type)));
let (lhsptr, rhs) =
if !dynamic {
(lhsptr, rhs)
} else {
(PointerCast(cx, lhsptr, T_ptr(T_ptr(ccx.opaque_vec_type))),
PointerCast(cx, rhs, T_ptr(ccx.opaque_vec_type)))
};
let strings = alt ty::struct(bcx_tcx(cx), vec_ty) {
ty::ty_str { true }
ty::ty_vec(_) { false }
......@@ -187,7 +191,11 @@ fn trans_append(cx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
copy_val(bcx, INIT, write_ptr,
load_if_immediate(bcx, addr, unit_ty),
unit_ty);
let incr = dynamic ? unit_sz : C_int(ccx, 1);
let incr = if dynamic {
unit_sz
} else {
C_int(ccx, 1)
};
Store(bcx, InBoundsGEP(bcx, write_ptr, [incr]),
write_ptr_ptr);
ret bcx;
......@@ -244,8 +252,11 @@ fn trans_add(bcx: @block_ctxt, vec_ty: ty::t, lhs: ValueRef,
let bcx = copy_val(bcx, INIT, write_ptr,
load_if_immediate(bcx, addr, unit_ty), unit_ty);
let incr =
ty::type_has_dynamic_size(bcx_tcx(bcx), unit_ty) ?
llunitsz : C_int(ccx, 1);
if ty::type_has_dynamic_size(bcx_tcx(bcx), unit_ty) {
llunitsz
} else {
C_int(ccx, 1)
};
Store(bcx, InBoundsGEP(bcx, write_ptr, [incr]),
write_ptr_ptr);
ret bcx;
......
......@@ -1578,9 +1578,17 @@ fn check_call_or_bind(fcx: @fn_ctxt, sp: span, fty: ty::t,
fcx.ccx.tcx.sess.span_err(
sp, #fmt["this function takes %u parameter%s but %u \
parameter%s supplied", expected_arg_count,
expected_arg_count == 1u ? "" : "s",
if expected_arg_count == 1u {
""
} else {
"s"
},
supplied_arg_count,
supplied_arg_count == 1u ? " was" : "s were"]);
if supplied_arg_count == 1u {
" was"
} else {
"s were"
}]);
// HACK: build an arguments list with dummy arguments to
// check against
let dummy = {mode: ast::by_ref, ty: ty::mk_bot(fcx.ccx.tcx)};
......
......@@ -305,10 +305,42 @@ fn lit_to_const(lit: @lit) -> const_val {
fn compare_const_vals(a: const_val, b: const_val) -> int {
alt (a, b) {
(const_int(a), const_int(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_uint(a), const_uint(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_float(a), const_float(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_str(a), const_str(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_int(a), const_int(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
(const_uint(a), const_uint(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
(const_float(a), const_float(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
(const_str(a), const_str(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
}
}
......
......@@ -197,31 +197,31 @@ fn scan_number(c: char, rdr: reader) -> token::token {
c = rdr.curr;
n = rdr.next();
if c == 'u' || c == 'i' {
let signed = c == 'i', tp = signed ? either::left(ast::ty_i)
: either::right(ast::ty_u);
let signed = c == 'i', tp = if signed { either::left(ast::ty_i) }
else { either::right(ast::ty_u) };
rdr.bump();
c = rdr.curr;
if c == '8' {
rdr.bump();
tp = signed ? either::left(ast::ty_i8)
: either::right(ast::ty_u8);
tp = if signed { either::left(ast::ty_i8) }
else { either::right(ast::ty_u8) };
}
n = rdr.next();
if c == '1' && n == '6' {
rdr.bump();
rdr.bump();
tp = signed ? either::left(ast::ty_i16)
: either::right(ast::ty_u16);
tp = if signed { either::left(ast::ty_i16) }
else { either::right(ast::ty_u16) };
} else if c == '3' && n == '2' {
rdr.bump();
rdr.bump();
tp = signed ? either::left(ast::ty_i32)
: either::right(ast::ty_u32);
tp = if signed { either::left(ast::ty_i32) }
else { either::right(ast::ty_u32) };
} else if c == '6' && n == '4' {
rdr.bump();
rdr.bump();
tp = signed ? either::left(ast::ty_i64)
: either::right(ast::ty_u64);
tp = if signed { either::left(ast::ty_i64) }
else { either::right(ast::ty_u64) };
}
let parsed = u64::from_str(num_str, base as u64);
alt tp {
......
......@@ -538,8 +538,11 @@ fn parse_arg(p: parser) -> ast::arg {
fn parse_fn_block_arg(p: parser) -> ast::arg {
let m = parse_arg_mode(p);
let i = parse_value_ident(p);
let t = eat(p, token::COLON) ? parse_ty(p, false) :
@spanned(p.span.lo, p.span.hi, ast::ty_infer);
let t = if eat(p, token::COLON) {
parse_ty(p, false)
} else {
@spanned(p.span.lo, p.span.hi, ast::ty_infer)
};
ret {mode: m, ty: t, ident: i, id: p.get_id()};
}
......@@ -676,7 +679,12 @@ fn parse_path(p: parser) -> @ast::path {
fn parse_path_and_ty_param_substs(p: parser, colons: bool) -> @ast::path {
let lo = p.span.lo;
let path = parse_path(p);
if colons ? eat(p, token::MOD_SEP) : p.token == token::LT {
let b = if colons {
eat(p, token::MOD_SEP)
} else {
p.token == token::LT
};
if b {
let seq = parse_seq_lt_gt(some(token::COMMA),
{|p| parse_ty(p, false)}, p);
@spanned(lo, seq.span.hi, {types: seq.node with path.node})
......@@ -1504,7 +1512,11 @@ fn parse_pat(p: parser) -> @ast::pat {
_ { true }
} {
let name = parse_path(p);
let sub = eat(p, token::AT) ? some(parse_pat(p)) : none;
let sub = if eat(p, token::AT) {
some(parse_pat(p))
} else {
none
};
pat = ast::pat_ident(name, sub);
} else {
let enum_path = parse_path_and_ty_param_substs(p, true);
......@@ -1546,7 +1558,11 @@ fn parse_local(p: parser, allow_init: bool) -> @ast::local {
fn parse_let(p: parser) -> @ast::decl {
fn parse_let_style(p: parser) -> ast::let_style {
eat(p, token::BINOP(token::AND)) ? ast::let_ref : ast::let_copy
if eat(p, token::BINOP(token::AND)) {
ast::let_ref
} else {
ast::let_copy
}
}
let lo = p.span.lo;
let locals = [(parse_let_style(p), parse_local(p, true))];
......@@ -1786,11 +1802,19 @@ fn parse_fn_decl(p: parser, purity: ast::purity)
}
fn parse_fn_block_decl(p: parser) -> ast::fn_decl {
let inputs = eat(p, token::OROR) ? [] :
parse_seq(token::BINOP(token::OR), token::BINOP(token::OR),
seq_sep(token::COMMA), parse_fn_block_arg, p).node;
let output = eat(p, token::RARROW) ? parse_ty(p, false) :
@spanned(p.span.lo, p.span.hi, ast::ty_infer);
let inputs = if eat(p, token::OROR) {
[]
} else {
parse_seq(token::BINOP(token::OR),
token::BINOP(token::OR),
seq_sep(token::COMMA),
parse_fn_block_arg, p).node
};
let output = if eat(p, token::RARROW) {
parse_ty(p, false)
} else {
@spanned(p.span.lo, p.span.hi, ast::ty_infer)
};
ret {inputs: inputs,
output: output,
purity: ast::impure_fn,
......
......@@ -24,10 +24,10 @@ fn fn_input_to_str(cx: ctxt, input: {mode: middle::ty::mode, ty: t}) ->
str {
let modestr = alt input.mode {
ast::by_ref {
ty::type_is_immediate(cx, input.ty) ? "&&" : ""
if ty::type_is_immediate(cx, input.ty) { "&&" } else { "" }
}
ast::by_val {
ty::type_is_immediate(cx, input.ty) ? "" : "++"
if ty::type_is_immediate(cx, input.ty) { "" } else { "++" }
}
_ { mode_str(input.mode) }
};
......
......@@ -46,7 +46,7 @@
*/
fn to_str_common(num: float, digits: uint, exact: bool) -> str {
if is_NaN(num) { ret "NaN"; }
let (num, accum) = num < 0.0 ? (-num, "-") : (num, "");
let (num, accum) = if num < 0.0 { (-num, "-") } else { (num, "") };
let trunc = num as uint;
let frac = num - (trunc as float);
accum += uint::str(trunc);
......
......@@ -7,14 +7,14 @@
Returns the minimum of two values
*/
pure fn min<T: copy>(x: T, y: T) -> T { x < y ? x : y }
pure fn min<T: copy>(x: T, y: T) -> T { if x < y { x } else { y} }
/*
Function: max
Returns the maximum of two values
*/
pure fn max<T: copy>(x: T, y: T) -> T { x < y ? y : x }
pure fn max<T: copy>(x: T, y: T) -> T { if x < y { y } else { x } }
#[test]
fn test_max_min() {
......@@ -310,4 +310,4 @@ fn test_log_functions() {
assert ln1p(float::infinity) == float::infinity;
}
*/
\ No newline at end of file
*/
......@@ -65,7 +65,7 @@ fn rot(r: int, x: u32) -> u32 {
let j = 0u, q = 0x6ed9eba1u32;
while j < 8u {
let jj = j > 2u ? j - 3u : j;
let jj = if j > 2u { j - 3u } else { j };
a = rot(3, a + (b ^ c ^ d) + x[jj] + q);
d = rot(9, d + (a ^ b ^ c) + x[jj + 8u] + q);
c = rot(11, c + (d ^ a ^ b) + x[jj + 4u] + q);
......
......@@ -24,7 +24,17 @@ fn test7() -> uint {
(*regs < 2) as uint
}
fn test8() -> int { let val = @0; alt true { true { } } if *val < 1 { 0 } else { 1 } }
fn test8() -> int {
let val = @0;
alt true {
true { }
}
if *val < 1 {
0
} else {
1
}
}
fn test9() { let regs = @mutable 0; alt true { true { } } *regs += 1; }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册