Beispiel #1
0
 public static Obj CreateRecord(ushort[] labels, Obj[] values)
 {
     Obj[] labelObjs = new Obj[labels.Length];
     for (int i = 0; i < labels.Length; i++)
     {
         labelObjs[i] = SymbObj.Get(labels[i]);
     }
     return(CreateMap(labelObjs, values));
 }
Beispiel #2
0
 public static Obj FileAppend_P(Obj fname, Obj data, object env)
 {
     try {
         IO.AppendAllBytes(fname.GetString(), data.GetByteArray());
         return(SymbObj.Get(SymbObj.TrueSymbId));
     }
     catch (Exception) {
         return(SymbObj.Get(SymbObj.FalseSymbId));
     }
 }
Beispiel #3
0
        //////////////////////////////////////////////////////////////////////////////

        public override int InternalOrder(Obj other)
        {
            Debug.Assert(GetSize() == other.GetSize());

            Obj[]       col, otherCol;
            int         size     = GetSize();
            NeBinRelObj otherRel = (NeBinRelObj)other;

            if (other is RecordObj)
            {
                RecordObj otherRecord = (RecordObj)other;

                ushort[] otherFieldIds = otherRecord.fieldIds;
                if (fieldIds != otherFieldIds)
                {
                    for (int i = 0; i < size; i++)
                    {
                        int res = SymbObj.CompSymbs(fieldIds[i], otherFieldIds[i]);
                        if (res != 0)
                        {
                            return(res);
                        }
                    }
                }
            }
            else
            {
                BuildCol1();

                col      = col1;
                otherCol = otherRel.Col1();
                for (int i = 0; i < size; i++)
                {
                    int ord = col[i].QuickOrder(otherCol[i]);
                    if (ord != 0)
                    {
                        return(ord);
                    }
                }
            }

            col      = col2;
            otherCol = otherRel.Col2();
            for (int i = 0; i < size; i++)
            {
                int ord = col[i].QuickOrder(otherCol[i]);
                if (ord != 0)
                {
                    return(ord);
                }
            }

            return(0);
        }
Beispiel #4
0
 public static Obj FileRead_P(Obj fname, object env)
 {
     try {
         byte[] content  = IO.ReadAllBytes(fname.GetString());
         Obj    bytesObj = Builder.CreateSeq(content);
         return(Builder.CreateTaggedObj(SymbObj.JustSymbId, bytesObj));
     }
     catch (Exception) {
         return(SymbObj.Get(SymbObj.NothingSymbId));
     }
 }
Beispiel #5
0
 public static Obj Save_P(Obj fname, RelAutoBase automaton, RelAutoUpdaterBase updater, object env)
 {
     try {
         DataWriter writer = IO.FileDataWriter(fname.GetString());
         automaton.WriteState(writer);
         return(SymbObj.Get(true));
     }
     catch (Exception e) {
         updater.lastException = e;
         return(SymbObj.Get(false));
     }
 }
Beispiel #6
0
        public static Obj GetChar_P(object env)
        {
            int ch = IO.StdInRead(-1, -1);

            if (ch != -1)
            {
                return(Builder.CreateTaggedObj(SymbObj.JustSymbId, IntObj.Get(ch)));
            }
            else
            {
                return(SymbObj.Get(SymbObj.NothingSymbId));
            }
        }
Beispiel #7
0
        //////////////////////////////////////////////////////////////////////////////

        public static Obj CreateSeq(bool[] vals, int len)
        {
            if (len == 0)
            {
                return(EmptySeqObj.singleton);
            }

            Obj[] objs = new Obj[len];
            for (int i = 0; i < len; i++)
            {
                objs[i] = SymbObj.Get(vals[i]);
            }
            return(ArrayObjs.Create(objs));
        }
Beispiel #8
0
 private void BuildCol1()
 {
     if (col1 == null)
     {
         int len = fieldIds.Length;
         col1       = new Obj[len];
         hashcodes1 = new uint[len];
         for (int i = 0; i < len; i++)
         {
             Obj symbObj = SymbObj.Get(fieldIds[i]);
             col1[i]       = symbObj;
             hashcodes1[i] = symbObj.Hashcode();
         }
     }
 }
Beispiel #9
0
        private RecordObj(ushort[] fieldIds, Obj[] values)
        {
            Debug.Assert(fieldIds.Length > 0);
            for (int i = 1; i < fieldIds.Length; i++)
            {
                Debug.Assert(SymbObj.CompSymbs(fieldIds[i - 1], fieldIds[i]) == 1);
            }

            data      = BinRelObjData((uint)fieldIds.Length);
            extraData = NeBinRelObjExtraData();

            this.fieldIds = fieldIds;
            col2          = values;
            isMap         = true;
        }
