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

unify batch and expressions

parent 752421c1
No related branches found
No related tags found
1 merge request!125procedural aql
...@@ -3,19 +3,19 @@ ...@@ -3,19 +3,19 @@
   
#include <ast/Command.h> #include <ast/Command.h>
#include <environment/Environment.h> #include <environment/Environment.h>
#include <ast/Statement.h> #include <ast/Expression.h>
   
namespace cli { namespace cli {
   
class ExecuteCommand : public Command { class ExecuteCommand : public Command {
std::shared_ptr < Statement > m_command; std::unique_ptr < Expression > m_expr;
   
public: public:
ExecuteCommand ( std::shared_ptr < StatementList > command ) : m_command ( std::move ( command ) ) { ExecuteCommand ( std::unique_ptr < Expression > expr ) : m_expr ( std::move ( expr ) ) {
} }
   
CommandResult run ( Environment & environment ) const override { CommandResult run ( Environment & environment ) const override {
m_command->translateAndEval ( nullptr, environment ); m_expr->translateAndEval ( environment );
return CommandResult::OK; return CommandResult::OK;
} }
}; };
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
   
#include <ast/Command.h> #include <ast/Command.h>
#include <environment/Environment.h> #include <environment/Environment.h>
#include <ast/Statement.h> #include <ast/Expression.h>
   
#include <global/GlobalData.h> #include <global/GlobalData.h>
#include <registry/Registry.h> #include <registry/Registry.h>
...@@ -11,14 +11,14 @@ ...@@ -11,14 +11,14 @@
namespace cli { namespace cli {
   
class PrintCommand : public Command { class PrintCommand : public Command {
std::shared_ptr < Statement > m_command; std::unique_ptr < Expression > m_expr;
   
public: public:
PrintCommand ( std::shared_ptr < Statement > command ) : m_command ( std::move ( command ) ) { PrintCommand ( std::unique_ptr < Expression > expr ) : m_expr ( std::move ( expr ) ) {
} }
   
CommandResult run ( Environment & environment ) const override { CommandResult run ( Environment & environment ) const override {
std::shared_ptr < abstraction::Value > value = m_command->translateAndEval ( nullptr, environment ); std::shared_ptr < abstraction::Value > value = m_expr->translateAndEval ( environment );
   
std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getValuePrinterAbstraction ( value->getType ( ) ); std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getValuePrinterAbstraction ( value->getType ( ) );
   
......
...@@ -3,20 +3,20 @@ ...@@ -3,20 +3,20 @@
   
#include <ast/Command.h> #include <ast/Command.h>
#include <environment/Environment.h> #include <environment/Environment.h>
#include <ast/Statement.h> #include <ast/Expression.h>
   
namespace cli { namespace cli {
   
class QuitCommand : public Command { class QuitCommand : public Command {
std::shared_ptr < Statement > m_command; std::unique_ptr < Expression > m_expr;
   
public: public:
QuitCommand ( std::shared_ptr < Statement > command ) : m_command ( std::move ( command ) ) { QuitCommand ( std::unique_ptr < Expression > expr ) : m_expr ( std::move ( expr ) ) {
} }
   
CommandResult run ( Environment & environment ) const override { CommandResult run ( Environment & environment ) const override {
if ( m_command ) if ( m_expr )
environment.setResult ( m_command->translateAndEval ( nullptr, environment ) ); environment.setResult ( m_expr->translateAndEval ( environment ) );
   
return CommandResult::QUIT; return CommandResult::QUIT;
} }
......
...@@ -3,20 +3,20 @@ ...@@ -3,20 +3,20 @@
   
#include <ast/Command.h> #include <ast/Command.h>
#include <environment/Environment.h> #include <environment/Environment.h>
#include <ast/Statement.h> #include <ast/Expression.h>
   
namespace cli { namespace cli {
   
class ReturnCommand : public Command { class ReturnCommand : public Command {
std::shared_ptr < Statement > m_command; std::unique_ptr < Expression > m_command;
   
public: public:
ReturnCommand ( std::shared_ptr < Statement > command ) : m_command ( std::move ( command ) ) { ReturnCommand ( std::unique_ptr < Expression > command ) : m_command ( std::move ( command ) ) {
} }
   
CommandResult run ( Environment & environment ) const override { CommandResult run ( Environment & environment ) const override {
if ( m_command ) if ( m_command )
environment.setResult ( m_command->translateAndEval ( nullptr, environment ) ); environment.setResult ( m_command->translateAndEval ( environment ) );
   
return CommandResult::RETURN; return CommandResult::RETURN;
} }
......
#ifndef _CLI_BATCH_EXPRESSION_H_
#define _CLI_BATCH_EXPRESSION_H_
#include <ast/Expression.h>
#include <ast/Statement.h>
namespace cli {
class BatchExpression final : public Expression {
std::shared_ptr < Statement > m_statement;
public:
BatchExpression ( std::shared_ptr < Statement > statement ) : m_statement ( std::move ( statement ) ) {
}
std::shared_ptr < abstraction::Value > translateAndEval ( Environment & environment ) const override {
return m_statement->translateAndEval ( nullptr, environment );
}
};
} /* namespace cli */
#endif /* _CLI_BATCH_EXPRESSION_H_ */
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <ast/command/IfCommand.h> #include <ast/command/IfCommand.h>
#include <ast/command/ReturnCommand.h> #include <ast/command/ReturnCommand.h>
   
#include <ast/expression/BatchExpression.h>
#include <ast/expression/BinaryExpression.h> #include <ast/expression/BinaryExpression.h>
#include <ast/expression/PrefixExpression.h> #include <ast/expression/PrefixExpression.h>
#include <ast/expression/PostfixExpression.h> #include <ast/expression/PostfixExpression.h>
...@@ -375,40 +376,71 @@ std::unique_ptr < Command > Parser::semicolon_command ( ) { ...@@ -375,40 +376,71 @@ std::unique_ptr < Command > Parser::semicolon_command ( ) {
return res; return res;
} }
   
std::unique_ptr < Expression > Parser::expression ( ) {
return assign_expression ( );
}
std::unique_ptr < Expression > Parser::batch ( ) {
std::shared_ptr < StatementList > res = statement_list ( );
return std::make_unique < BatchExpression > ( std::move ( res ) );
}
std::unique_ptr < Expression > Parser::expression_or_batch ( ) {
if ( check_nonreserved_kw ( "batch" ) ) {
match_nonreserved_kw ( "batch" );
return batch ( );
} else {
if ( check_nonreserved_kw ( "expression" ) )
match_nonreserved_kw ( "expression" );
return expression ( );
}
}
std::unique_ptr < Expression > Parser::batch_or_expression ( ) {
if ( check_nonreserved_kw ( "expression" ) ) {
match_nonreserved_kw ( "expression" );
return expression ( );
} else {
if ( check_nonreserved_kw ( "batch" ) )
match_nonreserved_kw ( "batch" );
return batch ( );
}
}
std::unique_ptr < Command > Parser::command ( ) { std::unique_ptr < Command > Parser::command ( ) {
clearCheckOptions ( ); clearCheckOptions ( );
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::unique_ptr < Expression > expr = batch_or_expression ( );
return std::make_unique < ExecuteCommand > ( std::move ( res ) ); return std::make_unique < ExecuteCommand > ( std::move ( expr ) );
} else if ( check_nonreserved_kw ( "print" ) ) { } else if ( check_nonreserved_kw ( "print" ) ) {
match_nonreserved_kw ( "print" ); match_nonreserved_kw ( "print" );
std::shared_ptr < StatementList > res = statement_list ( ); std::unique_ptr < Expression > expr = batch_or_expression ( );
return std::make_unique < PrintCommand > ( std::move ( res ) ); return std::make_unique < PrintCommand > ( std::move ( expr ) );
} else if ( check_nonreserved_kw ( "quit" ) ) { } else if ( check_nonreserved_kw ( "quit" ) ) {
match_nonreserved_kw ( "quit" ); match_nonreserved_kw ( "quit" );
   
std::shared_ptr < StatementList > res; std::unique_ptr < Expression > expr;
if ( ! check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) if ( ! check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) )
res = statement_list ( ); expr = batch_or_expression ( );
   
return std::make_unique < QuitCommand > ( res ); return std::make_unique < QuitCommand > ( std::move ( expr ) );
} else if ( check_nonreserved_kw ( "exit" ) ) { } else if ( check_nonreserved_kw ( "exit" ) ) {
match_nonreserved_kw ( "exit" ); match_nonreserved_kw ( "exit" );
   
std::shared_ptr < StatementList > res; std::unique_ptr < Expression > expr;
if ( ! check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) if ( ! check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) )
res = statement_list ( ); expr = batch_or_expression ( );
   
return std::make_unique < QuitCommand > ( res ); return std::make_unique < QuitCommand > ( std::move ( expr ) );
} else if ( check_nonreserved_kw ( "return" ) ) { } else if ( check_nonreserved_kw ( "return" ) ) {
match_nonreserved_kw ( "return" ); match_nonreserved_kw ( "return" );
   
std::shared_ptr < StatementList > res; std::unique_ptr < Expression > expr;
if ( ! check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) if ( ! check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) )
res = statement_list ( ); expr = expression ( );
   
return std::make_unique < ReturnCommand > ( res ); return std::make_unique < ReturnCommand > ( std::move ( expr ) );
} else if ( check_nonreserved_kw ( "help" ) ) { } else if ( check_nonreserved_kw ( "help" ) ) {
match_nonreserved_kw ( "help" ); match_nonreserved_kw ( "help" );
std::unique_ptr < cli::Arg > command = optional_arg ( ); std::unique_ptr < cli::Arg > command = optional_arg ( );
...@@ -443,7 +475,7 @@ std::unique_ptr < Command > Parser::command ( ) { ...@@ -443,7 +475,7 @@ std::unique_ptr < Command > Parser::command ( ) {
} else if ( check_nonreserved_kw ( "calc" ) ) { } else if ( check_nonreserved_kw ( "calc" ) ) {
match_nonreserved_kw ( "calc" ); match_nonreserved_kw ( "calc" );
   
std::unique_ptr < Expression > expr = assign_expression ( ); std::unique_ptr < Expression > expr = expression ( );
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 ( ) );
...@@ -465,7 +497,7 @@ std::unique_ptr < Command > Parser::command ( ) { ...@@ -465,7 +497,7 @@ std::unique_ptr < Command > Parser::command ( ) {
throw exception::CommonException ( "Statement not available in global scope." ); throw exception::CommonException ( "Statement not available in global scope." );
match_nonreserved_kw ( "if" ); match_nonreserved_kw ( "if" );
match ( cli::Lexer::TokenType::LEFT_PAREN ); match ( cli::Lexer::TokenType::LEFT_PAREN );
std::unique_ptr < Expression > condition = assign_expression ( ); std::unique_ptr < Expression > condition = expression_or_batch ( );
match ( cli::Lexer::TokenType::RIGHT_PAREN ); match ( cli::Lexer::TokenType::RIGHT_PAREN );
   
match_nonreserved_kw ( "then" ); match_nonreserved_kw ( "then" );
...@@ -739,7 +771,7 @@ std::unique_ptr < Expression > Parser::atom ( ) { ...@@ -739,7 +771,7 @@ std::unique_ptr < Expression > Parser::atom ( ) {
return std::make_unique < VariableExpression > ( std::move ( name ) ); return std::make_unique < VariableExpression > ( std::move ( name ) );
} else if ( check ( cli::Lexer::TokenType::LEFT_PAREN ) ) { } else if ( check ( cli::Lexer::TokenType::LEFT_PAREN ) ) {
match ( cli::Lexer::TokenType::LEFT_PAREN ); match ( cli::Lexer::TokenType::LEFT_PAREN );
std::unique_ptr < Expression > expr = assign_expression ( ); std::unique_ptr < Expression > expr = expression ( );
match ( cli::Lexer::TokenType::RIGHT_PAREN ); match ( cli::Lexer::TokenType::RIGHT_PAREN );
return expr; return expr;
} else if ( check ( cli::Lexer::TokenType::STRING ) ) { } else if ( check ( cli::Lexer::TokenType::STRING ) ) {
...@@ -757,10 +789,10 @@ std::vector < std::unique_ptr < Expression > > Parser::bracketed_expression_list ...@@ -757,10 +789,10 @@ std::vector < std::unique_ptr < Expression > > Parser::bracketed_expression_list
std::vector < std::unique_ptr < Expression > > res; std::vector < std::unique_ptr < Expression > > res;
match ( cli::Lexer::TokenType::LEFT_PAREN ); match ( cli::Lexer::TokenType::LEFT_PAREN );
if ( ! check ( cli::Lexer::TokenType::RIGHT_PAREN ) ) { if ( ! check ( cli::Lexer::TokenType::RIGHT_PAREN ) ) {
res.push_back ( assign_expression ( ) ); res.push_back ( expression ( ) );
while ( check ( cli::Lexer::TokenType::COMMA ) ) { while ( check ( cli::Lexer::TokenType::COMMA ) ) {
match ( cli::Lexer::TokenType::COMMA ); match ( cli::Lexer::TokenType::COMMA );
res.push_back ( assign_expression ( ) ); res.push_back ( expression ( ) );
} }
} }
match ( cli::Lexer::TokenType::RIGHT_PAREN ); match ( cli::Lexer::TokenType::RIGHT_PAREN );
......
...@@ -221,6 +221,14 @@ public: ...@@ -221,6 +221,14 @@ public:
   
std::unique_ptr < CommandList > block ( ); std::unique_ptr < CommandList > block ( );
   
std::unique_ptr < Expression > expression ( );
std::unique_ptr < Expression > batch ( );
std::unique_ptr < Expression > expression_or_batch ( );
std::unique_ptr < Expression > batch_or_expression ( );
std::unique_ptr < Command > command ( ); std::unique_ptr < Command > command ( );
   
std::unique_ptr < Command > semicolon_command ( ); std::unique_ptr < Command > semicolon_command ( );
......
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