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