From d25324caa688b3ea82dddb6971135a511ae69eb4 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 2 Aug 2017 13:53:26 +0200 Subject: [PATCH] rework exporting result to variable in cli --- .../src/ast/BindedVariableResultStatement.h | 24 ++++++++++++++ alib2cli/src/command/ExportCommand.h | 32 ------------------- alib2cli/src/command/HelpCommand.h | 8 ----- alib2cli/src/parser/Parser.cpp | 15 ++++----- alib2cli/test-src/cli/CliTest.cpp | 2 +- 5 files changed, 31 insertions(+), 50 deletions(-) create mode 100644 alib2cli/src/ast/BindedVariableResultStatement.h delete mode 100644 alib2cli/src/command/ExportCommand.h diff --git a/alib2cli/src/ast/BindedVariableResultStatement.h b/alib2cli/src/ast/BindedVariableResultStatement.h new file mode 100644 index 0000000000..66c4f9f287 --- /dev/null +++ b/alib2cli/src/ast/BindedVariableResultStatement.h @@ -0,0 +1,24 @@ +#ifndef _CLI_BINDED_VARIABLE_RESULT_STATEMENT_H_ +#define _CLI_BINDED_VARIABLE_RESULT_STATEMENT_H_ + +#include <ast/Statement.h> + +namespace cli { + +class BindedVariableResultStatement : public Statement { + std::string m_bind; + +public: + BindedVariableResultStatement ( std::string bind ) : m_bind ( bind ) { + } + + virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > & prev, Environment & environment ) const override { + environment.setVariable ( m_bind, prev ); + return prev; + } + +}; + +} /* namespace cli */ + +#endif /* _CLI_BINDED_VARIABLE_RESULT_STATEMENT_H_ */ diff --git a/alib2cli/src/command/ExportCommand.h b/alib2cli/src/command/ExportCommand.h deleted file mode 100644 index e024eb06ab..0000000000 --- a/alib2cli/src/command/ExportCommand.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef _CLI_EXPORT_COMMAND_H_ -#define _CLI_EXPORT_COMMAND_H_ - -#include <command/Command.h> -#include <environment/Environment.h> -#include <ast/Statement.h> - -namespace cli { - -class ExportCommand : public Command { - std::string m_name; - std::shared_ptr < Statement > m_command; - -public: - ExportCommand ( std::string name, std::shared_ptr < StatementList > command ) : m_name ( std::move ( name ) ), m_command ( command ) { - } - - virtual Command::Result run ( Environment & environment ) const override { - try { - std::shared_ptr < abstraction::OperationAbstraction > result = m_command->translateAndEval ( nullptr, environment ); - environment.setVariable ( m_name, result ); - return Command::Result::OK; - } catch ( const exception::CommonException & exception ) { - alib::XmlDataFactory::toStdout ( exception ); - return Command::Result::EXCEPTION; - } - } -}; - -} /* namespace cli */ - -#endif /* _CLI_EXPORT_COMMAND_H_ */ diff --git a/alib2cli/src/command/HelpCommand.h b/alib2cli/src/command/HelpCommand.h index df9fcbf3bc..366f1d0abc 100644 --- a/alib2cli/src/command/HelpCommand.h +++ b/alib2cli/src/command/HelpCommand.h @@ -16,14 +16,6 @@ public: virtual Command::Result run ( Environment & ) const override { if ( m_command == "execute" ) { std::cout << "Execute command executes statements and either prints the result or writes the result to a file" << std::endl; - } else if ( m_command == "export" ) { - std::cout << "Export command stores the result of statements in a variable" << std::endl; - std::cout << "" << std::endl; - std::cout << "Syntax:" << std::endl; - std::cout << "execute variable = statement_list" << std::endl; - std::cout << "" << std::endl; - std::cout << "variable is an identifier under which the result should be stored." << std::endl; - std::cout << "for details of syntax of statement_list see help of execute statement." << std::endl; } else if ( m_command == "" ) { std::cout << "Simple help for the query language" << std::endl; std::cout << "" << std::endl; diff --git a/alib2cli/src/parser/Parser.cpp b/alib2cli/src/parser/Parser.cpp index 839fc620c9..baafb13f05 100644 --- a/alib2cli/src/parser/Parser.cpp +++ b/alib2cli/src/parser/Parser.cpp @@ -4,6 +4,7 @@ #include <ast/SingleStatement.h> #include <ast/FileResultStatement.h> #include <ast/BindedFileResultStatement.h> +#include <ast/BindedVariableResultStatement.h> #include <ast/PrintResultStatement.h> #include <ast/PreviousResultStatement.h> @@ -17,7 +18,6 @@ #include <ast/CastParam.h> #include <command/ExecuteCommand.h> -#include <command/ExportCommand.h> #include <command/QuitCommand.h> #include <command/HelpCommand.h> @@ -128,6 +128,11 @@ void Parser::out_redirect ( std::shared_ptr < StatementList > & list ) { std::string name = getTokenValue ( ); match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER ); list->append ( std::make_unique < BindedFileResultStatement > ( std::move ( name ) ) ); + } else if ( check ( cli::Lexer::TokenType::DOLAR_SIGN ) ) { + match ( cli::Lexer::TokenType::DOLAR_SIGN ); + std::string name = getTokenValue ( ); + match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER ); + list->append ( std::make_unique < BindedVariableResultStatement > ( std::move ( name ) ) ); } else { std::string filename = matchIdentifier ( ); list->append ( std::make_unique < FileResultStatement > ( std::move ( filename ) ) ); @@ -160,14 +165,6 @@ std::unique_ptr < Command > Parser::parse ( ) { result ( res ); match ( cli::Lexer::TokenType::END ); return std::make_unique < ExecuteCommand > ( res ); - } else if ( check_nonreserved_kw ( "export" ) ) { - match_nonreserved_kw ( "export" ); - std::string name = getTokenValue ( ); - match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER ); - match ( cli::Lexer::TokenType::EQUAL_SIGN ); - std::shared_ptr < StatementList > res = statement_list ( ); - match ( cli::Lexer::TokenType::END ); - return std::make_unique < ExportCommand > ( std::move ( name ), res ); } else if ( check_nonreserved_kw ( "quit" ) ) { match_nonreserved_kw ( "quit" ); match ( cli::Lexer::TokenType::END ); diff --git a/alib2cli/test-src/cli/CliTest.cpp b/alib2cli/test-src/cli/CliTest.cpp index b0480148d3..8b2ce29b5a 100644 --- a/alib2cli/test-src/cli/CliTest.cpp +++ b/alib2cli/test-src/cli/CliTest.cpp @@ -91,7 +91,7 @@ void CliTest::testCreateUnique ( ) { parser = cli::Parser ( cli::Lexer ( "execute One | Add <( Add (int) <:2 <(One) ) - | (double) Neg - | Divide - <(One | (double) Add <(One) - )" ) ); parser.parse ( )->run ( environment ); - parser = cli::Parser ( cli::Lexer ( "export res = One" ) ); + parser = cli::Parser ( cli::Lexer ( "execute One > $res" ) ); parser.parse ( )->run ( environment ); parser = cli::Parser ( cli::Lexer ( "execute $res" ) ); -- GitLab