* { font-family: Consolas, Lucida Console, Courier New; font-size: 100%; }
h1 { font-size: 150%; }
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 100%; width: 100%; }
.highlight span { width: 100%; display:-moz-inline-stack; display:inline-block; zoom:1; *display:inline; }
body { background: #ffffff; }
body .c { color: #999988; font-style: italic } /* Comment */
body .err { color: #a61717; background-color: #e3d2d2 } /* Error */
body .k { font-weight: bold } /* Keyword */
body .o { font-weight: bold } /* Operator */
body .cm { color: #999988; font-style: italic } /* Comment.Multiline */
body .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
body .c1 { color: #999988; font-style: italic } /* Comment.Single */
body .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
body .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
body .ge { font-style: italic } /* Generic.Emph */
body .gr { color: #aa0000 } /* Generic.Error */
body .gh { color: #999999 } /* Generic.Heading */
body .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
body .go { color: #888888 } /* Generic.Output */
body .gp { color: #555555 } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { color: #aaaaaa } /* Generic.Subheading */
body .gt { color: #aa0000 } /* Generic.Traceback */
body .kc { font-weight: bold } /* Keyword.Constant */
body .kd { font-weight: bold } /* Keyword.Declaration */
body .kp { font-weight: bold } /* Keyword.Pseudo */
body .kr { font-weight: bold } /* Keyword.Reserved */
body .kt { color: #445588; font-weight: bold } /* Keyword.Type */
body .m { color: #009999 } /* Literal.Number */
body .s { color: #bb8844 } /* Literal.String */
body .na { color: #008080 } /* Name.Attribute */
body .nb { color: #999999 } /* Name.Builtin */
body .nc { color: #445588; font-weight: bold } /* Name.Class */
body .no { color: #008080 } /* Name.Constant */
body .ni { color: #800080 } /* Name.Entity */
body .ne { color: #990000; font-weight: bold } /* Name.Exception */
body .nf { color: #990000; font-weight: bold } /* Name.Function */
body .nn { color: #555555 } /* Name.Namespace */
body .nt { color: #000080 } /* Name.Tag */
body .nv { color: #008080 } /* Name.Variable */
body .ow { font-weight: bold } /* Operator.Word */
body .w { color: #bbbbbb } /* Text.Whitespace */
body .mf { color: #009999 } /* Literal.Number.Float */
body .mh { color: #009999 } /* Literal.Number.Hex */
body .mi { color: #009999 } /* Literal.Number.Integer */
body .mo { color: #009999 } /* Literal.Number.Oct */
body .sb { color: #bb8844 } /* Literal.String.Backtick */
body .sc { color: #bb8844 } /* Literal.String.Char */
body .sd { color: #bb8844 } /* Literal.String.Doc */
body .s2 { color: #bb8844 } /* Literal.String.Double */
body .se { color: #bb8844 } /* Literal.String.Escape */
body .sh { color: #bb8844 } /* Literal.String.Heredoc */
body .si { color: #bb8844 } /* Literal.String.Interpol */
body .sx { color: #bb8844 } /* Literal.String.Other */
body .sr { color: #808000 } /* Literal.String.Regex */
body .s1 { color: #bb8844 } /* Literal.String.Single */
body .ss { color: #bb8844 } /* Literal.String.Symbol */
body .bp { color: #999999 } /* Name.Builtin.Pseudo */
body .vc { color: #008080 } /* Name.Variable.Class */
body .vg { color: #008080 } /* Name.Variable.Global */
body .vi { color: #008080 } /* Name.Variable.Instance */
body .il { color: #009999 } /* Literal.Number.Integer.Long */
Info
Revision:3297
Author:victor
Date:2008-06-06 00:11:53 +0300 (Fri, 06 Jun 2008)
Comment:NXSL: arrays implemented
Changes
U trunk/include/nxsl.h
U trunk/include/nxsl_classes.h
U trunk/src/libnxsl/array.cpp
U trunk/src/libnxsl/program.cpp
Diff
Modified: trunk/include/nxsl.h
===================================================================
--- trunk/include/nxsl.h 2008-06-04 22:21:16 UTC (rev 3296)
+++ trunk/include/nxsl.h 2008-06-05 21:11:53 UTC (rev 3297)
@@ -68,6 +68,8 @@
#define NXSL_ERR_INVALID_OBJECT_OPERATION 20
#define NXSL_ERR_BAD_CLASS 21
#define NXSL_ERR_VARIABLE_ALREADY_EXIST 22
+#define NXSL_ERR_INDEX_NOT_INTEGER 23
+#define NXSL_ERR_NOT_ARRAY 24
//
Modified: trunk/include/nxsl_classes.h
===================================================================
--- trunk/include/nxsl_classes.h 2008-06-04 22:21:16 UTC (rev 3296)
+++ trunk/include/nxsl_classes.h 2008-06-05 21:11:53 UTC (rev 3297)
@@ -123,11 +123,19 @@
// Array
//
+struct NXSL_ArrayElement
+{
+ int index;
+ NXSL_Value *value;
+};
+
class LIBNXSL_EXPORTABLE NXSL_Array
{
private:
int m_refCount;
- NXSL_Value **m_data;
+ int m_size;
+ int m_allocated;
+ NXSL_ArrayElement *m_data;
public:
NXSL_Array();
Modified: trunk/src/libnxsl/array.cpp
===================================================================
--- trunk/src/libnxsl/array.cpp 2008-06-04 22:21:16 UTC (rev 3296)
+++ trunk/src/libnxsl/array.cpp 2008-06-05 21:11:53 UTC (rev 3297)
@@ -31,11 +31,31 @@
NXSL_Array::NXSL_Array()
{
m_refCount = 0;
+ m_size = 0;
+ m_allocated = 0;
+ m_data = NULL;
}
NXSL_Array::NXSL_Array(NXSL_Array *src)
{
+ int i;
+
m_refCount = 0;
+ m_size = src->m_size;
+ m_allocated = src->m_size;
+ if (m_size > 0)
+ {
+ m_data = (NXSL_ArrayElement *)malloc(sizeof(NXSL_ArrayElement) * m_size);
+ for(i = 0; i < m_size; i++)
+ {
+ m_data[i].index = src->m_data[i].index;
+ m_data[i].value = new NXSL_Value(src->m_data[i].value);
+ }
+ }
+ else
+ {
+ m_data = NULL;
+ }
}
@@ -45,4 +65,63 @@
NXSL_Array::~NXSL_Array()
{
+ int i;
+
+ for(i = 0; i < m_size; i++)
+ delete m_data[i].value;
+ safe_free(m_data);
}
+
+
+//
+// Compare two ints
+//
+
+static int CompareElements(const void *p1, const void *p2)
+{
+ return COMPARE_NUMBERS(((NXSL_ArrayElement *)p1)->index, ((NXSL_ArrayElement *)p2)->index);
+}
+
+
+//
+// Get element
+//
+
+NXSL_Value *NXSL_Array::Get(int index)
+{
+ NXSL_ArrayElement *element, key;
+
+ key.index = index;
+ element = (NXSL_ArrayElement *)bsearch(&key, m_data, m_size, sizeof(NXSL_ArrayElement), CompareElements);
+ return (element != NULL) ? element->value : NULL;
+}
+
+
+//
+// Set element
+//
+
+void NXSL_Array::Set(int index, NXSL_Value *value)
+{
+ NXSL_ArrayElement *element, key;
+
+ key.index = index;
+ element = (NXSL_ArrayElement *)bsearch(&key, m_data, m_size, sizeof(NXSL_ArrayElement), CompareElements);
+ if (element != NULL)
+ {
+ delete element->value;
+ element->value = value;
+ }
+ else
+ {
+ if (m_size == m_allocated)
+ {
+ m_allocated += 32;
+ m_data = (NXSL_ArrayElement *)realloc(m_data, sizeof(NXSL_ArrayElement) * m_allocated);
+ }
+ m_data[m_size].index = index;
+ m_data[m_size].value = value;
+ m_size++;
+ qsort(m_data, m_size, sizeof(NXSL_ArrayElement), CompareElements);
+ }
+}
Modified: trunk/src/libnxsl/program.cpp
===================================================================
--- trunk/src/libnxsl/program.cpp 2008-06-04 22:21:16 UTC (rev 3296)
+++ trunk/src/libnxsl/program.cpp 2008-06-05 21:11:53 UTC (rev 3297)
@@ -33,7 +33,7 @@
// Constants
//
-#define MAX_ERROR_NUMBER 22
+#define MAX_ERROR_NUMBER 24
#define CONTROL_STACK_LIMIT 32768
@@ -84,7 +84,9 @@
_T("Function or operation argument is not a whole number"),
_T("Invalid operation on object"),
_T("Bad (or incompatible) object class"),
- _T("Variable already exist")
+ _T("Variable already exist"),
+ _T("Array index is not an integer"),
+ _T("Attempt to use array element access operation on non-array")
};
@@ -608,6 +610,84 @@
Error(NXSL_ERR_VARIABLE_ALREADY_EXIST);
}
break;
+ case OPCODE_SET_ELEMENT: // Set array element; stack should contain: array index value (top)
+ pValue = (NXSL_Value *)m_pDataStack->Pop();
+ if (pValue != NULL)
+ {
+ NXSL_Value *array, *index;
+
+ index = (NXSL_Value *)m_pDataStack->Pop();
+ array = (NXSL_Value *)m_pDataStack->Pop();
+ if ((index != NULL) && (array != NULL))
+ {
+ if (!index->IsInteger())
+ {
+ Error(NXSL_ERR_INDEX_NOT_INTEGER);
+ delete pValue;
+ }
+ else if (!array->IsArray())
+ {
+ Error(NXSL_ERR_NOT_ARRAY);
+ delete pValue;
+ }
+ else
+ {
+ array->GetValueAsArray()->Set(index->GetValueAsInt32(), pValue);
+ }
+ }
+ else
+ {
+ Error(NXSL_ERR_DATA_STACK_UNDERFLOW);
+ delete pValue;
+ }
+ delete index;
+ delete array;
+ }
+ else
+ {
+ Error(NXSL_ERR_DATA_STACK_UNDERFLOW);
+ }
+ break;
+ case OPCODE_GET_ELEMENT: // Get array element; stack should contain: array index (top)
+ pValue = (NXSL_Value *)m_pDataStack->Pop();
+ if (pValue != NULL)
+ {
+ NXSL_Value *array;
+
+ array = (NXSL_Value *)m_pDataStack->Pop();
+ if (array != NULL)
+ {
+ if (array->IsArray())
+ {
+ if (pValue->IsInteger())
+ {
+ NXSL_Value *element;
+
+ element = array->GetValueAsArray()->Get(pValue->GetValueAsInt32());
+ m_pDataStack->Push((element != NULL) ? new NXSL_Value(element) : new NXSL_Value);
+ }
+ else
+ {
+ Error(NXSL_ERR_INDEX_NOT_INTEGER);
+ }
+ }
+ else
+ {
+ Error(NXSL_ERR_NOT_ARRAY);
+ }
+ delete array;
+ }
+ else
+ {
+ Error(NXSL_ERR_DATA_STACK_UNDERFLOW);
+ }
+ delete pValue;
+ }
+ else
+ {
+ Error(NXSL_ERR_DATA_STACK_UNDERFLOW);
+ }
+ break;
case OPCODE_CAST:
pValue = (NXSL_Value *)m_pDataStack->Peek();
if (pValue != NULL)
Received on Fri Jun 06 2008 - 00:11:56 EEST
This archive was generated by hypermail 2.2.0 : Fri Jun 06 2008 - 00:09:20 EEST