diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 22e0c83..77ef6c4 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -34,8 +34,28 @@ func (c *Compiler) Registry() *CommandRegistry { // Compile processes preprocessed lines and generates assembly output func (c *Compiler) Compile(lines []preproc.Line) ([]string, error) { var codeOutput []string + var lastKind = preproc.Source for _, line := range lines { + // Detect kind transitions and emit markers + if line.Kind != lastKind { + // Close previous block + if lastKind == preproc.Assembler { + codeOutput = append(codeOutput, "; ENDASM") + } else if lastKind == preproc.Script { + codeOutput = append(codeOutput, "; ENDSCRIPT") + } + + // Open new block + if line.Kind == preproc.Assembler { + codeOutput = append(codeOutput, "; ASM") + } else if line.Kind == preproc.Script { + codeOutput = append(codeOutput, "; SCRIPT") + } + + lastKind = line.Kind + } + // Skip non-source lines (assembler and script handled differently) if line.Kind != preproc.Source { if line.Kind == preproc.Assembler { @@ -88,6 +108,15 @@ func (c *Compiler) Compile(lines []preproc.Line) ([]string, error) { codeOutput = append(codeOutput, asmLines...) } + // Close any open block + if lastKind == preproc.Assembler { + //codeOutput = append(codeOutput, "; ENDASM") + return nil, fmt.Errorf("Unclosed ASM block.") + } else if lastKind == preproc.Script { + //codeOutput = append(codeOutput, "; ENDSCRIPT") + return nil, fmt.Errorf("Unclosed SCRIPT block.") + } + // Assemble final output with headers and footers return c.assembleOutput(codeOutput), nil }