37 #ifndef INFACT_INTERPRETER_H_
38 #define INFACT_INTERPRETER_H_
44 #include <unordered_map>
45 #include <unordered_set>
53 using std::unique_ptr;
60 virtual unique_ptr<istream>
Build(
61 const string &filename,
62 std::ios_base::openmode mode = std::ios_base::in)
const = 0;
71 unique_ptr<istream>
Build(
const string &filename,
72 std::ios_base::openmode mode = std::ios_base::in)
224 Interpreter(unique_ptr<IStreamBuilder> istream_builder,
int debug = 0) :
226 istream_builder_(std::move(istream_builder)),
234 istream_builder_ = std::move(istream_builder);
238 void Eval(
const string &filename);
257 env_->PrintFactories(os);
293 template<
typename T,
typename... Args>
296 bool success =
Get(varname, var);
298 std::ostringstream err_ss;
299 err_ss <<
"infact::Interpreter: no variable with name '"
304 return GetMany(std::forward<Args>(rest)...);
317 bool Get(
const string &varname, T *value)
const {
318 return env_->Get(varname, value);
331 bool IsAbsolute(
const string &filename)
const;
334 bool CanReadFile(
const string &filename)
const;
347 bool CanReadFile(
const string &f1,
const string &f2,
string *filename)
const;
351 bool HasCycle(
const string &filename,
const vector<string> &filenames)
const;
354 void EvalFile(
const string &filename);
364 string curr_filename()
const {
365 return filenames_.size() > 0 ? filenames_.back() :
"";
374 string filestack(StreamTokenizer &st,
size_t pos)
const;
376 void WrongTokenError(StreamTokenizer &st,
378 const string &expected,
382 void WrongTokenTypeError(StreamTokenizer &st,
386 const string &token)
const;
388 void WrongTokenTypeError(StreamTokenizer &st,
390 const string &expected_type,
391 const string &found_type,
392 const string &token)
const;
395 unique_ptr<EnvironmentImpl> env_;
402 vector<string> filenames_;
405 unique_ptr<IStreamBuilder> istream_builder_;
void PrintFactories(ostream &os) const
void EvalString(const string &input)
Evaluates the statements in the specified string.
~DefaultIStreamBuilder() override=default
virtual unique_ptr< istream > Build(const string &filename, std::ios_base::openmode mode=std::ios_base::in) const =0
void Eval(istream &is)
Evaluates the statements in the specified stream.
bool GetMany()
The base case for the variadic template method GetMany defined below.
void Eval(const string &filename)
Evaluates the statements in the specified text file.
virtual ~Interpreter()=default
Destroys this interpreter.
The default implementation for the IStreamBuilder interface, returning std::ifstream instances...
EnvironmentImpl * env()
Returns a pointer to the environment of this interpreter.
Interpreter(int debug=0)
Constructs a new instance with the specified debug level.
bool Get(const string &varname, T *value) const
Retrieves the value of the specified variable.
bool GetMany(const string &varname, T *var, Args &&...rest)
Retrieves values for many variables, specified as a sequence of pairs of arguments.
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
void SetIStreamBuilder(unique_ptr< IStreamBuilder > istream_builder)
Sets the IStreamBuilder object, to be owned by this object.
void PrintEnv(ostream &os) const
void Error(const std::string &message)
Reports an error encountered during parsing and/or construction of an object.
Provides an environment for variables and their values, either primitive or Factory-constructible obj...
virtual ~IStreamBuilder()=default
TokenType
The set of types of tokens read by this stream tokenizer.
Provides a set of named variables and their types, as well as the values for those variables...
unique_ptr< istream > Build(const string &filename, std::ios_base::openmode mode=std::ios_base::in) const override
An interface for classes that can build istreams for named files.
Provides an interpreter for assigning primitives and Factory-constructible objects to named variables...
Interpreter(unique_ptr< IStreamBuilder > istream_builder, int debug=0)
Constructs a new instance with the specified IStreamBuilder and debug level.