Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
WhileCommand.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 WhileCommand : public Command {
16 std::unique_ptr < Expression > m_condition;
17 std::unique_ptr < Command > m_body;
18
19public:
20 WhileCommand ( std::unique_ptr < Expression > condition, std::unique_ptr < Command > body ) : m_condition ( std::move ( condition ) ), m_body ( std::move ( body ) ) {
21 }
22
23 CommandResult run ( Environment & environment ) const override {
25 while ( res == cli::CommandResult::OK ) {
26 std::shared_ptr < abstraction::Value > conditionResult = m_condition->translateAndEval ( environment );
27
28 std::shared_ptr < abstraction::Value > castedResult = abstraction::CastHelper::eval ( environment, conditionResult, "bool" );
29
30 if ( ! std::static_pointer_cast < abstraction::ValueHolderInterface < bool > > ( castedResult )->getValue ( ) )
31 break;
32
33 res = m_body->run ( environment );
34
37
38 if ( res == CommandResult::BREAK ) {
40 break;
41 }
42 }
43
44 return res;
45 }
46};
47
48} /* namespace cli */
49
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: WhileCommand.h:15
CommandResult run(Environment &environment) const override
Definition: WhileCommand.h:23
WhileCommand(std::unique_ptr< Expression > condition, std::unique_ptr< Command > body)
Definition: WhileCommand.h:20
return res
Definition: MinimizeByPartitioning.h:145
Definition: Arg.h:11
CommandResult
Definition: CommandResult.h:10
Definition: FordFulkerson.hpp:16