xronos /System.Xronos/Language/RT.cs

Language C# Lines 395
MD5 Hash e89c1eef69eab9ac5698d8503e17cb5c Estimated Cost $8,006 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* ****************************************************************************
 * 
 * Copyright (c) 2008 Stefan Rusek and Benjamin Pollack
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xronos.Language.Threading;
using Microsoft.Scripting.Actions;
using System.Xronos.Scripting.Binders;
using Microsoft.Runtime.CompilerServices;
using Microsoft.Scripting.Math;

namespace System.Xronos.Language
{
    public static class RT
    {
        public static readonly object[] EmptyArray = new object[0];
        public static readonly object True = true;
        public static readonly object False = false;

        public static readonly Symbol Clojure = Symbol.Create("clojure");
        public static readonly Symbol Xronos = Symbol.Create("xronos");

        public static readonly Keyword Tag = Keyword.Intern(null, "tag");
        public static readonly Keyword Filename = Keyword.Intern(null, "filename");
        public static readonly Keyword Line = Keyword.Intern(null, "line");
        public static readonly Keyword LineEnd = Keyword.Intern(null, "lineend");
        public static readonly Keyword Char = Keyword.Intern(null, "char");
        public static readonly Keyword CharEnd = Keyword.Intern(null, "charend");

        public static readonly object[] DefaultImports = new object[]{
            Symbol.Create("Boolean"), typeof(Boolean),
            Symbol.Create("Byte"), typeof(Byte),
            Symbol.Create("Character"), typeof(Char),
            Symbol.Create("Type"), typeof(Type),
            Symbol.Create("Class"), typeof(Type),
            Symbol.Create("Compiler"), typeof(Compiler),
            Symbol.Create("Double"), typeof(Double),
            Symbol.Create("Enum"), typeof(Enum),
            Symbol.Create("Float"), typeof(Single),
            Symbol.Create("Integer"), typeof(int),
            Symbol.Create("Long"), typeof(long),
            Symbol.Create("Math"), typeof(Math),
            Symbol.Create("Object"), typeof(Object),
            Symbol.Create("Short"), typeof(short),
            Symbol.Create("String"), typeof(String),
            Symbol.Create("StringBuilder"), typeof(StringBuilder),
            Symbol.Create("Thread"), typeof(System.Threading.Thread),
            Symbol.Create("Throwable"), typeof(Exception),
            Symbol.Create("Void"), typeof(void),
            Symbol.Create("Cloneable"), typeof(ICloneable),
            Symbol.Create("Comparable"), typeof(IComparable),
            Symbol.Create("Iterable"), typeof(IEnumerable),
            Symbol.Create("ArithmeticException"), typeof(ArithmeticException),
            Symbol.Create("ArrayIndexOutOfBoundsException"), typeof(IndexOutOfRangeException),
            Symbol.Create("ClassCastException"), typeof(InvalidCastException),
            Symbol.Create("ClassNotFoundException"), typeof(TypeLoadException),
            Symbol.Create("Exception"), typeof(Exception),
            Symbol.Create("IndexOutOfBoundsException"), typeof(IndexOutOfRangeException),
            Symbol.Create("InterruptedException"), typeof(System.Threading.ThreadAbortException),
            Symbol.Create("NullPointerException"), typeof(NullReferenceException),
            Symbol.Create("NumberFormatException"), typeof(FormatException),
            Symbol.Create("RuntimeException"), typeof(Exception),
            Symbol.Create("SecurityException"), typeof(System.Security.SecurityException),
            Symbol.Create("StringIndexOutOfBoundsException"), typeof(IndexOutOfRangeException),
            Symbol.Create("TypeNotPresentException"), typeof(TypeLoadException),
            Symbol.Create("UnsupportedOperationException"), typeof(NotSupportedException),
            Symbol.Create("IllegalAccessError"), typeof(InvalidOperationException),
            Symbol.Create("OutOfMemoryError"), typeof(OutOfMemoryException),
            Symbol.Create("StackOverflowError"), typeof(StackOverflowException),
            Symbol.Create("UnknownError"), typeof(Exception),
            Symbol.Create("VerifyError"), typeof(InvalidProgramException)};

        public static ISequence Cons(object first, object orest)
        {
            ISequence rest = seq(orest);
            if (rest != null)
                return rest.cons(first);
            return (ISequence)PersistentList.Empty.cons(first);
        }

        public static object NewAtom(object value)
        {
            return new AtomicReference<object>(value);
        }

        public static ISequence Concat(ISequence seqofseqs)
        {
            var result = EnumeratorSequence.Create(InternalConcat(seq(seqofseqs)));
            if (result == null)
                return null;
            return result;
        }

        static IEnumerable InternalConcat(ISequence seq)
        {
            for (; seq != null; seq = seq.rest())
            {
                var inner = RT.seq(seq.first());
                if (inner != null)
                    for (; inner != null; inner = inner.rest())
                        yield return inner.first();
            }
        }

        internal static IPersistentList List(params object[] rest)
        {
            return ToList(rest);
        }

        internal static IPersistentList ToList(object[] rest)
        {
            return PersistentList.Create(rest);
        }

        public static IPersistentList ToList(ISequence seq)
        {
            return PersistentList.Create(RT.seq(seq));
        }

