Esempio n. 1
0
        public static Object Try(Cons args, Environment environment)
        {
            try
            {
                return(Runtime.Eval(args.First(), environment));
            }
            catch (Exception e)
            {
                environment.AssignLocal(Symbol.IT, e);

                // If a catch form is specified then evaluate it
                if (args.Second() as Symbol == Symbol.NULL)
                {
                    throw;
                }
                return(Runtime.Eval(args.Second(), environment));
            }
            finally
            {
                // If a finally form was specified then evaluate it
                if (args.Length() > 2)
                {
                    Runtime.Eval(args.Third(), environment);
                }
            }
        }
Esempio n. 2
0
        public static Object DefClass(Cons args, Environment environment)
        {
            string className = args.First().ToString();

            Cons   superClasses = args.Cadr() as Cons;
            string superClass   = null;
            string interfaces   = null;

            if (superClasses != null)
            {
                superClass = superClasses.First().ToString();


                if (superClasses.Length() >= 2)
                {
                    StringBuilder b = new StringBuilder();
                    b.Append(superClasses.Second());

                    foreach (object item in (Cons)superClasses.Cddr())
                    {
                        b.Append(", " + item);
                    }
                    interfaces = b.ToString();
                }
            }

            return(ClassBuilder.CreateClass(className, superClass, interfaces));
        }
Esempio n. 3
0
        public static Object Assoc(Cons args, Environment environment)
        {
            /*
             *
             * (defun assoc (item lst)
             * (= m null)
             * (each c lst
             * (and
             * (== m null)
             * (== item (car c))
             * (= m c)))
             * m)
             *
             */
            object item = args.First();
            Cons   list = (Cons)args.Second();

            foreach (Cons c in list)
            {
                if (c.Car().Equals(item))
                {
                    return(c);
                }
            }
            return(null);
        }
Esempio n. 4
0
        public static Object Cond(Cons args, Environment environment)
        {
            Cons clauses = args;

            while (clauses.Length() > 0)
            {
                if (clauses.Length() == 1)
                {
                    // This is a default (else) clause, so just execute it
                    return(Runtime.Eval(clauses.First(), environment));
                }

                if (clauses.Length() >= 2)
                {
                    if (Conversions.ObjectToBoolean(Runtime.Eval(clauses.First(), environment)))
                    {
                        return(Runtime.Eval(clauses.Second(), environment));
                    }
                    else
                    {
                        clauses = (Cons)clauses.Cddr();
                    }
                }
            }
            return(null);
        }
Esempio n. 5
0
        public static object BackQuoteExpand(Object form, Environment environment)
        {
            if (!(form is Cons))
            {
                return(form);
            }

            Cons expression = (Cons)form;

            Cons result = null;

            foreach (object item in expression)
            {
                if (item is Cons)
                {
                    Cons   list = (Cons)item;
                    Symbol sym  = list.First() as Symbol;
                    if (sym == Symbol.BACKQUOTE)
                    {
                        result = new Cons(BackQuoteExpand(list.Second(), environment), result);
                    }
                    else if (sym == Symbol.UNQUOTE)
                    {
                        result = new Cons(Runtime.Eval(BackQuoteExpand(list.Second(), environment), environment), result);
                    }
                    else if (sym == Symbol.SPLICE)
                    {
                        Cons l = (Cons)Runtime.Eval(BackQuoteExpand(list.Second(), environment), environment);
                        foreach (object o in l)
                        {
                            result = new Cons(o, result);
                        }
                    }
                    else
                    {
                        result = new Cons(BackQuoteExpand(item, environment), result);
                    }
                }
                else
                {
                    result = new Cons(item, result);
                }
            }
            return(result.Reverse());
        }
Esempio n. 6
0
        public static Object Is(Cons args, Environment environment)
        {
            object obj = args.Second();

            string typeName = args.First().ToString();
            Type   type     = TypeCache.FindType(typeName);

            object result = (((Type)type).IsInstanceOfType(obj));

            return(result);
        }
