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

revise cli options+use in algorithm categorisation

parent a7abb9ee
No related branches found
No related tags found
No related merge requests found
#ifndef _CLI_CATEGORY_OPTION_H_
#define _CLI_CATEGORY_OPTION_H_
#include <ast/Option.h>
#include <abstraction/common/AlgorithmCategories.hpp>
#include <exception/CommonException.h>
namespace cli {
class CategoryOption final : public Option {
std::string m_key;
public:
CategoryOption ( std::string key ) : m_key ( std::move ( key ) ) {
}
virtual void eval ( SingleStatement & statement ) const override {
statement.setCategory ( abstraction::AlgorithmCategories::algorithmCategory ( m_key ) );
}
};
} /* namespace cli */
#endif /* _CLI_CATEGORY_OPTION_H_ */
...@@ -7,8 +7,8 @@ namespace cli { ...@@ -7,8 +7,8 @@ namespace cli {
   
class HL3Option final : public Option { class HL3Option final : public Option {
public: public:
virtual void eval ( SingleStatement & statement ) const override { virtual void eval ( SingleStatement & ) const override {
statement.setHL3 ( ); std::cout << "The cake is a lie, as well as the release date of HL3. GLaDOS told me." << std::endl;
} }
}; };
   
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
   
namespace cli { namespace cli {
   
SingleStatement::SingleStatement ( std::unique_ptr < Arg > name, ext::vector < std::unique_ptr < Param > > params, ext::vector < std::unique_ptr < Option > > options ) : m_name ( std::move ( name ) ), m_params ( std::move ( params ) ), m_options ( std::move ( options ) ) { SingleStatement::SingleStatement ( std::unique_ptr < Arg > name, ext::vector < std::unique_ptr < Param > > params, ext::vector < std::unique_ptr < Option > > options ) : m_name ( std::move ( name ) ), m_params ( std::move ( params ) ), m_options ( std::move ( options ) ), m_category ( abstraction::AlgorithmCategories::AlgorithmCategory::NONE ) {
for ( const std::unique_ptr < Option > & option : m_options ) { for ( const std::unique_ptr < Option > & option : m_options ) {
option->eval ( * this ); option->eval ( * this );
} }
...@@ -25,7 +25,11 @@ std::shared_ptr < abstraction::OperationAbstraction > SingleStatement::translate ...@@ -25,7 +25,11 @@ std::shared_ptr < abstraction::OperationAbstraction > SingleStatement::translate
   
std::string name = m_name->eval ( environment ); std::string name = m_name->eval ( environment );
   
return abstraction::AlgorithmHelper::eval ( name, params, moves, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT ); return abstraction::AlgorithmHelper::eval ( name, params, moves, m_category );
}
void SingleStatement::setCategory ( abstraction::AlgorithmCategories::AlgorithmCategory category ) {
m_category = category;
} }
   
} /* namespace cli */ } /* namespace cli */
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <abstraction/Registry.h> #include <abstraction/Registry.h>
#include <exception/CommonException.h> #include <exception/CommonException.h>
#include <iostream> #include <iostream>
#include <abstraction/common/AlgorithmCategories.hpp>
   
namespace cli { namespace cli {
   
...@@ -13,14 +14,14 @@ class SingleStatement final : public Statement { ...@@ -13,14 +14,14 @@ class SingleStatement final : public Statement {
ext::vector < std::unique_ptr < Param > > m_params; ext::vector < std::unique_ptr < Param > > m_params;
ext::vector < std::unique_ptr < Option > > m_options; ext::vector < std::unique_ptr < Option > > m_options;
   
abstraction::AlgorithmCategories::AlgorithmCategory m_category;
public: public:
SingleStatement ( std::unique_ptr < cli::Arg > name, ext::vector < std::unique_ptr < Param > > params, ext::vector < std::unique_ptr < Option > > options ); SingleStatement ( std::unique_ptr < cli::Arg > name, ext::vector < std::unique_ptr < Param > > params, ext::vector < std::unique_ptr < Option > > options );
   
virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > & prev, Environment & environment ) const override; virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > & prev, Environment & environment ) const override;
   
void setHL3 ( ) { void setCategory ( abstraction::AlgorithmCategories::AlgorithmCategory category );
std::cout << "The cake is a lie, as well as the release date of HL3. GLaDOS told me." << std::endl;
}
}; };
   
} /* namespace cli */ } /* namespace cli */
......
...@@ -31,19 +31,30 @@ ...@@ -31,19 +31,30 @@
#include <command/SetCommand.h> #include <command/SetCommand.h>
   
#include <ast/options/HL3Option.h> #include <ast/options/HL3Option.h>
#include <ast/options/CategoryOption.h>
   
