Exemplo n.º 1
0
 public RCSymbolScalar(RCSymbolScalar previous, object key)
 {
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     if (key.GetType() == typeof(RCSymbolScalar))
     {
         throw new Exception("Symbols may not contain other symbols.");
     }
     if (previous == null)
     {
         Previous = RCSymbolScalar.Empty;
     }
     else
     {
         Previous = previous;
     }
     if (key.GetType() == typeof(string))
     {
         string str = (string)key;
         if (str.Length > 0)
         {
             if (str[0] == '\'')
             {
                 if (str[str.Length - 1] == '\'')
                 {
                     key = str.Substring(1, str.Length - 2);
                 }
                 else
                 {
                     throw new Exception("Invalid symbol part: " + str);
                 }
             }
         }
     }
     if (key.GetType() == typeof(int))
     {
         key = (long)(int)key;
     }
     Key  = key;
     Type = RCVectorBase.EmptyOf(Key.GetType());
     if (Previous == RCSymbolScalar.Empty)
     {
         Length = 1;
         string part   = Type.IdShorthand(Key);
         string prefix = part.Length > 0 && part[0] == '#' ? "" : "#";
         _string = prefix + part + Type.Suffix;
     }
     else
     {
         Length  = previous.Length + 1;
         _string = previous.ToString() + "," +
                   Type.IdShorthand(Key) + Type.Suffix;
     }
     if (Previous.Key.Equals("*"))
     {
         _leadingStar = true;
     }
 }
Exemplo n.º 2
0
 public override void VisitScalar <T> (string name, Column <T> column, int row)
 {
     _row = new RCBlock(_row,
                        name,
                        ":",
                        RCVectorBase.FromArray(new RCArray <T> (column.Data[row])));
 }
Exemplo n.º 3
0
        public Type GetType(int index)
        {
            RCVectorBase vector = this.Get(index) as RCVectorBase;

            if (vector != null)
            {
                return(vector.GetElementType());
            }
            else
            {
                throw new Exception("GetType (index) assumes a vector");
            }
        }
Exemplo n.º 4
0
        public static void WriteScalarSymbol(RCArray <byte> result, RCSymbolScalar scalar)
        {
            // This is not going to be a triumph of efficiency.
            // I am considering rewriting RCSymbolScalar so that it stores all of its
            // data as an array of bytes.  Then the ToByte operation would just
            // return the internal representation.
            for (int i = 0; i < scalar.Length; ++i)
            {
                object part = scalar.Part(i);
                char   type = RCVectorBase.EmptyOf(part.GetType()).TypeCode;
                result.Write((byte)type);
                switch (type)
                {
                case 'l':
                    result.Write(BitConverter.GetBytes((long)part));
                    break;

                case 'd':
                    result.Write(BitConverter.GetBytes((double)part));
                    break;

                case 'm':
                    Binary.WriteScalarDecimal((decimal)part, result);
                    break;

                case 'b':
                    result.Write(BitConverter.GetBytes((bool)part));
                    break;

                case 's':
                    Binary.WriteScalarString((string)part, result);
                    break;

                default: throw new Exception("Unknown type:" + type + " found in symbol");
                }
            }
        }
