forked from JohnPool/CSharpProlog
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathIO.cs
More file actions
430 lines (308 loc) · 11 KB
/
Copy pathIO.cs
File metadata and controls
430 lines (308 loc) · 11 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*-----------------------------------------------------------------------------------------
C#Prolog -- Copyright (C) 2007-2015 John Pool -- j.pool@ision.nl
This library is free software; you can redistribute it and/or modify it under the terms of
the GNU Lesser General Public License as published by the Free Software Foundation; either
version 3.0 of the License, or any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl-3.0.html), or
enter 'license' at the command prompt.
-------------------------------------------------------------------------------------------*/
using System;
using System.IO;
using System.Text;
namespace Prolog
{
#if NETSTANDARD
using ApplicationException = System.Exception;
#endif
#region BasicIo
public abstract class BasicIo
{
#region abstract methods
public abstract string ReadLine();
public abstract int ReadChar();
public abstract void Write(string s);
public abstract void WriteLine(string s);
public abstract void WriteLine();
public abstract void Clear();
public abstract void Reset();
#endregion abstract methods
public void Write(string s, params object[] o)
{
Write(string.Format(s, o));
}
public void WriteLine(string s, params object[] o)
{
WriteLine(string.Format(s, o));
}
}
#endregion BasicIo
#region DosIO. Base class for DOS IO
public class DosIO : BasicIo
{
public override string ReadLine()
{
return Console.ReadLine();
}
public override int ReadChar()
{
return Console.ReadKey().KeyChar;
}
public override void Write(string s)
{
Console.Write(s);
}
public override void WriteLine(string s)
{
Console.WriteLine(s);
}
public override void WriteLine()
{
Console.WriteLine();
}
public override void Clear()
{
Console.Clear();
}
public override void Reset()
{
}
}
#endregion DosIO
#region FileIO. Base class for text File IO
public class FileIO : BasicIo
{
StreamReader inFile;
StreamWriter outFile;
public FileIO(string inFileName, string outFileName)
{
// no try/catch, as I would not know how to handle the exception caught
inFile = new StreamReader(File.OpenRead(inFileName));
outFile = new StreamWriter(File.OpenWrite(outFileName));
outFile.AutoFlush = true; // file will contain all output even if not closed explicitly
}
public override string ReadLine()
{
if (inFile == null)
throw new ApplicationException("FileIO class: input file is not defined");
return inFile.ReadLine();
}
public override int ReadChar()
{
if (inFile == null)
throw new ApplicationException("FileIO class: input file is not defined");
return inFile.Read();
}
public override void Write(string s)
{
if (outFile == null)
throw new ApplicationException("FileIO class: output file is not defined");
outFile.Write(s);
}
public override void WriteLine(string s)
{
if (outFile == null)
throw new ApplicationException("FileIO class: output file is not defined");
outFile.WriteLine(s);
}
public override void WriteLine()
{
if (outFile == null)
throw new ApplicationException("FileIO class: output file is not defined");
outFile.WriteLine();
}
public override void Clear()
{
}
public override void Reset()
{
}
public void Close()
{
if (inFile != null) inFile.Dispose();
if (outFile != null) outFile.Dispose();
}
}
#endregion FileIO
public partial class PrologEngine
{
FileReaderTerm currentFileReader;
FileWriterTerm currentFileWriter;
public BasicIo BasicIO { set { IO.BasicIO = value; } }
#region BaseRead(Line/Term/Char)CurrentInput and BaseWriteCurrentOutput
// BaseReadCurrentInput. Input is read from StandardInput.
// StandardInput is the file set by the see command, or Console if no such file exists.
string BaseReadLineCurrentInput() // returns null at end of file
{
return (currentFileReader == null) ? IO.ReadLine() : currentFileReader.ReadLine();
}
BaseTerm BaseReadTermCurrentInput()
{
if (currentFileReader == null)
{
StringBuilder query = new StringBuilder();
string line;
PrologParser p = new PrologParser(this);
bool first = true;
while (true)
{
IO.Write("|: ");
if (first) first = false; else query.AppendLine();
if ((line = IO.ReadLine()) == null) return FileTerm.END_OF_FILE;
query.Append(line = line.Trim());
if (line.EndsWith(".")) break;
}
p.StreamIn = "&reading\r\n" + query.ToString(); // equal to parser ReadingSym
BaseTerm result = p.ReadTerm;
return (result == null) ? FileTerm.END_OF_FILE : result;
}
return currentFileReader.ReadTerm();
}
int BaseReadCharCurrentInput() // returns -1 at end of file
{
return (currentFileReader == null) ? IO.ReadChar() : currentFileReader.ReadChar();
}
// BaseWriteCurrentOutput
// Output from print, display, tab, put etc. is written to StandardOutput.
// StandardOutput is the file set by the tell command, or Console if
// no such file exists.
void BaseWriteCurrentOutput(string s)
{
if (currentFileWriter == null)
IO.Write(s);
else
currentFileWriter.Write(s);
}
void BaseWriteCurrentOutput(string s, object[] args)
{
BaseWriteCurrentOutput(string.Format(s, args));
}
#endregion BaseRead(Line/Term/Char)CurrentInput and BaseWriteCurrentOutput
#region Read(Line/Char) and Write(Line)
BaseTerm ReadTerm()
{
return BaseReadTermCurrentInput();
}
string ReadLine()
{
return BaseReadLineCurrentInput();
}
int ReadChar()
{
return BaseReadCharCurrentInput();
}
void Write(BaseTerm t, bool dequote)
{
if (t.IsString)
BaseWriteCurrentOutput(dequote ? t.FunctorToString : '"' + t.FunctorToString + '"');
else if (t.IsAtom)
BaseWriteCurrentOutput(dequote ? t.FunctorToString.Dequoted("'") : t.FunctorToString);
else
BaseWriteCurrentOutput(t.ToString());
}
public void Write(string s)
{
BaseWriteCurrentOutput(s);
}
public void Write(string s, params object[] args)
{
BaseWriteCurrentOutput(s, args);
}
public void WriteLine(string s)
{
BaseWriteCurrentOutput(s + Environment.NewLine);
}
public void WriteLine(string s, params object[] args)
{
BaseWriteCurrentOutput(s + Environment.NewLine, args);
}
public void NewLine()
{
BaseWriteCurrentOutput(Environment.NewLine);
}
#endregion Read(Line/Char) and Write(Line)
// for IO *not* generated by Prolog predicates and not subject to
// current input and current output (i.e. error messages etc.)
public static class IO
{
static BasicIo basicIO;
public static BasicIo BasicIO { get { return basicIO; } set { basicIO = value; } }
public static bool Verbose = true; // message display
static int debugLevel = 0;
public static void SetDebugLevel(int level)
{
debugLevel = level;
}
public static bool Error(string msg, params object[] o)
{
Error(String.Format(msg, o));
return false;
}
public static bool Error(string msg)
{
if (Globals.LineNo == -1) // interactive
throw new ParserException("\r\n*** error: " + msg);
else
throw new ParserException(String.Format("\r\n*** error in line {0} at position {1}: {2}",
Globals.LineNo, Globals.ColNo, msg));
}
public static void Warning(string msg, params object[] o)
{
BasicIO.WriteLine(string.Format("\r\n*** warning: " + msg, o));
}
public static void Warning(string msg)
{
BasicIO.WriteLine("\r\n*** warning: " + msg);
}
public static void Message(string msg, params object[] o)
{
BasicIO.WriteLine(string.Format("\r\n--- " + msg, o));
}
public static void Message(string msg)
{
BasicIO.WriteLine("\r\n--- " + msg);
}
public static void Fatal(string msg, params object[] o)
{
throw new Exception("\r\n*** fatal: " + String.Format(msg, o));
}
public static void Fatal(string msg)
{
throw new Exception("\r\n*** fatal: " + msg);
}
public static string ReadLine()
{
return BasicIO.ReadLine();
}
public static int ReadChar()
{
return BasicIO.ReadChar();
}
public static void Write(string s, params object[] o)
{
BasicIO.Write(string.Format(s, o));
}
public static void Write(string s)
{
BasicIO.Write(s);
}
public static void WriteLine(string s, params object[] o)
{
BasicIO.WriteLine(string.Format(s, o));
}
public static void WriteLine(string s)
{
BasicIO.WriteLine(s);
}
public static void WriteLine()
{
BasicIO.WriteLine();
}
public static void ClearScreen()
{
BasicIO.Clear();
}
}
}
}