[Avida-SVN] r2480 - in development/source: script tools
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Thu Mar 20 14:07:00 PDT 2008
Author: brysonda
Date: 2008-03-20 17:07:00 -0400 (Thu, 20 Mar 2008)
New Revision: 2480
Modified:
development/source/script/cDirectInterpretASTVisitor.cc
development/source/script/cDirectInterpretASTVisitor.h
development/source/tools/tHashTable.h
Log:
AS:
Fill out support for dict builtin methods.
Modified: development/source/script/cDirectInterpretASTVisitor.cc
===================================================================
--- development/source/script/cDirectInterpretASTVisitor.cc 2008-03-20 19:30:32 UTC (rev 2479)
+++ development/source/script/cDirectInterpretASTVisitor.cc 2008-03-20 21:07:00 UTC (rev 2480)
@@ -862,48 +862,80 @@
case AS_BUILTIN_IS_ARRAY:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(ARRAY);
m_rtype = TYPE(BOOL);
break;
case AS_BUILTIN_IS_BOOL:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(BOOL);
m_rtype = TYPE(BOOL);
break;
case AS_BUILTIN_IS_CHAR:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(CHAR);
m_rtype = TYPE(BOOL);
break;
case AS_BUILTIN_IS_DICT:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(DICT);
m_rtype = TYPE(BOOL);
break;
case AS_BUILTIN_IS_INT:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(INT);
m_rtype = TYPE(BOOL);
break;
case AS_BUILTIN_IS_FLOAT:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(FLOAT);
m_rtype = TYPE(BOOL);
break;
case AS_BUILTIN_IS_MATRIX:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(MATRIX);
m_rtype = TYPE(BOOL);
break;
case AS_BUILTIN_IS_STRING:
args->Iterator().Next()->Accept(*this);
+ {
+ sAggregateValue val(m_rtype, m_rvalue);
+ val.Cleanup();
+ }
m_rvalue.as_bool = m_rtype.type == TYPE(STRING);
m_rtype = TYPE(BOOL);
break;
@@ -911,29 +943,75 @@
case AS_BUILTIN_CLEAR:
trgt->Accept(*this);
- if (m_rtype.type == TYPE(DICT)) { // @TODO - builtin clear
- INTERPRET_ERROR(INTERNAL);
+ if (m_rtype.type == TYPE(DICT)) {
+ m_rvalue.as_dict->Clear();
+ m_rvalue.as_dict->RemoveReference();
} else if (m_rtype.type == TYPE(MATRIX)) { // @TODO - builtin clear
INTERPRET_ERROR(INTERNAL);
} else if (m_rtype.type == TYPE(ARRAY)) {
m_rvalue.as_array->Resize(0);
+ m_rvalue.as_array->RemoveReference();
} else {
- INTERPRET_ERROR(UNDEFINED_TYPE_OP, "resize", mapType(m_rtype));
+ INTERPRET_ERROR(UNDEFINED_TYPE_OP, "clear", mapType(m_rtype));
}
break;
- case AS_BUILTIN_COPY: // @TODO
- INTERPRET_ERROR(INTERNAL);
+ case AS_BUILTIN_COPY:
+ trgt->Accept(*this);
+ if (m_rtype.type == TYPE(ARRAY)) {
+ cLocalArray* arr = new cLocalArray(m_rvalue.as_array);
+ m_rvalue.as_array->RemoveReference();
+ m_rvalue.as_array = arr;
+ } else {
+ INTERPRET_ERROR(UNDEFINED_TYPE_OP, "copy", mapType(m_rtype));
+ }
+ break;
- case AS_BUILTIN_HASKEY: // @TODO
- INTERPRET_ERROR(INTERNAL);
+ case AS_BUILTIN_HASKEY:
+ trgt->Accept(*this);
+ if (m_rtype.type == TYPE(DICT)) {
+ cLocalDict* dict = m_rvalue.as_dict;
+
+ args->Iterator().Next()->Accept(*this);
+ sAggregateValue idx(m_rtype, m_rvalue);
+ m_rvalue.as_bool = dict->HasKey(idx);
+ idx.Cleanup();
+ dict->RemoveReference();
+
+ m_rtype = TYPE(BOOL);
+ } else {
+ INTERPRET_ERROR(UNDEFINED_TYPE_OP, "keys", mapType(m_rtype));
+ }
+ break;
- case AS_BUILTIN_KEYS: // @TODO
- INTERPRET_ERROR(INTERNAL);
+ case AS_BUILTIN_KEYS:
+ trgt->Accept(*this);
+ if (m_rtype.type == TYPE(DICT)) {
+ cLocalArray* arr = new cLocalArray();
+ arr->SetWithKeys(m_rvalue.as_dict);
+ m_rvalue.as_dict->RemoveReference();
+ m_rvalue.as_array = arr;
+ m_rtype = TYPE(ARRAY);
+ } else {
+ INTERPRET_ERROR(UNDEFINED_TYPE_OP, "keys", mapType(m_rtype));
+ }
+ break;
+
case AS_BUILTIN_VALUES: // @TODO
- INTERPRET_ERROR(INTERNAL);
+ trgt->Accept(*this);
+ if (m_rtype.type == TYPE(DICT)) {
+ cLocalArray* arr = new cLocalArray();
+ arr->SetWithValues(m_rvalue.as_dict);
+ m_rvalue.as_dict->RemoveReference();
+ m_rvalue.as_array = arr;
+ m_rtype = TYPE(ARRAY);
+ } else {
+ INTERPRET_ERROR(UNDEFINED_TYPE_OP, "values", mapType(m_rtype));
+ }
+ break;
+
case AS_BUILTIN_LEN:
trgt->Accept(*this);
if (m_rtype == TYPE(STRING)) {
@@ -948,22 +1026,58 @@
m_rtype = TYPE(INT);
break;
- case AS_BUILTIN_REMOVE: // @TODO
- INTERPRET_ERROR(INTERNAL);
-
+ case AS_BUILTIN_REMOVE:
+ trgt->Accept(*this);
+ if (m_rtype.type == TYPE(DICT)) {
+ cLocalDict* dict = m_rvalue.as_dict;
+
+ if (!dict->IsShared()) {
+ dict->RemoveReference();
+ break;
+ }
+
+ args->Iterator().Next()->Accept(*this);
+ sAggregateValue idx(m_rtype, m_rvalue);
+ dict->Remove(idx);
+ idx.Cleanup();
+ dict->RemoveReference();
+ } else if (m_rtype.type == TYPE(ARRAY)) {
+ cLocalArray* arr = m_rvalue.as_array;
+
+ if (!arr->IsShared()) {
+ arr->RemoveReference();
+ break;
+ }
+
+ args->Iterator().Next()->Accept(*this);
+ int i = asInt(m_rtype, m_rvalue, node);
+
+ if (i < 0 || i >= arr->GetSize()) INTERPRET_ERROR(INDEX_OUT_OF_BOUNDS);
+
+ for (; i < (arr->GetSize() - 1); i++) arr->Set(i, arr->Get(i + 1));
+ arr->Resize(arr->GetSize() - 1);
+ } else {
+ INTERPRET_ERROR(UNDEFINED_TYPE_OP, "remove", mapType(m_rtype));
+ }
+ break;
+
case AS_BUILTIN_RESIZE:
trgt->Accept(*this);
- if (m_rtype.type == TYPE(DICT)) { // @TODO - builtin resize
+ if (m_rtype.type == TYPE(MATRIX)) { // @TODO - builtin resize
INTERPRET_ERROR(INTERNAL);
- } else if (m_rtype.type == TYPE(MATRIX)) { // @TODO - builtin resize
- INTERPRET_ERROR(INTERNAL);
} else if (m_rtype.type == TYPE(ARRAY)) {
cLocalArray* arr = m_rvalue.as_array;
+
+ if (!arr->IsShared()) {
+ arr->RemoveReference();
+ break;
+ }
args->Iterator().Next()->Accept(*this);
int sz = asInt(m_rtype, m_rvalue, node);
arr->Resize(sz);
+ arr->RemoveReference();
} else {
INTERPRET_ERROR(UNDEFINED_TYPE_OP, "resize", mapType(m_rtype));
}
@@ -1728,6 +1842,47 @@
}
}
+
+void cDirectInterpretASTVisitor::cLocalArray::SetWithKeys(cLocalDict* dict)
+{
+ Resize(0);
+
+ dict->GetKeys(m_storage);
+
+ for (int i = 0; i < m_storage.GetSize(); i++) {
+ switch (m_storage[i].type.type) {
+ case TYPE(STRING): m_storage[i].value.as_string = new cString(*m_storage[i].value.as_string); break;
+ case TYPE(ARRAY): m_storage[i].value.as_array->GetReference(); break;
+ case TYPE(DICT): m_storage[i].value.as_dict->GetReference(); break;
+ case TYPE(MATRIX): // @TODO - array copy
+ break;
+
+ default: break;
+ }
+ }
+}
+
+
+void cDirectInterpretASTVisitor::cLocalArray::SetWithValues(cLocalDict* dict)
+{
+ Resize(0);
+
+ dict->GetValues(m_storage);
+
+ for (int i = 0; i < m_storage.GetSize(); i++) {
+ switch (m_storage[i].type.type) {
+ case TYPE(STRING): m_storage[i].value.as_string = new cString(*m_storage[i].value.as_string); break;
+ case TYPE(ARRAY): m_storage[i].value.as_array->GetReference(); break;
+ case TYPE(DICT): m_storage[i].value.as_dict->GetReference(); break;
+ case TYPE(MATRIX): // @TODO - array copy
+ break;
+
+ default: break;
+ }
+ }
+}
+
+
cDirectInterpretASTVisitor::cLocalArray::~cLocalArray()
{
int sz = m_storage.GetSize();
@@ -1743,19 +1898,37 @@
if (m_storage.Find(idx, o_val)) {
o_val.Cleanup();
m_storage.SetValue(idx, val);
- const_cast<sAggregateValue&>(idx).Cleanup(); // make sure to remove references to the index value
} else {
m_storage.SetValue(idx, val);
}
}
+void cDirectInterpretASTVisitor::cLocalDict::Clear()
+{
+ tList<sAggregateValue> keys;
+ tList<sAggregateValue> vals;
+
+ m_storage.AsListsUnsorted(keys, vals);
+
+ sAggregateValue* av = NULL;
+
+ tListIterator<sAggregateValue> kit(keys);
+ while ((av = kit.Next())) av->Cleanup();
+
+ tListIterator<sAggregateValue> vit(vals);
+ while ((av = vit.Next())) av->Cleanup();
+
+ m_storage.ClearAll();
+}
cDirectInterpretASTVisitor::cLocalDict::~cLocalDict()
{
tList<sAggregateValue> keys;
tList<sAggregateValue> vals;
+ m_storage.AsListsUnsorted(keys, vals);
+
sAggregateValue* av = NULL;
tListIterator<sAggregateValue> kit(keys);
Modified: development/source/script/cDirectInterpretASTVisitor.h
===================================================================
--- development/source/script/cDirectInterpretASTVisitor.h 2008-03-20 19:30:32 UTC (rev 2479)
+++ development/source/script/cDirectInterpretASTVisitor.h 2008-03-20 21:07:00 UTC (rev 2480)
@@ -161,8 +161,12 @@
inline const sAggregateValue& Get(int i) const { return m_storage[i]; }
void Set(int i, const sASTypeInfo& type, uAnyType value);
+ inline void Set(int i, const sAggregateValue& val) { Set(i, val.type, val.value); }
+ void SetWithKeys(cLocalDict* dict);
+ void SetWithValues(cLocalDict* dict);
+
private:
void copy(int offset, tArray<sAggregateValue>& in_storage);
};
@@ -185,9 +189,15 @@
inline int GetSize() const { return m_storage.GetSize(); }
+ void Clear();
+
bool Get(const sAggregateValue& idx, sAggregateValue& val) const { return m_storage.Find(idx, val); }
void Set(const sAggregateValue& idx, const sAggregateValue& val);
- void Remove(const sAggregateValue& idx) { m_storage.Remove(idx); }
+ void Remove(const sAggregateValue& idx) { sAggregateValue val = m_storage.Remove(idx); val.Cleanup(); }
+
+ inline bool HasKey(const sAggregateValue& idx) { return m_storage.HasEntry(idx); }
+ inline void GetKeys(tArray<sAggregateValue>& out_array) { m_storage.GetKeys(out_array); }
+ inline void GetValues(tArray<sAggregateValue>& out_array) { m_storage.GetValues(out_array); }
};
Modified: development/source/tools/tHashTable.h
===================================================================
--- development/source/tools/tHashTable.h 2008-03-20 19:30:32 UTC (rev 2479)
+++ development/source/tools/tHashTable.h 2008-03-20 21:07:00 UTC (rev 2480)
@@ -386,13 +386,38 @@
}
+ // The following method allows the user to convert the hash table contents into lists.
+ // Empty lists show be passed in as arguments and the method will fill in their contents.
+ void AsListsUnsorted(tList<HASH_TYPE>& key_list, tList<DATA_TYPE>& value_list) const
+ {
+ assert(key_list.GetSize() == 0);
+ assert(value_list.GetSize() == 0);
+
+ // Loop through the current entries and included them into the output list one at a time.
+ list_it.Reset();
+ while (list_it.Next() != NULL) {
+ key_list.Push(&list_it.Get()->key);
+ value_list.Push(&list_it.Get()->data);
+ }
+ }
+
+
void GetKeys(tList<HASH_TYPE>& key_list) const
{
list_it.Reset();
while (list_it.Next() != NULL) key_list.Push(&list_it.Get()->key);
}
+ void GetKeys(tArray<DATA_TYPE>& value_array) const
+ {
+ value_array.Resize(entry_count);
+ int idx = 0;
+
+ list_it.Reset();
+ while (list_it.Next() != NULL) value_array[idx++] = list_it.Get()->key;
+ }
+
void GetValues(tList<DATA_TYPE>& value_list) const
{
list_it.Reset();
More information about the Avida-cvs
mailing list