private object print(object[] parameters)
        {
            object parameter0 = parameters[0];

            if (parameter0 == null)
            {
                throw new Exception("Parameter0 is null!");
            }
            m_output.Add(ReturnValueConversions.PrettyStringRepresenation(parameter0));
            return(VoidType.voidType);
        }
Exemple #2
0
        private object AddStuffTogetherHack()
        {
            object rhs = PopValue();
            object lhs = PopValue();

            var rightValueType = rhs.GetType();
            var leftValueType  = lhs.GetType();

            //Console.WriteLine("Adding " + lhs + " of type " + leftValueType + " together with " + rhs + " of type " + rightValueType);

            if (rightValueType == typeof(float) && leftValueType == typeof(float))
            {
                return((float)rhs + (float)lhs);
            }
            if (rightValueType == typeof(int) && leftValueType == typeof(int))
            {
                return((float)((int)rhs + (int)lhs));
            }
            else if (rightValueType == typeof(string) || leftValueType == typeof(string))
            {
                return(ReturnValueConversions.PrettyStringRepresenation(lhs) + ReturnValueConversions.PrettyStringRepresenation(rhs));
            }
            else if (rightValueType == typeof(object[]) && leftValueType == typeof(object[]))
            {
                throw new Error("Primitive array concatenation is temporarily disabled.");
            }
            else if (rightValueType == typeof(SortedDictionary <KeyWrapper, object>) && leftValueType == typeof(SortedDictionary <KeyWrapper, object>))
            {
                var lhsArray = lhs as SortedDictionary <KeyWrapper, object>;
                var rhsArray = rhs as SortedDictionary <KeyWrapper, object>;
                var newArray = new SortedDictionary <KeyWrapper, object>();
                for (int i = 0; i < lhsArray.Count; i++)
                {
                    newArray.Add(new KeyWrapper(i), lhsArray[new KeyWrapper(i)]);
                }
                for (int i = 0; i < rhsArray.Count; i++)
                {
                    newArray.Add(new KeyWrapper(i + lhsArray.Count), rhsArray[new KeyWrapper(i)]);
                }
                Console.WriteLine("Created new array by concatenation: " + ReturnValueConversions.PrettyStringRepresenation(newArray));
                return(newArray);
            }
            else
            {
                throw new Error("Can't add " + lhs + " to " + rhs);
            }
        }
Exemple #3
0
        private void ArrayLookup()
        {
            object index = PopValue();
            object array = m_currentScope.getValue(CurrentNode.getTokenString());
            object val   = null;

            if (array is Range)
            {
                //Console.WriteLine ("LOOKING UP KEY " + index + " IN RANGE " + array.ToString ());

                if (index.GetType() == typeof(float))
                {
                    Range range      = (Range)array;
                    float i          = range.step * (int)(float)index;
                    float theNumber  = range.start + i;
                    float lowerBound = 0;
                    float upperBound = 0;
                    if (range.step > 0)
                    {
                        lowerBound = range.start;
                        upperBound = range.end;
                    }
                    else
                    {
                        lowerBound = range.end;
                        upperBound = range.start;
                    }
                    if (theNumber < lowerBound)
                    {
                        throw new Error("Index " + index.ToString() + " is outside the range " + array.ToString());
                    }
                    else if (theNumber > upperBound)
                    {
                        throw new Error("Index " + index.ToString() + " is outside the range " + array.ToString());
                    }
                    val = (float)theNumber;
                    //Console.WriteLine("The result was " + val);
                }
                else
                {
                    throw new Error("Can't look up " + index.ToString() + " in the range " + array.ToString());
                }
            }
            else if (array.GetType() == typeof(SortedDictionary <KeyWrapper, object>))
            {
                //Console.WriteLine ("LOOKING UP KEY " + index + " of type " + index.GetType() + " IN ARRAY " + ReturnValueConversions.PrettyStringRepresenation(array));

                var a = array as SortedDictionary <KeyWrapper, object>;

                if (a.TryGetValue(new KeyWrapper(index), out val))
                {
                    //Console.WriteLine("The result was " + val);
                }
                else
                {
                    throw new Error("Can't find the index '" + index + "' (" + ReturnValueConversions.PrettyObjectType(index.GetType()) + ") in the array '" + CurrentNode.getTokenString() + "'", Error.ErrorType.RUNTIME, CurrentNode.getToken().LineNr, CurrentNode.getToken().LinePosition);
                }
            }
            else if (array.GetType() == typeof(object[]))
            {
                throw new Error("Illegal object[] array: " + ReturnValueConversions.PrettyStringRepresenation(array));
//				var a = (object[])array;
//				if(index.GetType() != typeof(float)) {
//					throw new Exception("Index " + index + " is of wrong type: " + index.GetType());
//				}
//				int i = (int)(float)index;
//				val = a[i];
            }
            else if (array.GetType() == typeof(string))
            {
                int i = 0;
                if (index.GetType() == typeof(float))
                {
                    i = (int)(float)index;
                }
                else if (index.GetType() == typeof(int))
                {
                    i = (int)index;
                }
                else
                {
                    throw new Error("Must use nr when looking up index in string");
                }
                string s = (string)array;
                if (i >= 0 && i < s.Length)
                {
                    val = s[i].ToString();
                }
                else
                {
                    throw new Error("The index '" + i + "' (" + index.GetType() + ") is outside the bounds of the string '" + CurrentNode.getTokenString() + "'", Error.ErrorType.RUNTIME, CurrentNode.getToken().LineNr, CurrentNode.getToken().LinePosition);
                }
            }
            else
            {
                throw new Error("Can't convert " + array.ToString() + " to an array (for lookup)");
            }

            PushValue(val);
        }