Add basic node type for blocks

This commit is contained in:
Darryl Pogue
2010-12-20 22:58:44 -08:00
parent 2c39057f1b
commit 6d0fbcf522
2 changed files with 52 additions and 1 deletions

View File

@@ -49,3 +49,25 @@ const char* ASTCompare::op_str() const
};
return s_cmp_strings[op()];
}
/* ASTBlock */
void ASTBlock::removeLast()
{
list_t::iterator it = m_nodes.end();
--it;
m_nodes.erase(it);
}
void ASTBlock::removeFirst()
{
list_t::iterator it = m_nodes.begin();
m_nodes.erase(it);
}
const char* ASTBlock::type_str() const
{
static const char* s_type_strings[] = {
"", "try", "except", "finally", "while", "for"
};
return s_type_strings[type()];
}

View File

@@ -12,7 +12,7 @@ public:
NODE_INVALID, NODE_NODELIST, NODE_OBJECT, NODE_UNARY, NODE_BINARY,
NODE_COMPARE, NODE_STORE, NODE_RETURN, NODE_NAME, NODE_DELETE,
NODE_FUNCTION, NODE_CLASS, NODE_CALL, NODE_IMPORT, NODE_TUPLE,
NODE_LIST, NODE_MAP, NODE_SUBSCR, NODE_PRINT, NODE_JUMP,
NODE_LIST, NODE_MAP, NODE_SUBSCR, NODE_PRINT, NODE_JUMP, NODE_BLOCK,
// Empty nodes
NODE_PASS, NODE_LOCALS
@@ -331,4 +331,33 @@ private:
Condition m_jtype;
PycRef<ASTNode> m_cond;
};
class ASTBlock : public ASTNode {
public:
typedef std::list<PycRef<ASTNode> > list_t;
enum Type {
BLK_MAIN, BLK_TRY, BLK_EXCEPT, BLK_FINALLY, BLK_WHILE, BLK_FOR
};
ASTBlock(Type type, unsigned int start = 0, unsigned int end = 0)
: ASTNode(NODE_BLOCK), m_type(type), m_start(start), m_end(end)
{ }
Type type() const { return m_type; }
unsigned int start() const { return m_start; }
unsigned int end() const { return m_end; }
const list_t& nodes() const { return m_nodes; }
void removeFirst();
void removeLast();
void append(PycRef<ASTNode> node) { m_nodes.push_back(node); }
const char* type_str() const;
private:
Type m_type;
unsigned int m_start;
unsigned int m_end;
list_t m_nodes;
};
#endif