mirror of
https://github.com/creeperlv/SVM.git
synced 2026-01-10 20:39:54 +00:00
Added a brunch of error messages and fixed a lot of things.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using LibCLCC.NET.Lexer;
|
||||
using LibCLCC.NET.Operations;
|
||||
using SVM.Assembler.Core.Errors;
|
||||
using SVM.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -13,9 +14,9 @@ namespace SVM.Assembler.Core
|
||||
@"
|
||||
Match:
|
||||
|
||||
D \d
|
||||
Number {D}+
|
||||
InstMath bmath
|
||||
InstSDInt32 sd\.int32
|
||||
InstSDInt sd\.int
|
||||
InstCvt cvt
|
||||
InstSystem system
|
||||
InstSys sys
|
||||
@@ -24,15 +25,19 @@ Register \${D}+
|
||||
LabelCode \.code\:
|
||||
LabelData \.data\:
|
||||
LabelConst \.const\:
|
||||
word [\w\d]+
|
||||
word [\w\d\.]+
|
||||
GenericLabel {word}\:
|
||||
LineEnd \n
|
||||
string ""\"".*\""""
|
||||
Number \d+
|
||||
D \d
|
||||
|
||||
|
||||
Id:
|
||||
word Word
|
||||
InstMath inst
|
||||
InstSDInt inst
|
||||
InstSDInt32 inst
|
||||
InstCvt inst
|
||||
InstSystem inst
|
||||
InstSys inst
|
||||
@@ -75,6 +80,7 @@ LabelConstant InternalLbl
|
||||
if (r.Result.LexSegmentId != null && r.Result.LexMatchedItemId != null)
|
||||
{
|
||||
if (r.Result.LexSegmentId == "LE") continue;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -119,7 +125,7 @@ LabelConstant InternalLbl
|
||||
}
|
||||
if (!ISA.InstructionDefinitionAliases.TryGetValue(LexDef.Content ?? "", out var instructionDef))
|
||||
{
|
||||
operationResult.AddError(new Error());
|
||||
operationResult.AddError(new ErrorWMsg($"Unknown Instruction:{LexDef.Content ?? "<null>"}", LexDef));
|
||||
return operationResult;
|
||||
}
|
||||
intermediateInstruction.inst = instructionDef.PrimaryInstruction;
|
||||
@@ -140,6 +146,7 @@ LabelConstant InternalLbl
|
||||
}
|
||||
if (!item.AllowedTokenIds.Contains(next.Result.LexSegmentId))
|
||||
{
|
||||
operationResult.AddError(new ErrorWMsg($"Token: {LexDef.Content ?? "<null>"} is not allowed here.", LexDef));
|
||||
return operationResult;
|
||||
}
|
||||
intermediateInstruction.Parameters.Add(next.Result);
|
||||
|
||||
27
src/SVM.Assembler.Core/Errors/ErrorWMsg.cs
Normal file
27
src/SVM.Assembler.Core/Errors/ErrorWMsg.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using LibCLCC.NET.Lexer;
|
||||
using LibCLCC.NET.Operations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SVM.Assembler.Core.Errors
|
||||
{
|
||||
public class ErrorWMsg : Error
|
||||
{
|
||||
public String Message;
|
||||
public LexSegment? Pos;
|
||||
public ErrorWMsg(string message, LexSegment? pos = null)
|
||||
{
|
||||
Message = message;
|
||||
Pos = pos;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Pos == null)
|
||||
return Message;
|
||||
else
|
||||
return Message + $" At: {Pos.SourceID}:{Pos.Position}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
using LibCLCC.NET.Operations;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
using SVM.Assembler.Core.Errors;
|
||||
using SVM.Core;
|
||||
using SVM.Core.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text;
|
||||
|
||||
@@ -262,6 +264,7 @@ namespace SVM.Assembler.Core
|
||||
public unsafe static OperationResult<bool> translate(InstructionDefinition def, LinkingContext context, IntermediateInstruction iinstruction, SVMInstruction* instruction)
|
||||
{
|
||||
OperationResult<bool> result = new OperationResult<bool>(false);
|
||||
((IntPtr)instruction).SetData((byte)def.PrimaryInstruction);
|
||||
//SVMInstruction instruction = new SVMInstruction();
|
||||
for (int i = 0; i < iinstruction.Parameters.Count; i++)
|
||||
{
|
||||
@@ -270,6 +273,7 @@ namespace SVM.Assembler.Core
|
||||
string converter = paraDef.ExpectdValue.Converter;
|
||||
if (para.Content == null)
|
||||
{
|
||||
result.AddError(new ErrorWMsg($"{para.Content} (Parameter {i}) have no content!", para));
|
||||
return result;
|
||||
}
|
||||
if (converter.StartsWith("InternalEnum:"))
|
||||
@@ -291,6 +295,7 @@ namespace SVM.Assembler.Core
|
||||
{
|
||||
if (!TryParseRegister(para.Content, context, out var registerID))
|
||||
{
|
||||
result.AddError(new ErrorWMsg($"{para.Content} cannot be parsed to Register!", para));
|
||||
return result;
|
||||
}
|
||||
WriteData(instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, ®isterID);
|
||||
@@ -300,6 +305,7 @@ namespace SVM.Assembler.Core
|
||||
{
|
||||
if (!TryParseInt32(para.Content, context, out var registerID))
|
||||
{
|
||||
result.AddError(new ErrorWMsg($"{para.Content} cannot be parsed to Integer32!", para));
|
||||
return result;
|
||||
}
|
||||
WriteData(instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, (byte*)®isterID);
|
||||
|
||||
@@ -60,8 +60,16 @@
|
||||
<InstructionParameter>
|
||||
<MatchingItems>
|
||||
<Item Id="Word"/>
|
||||
<Item Id="Register"/>
|
||||
</MatchingItems>
|
||||
<ExpectedValue Type="Int32" Pos="8" Converter="Ingeter32" />
|
||||
<ExpectedValue Type="UInt8" Pos="1" Converter="Register" />
|
||||
</InstructionParameter>
|
||||
<InstructionParameter>
|
||||
<MatchingItems>
|
||||
<Item Id="Number"/>
|
||||
<Item Id="Word"/>
|
||||
</MatchingItems>
|
||||
<ExpectedValue Type="Int32" Pos="8" Converter="Integer32" />
|
||||
</InstructionParameter>
|
||||
</Parameters>
|
||||
</InstructionDefinition>
|
||||
|
||||
@@ -40,12 +40,20 @@ namespace SVM.Assembler
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (files.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Assembler.Core.Assembler assembler = new Core.Assembler(def);
|
||||
foreach (var item in files)
|
||||
{
|
||||
var result = assembler.AssembleIntermediateObject(File.ReadAllText(item), item);
|
||||
if (result.HasError())
|
||||
{
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
Console.Error.WriteLine(error.ToString());
|
||||
}
|
||||
Console.Error.WriteLine($"Error at assembling {item}. Abort.");
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user