public Interpreter() { this.GlobalEnv = new Environment(); this.tokenizer = new Tokenizer(); this.parser = new PairParser(); this.evaluator = new Evaluator(this); this.macroEvaluator = new Evaluator(this); this.macroExpander = new MacroExpander(macroEvaluator); DefinePrimitives(); }
public override IScheminType Execute(Environment env, Evaluator eval, ScheminPair args) { ScheminPort readFrom = eval.CurrentInputPort; if (args.Length > 0) { readFrom = (ScheminPort) args.Car; } Tokenizer t = new Tokenizer(); PairParser p = new PairParser(); bool completeInput = false; bool emptyInput = true; int openParens = 0; int closeParens = 0; List<Token> partialInput = new List<Token>(); while (completeInput != true) { StringBuilder built = new StringBuilder(); char next; for (;;) { int nextRead = readFrom.InputStream.Read(); next = (char)nextRead; if (nextRead == -1 || (next == '\n' && !emptyInput)) { break; } if (next != '\r') { built.Append(next); emptyInput = false; } } var lineTokens = t.Tokenize(built.ToString()); foreach (Token token in lineTokens) { partialInput.Add(token); if (token.Type == TokenType.OpenParen) { openParens++; } else if (token.Type == TokenType.CloseParen) { closeParens++; } } if (openParens == closeParens) { completeInput = true; break; } } var parsed = p.Parse(partialInput, true).Car; return parsed; }