-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.thyme
More file actions
123 lines (105 loc) · 2.68 KB
/
script.thyme
File metadata and controls
123 lines (105 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
_ackfile = "ack.txt";
_infile = "input.txt";
_outfile = "output.txt";
_lastline = -1;
_pollEvery = 15;
_currentLine = "";
_currentCmd = "";
_currentSeq = 0;
_currentSplit = [];
_parsedCmd = "";
_init = false;
onSpawn = (e)=> {
Sim.running = true;
scene.my.marblecount = 10;
};
postStep = (e) => {
e.this._init ? {} : {
scene.my._outfile = e.this._outfile;
scene.my._eventCount = 0;
scene.my.sendEvent = (type, params) => {
line := "" + scene.my._eventCount + " " + type + " " + math.toString(params);
System.WriteToFile(scene.my._outfile, line + "\n");
scene.my._eventCount = scene.my._eventCount + 1;
};
e.this._init = true;
};
((Sim.tick % e.this._pollEvery) == 0) ? {
e.this._pollCommands(e.this);
} : {};
};
_pollCommands = (e) => {
t = System.ReadWholeFile(e._infile);
(t == nil) ? {} : {
lines = string.split(t, "\n");
for(string.length(lines), (i) => {
(string.length(lines(i)) == 0) ? {} : {
e._currentLine = lines(i);
e._readLine(e);
};
})
}
};
_readLine = (e) => {
split = string.split(e._currentLine, " ");
e._currentSplit = split;
seq = math.toInt(split(0));
cmd = split(1);
e._currentSeq = seq;
e._currentCmd = cmd;
(e._lastline >= seq) ? {} : {
(e._currentCmd == "PING") ? {
e._executePingCmd(e);
} : {};
(e._currentCmd == "PRINT") ? {
e._executePrintCmd(e);
} : {};
(e._currentCmd == "EVAL") ? {
e._executeEvalCmd(e);
} : {};
(e._currentCmd == "RESET") ? {
e._executeResetCmd(e);
} : {
e._ackCmd(e);
};
}
};
_executePrintCmd = (e) => {
print(e._currentSplit(2));
};
_executePingCmd = (e) => {
print("received PING");
};
_executeEvalCmd = (e) => {
parts = e._currentSplit;
n = string.length(parts);
start = 2;
cmd = "";
joinChunks = (from) => {
remaining = n - from;
(remaining <= 0) ? {} : {
chunk = (remaining < 50) ? remaining : 50;
for(chunk, (i) => {
tok = parts(from + i);
cmd = (string.length(cmd) == 0) ? tok : cmd + " " + tok;
});
((from + chunk) < n) ? { joinChunks(from + chunk); } : {};
}();
};
joinChunks(start);
e._parsedCmd = cmd;
print("received command: " + cmd);
geval(cmd);
};
_executeResetCmd = (e) => {
print("reset");
seq = e._currentSeq;
scene.my._eventCount = 0;
System.WriteToFile(e._ackfile, "" + seq + "\n");
e._lastline = -1;
};
_ackCmd = (e) => {
seq = e._currentSeq;
System.WriteToFile(e._ackfile, "" + seq + "\n");
e._lastline = seq;
};