2018-11-10 12:11:36 +00:00
|
|
|
|
using System;
|
2018-11-17 18:13:05 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2018-11-11 09:26:52 +00:00
|
|
|
|
using Upsilon;
|
2018-11-10 12:11:36 +00:00
|
|
|
|
using Upsilon.Evaluator;
|
2018-11-23 11:55:28 +00:00
|
|
|
|
using Upsilon.StandardLibraries;
|
2018-11-10 12:11:36 +00:00
|
|
|
|
|
2018-11-14 12:09:01 +00:00
|
|
|
|
namespace Ycicle
|
2018-11-10 12:11:36 +00:00
|
|
|
|
{
|
2018-11-14 12:09:01 +00:00
|
|
|
|
internal static class Program
|
2018-11-10 12:11:36 +00:00
|
|
|
|
{
|
2018-11-14 12:09:01 +00:00
|
|
|
|
private static void Main()
|
2018-11-10 12:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Upsilon REPL");
|
2018-11-17 11:40:28 +00:00
|
|
|
|
Script script = null;
|
2018-11-26 16:23:56 +00:00
|
|
|
|
var options = new ScriptOptions()
|
|
|
|
|
{
|
|
|
|
|
ThrowExceptionOnError = false
|
|
|
|
|
};
|
2018-11-23 11:55:28 +00:00
|
|
|
|
|
2018-11-10 12:11:36 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Console.Write("» ");
|
|
|
|
|
var input = Console.ReadLine();
|
2018-11-17 14:18:51 +00:00
|
|
|
|
if (input == "exit") return;
|
2018-11-23 11:55:28 +00:00
|
|
|
|
script = script == null
|
2018-11-26 16:23:56 +00:00
|
|
|
|
? Executor.ParseInput(input, options)
|
2018-11-26 15:55:10 +00:00
|
|
|
|
: Executor.ContinueWith(script, input);
|
2018-11-15 14:51:05 +00:00
|
|
|
|
|
2018-11-28 09:56:35 +00:00
|
|
|
|
if (script.Diagnostics.Errors.Count > 0)
|
2018-11-11 09:26:52 +00:00
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
Console.WriteLine("Errors were found during parsing");
|
2018-11-28 09:56:35 +00:00
|
|
|
|
foreach (var diagnosticsMessage in script.Diagnostics.Errors)
|
2018-11-11 09:26:52 +00:00
|
|
|
|
{
|
2018-11-28 09:56:35 +00:00
|
|
|
|
LogMessage(DiagnosticLevel.Error, diagnosticsMessage);
|
2018-11-11 09:26:52 +00:00
|
|
|
|
}
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-11-18 15:08:20 +00:00
|
|
|
|
//Console.WriteLine(script.PrettyPrintSyntaxTree());
|
2018-11-20 13:05:34 +00:00
|
|
|
|
var evaluate = script.Evaluate();
|
2018-11-11 09:26:52 +00:00
|
|
|
|
|
2018-11-28 09:56:35 +00:00
|
|
|
|
if (script.Diagnostics.Errors.Count > 0)
|
2018-11-11 09:26:52 +00:00
|
|
|
|
{
|
2018-11-17 12:05:08 +00:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
Console.WriteLine("Errors were found during evaluating");
|
2018-11-28 09:56:35 +00:00
|
|
|
|
foreach (var diagnosticsMessage in script.Diagnostics.Errors)
|
2018-11-17 12:05:08 +00:00
|
|
|
|
{
|
2018-11-28 09:56:35 +00:00
|
|
|
|
LogMessage(DiagnosticLevel.Error, diagnosticsMessage);
|
2018-11-17 12:05:08 +00:00
|
|
|
|
}
|
|
|
|
|
Console.ResetColor();
|
2018-11-11 09:26:52 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-11-28 09:56:35 +00:00
|
|
|
|
foreach (var diagnosticsMessage in script.Diagnostics.Warnings)
|
|
|
|
|
{
|
|
|
|
|
LogMessage(DiagnosticLevel.Warning, diagnosticsMessage);
|
|
|
|
|
}
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
|
2018-11-16 13:11:27 +00:00
|
|
|
|
if (evaluate == null)
|
|
|
|
|
continue;
|
2018-11-17 12:05:08 +00:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
2018-11-17 18:13:05 +00:00
|
|
|
|
|
|
|
|
|
Console.WriteLine(ParseEvaluated(evaluate));
|
2018-11-17 12:05:08 +00:00
|
|
|
|
Console.ResetColor();
|
2018-11-14 12:09:01 +00:00
|
|
|
|
}
|
2018-11-10 12:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-11 09:26:52 +00:00
|
|
|
|
|
2018-11-28 09:56:35 +00:00
|
|
|
|
private static void LogMessage(DiagnosticLevel level, DiagnosticsMessage message)
|
2018-11-11 09:26:52 +00:00
|
|
|
|
{
|
2018-11-28 09:56:35 +00:00
|
|
|
|
if (level == DiagnosticLevel.Error)
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
else if (level == DiagnosticLevel.Warning)
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
|
|
|
|
|
2018-11-11 09:26:52 +00:00
|
|
|
|
Console.WriteLine(message.Message);
|
|
|
|
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
2018-11-17 13:20:43 +00:00
|
|
|
|
Console.Write(message.GetDiagnosticPosition() + " ");
|
2018-11-17 13:49:26 +00:00
|
|
|
|
Console.Write(message.LineBeforeError());
|
2018-11-11 09:26:52 +00:00
|
|
|
|
|
2018-11-28 09:56:35 +00:00
|
|
|
|
if (level == DiagnosticLevel.Error)
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
else if (level == DiagnosticLevel.Warning)
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
|
|
|
|
|
2018-11-11 09:26:52 +00:00
|
|
|
|
Console.Write(message.AtError());
|
|
|
|
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
2018-11-17 13:49:26 +00:00
|
|
|
|
Console.Write(message.LineAfterError());
|
2018-11-11 09:26:52 +00:00
|
|
|
|
Console.WriteLine();
|
|
|
|
|
}
|
2018-11-17 18:13:05 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2018-11-23 12:28:11 +00:00
|
|
|
|
sb.Append(o);
|
2018-11-17 18:13:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2018-11-10 12:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|