Beispiel #10
0
 public override uint Hashcode()
 {
     if (hcode == Hashing.NULL_HASHCODE)
     {
         long hcode = 0;
         for (int i = 0; i < fieldIds.Length; i++)
         {
             hcode += Hashing.Hashcode(SymbObj.Get(fieldIds[i]).Hashcode(), col2[i].Hashcode());
         }
         hcode = Hashing.Hashcode64(hcode);
         if (hcode == Hashing.NULL_HASHCODE)
         {
             hcode++;
         }
     }
     return(hcode);
 }
Beispiel #11
0
        public ushort TryReadingLabel()
        {
            ConsumeWhiteSpace();

            if (!NextIsAlphaNum())
            {
                return(SymbObj.InvalidSymbId);
            }

            buffer[0] = (byte)Peek();
            for (int i = 1; i < BUFFER_SIZE; i++)
            {
                int ch = Peek(i);

                if (IsAlphaNum(ch))
                {
                    buffer[i] = (byte)ch;
                }
                else if (ch == '_')
                {
                    buffer[i++] = (byte)ch;
                    ch          = Peek(i);
                    if (IsAlphaNum(ch))
                    {
                        buffer[i] = (byte)ch;
                    }
                    else
                    {
                        throw Fail();
                    }
                }
                else if (ch == ':')
                {
                    Skip(i + 1);
                    return(SymbObj.BytesToIdx(buffer, i));
                }
                else
                {
                    return(SymbObj.InvalidSymbId);
                }
            }

            // The label was too long, we give up
            throw Fail();
        }
Beispiel #12
0
        ////////////////////////////////////////////////////////////////////////////////

        // The opening parenthesis and the first label including
        // its trailing colon must have already been consumed
        Obj ParseRec(ushort firstLabelId)
        {
            ushort[] labels = new ushort[8];
            Obj[]    values = new Obj[8];

            labels[0] = firstLabelId;
            values[0] = ParseObj();

            int i = 1;

            while (TryConsumingComma())
            {
                if (i >= labels.Length)
                {
                    labels = Array.Extend(labels, 2 * labels.Length);
                    values = Array.Extend(values, 2 * values.Length);
                }
                ushort labelId = TryReadingLabel();
                if (labelId == SymbObj.InvalidSymbId)
                {
                    throw Fail();
                }
                //## BAD BAD BAD: WITH A LARGE RECORD...
                for (int j = 0; j < i; j++)
                {
                    if (labels[j] == labelId)
                    {
                        throw Fail();
                    }
                }
                labels[i]   = labelId;
                values[i++] = ParseObj();
            }
            ConsumeClosePar();

            Obj[] labelObjs = new Obj[i];
            for (int j = 0; j < i; j++)
            {
                labelObjs[j] = SymbObj.Get(labels[j]);
            }

            //## IT WOULD BE BETTER TO CREATE A RecordObj, BUT THE LABELS WOULD NEED TO BE SORTED FIRST
            return(Builder.CreateMap(labelObjs, values, i));
        }
Beispiel #13
0
 public SymbObj GetTag()
 {
     return(SymbObj.Get(GetTagId()));
 }
Beispiel #14
0
 public void SymbObj(SymbObj obj)
 {
     writer.Write(obj.stringRepr);
 }
Beispiel #15
0
 public void SymbObj(SymbObj obj)
 {
     ConsumeSize(obj, obj.stringRepr.Length);
 }
Beispiel #16
0
        ////////////////////////////////////////////////////////////////////////////////

        Obj ParseSymbOrTaggedObj()
        {
            ushort symbId = ReadSymbol();

            if (!TryConsumingOpenPar())
            {
                return(SymbObj.Get(symbId));
            }

            TokenType type = PeekType();

            Obj firstValue = null;

            if (type == TokenType.Int)
            {
                long value = ReadLong();
                if (TryConsumingClosePar())
                {
                    return(Builder.CreateTaggedIntObj(symbId, value));
                }
                // Here we've consumed the opening parenthesis and the integer
                // Since the opening parenthesis was not follow by a label,
                // we're dealing with a sequence, possibly a sequence of integers
                //## OPTIMIZE FOR SEQUENCES OF INTEGERS
                firstValue = IntObj.Get(value);
            }
            else if (type == TokenType.Symbol)
            {
                ushort labelId = TryReadingLabel();
                if (labelId != SymbObj.InvalidSymbId)
                {
                    return(CreateTaggedObj(symbId, ParseRec(labelId)));
                }
                firstValue = ParseObj();
            }
            else
            {
                firstValue = ParseObj();
            }

            if (TryConsumingClosePar())
            {
                return(CreateTaggedObj(symbId, firstValue));
            }

            Obj[] elts = new Obj[16];
            elts[0] = firstValue;

            int i = 1;

            while (TryConsumingComma())
            {
                if (i >= elts.Length)
                {
                    elts = Array.Extend(elts, 2 * elts.Length);
                }
                elts[i++] = ParseObj();
            }
            ConsumeClosePar();

            return(CreateTaggedObj(symbId, Builder.CreateSeq(elts, i)));
        }