diff options
Diffstat (limited to 'src/ast.cpp')
| -rw-r--r-- | src/ast.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/ast.cpp b/src/ast.cpp new file mode 100644 index 0000000..03ed4b6 --- /dev/null +++ b/src/ast.cpp | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | #include "ast.h" | ||
| 2 | |||
| 3 | #include "astleaf.h" | ||
| 4 | #include "astbranch.h" | ||
| 5 | |||
| 6 | Ast::Ast() | ||
| 7 | { | ||
| 8 | } | ||
| 9 | |||
| 10 | Ast::~Ast() | ||
| 11 | { | ||
| 12 | } | ||
| 13 | |||
| 14 | void Ast::addNode( AstNode::Type eType ) | ||
| 15 | { | ||
| 16 | switch( eType&AstNode::typeClassMask ) | ||
| 17 | { | ||
| 18 | case AstNode::typeBranch: | ||
| 19 | { | ||
| 20 | AstBranch *pNode = new AstBranch( eType ); | ||
| 21 | addNode( pNode ); | ||
| 22 | sBranch.push( pNode ); | ||
| 23 | } | ||
| 24 | break; | ||
| 25 | |||
| 26 | case AstNode::typeLeaf: | ||
| 27 | { | ||
| 28 | AstLeaf *pNode = new AstLeaf( eType ); | ||
| 29 | addNode( pNode ); | ||
| 30 | } | ||
| 31 | break; | ||
| 32 | |||
| 33 | default: | ||
| 34 | throw Bu::ExceptionBase("You got it wrong."); | ||
| 35 | break; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | void Ast::addNode( AstNode::Type eType, int iVal ) | ||
| 40 | { | ||
| 41 | addNode( new AstLeaf( eType, iVal ) ); | ||
| 42 | } | ||
| 43 | |||
| 44 | void Ast::addNode( AstNode::Type eType, float fVal ) | ||
| 45 | { | ||
| 46 | addNode( new AstLeaf( eType, fVal ) ); | ||
| 47 | } | ||
| 48 | |||
| 49 | void Ast::addNode( AstNode::Type eType, bool bVal ) | ||
| 50 | { | ||
| 51 | addNode( new AstLeaf( eType, bVal ) ); | ||
| 52 | } | ||
| 53 | |||
| 54 | void Ast::addNode( AstNode::Type eType, const Bu::FString &sVal ) | ||
| 55 | { | ||
| 56 | addNode( new AstLeaf( eType, sVal ) ); | ||
| 57 | } | ||
| 58 | |||
| 59 | void Ast::addNode( AstNode::Type eType, const char *sVal ) | ||
| 60 | { | ||
| 61 | addNode( new AstLeaf( eType, sVal ) ); | ||
| 62 | } | ||
| 63 | |||
| 64 | void Ast::addNode( AstNode *pNode ) | ||
| 65 | { | ||
| 66 | if( sBranch.isEmpty() ) | ||
| 67 | lNode.append( pNode ); | ||
| 68 | else | ||
| 69 | sBranch.peek()->addNode( pNode ); | ||
| 70 | } | ||
| 71 | |||
| 72 | void Ast::openBranch() | ||
| 73 | { | ||
| 74 | sBranch.peek()->addBranch(); | ||
| 75 | } | ||
| 76 | |||
| 77 | void Ast::closeNode() | ||
| 78 | { | ||
| 79 | sBranch.pop(); | ||
| 80 | } | ||
| 81 | |||
| 82 | Ast::NodeList::const_iterator Ast::getNodeBegin() const | ||
| 83 | { | ||
| 84 | return lNode.begin(); | ||
| 85 | } | ||
| 86 | |||
| 87 | Bu::Formatter &operator<<( Bu::Formatter &f, const Ast &a ) | ||
| 88 | { | ||
| 89 | f << "Abstract Syntax Tree:"; | ||
| 90 | f.incIndent(); | ||
| 91 | f << f.nl; | ||
| 92 | for( Ast::NodeList::const_iterator i = a.getNodeBegin(); i; i++ ) | ||
| 93 | f << **i << f.nl; | ||
| 94 | f << f.nl; | ||
| 95 | f.decIndent(); | ||
| 96 | return f; | ||
| 97 | } | ||
| 98 | |||