        public static IPersistentVector ToVector(ISequence seq)
        {
            return PersistentVector.Create(RT.seq(seq));
        }

        public static IPersistentMap ToMap(ISequence seq)
        {
            return PersistentArrayMap.Create(RT.seq(seq));
        }

        public static string Str(object oseq)
        {
            var sb = new StringBuilder();
            for (var seq = RT.seq(oseq); seq != null; seq = seq.rest())
                sb.Append(seq.first());
            return sb.ToString();
        }

        internal static IPersistentVector ToVector(object[] rest)
        {
            return PersistentVector.Create(rest);
        }

        internal static IPersistentList ListStar(IList list)
        {
            return PersistentList.Create(list);
        }

        public static int Count(object obj)
        {
            var coll = obj as ICollection;
            if (coll != null)
                return coll.Count;

            return CountSequence(obj);
        }

        internal static int CountSequence(object obj)
        {
            var sequence = RT.seq(obj);
            int c = 0;
            while (sequence != null)
            {
                c++; sequence = sequence.rest();
            }
            return c;
        }

        static CallSite<Func<CallSite, object, object, object>> getCallSite = CallSite<Func<CallSite, object, object, object>>.Create(BinderFactory.Call(null, "get_Item", 1));
        static object InternalGet(object hash, object key, Func<object> notFound)
        {
            try
            {
                return getCallSite.Target.Invoke(getCallSite, hash, key);
            }
            catch (KeyNotFoundException)
            {
                return notFound();
            }
            catch (IndexOutOfRangeException)
            {
                return notFound();
            }
        }

        public static object Get(object hash, object key)
        {
            var map = hash as IAssociative;
            if (map != null)
                return map.valAt(key);

            return InternalGet(hash, key, () => null);
        }

        public static object Get(object hash, object key, object notFound)
        {
            var map = hash as IAssociative;
            if (map != null)
                return map.valAt(key, notFound);

            return InternalGet(hash, key, () => notFound);
        }

        public static object first(object o)
        {
            var s = seq(o);
            if (s == null)
                return null;
            return s.first();
        }

        public static ISequence rest(object o)
        {
            var s = seq(o);
            if (s == null)
                return null;
            return s.rest();
        }

        public static ISequence seq(object o)
        {
            if (o == null)
                return null;

            var gen = o as ISequential;
            if (gen != null)
                return gen.seq();

            var result = o as ISequence;
            if (result != null)
                return result;

            var enu = o as IEnumerable;
            if (enu != null)
                return EnumeratorSequence.Create(enu);

            return null;
        }

        public static object reduce(object fn, object coll)
        {
            var reduce = coll as IReduce;

            if (reduce != null)
                return reduce.reduce(fn);

            var seq = RT.seq(coll);
            if (seq == null)
                return FunctionHelper.Invoke<object>(fn);

            object result = seq.first();
            seq = seq.rest();
            do
            {
                result = FunctionHelper.Invoke<object>(fn, result, seq.first());
                seq = seq.rest();
            } while (seq != null);

            return result;
        }

        public static object reduce(object fn, object result, object coll)
        {
            var reduce = coll as IReduce;

            if (reduce != null)
                return reduce.reduce(fn, result);

            var seq = RT.seq(coll);
            while (seq != null)
            {
                result = FunctionHelper.Invoke<object>(fn, result, seq.first());
                seq = seq.rest();
            }

            return result;
        }

        public static IPersistentMap meta(object o)
        {
            var xo = o as XronosObject;
            if (xo != null)
                return xo.meta();
            return null;
        }

        public static object withMeta(object o, object meta)
        {
            var xo = o as XronosObject;
            var im = meta as IPersistentMap;

            if (xo != null && im != null)
                return xo.withMeta(im);

            return o;
        }

        internal static int CombineHash(int a, int b)
        {
            return a ^ b;
        }

        internal static IPersistentMap Map(params object[] args)
        {
            if (args.Length == 2)
                return PersistentArrayMap.Create(args);
            return PersistentHashMap.Create(args);
        }

        public static object NormalizeNumber(object o)
        {
            var s = Convert.ToDouble(o).ToString();

            long l;
            if (!long.TryParse(s, out l))
            {
                var d = double.Parse(s);
                if ((float)d == d)
                    return (float)d;
            }
            if (short.MinValue <= l && l <= short.MaxValue)
                return (short)l;
            if (int.MinValue <= l && l <= int.MaxValue)
                return (int)l;

            return l;
        }

        public static byte ToByte(object o)
        {
            return Convert.ToByte(o);
        }

        public static short ToShort(object o)
        {
            return Convert.ToInt16(o);
        }

        public static int ToInt(object o)
        {
            return Convert.ToInt32(o);
        }

        public static long ToLong(object o)
        {
            return Convert.ToInt64(o);
        }

        public static float ToFloat(object o)
        {
            return Convert.ToSingle(o);
        }

        public static double ToDouble(object o)
        {
            return Convert.ToDouble(o);
        }

        public static char ToChar(object o)
        {
            return Convert.ToChar(o);
        }

        public static bool ToBoolean(object o)
        {
            if (o == null)
                return false;
            bool? r = o as bool?;
            return !r.HasValue || r.Value != false;
        }
    }
}
Back to Top