make editor editing and running

This commit is contained in:
Qrakhen 2025-11-25 23:09:15 +01:00
parent f002cd8de4
commit 30a11c7b0b
8 changed files with 88 additions and 24 deletions

View File

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

View File

@ -2,11 +2,19 @@
public static class Console
{
// TEMP
public static Action<string>? StdOut;
// TEMP
public static void Write(object? any)
{
string[] split = $"{any ?? "null"}".Split('\n');
for (int i = 0; i < split.Length; i++) {
System.Console.Write((i == 0 ? " :> " : " ") + $"{split[i]}\n");
string line = (i == 0 ? " :> " : " ") + $"{split[i]}\n";
if (StdOut == null)
System.Console.Write(line);
else
StdOut(line);
}
}
}

View File

@ -48,6 +48,7 @@ public class Runner : IDisposable
Options = options;
Calls = new StackLike<Call>(options.MaxCalls);
Stack = new StackLike<Value>(options.InitialStackSize);
IO.Console.StdOut = options.StdOut;
}
public ExecutionResult Run(Stream stream)
@ -664,13 +665,17 @@ public enum ExecutionResult
}
public readonly struct Options(
int maxCalls = 0x100,
int stackSize = 0x200,
int initialStackSize = 0x80)
Action<string>? stdOut = null,
Action<string>? stdIn = null,
int maxCalls = 0x100,
int stackSize = 0x200,
int initialStackSize = 0x80)
{
public readonly Action<string>? StdOut = stdOut;
public readonly Action<string>? StdIn = stdIn;
public readonly int MaxCalls = maxCalls;
public readonly int StackSize = stackSize;
public readonly int InitialStackSize = initialStackSize;
public Options() : this(0x100, 0x200, 0x80) { }
public Options() : this(null, null, 0x100, 0x200, 0x80) { }
}

View File

@ -131,7 +131,7 @@ public class Logger : ILogger, ILogFormatter
foreach (var value in values) {
var lines = Format(value);
for (int i = 0; i < lines.Length; i++)
Console.WriteLine($"{(i == 0 ? header.First : header.Extra)}{lines[i]}");
IO.Console.Write($"{(i == 0 ? header.First : header.Extra)}{lines[i]}\n");
}
Console.ForegroundColor = ConsoleColor.White;
}

View File

@ -1,4 +1,5 @@
using Qrakhen.Qamp.Memory;
using Qrakhen.Qamp.Editor.Primitives;
using Qrakhen.Qamp.Memory;
using System;
using System.ComponentModel;
using System.Windows;
@ -38,11 +39,11 @@ public partial class LineRenderer : UserControl
);
var x = Random.Shared.Next(0, LineBuffer.Tail);
var s = new BufferSpan(x, Random.Shared.Next(x, LineBuffer.Tail));
var s = new BufferRegion(new BufferPosition(0, x), new BufferPosition(0, Random.Shared.Next(x, LineBuffer.Tail)));
Selection = s;
var highlighter = TextHelper.GetText(
new string(' ', Selection.Start) + new string('█', Selection.End - Selection.Start),
new string(' ', Selection.From.Column) + new string('█', Selection.To.Column - Selection.From.Column),
FontSize,
Highlighter,
VisualTreeHelper.GetDpi(this).PixelsPerDip
@ -90,12 +91,12 @@ public partial class LineRenderer : UserControl
public static readonly DependencyProperty SelectionProperty =
DependencyProperty.Register(
nameof(Selection),
typeof(BufferSpan),
typeof(BufferRegion),
typeof(LineRenderer),
new FrameworkPropertyMetadata(BufferSpan.None, OnSelectionChanged));
new FrameworkPropertyMetadata(BufferRegion.Void, OnSelectionChanged));
public BufferSpan Selection {
get => (BufferSpan)GetValue(SelectionProperty);
public BufferRegion Selection {
get => (BufferRegion)GetValue(SelectionProperty);
set => SetValue(SelectionProperty, value);
}

View File

@ -92,7 +92,8 @@
<TextBlock Background="#202122"
Foreground="#32ef64"
Padding="4"
FontFamily="Consolas">$ ...</TextBlock>
Text="{Binding RunnerOutput}"
FontFamily="Consolas"></TextBlock>
</ScrollViewer>
</Grid>
</Grid>

View File

@ -1,9 +1,11 @@
using Qrakhen.Qamp.Editor.Commands;
using Microsoft.Win32;
using Qrakhen.Qamp.Editor.Commands;
using Qrakhen.Qamp.Editor.Primitives;
using Qrakhen.Qamp.Memory;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Windows.Documents;
using System.Windows.Input;
@ -17,6 +19,12 @@ public class SelectableLineBuffer(LineBuffer buffer)
public class EditorFrameViewModel : ObservableObject
{
private Encoding _encoding;
public Encoding Encoding {
get => _encoding;
set => SetProperty(ref _encoding, value);
}
private FileInfo _fileInfo;
public FileInfo FileInfo {
get => _fileInfo;
@ -56,6 +64,8 @@ public class EditorFrameViewModel : ObservableObject
public EditorFrameViewModel()
{
Encoding = Encoding.ASCII;
MoveCaretCommand = new RelayCommand(ExecuteMoveCaret);
MoveBlockCommand = new RelayCommand(ExecuteMoveBlock);
InsertCommand = new RelayCommand(ExecuteInsert);
@ -202,4 +212,25 @@ public class EditorFrameViewModel : ObservableObject
}
#endregion
public string Serialize(string? lineTerminator = null)
{
lineTerminator ??= Environment.NewLine;
string result = "";
foreach (var line in Lines) {
result += $"{line.Buffer.ToString()}{lineTerminator}";
}
return result;
}
public void Deserialize(string source, Encoding? encoding = null, string? lineTerminator = null)
{
encoding ??= Encoding.ASCII;
lineTerminator ??= Environment.NewLine;
Lines.Clear();
string[] lines = source.Split(lineTerminator);
foreach (var line in lines) {
Lines.Add(new SelectableLineBuffer(new LineBuffer(line, encoding)));
}
}
}

View File

@ -1,15 +1,24 @@
using Qrakhen.Qamp.Editor.Commands;
using Qrakhen.Qamp.Core.Execution;
using Qrakhen.Qamp.Editor.Commands;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Input;
namespace Qrakhen.Qamp.Editor.ViewModel;
public class MainViewModel
public class MainViewModel : ObservableObject
{
public EditorFrameViewModel EditorFrame { get; set; }
private string _runnerOutput = "";
public string RunnerOutput
{
get => _runnerOutput;
set => SetProperty(ref _runnerOutput, value);
}
public ICommand RunCommand { get; set; }
public ICommand SaveCommand { get; set; }
public ICommand LoadCommand { get; set; }
@ -25,7 +34,16 @@ public class MainViewModel
private void ExecuteRun()
{
try {
string code = EditorFrame.Serialize();
Runner runner = new Runner(new Options((m) => { RunnerOutput += m; }));
MemoryStream stream = new MemoryStream();
stream.Write(EditorFrame.Encoding.GetBytes(code));
runner.Run(stream);
OnPropertyChanged(nameof(RunnerOutput));
} catch (Exception ex) {
RunnerOutput += $"\nERR: {ex.Message}\n";
}
}
private bool CanRun()