Skip to content

Commit e7143ff

Browse files
authored
Merge pull request #854 from refaktor/dotwords
matrix value type, evaluator optimization, more internal test
2 parents 6b58238 + b3ac0da commit e7143ff

19 files changed

Lines changed: 3253 additions & 229 deletions

cmd/loop_benchmark/fib2.rye

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
fib: fn { n "recursive" } { either _< n 2 { n } { _+ fib _- n 1 fib _- n 2 } }
2+
fib: fn { n "recursive" } { either n < 2 { n } { _+ fib n - 1 fib n - 2 } }
33

44
print time-it { print fib 30 }
55

env/idxs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var NativeTypes = [...]string{ // Todo change to BuiltinTypes
6767
"Secret",
6868
"LazyValue",
6969
"Dotword",
70+
"Matrix",
7071
"PersistentTable",
7172
}
7273

env/object.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
//

evaldo/builtins.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,6 +2863,7 @@ func RegisterBuiltins(ps *env.ProgramState) {
28632863
RegisterBuiltins2(Builtins_match, ps, "match")
28642864
RegisterBuiltins2(Builtins_table, ps, "table")
28652865
RegisterBuiltins2(Builtins_vector, ps, "vector")
2866+
RegisterBuiltins2(Builtins_matrix, ps, "matrix")
28662867
RegisterBuiltins2(Builtins_io, ps, "io")
28672868
RegisterBuiltins2(Builtins_cmd, ps, "cmd")
28682869
RegisterBuiltins2(Builtins_regexp, ps, "regexp")
@@ -3085,6 +3086,7 @@ var allBuiltinGroups = []builtinGroup{
30853086
{"match", Builtins_match, false},
30863087
{"table", Builtins_table, false},
30873088
{"vector", Builtins_vector, false},
3089+
{"matrix", Builtins_matrix, false},
30883090
{"io", Builtins_io, false},
30893091
{"cmd", Builtins_cmd, false},
30903092
{"regexp", Builtins_regexp, false},

0 commit comments

Comments
 (0)