4 using System.Collections;
6 public class LispWriter {
7 private TextWriter stream;
8 private int IndentDepth;
9 private Stack lists = new Stack();
11 public LispWriter(TextWriter stream) {
15 public void WriteComment(string comment) {
16 stream.WriteLine("; " + comment);
19 public void StartList(string name) {
21 stream.WriteLine("(" + name);
26 public void EndList(string name) {
28 throw new Exception("Trying to close list while none is open");
29 string back = (string) lists.Pop();
32 String.Format("Trying to close {0} which is not open", name));
36 stream.WriteLine(")");
39 public void Write(string name, object value) {
41 stream.Write("(" + name);
43 stream.Write(" \"" + value.ToString() + "\"");
44 } else if(value is IEnumerable) {
45 foreach(object o in (IEnumerable) value) {
53 stream.WriteLine(")");
56 private void WriteValue(object val) {
58 stream.Write((bool) val ? "#t" : "#f");
59 } else if(val is int || val is float) {
60 stream.Write(val.ToString());
62 stream.Write("\"" + val.ToString() + "\"");
66 public void WriteVerbatimLine(string line) {
68 stream.WriteLine(line);
71 private void indent() {
72 for(int i = 0; i < IndentDepth; ++i)