Beispiel #1
0
        private int testArraySize(TypName typ, int index)
        {
            int scope = 0;
            int size  = 0;

            do
            {
                switch (tokenList[index].value)
                {
                case "[": scope++;
                    if (tokenList[index + 1].value == "]")
                    {
                        return(0);
                    }
                    break;

                case "]": scope--; break;

                case ",": size++; break;

                case "to":
                    size += Math.Abs(Convert.ToInt32(readNativeValue(typ, referseToTokenIndex(index, new string[] { "[", "]", "," }) + 1)) - Convert.ToInt32(readNativeValue(typ, index + 1)));
                    break;
                }
                index++;
            } while (scope > 0);
            return(size + 1);
        }
Beispiel #2
0
        private void addValueToArray(ref object array, TypName typ, object value, int index)
        {
            switch (typ)
            {
            case TypName.Ref:
            case TypName.Byte: ((byte[])array)[index] = Convert.ToByte(value); break;

            case TypName.Int: ((int[])array)[index] = Convert.ToInt32(value); break;

            case TypName.Float: ((float[])array)[index] = Convert.ToSingle(value); break;

            case TypName.Double: ((double[])array)[index] = Convert.ToDouble(value); break;

            case TypName.Bool: ((bool[])array)[index] = Convert.ToBoolean(value); break;

            case TypName.String: ((string[])array)[index] = (string)value; break;
            }
        }
Beispiel #3
0
        private object combineArray(TypName typ, object array1, object array2)
        {
            //0 byte, 1 int, 2 float, 3 double, 4 bool, 5 string, 6 var,7 cond
            switch (typ)
            {
            case TypName.Ref:
            case TypName.Byte: return(combineArray((byte[])array1, (byte[])array2));

            case TypName.Int: return(combineArray((int[])array1, (int[])array2));

            case TypName.Float: return(combineArray((float[])array1, (float[])array2));

            case TypName.Double: return(combineArray((double[])array1, (double[])array2));

            case TypName.Bool: return(combineArray((bool[])array1, (bool[])array2));

            case TypName.String: return(combineArray((string[])array1, (string[])array2));
            }
            return(null);
        }
Beispiel #4
0
        /// <summary>Removes all previously added attributes, enums and objects.</summary>
        public void Clear()
        {
            typesIndex = 0;
            types      = new Typ[256];
            initNativeTypes();

            attributesIndex     = 0;
            attributesTyp       = new TypName[256];
            attributesArray     = new bool[256];
            attributesName      = new string[256];
            attributesInitValue = new object[256];

            enumIndex = 0;
            enumValue = new int[512];
            enumNames = new string[512];

            objectsIndex = 0;
            objectNames  = new string[256];

            results = new Struct[256];
        }
Beispiel #5
0
        private object defaultTypValue(TypName typ, bool array)
        {
            //0 byte, 1 int, 2 float, 3 double, 4 bool, 5 string, 6 var,7 cond
            if (!array)
            {
                switch (typ)
                {
                case TypName.Ref:
                case TypName.Byte: return((byte)0);

                case TypName.Int: return((int)0);

                case TypName.Float: return((float)0);

                case TypName.Double: return((double)0);

                case TypName.Bool: return(false);
                }
            }
            return(null);
        }
Beispiel #6
0
        private object getValue(ref int index, int attrIndex)
        {
            TypName typ   = attributesTyp[attrIndex];
            var     array = attributesArray[attrIndex];

            object retValue = null;

            if (!array)
            {
                retValue = readNativeValue(typ, ref index);
            }
            else
            {
                //0 byte, 1 int, 2 float, 3 double, 4 bool, 5 string, 6 ref, 7 var,8 cond
                int size = testArraySize(typ, index);
                switch (typ)
                {
                case TypName.Ref:
                case TypName.Byte: retValue = new byte[size]; break;

                case TypName.Int: retValue = new int[size]; break;

                case TypName.Float: retValue = new float[size]; break;

                case TypName.Double: retValue = new double[size]; break;

                case TypName.Bool: retValue = new bool[size]; break;

                case TypName.String: retValue = new string[size]; break;
                }

                if (size == 0)
                {
                    index += 1;
                    return(retValue);
                }


                int arrayIndex = 0;
                int scope      = 1;
                while (scope > 0)
                {
                    index += 1;
                    switch (tokenList[index].value)
                    {
                    case "[": scope++; break;

                    case "]": scope--; break;

                    case ",": break;

                    default:
                        object value = readNativeValue(typ, ref index);
                        if (tokenList[index + 1].value == "to")
                        {
                            index += 2;
                            int v1 = Convert.ToInt32(value);
                            int v2 = Convert.ToInt32(readNativeValue(typ, ref index));
                            if (v1 < v2)
                            {
                                for (int i = v1; i <= v2; i++)
                                {
                                    addValueToArray(ref retValue, typ, i, arrayIndex++);
                                }
                            }
                            else
                            {
                                for (int i = v1; i >= v2; i--)
                                {
                                    addValueToArray(ref retValue, typ, i, arrayIndex++);
                                }
                            }
                        }
                        else
                        {
                            addValueToArray(ref retValue, typ, value, arrayIndex++);
                        }
                        break;
                    }
                }
            }
            return(retValue);
        }
