fix: Phase 3 — #25,28,29,41 token/AST/parser cleanup

Files modified (4):
  compiler/token/token.go — #25: Replace hand-rolled itoa with strconv.Itoa
    Fixes math.MinInt overflow bug in original implementation
  compiler/ast/ast.go — #29: Fix VarDecl.End() returning last var position
    Was returning Pos() (useless span info)
  compiler/parser/stmtreg.go — #28: Eliminate all 7 token array mutations
    rewriteAsIdent() modifies p.current only, not the token array
    Prevents backtracking corruption and improves safety
  compiler/lexer/lexer.go — Already clean from Phase 2

Issues resolved: #25 (MEDIUM), #28 (MEDIUM), #29 (MEDIUM), #41 partial (LOW)
Total fixed: 29/53

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-01 11:58:20 +09:00
parent f950cb0784
commit 6ffcf77dd8
4 changed files with 103 additions and 51 deletions

View File

@@ -6,6 +6,8 @@
// (ref/typescript-go/internal/ast/kind.go, precedence.go).
package token
import "strconv"
// Kind represents a token type. Using int16 following tsgo pattern.
type Kind int16
@@ -215,33 +217,12 @@ type Position struct {
func (p Position) String() string {
if p.File != "" {
return p.File + ":" + itoa(p.Line) + ":" + itoa(p.Col)
return p.File + ":" + strconv.Itoa(p.Line) + ":" + strconv.Itoa(p.Col)
}
return itoa(p.Line) + ":" + itoa(p.Col)
return strconv.Itoa(p.Line) + ":" + strconv.Itoa(p.Col)
}
// simple int-to-string without importing strconv
func itoa(n int) string {
if n == 0 {
return "0"
}
buf := [20]byte{}
i := len(buf) - 1
neg := n < 0
if neg {
n = -n
}
for n > 0 {
buf[i] = byte('0' + n%10)
i--
n /= 10
}
if neg {
buf[i] = '-'
i--
}
return string(buf[i+1:])
}
// itoa removed — using strconv.Itoa (fixes math.MinInt overflow bug)
// --- Operator Precedence (tsgo pattern) ---