summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-24 14:18:28 +0300
committerPaul Buetow <paul@buetow.org>2026-05-24 14:18:28 +0300
commit7095df85a86be4cb9dab952f91b42a4ae2f2d65e (patch)
tree7fefb051c76cec02f9b80405079d87d6347d75a3
parent9f83f4328f89ebd03c7455725e5d21fb4f4ff586 (diff)
fix(rpn): extract var-name type switch into helper (tj)
Extract the duplicated type switch on StackValue in AssignLeft() and AssignRight() into a single extractVarName() helper, closing the OCP violation — adding a new StackValue subtype as a valid variable name now only requires editing one function.
-rw-r--r--internal/rpn/operations_variables.go36
1 files changed, 16 insertions, 20 deletions
diff --git a/internal/rpn/operations_variables.go b/internal/rpn/operations_variables.go
index 56bf9cd..9f60133 100644
--- a/internal/rpn/operations_variables.go
+++ b/internal/rpn/operations_variables.go
@@ -97,6 +97,20 @@ func (o *Operations) ClearVariables() {
o.vars.ClearVariables()
}
+// extractVarName extracts a variable name from a StackValue.
+// Symbols use their internal name, StringNums use their string value,
+// and all other types fall back to String().
+func extractVarName(val StackValue) string {
+ switch v := val.(type) {
+ case *Symbol:
+ return v.Name()
+ case *StringNum:
+ return v.String()
+ default:
+ return val.String()
+ }
+}
+
// AssignLeft assigns a value to a variable (for =: operator).
// Stack order: value name =: (value on bottom, name on top).
// This function pops name first (top of stack), then value.
@@ -112,16 +126,7 @@ func (o *Operations) AssignLeft(stack *Stack) error {
return err
}
- // Get the variable name - handle Symbol, StringNum, or convert to string
- varName := ""
- switch v := name.(type) {
- case *Symbol:
- varName = v.Name()
- case *StringNum:
- varName = v.String()
- default:
- varName = name.String()
- }
+ varName := extractVarName(name)
valF, err := toFloat64(val, "assigning variable")
if err != nil {
@@ -145,16 +150,7 @@ func (o *Operations) AssignRight(stack *Stack) error {
return err
}
- // Get the variable name - handle Symbol, StringNum, or convert to string
- varName := ""
- switch v := name.(type) {
- case *Symbol:
- varName = v.Name()
- case *StringNum:
- varName = v.String()
- default:
- varName = name.String()
- }
+ varName := extractVarName(name)
valF, err := toFloat64(val, "assigning variable")
if err != nil {