macro processor: Make @#include and @#includepath immutable

issue#70
Houtan Bastani 2019-08-19 18:19:10 +02:00
parent 588896b509
commit cd99bb3af9
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
3 changed files with 25 additions and 13 deletions

View File

@ -41,13 +41,19 @@ Eval::interpret(ostream &output, bool no_line_macro)
void
Include::interpret(ostream &output, bool no_line_macro)
{
error(StackTrace("@#include", "should never be interpreted", location));
}
string
Include::interpretAndGetName()
{
try
{
StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
if (!msp)
throw StackTrace("File name does not evaluate to a string");
name = *msp;
return msp->to_string();
}
catch (StackTrace &ex)
{
@ -58,17 +64,24 @@ Include::interpret(ostream &output, bool no_line_macro)
{
error(StackTrace("@#include", e.what(), location));
}
return "";
}
void
IncludePath::interpret(ostream &output, bool no_line_macro)
{
error(StackTrace("@#includepath", "should never be interpreted", location));
}
string
IncludePath::interpretAndGetPath()
{
try
{
StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
if (!msp)
throw StackTrace("File name does not evaluate to a string");
path = *msp;
return *msp;
}
catch (StackTrace &ex)
{
@ -79,6 +92,7 @@ IncludePath::interpret(ostream &output, bool no_line_macro)
{
error(StackTrace("@#includepath", e.what(), location));
}
return "";
}
void

View File

@ -66,12 +66,11 @@ namespace macro
{
private:
const ExpressionPtr expr;
string name;
public:
Include(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { }
void interpret(ostream &output, bool no_line_macro) override;
inline const string & getName() const { return name; }
string interpretAndGetName();
};
@ -79,12 +78,11 @@ namespace macro
{
private:
const ExpressionPtr expr;
string path;
public:
IncludePath(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { }
void interpret(ostream &output, bool no_line_macro) override;
inline const string & getPath() const { return path; }
string interpretAndGetPath();
};

View File

@ -80,16 +80,14 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi
statement->printLineInfo(output, no_line_macro);
printLine = false;
}
statement->interpret(output, no_line_macro);
auto ipp = dynamic_pointer_cast<IncludePath>(statement);
if (ipp)
paths.emplace_back(ipp->getPath());
auto ip = dynamic_pointer_cast<Include>(statement);
if (ip)
auto ipp = dynamic_pointer_cast<IncludePath>(statement);
if (ipp)
paths.emplace_back(ipp->interpretAndGetPath());
else if (ip)
{
string filename = ip->getName();
string filename = ip->interpretAndGetName();
ifstream incfile(filename, ios::binary);
if (incfile.fail())
{
@ -116,6 +114,8 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi
Driver m(env, no_line_macro);
m.parse(filename, basename, incfile, output, debug, vector<pair<string, string>>{}, paths);
}
else
statement->interpret(output, no_line_macro);
}
}