Added for loop

This commit is contained in:
Jonathan Turner 2009-07-01 13:33:29 +00:00
parent bb174b37a6
commit 3214f427ff
2 changed files with 53 additions and 25 deletions

View File

@ -356,8 +356,7 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value();
}
break;
/*
case(TokenType::For_Block) : {
case(Token_Type::For) : {
dispatchkit::Boxed_Value condition;
bool cond;
@ -399,7 +398,6 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value();
}
break;
*/
case (Token_Type::Def) : {
std::vector<std::string> param_names;
@ -463,22 +461,6 @@ namespace chaiscript
throw BreakLoop(node);
}
break;
/*
case (TokenType::Statement) :
case (TokenType::Carriage_Return) :
case (TokenType::Semicolon) :
case (TokenType::Comment) :
case (TokenType::Operator) :
case (TokenType::Whitespace) :
case (TokenType::Parens_Open) :
case (TokenType::Parens_Close) :
case (TokenType::Square_Open) :
case (TokenType::Square_Close) :
case (TokenType::Curly_Open) :
case (TokenType::Curly_Close) :
case (TokenType::Comma) :
break;
*/
}
return retval;

View File

@ -701,17 +701,17 @@ namespace chaiscript
retval = true;
if (!Char('(')) {
throw Parse_Error("Incomplete if expression", File_Position(line, col), filename);
throw Parse_Error("Incomplete 'if' expression", File_Position(line, col), filename);
}
if (!(Expression() && Char(')'))) {
throw Parse_Error("Incomplete if expression", File_Position(line, col), filename);
throw Parse_Error("Incomplete 'if' expression", File_Position(line, col), filename);
}
while (Eol());
if (!Block()) {
throw Parse_Error("Incomplete if block", File_Position(line, col), filename);
throw Parse_Error("Incomplete 'if' block", File_Position(line, col), filename);
}
build_match(Token_Type::If, prev_stack_top);
@ -729,17 +729,17 @@ namespace chaiscript
retval = true;
if (!Char('(')) {
throw Parse_Error("Incomplete while expression", File_Position(line, col), filename);
throw Parse_Error("Incomplete 'while' expression", File_Position(line, col), filename);
}
if (!(Expression() && Char(')'))) {
throw Parse_Error("Incomplete while expression", File_Position(line, col), filename);
throw Parse_Error("Incomplete 'while' expression", File_Position(line, col), filename);
}
while (Eol());
if (!Block()) {
throw Parse_Error("Incomplete while block", File_Position(line, col), filename);
throw Parse_Error("Incomplete 'while' block", File_Position(line, col), filename);
}
build_match(Token_Type::While, prev_stack_top);
@ -748,6 +748,44 @@ namespace chaiscript
return retval;
}
bool For_Guards() {
Equation();
if (Char(';') && Expression() && Char(';') && Expression()) {
return true;
}
else {
throw Parse_Error("Incomplete conditions in 'for' loop", File_Position(line, col), filename);
}
}
bool For() {
bool retval = false;
int prev_stack_top = match_stack.size();
if (Keyword("for")) {
retval = true;
if (!Char('(')) {
throw Parse_Error("Incomplete 'for' expression", File_Position(line, col), filename);
}
if (!(For_Guards() && Char(')'))) {
throw Parse_Error("Incomplete 'for' expression", File_Position(line, col), filename);
}
while (Eol());
if (!Block()) {
throw Parse_Error("Incomplete 'for' block", File_Position(line, col), filename);
}
build_match(Token_Type::For, prev_stack_top);
}
return retval;
}
bool Block() {
bool retval = false;
@ -1196,6 +1234,14 @@ namespace chaiscript
retval = true;
saw_eol = false;
}
else if (For()) {
if (!saw_eol) {
throw Parse_Error("Two function definitions missing line separator", match_stack.back());
}
has_more = true;
retval = true;
saw_eol = false;
}
else if (Statement()) {
if (!saw_eol) {
throw Parse_Error("Two expressions missing line separator", match_stack.back());