KumiScript/interpreter/ReadEvalPrintLoop.cs

46 lines
1.5 KiB
C#
Raw Normal View History

2024-01-26 05:37:51 +00:00
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 = 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;
}
}
}
}
}