@@ -215,6 +215,13 @@ const (
215215 // .word
216216 // It acts as a method-style operator that binds tightly to its left subject.
217217 DotwordType Type = 50
218+ // Matrix is a constructed type
219+ // matrix 3 4
220+ // It represents a 2D matrix of float64 values.
221+ MatrixType Type = 51
222+ // CachedBuiltin wraps a Builtin with its original word mode (word/opword/pipeword)
223+ // Used for pre-resolved builtins in loops to avoid repeated lookups
224+ CachedBuiltinType Type = 52
218225 // PersistentTable is a constructed type
219226 // (internal)
220227 // It represents a persistent table.
@@ -1946,6 +1953,65 @@ func (i Builtin) Dump(e Idxs) string {
19461953 return ""
19471954}
19481955
1956+ //
1957+ // CACHED BUILTIN - Wraps a Builtin with its original word mode for optimized lookups
1958+ //
1959+
1960+ // CachedBuiltinMode indicates the original word type
1961+ const (
1962+ CachedModeWord = 0 // Regular word (prefix)
1963+ CachedModeOpword = 1 // Opword (infix like +)
1964+ CachedModePipeword = 2 // Pipeword (pipe like |fn)
1965+ CachedModeDotword = 3 // Dotword (method-like .fn)
1966+ )
1967+
1968+ // CachedBuiltin wraps a resolved Builtin with its original word semantics.
1969+ // Used in loops to avoid repeated context lookups while preserving
1970+ // infix/pipe/dot behavior.
1971+ type CachedBuiltin struct {
1972+ Builtin Builtin
1973+ Mode int // CachedModeWord, CachedModeOpword, etc.
1974+ Force int // For Opword/Pipeword: the Force flag from original word
1975+ }
1976+
1977+ func NewCachedBuiltin (builtin Builtin , mode int , force int ) * CachedBuiltin {
1978+ return & CachedBuiltin {builtin , mode , force }
1979+ }
1980+
1981+ func (b CachedBuiltin ) Type () Type {
1982+ return CachedBuiltinType
1983+ }
1984+
1985+ func (b CachedBuiltin ) Inspect (e Idxs ) string {
1986+ modes := []string {"word" , "opword" , "pipeword" , "dotword" }
1987+ return "[CachedBuiltin(" + modes [b .Mode ] + "): " + b .Builtin .Doc + "]"
1988+ }
1989+
1990+ func (b CachedBuiltin ) Print (e Idxs ) string {
1991+ return b .Builtin .Print (e )
1992+ }
1993+
1994+ func (b CachedBuiltin ) Trace (msg string ) {
1995+ fmt .Print (msg + " (cached-builtin): " )
1996+ fmt .Println (b .Builtin .Argsn )
1997+ }
1998+
1999+ func (b CachedBuiltin ) GetKind () int {
2000+ return int (CachedBuiltinType )
2001+ }
2002+
2003+ func (b CachedBuiltin ) Equal (o Object ) bool {
2004+ if b .Type () != o .Type () {
2005+ return false
2006+ }
2007+ other := o .(CachedBuiltin )
2008+ return b .Mode == other .Mode && b .Builtin .Equal (other .Builtin )
2009+ }
2010+
2011+ func (b CachedBuiltin ) Dump (e Idxs ) string {
2012+ return ""
2013+ }
2014+
19492015//
19502016// ERROR
19512017//
@@ -2985,6 +3051,98 @@ func (i Vector) Dump(e Idxs) string {
29853051 return b .String ()
29863052}
29873053
3054+ //
3055+ // MATRIX
3056+ //
3057+
3058+ // Matrix represents a 2D matrix of float64 values (row-major storage)
3059+ // Element at (i,j) is stored at Data[i*Cols + j]
3060+ type Matrix struct {
3061+ Data []float64
3062+ Rows int
3063+ Cols int
3064+ Kind Word
3065+ }
3066+
3067+ // NewMatrix creates a new zero-initialized matrix with the given dimensions
3068+ func NewMatrix (rows , cols int ) * Matrix {
3069+ return & Matrix {
3070+ Data : make ([]float64 , rows * cols ),
3071+ Rows : rows ,
3072+ Cols : cols ,
3073+ Kind : Word {0 },
3074+ }
3075+ }
3076+
3077+ // NewMatrixWithData creates a new matrix with the given data (row-major order)
3078+ func NewMatrixWithData (rows , cols int , data []float64 ) * Matrix {
3079+ if len (data ) != rows * cols {
3080+ return nil
3081+ }
3082+ return & Matrix {
3083+ Data : data ,
3084+ Rows : rows ,
3085+ Cols : cols ,
3086+ Kind : Word {0 },
3087+ }
3088+ }
3089+
3090+ // Get returns element at row i, column j (0-indexed)
3091+ func (m Matrix ) Get (i , j int ) float64 {
3092+ return m .Data [i * m .Cols + j ]
3093+ }
3094+
3095+ // Set sets element at row i, column j (0-indexed)
3096+ func (m * Matrix ) Set (i , j int , val float64 ) {
3097+ m .Data [i * m .Cols + j ] = val
3098+ }
3099+
3100+ func (m Matrix ) Type () Type {
3101+ return MatrixType
3102+ }
3103+
3104+ func (m Matrix ) Inspect (e Idxs ) string {
3105+ return fmt .Sprintf ("[Matrix: %dx%d]" , m .Rows , m .Cols )
3106+ }
3107+
3108+ func (m Matrix ) Print (e Idxs ) string {
3109+ return fmt .Sprintf ("M[%dx%d]" , m .Rows , m .Cols )
3110+ }
3111+
3112+ func (m Matrix ) Trace (msg string ) {
3113+ fmt .Printf ("%s(Matrix): %dx%d\n " , msg , m .Rows , m .Cols )
3114+ }
3115+
3116+ func (m Matrix ) GetKind () int {
3117+ return int (MatrixType )
3118+ }
3119+
3120+ func (m Matrix ) Equal (o Object ) bool {
3121+ if m .Type () != o .Type () {
3122+ return false
3123+ }
3124+ other := o .(Matrix )
3125+ if m .Rows != other .Rows || m .Cols != other .Cols {
3126+ return false
3127+ }
3128+ for i := range m .Data {
3129+ if m .Data [i ] != other .Data [i ] {
3130+ return false
3131+ }
3132+ }
3133+ return true
3134+ }
3135+
3136+ func (m Matrix ) Dump (e Idxs ) string {
3137+ var b strings.Builder
3138+ b .WriteString (fmt .Sprintf ("matrix %d %d { " , m .Rows , m .Cols ))
3139+ for _ , v := range m .Data {
3140+ b .WriteString (fmt .Sprintf ("%f " , v ))
3141+ }
3142+ b .WriteString ("}" )
3143+ return b .String ()
3144+ }
3145+
29883146//
29893147// VARBUILTIN FUNCTION
29903148//
0 commit comments