add stdoutinerr support

This commit is contained in:
Qrakhen 2025-12-11 20:29:44 +01:00
parent 3b50abebe4
commit c4de1dfb0a
3 changed files with 70 additions and 23 deletions

View File

@ -33,14 +33,14 @@ public static class Benchmark
if (!_clocks.TryGetValue(key, out Stopwatch? sw)) { if (!_clocks.TryGetValue(key, out Stopwatch? sw)) {
sw = _clocks[key] = new Stopwatch(); sw = _clocks[key] = new Stopwatch();
IO.Console.Write($" ::: Registered new benchmark clock '{key}'\n"); IO.Console.Write($" ::: Registered new benchmark clock '{key}'");
} }
if (!string.IsNullOrEmpty(message)) if (!string.IsNullOrEmpty(message))
IO.Console.Write($" ::: {key}:{line} > {message}\n"); IO.Console.Write($" ::: {key}:{line} > {message}");
if (sw.IsRunning) if (sw.IsRunning)
IO.Console.Write($" ::: {key}:{line} > already running with an elapsed time of {sw.Elapsed}. clock was reset.\n"); IO.Console.Write($" ::: {key}:{line} > already running with an elapsed time of {sw.Elapsed}. clock was reset.");
sw.Reset(); sw.Reset();
sw.Start(); sw.Start();

View File

@ -1,20 +1,61 @@
namespace Qrakhen.Qamp.Core.IO; using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Qrakhen.Qamp.Core.IO;
public static class Console public static class Console
{ {
// TEMP public static OutputStreamMessageHandler StdOut;
public static Action<string>? StdOut; public static ErrorStreamMessageHandler StdErr;
// TEMP public static InputStreamListenHandler StdIn;
public static MessageFormatter Formatter;
public static void Write(object? any) static Console()
{
StdOut = System.Console.Out.WriteLine;
StdErr = System.Console.Error.WriteLine;
StdIn = System.Console.In.ReadLine;
Formatter = (p) => {
if (p.Length == 0)
return [];
return p
.Select(v => $"{v}".Split('\n'))
.Select(v => string.Join($"\n :> ", v))
.Select(v => $" :> {v}")
.ToArray();
};
}
public static void Write(params object[] parameters)
{ {
string[] split = $"{any ?? "null"}".Split('\n');
for (int i = 0; i < split.Length; i++) {
string line = (i == 0 ? " :> " : " ") + $"{split[i]}\n";
if (StdOut == null) if (StdOut == null)
System.Console.Write(line); return;
else
var lines = Formatter(parameters);
foreach (var line in lines)
StdOut(line); StdOut(line);
} }
public static string? Read()
{
if (StdIn == null)
throw new InvalidOperationException($"No stdin provided, can not read input stream.");
return StdIn();
}
public static void Error(params object[] parameters)
{
if (StdErr == null)
Write(parameters);
var lines = Formatter(parameters);
foreach (var line in lines)
StdErr(line);
} }
} }
public delegate string? InputStreamListenHandler();
public delegate void OutputStreamMessageHandler(string? message);
public delegate void ErrorStreamMessageHandler(string? message);
public delegate string[] MessageFormatter(params object[] parameters);

View File

@ -48,7 +48,8 @@ public class Runner : IDisposable
Options = options; Options = options;
Calls = new StackLike<Call>(options.MaxCalls); Calls = new StackLike<Call>(options.MaxCalls);
Stack = new StackLike<Value>(options.InitialStackSize); Stack = new StackLike<Value>(options.InitialStackSize);
IO.Console.StdOut = options.StdOut; IO.Console.StdOut ??= options.StdOut;
IO.Console.StdIn ??= options.StdIn;
} }
public ExecutionResult Run(Stream stream) public ExecutionResult Run(Stream stream)
@ -62,15 +63,20 @@ public class Runner : IDisposable
#endif #endif
Stack = new StackLike<Value>(Options.InitialStackSize); Stack = new StackLike<Value>(Options.InitialStackSize);
} }
Benchmark.Start($"Compile Start");
using Reader reader = new Reader(stream); using Reader reader = new Reader(stream);
Compilation.Digester digester = new(reader); Compilation.Digester digester = new(reader);
Function function = digester.Digest(); Function function = digester.Digest();
Benchmark.End($"Compile End");
if (stream.Length > 64) if (stream.Length > 64)
File.WriteAllBytes($"./func.{DateTime.Now:yyyyMMdd_HHmmss}.sqi", function.Serialize(true)); File.WriteAllBytes($"./func.{DateTime.Now:yyyyMMdd_HHmmss}.sqi", function.Serialize(true));
Context closure = new Context(function); Context closure = new Context(function);
Push(Obj.Create(closure)); Push(Obj.Create(closure));
Invoke(closure, 0); Invoke(closure, 0);
return Execute(); Benchmark.Start($"Execution Start");
ExecutionResult result = Execute();
Benchmark.End($"Execution End");
return result;
} }
private bool TestFlag(Op code, Op flag) => (code & Op.F_Mask) == flag; private bool TestFlag(Op code, Op flag) => (code & Op.F_Mask) == flag;
@ -681,14 +687,14 @@ public enum ExecutionResult
} }
public readonly struct Options( public readonly struct Options(
Action<string>? stdOut = null, IO.OutputStreamMessageHandler stdOut = null,
Action<string>? stdIn = null, IO.InputStreamListenHandler stdIn = null,
int maxCalls = 0x100, int maxCalls = 0x100,
int stackSize = 0x200, int stackSize = 0x200,
int initialStackSize = 0x80) int initialStackSize = 0x80)
{ {
public readonly Action<string>? StdOut = stdOut; public readonly IO.OutputStreamMessageHandler StdOut = stdOut;
public readonly Action<string>? StdIn = stdIn; public readonly IO.InputStreamListenHandler StdIn = stdIn;
public readonly int MaxCalls = maxCalls; public readonly int MaxCalls = maxCalls;
public readonly int StackSize = stackSize; public readonly int StackSize = stackSize;
public readonly int InitialStackSize = initialStackSize; public readonly int InitialStackSize = initialStackSize;