Skip to content

Commit 113b736

Browse files
committed
Implement NFA serialize/deserialize.
1 parent fb1a50d commit 113b736

File tree

1 file changed

+83
-2
lines changed
  • src/vm/jvm/runtime/org/perl6/nqp/sixmodel/reprs

1 file changed

+83
-2
lines changed

src/vm/jvm/runtime/org/perl6/nqp/sixmodel/reprs/NFA.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,91 @@ public SixModelObject deserialize_stub(ThreadContext tc, STable st) {
4444

4545
public void deserialize_finish(ThreadContext tc, STable st,
4646
SerializationReader reader, SixModelObject obj) {
47-
// XXX TODO
47+
NFAInstance body = (NFAInstance)obj;
48+
49+
/* Read fates. */
50+
body.fates = reader.readRef();
51+
52+
/* Read number of states. */
53+
body.numStates = (int)reader.readLong();
54+
55+
if (body.numStates > 0) {
56+
/* Read state edge list counts. */
57+
int[] numStateEdges = new int[body.numStates];
58+
for (int i = 0; i < body.numStates; i++)
59+
numStateEdges[i] = (int)reader.readLong();
60+
61+
/* Read state graph. */
62+
body.states = new NFAStateInfo[body.numStates][];
63+
for (int i = 0; i < body.numStates; i++) {
64+
int edges = numStateEdges[i];
65+
body.states[i] = new NFAStateInfo[edges];
66+
for (int j = 0; j < edges; j++) {
67+
body.states[i][j] = new NFAStateInfo();
68+
body.states[i][j].act = (int)reader.readLong();
69+
body.states[i][j].to = (int)reader.readLong();
70+
switch (body.states[i][j].act) {
71+
case EDGE_FATE:
72+
case EDGE_CODEPOINT:
73+
case EDGE_CODEPOINT_NEG:
74+
case EDGE_CHARCLASS:
75+
case EDGE_CHARCLASS_NEG:
76+
body.states[i][j].arg_i = (int)reader.readLong();
77+
break;
78+
case EDGE_CHARLIST:
79+
case EDGE_CHARLIST_NEG:
80+
body.states[i][j].arg_s = reader.readStr();
81+
break;
82+
case EDGE_CODEPOINT_I:
83+
case EDGE_CODEPOINT_I_NEG: {
84+
body.states[i][j].arg_lc = (char)reader.readLong();
85+
body.states[i][j].arg_uc = (char)reader.readLong();
86+
break;
87+
}
88+
}
89+
}
90+
}
91+
}
4892
}
4993

5094
public void serialize(ThreadContext tc, SerializationWriter writer, SixModelObject obj) {
51-
// XXX TODO
95+
NFAInstance body = (NFAInstance)obj;
96+
97+
/* Write fates. */
98+
writer.writeRef(body.fates);
99+
100+
/* Write number of states. */
101+
writer.writeInt(body.numStates);
102+
103+
/* Write state edge list counts. */
104+
for (int i = 0; i < body.numStates; i++)
105+
writer.writeInt(body.states[i].length);
106+
107+
/* Write state graph. */
108+
for (int i = 0; i < body.numStates; i++) {
109+
for (int j = 0; j < body.states[i].length; j++) {
110+
writer.writeInt(body.states[i][j].act);
111+
writer.writeInt(body.states[i][j].to);
112+
switch (body.states[i][j].act) {
113+
case EDGE_FATE:
114+
case EDGE_CODEPOINT:
115+
case EDGE_CODEPOINT_NEG:
116+
case EDGE_CHARCLASS:
117+
case EDGE_CHARCLASS_NEG:
118+
writer.writeInt(body.states[i][j].arg_i);
119+
break;
120+
case EDGE_CHARLIST:
121+
case EDGE_CHARLIST_NEG:
122+
writer.writeStr(body.states[i][j].arg_s);
123+
break;
124+
case EDGE_CODEPOINT_I:
125+
case EDGE_CODEPOINT_I_NEG: {
126+
writer.writeInt(body.states[i][j].arg_lc);
127+
writer.writeInt(body.states[i][j].arg_uc);
128+
break;
129+
}
130+
}
131+
}
132+
}
52133
}
53134
}

0 commit comments

Comments
 (0)