From ac817ff33ac14961b1734581920c3374f304488b Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 17 Jul 2009 14:16:27 +0000 Subject: [PATCH] Change 'elseif' to 'else if' to better support C++ expectations --- .../chaiscript/language/chaiscript_eval.hpp | 4 +- .../chaiscript/language/chaiscript_parser.hpp | 37 ++++++++++--------- unittests/if_elseif.chai | 4 +- unittests/if_elseif_else.chai | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index e472f72..2de3ffa 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -525,13 +525,13 @@ namespace chaiscript retval = eval_token(ss, node->children[i+1]); cond = true; } - else if (node->children[i]->text == "elseif") { + else if (node->children[i]->text == "else if") { retval = eval_token(ss, node->children[i+1]); try { cond = dispatchkit::boxed_cast(retval); } catch (const dispatchkit::bad_boxed_cast &) { - throw Eval_Error("Elseif condition not boolean", node->children[i+1]); + throw Eval_Error("'else if' condition not boolean", node->children[i+1]); } if (cond) { retval = eval_token(ss, node->children[i+2]); diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 2e958eb..1609c91 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -846,29 +846,32 @@ namespace chaiscript while (has_matches) { while (Eol()); has_matches = false; - if (Keyword("elseif", true)) { - if (!Char('(')) { - throw Eval_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); - } + if (Keyword("else", true)) { + if (Keyword("if")) { + match_stack.back()->text = "else if"; + if (!Char('(')) { + throw Eval_Error("Incomplete 'else if' expression", File_Position(line, col), filename); + } - if (!(Expression() && Char(')'))) { - throw Eval_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); - } + if (!(Expression() && Char(')'))) { + throw Eval_Error("Incomplete 'else if' expression", File_Position(line, col), filename); + } - while (Eol()); + while (Eol()); - if (!Block()) { - throw Eval_Error("Incomplete 'elseif' block", File_Position(line, col), filename); + if (!Block()) { + throw Eval_Error("Incomplete 'else if' block", File_Position(line, col), filename); + } + has_matches = true; } - has_matches = true; - } - else if (Keyword("else", true)) { - while (Eol()); + else { + while (Eol()); - if (!Block()) { - throw Eval_Error("Incomplete 'else' block", File_Position(line, col), filename); + if (!Block()) { + throw Eval_Error("Incomplete 'else' block", File_Position(line, col), filename); + } + has_matches = true; } - has_matches = true; } } diff --git a/unittests/if_elseif.chai b/unittests/if_elseif.chai index 5bbae52..fc93a91 100644 --- a/unittests/if_elseif.chai +++ b/unittests/if_elseif.chai @@ -2,10 +2,10 @@ var i = 3 if (i == 2) { print("2") } -elseif (i == 4) { +else if (i == 4) { print("4") } -elseif (i == 3) { +else if (i == 3) { print("3") } diff --git a/unittests/if_elseif_else.chai b/unittests/if_elseif_else.chai index 785a8c6..db4969d 100644 --- a/unittests/if_elseif_else.chai +++ b/unittests/if_elseif_else.chai @@ -2,7 +2,7 @@ var i = 3 if (i == 2) { print("2") } -elseif (i == 4) { +else if (i == 4) { print("4") } else {