From 16104446712acf7e381039199eaf39dfa0fcea35 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 1 Nov 2003 19:15:08 +0000 Subject: [PATCH] * Conditions, string equality. --- src/fix-ng/eval.cc | 24 ++++++++++++++++++++++++ src/fix-ng/fix.cc | 6 ------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/fix-ng/eval.cc b/src/fix-ng/eval.cc index 46bb1f941..c58a06dff 100644 --- a/src/fix-ng/eval.cc +++ b/src/fix-ng/eval.cc @@ -106,6 +106,15 @@ Path evalPath(EvalState & state, Expr e) } +bool evalBool(EvalState & state, Expr e) +{ + e = evalExpr(state, e); + if (ATmatch(e, "True")) return true; + else if (ATmatch(e, "False")) return false; + else throw badTerm("expecting a boolean", e); +} + + Expr evalExpr2(EvalState & state, Expr e) { Expr e1, e2, e3, e4; @@ -165,6 +174,21 @@ Expr evalExpr2(EvalState & state, Expr e) if (ATmatch(e, "LetRec()", &e1)) return evalExpr(state, ATmake("Select(Rec(), \"body\")", e1)); + /* Conditionals. */ + if (ATmatch(e, "If(, , )", &e1, &e2, &e3)) { + if (evalBool(state, e1)) + return evalExpr(state, e2); + else + return evalExpr(state, e3); + } + + /* Equality. Just strings for now. */ + if (ATmatch(e, "OpEq(, )", &e1, &e2)) { + string s1 = evalString(state, e1); + string s2 = evalString(state, e2); + return s1 == s2 ? ATmake("True") : ATmake("False"); + } + /* Barf. */ throw badTerm("invalid expression", e); } diff --git a/src/fix-ng/fix.cc b/src/fix-ng/fix.cc index d791461bd..1c37a0b7b 100644 --- a/src/fix-ng/fix.cc +++ b/src/fix-ng/fix.cc @@ -32,12 +32,6 @@ static Expr evalExpr2(EvalState & state, Expr e) { /* Conditional. */ if (ATmatch(e, "If(, , )", &e1, &e2, &e3)) { - e1 = evalExpr(state, e1); - Expr x; - if (ATmatch(e1, "True")) x = e2; - else if (ATmatch(e1, "False")) x = e3; - else throw badTerm("expecting a boolean", e1); - return evalExpr(state, x); } /* Ad-hoc function for string matching. */