Files
five/hbrt/macro.go
Charles KWON OhJun 207fa9f7dd fix: Phase 1 Step 0 cleanup + CRITICAL #3, MEDIUM #36-37, LOW #50
Files modified (5):
  hbrt/macro.go — Replace hand-rolled parseFloat/parseInt64 with strconv (#50)
                   Remove stale TODO, redundant TrimSpace
  hbrt/macroeval.go — Use strconv for literal parsing (was using removed functions)
  hbrt/class.go — CRITICAL #3: Change RWMutex to Mutex on classList
                   Prevents slice reallocation race on concurrent GetClass
  hbrt/goroutine.go — #36: Channel double-close protection (sync.Once)
                       #37: Send on closed channel recovery (defer/recover)
                       Add IsClosed(), safe Receive (handles closed channel)
  hbrt/gobridge.go — Already clean (confirmed)
  hbrt/hbfunc.go — Already clean (confirmed)

Issues resolved: #3 (CRITICAL), #36, #37 (MEDIUM), #50 (LOW)
Total fixed: 16/53

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 10:51:20 +09:00

115 lines
3.2 KiB
Go

// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
// All rights reserved.
// Runtime macro compiler for Five.
// Implements &variable and &(expression) — runtime code compilation.
//
// Harbour has a full macro compiler (src/macro/macro.y) that parses
// and compiles expressions at runtime. Five uses a simplified approach:
// parse the expression string, then evaluate it using the existing
// lexer/parser/evaluator infrastructure.
//
// Usage:
// LOCAL cField := "salary"
// ? &cField → evaluates variable named "salary"
// ? &(cField + "_new") → evaluates variable named "salary_new"
//
// Reference: /mnt/d/harbour-core/src/macro/
package hbrt
import (
"strconv"
"strings"
)
// MacroCompile compiles and evaluates a macro expression string.
// Returns the result value.
//
// For simple variable references (&cVar):
// Looks up the variable name in memvars/locals.
//
// For complex expressions (&(expr)):
// Would need full expression parser — simplified for now.
func (t *Thread) MacroCompile(expr string) Value {
expr = strings.TrimSpace(expr)
if expr == "" {
return MakeNil()
}
// Simple case: expression is a variable name
// Look up in memvars first, then try as function call
if isSimpleIdent(expr) {
// Try calling as a function (memvar lookup deferred to MacroEval)
sym := t.vm.FindSymbol(strings.ToUpper(expr))
if sym != nil && sym.Func != nil {
t.PushSymbol(sym)
t.PushNil()
t.Function(0)
return t.pop()
}
return MakeString(expr) // return as string if not found
}
// Complex expression: try parsing as number, then as function call
// Full runtime expression parser would be needed for complete macro support.
// This handles common patterns: &("literal"), &(numericExpr)
// Try numeric (use stdlib strconv)
if len(expr) > 0 && (expr[0] >= '0' && expr[0] <= '9' || expr[0] == '-' || expr[0] == '+') {
if strings.Contains(expr, ".") {
if f, err := strconv.ParseFloat(expr, 64); err == nil {
return MakeDoubleAuto(f)
}
} else {
if n, err := strconv.ParseInt(expr, 10, 64); err == nil {
return MakeNumInt(n)
}
}
}
// Try string literal
if len(expr) >= 2 && (expr[0] == '"' && expr[len(expr)-1] == '"' || expr[0] == '\'' && expr[len(expr)-1] == '\'') {
return MakeString(expr[1 : len(expr)-1])
}
// Try .T./.F.
upper := strings.ToUpper(expr)
if upper == ".T." {
return MakeBool(true)
}
if upper == ".F." {
return MakeBool(false)
}
// Return as string (field name, variable name, etc.)
return MakeString(expr)
}
// MacroPush compiles a macro and pushes the result on stack.
// Harbour: HB_P_MACROPUSH
func (t *Thread) MacroPush() {
exprVal := t.pop()
result := t.MacroCompile(exprVal.AsString())
t.push(result)
}
// parseFloat and parseInt64 removed — using strconv.ParseFloat/ParseInt instead.
// isSimpleIdent checks if string is a valid simple identifier.
func isSimpleIdent(s string) bool {
if len(s) == 0 {
return false
}
ch := s[0]
if !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_') {
return false
}
for i := 1; i < len(s); i++ {
ch = s[i]
if !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_') {
return false
}
}
return true
}