Skip to content

Commit f42977d

Browse files
committed
rustc: Modernize front::eval
1 parent c7ab88c commit f42977d

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

src/comp/front/eval.rs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,56 +38,49 @@ type ctx = @rec(parser p,
3838
mutable uint next_ann);
3939

4040
fn mk_env() -> env {
41-
let env e = [];
42-
ret e;
41+
ret [];
4342
}
4443

4544
fn val_is_bool(val v) -> bool {
4645
alt (v) {
47-
case (val_bool(_)) { ret true; }
48-
case (_) { }
46+
case (val_bool(_)) { true }
47+
case (_) { false }
4948
}
50-
ret false;
5149
}
5250

5351
fn val_is_int(val v) -> bool {
5452
alt (v) {
55-
case (val_int(_)) { ret true; }
56-
case (_) { }
53+
case (val_int(_)) { true }
54+
case (_) { false }
5755
}
58-
ret false;
5956
}
6057

6158
fn val_is_str(val v) -> bool {
6259
alt (v) {
63-
case (val_str(_)) { ret true; }
64-
case (_) { }
60+
case (val_str(_)) { true }
61+
case (_) { false }
6562
}
66-
ret false;
6763
}
6864

6965
fn val_as_bool(val v) -> bool {
7066
alt (v) {
71-
case (val_bool(?b)) { ret b; }
72-
case (_) { }
67+
case (val_bool(?b)) { b }
68+
case (_) { fail }
7369
}
74-
fail;
7570
}
7671

7772
fn val_as_int(val v) -> int {
7873
alt (v) {
79-
case (val_int(?i)) { ret i; }
80-
case (_) { }
74+
case (val_int(?i)) { i }
75+
case (_) { fail }
8176
}
82-
fail;
8377
}
8478

8579
fn val_as_str(val v) -> str {
8680
alt (v) {
87-
case (val_str(?s)) { ret s; }
88-
case (_) { }
81+
case (val_str(?s)) { s }
82+
case (_) { fail }
8983
}
90-
fail;
9184
}
9285

9386
fn lookup(session::session sess, env e, span sp, ident i) -> val {
@@ -96,20 +89,18 @@ fn lookup(session::session sess, env e, span sp, ident i) -> val {
9689
ret pair._1;
9790
}
9891
}
99-
sess.span_err(sp, "unknown variable: " + i);
100-
fail;
92+
sess.span_err(sp, "unknown variable: " + i)
10193
}
10294

10395
fn eval_lit(ctx cx, span sp, @ast::lit lit) -> val {
10496
alt (lit.node) {
105-
case (ast::lit_bool(?b)) { ret val_bool(b); }
106-
case (ast::lit_int(?i)) { ret val_int(i); }
107-
case (ast::lit_str(?s,_)) { ret val_str(s); }
97+
case (ast::lit_bool(?b)) { val_bool(b) }
98+
case (ast::lit_int(?i)) { val_int(i) }
99+
case (ast::lit_str(?s,_)) { val_str(s) }
108100
case (_) {
109-
cx.sess.span_err(sp, "evaluating unsupported literal");
101+
cx.sess.span_err(sp, "evaluating unsupported literal")
110102
}
111103
}
112-
fail;
113104
}
114105

115106
fn eval_expr(ctx cx, env e, @ast::expr x) -> val {
@@ -219,17 +210,15 @@ fn eval_expr(ctx cx, env e, @ast::expr x) -> val {
219210

220211
fn val_eq(session::session sess, span sp, val av, val bv) -> bool {
221212
if (val_is_bool(av) && val_is_bool(bv)) {
222-
ret val_as_bool(av) == val_as_bool(bv);
223-
}
224-
if (val_is_int(av) && val_is_int(bv)) {
225-
ret val_as_int(av) == val_as_int(bv);
213+
val_as_bool(av) == val_as_bool(bv)
214+
} else if (val_is_int(av) && val_is_int(bv)) {
215+
val_as_int(av) == val_as_int(bv)
216+
} else if (val_is_str(av) && val_is_str(bv)) {
217+
str::eq(val_as_str(av),
218+
val_as_str(bv))
219+
} else {
220+
sess.span_err(sp, "bad types in comparison")
226221
}
227-
if (val_is_str(av) && val_is_str(bv)) {
228-
ret str::eq(val_as_str(av),
229-
val_as_str(bv));
230-
}
231-
sess.span_err(sp, "bad types in comparison");
232-
fail;
233222
}
234223

235224
fn eval_crate_directives(ctx cx,

0 commit comments

Comments
 (0)