From 042df442a15e84d2039a47b0fc68b247a05b49b7 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 1 Jul 2009 17:13:52 +0000 Subject: [PATCH] Fixes to for loops. Added elseif and else --- chaiscript/chaiscript_parser.hpp | 40 ++++++++++++++++++++++++++++---- samples/lambda.chai | 4 ++-- samples/range.chai | 2 +- samples/scope.chai | 2 +- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/chaiscript/chaiscript_parser.hpp b/chaiscript/chaiscript_parser.hpp index 95a80d5..e79642b 100644 --- a/chaiscript/chaiscript_parser.hpp +++ b/chaiscript/chaiscript_parser.hpp @@ -714,6 +714,36 @@ namespace chaiscript throw Parse_Error("Incomplete 'if' block", File_Position(line, col), filename); } + bool has_matches = true; + while (has_matches) { + while (Eol()); + has_matches = false; + if (Keyword("elseif", true)) { + if (!Char('(')) { + throw Parse_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); + } + + if (!(Expression() && Char(')'))) { + throw Parse_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); + } + + while (Eol()); + + if (!Block()) { + throw Parse_Error("Incomplete 'elseif' block", File_Position(line, col), filename); + } + has_matches = true; + } + else if (Keyword("else", true)) { + while (Eol()); + + if (!Block()) { + throw Parse_Error("Incomplete 'else' block", File_Position(line, col), filename); + } + has_matches = true; + } + } + build_match(Token_Type::If, prev_stack_top); } @@ -751,7 +781,7 @@ namespace chaiscript bool For_Guards() { Equation(); - if (Char(';') && Expression() && Char(';') && Expression()) { + if (Char(';') && Expression() && Char(';') && Equation()) { return true; } else { @@ -1216,7 +1246,7 @@ namespace chaiscript } has_more = true; retval = true; - saw_eol = false; + saw_eol = true; } else if (If()) { if (!saw_eol) { @@ -1224,7 +1254,7 @@ namespace chaiscript } has_more = true; retval = true; - saw_eol = false; + saw_eol = true; } else if (While()) { if (!saw_eol) { @@ -1232,7 +1262,7 @@ namespace chaiscript } has_more = true; retval = true; - saw_eol = false; + saw_eol = true; } else if (For()) { if (!saw_eol) { @@ -1240,7 +1270,7 @@ namespace chaiscript } has_more = true; retval = true; - saw_eol = false; + saw_eol = true; } else if (Statement()) { if (!saw_eol) { diff --git a/samples/lambda.chai b/samples/lambda.chai index 81aafe6..a746d45 100644 --- a/samples/lambda.chai +++ b/samples/lambda.chai @@ -1,5 +1,5 @@ -var add2 = function(x, y) { x + y } -var add1 = function(x, f) { f(3,x) } +var add2 = fun(x, y) { x + y } +var add1 = fun(x, f) { f(3,x) } print(add2(3, 4)) print(add1(2, add2)) diff --git a/samples/range.chai b/samples/range.chai index 8e13119..63fb766 100644 --- a/samples/range.chai +++ b/samples/range.chai @@ -11,5 +11,5 @@ def for_each(container, function) var vec = [1,2,3,4,5,6,7,8,9, "hi", 4.5] -for_each(vec, function(x) { print(x); } ); +for_each(vec, fun(x) { print(x); } ); diff --git a/samples/scope.chai b/samples/scope.chai index 333ff7a..b5bfdbf 100644 --- a/samples/scope.chai +++ b/samples/scope.chai @@ -8,7 +8,7 @@ var x = 4 def do_it() { var x = 1 print(x) - var y = function(x) { x + 1 } + var y = fun(x) { x + 1 } print(y(9)) } do_it()