5 * Created by Alyssa Milburn on Sun Oct 24 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
29 #include "caosScript.h"
31 // #include "malloc.h" <- unportable horror!
33 #include <boost/format.hpp>
39 DBG: OUTS (command) val (string)
43 Outputs a string to the debug log.
45 void caosVM::c_DBG_OUTS() {
48 cout
<< val
<< std::endl
;
52 DBGM (command) val (bareword)
54 %pragma variants c1 c2
55 %pragma implementation caosVM::c_DBG_OUTS
59 DBG: OUTV (command) val (decimal)
63 Outputs a decimal value to the debug log.
65 void caosVM::c_DBG_OUTV() {
70 cout
<< boost::format("%0.06f") % val
.getFloat();
71 } else if (val
.hasInt()) {
73 } else if (val
.hasVector()) {
74 const Vector
<float> &v
= val
.getVector();
75 cout
<< boost::format("(%0.6f, %0.6f)") % v
.x
% v
.y
;
76 } else throw badParamException();
82 DBGV (command) val (integer)
84 %pragma variants c1 c2
85 %pragma implementation caosVM::c_DBG_OUTV
89 DBUG (command) val (integer)
91 %pragma variants c1 c2
93 void caosVM::c_DBUG() {
101 %pragma variants c3 cv sm
103 Returns the unique ID of the target agent.
104 This is currently no good for persisting.
106 XXX: when serialization support works, this might well become good for
109 void caosVM::v_UNID() {
112 result
.setInt(targ
->getUNID());
119 %pragma implementation caosVM::v_UNID_c2
121 Returns the unique ID of the target agent.
122 This is currently no good for persisting.
124 void caosVM::v_UNID_c2() {
127 result
.setAgent(targ
);
131 AGNT (agent) id (integer)
134 Returns the agent with the given UNID, or NULL if agent has been deleted.
136 void caosVM::v_AGNT() {
140 result
.setAgent(world
.lookupUNID(id
));
144 DBG: MALLOC (command)
147 Dumps some random memory stats to stderr.
149 void caosVM::c_DBG_MALLOC() {
152 // more unportable horror!
153 /* struct mallinfo mi = mallinfo();
154 #define MPRINT(name) \
155 fprintf(stderr, "%10s = %d\n", #name, mi. name)
168 /*std::cerr << "caosSlab free=" << caosVarSlab.free_elements() <<
169 " used=" << caosVarSlab.used_elements() <<
170 " total=" << caosVarSlab.total_elements() <<
179 Dumps the current script's bytecode to stderr.
181 void caosVM::c_DBG_DUMP() {
182 std::cerr
<< vm
->currentscript
->dump();
186 DBG: TRACE (command) level (integer)
190 Sets opcode trace level. Zero disables.
192 void caosVM::c_DBG_TRACE() {
195 std::cerr
<< "trace: " << en
<< std::endl
;
202 MANN (command) cmd (string)
205 Looks up documentation on the given command and spits it on the current output stream.
207 void caosVM::c_MANN() {
210 caos_assert(outputstream
);
212 std::transform(cmd
.begin(), cmd
.end(), cmd
.begin(), toupper
);
213 const cmdinfo
*i
= currentscript
->dialect
->cmdbase();
216 while (i
->lookup_key
) {
217 // TODO: this doesn't work for FACE at the moment due to hack elsewhere
218 if (cmd
== i
->fullname
) {
220 std::string d
= i
->docs
;
221 // TODO: docs should always include name/parameters/etc, so should never be empty
223 *outputstream
<< std::string(i
->docs
) << std::endl
;
225 *outputstream
<< "no documentation for " << cmd
<< std::endl
<< std::endl
;
232 *outputstream
<< "didn't find " << cmd
<< std::endl
;
238 DBG: DISA (command) family (integer) genus (integer) species (integer) event (integer)
242 Dumps the "bytecode" of the indicated script to the current output channel.
243 Note that this isn't really bytecode yet (though that's a possible future
246 If the script is not found no output will be generated.
248 void caosVM::c_DBG_DISA() {
249 VM_PARAM_INTEGER(event
)
250 VM_PARAM_INTEGER(species
)
251 VM_PARAM_INTEGER(genus
)
252 VM_PARAM_INTEGER(family
)
254 caos_assert(outputstream
);
256 shared_ptr
<script
> s
= world
.scriptorium
.getScript(family
, genus
, species
, event
);
258 if (s
->fmly
!= family
|| s
->gnus
!= genus
|| s
->spcs
!= species
) {
259 *outputstream
<< "warning: search resulted in script from " << s
->fmly
<< ", " << s
->gnus
<< ", " << s
->spcs
<< " script" << std::endl
;
261 *outputstream
<< s
->dump();
263 *outputstream
<< "no such script" << std::endl
;
267 DBG: ASRT (command) condition (condition)
268 %pragma parser new AssertParser()
272 Blows up unless the given condition is true.
274 void caosVM::c_DBG_ASRT() {
275 throw caosException("DBG: ASRT condition failed");
279 DBG: IDNT (string) agent (agent)
284 Return a nicely-formatted string identifying the classifier of the agent,
285 using the catalogue to find the name if possible.
287 void caosVM::v_DBG_IDNT() {
291 result
.setString("(null)");
293 result
.setString(a
->identify());
300 Dumps the current agent profiling information to the output stream, in CSV format.
302 void caosVM::c_DBG_PROF() {
310 Clears the current agent profiling information.
312 void caosVM::c_DBG_CPRO() {
317 DBG: STOK (string) bareword (bareword)
321 Returns the bare token in 'bareword' as a string.
323 void caosVM::v_DBG_STOK() {
324 VM_PARAM_STRING(bareword
)
326 result
.setString(bareword
);
330 DBG: TSLC (command) timeslice (integer)
335 Sets the currently executing script's remaining timeslice value. This command
336 affects only the current timeslice; future slices use the normal amount for
337 the dialect in question.
339 void caosVM::c_DBG_TSLC() {
340 VM_PARAM_INTEGER(tslc
);
349 Returns the number of ticks left in the current script's remaining timeslice.
351 void caosVM::v_DBG_TSLC() {
352 result
.setInt(timeslice
);