public static Object BackQuoteReader(params Object[] args) { TextReader textReader = (TextReader)args[0]; ReadTable readTable = (ReadTable)args[1]; return(new Cons(Symbol.FromName("backquote"), new Cons(Read(textReader, readTable, null), null))); }
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); }
/// <summary> /// Starts the toploop running using specified input, output and error streams /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> /// <param name="error"></param> public void Run(TextReader reader, TextWriter writer, TextWriter error) { Symbol LAST = Symbol.FromName("?"); while (true) { try { Object o = trace ? Runtime.EvalString("(eval (prl (read (in Console))))", environment) : Runtime.EvalString("(eval (read (in Console)))", environment); if (o == Reader.EOFVALUE) { return; } if (tracereturn) { writer.Write(prompt); writer.WriteLine(Printer.WriteToString(o)); } environment.AssignLocal(LAST, o); } catch (LSharpException e) { error.WriteLine(e.Message); } catch (Exception e) { error.WriteLine(e.GetBaseException()); } } }
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 static Object AtomReader(int c, TextReader textReader) { if (((Char)c) == ')') { return(Symbol.FromName(")")); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append((char)c); bool done = false; do { int nextChar = textReader.Peek(); if ((nextChar == -1) || (TerminatingCharP((Char)nextChar))) { done = true; } else { c = textReader.Read(); stringBuilder.Append((char)c); } } while (!done); string token = stringBuilder.ToString(); Double d; try { // Try reading the number as an integer if (Double.TryParse(token, System.Globalization.NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d)) { return((int)d); } // Try reading the number as a double if (Double.TryParse(token, System.Globalization.NumberStyles.Any, NumberFormatInfo.InvariantInfo, out d)) { return(d); } else { return(Symbol.FromName(token)); } } catch (FormatException) // OH NO MONO! { System.Diagnostics.Trace.WriteLine("Why???? " + token); return(Symbol.FromName(token)); } }
public static Object AtomReader(int c, TextReader textReader) { if (((Char)c) == ')') { return(RPAREN); } if (((Char)c) == ']') { return(RBRACKET); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append((char)c); bool done = false; do { int nextChar = textReader.Peek(); if ((nextChar == -1) || (IsTerminator((Char)nextChar))) { done = true; } else { c = textReader.Read(); stringBuilder.Append((char)c); } } while (!done); string token = stringBuilder.ToString(); Double d; // Try reading the number as an integer if (Double.TryParse(token, System.Globalization.NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d)) { return((int)d); } // Try reading the number as a double if (Double.TryParse(token, System.Globalization.NumberStyles.Any, NumberFormatInfo.InvariantInfo, out d)) { return(d); } else { return(Symbol.FromName(token)); } }
public static Object UnQuoteReader(params Object[] args) { TextReader textReader = (TextReader)args[0]; ReadTable readTable = (ReadTable)args[1]; if (textReader.Peek() == '@') { textReader.Read(); return(new Cons(Symbol.FromName("splice"), new Cons(Read(textReader, readTable, null), null))); } return(new Cons(Symbol.FromName("unquote"), new Cons(Read(textReader, readTable, null), null))); }
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 void InitialiseLSharpBindings() { isresetting = true; foreach (Extension e in extensions) { InitSymbols(e.type, e.delegatetype); extmark++; } // leppie: should this not be Assign() ? // read table Assign(Symbol.FromName("*readtable*"), ReadTable.DefaultReadTable()); // Macros foreach (Type t in macroextensions) { InitMacros(t); } isresetting = false; }
/// <summary> /// Creates a new environment which has access to a previous environment /// </summary> public Environment(Environment environment) { this.previousEnvironment = environment; AssignLocal(Symbol.FromName("environment"), this); }
public Environment() { AssignLocal(Symbol.FromName("environment"), this); }
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); }
private static void ProcessKeyArguments(Cons argumentNameList, Cons argumentList, Environment localEnvironment) { // Make sure that all of the defined key arguments are inserted to the local enviroment with their // defaults. while (argumentNameList != null) { Symbol argumentName = null; object argumentValue = null; // We need to get the name of the argument, it can either be just the name or, it can be // it's own Cons with the name and an expression for the default value. if (argumentNameList.Car() is Cons) { // It is a Cons, so extract the name and the default value. Because the default can be // any expression, we need to evaluate the value every time the function is called. argumentName = (Symbol)argumentNameList.Caar(); argumentValue = Runtime.Eval(argumentNameList.Cadar(), localEnvironment); } else { argumentName = (Symbol)argumentNameList.Car(); } // Add this variable to the closure's environment, then advance to the next parameter. localEnvironment.AssignLocal(argumentName, argumentValue); argumentNameList = (Cons)argumentNameList.Cdr(); } // Now that the parameters and their defaults have been added to the environment we can now // process the supplied arguments. while (argumentList != null) { // Because these are keyed parameters, the caller needs to specify the name of each // parameter. if (argumentList.Car().GetType() != typeof(Symbol)) { throw new LSharpException("Key parameters must be specified by name."); } // Grab the current parameter and the value associated with it. Then make sure that this // is a keyword. Symbol keywordName = (Symbol)argumentList.Car(); object argumentValue = argumentList.Cadr(); if (keywordName.Name[0] != ':') { throw new LSharpException(keywordName + " is not a valid keyword."); } // Now that we know they supplied a keyword, create a symbol out of it and make sure that // it exists. //keywordName = new Symbol(keywordName.Name.Substring(1)); keywordName = Symbol.FromName(keywordName.Name.Substring(1)); if (localEnvironment.Contains(keywordName) == false) { throw new LSharpException(keywordName + " is not a recognised keyword."); } // Update the parameter with the value that the user specified and then move onto the next // argument in the list. localEnvironment.AssignLocal(keywordName, argumentValue); argumentList = (Cons)argumentList.Cddr(); } }