commit:ae9c4240cbd79c930b43047610a9b47c6479d15f
author:Chip
committer:Chip
date:Wed Apr 9 21:22:23 2025 -0500
parents:ce924fccf9c16684e5da8352de4796c2dbd14a25
Move error reporting and line buffer to parse.pas
diff --git a/compiler.pas b/compiler.pas
line changes: +1/-19
index 5e77a8a..f8b836a
--- a/compiler.pas
+++ b/compiler.pas
@@ -52,10 +52,6 @@ Parts = record
 end;
 DataDefType = (DataDB, DataDW, DataDD);
 
-var
-    currentLine: Integer;
-    linebuf: string;
-
 procedure PartsClear(var pts: Parts);
 begin
     pts.Kind := partsNone;
@@ -86,20 +82,6 @@ begin
     end;
 end;
 
-procedure ErrorAtLine(msg: string);
-begin
-    Writeln();
-    Writeln('ERR: ', msg,' on line ', currentLine, ':');
-    Writeln(linebuf);
-end;
-
-procedure WarnAtLine(msg: string);
-begin
-    Writeln();
-    Writeln('WARN: ', msg,' on line ', currentLine, ':');
-    Writeln(linebuf);
-end;
-
 constructor CompileBuffer.Init;
 begin
     New(data);
@@ -487,7 +469,7 @@ begin
         AddPublic(pts.args);
     end
     else if pts.mnemonic = 'EXTRN' then begin
-        Writeln('EXTRN not yet implemented');
+        ErrorAtLine('EXTRN not yet implemented');
         Halt(1);
     end
     else if pts.mnemonic = 'END' then begin

diff --git a/parse.pas b/parse.pas
line changes: +25/-6
index 80f5bb1..608ba42
--- a/parse.pas
+++ b/parse.pas
@@ -17,8 +17,13 @@ function ParseOperand(a: String; var o: Operand; const ot: OperandType): Boolean
 function MnemonicStr(const m: Mnemonic): string;
 function ShortOperandStr(const ot: OperandType): string;
 
+procedure ErrorAtLine(msg: string);
+procedure WarnAtLine(msg: string);
 procedure DieBadOperand(const a: String);
 
+var currentLine: Integer;
+    linebuf: string;
+
 implementation
 
 uses Compiler;
@@ -136,13 +141,13 @@ begin
     for i := 1 to Length(s) do begin
         if (s[i] = 'h') OR (s[i] = 'H') then begin
             if i < Length(s) then begin
-                Writeln('Trailing garbage in hex literal: ', s);
+                ErrorAtLine('Trailing garbage in hex literal: ' + s);
                 Halt(1);
             end;
             hex := True;
         end
         else if NOT (IsHexadecimal(s[i]) OR (s[i] = '-') OR (s[i] = '.')) then begin
-            Writeln('Invalid literal: ', s);
+            ErrorAtLine('Invalid literal: ' + s);
             Halt(1);
         end;
     end;
@@ -152,7 +157,7 @@ begin
         ss := Copy(s, 1, i);
     Val(ss, j, i);
     if i <> 0 then begin
-        Writeln('Invalid literal: ', s);
+        ErrorAtLine('Invalid literal: ' + s);
         Halt(1);
     end;
     ParseIntLiteral := Word(j);
@@ -232,7 +237,7 @@ begin
         if FindSymbol(currentObj^.symbols, w, j) then
             o.immed := j
         else begin
-            Writeln('Unknown symbol: ', a);
+            ErrorAtLine('Unknown symbol: ' + a);
             { DumpSymbols(currentObj^.symbols); }
             Halt(1);
         end;
@@ -250,7 +255,7 @@ begin
     { First try an identifier }
     l := ToUpCase(ConsumeIdentifier(a, i));
     if i < Length(a) + 1 then begin
-        Writeln('Malformed location? `', a, '`');
+        ErrorAtLine('Malformed location? `' + a + '`');
         Halt(1);
     end;
     if FindSymbol(currentObj^.symbols, l, o.loc) then begin
@@ -365,9 +370,23 @@ begin
     ShortOperandStr := ShortOperands[ot];
 end;
 
+procedure ErrorAtLine(msg: string);
+begin
+    Writeln();
+    Writeln('ERR: ', msg,' on line ', currentLine, ':');
+    Writeln(linebuf);
+end;
+
+procedure WarnAtLine(msg: string);
+begin
+    Writeln();
+    Writeln('WARN: ', msg,' on line ', currentLine, ':');
+    Writeln(linebuf);
+end;
+
 procedure DieBadOperand(const a: String);
 begin
-    Writeln('Invalid operand: ', a);
+    ErrorAtLine('Invalid operand: ' + a);
     {
     Write('  possible operands: ');
     for i := Low(OperandType) to High(OperandType) do

diff --git a/util/symtable.pas b/util/symtable.pas
line changes: +2/-2
index c3ee14a..5da70e7
--- a/util/symtable.pas
+++ b/util/symtable.pas
@@ -21,7 +21,7 @@ procedure DumpSymbols(const table: SymbolTable);
 
 implementation
 
-uses Util;
+uses Parse, Util;
 
 function GetTableEnd(const table: SymbolTable): PSymbolEntry;
 var p: PSymbolEntry;
@@ -41,7 +41,7 @@ var p: PSymbolEntry;
     new_entry: PSymbolEntry;
 begin
     if FindSymbolTableEntry(table, n) <> nil then begin
-        Writeln('Symbol ''', n, ''' already defined');
+        ErrorAtLine('Symbol ''' + n + ''' already defined');
         Halt(1);
     end;
     if table.head = nil then begin