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

eval and interpret commands in aql

parent 4aaa81fa
No related branches found
No related tags found
1 merge request!125procedural aql
#ifndef _CLI_EVAL_COMMAND_H_
#define _CLI_EVAL_COMMAND_H_
#include <ast/Command.h>
#include <environment/Environment.h>
#include <readline/StringLineInterface.h>
#include <iostream>
namespace cli {
class EvalCommand : public Command {
std::string m_code;
public:
EvalCommand ( std::string code ) : m_code ( std::move ( code ) ) {
}
CommandResult run ( Environment & environment ) const override {
CommandResult state = environment.execute ( std::make_shared < cli::StringLineInterface > ( cli::StringLineInterface ( m_code ) ) );
if ( state != cli::CommandResult::QUIT )
state = cli::CommandResult::OK;
return state;
}
};
} /* namespace cli */
#endif /* _CLI_EVAL_COMMAND_H_ */
#ifndef _CLI_INTERPRET_COMMAND_H_
#define _CLI_INTERPRET_COMMAND_H_
#include <ast/Command.h>
#include <environment/Environment.h>
#include <readline/IstreamLineInterface.h>
#include <fstream>
namespace cli {
class InterpretCommand : public Command {
std::string m_fileName;
public:
InterpretCommand ( std::string libraryName ) : m_fileName ( std::move ( libraryName ) ) {
}
CommandResult run ( Environment & environment ) const override {
std::ifstream ifs ( m_fileName );
if ( ! ifs.is_open ( ) )
throw exception::CommonException ( "File '" + m_fileName + "' not found." );
CommandResult state = environment.execute ( std::make_shared < cli::IstreamLineInterface < std::ifstream > > ( cli::IstreamLineInterface < std::ifstream > ( std::move ( ifs ) ) ) );
if ( state != cli::CommandResult::QUIT )
state = cli::CommandResult::OK;
return state;
}
};
} /* namespace cli */
#endif /* _CLI_INTERPRET_COMMAND_H_ */
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include <ast/command/UnloadCommand.h> #include <ast/command/UnloadCommand.h>
#include <ast/command/CalcCommand.h> #include <ast/command/CalcCommand.h>
#include <ast/command/BlockCommand.h> #include <ast/command/BlockCommand.h>
#include <ast/command/EvalCommand.h>
#include <ast/command/InterpretCommand.h>
   
#include <ast/expression/BinaryExpression.h> #include <ast/expression/BinaryExpression.h>
#include <ast/expression/PrefixExpression.h> #include <ast/expression/PrefixExpression.h>
...@@ -435,6 +437,19 @@ std::unique_ptr < Command > Parser::command ( ) { ...@@ -435,6 +437,19 @@ std::unique_ptr < Command > Parser::command ( ) {
return std::make_unique < CalcCommand > ( std::move ( expr ) ); return std::make_unique < CalcCommand > ( std::move ( expr ) );
} else if ( check_nonreserved_kw ( "begin" ) ) { } else if ( check_nonreserved_kw ( "begin" ) ) {
return std::make_unique < BlockCommand > ( block ( ) ); return std::make_unique < BlockCommand > ( block ( ) );
} else if ( check_nonreserved_kw ( "eval" ) ) {
match_nonreserved_kw ( "eval" );
std::string evalString = getTokenValue ( );
match ( cli::Lexer::TokenType::UNSIGNED, cli::Lexer::TokenType::IDENTIFIER, cli::Lexer::TokenType::STRING );
return std::make_unique < EvalCommand > ( std::move ( evalString ) );
} else if ( check_nonreserved_kw ( "interpret" ) ) {
match_nonreserved_kw ( "interpret" );
setHint ( Lexer::Hint::FILE );
std::string fileName = getTokenValue ( );
match ( cli::Lexer::TokenType::FILE, cli::Lexer::TokenType::STRING );
return std::make_unique < InterpretCommand > ( std::move ( fileName ) );
} else { } else {
throw exception::CommonException ( "Mismatched set " + ext::to_string ( getCheckOptions ( ) ) + " while expanding parse rule. Token is " + ( ( std::string ) m_current ) + "." ); throw exception::CommonException ( "Mismatched set " + ext::to_string ( getCheckOptions ( ) ) + " while expanding parse rule. Token is " + ( ( std::string ) m_current ) + "." );
} }
......
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