3 using System.Collections;
5 public class LispWriter {
6 private TextWriter stream;
7 private int IndentDepth;
8 private Stack lists = new Stack();
10 public LispWriter(TextWriter stream) {
14 public void WriteComment(string comment) {
15 stream.WriteLine("; " + comment);
18 public void StartList(string name) {
20 stream.WriteLine("(" + name);
25 public void EndList(string name) {
27 throw new Exception("Trying to close list while none is open");
28 string back = (string) lists.Pop();
31 String.Format("Trying to close {0} which is not open", name));
35 stream.WriteLine(")");
38 public void Write(string name, object value) {
40 stream.Write("(" + name);
42 stream.Write(" \"" + value.ToString() + "\"");
43 } else if(value is IEnumerable) {
44 foreach(object o in (IEnumerable) value) {
52 stream.WriteLine(")");
55 private void WriteValue(object val) {
57 stream.Write((bool) val ? "#t" : "#f");
58 } else if(val is int || val is float) {
59 stream.Write(val.ToString());
61 stream.Write("\"" + val.ToString() + "\"");
65 public void WriteVerbatimLine(string line) {
67 stream.WriteLine(line);
70 private void indent() {
71 for(int i = 0; i < IndentDepth; ++i)