diff --git a/src/SVM.Assembler.Core/Assembler.cs b/src/SVM.Assembler.Core/Assembler.cs index 6315046..4743e89 100644 --- a/src/SVM.Assembler.Core/Assembler.cs +++ b/src/SVM.Assembler.Core/Assembler.cs @@ -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 ?? ""}", 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 ?? ""} is not allowed here.", LexDef)); return operationResult; } intermediateInstruction.Parameters.Add(next.Result); diff --git a/src/SVM.Assembler.Core/Errors/ErrorWMsg.cs b/src/SVM.Assembler.Core/Errors/ErrorWMsg.cs new file mode 100644 index 0000000..56d73ee --- /dev/null +++ b/src/SVM.Assembler.Core/Errors/ErrorWMsg.cs @@ -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}"; + } + } +} diff --git a/src/SVM.Assembler.Core/Linker.cs b/src/SVM.Assembler.Core/Linker.cs index b9e1ffc..d9a149e 100644 --- a/src/SVM.Assembler.Core/Linker.cs +++ b/src/SVM.Assembler.Core/Linker.cs @@ -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 translate(InstructionDefinition def, LinkingContext context, IntermediateInstruction iinstruction, SVMInstruction* instruction) { OperationResult result = new OperationResult(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); diff --git a/src/SVM.Assembler/ISA.xml b/src/SVM.Assembler/ISA.xml index 54f492e..ecc099d 100644 --- a/src/SVM.Assembler/ISA.xml +++ b/src/SVM.Assembler/ISA.xml @@ -60,8 +60,16 @@ + - + + + + + + + + diff --git a/src/SVM.Assembler/Program.cs b/src/SVM.Assembler/Program.cs index 2604f8d..e8efebd 100644 --- a/src/SVM.Assembler/Program.cs +++ b/src/SVM.Assembler/Program.cs @@ -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; }