#ifndef _PYC_ASTNODE_H #define _PYC_ASTNODE_H #include "module.h" #include /* Similar interface to PycObject, so PycRef can work on it... * * However, this does *NOT* mean the two are interchangeable! */ class ASTNode { public: enum Type { 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, // Empty nodes NODE_PASS, NODE_LOCALS, //Hack to unindent NODE_POP_HACK }; ASTNode(int type = NODE_INVALID) : m_refs(0), m_type(type) { } virtual ~ASTNode() { } int type() const { return (this) ? m_type : NODE_INVALID; } private: int m_refs; int m_type; public: void addRef() { if (this) ++m_refs; } void delRef() { if (this && --m_refs == 0) delete this; } }; /* A NULL node for comparison */ extern PycRef Node_NULL; class ASTNodeList : public ASTNode { public: typedef std::list > list_t; ASTNodeList(list_t nodes) : ASTNode(NODE_NODELIST), m_nodes(nodes) { } const list_t& nodes() const { return m_nodes; } void removeFirst(); void removeLast(); void append(PycRef node) { m_nodes.push_back(node); } private: list_t m_nodes; }; class ASTObject : public ASTNode { public: ASTObject(PycRef obj) : ASTNode(NODE_OBJECT), m_obj(obj) { } PycRef object() const { return m_obj; } private: PycRef m_obj; }; class ASTUnary : public ASTNode { public: enum UnOp { UN_POSITIVE, UN_NEGATIVE, UN_INVERT, UN_NOT, UN_CONVERT }; ASTUnary(PycRef operand, int op) : ASTNode(NODE_UNARY), m_op(op), m_operand(operand) { } PycRef operand() const { return m_operand; } int op() const { return m_op; } virtual const char* op_str() const; protected: int m_op; private: PycRef m_operand; }; class ASTBinary : public ASTNode { public: enum BinOp { BIN_ATTR, BIN_POWER, BIN_MULTIPLY, BIN_DIVIDE, BIN_FLOOR, BIN_MODULO, BIN_ADD, BIN_SUBTRACT, BIN_LSHIFT, BIN_RSHIFT, BIN_AND, BIN_XOR, BIN_OR, BIN_LOG_AND, BIN_LOG_OR, BIN_IP_ADD, BIN_IP_SUBTRACT, BIN_IP_MULTIPLY, BIN_IP_DIVIDE, BIN_IP_MODULO, BIN_IP_POWER, BIN_IP_LSHIFT, BIN_IP_RSHIFT, BIN_IP_AND, BIN_IP_XOR, BIN_IP_OR, BIN_IP_FLOOR, BIN_SUBSCR }; ASTBinary(PycRef left, PycRef right, int op, int type = NODE_BINARY) : ASTNode(type), m_op(op), m_left(left), m_right(right) { } PycRef left() const { return m_left; } PycRef right() const { return m_right; } int op() const { return m_op; } virtual const char* op_str() const; protected: int m_op; private: PycRef m_left; PycRef m_right; }; class ASTCompare : public ASTBinary { public: enum CompareOp { CMP_LESS, CMP_LESS_EQUAL, CMP_EQUAL, CMP_NOT_EQUAL, CMP_GREATER, CMP_GREATER_EQUAL, CMP_IN, CMP_NOT_IN, CMP_IS, CMP_IS_NOT, CMP_EXCEPTION, CMP_BAD }; ASTCompare(PycRef left, PycRef right, int op) : ASTBinary(left, right, op, NODE_COMPARE) { } const char* op_str() const; }; class ASTStore : public ASTNode { public: ASTStore(PycRef src, PycRef dest) : ASTNode(NODE_STORE), m_src(src), m_dest(dest) { } PycRef src() const { return m_src; } PycRef dest() const { return m_dest; } private: PycRef m_src; PycRef m_dest; }; class ASTReturn : public ASTNode { public: ASTReturn(PycRef value) : ASTNode(NODE_RETURN), m_value(value) { } PycRef value() const { return m_value; } private: PycRef m_value; }; class ASTName : public ASTNode { public: ASTName(PycRef name) : ASTNode(NODE_NAME), m_name(name) { } PycRef name() const { return m_name; } private: PycRef m_name; }; class ASTDelete : public ASTNode { public: ASTDelete(PycRef value) : ASTNode(NODE_DELETE), m_value(value) { } PycRef value() const { return m_value; } private: PycRef m_value; }; class ASTFunction : public ASTNode { public: typedef std::list > defarg_t; ASTFunction(PycRef code, defarg_t defArgs) : ASTNode(NODE_FUNCTION), m_code(code), m_defargs(defArgs) { } PycRef code() const { return m_code; } const defarg_t& defargs() const { return m_defargs; } private: PycRef m_code; defarg_t m_defargs; }; class ASTClass : public ASTNode { public: ASTClass(PycRef code, PycRef bases, PycRef name) : ASTNode(NODE_CLASS), m_code(code), m_bases(bases), m_name(name) { } PycRef code() const { return m_code; } PycRef bases() const { return m_bases; } PycRef name() const { return m_name; } private: PycRef m_code; PycRef m_bases; PycRef m_name; }; class ASTCall : public ASTNode { public: typedef std::list > pparam_t; typedef std::list, PycRef > > kwparam_t; ASTCall(PycRef func, pparam_t pparams, kwparam_t kwparams) : ASTNode(NODE_CALL), m_func(func), m_pparams(pparams), m_kwparams(kwparams) { } PycRef func() const { return m_func; } const pparam_t& pparams() const { return m_pparams; } const kwparam_t& kwparams() const { return m_kwparams; } private: PycRef m_func; pparam_t m_pparams; kwparam_t m_kwparams; }; class ASTImport : public ASTNode { public: ASTImport(PycRef name, PycRef fromlist) : ASTNode(NODE_IMPORT), m_name(name), m_fromlist(fromlist) { } PycRef name() const { return m_name; } PycRef fromlist() const { return m_fromlist; } private: PycRef m_name; PycRef m_fromlist; }; class ASTTuple : public ASTNode { public: typedef std::vector > value_t; ASTTuple(value_t values) : ASTNode(NODE_TUPLE), m_values(values) { } const value_t& values() const { return m_values; } private: value_t m_values; }; class ASTList : public ASTNode { public: typedef std::list > value_t; ASTList(value_t values) : ASTNode(NODE_LIST), m_values(values) { } const value_t& values() const { return m_values; } private: value_t m_values; }; class ASTMap : public ASTNode { public: typedef std::list, PycRef > > map_t; ASTMap() : ASTNode(NODE_MAP) { } void add(PycRef key, PycRef value) { m_values.push_back(std::make_pair(key, value)); } const map_t& values() const { return m_values; } private: map_t m_values; }; class ASTSubscr : public ASTNode { public: ASTSubscr(PycRef name, PycRef key) : ASTNode(NODE_SUBSCR), m_name(name), m_key(key) { } PycRef name() const { return m_name; } PycRef key() const { return m_key; } private: PycRef m_name; PycRef m_key; }; class ASTPrint : public ASTNode { public: ASTPrint(PycRef value) : ASTNode(NODE_PRINT), m_value(value) { } PycRef value() const { return m_value; } private: PycRef m_value; }; class ASTJump : public ASTNode { public: enum Condition { JUMP, JMP_FALSE, JMP_TRUE }; ASTJump(int dest, Condition jtype, PycRef cond) : ASTNode(NODE_JUMP), m_dest(dest), m_jtype(jtype), m_cond(cond) {} int dest() const { return m_dest; } Condition jtype() const { return m_jtype; } PycRef cond() const { return m_cond; } private: int m_dest; Condition m_jtype; PycRef m_cond; }; class ASTPopHack : public ASTNode { public: ASTPopHack() : ASTNode(NODE_POP_HACK) { } }; #endif