macroprocessor: add length command. closes #436

issue#70
Houtan Bastani 2013-08-12 17:24:47 -04:00
parent 2464c330b3
commit 792b1b5924
4 changed files with 25 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008-2012 Dynare Team
* Copyright (C) 2008-2013 Dynare Team
*
* This file is part of Dynare.
*
@ -80,7 +80,7 @@ class MacroDriver;
%}
%token DEFINE LINE FOR IN IF ELSE ENDIF ECHO_DIR ERROR IFDEF IFNDEF
%token LPAREN RPAREN LBRACKET RBRACKET EQUAL EOL
%token LPAREN RPAREN LBRACKET RBRACKET EQUAL EOL LENGTH
%token <int_val> INTEGER
%token <string_val> NAME STRING
@ -145,6 +145,8 @@ expr : INTEGER
}
delete $1;
}
| LENGTH LPAREN array_expr RPAREN
{ TYPERR_CATCH($$ = $3->length(), @$); }
| LPAREN expr RPAREN
{ $$ = $2; }
| expr PLUS expr

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008-2012 Dynare Team
* Copyright (C) 2008-2013 Dynare Team
*
* This file is part of Dynare.
*
@ -151,6 +151,7 @@ CONT \\\\
<STMT,EXPR>[*] { return token::TIMES; }
<STMT,EXPR>[/] { return token::DIVIDE; }
<STMT,EXPR>in { return token::IN; }
<STMT,EXPR>length { return token::LENGTH; }
<STMT,EXPR>\"[^\"]*\" {
yylval->string_val = new string(yytext + 1);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008-2011 Dynare Team
* Copyright (C) 2008-2013 Dynare Team
*
* This file is part of Dynare.
*
@ -106,6 +106,12 @@ MacroValue::operator[](const MacroValue &mv) const throw (TypeError, OutOfBounds
throw TypeError("Operator [] does not exist for this type");
}
const MacroValue *
MacroValue::length() const throw (TypeError)
{
throw TypeError("Length not supported for this type");
}
const MacroValue *
MacroValue::append(const MacroValue *mv) const throw (TypeError)
{

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008-2010 Dynare Team
* Copyright (C) 2008-2013 Dynare Team
*
* This file is part of Dynare.
*
@ -92,6 +92,8 @@ public:
virtual string toString() const = 0;
//! Converts value to array form
virtual const MacroValue *toArray() const = 0;
//! Gets length
virtual const MacroValue *length() const throw (TypeError);
//! Appends value at the end of an array
/*! The argument must be an array. */
virtual const MacroValue *append(const MacroValue *array) const throw (TypeError);
@ -217,6 +219,8 @@ public:
virtual string toString() const;
//! Returns itself
virtual const MacroValue *toArray() const;
//! Gets length
virtual const MacroValue *length() const throw (TypeError);
};
template<typename T>
@ -329,4 +333,11 @@ ArrayMV<T>::toArray() const
return this;
}
template<typename T>
const MacroValue *
ArrayMV<T>::length() const throw (TypeError)
{
return new IntMV(driver, values.size());
}
#endif