-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacro.go
More file actions
89 lines (78 loc) · 2.52 KB
/
macro.go
File metadata and controls
89 lines (78 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gcode
import (
"fmt"
"sync"
)
// Macro defines a named expansion that produces a sequence of Lines.
// Use SimpleMacro for a fixed sequence; implement Macro yourself to
// substitute values from args. The library does not provide template
// markers or expression evaluation — substitution logic is entirely
// the implementer's responsibility.
type Macro interface {
Name() string
Expand(args map[string]float64) ([]Line, error)
}
// SimpleMacro is a Macro backed by a fixed slice of Lines that ignores
// the args parameter. Use it for fixed command sequences (e.g.
// "preheat PLA") that need no parameter substitution.
type SimpleMacro struct {
name string
lines []Line
}
// NewSimpleMacro returns a SimpleMacro with the given name and lines.
// The provided lines are deep-copied so subsequent mutations to the
// caller's slice do not affect the macro.
func NewSimpleMacro(name string, lines ...Line) *SimpleMacro {
cp := make([]Line, len(lines))
for i, l := range lines {
cp[i] = l.Clone()
}
return &SimpleMacro{name: name, lines: cp}
}
// Name returns the macro's name.
func (m *SimpleMacro) Name() string { return m.name }
// Expand returns a deep copy of the stored lines. The args parameter
// is ignored.
func (m *SimpleMacro) Expand(_ map[string]float64) ([]Line, error) {
cp := make([]Line, len(m.lines))
for i, l := range m.lines {
cp[i] = l.Clone()
}
return cp, nil
}
// MacroRegistry is a collection of named macros.
// Lookup and Expand are safe for concurrent use; Register acquires a
// write lock and must not be called concurrently with itself.
type MacroRegistry struct {
mu sync.RWMutex
macros map[string]Macro
}
// NewMacroRegistry returns a new empty MacroRegistry.
func NewMacroRegistry() *MacroRegistry {
return &MacroRegistry{macros: make(map[string]Macro)}
}
// Register adds or replaces a macro in the registry and returns r so
// calls can be chained.
func (r *MacroRegistry) Register(m Macro) *MacroRegistry {
r.mu.Lock()
defer r.mu.Unlock()
r.macros[m.Name()] = m
return r
}
// Lookup returns the named macro and true, or nil and false if not found.
func (r *MacroRegistry) Lookup(name string) (Macro, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
m, ok := r.macros[name]
return m, ok
}
// Expand looks up the named macro and invokes its Expand method.
func (r *MacroRegistry) Expand(name string, args map[string]float64) ([]Line, error) {
r.mu.RLock()
m, ok := r.macros[name]
r.mu.RUnlock()
if !ok {
return nil, fmt.Errorf("gcode: macro %q not registered", name)
}
return m.Expand(args)
}