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); } } }
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); }
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); } }
/// <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()); }
/// <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); } }
/// <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; }
/// <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; }
/// <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; }
/// <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; }
/// <summary> /// (try expression catch [finally]) /// </summary> public static Object Try(Cons args, LSharp.Environment environment) { string v = "";//"//(try " + Printer.ConsToString(args) + ")" + NewLine + string.Format(@" try {{ {0} }} catch (Exception e) {{ {1} }}", Generate(args.First(), environment) ,(args.Second() as Symbol == Symbol.NULL) ? "throw" : Generate(args.Second(),environment)); if (args.Length() > 2) { v += string.Format(@" finally {{ {0} }} ", Generate(args.Third(),environment)); } return v; }