KumiScript/interpreter/ReadEvalPrintLoop.cs

46 lines
No EOL
1.6 KiB
C#

using KumiScript.Reader;
namespace KumiScript.Interpreter
{
public class ReadEvalPrintLoop
{
Stream _stdin;
Stream _stdout;
public ReadEvalPrintLoop(Stream stdin, Stream stdout)
{
_stdin = stdin;
_stdout = stdout;
}
public void Loop()
{
Lexer lexer = new Lexer(_stdin);
Parser parser = new Parser(lexer);
StreamWriter streamWriter = new StreamWriter(_stdout);
Environment top = MathPrimitives.RegisterPrimitives(LispPrimitives.RegisterPrimitives());
while (true) //this thing is ugly but it's just a little test
{
Expression expr = parser.NextTopLevelExpression();
try
{
Expression result = expr.Eval(top);
streamWriter.Write(string.Format("{0}\n", result.ToString()));
streamWriter.Flush();
} catch (Exception ex) {
if (ex is InterpreterInvalidApplicationException
or InterpreterInvalidDefinitionException
or InterpreterInvalidInvocationException
or InterpreterTypingException
or InterpreterUnboundSymbolException)
{
streamWriter.WriteLine(ex);
streamWriter.Flush();
continue;
}
throw;
}
}
}
}
}