Esempio n. 7
0
        public static Object Map(Cons args, Environment environment)
        {
            Cons temp = null;

            foreach (object o in (IEnumerable)args.Second())
            {
                temp = new Cons(
                    Runtime.Apply(args.First(), new Cons(o), environment),
                    temp);
            }
            return(temp.Reverse());
        }
Esempio n. 8
0
        public static Object Setq(Cons args, Environment environment)
        {
            object v = null;

            while (args != null)
            {
                Symbol s = (Symbol)args.First();
                v = Runtime.Eval(args.Second(), environment);
                environment.Assign(s, v);
                args = (Cons)args.Cddr();
            }

            return(v);
        }
Esempio n. 9
0
        /// <summary>
        /// Converts a list to a SortedList
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static SortedList ConsToSortedList(Object o)
        {
            SortedList sortedList = new SortedList();

            Object temp = o;

            while (temp != null)
            {
                Cons element = (Cons)((Cons)temp).First();
                sortedList.Add(element.First(), element.Second());
                temp = ((Cons)temp).Rest();
            }
            return(sortedList);
        }
Esempio n. 10
0
        public static Object Read(Cons args, Environment environment)
        {
            ReadTable readTable = (ReadTable)environment.GetValue(Symbol.FromName("*readtable*"));

            TextReader textReader = args.First() as TextReader;

            object eofValue = Reader.EOFVALUE;

            if (args.Length() > 1)
            {
                eofValue = args.Second();
            }

            return(Reader.Read(textReader, readTable, eofValue));
        }
Esempio n. 11
0
        public static Object Let(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            localEnvironment.AssignLocal((Symbol)args.First(), Runtime.Eval(args.Second(), environment));

            object result = null;

            foreach (object item in (Cons)args.Cddr())
            {
                result = Runtime.Eval(item, localEnvironment);
            }
            //return Runtime.Eval(args.Third(),localEnvironment);
            return(result);
        }
Esempio n. 12
0
        /// <summary>
        /// Converts a list to a Hashtable
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static Hashtable ConsToHashtable(Object o)
        {
            Hashtable hashtable = new Hashtable();

            Object temp = o;

            while (temp != null)
            {
                Cons element = (Cons)((Cons)temp).First();

                hashtable[element.First()] = element.Second();
                temp = ((Cons)temp).Rest();
            }
            return(hashtable);
        }
Esempio n. 13
0
        public static Object To(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            localEnvironment.AssignLocal((Symbol)args.First(), 0);
            int endStop = int.Parse(Runtime.Eval(args.Second(), localEnvironment).ToString());

            while ((int)localEnvironment.GetValue((Symbol)args.First()) < endStop)
            {
                foreach (object item in (Cons)args.Cddr())
                {
                    Runtime.Eval(item, localEnvironment);
                }
                localEnvironment.AssignLocal((Symbol)args.First(), ((int)Runtime.Eval(args.First(), localEnvironment)) + 1);
            }
            return(null);
        }
Esempio n. 14
0
        public static Object For(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            Runtime.Eval(args.First(), localEnvironment);
            object test;

            while ((Conversions.ObjectToBoolean(test = Runtime.Eval(args.Second(), localEnvironment))))
            {
                foreach (object item in (Cons)args.Cdddr())
                {
                    Runtime.Eval(item, localEnvironment);
                }
                Runtime.Eval(args.Third(), localEnvironment);
            }
            return(test);
        }
Esempio n. 15
0
 public static Object If(Cons args, Environment environment)
 {
     if (Conversions.ObjectToBoolean(Runtime.Eval(args.First(), environment)))
     {
         // Evaluate the then part
         return(Runtime.Eval(args.Second(), environment));
     }
     else
     if (args.Length() > 2)
     {
         // Evaluate the optional else part
         return(Runtime.Eval(args.Third(), environment));
     }
     else
     {
         return(null);
     }
 }
