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

rework exporting result to variable in cli

parent 1d4fd8e7
No related branches found
No related tags found
No related merge requests found
#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_ */
#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_ */
......@@ -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;
......
......@@ -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 );
......
......@@ -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" ) );
......
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