Skip to content

Commit

Permalink
Wrap operations with source info
Browse files Browse the repository at this point in the history
Instead of embedding every operation with metadata and having to be careful to
set it when operations are parsed, we wrap all operations a SourceInfo. It
embeds the Operation interface and otherwise delegates to it. Taking a hint from
the error interface, callers can Unwrap to get the original Operation.

Squashed commit of 4cd9a6d..1b508bb.
  • Loading branch information
smoynes committed Oct 10, 2023
1 parent cb162e3 commit 392193d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 134 deletions.
17 changes: 11 additions & 6 deletions internal/asm/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,22 @@ type Operation interface {
// Generate encodes an operation as machine code. Using the values from Parse, the operation is
// converted to one (or more) words.
Generate(symbols SymbolTable, pc uint16) ([]uint16, error)

// Source returns information about the source code location of an operation.
Source() SourceInfo
}

// SourceInfo holds information on the source of an operation.
// SourceInfo wraps an operation to annotate it with parser metadata.
type SourceInfo struct {
Filename string
Pos uint16
Line string

Operation
}

// Source returns a copy of the source information.
func (s SourceInfo) Source() SourceInfo { return s }
// Unwrap returns the operation which the source info wraps.
func (si *SourceInfo) Unwrap() Operation {
if si.Operation == nil {
return nil
}

return si.Operation
}
46 changes: 27 additions & 19 deletions internal/asm/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,37 @@ func (gen *Generator) WriteTo(out io.Writer) (int64, error) {
return 0, nil
}

// Write the object-code header: the origin offset. The .ORIG directive should be the first
// operation in the syntax table.
if orig := origin(gen.syntax[0]); orig == nil {
return 0, errors.New("gen: .ORIG directive must be the first operation")
} else {
// Write the origin offset as the leader of the object file. The .ORIG directive should be the
// first operation in the syntax table. However, operations may be wrapped, so we unwrap to the
// base case, first.
if orig, ok := unwrap(gen.syntax[0]).(*ORIG); ok {
gen.pc = orig.LITERAL
gen.log.Debug("Wrote object header", "ORIG", fmt.Sprintf("%0#4x", orig.LITERAL))
} else {
return 0, fmt.Errorf(".ORIG should be first operation; was: %T", gen.syntax[0])
}

for i, code := range gen.syntax {
if code == nil {
continue
} else if origin(code) != nil && i != 0 {
err = errors.New("gen: .ORIG directive may only be the first operation")
} else if _, ok := (unwrap(code)).(*ORIG); ok && i != 0 {
err = errors.New(".ORIG directive may only be the first operation")
break
}

encoded, err = code.Generate(gen.symbols, gen.pc)

if err != nil {
src := code.Source()
err = &SyntaxError{
File: src.Filename,
Loc: gen.pc,
Pos: src.Pos,
Line: src.Line,
Err: err,
// If code generation caused an error, we try to get the source of the operation and
// covert annotate the error with the source code annotation.
if src, ok := code.(*SourceInfo); ok {
err = &SyntaxError{
File: src.Filename,
Loc: gen.pc,
Pos: src.Pos,
Line: src.Line,
Err: err,
}
}

break
Expand All @@ -94,10 +99,13 @@ func (gen *Generator) WriteTo(out io.Writer) (int64, error) {
return count, nil
}

func origin(op Operation) *ORIG {
if op, ok := op.(*ORIG); ok {
return op
// unwrap returns the base operation from possibly wrapped operation.
func unwrap(oper Operation) Operation {
for {
if wrap, ok := oper.(interface{ Unwrap() Operation }); ok {
oper = wrap.Unwrap()
} else {
return oper
}
}

return nil
}
2 changes: 1 addition & 1 deletion internal/asm/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestGenerator(tt *testing.T) {
var buf bytes.Buffer

syntax := make(SyntaxTable, 0)

syntax.Add(&ORIG{LITERAL: 0x3000})
syntax.Add(&NOT{DR: "R0", SR: "R7"})
syntax.Add(&AND{DR: "R3", SR1: "R4", SR2: "R6"})
Expand All @@ -97,7 +98,6 @@ func TestGenerator(tt *testing.T) {
symbols.Add("LABEL", 0x2ff0)

gen := NewGenerator(symbols, syntax)

count, err := gen.WriteTo(&buf)
if err != nil {

Check failure on line 102 in internal/asm/gen_test.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
t.Error(err)
Expand Down
Loading

0 comments on commit 392193d

Please sign in to comment.