Upsilon/Ycicle/Program.cs

92 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using Upsilon;
using Upsilon.BaseTypes;
using Upsilon.Binder;
using Upsilon.Evaluator;
using UpsilonCompiler;
namespace Ycicle
{
internal static class Program
{
private static void Main()
{
Console.WriteLine("Upsilon REPL");
var variables = new Dictionary<VariableSymbol, LuaType>();
var showTranspiledOutput = false;
while (true)
{
Console.Write("» ");
var input = Console.ReadLine();
switch (input)
{
case "exit":
return;
case "compiled":
showTranspiledOutput = !showTranspiledOutput;
break;
}
var parsed = new Script(input, variables);
if (parsed.Diagnostics.Messages.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Errors were found during parsing");
foreach (var diagnosticsMessage in parsed.Diagnostics.Messages)
{
LogError(diagnosticsMessage);
}
Console.ResetColor();
continue;
}
if (showTranspiledOutput)
{
var transpiler = new Transpiler();
var transpiled = transpiler.TranspilerToCSharp(parsed.Bind());
Console.WriteLine(transpiled);
}
else
{
var evaluate = parsed.Evaluate();
if (evaluate == null)
continue;
if (parsed.Diagnostics.Messages.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Errors were found during evaluating");
foreach (var diagnosticsMessage in parsed.Diagnostics.Messages)
{
LogError(diagnosticsMessage);
}
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(evaluate);
Console.ResetColor();
}
}
}
}
private static void LogError(DiagnosticsMessage message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message.Message);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(message.BeforeError());
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(message.AtError());
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(message.AfterError());
Console.WriteLine();
}
}
}