javascript - Implementing if-condition in preprocessor -


for project of mine, want implement customized preprocessor in javascript mimics gnu c preprocessor (i.e. gcc -e).

this preprocessor wrote has things working, except conditionals. so, #ifdef, #ifndef, #else , #endif stuck at.

my code far: preprocessor.js (permanent link)

i'd glad hear suggestions on how implement it! :)

maintain stack of conditions , keep skipping lines long condition false. in pseudocode:

 each line     if line == "#if <cond>"         conditions.push(<cond>)     else if line == "#else"          conditions.push(!conditions.pop())     else if line == "#endif"          conditions.pop()     else if conditions true         fine, handle line     else         skip line 

Comments