Exemplo n.º 5
0
        /*
         * protected Type InferType(string[] name, string original)
         * {
         * //Let's try to figure it out!
         * RCValue target = null;
         *
         * //Try the block under construction.
         * if (_block != null)
         * {
         * target = _block.Get(name);
         * }
         *
         * //Try to find it higher up the stack.
         * if (target == null)
         * {
         * RCBlock[] parents = _blocks.ToArray();
         * //When you ToArray the stack items come out in the same order
         * //you would have taken them off the stack.
         * for (int i = 0; i < parents.Length; ++i)
         * {
         * //There will be null blocks on the stack.
         * if (parents[i] != null)
         * {
         * target = parents[i].Get(name);
         * if (target != null) break;
         * }
         * }
         * }
         *
         * if (target == null)
         * {
         * throw new Exception("Unable to infer type for reference " + original + ".");
         * }
         * else return target.Yields;
         * }
         */

        protected RCVectorBase MakeVector(RCArray <RCToken> vector)
        {
            RCVectorBase result = null;

            if (vector[0].Text[0] == '~')
            {
                switch (vector[0].Text[1])
                {
                case 'x': return(RCByte.Empty);

                case 'b': return(RCBoolean.Empty);

                case 'l': return(RCLong.Empty);

                case 'd': return(RCDouble.Empty);

                case 'm': return(RCDecimal.Empty);

                case 's': return(RCString.Empty);

                case 'y': return(RCSymbol.Empty);

                case 't': return(RCTime.Empty);

                case 'n': return(RCIncr.Empty);

                default: throw new Exception("Unrecognized type code: " + vector[0].Text[1]);
                }
            }
            if (vector[0].Type == RCTokenType.Symbol)
            {
                RCArray <RCSymbolScalar> list = new RCArray <RCSymbolScalar> (vector.Count);
                for (int i = 0; i < vector.Count; ++i)
                {
                    list.Write(vector[i].ParseSymbol(_lexer));
                }
                result = new RCSymbol(list);
            }
            else if (vector[0].Type == RCTokenType.String)
            {
                RCArray <string> list = new RCArray <string> (vector.Count);
                for (int i = 0; i < vector.Count; ++i)
                {
                    list.Write(vector[i].ParseString(_lexer));
                }
                result = new RCString(list);
            }
            else if (vector[0].Type == RCTokenType.Boolean)
            {
                RCArray <bool> list = new RCArray <bool> (vector.Count);
                for (int i = 0; i < vector.Count; ++i)
                {
                    list.Write(vector[i].ParseBoolean(_lexer));
                }
                result = new RCBoolean(list);
            }
            else if (vector[0].Type == RCTokenType.Incr)
            {
                RCArray <RCIncrScalar> list = new RCArray <RCIncrScalar> (vector.Count);
                for (int i = 0; i < vector.Count; ++i)
                {
                    list.Write(vector[i].ParseIncr(_lexer));
                }
                result = new RCIncr(list);
            }
            else if (vector[0].Type == RCTokenType.Literal)
            {
                char type = vector[0].Text[1];
                switch (type)
                {
                case 'x':
                    RCArray <byte> list = new RCArray <byte> (vector.Count);
                    for (int i = 0; i < vector.Count; ++i)
                    {
                        list.Write(vector[i].ParseByte(_lexer));
                    }
                    result = new RCByte(list);
                    break;

                default: throw new Exception("Unknown type specifier:" + type);
                }
            }
            else if (vector[0].Type == RCTokenType.Time)
            {
                RCArray <RCTimeScalar> list = new RCArray <RCTimeScalar> (vector.Count);
                for (int i = 0; i < vector.Count; ++i)
                {
                    list.Write(vector[i].ParseTime(_lexer));
                }
                result = new RCTime(list);
            }
            else if (vector[0].Type == RCTokenType.Number)
            {
                // have a look at the last character in the last token
                // if there is a type specifier there we will use it to
                // create the appropriate type of vector.
                RCToken last = vector[vector.Count - 1];
                char    type = last.Text[last.Text.Length - 1];
                if (type == 'l')
                {
                    RCArray <long> list = new RCArray <long> (vector.Count);
                    for (int i = 0; i < vector.Count; ++i)
                    {
                        list.Write(vector[i].ParseLong(_lexer));
                    }
                    result = new RCLong(list);
                }
                if (type == 'd')
                {
                    RCArray <double> list = new RCArray <double> (vector.Count);
                    for (int i = 0; i < vector.Count; ++i)
                    {
                        list.Write(vector[i].ParseDouble(_lexer));
                    }
                    result = new RCDouble(list);
                }
                else if (type == 'm')
                {
                    RCArray <decimal> list = new RCArray <decimal> (vector.Count);
                    for (int i = 0; i < vector.Count; ++i)
                    {
                        list.Write(vector[i].ParseDecimal(_lexer));
                    }
                    result = new RCDecimal(list);
                }
                else // default to double
                {
                    if (vector[0].Text.IndexOf('.') > -1 || vector[0].Text == "NaN")
                    {
                        RCArray <double> list = new RCArray <double> (vector.Count);
                        for (int i = 0; i < vector.Count; ++i)
                        {
                            list.Write(vector[i].ParseDouble(_lexer));
                        }
                        result = new RCDouble(list);
                    }
                    else
                    {
                        RCArray <long> list = new RCArray <long> (vector.Count);
                        for (int i = 0; i < vector.Count; ++i)
                        {
                            list.Write(vector[i].ParseLong(_lexer));
                        }
                        result = new RCLong(list);
                    }
                }
            }
            return(result);
        }