Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
Environment.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/typeinfo>
9#include <ext/memory>
10
11#include <alib/string>
12#include <alib/map>
13#include <alib/optional>
14
15#include <abstraction/Value.hpp>
17
21#include <lexer/CharSequence.h>
22
24
25namespace cli {
26
27class Command;
28
32 std::shared_ptr < abstraction::Value > m_result;
33
35
36 std::shared_ptr < abstraction::Value > getVariableInt ( const std::string & name ) const;
37
38 void setVariableInt ( std::string name, std::shared_ptr < abstraction::Value > value ) {
39 m_variables [ std::move ( name ) ] = std::move ( value );
40 }
41
42public:
43 Environment ( ) = default;
44
45 Environment ( ext::optional_ref < Environment > upper ) : m_upper ( upper ) {
46 }
47
48 std::string getBinding ( const std::string & name ) const;
49
50 std::set < std::string > getBindingNames ( ) const {
51 std::set < std::string > res;
52
53 for ( const std::pair < const std::string, std::string > & kv : m_bindings ) {
54 res.insert ( kv.first );
55 }
56
57 return res;
58 }
59
60 void setBinding ( std::string name, std::string value ) {
61 m_bindings [ std::move ( name ) ] = std::move ( value );
62 }
63
64 bool clearBinding ( const std::string & name ) {
65 return m_bindings.erase ( name ) != 0;
66 }
67
68 std::shared_ptr < abstraction::Value > getVariable ( const std::string & name ) const {
69 return getVariableInt ( name );
70 }
71
72 template < class T >
73 const T & getVariable ( const std::string & name ) const {
74 return abstraction::retrieveValue < const T & > ( getVariableInt ( name ) );
75 }
76
77 std::set < std::string > getVariableNames ( ) const {
78 std::set < std::string > res;
79
80 for ( const std::pair < const std::string, std::shared_ptr < abstraction::Value > > & kv : m_variables ) {
81 res.insert ( kv.first );
82 }
83
84 return res;
85 }
86
87 void setVariable ( std::string name, std::shared_ptr < abstraction::Value > value ) {
88 setVariableInt ( std::move ( name ), std::move ( value ) );
89 }
90
91 template < class T >
92 void setVariable ( std::string name, T value ) {
93 auto variable = std::make_shared < abstraction::ValueHolder < T > > ( std::move ( value ), false );
94 setVariableInt ( std::move ( name ), variable );
95 }
96
97 bool clearVariable ( const std::string & name ) {
98 return m_variables.erase ( name ) != 0u;
99 }
100
101 void setResult ( std::shared_ptr < abstraction::Value > value ) {
102 m_result = std::move ( value );
103 }
104
105 std::shared_ptr < abstraction::Value > getResult ( ) const {
106 return m_result;
107 }
108
109 cli::CommandResult execute ( const std::shared_ptr < cli::LineInterface > & lineInterface );
110
112
114};
115
116} /* namespace cli */
117
Definition: TemporariesHolder.h:13
Definition: CharSequence.h:16
Definition: Environment.h:29
std::set< std::string > getVariableNames() const
Definition: Environment.h:77
void setVariable(std::string name, std::shared_ptr< abstraction::Value > value)
Definition: Environment.h:87
bool clearVariable(const std::string &name)
Definition: Environment.h:97
void setVariable(std::string name, T value)
Definition: Environment.h:92
std::shared_ptr< abstraction::Value > getResult() const
Definition: Environment.h:105
Environment(ext::optional_ref< Environment > upper)
Definition: Environment.h:45
Environment()=default
std::set< std::string > getBindingNames() const
Definition: Environment.h:50
std::string getBinding(const std::string &name) const
Definition: Environment.cpp:33
std::shared_ptr< abstraction::Value > getVariable(const std::string &name) const
Definition: Environment.h:68
void setBinding(std::string name, std::string value)
Definition: Environment.h:60
cli::CommandResult execute_line(cli::CharSequence charSequence)
Definition: Environment.cpp:58
void setResult(std::shared_ptr< abstraction::Value > value)
Definition: Environment.h:101
const T & getVariable(const std::string &name) const
Definition: Environment.h:73
bool clearBinding(const std::string &name)
Definition: Environment.h:64
Environment & getGlobalScope()
Definition: Environment.cpp:74
cli::CommandResult execute(const std::shared_ptr< cli::LineInterface > &lineInterface)
Definition: Environment.cpp:49
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
size_t erase(const K &key)
Erase by key of arbitrary type.
Definition: map.hpp:340
Definition: optional_ref.hpp:33
return res
Definition: MinimizeByPartitioning.h:145
Definition: Arg.h:11
CommandResult
Definition: CommandResult.h:10