Apply changes to a typed region as patches to its own header

Instrument.Patches works out the opcode changes that make region i read as an
updated Region, checking them against the reader, and Instrument.Apply makes
them in the document. Only the changed settings are touched, always in the
region's own header, so unknown opcodes and what other regions inherit stay as
they are. A change the header can't hold is an error and changes nothing.

FlatRegion gains Own, Inherited and Header to trace a region to its source, and
regionLines lists a region's opcodes with the field each sets.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:42:17 +00:00
co-authored by Claude Sonnet 5.5
parent 359de59033
commit 83c905633c
7 changed files with 704 additions and 102 deletions
+20 -6
View File
@@ -25,6 +25,12 @@ type FlatRegion struct {
// File and Line are where the <region> header is.
File string
Line int
// Own holds the opcodes of the region's own header, and Inherited those that
// it inherits, which Opcodes is the two of together.
Own, Inherited Opcodes
// Header is the <region> header, or nil if a variable made it, so that it
// has no node of its own.
Header *Header
}
// Flat is an instrument with its directives expanded and its headers resolved.
@@ -70,6 +76,7 @@ type target struct {
region bool
line int
file string
header *Header
}
// flattener reads a document's nodes in order.
@@ -104,7 +111,11 @@ func (f *flattener) header(file *File, n *Node, name string, variable bool) {
// A header that a variable made has no node of its own, so it takes the line
// of the node that made it.
line, _ := file.Position(n)
f.startHeader(name, line, file.Path)
var own *Header
if !variable {
own = &Header{File: file, Node: n}
}
f.startHeader(name, line, file, own)
}
func (f *flattener) opcode(_ *File, _ *Node, name, value string, _ bool) { f.setOpcode(name, value) }
@@ -121,11 +132,14 @@ func (f *flattener) closeRegion() {
control[name] = v
}
}
ops := control.with(f.levels[levelGlobal]).with(f.levels[levelMaster]).with(f.levels[levelGroup]).with(f.into.ops)
f.flat.Regions = append(f.flat.Regions, FlatRegion{Opcodes: ops, File: f.into.file, Line: f.into.line})
inherited := control.with(f.levels[levelGlobal]).with(f.levels[levelMaster]).with(f.levels[levelGroup])
f.flat.Regions = append(f.flat.Regions, FlatRegion{
Opcodes: inherited.with(f.into.ops), File: f.into.file, Line: f.into.line,
Own: f.into.ops, Inherited: inherited, Header: f.into.header,
})
}
func (f *flattener) startHeader(header string, line int, file string) {
func (f *flattener) startHeader(header string, line int, file *File, own *Header) {
f.closeRegion()
name := strings.ToLower(header)
switch name {
@@ -148,9 +162,9 @@ func (f *flattener) startHeader(header string, line int, file string) {
for l := lvl; l <= levelRegion; l++ {
f.levels[l] = Opcodes{}
}
f.into = &target{ops: f.levels[lvl], file: file}
f.into = &target{ops: f.levels[lvl], file: file.Path}
if lvl == levelRegion {
f.into.region, f.into.line = true, line
f.into.region, f.into.line, f.into.header = true, line, own
}
}
}