Skip to content

Commit de5cdc7

Browse files
committed
refactor: remove unused SymbolTable, keep Levenshtein
SymbolTable was built for scope tracking and "did you mean?" suggestions but never integrated into the interpreter. Levenshtein is used in env.go for fuzzy matching, so move it to fuzzy.go where it belongs. If we need a symbol table later, we can rebuild it with the actual requirements in hand.
1 parent 9f0e414 commit de5cdc7

File tree

4 files changed

+65
-379
lines changed

4 files changed

+65
-379
lines changed

core/fuzzy.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,44 @@ import (
55
"unicode/utf8"
66
)
77

8+
// Levenshtein calculates the Levenshtein distance between two strings.
9+
// Used for "did you mean?" suggestions.
10+
func Levenshtein(a, b string) int {
11+
if len(a) == 0 {
12+
return len(b)
13+
}
14+
if len(b) == 0 {
15+
return len(a)
16+
}
17+
18+
// Create matrix
19+
matrix := make([][]int, len(a)+1)
20+
for i := range matrix {
21+
matrix[i] = make([]int, len(b)+1)
22+
matrix[i][0] = i
23+
}
24+
for j := 0; j <= len(b); j++ {
25+
matrix[0][j] = j
26+
}
27+
28+
// Fill matrix
29+
for i := 1; i <= len(a); i++ {
30+
for j := 1; j <= len(b); j++ {
31+
cost := 0
32+
if a[i-1] != b[j-1] {
33+
cost = 1
34+
}
35+
matrix[i][j] = min(
36+
matrix[i-1][j]+1, // deletion
37+
matrix[i][j-1]+1, // insertion
38+
matrix[i-1][j-1]+cost, // substitution
39+
)
40+
}
41+
}
42+
43+
return matrix[len(a)][len(b)]
44+
}
45+
846
// FuzzyMatchFold returns true if each character in source can be found in
947
// target in order, using case-insensitive matching. This is a simplified
1048
// fuzzy search - not Levenshtein distance.

core/fuzzy_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package core
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
48

59
func TestFuzzyMatchFold(t *testing.T) {
610
tests := []struct {
@@ -45,3 +49,25 @@ func TestFuzzyMatchFold(t *testing.T) {
4549
})
4650
}
4751
}
52+
53+
func TestLevenshtein(t *testing.T) {
54+
tests := []struct {
55+
a, b string
56+
expected int
57+
}{
58+
{"", "", 0},
59+
{"a", "", 1},
60+
{"", "a", 1},
61+
{"abc", "abc", 0},
62+
{"abc", "abd", 1},
63+
{"abc", "ab", 1},
64+
{"abc", "abcd", 1},
65+
{"kitten", "sitting", 3},
66+
{"foobar", "foobaz", 1},
67+
}
68+
69+
for _, tc := range tests {
70+
result := Levenshtein(tc.a, tc.b)
71+
assert.Equal(t, tc.expected, result, "Levenshtein(%q, %q)", tc.a, tc.b)
72+
}
73+
}

core/symbols.go

Lines changed: 0 additions & 239 deletions
This file was deleted.

0 commit comments

Comments
 (0)