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

variable declaration

parent 3f17fa9a
No related branches found
No related tags found
1 merge request!125procedural aql
#ifndef _CLI_VAR_DECLARE_COMMAND_H_
#define _CLI_VAR_DECLARE_COMMAND_H_
#include <ast/Command.h>
#include <environment/Environment.h>
namespace cli {
class VarDeclareCommand : public Command {
std::unique_ptr < cli::Arg > m_name;
abstraction::ParamQualifiers::ParamQualifierSet m_paramQualifierSet;
std::unique_ptr < Expression > m_expr;
public:
VarDeclareCommand ( std::unique_ptr < cli::Arg > name, abstraction::ParamQualifiers::ParamQualifierSet paramQualifierSet, std::unique_ptr < Expression > expr ) : m_name ( std::move ( name ) ), m_paramQualifierSet ( paramQualifierSet ), m_expr ( std::move ( expr ) ) {
}
CommandResult run ( Environment & environment ) const override {
std::shared_ptr < abstraction::Value > value = m_expr->translateAndEval ( environment );
std::shared_ptr < abstraction::Value > res = value->clone ( m_paramQualifierSet, false );
environment.setVariable ( m_name->eval ( environment ), res );
return CommandResult::OK;
}
};
} /* namespace cli */
#endif /* _CLI_VAR_DECLARE_COMMAND_H_ */
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <ast/command/BreakCommand.h> #include <ast/command/BreakCommand.h>
#include <ast/command/ContinueCommand.h> #include <ast/command/ContinueCommand.h>
#include <ast/command/ReturnCommand.h> #include <ast/command/ReturnCommand.h>
#include <ast/command/VarDeclareCommand.h>
   
#include <ast/expression/BatchExpression.h> #include <ast/expression/BatchExpression.h>
#include <ast/expression/BinaryExpression.h> #include <ast/expression/BinaryExpression.h>
...@@ -535,6 +536,34 @@ std::unique_ptr < Command > Parser::command ( ) { ...@@ -535,6 +536,34 @@ 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 ( "continue" ); match_nonreserved_kw ( "continue" );
return std::make_unique < ContinueCommand > ( ); return std::make_unique < ContinueCommand > ( );
} else if ( check_nonreserved_kw ( "declare" ) ) {
match_nonreserved_kw ( "declare" );
abstraction::ParamQualifiers::ParamQualifierSet paramQualifierSet = abstraction::ParamQualifiers::ParamQualifierSet::NONE;
if ( check_nonreserved_kw ( "const" ) ) {
match_nonreserved_kw ( "const" );
paramQualifierSet = paramQualifierSet | abstraction::ParamQualifiers::ParamQualifierSet::CONST;
}
match_nonreserved_kw ( "auto" );
if ( check ( cli::Lexer::TokenType::AND_OPERATOR ) ) {
match ( cli::Lexer::TokenType::AND_OPERATOR );
paramQualifierSet = paramQualifierSet | abstraction::ParamQualifiers::ParamQualifierSet::RREF;
} else if ( check ( cli::Lexer::TokenType::AMPERSAND_SIGN ) ) {
match ( cli::Lexer::TokenType::AMPERSAND_SIGN );
paramQualifierSet = paramQualifierSet | abstraction::ParamQualifiers::ParamQualifierSet::LREF;
}
match ( cli::Lexer::TokenType::DOLAR_SIGN );
std::unique_ptr < Arg > name = arg ( );
match ( cli::Lexer::TokenType::ASSIGN_OPERATOR );
std::unique_ptr < Expression > expr = expression ( );
return std::make_unique < VarDeclareCommand > ( std::move ( name ), paramQualifierSet, std::move ( expr ) );
} 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