public static object ReadString(string expression, Environment environment) { ReadTable readTable = (ReadTable)environment.GetValue(Symbol.FromName("*readtable*")); object input = Reader.Read(new StringReader(expression), readTable); return(input); }
public static Object ReadString(Cons args, Environment environment) { ReadTable readTable = (ReadTable)environment.GetValue(Symbol.FromName("*readtable*")); TextReader textReader = new StringReader((string)args.First()); return(Reader.Read(textReader, readTable)); }
public object GetValue(Symbol symbol) { object o = hashtable[symbol]; if ((o == null) && (previousEnvironment != null)) { o = previousEnvironment.GetValue(symbol); } return(o); }
public object GetValue(Symbol symbol) { if (hashtable.ContainsKey(symbol)) { return(hashtable[symbol]); } if (previousEnvironment != null) { return(previousEnvironment.GetValue(symbol)); } throw new LSharpException("Reference to undefined identifier: " + symbol.Name, this); }
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)); }
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); }
/// <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); }
/// <summary> /// Evaluates an expression in a given lexical environment /// </summary> /// <param name="expression"></param> /// <param name="environment"></param> /// <returns></returns> public static Object Eval(Object expression, Environment environment) { profiler.TraceCall(expression); if (expression == null) return profiler.TraceReturn(null); // The expression is either an atom or a list if (Primitives.IsAtom(expression)) { // Number if (expression.GetType() == typeof(Double)) return profiler.TraceReturn(expression); if (expression.GetType() == typeof(int)) return profiler.TraceReturn(expression); // Character if (expression.GetType() == typeof(char)) return profiler.TraceReturn(expression); // String if (expression.GetType() == typeof(string)) return profiler.TraceReturn(expression); if (((Symbol)expression) == Symbol.TRUE) return profiler.TraceReturn(true); if (((Symbol)expression) == Symbol.FALSE) return profiler.TraceReturn(false); if (((Symbol)expression) == Symbol.NULL) return profiler.TraceReturn(null); // If the symbol is bound to a value in this lexical environment if (environment.Contains((Symbol)expression)) // Then it's a variable so return it's value return profiler.TraceReturn(environment.GetValue((Symbol)expression)); else { // Otherwise symbols evaluate to themselves return profiler.TraceReturn(expression); } } else { // The expression must be a list Cons cons = (Cons)expression; // Lists are assumed to be of the form (function arguments) object function = null; // However, we need to check to see if the function is actually a cons // that evaluates to a function first, as in: // (= foo (fn () "bar")) // (= dispatch (new system.collections.hashtable)) // (set_item dispatch "foo" foo) // ((item dispatch "foo")) if (cons.First() is Cons) { // if the first arg is a Cons, try to evaluate it to a function function = Eval(cons.First(), environment); } else if (cons.First() is Symbol) { // See if there is a binding to a function, clsoure, macro or special form // in this lexical environment function = environment.GetValue((Symbol)cons.First()); if (function == null) function = cons.First(); } else if (cons.First() == null) { string msg = "Expected a function as the first argument in a list but received null"; throw new System.ArgumentNullException(msg); } else { string msg = "Expected a function as the first argument in a list but received type '"; msg += cons.First().GetType().ToString() + "'"; throw new System.ArgumentException(msg); } // If it's a special form if (function.GetType() == typeof(SpecialForm)) { return profiler.TraceReturn(((SpecialForm)function)((Cons)cons.Cdr(), environment)); } // If its a macro application if (function.GetType() == typeof(Macro)) { object expansion = ((Macro)function).Expand((Cons)cons.Cdr()); return profiler.TraceReturn(Runtime.Eval(expansion, environment)); } // It must be a function, closure or method invocation, // so call apply Object arguments = EvalList((Cons)cons.Cdr(), environment); return profiler.TraceReturn(Runtime.Apply(function, arguments, environment)); } }
public static object EvalString (string expression, Environment environment) { ReadTable readTable = (ReadTable) environment.GetValue(Symbol.FromName("*readtable*")); object input = Reader.Read(new StringReader(expression), readTable); object output = Runtime.Eval(input, environment); return output; }
/// <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; }
/// <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); }
/// <summary> /// Returns the value that symbol is bound to in the /// given environment /// </summary> public static object VarRef(Symbol symbol, Environment environment) { object value = environment.GetValue(symbol); return value; }
public static object MacroExpand(object expression, bool once, Environment environment) { if (expression is Pair) { Pair args = (Pair)expression; if (args.First() is Symbol) { object value = environment.GetValue((Symbol)args.First()); if (value is Macro) { // If its a Macro then invoke it to get the expansion object expansion = ((Macro)value).Call(Runtime.AsArray(((Pair)args).Rest())); // The expansion itself may be a call to a macro - expand that? if (!once) expansion = MacroExpand(expansion, false, environment); return expansion; } } } return expression; }
public static Expression Compile1(Object s, Environment environment) { // Literals are constants if (Runtime.IsLiteral(s)) return Expression.Constant(s); // Special Syntax gets expanded and compiled if (Runtime.IsSsyntax(s)) return Compile1(Runtime.ExpandSSyntax(s),environment); // Symbols are variable references if (s is Symbol) return CompileVarRef((Symbol)s); // Special Syntax gets expanded and compiled if (Runtime.IsSsyntax(Runtime.Car(s))) { object expansion = Runtime.ExpandSSyntax(((Pair)s).First()); return Compile1(new Pair(expansion, (Pair)s),environment); } if (s is Pair) { object f = ((Pair)s).First(); // Special Forms if (f == Symbol.FromName("if")) return Compiler.CompileIf(((Pair)s).Rest(), environment); if (f == Symbol.FromName("quote")) return Compiler.CompileQuote(((Pair)s).Rest(), environment); if (f == Symbol.FromName("call-clr")) return Compiler.CompileCall((Pair)((Pair)s).Rest(), environment); if (f == Symbol.FromName("call-instance")) return Compiler.CompileInstanceCall((Pair)((Pair)s).Rest(), environment); if (f == Symbol.FromName("fn")) return Compiler.CompileFn((Pair)((Pair)s).Rest(), environment); if (f == Symbol.FromName("macro")) return Compiler.CompileMac((Pair)((Pair)s).Rest(), environment); if (f == Symbol.FromName("quasiquote")) return Compiler.CompileQuasiQuote((Pair)((Pair)s).Rest(), environment); if ((f is Symbol) && (environment.Contains((Symbol)f))) { object value = environment.GetValue((Symbol)f); // Macros get invoked at compile time, then their results are themeselves compiled if (value is Macro) { object expansion = ((Macro)value).Call(Runtime.AsArray(((Pair)s).Rest())); return Compile1(expansion,environment); } } // It must be a function call return CompileFunctionCall(f, ((Pair)s).Rest(),environment); } throw new LSharpException(string.Format("Bad object in expression: ", s),environment); }
/// <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; }
public static Expression Compile1(Object s, Environment environment) { // Literals are constants if (Runtime.IsLiteral(s)) { return(Expression.Constant(s)); } // Special Syntax gets expanded and compiled if (Runtime.IsSsyntax(s)) { return(Compile1(Runtime.ExpandSSyntax(s), environment)); } // Symbols are variable references if (s is Symbol) { return(CompileVarRef((Symbol)s)); } // Special Syntax gets expanded and compiled if (Runtime.IsSsyntax(Runtime.Car(s))) { object expansion = Runtime.ExpandSSyntax(((Pair)s).First()); return(Compile1(new Pair(expansion, (Pair)s), environment)); } if (s is Pair) { object f = ((Pair)s).First(); // Special Forms if (f == Symbol.FromName("if")) { return(Compiler.CompileIf(((Pair)s).Rest(), environment)); } if (f == Symbol.FromName("quote")) { return(Compiler.CompileQuote(((Pair)s).Rest(), environment)); } if (f == Symbol.FromName("call-clr")) { return(Compiler.CompileCall((Pair)((Pair)s).Rest(), environment)); } if (f == Symbol.FromName("call-instance")) { return(Compiler.CompileInstanceCall((Pair)((Pair)s).Rest(), environment)); } if (f == Symbol.FromName("fn")) { return(Compiler.CompileFn((Pair)((Pair)s).Rest(), environment)); } if (f == Symbol.FromName("macro")) { return(Compiler.CompileMac((Pair)((Pair)s).Rest(), environment)); } if (f == Symbol.FromName("quasiquote")) { return(Compiler.CompileQuasiQuote((Pair)((Pair)s).Rest(), environment)); } if ((f is Symbol) && (environment.Contains((Symbol)f))) { object value = environment.GetValue((Symbol)f); // Macros get invoked at compile time, then their results are themeselves compiled if (value is Macro) { object expansion = ((Macro)value).Call(Runtime.AsArray(((Pair)s).Rest())); return(Compile1(expansion, environment)); } } // It must be a function call return(CompileFunctionCall(f, ((Pair)s).Rest(), environment)); } throw new LSharpException(string.Format("Bad object in expression: ", s), environment); }
/// <summary> /// Evaluates an expression in a given lexical environment /// </summary> /// <param name="form"></param> /// <param name="environment"></param> /// <returns></returns> public static Object Eval(Object expression, Environment environment) { profiler.TraceCall(expression); if (expression == Reader.EOFVALUE) { return(profiler.TraceReturn(expression)); } if (expression == null) { return(profiler.TraceReturn(null)); } // The expression is either an atom or a list if (Primitives.IsAtom(expression)) { // Number if (expression is double) { return(profiler.TraceReturn(expression)); } if (expression is int) { return(profiler.TraceReturn(expression)); } // Character if (expression is char) { return(profiler.TraceReturn(expression)); } // String if (expression is string) { return(profiler.TraceReturn(expression)); } Symbol sym = expression as Symbol; if (sym == Symbol.TRUE) { return(profiler.TraceReturn(true)); } if (sym == Symbol.FALSE) { return(profiler.TraceReturn(false)); } if (sym == Symbol.NULL) { return(profiler.TraceReturn(null)); } // If the symbol is bound to a value in this lexical environment if (environment.Contains(sym)) { // Then it's a variable so return it's value return(profiler.TraceReturn(environment.GetValue(sym))); } else { // Otherwise symbols evaluate to themselves return(profiler.TraceReturn(expression)); } } else { // The expression must be a list Cons cons = (Cons)expression; // Lists are assumed to be of the form (function arguments) // See if there is a binding to a function, clsoure, macro or special form // in this lexical environment object function = environment.GetValue((Symbol)cons.First()); // If there is no binding, then use the function name directly - it's probably // the name of a .NET method if (function == null) { function = cons.First(); } // If it's a special form if (function is SpecialForm) { return(profiler.TraceReturn(((SpecialForm)function)((Cons)cons.Cdr(), environment))); } // If its a macro application if (function is Macro) { object expansion = ((Macro)function).Expand((Cons)cons.Cdr()); return(profiler.TraceReturn(Runtime.Eval(expansion, environment))); } // It must be a function, closure or method invocation, // so call apply Object arguments = EvalList((Cons)cons.Cdr(), environment); return(profiler.TraceReturn(Runtime.Apply(function, arguments, environment))); } }