2009-02-17 15:02:16 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-27 02:23:40 +00:00
|
|
|
#include "sci/engine/state.h"
|
2009-02-24 05:51:55 +00:00
|
|
|
#include "sci/engine/kernel.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
#ifdef LOOKUP_NODE
|
|
|
|
# undef LOOKUP_NODE
|
|
|
|
# define LOOKUP_NODE(addr) inline_lookup_node(s, (addr), __FILE__, __LINE__)
|
|
|
|
#endif
|
|
|
|
|
2009-02-28 11:12:59 +00:00
|
|
|
inline Node *inline_lookup_node(EngineState *s, reg_t addr, const char *file, int line) {
|
|
|
|
MemObject *mobj;
|
|
|
|
NodeTable *nt;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!addr.offset && !addr.segment)
|
2009-02-20 16:18:08 +00:00
|
|
|
return NULL; // Non-error null
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 23:27:24 +00:00
|
|
|
mobj = GET_SEGMENT(*s->seg_manager, addr.segment, MEM_OBJ_NODES);
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!mobj) {
|
2009-02-20 16:18:08 +00:00
|
|
|
sciprintf("%s, L%d: Attempt to use non-node "PREG" as list node\n", __FILE__, __LINE__, PRINT_REG(addr));
|
2009-02-15 06:10:59 +00:00
|
|
|
script_debug_flag = script_error_flag = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nt = &(mobj->data.nodes);
|
|
|
|
|
|
|
|
if (!ENTRY_IS_VALID(nt, addr.offset)) {
|
2009-02-20 16:18:08 +00:00
|
|
|
sciprintf("%s, L%d: Attempt to use non-node "PREG" as list node\n", __FILE__, __LINE__, PRINT_REG(addr));
|
2009-02-15 06:10:59 +00:00
|
|
|
script_debug_flag = script_error_flag = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &(nt->table[addr.offset].entry);
|
|
|
|
}
|
|
|
|
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *lookup_node(EngineState *s, reg_t addr, const char *file, int line) {
|
2009-02-15 06:10:59 +00:00
|
|
|
return inline_lookup_node(s, addr, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOOKUP_NULL_LIST(addr) _lookup_list(s, addr, __FILE__, __LINE__, 1)
|
|
|
|
|
2009-02-28 11:12:59 +00:00
|
|
|
inline List *_lookup_list(EngineState *s, reg_t addr, const char *file, int line, int may_be_null) {
|
|
|
|
MemObject *mobj;
|
|
|
|
ListTable *lt;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (may_be_null && !addr.segment && !addr.offset)
|
|
|
|
return NULL;
|
|
|
|
|
2009-02-21 23:27:24 +00:00
|
|
|
mobj = GET_SEGMENT(*s->seg_manager, addr.segment, MEM_OBJ_LISTS);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!mobj) {
|
2009-02-20 16:18:08 +00:00
|
|
|
sciprintf("%s, L%d: Attempt to use non-list "PREG" as list\n", __FILE__, __LINE__, PRINT_REG(addr));
|
2009-02-15 06:10:59 +00:00
|
|
|
script_debug_flag = script_error_flag = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lt = &(mobj->data.lists);
|
|
|
|
|
|
|
|
if (!ENTRY_IS_VALID(lt, addr.offset)) {
|
2009-02-20 16:18:08 +00:00
|
|
|
sciprintf("%s, L%d: Attempt to use non-list "PREG" as list\n", __FILE__, __LINE__, PRINT_REG(addr));
|
2009-02-15 06:10:59 +00:00
|
|
|
script_debug_flag = script_error_flag = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &(lt->table[addr.offset].entry);
|
|
|
|
}
|
|
|
|
|
2009-02-28 11:12:59 +00:00
|
|
|
List *lookup_list(EngineState *s, reg_t addr, const char *file, int line) {
|
2009-02-15 06:10:59 +00:00
|
|
|
return _lookup_list(s, addr, file, line, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DISABLE_VALIDATIONS
|
|
|
|
|
|
|
|
#define sane_nodep(a, b) 1
|
|
|
|
#define sane_listp(a, b) 1
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
static inline int sane_nodep(EngineState *s, reg_t addr) {
|
2009-02-15 06:10:59 +00:00
|
|
|
int have_prev = 0;
|
2009-02-15 14:52:51 +00:00
|
|
|
reg_t prev = addr;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
do {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *node = LOOKUP_NODE(addr);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return 0;
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
if ((have_prev) && !REG_EQ(node->pred, prev))
|
2009-02-15 06:10:59 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
prev = addr;
|
|
|
|
addr = node->succ;
|
|
|
|
have_prev = 1;
|
|
|
|
} while (!IS_NULL_REG(addr));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
int sane_listp(EngineState *s, reg_t addr) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_LIST(addr);
|
2009-02-15 06:10:59 +00:00
|
|
|
int empties = 0;
|
|
|
|
|
|
|
|
if (IS_NULL_REG(l->first))
|
|
|
|
++empties;
|
|
|
|
if (IS_NULL_REG(l->last))
|
|
|
|
++empties;
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
// Either none or both must be set
|
2009-02-15 06:10:59 +00:00
|
|
|
if (empties == 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!empties) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *node_a, *node_z;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
node_a = LOOKUP_NODE(l->first);
|
|
|
|
node_z = LOOKUP_NODE(l->last);
|
|
|
|
|
|
|
|
if (!node_a || !node_z)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!IS_NULL_REG(node_a->pred))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!IS_NULL_REG(node_z->succ))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return sane_nodep(s, l->first);
|
|
|
|
}
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
return 1; // Empty list is fine
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kNewList(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-15 06:10:59 +00:00
|
|
|
reg_t listbase;
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l;
|
|
|
|
l = s->seg_manager->alloc_List(&listbase);
|
2009-02-15 06:10:59 +00:00
|
|
|
l->first = l->last = NULL_REG;
|
|
|
|
SCIkdebug(SCIkNODES, "New listbase at "PREG"\n", PRINT_REG(listbase));
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
return listbase; // Return list base address
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kDisposeList(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_LIST(argv[0]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!l) {
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "Attempt to dispose non-list at "PREG"!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sane_listp(s, argv[0]))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is not sane anymore!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
/* if (!IS_NULL_REG(l->first)) {
|
|
|
|
reg_t n_addr = l->first;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
while (!IS_NULL_REG(n_addr)) { // Free all nodes
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *n = LOOKUP_NODE(n_addr);
|
|
|
|
s->seg_manager->free_Node(n_addr);
|
2009-02-20 16:18:08 +00:00
|
|
|
n_addr = n->succ;
|
2009-02-15 22:28:12 +00:00
|
|
|
}
|
2009-02-20 16:18:08 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 23:27:24 +00:00
|
|
|
s->seg_manager->free_list(argv[0]);
|
2009-02-20 16:18:08 +00:00
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
return s->r_acc;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
inline reg_t _k_new_node(EngineState *s, reg_t value, reg_t key) {
|
2009-02-15 06:10:59 +00:00
|
|
|
reg_t nodebase;
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *n = s->seg_manager->alloc_Node(&nodebase);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!n) {
|
|
|
|
KERNEL_OOPS("Out of memory while creating a node");
|
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
|
|
|
n->pred = n->succ = NULL_REG;
|
|
|
|
n->key = key;
|
|
|
|
n->value = value;
|
|
|
|
|
|
|
|
return nodebase;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kNewNode(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-15 06:10:59 +00:00
|
|
|
s->r_acc = _k_new_node(s, argv[0], argv[1]);
|
|
|
|
|
|
|
|
SCIkdebug(SCIkNODES, "New nodebase at "PREG"\n", PRINT_REG(s->r_acc));
|
|
|
|
|
|
|
|
return s->r_acc;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kFirstNode(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_NULL_LIST(argv[0]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (l && !sane_listp(s, argv[0]))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is not sane anymore!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (l)
|
|
|
|
return l->first;
|
|
|
|
else
|
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kLastNode(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_LIST(argv[0]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-15 22:28:12 +00:00
|
|
|
if (l && !sane_listp(s, argv[0]))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is not sane anymore!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (l)
|
|
|
|
return l->last;
|
|
|
|
else
|
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kEmptyList(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_LIST(argv[0]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!l || !sane_listp(s, argv[0]))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is invalid or not sane anymore!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-15 22:28:12 +00:00
|
|
|
return make_reg(0, ((l) ? IS_NULL_REG(l->first) : 0));
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
inline void _k_add_to_front(EngineState *s, reg_t listbase, reg_t nodebase) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_LIST(listbase);
|
|
|
|
Node *new_n = LOOKUP_NODE(nodebase);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
SCIkdebug(SCIkNODES, "Adding node "PREG" to end of list "PREG"\n", PRINT_REG(nodebase), PRINT_REG(listbase));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!new_n)
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "Attempt to add non-node ("PREG") to list at "PREG"\n", PRINT_REG(nodebase), PRINT_REG(listbase));
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!l || !sane_listp(s, listbase))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is not sane anymore!\n", PRINT_REG(listbase));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
new_n->succ = l->first;
|
|
|
|
new_n->pred = NULL_REG;
|
2009-02-20 16:18:08 +00:00
|
|
|
// Set node to be the first and last node if it's the only node of the list
|
2009-02-15 06:10:59 +00:00
|
|
|
if (IS_NULL_REG(l->first))
|
|
|
|
l->last = nodebase;
|
|
|
|
else {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *old_n = LOOKUP_NODE(l->first);
|
2009-02-15 06:10:59 +00:00
|
|
|
old_n->pred = nodebase;
|
|
|
|
}
|
|
|
|
l->first = nodebase;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
inline void _k_add_to_end(EngineState *s, reg_t listbase, reg_t nodebase) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_LIST(listbase);
|
|
|
|
Node *new_n = LOOKUP_NODE(nodebase);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
SCIkdebug(SCIkNODES, "Adding node "PREG" to end of list "PREG"\n", PRINT_REG(nodebase), PRINT_REG(listbase));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!new_n)
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "Attempt to add non-node ("PREG") to list at "PREG"\n", PRINT_REG(nodebase), PRINT_REG(listbase));
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!l || !sane_listp(s, listbase))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is not sane anymore!\n", PRINT_REG(listbase));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
new_n->succ = NULL_REG;
|
|
|
|
new_n->pred = l->last;
|
2009-02-20 16:18:08 +00:00
|
|
|
// Set node to be the first and last node if it's the only node of the list
|
2009-02-15 06:10:59 +00:00
|
|
|
if (IS_NULL_REG(l->last))
|
|
|
|
l->first = nodebase;
|
|
|
|
else {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *old_n = LOOKUP_NODE(l->last);
|
2009-02-15 06:10:59 +00:00
|
|
|
old_n->succ = nodebase;
|
|
|
|
}
|
|
|
|
l->last = nodebase;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kNextNode(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *n = LOOKUP_NODE(argv[0]);
|
2009-02-15 22:28:12 +00:00
|
|
|
if (!sane_nodep(s, argv[0])) {
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List node at "PREG" is not sane anymore!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
script_error_flag = script_debug_flag = 0;
|
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n->succ;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kPrevNode(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *n = LOOKUP_NODE(argv[0]);
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!sane_nodep(s, argv[0]))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List node at "PREG" is not sane anymore!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
return n->pred;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kNodeValue(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *n = LOOKUP_NODE(argv[0]);
|
2009-02-15 22:28:12 +00:00
|
|
|
if (!sane_nodep(s, argv[0])) {
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List node at "PREG" is not sane!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
script_debug_flag = script_error_flag = 0;
|
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n->value;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kAddToFront(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-15 06:10:59 +00:00
|
|
|
_k_add_to_front(s, argv[0], argv[1]);
|
|
|
|
return s->r_acc;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kAddAfter(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-28 11:12:59 +00:00
|
|
|
List *l = LOOKUP_LIST(argv[0]);
|
|
|
|
Node *firstnode = IS_NULL_REG(argv[1]) ? NULL : LOOKUP_NODE(argv[1]);
|
|
|
|
Node *newnode = LOOKUP_NODE(argv[2]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!l || !sane_listp(s, argv[0]))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is not sane anymore!\n", PRINT_REG(argv[0]));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!newnode) {
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "New 'node' "PREG" is not a node!\n", argv[1], argv[2]);
|
2009-02-15 06:10:59 +00:00
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != 3) {
|
2009-02-20 20:11:12 +00:00
|
|
|
warning("kAddAfter: Haven't got 3 arguments, aborting");
|
2009-02-15 06:10:59 +00:00
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
if (firstnode) { // We're really appending after
|
2009-02-15 06:10:59 +00:00
|
|
|
reg_t oldnext = firstnode->succ;
|
|
|
|
|
|
|
|
newnode->pred = argv[1];
|
|
|
|
firstnode->succ = argv[2];
|
|
|
|
newnode->succ = oldnext;
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
if (IS_NULL_REG(oldnext)) // Appended after last node?
|
|
|
|
// Set new node as last list node
|
2009-02-15 06:10:59 +00:00
|
|
|
l->last = argv[2];
|
|
|
|
else
|
|
|
|
LOOKUP_NODE(oldnext)->pred = argv[2];
|
|
|
|
|
|
|
|
return s->r_acc;
|
2009-02-20 16:18:08 +00:00
|
|
|
} else { // !firstnode
|
|
|
|
// Prepare call to AddToFront...
|
2009-02-15 06:10:59 +00:00
|
|
|
argv[1] = argv[0];
|
2009-02-20 16:18:08 +00:00
|
|
|
return kAddToFront(s, funct_nr, 2, argv + 1); // Set as initial list node
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kAddToEnd(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-15 06:10:59 +00:00
|
|
|
_k_add_to_end(s, argv[0], argv[1]);
|
|
|
|
return s->r_acc;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kFindKey(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-15 06:10:59 +00:00
|
|
|
reg_t node_pos;
|
|
|
|
reg_t key = argv[1];
|
|
|
|
reg_t list_pos = argv[0];
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
SCIkdebug(SCIkNODES, "Looking for key "PREG" in list "PREG"\n", PRINT_REG(key), PRINT_REG(list_pos));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!sane_listp(s, list_pos))
|
2009-02-21 14:11:41 +00:00
|
|
|
SCIkwarn(SCIkERROR, "List at "PREG" is not sane anymore!\n", PRINT_REG(list_pos));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
node_pos = LOOKUP_LIST(list_pos)->first;
|
|
|
|
|
|
|
|
SCIkdebug(SCIkNODES, "First node at "PREG"\n", PRINT_REG(node_pos));
|
|
|
|
|
|
|
|
while (!IS_NULL_REG(node_pos)) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *n = LOOKUP_NODE(node_pos);
|
2009-02-15 06:10:59 +00:00
|
|
|
if (REG_EQ(n->key, key)) {
|
|
|
|
SCIkdebug(SCIkNODES, " Found key at "PREG"\n", PRINT_REG(node_pos));
|
|
|
|
return node_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
node_pos = n->succ;
|
|
|
|
SCIkdebug(SCIkNODES, "NextNode at "PREG"\n", PRINT_REG(node_pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
SCIkdebug(SCIkNODES, "Looking for key without success\n");
|
|
|
|
return NULL_REG;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kDeleteKey(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-15 06:10:59 +00:00
|
|
|
reg_t node_pos = kFindKey(s, funct_nr, 2, argv);
|
2009-02-28 11:12:59 +00:00
|
|
|
Node *n;
|
|
|
|
List *l = LOOKUP_LIST(argv[0]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (IS_NULL_REG(node_pos))
|
2009-02-20 16:18:08 +00:00
|
|
|
return NULL_REG; // Signal falure
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
n = LOOKUP_NODE(node_pos);
|
|
|
|
if (REG_EQ(l->first, node_pos))
|
|
|
|
l->first = n->succ;
|
|
|
|
if (REG_EQ(l->last, node_pos))
|
|
|
|
l->last = n->pred;
|
|
|
|
|
|
|
|
if (!IS_NULL_REG(n->pred))
|
|
|
|
LOOKUP_NODE(n->pred)->succ = n->succ;
|
|
|
|
if (!IS_NULL_REG(n->succ))
|
|
|
|
LOOKUP_NODE(n->succ)->pred = n->pred;
|
|
|
|
|
2009-02-28 11:12:59 +00:00
|
|
|
//s->seg_manager->free_Node(node_pos);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
return make_reg(0, 1); // Signal success
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 22:06:42 +00:00
|
|
|
struct sort_temp_t {
|
2009-02-15 06:10:59 +00:00
|
|
|
reg_t key, value;
|
|
|
|
reg_t order;
|
2009-02-21 22:06:42 +00:00
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
int sort_temp_cmp(const void *p1, const void *p2) {
|
|
|
|
sort_temp_t *st1 = (sort_temp_t *)p1;
|
|
|
|
sort_temp_t *st2 = (sort_temp_t *)p2;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
if (st1->order.segment < st1->order.segment || (st1->order.segment == st1->order.segment && st1->order.offset < st2->order.offset))
|
2009-02-15 06:10:59 +00:00
|
|
|
return -1;
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
if (st1->order.segment > st2->order.segment || (st1->order.segment == st2->order.segment && st1->order.offset > st2->order.offset))
|
2009-02-15 06:10:59 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t kSort(EngineState *s, int funct_nr, int argc, reg_t *argv) {
|
2009-02-15 06:10:59 +00:00
|
|
|
reg_t source = argv[0];
|
|
|
|
reg_t dest = argv[1];
|
|
|
|
reg_t order_func = argv[2];
|
|
|
|
|
|
|
|
int input_size = GET_SEL32SV(source, size);
|
|
|
|
int i;
|
|
|
|
|
2009-02-20 16:18:08 +00:00
|
|
|
sort_temp_t *temp_array = (sort_temp_t *)malloc(sizeof(sort_temp_t) * input_size);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
reg_t input_data = GET_SEL32(source, elements);
|
|
|
|
reg_t output_data = GET_SEL32(dest, elements);
|
|
|
|
|
2009-02-28 11:12:59 +00:00
|
|
|
List *list;
|
|
|
|
Node *node;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (!input_size)
|
|
|
|
return s->r_acc;
|
|
|
|
|
2009-02-15 22:28:12 +00:00
|
|
|
if (IS_NULL_REG(output_data)) {
|
2009-02-28 11:12:59 +00:00
|
|
|
list = s->seg_manager->alloc_List(&output_data);
|
2009-02-15 06:10:59 +00:00
|
|
|
list->first = list->last = NULL_REG;
|
|
|
|
PUT_SEL32(dest, elements, output_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
PUT_SEL32V(dest, size, input_size);
|
|
|
|
|
|
|
|
list = LOOKUP_LIST(input_data);
|
|
|
|
node = LOOKUP_NODE(list->first);
|
|
|
|
|
|
|
|
i = 0;
|
2009-02-15 22:28:12 +00:00
|
|
|
while (node) {
|
2009-02-15 06:10:59 +00:00
|
|
|
invoke_selector(INV_SEL(order_func, doit, 0), 1, node->value);
|
|
|
|
temp_array[i].key = node->key;
|
|
|
|
temp_array[i].value = node->value;
|
|
|
|
temp_array[i].order = s->r_acc;
|
|
|
|
i++;
|
|
|
|
node = LOOKUP_NODE(node->succ);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(temp_array, input_size, sizeof(sort_temp_t), sort_temp_cmp);
|
|
|
|
|
2009-02-15 22:28:12 +00:00
|
|
|
for (i = 0;i < input_size;i++) {
|
2009-02-20 16:18:08 +00:00
|
|
|
reg_t lNode = _k_new_node(s, temp_array[i].key, temp_array[i].value);
|
2009-02-15 15:06:14 +00:00
|
|
|
_k_add_to_end(s, output_data, lNode);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(temp_array);
|
|
|
|
|
|
|
|
return s->r_acc;
|
|
|
|
}
|
2009-02-21 10:23:36 +00:00
|
|
|
|
|
|
|
} // End of namespace Sci
|