Skip to content
Snippets Groups Projects
Commit 3f17fa9a authored by Jan Trávníček's avatar Jan Trávníček
Browse files

add break and continue commands

parent 0a234c4d
No related branches found
No related tags found
1 merge request!125procedural aql
This commit is part of merge request !125. Comments created here will be created in the context of that merge request.
#ifndef _CLI_BREAK_COMMAND_H_
#define _CLI_BREAK_COMMAND_H_
#include <ast/Command.h>
#include <environment/Environment.h>
namespace cli {
class BreakCommand : public Command {
public:
CommandResult run ( Environment & /* environment */ ) const override {
return CommandResult::BREAK;
}
};
} /* namespace cli */
#endif /* _CLI_BREAK_COMMAND_H_ */
......@@ -17,6 +17,9 @@ public:
for ( size_t i = 0; i < m_commands.size ( ) && res == CommandResult::OK; ++ i ) {
res = m_commands [ i ]->run ( environment );
environment.clearTemporaries ( );
if ( res == CommandResult::CONTINUE || res == CommandResult::BREAK )
return res;
}
 
return res;
......
......@@ -7,6 +7,8 @@ enum class CommandResult {
OK,
QUIT,
RETURN,
CONTINUE,
BREAK,
EXCEPTION,
ERROR,
EOT
......
#ifndef _CLI_CONTINUE_COMMAND_H_
#define _CLI_CONTINUE_COMMAND_H_
#include <ast/Command.h>
#include <environment/Environment.h>
namespace cli {
class ContinueCommand : public Command {
public:
CommandResult run ( Environment & /* environment */ ) const override {
return CommandResult::CONTINUE;
}
};
} /* namespace cli */
#endif /* _CLI_CONTINUE_COMMAND_H_ */
......@@ -27,6 +27,14 @@ public:
break;
 
res = m_body->run ( environment );
if ( res == CommandResult::CONTINUE )
res = CommandResult::OK;
if ( res == CommandResult::BREAK ) {
res = CommandResult::OK;
break;
}
}
 
return res;
......
......@@ -21,7 +21,12 @@ cli::CommandResult Environment::execute ( std::shared_ptr < cli::LineInterface >
cli::CommandResult Environment::execute_line ( cli::CharSequence charSequence ) {
try {
cli::Parser parser = cli::Parser ( cli::Lexer ( std::move ( charSequence ) ) );
return parser.parse ( )->run ( * this );
cli::CommandResult res = parser.parse ( )->run ( * this );
if ( res == CommandResult::CONTINUE || res == CommandResult::BREAK )
throw std::logic_error ( "There is no loop to continue/break." );
return res;
} catch ( ... ) {
alib::ExceptionHandler::handle ( common::Streams::err );
return cli::CommandResult::EXCEPTION;
......
......@@ -35,6 +35,8 @@
#include <ast/command/InterpretCommand.h>
#include <ast/command/IfCommand.h>
#include <ast/command/WhileCommand.h>
#include <ast/command/BreakCommand.h>
#include <ast/command/ContinueCommand.h>
#include <ast/command/ReturnCommand.h>
 
#include <ast/expression/BatchExpression.h>
......@@ -523,6 +525,16 @@ std::unique_ptr < Command > Parser::command ( ) {
std::unique_ptr < Command > body = semicolon_command ( );
 
return std::make_unique < WhileCommand > ( std::move ( condition ), std::move ( body ) );
} else if ( check_nonreserved_kw ( "break" ) ) {
if ( globalScope ( ) )
throw exception::CommonException ( "Statement not available in global scope." );
match_nonreserved_kw ( "break" );
return std::make_unique < BreakCommand > ( );
} else if ( check_nonreserved_kw ( "continue" ) ) {
if ( globalScope ( ) )
throw exception::CommonException ( "Statement not available in global scope." );
match_nonreserved_kw ( "continue" );
return std::make_unique < ContinueCommand > ( );
} else {
throw exception::CommonException ( "Mismatched set " + ext::to_string ( getCheckOptions ( ) ) + " while expanding parse rule. Token is " + ( ( std::string ) m_current ) + "." );
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment