From 752421c15d4f24437b1a414d4961a09efcbbabf5 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 14 Nov 2019 08:26:46 +0100 Subject: [PATCH] return command in aql --- alib2cli/src/ast/command/CommandResult.h | 1 + alib2cli/src/ast/command/InterpretCommand.h | 2 +- alib2cli/src/ast/command/ReturnCommand.h | 27 +++++++++++++++++++++ alib2cli/src/parser/Parser.cpp | 9 +++++++ aql2/src/aql.cpp | 4 +-- aql2/src/prompt/Prompt.cpp | 6 ++++- 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 alib2cli/src/ast/command/ReturnCommand.h diff --git a/alib2cli/src/ast/command/CommandResult.h b/alib2cli/src/ast/command/CommandResult.h index afa6d05bf8..65282cd23a 100644 --- a/alib2cli/src/ast/command/CommandResult.h +++ b/alib2cli/src/ast/command/CommandResult.h @@ -6,6 +6,7 @@ namespace cli { enum class CommandResult { OK, QUIT, + RETURN, EXCEPTION, ERROR, EOT diff --git a/alib2cli/src/ast/command/InterpretCommand.h b/alib2cli/src/ast/command/InterpretCommand.h index afd07d79fa..ca3be1733c 100644 --- a/alib2cli/src/ast/command/InterpretCommand.h +++ b/alib2cli/src/ast/command/InterpretCommand.h @@ -24,7 +24,7 @@ public: CommandResult state = environment.execute ( std::make_shared < cli::IstreamLineInterface < std::ifstream > > ( cli::IstreamLineInterface < std::ifstream > ( std::move ( ifs ) ) ) ); - if ( state != cli::CommandResult::QUIT ) + if ( state != cli::CommandResult::QUIT && state != cli::CommandResult::RETURN ) state = cli::CommandResult::OK; return state; diff --git a/alib2cli/src/ast/command/ReturnCommand.h b/alib2cli/src/ast/command/ReturnCommand.h new file mode 100644 index 0000000000..9b9de6bceb --- /dev/null +++ b/alib2cli/src/ast/command/ReturnCommand.h @@ -0,0 +1,27 @@ +#ifndef _CLI_RETURN_COMMAND_H_ +#define _CLI_RETURN_COMMAND_H_ + +#include <ast/Command.h> +#include <environment/Environment.h> +#include <ast/Statement.h> + +namespace cli { + +class ReturnCommand : public Command { + std::shared_ptr < Statement > m_command; + +public: + ReturnCommand ( std::shared_ptr < Statement > command ) : m_command ( std::move ( command ) ) { + } + + CommandResult run ( Environment & environment ) const override { + if ( m_command ) + environment.setResult ( m_command->translateAndEval ( nullptr, environment ) ); + + return CommandResult::RETURN; + } +}; + +} /* namespace cli */ + +#endif /* _CLI_RETURN_COMMAND_H_ */ diff --git a/alib2cli/src/parser/Parser.cpp b/alib2cli/src/parser/Parser.cpp index 2839050572..cdb6a08063 100644 --- a/alib2cli/src/parser/Parser.cpp +++ b/alib2cli/src/parser/Parser.cpp @@ -34,6 +34,7 @@ #include <ast/command/EvalCommand.h> #include <ast/command/InterpretCommand.h> #include <ast/command/IfCommand.h> +#include <ast/command/ReturnCommand.h> #include <ast/expression/BinaryExpression.h> #include <ast/expression/PrefixExpression.h> @@ -400,6 +401,14 @@ std::unique_ptr < Command > Parser::command ( ) { res = statement_list ( ); return std::make_unique < QuitCommand > ( res ); + } else if ( check_nonreserved_kw ( "return" ) ) { + match_nonreserved_kw ( "return" ); + + std::shared_ptr < StatementList > res; + if ( ! check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) + res = statement_list ( ); + + return std::make_unique < ReturnCommand > ( res ); } else if ( check_nonreserved_kw ( "help" ) ) { match_nonreserved_kw ( "help" ); std::unique_ptr < cli::Arg > command = optional_arg ( ); diff --git a/aql2/src/aql.cpp b/aql2/src/aql.cpp index 28afac30ae..9ea7e17add 100644 --- a/aql2/src/aql.cpp +++ b/aql2/src/aql.cpp @@ -162,9 +162,9 @@ int main ( int argc, char * argv[] ) { /* --------------------------------------------------------------------------------------------------------- */ - if ( res == cli::CommandResult::QUIT ) + if ( res == cli::CommandResult::QUIT || res == cli::CommandResult::RETURN ) return cli::ResultInterpret::cli ( Prompt::getPrompt ( ).getEnvironment ( ).getResult ( ) ); - else if ( res == cli::CommandResult::EOT ) + else if ( res == cli::CommandResult::EOT || res == cli::CommandResult::OK ) return 0; else return 4; diff --git a/aql2/src/prompt/Prompt.cpp b/aql2/src/prompt/Prompt.cpp index 926b13a4f1..f7e26beb4e 100644 --- a/aql2/src/prompt/Prompt.cpp +++ b/aql2/src/prompt/Prompt.cpp @@ -11,15 +11,19 @@ Prompt::Prompt ( cli::Environment environment ) : m_environment ( std::move ( en } cli::CommandResult Prompt::run ( ) { + cli::CommandResult res = cli::CommandResult::OK; + while ( ! m_lineInterfaces.empty ( ) ) { cli::CommandResult state = getEnvironment ( ).execute ( m_lineInterfaces.front ( ) ); if ( state == cli::CommandResult::QUIT ) return state; + if ( state == cli::CommandResult::RETURN ) + res = cli::CommandResult::RETURN; m_lineInterfaces.pop_front ( ); } - return cli::CommandResult::OK; + return res; } -- GitLab