-
Notifications
You must be signed in to change notification settings - Fork 0
/
SExpressions.cs
258 lines (252 loc) · 5.41 KB
/
SExpressions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Kawa.SExpressions
{
public class Symbol
{
public string Value { get; private set; }
public Symbol(string value)
{
Value = value;
}
public override string ToString()
{
return Value;
}
public static implicit operator string(Symbol s)
{
return s.Value;
}
public static implicit operator Symbol(string s)
{
return new Symbol(s);
}
public override bool Equals(object obj)
{
if (obj is string)
return Value == (string)obj;
if (obj is Symbol)
return Value == ((Symbol)obj).Value;
return base.Equals(obj);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
}
public class SExpression
{
public string Original { get; private set; }
public object Data { get; private set; }
public SExpression() { }
public SExpression(string str)
{
Original = str;
Data = Parse(str);
}
public override string ToString()
{
return ToSexpression(Data);
}
public string ToSexpression()
{
return ToSexpression(Data);
}
public string ToSexpression(object thing)
{
if (thing is string)
{
if (((string)thing).Contains(' ') || ((string)thing).Contains('\t') || ((string)thing).Contains('\n'))
return string.Format("\"{0}\"", thing);
else
return thing.ToString();
}
if (thing is List<object>)
{
return string.Format("({0})", string.Join(" ", ((List<object>)thing).Select(x => ToSexpression(x))));
}
return thing.ToString();
}
private enum States { TokenStart, ReadQuotedString, ReadStringOrNumber, Comment, ReadQSEscape }
private int lastInt;
private object Parse(string str)
{
var state = States.TokenStart;
var commentState = state;
var tokens = new List<object>();
var word = new StringBuilder();
foreach (var ch in str)
{
switch (state)
{
case States.TokenStart:
if (ch == '(' || ch == ')' || ch == '\'')
{
tokens.Add(ch);
}
else if (char.IsWhiteSpace(ch))
{
//just eat it.
}
else if (ch == '\"')
{
state = States.ReadQuotedString;
word.Clear();
}
else if (ch == ';')
{
commentState = state;
state = States.Comment;
word.Clear();
}
else
{
state = States.ReadStringOrNumber;
word.Clear();
word.Append(ch);
}
break;
case States.ReadQuotedString:
if (ch == '\"')
{
tokens.Add(word.ToString());
state = States.TokenStart;
}
else if (ch == '\\')
{
state = States.ReadQSEscape;
}
else
{
word.Append(ch);
}
break;
case States.ReadQSEscape:
if (ch == '\\')
{
word.Append('\\');
}
else if (ch == '\"')
{
word.Append('\"');
}
else if (ch == '\'')
{
word.Append('\'');
}
else if (ch == 'n')
{
word.Append('\n');
}
else
{
word.Append('\\');
word.Append(ch);
}
state = States.ReadQuotedString;
break;
case States.ReadStringOrNumber:
if (char.IsWhiteSpace(ch))
{
tokens.Add(SymbolOrNumber(word.ToString()));
state = States.TokenStart;
}
else if (ch == ')')
{
tokens.Add(SymbolOrNumber(word.ToString()));
tokens.Add(')');
state = States.TokenStart;
}
else
{
word.Append(ch);
}
break;
case States.Comment:
if (ch == '\r' || ch == '\n')
{
state = commentState;
}
break;
}
}
var i = 0;
return TokensToArray(tokens, ref i);
}
private object SymbolOrNumber(string word)
{
var i = 0;
var f = 0.0f;
if (int.TryParse(word, out i))
{
lastInt = i;
return i;
}
else if (float.TryParse(word, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f))
return f;
else
return new Symbol(word);
}
private object TokensToArray(List<object> tokens, ref int index)
{
var result = new List<object>();
while (index < tokens.Count)
{
if (tokens[index] is char && (char)tokens[index] == '\'')
{
index++;
var quote = new List<object>();
quote.Add(new Symbol("quote"));
if (tokens[index] is char && (char)tokens[index] == '(')
{
index++;
quote.Add(TokensToArray(tokens, ref index));
}
else
quote.Add(new List<object>() { tokens[index] });
result.Add(quote);
}
else if (tokens[index] is char && (char)tokens[index] == '(')
{
index++;
result.Add(TokensToArray(tokens, ref index));
}
else if (tokens[index] is char && (char)tokens[index] == ')')
{
return result;
}
else
{
result.Add(tokens[index]);
}
index++;
}
return result;
}
}
public static class SExtensions
{
public static string Stringify(this List<object> list)
{
var ret = new StringBuilder();
ret.Append('(');
var spacer = "";
foreach (var item in list)
{
ret.Append(spacer);
spacer = " ";
if (item is List<object>)
ret.Append(Stringify((List<object>)item));
else if (item is string)
ret.Append("\"" + item.ToString() + "\"");
else if (item is int || item is Symbol)
ret.Append(item.ToString());
}
ret.Append(')');
return ret.ToString();
}
}
}