Skip to content

Commit

Permalink
Add support for merging blocks into dicts, and block and dicts into s…
Browse files Browse the repository at this point in the history
…preadsheet rows
  • Loading branch information
yumaikas committed Dec 2, 2024
1 parent 3337e57 commit ba8c6c1
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 12 deletions.
1 change: 0 additions & 1 deletion env/idxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type Idxs struct {
}

var NativeTypes = [...]string{ // Todo change to BuiltinTypes
"INVALID",
"Block",
"Integer",
"Word",
Expand Down
36 changes: 36 additions & 0 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,42 @@ func NewDict(data map[string]any) *Dict {
return &Dict{data, Word{0}}
}

func MergeTwoDicts(base Dict, toAdd Dict) Dict {
data := make(map[string]any)
for k, v := range base.Data {
data[k] = v
}
for k, v := range toAdd.Data {
data[k] = v
}
return Dict{data, Word{0}}
}

func MergeDictAndBlock(base Dict, updatesBlock TSeries, idx *Idxs) Dict {
data := make(map[string]any)
for k, v := range base.Data {
data[k] = v
}

for updatesBlock.Pos() < updatesBlock.Len() {
key := updatesBlock.Pop()
val := updatesBlock.Pop()
// v001 -- only process the typical case of string val
switch k := key.(type) {
case String:
data[k.Value] = val
case Tagword:
data[idx.GetWord(k.Index)] = val
case Word:
data[idx.GetWord(k.Index)] = val
case Setword:
data[idx.GetWord(k.Index)] = val
}
}
return Dict{data, Word{0}}

}

func NewDictFromSeries(block TSeries, idx *Idxs) Dict {
data := make(map[string]any)
for block.Pos() < block.Len() {
Expand Down
37 changes: 37 additions & 0 deletions env/spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ func NewSpreadsheetRow(values []any, uplink *Spreadsheet) *SpreadsheetRow {
return &nat
}

func AddSpreadsheetRowAndBlock(row SpreadsheetRow, updatesBlock TSeries, idx *Idxs) SpreadsheetRow {
data := make(map[string]any)

for updatesBlock.Pos() < updatesBlock.Len() {
key := updatesBlock.Pop()
val := updatesBlock.Pop()
// v001 -- only process the typical case of string val
switch k := key.(type) {
case String:
data[k.Value] = val
case Tagword:
data[idx.GetWord(k.Index)] = val
case Word:
data[idx.GetWord(k.Index)] = val
case Setword:
data[idx.GetWord(k.Index)] = val
}
}

return AddSpreadsheetRowAndMap(row, data)
}

func AddSpreadsheetRowAndDict(row SpreadsheetRow, dict Dict) SpreadsheetRow {
return AddSpreadsheetRowAndMap(row, dict.Data)
}

func AddSpreadsheetRowAndMap(row SpreadsheetRow, dict map[string]any) SpreadsheetRow {
newRow := SpreadsheetRow{row.Values, row.Uplink}

for i, v := range row.Uplink.Cols {
if val, ok := dict[v]; ok {
newRow.Values[i] = val
}
}
return newRow
}

func SpreadsheetRowFromDict(dict Dict, uplink *Spreadsheet) (bool, string, *SpreadsheetRow) {
row := SpreadsheetRow{make([]any, len(uplink.Cols)), uplink}
for i, v := range uplink.Cols {
Expand Down
20 changes: 20 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,8 @@ var builtins = map[string]*env.Builtin{
// equal { "A" + "b" } "Ab"
// equal { "A" + 1 } "A1"
// equal { { 1 2 } + { 3 4 } } { 1 2 3 4 }
// equal { dict { "a" 1 } |+ { "b" 2 } } dict { "a" 1 "b" 2 }
// equal { dict { "a" 1 } |+ dict { "b" 2 } } dict { "a" 1 "b" 2 }
"_+": { // **
Argsn: 2,
Doc: "Adds or joins two values together (Integers, Strings, Uri-s and Blocks)",
Expand Down Expand Up @@ -1455,6 +1457,24 @@ var builtins = map[string]*env.Builtin{
default:
return MakeBuiltinError(ps, "Value in Block is not block type.", "_+")
}
case env.Dict:
switch b2 := arg1.(type) {
case env.Dict:
return env.MergeTwoDicts(s1, b2)
case env.Block:
return env.MergeDictAndBlock(s1, b2.Series, ps.Idx)
default:
return MakeArgError(ps, 2, []env.Type{env.DictType, env.BlockType}, "_+")
}
case env.SpreadsheetRow:
switch b2 := arg1.(type) {
case env.Dict:
return env.AddSpreadsheetRowAndDict(s1, b2)
case env.Block:
return env.AddSpreadsheetRowAndBlock(s1, b2.Series, ps.Idx)
default:
return MakeArgError(ps, 2, []env.Type{env.DictType, env.BlockType}, "_+")
}
case env.Time:
switch b2 := arg1.(type) {
case env.Integer:
Expand Down
12 changes: 4 additions & 8 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
// } 111
// equal {
// spr1: ref spreadsheet { "a" "b" } { 1 10 2 20 }
// incrA: fn { row } { print inspect row 30 }
// incrA: fn { row } { row + [ "a" ( "a" <- row ) + 9 ] }
// update-row! spr1 1 ?incrA
// spr1 |deref |A1
// } 10
Expand All @@ -383,10 +383,6 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
case env.Function:
CallFunction(updater, ps, spr.Rows[idx.Value-1], false, ps.Ctx)
ret := ps.Res
fmt.Printf("%#v", ret)
if !util.IsTruthy(ret) {
return makeError(ps, "Function given to update-row! should have returned a value, but didn't")
}
if ok, err, row := RyeValueToSpreadsheetRow(spr, ret); ok {
spr.Rows[idx.Value-1] = *row
return spr
Expand Down Expand Up @@ -427,7 +423,7 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
},
// Tests:
// equal {
// spr1: spreadsheet { "a" "b" } { 1 10 2 20 }
// spr1: ref spreadsheet { "a" "b" } { 1 10 2 20 }
// spr1 .remove-row! 1
// spr1 .deref .A1
// } 2
Expand Down Expand Up @@ -1534,7 +1530,7 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } |col-avg 'a } 2
// equal { spreadsheet { 'a } { 1 2 3 } |col-avg 'a } 2.0
"col-avg": {
Argsn: 2,
Doc: "Accepts a spreadsheet and a column name and returns a sum of a column.", // TODO -- let it accept a block and list also
Expand Down Expand Up @@ -1592,7 +1588,7 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Spreadsheet:
r := s0.Rows[0].Values[1]
r := s0.Rows[1].Values[0]
return env.ToRyeValue(r)

default:
Expand Down
2 changes: 2 additions & 0 deletions info/base.info.rye
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ section "base" "base text" {
equal { "A" + "b" } "Ab"
equal { "A" + 1 } "A1"
equal { { 1 2 } + { 3 4 } } { 1 2 3 4 }
equal { dict { "a" 1 } |+ { "b" 2 } } dict { "a" 1 "b" 2 }
equal { dict { "a" 1 } |+ dict { "b" 2 } } dict { "a" 1 "b" 2 }
}

group "_-"
Expand Down
6 changes: 3 additions & 3 deletions info/spreadsheet.info.rye
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ section "base" "base text" {
} 111
equal {
spr1: ref spreadsheet { "a" "b" } { 1 10 2 20 }
incrA: fn { row } { print inspect row 30 }
incrA: fn { row } { row + [ "a" ( "a" <- row ) + 9 ] }
update-row! spr1 1 ?incrA
spr1 |deref |A1
} 10
Expand All @@ -100,7 +100,7 @@ section "base" "base text" {

{
equal {
spr1: spreadsheet { "a" "b" } { 1 10 2 20 }
spr1: ref spreadsheet { "a" "b" } { 1 10 2 20 }
spr1 .remove-row! 1
spr1 .deref .A1
} 2
Expand Down Expand Up @@ -195,7 +195,7 @@ section "base" "base text" {
}

{
equal { spreadsheet { 'a } { 1 2 3 } |col-avg 'a } 2
equal { spreadsheet { 'a } { 1 2 3 } |col-avg 'a } 2.0
}

group "B1"
Expand Down

0 comments on commit ba8c6c1

Please sign in to comment.