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

print statement and redesign results of batches

parent 7b99a66e
No related branches found
No related tags found
1 merge request!123Merge jt
Pipeline #61220 failed
#ifndef _CLI_RESULT_PRINT_STATEMENT_H_ #ifndef _CLI_PRINT_COMMAND_H_
#define _CLI_RESULT_PRINT_STATEMENT_H_ #define _CLI_PRINT_COMMAND_H_
   
#include <ast/Command.h>
#include <environment/Environment.h>
#include <ast/Statement.h> #include <ast/Statement.h>
#include <global/GlobalData.h> #include <global/GlobalData.h>
#include <registry/Registry.h> #include <registry/Registry.h>
   
namespace cli { namespace cli {
   
class ResultPrintStatement final : public Statement { class PrintCommand : public Command {
std::shared_ptr < Statement > m_command;
public: public:
ResultPrintStatement ( ) = default; PrintCommand ( std::shared_ptr < Statement > command ) : m_command ( std::move ( command ) ) {
}
   
std::shared_ptr < abstraction::Value > translateAndEval ( const std::shared_ptr < abstraction::Value > & prev, Environment & ) const override { CommandResult run ( Environment & environment ) const override {
std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getValuePrinterAbstraction ( prev->getType ( ) ); std::shared_ptr < abstraction::Value > value = m_command->translateAndEval ( nullptr, environment );
   
if ( res->numberOfParams ( ) == 0 ) std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getValuePrinterAbstraction ( value->getType ( ) );
return std::make_shared < abstraction::Void > ( nullptr );
   
res->attachInput ( prev, 0, false ); res->attachInput ( value, 0, false );
res->attachInput ( std::make_shared < abstraction::ValueHolder < std::ostream & > > ( nullptr, common::Streams::out, false ), 1, false ); res->attachInput ( std::make_shared < abstraction::ValueHolder < std::ostream & > > ( nullptr, common::Streams::out, false ), 1, false );
std::shared_ptr < abstraction::Value > result = res->eval ( ); std::shared_ptr < abstraction::Value > result = res->eval ( );
if ( ! result ) if ( ! result )
throw std::invalid_argument ( "Eval of result print statement failed." ); throw std::invalid_argument ( "Eval of result print statement failed." );
return result;
}
   
return CommandResult::OK;
}
}; };
   
} /* namespace cli */ } /* namespace cli */
   
#endif /* _CLI_RESULT_PRINT_STATEMENT_H_ */ #endif /* _CLI_PRINT_COMMAND_H_ */
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include <ast/statements/ContainerStatement.h> #include <ast/statements/ContainerStatement.h>
#include <ast/statements/ResultFileStatement.h> #include <ast/statements/ResultFileStatement.h>
#include <ast/statements/ResultVariableStatement.h> #include <ast/statements/ResultVariableStatement.h>
#include <ast/statements/ResultPrintStatement.h>
#include <ast/statements/VariableStatement.h> #include <ast/statements/VariableStatement.h>
#include <ast/statements/ValueStatement.h> #include <ast/statements/ValueStatement.h>
#include <ast/statements/FileStatement.h> #include <ast/statements/FileStatement.h>
...@@ -15,6 +14,7 @@ ...@@ -15,6 +14,7 @@
#include <ast/args/BindedArg.h> #include <ast/args/BindedArg.h>
#include <ast/args/ImmediateArg.h> #include <ast/args/ImmediateArg.h>
   
