macro processor: make Variable class immutable

issue#70
Houtan Bastani 2019-08-20 11:16:33 +02:00
parent b3ec807b90
commit 16080f2bb8
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
2 changed files with 6 additions and 7 deletions

View File

@ -450,7 +450,7 @@ namespace macro
{
private:
const string name;
ArrayPtr indices; // for strings/arrays
const ArrayPtr indices; // for indexing strings/arrays
public:
Variable(string name_arg,
Environment &env_arg, Tokenizer::location location_arg) :
@ -458,10 +458,6 @@ namespace macro
Variable(string name_arg, ArrayPtr indices_arg,
Environment &env_arg, Tokenizer::location location_arg) :
Expression(env_arg, move(location_arg)), name{move(name_arg)}, indices{move(indices_arg)} { }
inline void addIndexing(const vector<ExpressionPtr> indices_arg)
{
indices = make_shared<Array>(indices_arg, env);
}
inline string to_string() const noexcept override { return name; }
inline void print(ostream &output, bool matlab_output = false) const noexcept override { output << name; }
BaseTypePtr eval() override;

View File

@ -300,6 +300,11 @@ primary_expr : LPAREN expr RPAREN
{ $$ = $2; }
| symbol
{ $$ = $1; } // Explicit rule needed for type conversion
| NAME LBRACKET comma_expr RBRACKET
{
$$ = make_shared<Variable>($1, make_shared<Array>($3, driver.env, @3),
driver.env, @$);
}
| NAME LPAREN comma_expr RPAREN
{ $$ = make_shared<Function>($1, $3, driver.env, @$); }
| TRUE
@ -312,8 +317,6 @@ primary_expr : LPAREN expr RPAREN
{ $$ = make_shared<String>($1, driver.env, @$); }
| LBRACKET comma_expr RBRACKET
{ $$ = make_shared<Array>($2, driver.env, @$); }
| symbol LBRACKET comma_expr RBRACKET
{ $1->addIndexing($3); $$ = $1; }
| LPAREN tuple_comma_expr RPAREN
{ $$ = make_shared<Tuple>($2, driver.env, @$); }
| LBRACKET expr IN expr WHEN expr RBRACKET