feat-visualization (#33)

尽管里面有点粪,但还是生成了漂亮的树,就当给树施肥了
剩下一个小问题:未考虑节点的宽度(因为名称不一样长),但目前未发现对图生成的明显影响

Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Reviewed-on: PostGuard/Canon#33
Co-authored-by: Ichirinko <1621543655@qq.com>
Co-committed-by: Ichirinko <1621543655@qq.com>
This commit is contained in:
Ichirinko
2024-04-19 14:59:45 +08:00
committed by jackfiled
parent 4b6635796c
commit dbbab1c761
44 changed files with 4689 additions and 4 deletions

View File

@@ -0,0 +1,85 @@
using System.Diagnostics.CodeAnalysis;
using Canon.Core.Abstractions;
namespace Canon.Server.Models;
public class CodeReader(SourceCode code) : ISourceReader
{
private int _pos = -1;
private uint _lastPos;
public uint Line { get; private set; } = 1;
public uint Pos { get; private set; }
public string FileName => "string";
public char Current
{
get
{
if (_pos == -1)
{
throw new InvalidOperationException("Reader at before the start.");
}
else
{
return code.Code[_pos];
}
}
}
public bool Retract()
{
if (_pos <= 0)
{
return false;
}
_pos -= 1;
if (Current == '\n')
{
Line -= 1;
// TODO: 如果一直回退就完蛋了
Pos = _lastPos;
}
else
{
Pos -= 1;
}
return true;
}
public bool MoveNext()
{
if (_pos >= code.Code.Length - 1)
{
return false;
}
if (_pos != -1 && Current == '\n')
{
Line += 1;
_lastPos = Pos;
Pos = 0;
}
_pos += 1;
Pos += 1;
return true;
}
public bool TryPeekChar([NotNullWhen(true)] out char? c)
{
if (_pos >= code.Code.Length - 1)
{
c = null;
return false;
}
c = code.Code[_pos + 1];
return true;
}
}

View File

@@ -0,0 +1,34 @@
using System.ComponentModel.DataAnnotations;
namespace Canon.Server.Models;
public class CompileResponse
{
[Required]
public string Id { get; set; }
[Required]
public string SourceCode { get; set; }
[Required]
public string CompiledCode { get; set; }
[Required]
public string ImageAddress { get; set; }
public CompileResponse()
{
Id = string.Empty;
SourceCode = string.Empty;
CompiledCode = string.Empty;
ImageAddress = string.Empty;
}
public CompileResponse(CompileResult result)
{
Id = result.CompileId;
SourceCode = result.SourceCode;
CompiledCode = result.CompiledCode;
ImageAddress = $"/api/file/{result.SytaxTreeImageFilename}";
}
}

View File

@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
using MongoDB.Bson;
namespace Canon.Server.Models;
public class CompileResult
{
public ObjectId Id { get; set; }
[MaxLength(40)]
public string CompileId { get; set; } = string.Empty;
public string SourceCode { get; set; } = string.Empty;
[MaxLength(40)]
public string SytaxTreeImageFilename { get; set; } = string.Empty;
public string CompiledCode { get; set; } = string.Empty;
}

View File

@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace Canon.Server.Models;
public class ImageResponse
{
[Required]
public required string ResultId { get; set; }
}

View File

@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;
namespace Canon.Server.Models;
public class SourceCode
{
[Required]
public required string Code { get; set; }
}