#include <ast/command/PrintCommand.h>
#include <ast/command/ExecuteCommand.h> #include <ast/command/ExecuteCommand.h>
#include <ast/command/QuitCommand.h> #include <ast/command/QuitCommand.h>
#include <ast/command/EOTCommand.h> #include <ast/command/EOTCommand.h>
...@@ -263,14 +263,19 @@ std::shared_ptr < Statement > Parser::statement ( ) { ...@@ -263,14 +263,19 @@ std::shared_ptr < Statement > Parser::statement ( ) {
std::shared_ptr < StatementList > Parser::statement_list ( ) { std::shared_ptr < StatementList > Parser::statement_list ( ) {
ext::vector < std::shared_ptr < Statement > > list; ext::vector < std::shared_ptr < Statement > > list;
list.emplace_back ( statement ( ) ); list.emplace_back ( statement ( ) );
while ( check ( cli::Lexer::TokenType::PIPE_SIGN ) ) { while ( check ( cli::Lexer::TokenType::PIPE_SIGN, cli::Lexer::TokenType::MORE_SIGN ) ) {
match ( cli::Lexer::TokenType::PIPE_SIGN ); if ( check ( cli::Lexer::TokenType::PIPE_SIGN ) ) {
list.emplace_back ( statement ( ) ); match ( cli::Lexer::TokenType::PIPE_SIGN );
list.emplace_back ( statement ( ) );
} else {
match ( cli::Lexer::TokenType::MORE_SIGN );
list.emplace_back ( out_redirect ( ) );
}
} }
return std::make_shared < StatementList > ( std::move ( list ) ); return std::make_shared < StatementList > ( std::move ( list ) );
} }
   
void Parser::out_redirect_file ( std::shared_ptr < StatementList > & list ) { std::unique_ptr < Statement > Parser::out_redirect_file ( ) {
std::unique_ptr < Arg > fileType; std::unique_ptr < Arg > fileType;
   
if ( check ( cli::Lexer::TokenType::LEFT_BRACKET ) ) { if ( check ( cli::Lexer::TokenType::LEFT_BRACKET ) ) {
...@@ -279,29 +284,16 @@ void Parser::out_redirect_file ( std::shared_ptr < StatementList > & list ) { ...@@ -279,29 +284,16 @@ void Parser::out_redirect_file ( std::shared_ptr < StatementList > & list ) {
match ( cli::Lexer::TokenType::RIGHT_BRACKET ); match ( cli::Lexer::TokenType::RIGHT_BRACKET );
} }
   
list->append ( std::make_unique < ResultFileStatement > ( file ( ), std::move ( fileType ) ) ); return std::make_unique < ResultFileStatement > ( file ( ), std::move ( fileType ) );
} }
   
void Parser::out_redirect ( std::shared_ptr < StatementList > & list ) { std::unique_ptr < Statement > Parser::out_redirect ( ) {
if ( check ( cli::Lexer::TokenType::DOLAR_SIGN ) ) { if ( check ( cli::Lexer::TokenType::DOLAR_SIGN ) ) {
match ( cli::Lexer::TokenType::DOLAR_SIGN ); match ( cli::Lexer::TokenType::DOLAR_SIGN );
std::unique_ptr < Arg > name = arg ( ); std::unique_ptr < Arg > name = arg ( );
list->append ( std::make_unique < ResultVariableStatement > ( std::move ( name ) ) ); return std::make_unique < ResultVariableStatement > ( std::move ( name ) );
} else if ( check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) {
return;
} else { } else {
out_redirect_file ( list ); return out_redirect_file ( );
}
}
void Parser::result ( std::shared_ptr < StatementList > & list ) {
if ( check ( cli::Lexer::TokenType::MORE_SIGN ) ) {
match ( cli::Lexer::TokenType::MORE_SIGN );
out_redirect ( list );
} else if ( check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) {
list->append ( std::make_unique < ResultPrintStatement > ( ) );
} else {
return;
} }
} }
   
...@@ -381,8 +373,11 @@ std::unique_ptr < Command > Parser::command ( ) { ...@@ -381,8 +373,11 @@ std::unique_ptr < Command > Parser::command ( ) {
if ( check_nonreserved_kw ( "execute" ) ) { if ( check_nonreserved_kw ( "execute" ) ) {
match_nonreserved_kw ( "execute" ); match_nonreserved_kw ( "execute" );
std::shared_ptr < StatementList > res = statement_list ( ); std::shared_ptr < StatementList > res = statement_list ( );
result ( res ); return std::make_unique < ExecuteCommand > ( std::move ( res ) );
return std::make_unique < ExecuteCommand > ( res ); } else if ( check_nonreserved_kw ( "print" ) ) {
match_nonreserved_kw ( "print" );
std::shared_ptr < StatementList > res = statement_list ( );
return std::make_unique < PrintCommand > ( std::move ( res ) );
} else if ( check_nonreserved_kw ( "quit" ) ) { } else if ( check_nonreserved_kw ( "quit" ) ) {
match_nonreserved_kw ( "quit" ); match_nonreserved_kw ( "quit" );
   
......
...@@ -138,11 +138,9 @@ public: ...@@ -138,11 +138,9 @@ public:
   
std::shared_ptr < StatementList > statement_list ( ); std::shared_ptr < StatementList > statement_list ( );
   
void out_redirect_file ( std::shared_ptr < StatementList > & list ); std::unique_ptr < Statement > out_redirect_file ( );
   
void out_redirect ( std::shared_ptr < StatementList > & list ); std::unique_ptr < Statement > out_redirect ( );
void result ( std::shared_ptr < StatementList > & list );
   
std::pair < bool, bool > introspect_cast_from_to ( ); std::pair < bool, bool > introspect_cast_from_to ( );
   
......
...@@ -142,7 +142,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) { ...@@ -142,7 +142,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < Sink > ( Sink::sink, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) ); abstraction::AlgorithmRegistry::registerAlgorithm < Sink > ( Sink::sink, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
   
cli::Environment environment; cli::Environment environment;
testLine ( "execute Source | Sink ^ - >", environment ); testLine ( "execute Source | Sink ^ -", environment );
} }
   
static std::unique_ptr < int > source; static std::unique_ptr < int > source;
...@@ -170,7 +170,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) { ...@@ -170,7 +170,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) {
{ {
source = std::make_unique < int > ( 1 ); source = std::make_unique < int > ( 1 );
cli::Environment environment; cli::Environment environment;
testLine ( "execute RvalueReferenceProvider | RvalueReferenceAcceptor - >", environment ); testLine ( "execute RvalueReferenceProvider | RvalueReferenceAcceptor -", environment );
} }
   
CHECK ( * target == 1 ); CHECK ( * target == 1 );
...@@ -180,7 +180,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) { ...@@ -180,7 +180,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) {
source = std::make_unique < int > ( 1 ); source = std::make_unique < int > ( 1 );
cli::Environment environment; cli::Environment environment;
CHECK_NOTHROW ( testLine ( "execute RvalueReferenceProvider > $tmp", environment ) ); CHECK_NOTHROW ( testLine ( "execute RvalueReferenceProvider > $tmp", environment ) );
CHECK_NOTHROW ( testLine ( "execute $tmp | RvalueReferenceAcceptor ^ - >", environment ) ); CHECK_NOTHROW ( testLine ( "execute $tmp | RvalueReferenceAcceptor ^ -", environment ) );
} }
   
CHECK ( * target == 1 ); CHECK ( * target == 1 );
...@@ -190,7 +190,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) { ...@@ -190,7 +190,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) {
source = std::make_unique < int > ( 1 ); source = std::make_unique < int > ( 1 );
cli::Environment environment; cli::Environment environment;
CHECK_NOTHROW ( testLine ( "execute RvalueReferenceProvider > $tmp", environment ) ); CHECK_NOTHROW ( testLine ( "execute RvalueReferenceProvider > $tmp", environment ) );
CHECK_THROWS ( testLine ( "execute $tmp | RvalueReferenceAcceptor - >", environment ) ); CHECK_THROWS ( testLine ( "execute $tmp | RvalueReferenceAcceptor -", environment ) );
} }
   
} }
...@@ -215,7 +215,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) { ...@@ -215,7 +215,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ConstReferenceAcceptor > ( ConstReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) ); abstraction::AlgorithmRegistry::registerAlgorithm < ConstReferenceAcceptor > ( ConstReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
   
cli::Environment environment; cli::Environment environment;
testLine ( "execute ConstReferenceProvider | ConstReferenceAcceptor - >", environment ); testLine ( "execute ConstReferenceProvider | ConstReferenceAcceptor -", environment );
} }
   