#include <primitive/Integer.h> #include <primitive/Integer.h>
#include <primitive/String.h> #include <primitive/String.h>
   
namespace cli { namespace cli {
   
std::unique_ptr < Option > Parser::category_option ( ) {
if ( check_nonreserved_kw ( "default", "test", "efficient", "student" ) ) {
std::string value = getTokenValue ( );
match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER );
return std::make_unique < CategoryOption > ( value );
} else {
return nullptr;
}
}
std::unique_ptr < Option > Parser::option ( ) { std::unique_ptr < Option > Parser::option ( ) {
match ( cli::Lexer::TokenType::COLON_SIGN ); match ( cli::Lexer::TokenType::COLON_SIGN );
if ( check_nonreserved_kw ( "hl3" ) ) { if ( check_nonreserved_kw ( "hl3" ) ) {
match_nonreserved_kw ( "hl3" ); match_nonreserved_kw ( "hl3" );
return std::make_unique < HL3Option > ( ); return std::make_unique < HL3Option > ( );
} else { } else {
throw exception::CommonException ( "Mismatched set while expanding option rule." ); return nullptr;
} }
} }
   
...@@ -178,7 +189,13 @@ std::shared_ptr < Statement > Parser::single_statement ( ) { ...@@ -178,7 +189,13 @@ std::shared_ptr < Statement > Parser::single_statement ( ) {
std::unique_ptr < Arg > name = std::make_unique < ImmediateArg > ( matchIdentifier ( ) ); std::unique_ptr < Arg > name = std::make_unique < ImmediateArg > ( matchIdentifier ( ) );
ext::vector < std::unique_ptr < Option > > options; ext::vector < std::unique_ptr < Option > > options;
while ( check ( cli::Lexer::TokenType::COLON_SIGN ) ) { while ( check ( cli::Lexer::TokenType::COLON_SIGN ) ) {
options.emplace_back ( option ( ) ); match ( cli::Lexer::TokenType::COLON_SIGN );
std::unique_ptr < Option > res = category_option ( );
if ( ! res )
res = option ( );
if ( ! res )
throw exception::CommonException ( "Option not recognised" );
options.emplace_back ( std::move ( res ) );
} }
ext::vector < std::unique_ptr < Param > > params; ext::vector < std::unique_ptr < Param > > params;
while ( ! check ( cli::Lexer::TokenType::OUT_REDIRECT ) && ! check ( cli::Lexer::TokenType::PIPE_SIGN ) && ! check ( cli::Lexer::TokenType::END ) && ! check ( cli::Lexer::TokenType::RIGHT_PAREN ) ) { while ( ! check ( cli::Lexer::TokenType::OUT_REDIRECT ) && ! check ( cli::Lexer::TokenType::PIPE_SIGN ) && ! check ( cli::Lexer::TokenType::END ) && ! check ( cli::Lexer::TokenType::RIGHT_PAREN ) ) {
......
...@@ -32,8 +32,9 @@ public: ...@@ -32,8 +32,9 @@ public:
return ext::orAll ( m_current.m_type == tokens ... ); return ext::orAll ( m_current.m_type == tokens ... );
} }
   
bool check_nonreserved_kw ( const std::string & kw ) const { template < class ... NonreservedTokens >
return m_current.m_type == Lexer::TokenType::IDENTIFIER && m_current.m_value == kw; bool check_nonreserved_kw ( const NonreservedTokens & ... kw ) const {
return m_current.m_type == Lexer::TokenType::IDENTIFIER && ext::orAll ( m_current.m_value == kw ... );
} }
   
template < class ... Tokens > template < class ... Tokens >
...@@ -79,6 +80,7 @@ public: ...@@ -79,6 +80,7 @@ public:
return m_current.m_value; return m_current.m_value;
} }
   
std::unique_ptr < Option > category_option ( );
std::unique_ptr < Option > option ( ); std::unique_ptr < Option > option ( );
   
std::unique_ptr < Arg > arg ( ); std::unique_ptr < Arg > arg ( );
......
...@@ -237,4 +237,3 @@ void CliTest::testConstRvalueReferencePassing ( ) { ...@@ -237,4 +237,3 @@ void CliTest::testConstRvalueReferencePassing ( ) {
cli::Parser parser ( cli::Lexer ( "execute ConstRvalueReferenceProvider | ConstRvalueReferenceAcceptor ^ - >" ) ); cli::Parser parser ( cli::Lexer ( "execute ConstRvalueReferenceProvider | ConstRvalueReferenceAcceptor ^ - >" ) );
parser.parse ( )->run ( environment ); parser.parse ( )->run ( environment );
} }
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