Beispiel #7
0
        private object readNativeValue(TypName typ, ref int index)
        {
            double neg = 1;

            //0 byte, 1 int, 2 float, 3 double, 4 bool, 5 string, 6 var,7 cond
            if (typ == TypName.Byte || typ == TypName.Int || typ == TypName.Float || typ == TypName.Double)
            {
                bool enableRet = false;
                int  retValue  = 0;
                if (tokenList[index].value == "-")
                {
                    neg = -1;
                    index++;
                }
                else if (tokenList[index].value == "+")
                {
                    index++;
                }
                else if (tokenList[index].value == "&")
                {
                    index++;
                    int indx = compareNames(tokenList[index].value, objectNames, objectsIndex);
                    if (indx == -1)
                    {
                        throw new ParserException(tokenList[index], "Object \"" + tokenList[index].value + "\" is not defined");
                    }
                    retValue = indx; enableRet = true;
                }
                else if (tokenList[index].kind == TokenKind.Other)
                {
                    int indx = compareNames(tokenList[index].value, enumNames, enumIndex);
                    if (indx == -1)
                    {
                        throw new ParserException(tokenList[index], "Enum \"" + tokenList[index].value + "\" is not defined");
                    }
                    retValue = enumValue[indx]; enableRet = true;
                }
                if (enableRet)
                {
                    switch (typ)
                    {
                    case TypName.Byte: return((byte)retValue);

                    case TypName.Int: return((int)retValue);

                    case TypName.Float: return((float)retValue);

                    case TypName.Double: return((double)retValue);
                    }
                }
            }

            string value = tokenList[index].value;

            try
            {
                switch (typ)
                {
                case TypName.Byte: return((byte)(Convert.ToByte(value) * neg));

                case TypName.Int: return((int)(Convert.ToInt32(value) * neg));

                case TypName.Float: return((float)(Convert.ToSingle(value.Replace('.', ',').TrimEnd('f')) * neg));

                case TypName.Double: return((double)(Convert.ToDouble(value.Replace('.', ',').TrimEnd('d')) * neg));

                case TypName.Bool:
                    if (value == "0")
                    {
                        return(false);
                    }
                    else if (value == "1")
                    {
                        return(true);
                    }
                    return(Convert.ToBoolean(value));

                case TypName.Ref: int id = compareNames(value, objectNames);
                    if (id == -1)
                    {
                        throw new ParserException(tokenList[index], "Object \"" + tokenList[index].value + "\" is not defined");
                    }
                    return((byte)id);

                case TypName.String: return(value);

                default: return(null);
                }
            }
            catch (FormatException e)
            {
                throw new ParserException(tokenList[index], "Value \"" + tokenList[index].value + "\" is not a " + (TypName)typ);
            }
        }
Beispiel #8
0
 private object readNativeValue(TypName typ, int index)
 {
     return(readNativeValue(typ, ref index));
 }
Beispiel #9
0
 private void readValueToArray(ref object array, TypName typ, ref int pos, int index)
 {
     addValueToArray(ref array, typ, readNativeValue(typ, ref pos), index);
 }
Beispiel #10
0
        private void pharseAttributes(string fnname)
        {
            int index = searchTokenIndex(fnname);

            if (index == -1)
            {
                return;
            }
            index += 2;
            while (tokenList[index].value != "}")
            {
                //Console.ForegroundColor = ConsoleColor.Green;
                bool   array = false;
                string type;
                if (tokenList[index + 1].value != "=")
                {
                    type = tokenList[index++].value;
                    if (tokenList[index].value == "[")
                    {
                        array = true; index += 2;
                    }

                    TypName typ = TypName.Var; //0 byte, 1 int, 2 float, 3 double, 4 bool, 5 string, 6 var,7 cond
                    if (type[0] == 'b' && type[1] == 'y')
                    {
                        typ = TypName.Byte;
                    }
                    else if (type[0] == 'i')
                    {
                        typ = TypName.Int;
                    }
                    else if (type[0] == 'f')
                    {
                        typ = TypName.Float;
                    }
                    else if (type[0] == 'd')
                    {
                        typ = TypName.Double;
                    }
                    else if (type[0] == 'b' && type[1] == 'o')
                    {
                        typ = TypName.Bool;
                    }
                    else if (type[0] == 's')
                    {
                        typ = TypName.String;
                    }
                    else if (type[0] == 'r')
                    {
                        typ = TypName.Ref;
                    }
                    else if (type[0] == 'v')
                    {
                        typ = TypName.Var;
                    }

                    int i = 0;
                    do
                    {
                        if (i++ > 0)
                        {
                            index += 2;
                        }
                        string name  = tokenList[index].value;
                        object value = null;

                        attributesTyp[attributesIndex]   = typ;
                        attributesArray[attributesIndex] = array;
                        attributesName[attributesIndex]  = tokenList[index].value;
                        if (tokenList[index + 1].value == "=")
                        {
                            index += 2;
                            value  = getValue(ref index, attributesIndex);
                            attributesInitValue[attributesIndex] = value;
                        }
                        else
                        {
                            attributesInitValue[attributesIndex] = defaultTypValue(typ, array);
                        }
                        attributesIndex++;

                        //Console.WriteLine(type + "[" + array + "]->" + name + (value != null ? ("=" + value) : "") + ";");
                    } while (tokenList[index + 1].value == ",");
                }
                else if (tokenList[index + 1].value == "=")
                {
                    int attri = compareNames(tokenList[index].value, attributesName);
                    if (attri == -1)
                    {
                        throw new ParserException(tokenList[index], "Attribute \"" + tokenList[index].value + "\" is not defined");
                    }
                    index += 2;
                    attributesInitValue[attri] = getValue(ref index, attri);

                    //Console.ForegroundColor = ConsoleColor.Red;
                    //Console.WriteLine(attributesName[attri]+"="+attributesInitValue[attri]+";");
                }

                //Console.ForegroundColor = ConsoleColor.Blue;
                //Console.WriteLine(tokenList[index].value);
                index++;
            }
            //Console.WriteLine("attributesIndex " + attributesIndex);
        }