macroprocessor: add @#ifdef

issue#70
Houtan Bastani 2012-01-02 18:08:14 +01:00
parent c22022cfb0
commit 2d15137289
4 changed files with 26 additions and 1 deletions

View File

@ -79,7 +79,7 @@ class MacroDriver;
%}
%token DEFINE LINE FOR IN IF ELSE ENDIF ECHO_DIR ERROR
%token DEFINE LINE FOR IN IF ELSE ENDIF ECHO_DIR ERROR IFDEF
%token LPAREN RPAREN LBRACKET RBRACKET EQUAL EOL
%token <int_val> INTEGER
@ -117,6 +117,8 @@ statement : expr
{ TYPERR_CATCH(driver.init_loop(*$2, $4), @$); delete $2; }
| IF expr
{ TYPERR_CATCH(driver.begin_if($2), @$); }
| IFDEF NAME
{ TYPERR_CATCH(driver.begin_ifdef(*$2), @$); delete $2; }
| ECHO_DIR expr
{ TYPERR_CATCH(driver.echo(@$, $2), @$); }
| ERROR expr

View File

@ -164,6 +164,24 @@ MacroDriver::begin_if(const MacroValue *value) throw (MacroValue::TypeError)
last_if = (bool) ival->value;
}
void
MacroDriver::begin_ifdef(const string &name)
{
try
{
get_variable(name);
const MacroValue *one = new IntMV(*this, 1);
begin_if(one);
delete one;
}
catch (UnknownVariable &)
{
const MacroValue *zero = new IntMV(*this, 0);
begin_if(zero);
delete zero;
}
}
void
MacroDriver::echo(const Macro::parser::location_type &l, const MacroValue *value) const throw (MacroValue::TypeError)
{

View File

@ -208,6 +208,9 @@ public:
//! Begins an @#if statement
void begin_if(const MacroValue *value) throw (MacroValue::TypeError);
//! Begins an @#ifdef statement
void begin_ifdef(const string &name);
//! Executes @#echo directive
void echo(const Macro::parser::location_type &l, const MacroValue *value) const throw (MacroValue::TypeError);

View File

@ -164,6 +164,8 @@ CONT \\\\
<STMT>for { reading_for_statement = true; return token::FOR; }
<STMT>endfor { driver.error(*yylloc, "@#endfor is not matched by a @#for statement"); }
<STMT>ifdef { reading_if_statement = true; return token::IFDEF; }
<STMT>if { reading_if_statement = true; return token::IF; }
<STMT>else { driver.error(*yylloc, "@#else is not matched by an @#if statement"); }
<STMT>endif { driver.error(*yylloc, "@#endif is not matched by an @#if statement"); }