Esempio n. 16
0
        public static Object ForEach(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            Symbol variable = (Symbol)args.First();
            Object list     = Runtime.Eval(args.Second(), localEnvironment);

            foreach (object o in (System.Collections.IEnumerable)list)
            {
                localEnvironment.AssignLocal(variable, o);
                //Runtime.Eval(args.Third(),localEnvironment);
                foreach (object item in (Cons)args.Cddr())
                {
                    Runtime.Eval(item, localEnvironment);
                }
            }

            return(null);
        }
Esempio n. 17
0
        public static Object With(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            Cons bindings = (Cons)args.First();

            while ((bindings != null) && (bindings.Length() > 1))
            {
                localEnvironment.AssignLocal((Symbol)bindings.First(), Runtime.Eval(bindings.Second(), environment));
                bindings = (Cons)bindings.Cddr();
            }

            object result = null;

            foreach (object item in (Cons)args.Cdr())
            {
                result = Runtime.Eval(item, localEnvironment);
            }
            return(result);
        }
Esempio n. 18
0
        public static Object Nth(Cons args, Environment environment)
        {
            int    index = (int)args.First();
            object o     = args.Second();

            if (o is IEnumerable)
            {
                IEnumerator e = ((IEnumerable)o).GetEnumerator();
                for (int i = 0; i <= index; i++)
                {
                    if (!e.MoveNext())
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
                return(e.Current);
            }
            else
            {
                throw new LSharpException(string.Format("Nth: {0} is not IEnumerable", o));
            }
        }
Esempio n. 19
0
        /// <summary>
        /// (is type expression)
        /// Used to check whether the run-time type of an object is
        /// compatible with a given type.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Is(Cons args, Environment environment)
        {
            object obj = args.Second();

            TypeCache typeCache = TypeCache.Instance();
            string typeName = args.First().ToString();
            Type type = typeCache.FindType(typeName);

            object result =  (((Type)type).IsInstanceOfType (obj));

            return result;

        }
Esempio n. 20
0
        //converts args[0] to type of args[1]
        public static object Coerce(Cons args, Environment e)
        {
            object o = args.First();
            object t = args.Second();
            Type type;

            if (t is String)
                type =(Type) TypeOf(new Cons(t), e);
            else
                type = (Type)t;

            return Convert.ChangeType(o, type, System.Globalization.CultureInfo.InvariantCulture);
        }
Esempio n. 21
0
        /// <summary>
        /// (assoc item alist)
        /// return the first cons in alist whose car is equal to item,
        /// or nil if no such cons is found.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Assoc(Cons args, Environment environment)
        {
            object item = args.First();
            Cons list = (Cons) args.Second();

            foreach (Cons c in list)
            {
                if (c.Car().Equals (item))
                    return c;
            }
            return null;
            
        }
Esempio n. 22
0
		/// <summary>
		/// (compile expression filename)
		/// This compiles to "expression" to "filename"
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Compile(Cons args, Environment environment) 
		{
			return Compiler.Compile(args.First().ToString(), args.Second().ToString(), Compiler.OutputType.Exe);
		}
Esempio n. 23
0
		/// <summary>
		/// (to variable limit expression)
		/// Starting at 0, assigns variable to succesive integers upto and 
		/// including limit. Executes expression on each iteration. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object To(Cons args, Environment environment) 
		{
			Environment localEnvironment = new Environment(environment);
			localEnvironment.AssignLocal((Symbol) args.First(), 0);
			int endStop = int.Parse(Runtime.Eval(args.Second(),localEnvironment).ToString());
			while ((int)localEnvironment.GetValue((Symbol) args.First()) < endStop)
			{
				foreach (object item in (Cons)args.Cddr()) 
				{
					Runtime.Eval(item, localEnvironment);
				}
				localEnvironment.AssignLocal((Symbol) args.First(), ((int)Runtime.Eval(args.First(), localEnvironment)) + 1);
				
			}
			return null;
		}
Esempio n. 24
0
 /// <summary>
 /// (spawn expression) is like eval except that the expression
 /// is evaluated on a new thread. Returns immediately with
 /// the new thread object, but execution of expressions
 /// continues synchronously. Experimental.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Spawn(Cons args, Environment environment)
 {
     if (args.Length() == 1)
         return ThreadAdapter.Fork(args.First(), environment, System.Threading.ApartmentState.MTA);
     else if (args.Length() == 2)
         return ThreadAdapter.Fork(args.First(), environment, 
             (System.Threading.ApartmentState) Runtime.Eval(args.Second(), environment));
     else
         throw new LSharpException("Incorrect arguments given to spawn");
 }
Esempio n. 25
0
		/// <summary>
		/// (let symbol value expression*)
		/// Binds a new local variable symbol to value in a new local lexical environment, 
		/// before evaluating expressions. Similar to with, but often more convenient for 
		/// decalring a single local variable. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Let(Cons args, Environment environment) 
		{
			Environment localEnvironment = new Environment(environment);
			localEnvironment.AssignLocal((Symbol) args.First(), Runtime.Eval(args.Second(),environment));
			
			object result = null;
			foreach (object item in (Cons)args.Cddr()) 
			{
				result = Runtime.Eval(item, localEnvironment);
			}
			//return Runtime.Eval(args.Third(),localEnvironment);
			return result;
		}
Esempio n. 26
0
 /// <summary>
 /// (apply function list)
 /// Applies function to a list of arguments. function may be a built-in lsharp function,
 /// a closure defined by fn a macro defined by macro or the name of a method in the
 /// .NET framework.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Apply(Cons args, Environment environment)
 {
     return Runtime.Apply(args.First(), args.Second(),environment);
 }
Esempio n. 27
0
    /// <summary>
    /// (let symbol value expression*)
    /// </summary>
    public static Object Let(Cons args, LSharp.Environment environment)
    {
        string v = //"//(let " + Printer.ConsToString(args) + ")" + NewLine +
            "{"  + NewLine;
        LSharp.Environment localEnvironment = new LSharp.Environment(environment);
        v += GenerateAssignLocal((Symbol) args.First(), Generate(args.Second(),environment), localEnvironment);
        
        foreach (object item in (Cons)args.Cddr())
        {
            v += Generate(item, localEnvironment);
        }

        return v + "}" + NewLine;
    }
Esempio n. 28
0
    /// <summary>
    /// (= { symbol value}*)
    /// </summary>
    public static Object Setq(Cons args, LSharp.Environment environment)
    {
        string v = "";//"//(setq " + Printer.ConsToString(args) + ")" + NewLine;

        while (args != null)
        {
            Symbol s = (Symbol)args.First();
            Cons sec = args.Second() as Cons;
            if (sec != null)
            {
                Symbol ss = sec.First() as Symbol;
                if (ss == Symbol.FromName("fn"))
                {
                    Closure c =  Runtime.Eval(sec, environment) as Closure;
                    environment.Assign(s, c);
                    extracode += Closure( new Cons(s), environment);
                    args = (Cons)args.Cddr();
                    continue;
                }
                if (ss == Symbol.FromName("macro"))
                {
                    Macro m =  Runtime.Eval(sec, environment) as Macro;
                    environment.Assign(s, m);
                    args = (Cons)args.Cddr();
                    continue;
                }
            }
            v += GenerateAssign(s,Generate(args.Second(),environment), environment);
            args = (Cons)args.Cddr();
        }

        return v;
    }
Esempio n. 29
0
        public static Object The(Cons args, Environment environment)
        {
            Type o = TypeCache.FindType(args.First().ToString());

            return(Conversions.The(o, Runtime.Eval(args.Second(), environment)));
        }
Esempio n. 30
0
 /// <summary>
 /// (the type value)
 /// </summary>
 public static Object The(Cons args, LSharp.Environment environment)
 {
     string v = "";// "//(the " + Printer.ConsToString(args) + ")" + NewLine;
     Type o = TypeCache.FindType(args.First().ToString());
     v += Generate(args.Second(),environment);
     return v + string.Format("retval = LSharp.Conversions.The(typeof({0}), retval);", o) + NewLine;
 }
Esempio n. 31
0
    /// <summary>
    /// (to variable limit expression)
    /// </summary>
    public static Object To(Cons args, LSharp.Environment environment)
    {
        string v = //"//(to " + Printer.ConsToString(args) + ")" + NewLine +
            "{" + NewLine;
        LSharp.Environment localEnvironment = new LSharp.Environment(environment);

        v += GenerateAssignLocal(args.First() as Symbol, 0, localEnvironment);
        v += Generate(args.Second(),environment);
        string lbl = MakeUnique("endstop");
        v += string.Format(@"
int {1} = (int)retval;
while ({0} < {1})
{{
", localEnvironment.GetValue(args.First() as Symbol), lbl);

        foreach (object item in (Cons)args.Cddr())
        {
            v += Generate(item, localEnvironment);
        }

        v += localEnvironment.GetValue(args.First() as Symbol) + "++;";
        
        v += "}" + NewLine;
        v += string.Format(@"
retval = null;
");
        return v + "}" + NewLine;
    }
Esempio n. 32
0
    /// <summary>
    /// (call method object argument*)
    /// </summary>
    public static Object Call(Cons args, LSharp.Environment environment)
    {
        string v = //"//(call " + Printer.ConsToString(args) + ")" + NewLine +
            "{" + NewLine;

        ArrayList argtypes = new ArrayList();
        ArrayList argz = new ArrayList();

        if (args.Length() > 2)
        {
            foreach (object arg in (args.Cddr() as Cons))
            {
                if (Primitives.IsAtom(arg))
                {
                    argz.Add(Printer.WriteToString(arg));
                }
                else
                {
                    string argn = MakeUnique("arg");
                    string sv = Generate(arg, environment);
                    sv += string.Format(@"{0} {1} = ({0}) retval;
", typeof(object) , argn);

                    argz.Add(argn);

                    v += sv;
                }
                argtypes.Add(typeof(object));
            }
        }

        string typemethname = args.Car().ToString();

        string methname = typemethname;
        string typename = string.Empty;
        Type type = typeof(object);

        int i = methname.LastIndexOf(".");
        
        if (i >= 0)
        {
            methname = methname.Substring(i + 1);
            typename = typemethname.Substring(0, i);
            type = TypeCache.FindType(typename);
        }
        MethodInfo mi = null;
        
        mi = type.GetMethod(methname, BindingFlags.IgnoreCase
                            | BindingFlags.Public | BindingFlags.Instance, binder, argtypes.ToArray(typeof(Type)) as Type[], null);

        string objn = string.Empty;

        if (mi == null)
        {
            type = TypeCache.FindType(args.Second().ToString());
            mi = type.GetMethod(methname, BindingFlags.IgnoreCase
                                | BindingFlags.Public | BindingFlags.Static, binder, argtypes.ToArray(typeof(Type)) as Type[], null);

            if (mi == null)
            {
                // use reflection
                v += Generate(args.Second(), environment);
                v += string.Format(@"retval = retval.GetType().InvokeMember(""{0}"",
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, null, retval,
new object[]{{", methname) +
                    string.Join(", ", argz.ToArray(typeof(string)) as string[]) + @"});
}
";
                return v;
            }
            else
            {
                objn = type.ToString();
            }
        }
        else
        {
            objn = MakeUnique("obj");
            v += Generate(args.Second(), environment);
            v += string.Format(@"{0} {1} = ({0}) retval;
", type, objn);
        }

        v += "retval = " + (mi.ReturnType == typeof(void) ? @"null;
" : "") + objn + "." + mi.Name + "(" +
            string.Join(", ", argz.ToArray(typeof(string)) as string[]);
        
        return v + @");
}
";
    }
Esempio n. 33
0
		/// <summary>
		/// (if test then [else])
		/// The if special form corresponds to the if-then-else construct found in 
		/// most algebraic programming languages. First the form test is evauated, 
		/// if true then the form then is evaluated.Otherwise, optionally the form 
		/// else is evaluated. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object If(Cons args, Environment environment) 
		{
			if (Conversions.ObjectToBoolean(Runtime.Eval(args.First(),environment)))
				// Evaluate the then part
				return Runtime.Eval(args.Second(),environment);
			else
				if (args.Length() > 2)
				// Evaluate the optional else part
				return Runtime.Eval(args.Third(),environment);
			else
				return null;
		}
Esempio n. 34
0
    /// <summary>
    /// (if test then [else])
    /// </summary>
    public static Object If(Cons args, LSharp.Environment environment)
    {
        string v = //"//(if " + Printer.ConsToString(args) + ")" + NewLine +
            Generate(args.First(),environment) +
            string.Format(@"
if (LSharp.Conversions.ObjectToBoolean(retval))
{{  // Evaluate the then part
  {0}
}}
", Generate(args.Second(),environment));

        if (args.Length() > 2)
        {
            // Evaluate the optional else part
            v += string.Format(@"
else
{{
  {0}
}}
", Generate(args.Third(),environment));
        }

        return v;
    }
Esempio n. 35
0
		/// <summary>
		/// (= { symbol value}*)
		/// Setq (Set Quote) is the variable assignment operator. 
		/// Sets each variable symbol to value in the current environment. 
		/// The abbreviation = is more commonly used in L Sharp.
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Setq(Cons args, Environment environment) 
		{
			object v = null;
			while (args != null) 
			{
				Symbol s = (Symbol)args.First();
				v = Runtime.Eval(args.Second(),environment);
				environment.Assign(s,v);
				args = (Cons)args.Cddr();
			}

			return v;

		}
Esempio n. 36
0
    /// <summary>
    /// (each symbol IEnumerable expression)
    /// </summary>
    public static Object ForEach(Cons args, LSharp.Environment environment)
    {
        //string v = "//(each " + Printer.ConsToString(args) + ")" + NewLine;
        string v = "";
        LSharp.Environment localEnvironment = new LSharp.Environment(environment);
        Symbol variable = (Symbol) args.First();
        
        string vn = localEnvironment.AssignLocal(variable, MakeUnique(variable.Name)) as string;
        
        v += Generate(args.Second(),environment) +
            string.Format(@"
foreach (object {0} in (System.Collections.IEnumerable)retval)
{{", vn);
        
        foreach (object item in (Cons)args.Cddr())
        {
            v += Generate(item, localEnvironment);
        }

        v += "}" + NewLine;

        return v;
    }
Esempio n. 37
0
		/// <summary>
		/// (the type value)
		/// Returns value converted or cast to an object of the specified type. 
		/// Throws an exception is the cast is not achievable. 
		/// The allows type casting and type conversion. This is much more than 
		/// a wrapper for the System.Convert class, it has special meaning for 
		/// conversions to and from Lists and certain common .NET data structures.
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object The(Cons args, Environment environment) 
		{
			Type o = TypeCache.FindType(args.First().ToString());
			return Conversions.The (o, Runtime.Eval(args.Second(),environment));
		}
Esempio n. 38
0
    /// <summary>
    /// (for initialiser test iterator statement)
    /// </summary>
    public static Object For(Cons args, LSharp.Environment environment)
    {
        //string v = "//(for " + Printer.ConsToString(args) + ")" + NewLine + "{" + NewLine;
        string v = "{" + NewLine;
        LSharp.Environment localEnvironment = new LSharp.Environment(environment);
        v += Generate(args.First(),localEnvironment);
        v += Generate(args.Second(),localEnvironment);
        v += @"while ((Conversions.ObjectToBoolean(retval))
{
";
        foreach (object item in (Cons)args.Cdddr())
        {
            v += Generate(item, localEnvironment);
        }

        v += Generate(args.Third(),localEnvironment);

        v += Generate(args.Second(),localEnvironment);

        v += @"}
";
        return v + "}" + NewLine;
    }
Esempio n. 39
0
		/// <summary>
		/// (try expression catch [finally])
		/// The try special form corresponds to the try-catch-finally construct found 
		/// in C#. If catch is null then there is deemed to be no catch block 
		/// at all. If an exception occurs, the variable "it" is bound to the Exception 
		/// object in the local environment. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Try(Cons args, Environment environment) 
		{
			try 
			{
				return Runtime.Eval(args.First(),environment);
			} 
			catch (Exception e) 
			{
				environment.AssignLocal(Symbol.IT,e);
						
				// If a catch form is specified then evaluate it
				if (args.Second() == Symbol.NULL)
					throw;
				return Runtime.Eval(args.Second(),environment);
			}
			finally 
			{
				// If a finally form was specified then evaluate it
				if  (args.Length() > 2)
					Runtime.Eval(args.Third(),environment);
			}
		}
Esempio n. 40
0
		/// <summary>
		/// Creates a dynamic class
		/// (defclass "classname" "inheritsfrom" [defmethod]) 
		/// e.g. (defclass "class1" "Object" DefinedMethods*)
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object DefClass(Cons args, Environment environment) 
		{
			string className = args.First().ToString();
			string superClass = args.Second().ToString();
			
			DefinedMethod[] methods = new DefinedMethod[args.Length() - 2];
			for (int i = 2; i < args.Length(); i++) 
			{
				try {
					Symbol toFind = (Symbol) args.Nth(i);
					object foundMethod =  environment.GetValue(toFind);
					methods[i - 2] = (DefinedMethod) foundMethod;
				} catch (Exception e ) {
					Console.WriteLine("DEFCLASS ERROR: " + e.Message + " " + e.StackTrace);
				}
			}
			return ClassBuilder.CreateClass(className, superClass, "", methods);
		}
Esempio n. 41
0
 /// <summary>
 /// (nth n list) Returns the (n+1)th element of list.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Nth(Cons args, Environment environment)
 {
     int index = (int)args.First();
     object o = args.Second();
     if (o is IEnumerable)
     {
         IEnumerator e = ((IEnumerable) o).GetEnumerator();
         for (int i = 0; i <= index; i ++ )
         {
             e.MoveNext();
         }
         return e.Current;
     }
     throw new LSharpException(string.Format("Nth: {0} is not IEnumerable",o));
 }
Esempio n. 42
0
		/// <summary>
		/// (defmethod name "arg1 arg2" "(expression1)" "(expression 2)" [...])
		/// Created methods used for DefClass <br />
		/// </summary>
		/// <param name="args"></param>
		/// <param name="e"></param>
		/// <returns></returns>
		public static object DefMethod(Cons args, Environment e)
		{
			string name;
			string[] _args;
			string commands = "";
			name = args.First().ToString();
			_args = args.Second().ToString().Split(new string[] {" "}, StringSplitOptions.None);;
			for (int i = 2; i < args.Length(); i++)
				commands += args.Nth(i) + " ";
			commands = commands.Replace("\\", "\\\\");
			commands = commands.Replace("\"", "\\\"");
			//FIXME: 
			//code = code.Replace("\n", "\\n");
			DefinedMethod ret = new DefinedMethod(commands, name, _args);
			e.AssignLocal(Symbol.FromName(name), ret);
			Console.WriteLine("Assigned '" + ret.Name + "' as a DefinedMethod");
			return ret;
		}
Esempio n. 43
0
        /// <summary>
        /// (read TextReader [eof])
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Read(Cons args, Environment environment)
        {
            ReadTable readTable = (ReadTable)environment.GetValue(Symbol.FromName("*readtable*"));

            TextReader textReader = (TextReader)args.First();
            object eofValue = null;
            if (args.Length() > 1)
                eofValue = args.Second();
            
            return Reader.Read(textReader, readTable, eofValue);
        }
Esempio n. 44
0
		/// <summary>
		/// (for initialiser test iterator statement)
		/// The for special form corresponds to the for construct found in most algebraic 
		/// programming languages. The initialiser is executed. The statement is executed 
		/// while test is true. The iterator is executed at the end of each statement execution. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object For(Cons args, Environment environment) 
		{
			Environment localEnvironment = new Environment(environment);
			Runtime.Eval(args.First(),localEnvironment);
			object test;
			while ((Conversions.ObjectToBoolean(test = Runtime.Eval(args.Second(),localEnvironment)))) 
			{
				foreach (object item in (Cons)args.Cdddr()) 
				{
					Runtime.Eval(item, localEnvironment);
				}
				Runtime.Eval(args.Third(),localEnvironment);
			}
			return test;
		}
Esempio n. 45
0
 /// <summary>
 /// (handle-event target eventName handler)
 /// Sets up a new event handler for events named eventName on target. The
 /// handler is an LSharp closure with two arguments, the sender and the
 /// event arguments (defun fn (sender args) (prl "Event Handled")).
 /// Experimental.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object HandleEvent(Cons args, Environment environment)
 {
     return EventAdapter.AddEventHandler(args.First(), (string)args.Second(), (Closure)args.Third());
 }
Esempio n. 46
0
		/// <summary>
		/// (each symbol IEnumerable expression)
		/// Iterates over any object which impelements IEnumerablewith succesive 
		/// elements being assigned to a variable named symbol; exceutes expression 
		/// on each iteration. Cons (LSharp lists), as well as many .NET collections 
		/// are IEnumerable. Foreach is a synonym for each. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object ForEach(Cons args, Environment environment) 
		{
			Environment localEnvironment = new Environment(environment);

			Symbol variable = (Symbol) args.First();
			Object list = Runtime.Eval(args.Second(),localEnvironment);

			foreach (object o in (System.Collections.IEnumerable)list) 
			{
				localEnvironment.AssignLocal(variable, o);
				//Runtime.Eval(args.Third(),localEnvironment);
				foreach (object item in (Cons)args.Cddr()) 
				{
					Runtime.Eval(item, localEnvironment);
				}
			}

			return null;
		}
Esempio n. 47
0
        /// <summary>
        /// (map function list) Maps function to each element in list return a new
        /// list of return values.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Map(Cons args, Environment environment)
        {
            if (args.Second() == null)
                return null;

            Cons temp = null;
            foreach (object o in (IEnumerable)args.Second())
            {
                temp = new Cons(
                    Runtime.Apply( args.First(),new Cons(o),environment),
                    temp);
            }
            return temp.Reverse();
        }
Esempio n. 48
0
 public static Object Apply(Cons args, Environment environment)
 {
     return(Runtime.Apply(args.First(), args.Second(), environment));
 }
Esempio n. 49
0
        /// <summary>
        /// (member item list)
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Member(Cons args, Environment environment)
        {
            object value = args.First();
            object list = args.Second();

            // TODO potential speed ups if list is IList or IDictionary

            foreach (object o in (IEnumerable)list)
            {
                if (Primitives.Eql(o,value))
                    return o;
            }

            return null;
        }
Esempio n. 50
0
    /// <summary>
    /// (when test expression*)
    /// </summary>
    public static Object When(Cons args, LSharp.Environment environment)
    {
        string v = //"//(when " + Printer.ConsToString(args) + ")" + NewLine +
            Generate(args.First(),environment) +
            string.Format(@"
if (LSharp.Conversions.ObjectToBoolean(retval))
{{  // Evaluate the then part
  {0}
}}
", Generate(args.Second(),environment));
        return v;
    }