From 8122b0a299f6aa927e1628c5da8874b8af71452e Mon Sep 17 00:00:00 2001 From: Graeme Geldenhuys Date: Mon, 20 Apr 2026 18:01:03 +0100 Subject: [PATCH] Wire semantic analysis pass into the compiler pipeline The semantic analyser now runs between parsing and code generation. Type mismatches, undeclared identifiers, and duplicate declarations are reported to stderr with source position and exit code 1 before any IR is emitted. --- compiler/src/main/pascal/Blaise.pas | 32 +++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/compiler/src/main/pascal/Blaise.pas b/compiler/src/main/pascal/Blaise.pas index 6c44aab..07d39e1 100644 --- a/compiler/src/main/pascal/Blaise.pas +++ b/compiler/src/main/pascal/Blaise.pas @@ -16,7 +16,7 @@ program Blaise; uses SysUtils, Classes, Process, - uLexer, uParser, uAST, uCodeGenQBE; + uLexer, uParser, uAST, uSemantic, uCodeGenQBE; const Version = '0.1.0-alpha'; @@ -272,10 +272,11 @@ var SourceFile, OutputFile: string; EmitIR: Boolean; Source: TStringList; - Lexer: TLexer; - Parser: TParser; - Prog: TProgram; - CG: TCodeGenQBE; + Lexer: TLexer; + Parser: TParser; + Prog: TProgram; + Semantic: TSemanticAnalyser; + CG: TCodeGenQBE; IR: string; IRFile: string; @@ -315,10 +316,11 @@ begin end; end; - Lexer := nil; - Parser := nil; - Prog := nil; - CG := nil; + Lexer := nil; + Parser := nil; + Prog := nil; + Semantic := nil; + CG := nil; try try Lexer := TLexer.Create(Source.Text); @@ -332,6 +334,17 @@ begin end; end; + try + Semantic := TSemanticAnalyser.Create; + Semantic.Analyse(Prog); + except + on E: ESemanticError do + begin + WriteLn(StdErr, 'Semantic error: ', E.Message); + Halt(1); + end; + end; + try CG := TCodeGenQBE.Create; CG.Generate(Prog); @@ -345,6 +358,7 @@ begin end; finally CG.Free; + Semantic.Free; Prog.Free; Parser.Free; Lexer.Free;