Skip to content

Commit 03042b7

Browse files
committed
Decoding simple files with functions and assignments is now possible
1 parent b89ae8b commit 03042b7

12 files changed

+330
-180
lines changed

ASTNode.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,37 @@
22

33
PycRef<ASTNode> Node_NULL = (ASTNode*)0;
44

5+
/* ASTNodeList */
6+
void ASTNodeList::removeLast()
7+
{
8+
list_t::iterator it = m_nodes.end();
9+
--it;
10+
m_nodes.erase(it);
11+
}
12+
13+
void ASTNodeList::removeFirst()
14+
{
15+
list_t::iterator it = m_nodes.begin();
16+
m_nodes.erase(it);
17+
}
18+
19+
20+
/* ASTBinary */
21+
const char* ASTBinary::op_str() const
22+
{
23+
static const char* s_op_strings[] = {
24+
"**", "*", "/", "%", "+", "-", "<<", ">>", "&", "^", "|"
25+
};
26+
return s_op_strings[op()];
27+
}
28+
29+
530
/* ASTCompare */
631
const char* ASTCompare::op_str() const
732
{
833
static const char* s_cmp_strings[] = {
934
"<", "<=", "==", "!=", ">", ">=", "in", "not in", "is", "is not",
1035
"<EXCEPTION MATCH>", "<BAD>"
1136
};
12-
return s_cmp_strings[m_op];
37+
return s_cmp_strings[op()];
1338
}

ASTNode.h

+69-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class ASTNode {
1010
public:
1111
enum Type {
1212
NODE_INVALID, NODE_LIST, NODE_OBJECT, NODE_UNARY, NODE_BINARY,
13-
NODE_COMPARE, NODE_STORE, NODE_RETURN, NODE_NAME, NODE_DELETE
13+
NODE_COMPARE, NODE_STORE, NODE_RETURN, NODE_NAME, NODE_DELETE,
14+
NODE_FUNCTION, NODE_CLASS, NODE_CALL, NODE_PASS
1415
};
1516

1617
ASTNode(int type = NODE_INVALID) : m_refs(0), m_type(type) { }
@@ -38,7 +39,10 @@ class ASTNodeList : public ASTNode {
3839
ASTNodeList(list_t nodes)
3940
: ASTNode(NODE_LIST), m_nodes(nodes) { }
4041

41-
list_t nodes() const { return m_nodes; }
42+
const list_t& nodes() const { return m_nodes; }
43+
void removeFirst();
44+
void removeLast();
45+
void append(PycRef<ASTNode> node) { m_nodes.push_back(node); }
4246

4347
private:
4448
list_t m_nodes;
@@ -71,11 +75,22 @@ class ASTUnary : public ASTNode {
7175

7276
class ASTBinary : public ASTNode {
7377
public:
74-
ASTBinary(PycRef<ASTNode> left, PycRef<ASTNode> right, int type = NODE_BINARY)
75-
: ASTNode(type), m_left(left), m_right(right) { }
78+
enum BinOp {
79+
BIN_POWER, BIN_MULTIPLY, BIN_DIVIDE, BIN_MODULO, BIN_ADD,
80+
BIN_SUBTRACT, BIN_LSHIFT, BIN_RSHIFT, BIN_AND, BIN_XOR, BIN_OR
81+
};
82+
83+
ASTBinary(PycRef<ASTNode> left, PycRef<ASTNode> right, int op,
84+
int type = NODE_BINARY)
85+
: ASTNode(type), m_op(op), m_left(left), m_right(right) { }
7686

7787
PycRef<ASTNode> left() const { return m_left; }
7888
PycRef<ASTNode> right() const { return m_right; }
89+
int op() const { return m_op; }
90+
virtual const char* op_str() const;
91+
92+
protected:
93+
int m_op;
7994

8095
private:
8196
PycRef<ASTNode> m_left;
@@ -91,14 +106,10 @@ class ASTCompare : public ASTBinary {
91106
CMP_EXCEPTION, CMP_BAD
92107
};
93108

94-
ASTCompare(PycRef<ASTNode> left, PycRef<ASTNode> right, CompareOp op)
95-
: ASTBinary(left, right, NODE_COMPARE), m_op(op) { }
109+
ASTCompare(PycRef<ASTNode> left, PycRef<ASTNode> right, int op)
110+
: ASTBinary(left, right, op, NODE_COMPARE) { }
96111

97-
CompareOp op() const { return m_op; }
98112
const char* op_str() const;
99-
100-
private:
101-
CompareOp m_op;
102113
};
103114

104115

@@ -135,7 +146,7 @@ class ASTName : public ASTNode {
135146
ASTName(PycRef<PycString> name)
136147
: ASTNode(NODE_NAME) { m_name.push_back(name); }
137148

138-
name_t name() const { return m_name; }
149+
const name_t& name() const { return m_name; }
139150
void add(PycRef<PycString> name) { m_name.push_back(name); }
140151

141152
private:
@@ -154,4 +165,51 @@ class ASTDelete : public ASTNode {
154165
PycRef<ASTNode> m_value;
155166
};
156167

168+
169+
class ASTFunction : public ASTNode {
170+
public:
171+
typedef std::list<PycRef<ASTNode> > defarg_t;
172+
173+
ASTFunction(PycRef<ASTNode> code, defarg_t defArgs)
174+
: ASTNode(NODE_FUNCTION), m_code(code) { }
175+
176+
PycRef<ASTNode> code() const { return m_code; }
177+
const defarg_t& defargs() const { return m_defargs; }
178+
179+
private:
180+
PycRef<ASTNode> m_code;
181+
defarg_t m_defargs;
182+
};
183+
184+
185+
class ASTClass : public ASTNode {
186+
public:
187+
ASTClass(PycRef<ASTNode> code)
188+
: ASTNode(NODE_CLASS), m_code(code) { }
189+
190+
PycRef<ASTNode> code() const { return m_code; }
191+
192+
private:
193+
PycRef<ASTNode> m_code;
194+
};
195+
196+
197+
class ASTCall : public ASTNode {
198+
public:
199+
typedef std::list<PycRef<ASTNode> > pparam_t;
200+
typedef std::list<std::pair<PycRef<ASTNode>, PycRef<ASTNode> > > kwparam_t;
201+
202+
ASTCall(PycRef<ASTNode> func, pparam_t pparams, kwparam_t kwparams)
203+
: ASTNode(NODE_CALL), m_func(func), m_pparams(pparams), m_kwparams(kwparams) { }
204+
205+
PycRef<ASTNode> func() const { return m_func; }
206+
const pparam_t& pparams() const { return m_pparams; }
207+
const kwparam_t& kwparams() const { return m_kwparams; }
208+
209+
private:
210+
PycRef<ASTNode> m_func;
211+
pparam_t m_pparams;
212+
kwparam_t m_kwparams;
213+
};
214+
157215
#endif

0 commit comments

Comments
 (0)