class ReferenceProvider { class ReferenceProvider {
...@@ -237,7 +237,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) { ...@@ -237,7 +237,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ReferenceAcceptor > ( ReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) ); abstraction::AlgorithmRegistry::registerAlgorithm < ReferenceAcceptor > ( ReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
   
cli::Environment environment; cli::Environment environment;
testLine ( "execute ReferenceProvider | ReferenceAcceptor - >", environment ); testLine ( "execute ReferenceProvider | ReferenceAcceptor -", environment );
} }
   
class ConstRvalueReferenceProvider { class ConstRvalueReferenceProvider {
...@@ -260,7 +260,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) { ...@@ -260,7 +260,7 @@ TEST_CASE ( "Cli", "[unit][cli]" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ConstRvalueReferenceAcceptor > ( ConstRvalueReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) ); abstraction::AlgorithmRegistry::registerAlgorithm < ConstRvalueReferenceAcceptor > ( ConstRvalueReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
   
cli::Environment environment; cli::Environment environment;
testLine ( "execute ConstRvalueReferenceProvider | ConstRvalueReferenceAcceptor ^ - >", environment ); testLine ( "execute ConstRvalueReferenceProvider | ConstRvalueReferenceAcceptor ^ -", environment );
} }
   
class Print { class Print {
......
...@@ -18,7 +18,7 @@ static std::string qPrepareString ( const std::string &file, const std::string & ...@@ -18,7 +18,7 @@ static std::string qPrepareString ( const std::string &file, const std::string &
} }
   
static std::string qExtendAlphabet ( const std::string & s1, const std::string & s2 ) { static std::string qExtendAlphabet ( const std::string & s1, const std::string & s2 ) {
return "execute string::GeneralAlphabet::add $" + s1 + " <( string::GeneralAlphabet::get $" + s2 + " ) >"; return "execute string::GeneralAlphabet::add $" + s1 + " <( string::GeneralAlphabet::get $" + s2 + " )";
} }
   
static std::string qGenString ( const size_t & len, const size_t &alph_len, const std::string & var ) { static std::string qGenString ( const size_t & len, const size_t &alph_len, const std::string & var ) {
......
...@@ -10,7 +10,7 @@ const size_t ALPHABET_SIZE = 4; ...@@ -10,7 +10,7 @@ const size_t ALPHABET_SIZE = 4;
const size_t RANDOM_ITERATIONS = 20; const size_t RANDOM_ITERATIONS = 20;
   
static std::string qExtendAlphabet ( const std::string & s1, const std::string & s2 ) { static std::string qExtendAlphabet ( const std::string & s1, const std::string & s2 ) {
return "execute string::GeneralAlphabet::add $" + s1 + " <( string::GeneralAlphabet::get $" + s2 + " ) > "; return "execute string::GeneralAlphabet::add $" + s1 + " <( string::GeneralAlphabet::get $" + s2 + " )";
} }
   
static std::string qGenString ( const size_t & len, const size_t &alph_len, const std::string & var ) { static std::string qGenString ( const size_t & len, const size_t &alph_len, const std::string & var ) {
......
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