fix:修复前端bug (#83)
Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Reviewed-on: PostGuard/Canon#83 Co-authored-by: ichirinko <1621543655@qq.com> Co-committed-by: ichirinko <1621543655@qq.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Canon.Core.Abstractions;
|
||||
using Canon.Core.Exceptions;
|
||||
using Canon.Core.LexicalParser;
|
||||
using Canon.Core.SemanticParser;
|
||||
using Canon.Core.SyntaxNodes;
|
||||
@@ -6,6 +7,7 @@ using Canon.Server.DataTransferObjects;
|
||||
using Canon.Server.Entities;
|
||||
using Canon.Server.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace Canon.Server.Services;
|
||||
|
||||
@@ -13,7 +15,6 @@ public class CompilerService(
|
||||
ILexer lexer,
|
||||
IGrammarParser grammarParser,
|
||||
SyntaxTreeTraveller traveller,
|
||||
ICompilerLogger compilerLogger,
|
||||
CompileDbContext dbContext,
|
||||
GridFsService gridFsService,
|
||||
SyntaxTreePresentationService syntaxTreePresentationService,
|
||||
@@ -34,19 +35,44 @@ public class CompilerService(
|
||||
}
|
||||
|
||||
CodeReader reader = new(sourceCode);
|
||||
IEnumerable<SemanticToken> tokens = lexer.Tokenize(reader);
|
||||
ProgramStruct root = grammarParser.Analyse(tokens);
|
||||
|
||||
ProgramStruct root;
|
||||
try
|
||||
{
|
||||
IEnumerable<SemanticToken> tokens = lexer.Tokenize(reader);
|
||||
root = grammarParser.Analyse(tokens);
|
||||
}
|
||||
catch (CanonException e)
|
||||
{
|
||||
CompileResult errorResult = new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Error = true,
|
||||
SourceCode = sourceCode.Code,
|
||||
CompiledCode = string.Empty,
|
||||
SytaxTreeImageFilename = string.Empty,
|
||||
CompileTime = DateTime.Now,
|
||||
CompileInformation = e.Message
|
||||
};
|
||||
|
||||
await dbContext.CompileResults.AddAsync(errorResult);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return new CompileResponse(errorResult);
|
||||
}
|
||||
|
||||
await using Stream imageStream = syntaxTreePresentationService.Present(root);
|
||||
string filename = await gridFsService.UploadStream(imageStream, "image/png");
|
||||
|
||||
ICompilerLogger compilerLogger = new CompilerLogger();
|
||||
CodeGeneratorVisitor visitor = new(compilerLogger);
|
||||
traveller.Travel(root, visitor);
|
||||
|
||||
CompileResult result = new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Error = visitor.IsError,
|
||||
SourceCode = sourceCode.Code,
|
||||
CompileId = Guid.NewGuid().ToString(),
|
||||
CompiledCode = visitor.Builder.Build(),
|
||||
SytaxTreeImageFilename = filename,
|
||||
CompileTime = DateTime.Now,
|
||||
|
||||
Reference in New Issue
Block a user