From a7d410dfe5a5b8d7599ff99aee03268fcdfaec10 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 7 Sep 2021 17:04:20 +0200 Subject: [PATCH] Better debugging --- BasicNumber.cs | 3 +++ Parser.cs | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/BasicNumber.cs b/BasicNumber.cs index 0ab63c4..f2b6c4b 100644 --- a/BasicNumber.cs +++ b/BasicNumber.cs @@ -56,5 +56,8 @@ namespace SuperBASIC return runtime.GetRegister(); } } + + public static implicit operator float(BasicNumber v) => v.GetValue(); + public override string ToString() => $"{number}"; } } diff --git a/Parser.cs b/Parser.cs index f0716e3..8cd9eb5 100644 --- a/Parser.cs +++ b/Parser.cs @@ -36,26 +36,34 @@ namespace SuperBASIC Regex leadings = new Regex(@"^\s+"); Regex trailings = new Regex(@"\s+$"); List codeLines = new List(); + List lineSpans = new List(); + int a = 0; foreach (string line in sourceLines) { + a++; string l = lws.Replace(line, " "); l = leadings.Replace(l, ""); l = trailings.Replace(l, ""); if(l != String.Empty) { + lineSpans.Add(a); + a = 0; codeLines.Add(l); } } - foreach(string line in codeLines) + for(int idx = 0; idx < codeLines.Count; idx++) { + string line = codeLines[idx]; var components = line.Split(' '); if(!library.nameResolution.ContainsKey(components[0])) { - throw new ParseException("Unknown operation \"" + components[0] + "\""); + int lineIndex = 0; + foreach (int cnt in lineSpans.GetRange(0, idx)) lineIndex += cnt; + throw new ParseException($"Unknown operation \"{components[0]}\"\n\tat line {lineIndex}"); } int opcode = library.nameResolution[components[0]]; @@ -63,7 +71,9 @@ namespace SuperBASIC if(arity != components.Length-1) { - throw new ParseException("Operation " + components[0] + " was provided with the wrong number of arguments: Expected "+arity.ToString()+" found "+(components.Length-1).ToString()); + int lineIndex = 0; + foreach (int cnt in lineSpans.GetRange(0, idx)) lineIndex += cnt; + throw new ParseException($"Operation {components[0]} was provided with the wrong number of arguments\n\tExpected {arity} found {components.Length-1}\n\tat line {lineIndex}"); } c.bytecode.Add(new BasicNumber(runtime, opcode));