diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-26 09:15:23 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-26 09:15:23 +0200 |
| commit | c5fe517d58df75acf33130e4bc20057b127934c4 (patch) | |
| tree | 8ba11e6a7be3dabb2bcea708c7d01bf79b26a98e | |
| parent | 1eb967082ac29d6833a87733ac5bbafd41399468 (diff) | |
fix: Handle boolean operators == and != correctly
| -rw-r--r-- | bug.txt | 15 | ||||
| -rw-r--r-- | internal/rpn/rpn_parse.go | 15 |
2 files changed, 12 insertions, 18 deletions
diff --git a/bug.txt b/bug.txt deleted file mode 100644 index 48811dd..0000000 --- a/bug.txt +++ /dev/null @@ -1,15 +0,0 @@ -Hi! - -The gt supports multiple ways to assign a variable, just the last example doesnt work. It should take 3 from the stack and assign it to x. Why doesnt it work? - -[I] paul@earth ~/g/gt (main)> ./gt -> x = 2 -x = 2 -> x 2 := -x = 2 -> 2 x =: -x = 2 -> 3 -3 -> x =: -> diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index 837e1c4..76edd40 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -56,8 +56,8 @@ func (r *RPN) evaluate(input string, tokens []string) (string, error) { stack := r.currentStack for i, token := range tokens { - // Check for variable assignment: name value = - if token == "=" { + // Check for variable assignment: name value = (but not == or != etc.) + if token == "=" && (i+1 >= len(tokens) || tokens[i+1] != "=") { return "", fmt.Errorf("rpn: invalid assignment syntax at token %d: 'name value =' requires spaces around =", i) } @@ -389,7 +389,16 @@ func (r *RPN) handleAssignment(input string) (string, bool, error) { } // Check for standard assignment format (name = value or name value = expression) - hasAssignment := strings.Contains(input, " = ") || strings.Contains(input, " =") + // Must check for " = " (with spaces) to avoid matching == or != + // The pattern "name value = expr..." or "name value =" requires " =" followed by non-= character + hasAssignment := strings.Contains(input, " = ") || strings.Contains(input, " =") + // Additional check: the = must not be followed by another = (i.e., not == or !=) + if hasAssignment && strings.Contains(input, "==") { + hasAssignment = false + } + if hasAssignment && strings.Contains(input, "!=") { + hasAssignment = false + } if !hasAssignment { return "", false, nil } |
