Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
IfCommand.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ast/Command.h>
10#include <ast/Statement.h>
11#include <common/CastHelper.h>
12
13namespace cli {
14
15class IfCommand : public Command {
16 std::unique_ptr < Expression > m_condition;
17 std::unique_ptr < Command > m_thenBranch;
18 std::unique_ptr < Command > m_elseBranch;
19
20public:
21 IfCommand ( std::unique_ptr < Expression > condition, std::unique_ptr < Command > thenBranch, std::unique_ptr < Command > elseBranch ) : m_condition ( std::move ( condition ) ), m_thenBranch ( std::move ( thenBranch ) ), m_elseBranch ( std::move ( elseBranch ) ) {
22 }
23
24 CommandResult run ( Environment & environment ) const override {
25 std::shared_ptr < abstraction::Value > conditionResult = m_condition->translateAndEval ( environment );
26
27 std::shared_ptr < abstraction::Value > castedResult = abstraction::CastHelper::eval ( environment, conditionResult, "bool" );
28
29 if ( std::static_pointer_cast < abstraction::ValueHolderInterface < bool > > ( castedResult )->getValue ( ) )
30 return m_thenBranch->run ( environment );
31 else if ( m_elseBranch != nullptr )
32 return m_elseBranch->run ( environment );
33
35 }
36};
37
38} /* namespace cli */
39
static std::shared_ptr< abstraction::Value > eval(abstraction::TemporariesHolder &environment, const std::shared_ptr< abstraction::Value > &param, const std::string &type)
Definition: CastHelper.cpp:13
Definition: ValueHolderInterface.hpp:15
Definition: Command.h:14
Definition: Environment.h:29
Definition: IfCommand.h:15
IfCommand(std::unique_ptr< Expression > condition, std::unique_ptr< Command > thenBranch, std::unique_ptr< Command > elseBranch)
Definition: IfCommand.h:21
CommandResult run(Environment &environment) const override
Definition: IfCommand.h:24
Definition: Arg.h:11
CommandResult
Definition: CommandResult.h:10
Definition: FordFulkerson.hpp:16