Skip to content

Commit 6d0fbcf

Browse files
committed
Add basic node type for blocks
1 parent 2c39057 commit 6d0fbcf

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

ASTNode.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,25 @@ const char* ASTCompare::op_str() const
4949
};
5050
return s_cmp_strings[op()];
5151
}
52+
53+
/* ASTBlock */
54+
void ASTBlock::removeLast()
55+
{
56+
list_t::iterator it = m_nodes.end();
57+
--it;
58+
m_nodes.erase(it);
59+
}
60+
61+
void ASTBlock::removeFirst()
62+
{
63+
list_t::iterator it = m_nodes.begin();
64+
m_nodes.erase(it);
65+
}
66+
67+
const char* ASTBlock::type_str() const
68+
{
69+
static const char* s_type_strings[] = {
70+
"", "try", "except", "finally", "while", "for"
71+
};
72+
return s_type_strings[type()];
73+
}

ASTNode.h

+30-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ASTNode {
1212
NODE_INVALID, NODE_NODELIST, NODE_OBJECT, NODE_UNARY, NODE_BINARY,
1313
NODE_COMPARE, NODE_STORE, NODE_RETURN, NODE_NAME, NODE_DELETE,
1414
NODE_FUNCTION, NODE_CLASS, NODE_CALL, NODE_IMPORT, NODE_TUPLE,
15-
NODE_LIST, NODE_MAP, NODE_SUBSCR, NODE_PRINT, NODE_JUMP,
15+
NODE_LIST, NODE_MAP, NODE_SUBSCR, NODE_PRINT, NODE_JUMP, NODE_BLOCK,
1616

1717
// Empty nodes
1818
NODE_PASS, NODE_LOCALS
@@ -331,4 +331,33 @@ class ASTJump : public ASTNode {
331331
Condition m_jtype;
332332
PycRef<ASTNode> m_cond;
333333
};
334+
335+
class ASTBlock : public ASTNode {
336+
public:
337+
typedef std::list<PycRef<ASTNode> > list_t;
338+
339+
enum Type {
340+
BLK_MAIN, BLK_TRY, BLK_EXCEPT, BLK_FINALLY, BLK_WHILE, BLK_FOR
341+
};
342+
343+
ASTBlock(Type type, unsigned int start = 0, unsigned int end = 0)
344+
: ASTNode(NODE_BLOCK), m_type(type), m_start(start), m_end(end)
345+
{ }
346+
347+
Type type() const { return m_type; }
348+
unsigned int start() const { return m_start; }
349+
unsigned int end() const { return m_end; }
350+
const list_t& nodes() const { return m_nodes; }
351+
void removeFirst();
352+
void removeLast();
353+
void append(PycRef<ASTNode> node) { m_nodes.push_back(node); }
354+
const char* type_str() const;
355+
356+
private:
357+
Type m_type;
358+
unsigned int m_start;
359+
unsigned int m_end;
360+
list_t m_nodes;
361+
};
362+
334363
#endif

0 commit comments

Comments
 (0)