port from perforce
This commit is contained in:
64
intromat/Intromat/ViewModels/CodePreviewViewModel.cs
Normal file
64
intromat/Intromat/ViewModels/CodePreviewViewModel.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Text;
|
||||
using Intromat.Model.Compiler;
|
||||
using Intromat.Model.Compiler.Error;
|
||||
using ReactiveUI;
|
||||
|
||||
namespace Intromat.ViewModels
|
||||
{
|
||||
public class CodePreviewViewModel : ReactiveObject
|
||||
{
|
||||
private readonly ObservableAsPropertyHelper<string> _compiledCode;
|
||||
|
||||
private IStatement? _code;
|
||||
|
||||
private string? _compilerError;
|
||||
|
||||
public CodePreviewViewModel()
|
||||
{
|
||||
this.WhenAnyValue(vm => vm.Code).Where(c => c != null)
|
||||
.Select(Compile)
|
||||
.ToProperty(this, vm => vm.CompiledCode, out _compiledCode);
|
||||
}
|
||||
|
||||
public IStatement? Code
|
||||
{
|
||||
get => _code;
|
||||
set => this.RaiseAndSetIfChanged(ref _code, value);
|
||||
}
|
||||
|
||||
public string? CompilerError
|
||||
{
|
||||
get => _compilerError;
|
||||
set => this.RaiseAndSetIfChanged(ref _compilerError, value);
|
||||
}
|
||||
|
||||
public string CompiledCode => _compiledCode.Value;
|
||||
|
||||
private string Compile(IStatement? c)
|
||||
{
|
||||
if (c == null)
|
||||
{
|
||||
CompilerError = "No code provided";
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
CompilerError = string.Empty;
|
||||
CompilerContext ctx = new();
|
||||
var sb = new StringBuilder();
|
||||
try
|
||||
{
|
||||
c.CompileHeader(ctx, sb);
|
||||
c.Compile(ctx, sb);
|
||||
return sb.ToString();
|
||||
}
|
||||
catch (CompilerException e)
|
||||
{
|
||||
string trace = string.Join("\n", ctx.VariablesScopesStack.Select(s => s.Identifier));
|
||||
CompilerError = e.Message + "\nProblem is near:\n" + trace;
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user