/System.Xronos/Language/RT.cs
C# | 394 lines | 314 code | 57 blank | 23 comment | 61 complexity | e89c1eef69eab9ac5698d8503e17cb5c MD5 | raw file
- /* ****************************************************************************
- *
- * 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;
- }
- }
- }