105 lines
3.5 KiB
C#
105 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Upsilon;
|
|
using Upsilon.Evaluator;
|
|
|
|
namespace Ycicle
|
|
{
|
|
internal static class Program
|
|
{
|
|
private static void Main()
|
|
{
|
|
Console.WriteLine("Upsilon REPL");
|
|
Script script = null;
|
|
while (true)
|
|
{
|
|
Console.Write("» ");
|
|
var input = Console.ReadLine();
|
|
if (input == "exit") return;
|
|
|
|
script = script == null ? new Script(input) : Script.ContinueWith(script, input);
|
|
|
|
if (script.Diagnostics.Messages.Count > 0)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine("Errors were found during parsing");
|
|
foreach (var diagnosticsMessage in script.Diagnostics.Messages)
|
|
{
|
|
LogError(diagnosticsMessage);
|
|
}
|
|
Console.ResetColor();
|
|
continue;
|
|
}
|
|
//Console.WriteLine(script.PrettyPrintSyntaxTree());
|
|
|
|
var evaluate = script.Evaluate();
|
|
if (script.Diagnostics.Messages.Count > 0)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine("Errors were found during evaluating");
|
|
foreach (var diagnosticsMessage in script.Diagnostics.Messages)
|
|
{
|
|
LogError(diagnosticsMessage);
|
|
}
|
|
Console.ResetColor();
|
|
}
|
|
else
|
|
{
|
|
if (evaluate == null)
|
|
continue;
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
|
|
Console.WriteLine(ParseEvaluated(evaluate));
|
|
Console.ResetColor();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void LogError(DiagnosticsMessage message)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine(message.Message);
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
Console.Write(message.GetDiagnosticPosition() + " ");
|
|
Console.Write(message.LineBeforeError());
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.Write(message.AtError());
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
Console.Write(message.LineAfterError());
|
|
Console.WriteLine();
|
|
}
|
|
|
|
private static string ParseEvaluated(object o, int jumps = 0)
|
|
{
|
|
var sb = new StringBuilder();
|
|
if (o is Dictionary<string, object> dic)
|
|
{
|
|
for (var i = 0; i < jumps; i++) sb.Append(" ");
|
|
sb.Append("{\n");
|
|
foreach (var (key, value) in dic)
|
|
{
|
|
for (var i = 0; i < jumps; i++) sb.Append(" ");
|
|
sb.Append(" {");
|
|
sb.Append("\"");
|
|
sb.Append(key);
|
|
sb.Append("\": ");
|
|
sb.Append(ParseEvaluated(value, jumps + 1));
|
|
sb.Append(" }");
|
|
sb.Append(",\n");
|
|
}
|
|
for (var i = 0; i < jumps; i++) sb.Append(" ");
|
|
sb.Append("}\n");
|
|
}
|
|
else
|
|
{
|
|
sb.Append(o.ToString());
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
} |