Skip to content

Commit

Permalink
Merge pull request #377 from refaktor/allin
Browse files Browse the repository at this point in the history
improving spreadsheet refed functions and tests
  • Loading branch information
refaktor authored Oct 10, 2024
2 parents 3e40111 + a08f12b commit c3bf067
Show file tree
Hide file tree
Showing 14 changed files with 411 additions and 68 deletions.
3 changes: 3 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ type ProgramState struct {
LiveObj *LiveEnv
Dialect DoDialect
Stack *EyrStack
Embedded bool
}

type DoDialect int
Expand Down Expand Up @@ -335,6 +336,7 @@ func NewProgramState(ser TSeries, idx *Idxs) *ProgramState {
nil, // NewLiveEnv(),
Rye2Dialect,
NewEyrStack(),
false,
}
return &ps
}
Expand Down Expand Up @@ -362,6 +364,7 @@ func NewProgramStateNEW() *ProgramState {
NewLiveEnv(),
Rye2Dialect,
NewEyrStack(),
false,
}
return &ps
}
Expand Down
11 changes: 11 additions & 0 deletions env/spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,14 @@ func (s Spreadsheet) GetColumns() List {
}
return *NewList(lst)
}

func (s Spreadsheet) GetColumnIndex(column string) int {
index := -1
for i, v := range s.Cols {
if v == column {
index = i
break
}
}
return index
}
16 changes: 14 additions & 2 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -8097,7 +8097,13 @@ var builtins = map[string]*env.Builtin{
Argsn: 1,
Doc: "",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
return util.StringToFieldsWithQuoted(strings.Join(os.Args[2:], " "), " ", "\"")
var firstArg int
if ps.Embedded {
firstArg = 1
} else {
firstArg = 2
}
return util.StringToFieldsWithQuoted(strings.Join(os.Args[firstArg:], " "), " ", "\"")
// block, _ := loader.LoadString(os.Args[0], false)
// return block
},
Expand All @@ -8106,8 +8112,14 @@ var builtins = map[string]*env.Builtin{
Argsn: 1,
Doc: "",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var firstArg int
if ps.Embedded {
firstArg = 1
} else {
firstArg = 2
}
if len(os.Args) > 1 {
return *env.NewString(strings.Join(os.Args[2:], " "))
return *env.NewString(strings.Join(os.Args[firstArg:], " "))
} else {
return *env.NewString("")
}
Expand Down
22 changes: 15 additions & 7 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,23 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
NameOfRyeType(ps.Res.Type()),
))
}
case env.Dict:
// dict should be able to have only the columns you want to update
row := spr.Rows[idx.Value-1]

default:
if ok, err, row := RyeValueToSpreadsheetRow(spr, updater); ok {
spr.Rows[idx.Value-1] = *row
return ps.Res
} else if len(err) > 0 {
return makeError(ps, err)
for keyStr, val := range updater.Data {
index := spr.GetColumnIndex(keyStr)
if index < 0 {
return makeError(ps, "Column "+keyStr+" was not found")
}
row.Values[index] = val
}
return MakeArgError(ps, 3, []env.Type{env.DictType, env.SpreadsheetRowType}, "update-row")
return spr
case env.SpreadsheetRow:
spr.Rows[idx.Value-1] = updater
return spr
default:
return MakeArgError(ps, 3, []env.Type{env.FunctionType, env.DictType, env.SpreadsheetRowType}, "update-row")
}
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "update-row!")
Expand Down
2 changes: 1 addition & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
LSETWORD <- ":" LETTER LETTERORNUM*
LMODWORD <- "::" LETTER LETTERORNUM*
GETWORD <- "?" LETTER LETTERORNUM*
PIPEWORD <- "/" LETTER LETTERORNUM* / "|" LETTER LETTERORNUM* / PIPEARROWS / "|" NORMOPWORDS
PIPEWORD <- "\\" LETTER LETTERORNUM* / "|" LETTER LETTERORNUM* / PIPEARROWS / "|" NORMOPWORDS
ONECHARPIPE <- "|" ONECHARWORDS
OPWORD <- "." LETTER LETTERORNUM* / "." NORMOPWORDS / OPARROWS / ONECHARWORDS / "[*" LETTERORNUM*
TAGWORD <- "'" LETTER LETTERORNUM*
Expand Down
57 changes: 57 additions & 0 deletions runner/buildtemp/main.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

rye .needs { fyne }

Data: context {

add!: fn { task } { tasks .add-rows! task }
remove!: fn { idx } { tasks .remove-row! idx + 1 }
check!: fn { idx val } { tasks .update-row! idx + 1 dict [ "done" val ] }
make-spreadsheet!: fn { texts } { .map { ::x [ 0 x ] } |unpack |spreadsheet* { "done" "text" } |ref |change! 'tasks }

tasks: ref spreadsheet { "done" "text" } { }
}

rye .args\raw .split "," |Data/make-spreadsheet!

do\par fyne {

win: app .window "Adhocest TODO app"

lst: list
does { length? deref Data/tasks }
does {
idx: hide label "idx"
h-box [
check "" fn\par { v } current
{ Data/check! to-integer idx .text? v , lst .refresh }
label ""
idx
layout-spacer
button-with-icon "" delete-icon fn\par { } current
{ Data/remove! to-integer idx .text? , lst .refresh }
]
}
fn { i box } {
set! box .objects? { chk lbl hdn xo btn }
chk .set-checked 0 <- i <- deref Data/tasks
lbl .set-text 1 <- i <- deref Data/tasks
hdn .set-text to-string i
}

input: entry
|place-holder! "Add to list here ..."
|on-submitted! fn { x } {
Data/add! [ 0 x ]
input .set-text ""
lst .refresh
}

cont: border nil input nil nil [ lst ]

with win {
.resize size 300.0 300.0 ,
.set-content cont ,
.show-and-run
}
}

81 changes: 43 additions & 38 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,50 +92,54 @@ func DoMain(regfn func(*env.ProgramState)) {
code = code + " cc " + v + " "
}
}

// Check for --help flag
if flag.NFlag() == 0 && flag.NArg() == 0 {
if Option_Embed_Main {
main_rye_file("buildtemp/main.rye", false, true, false, *console, code, *lang, regfn, *stin)
} else if Option_Do_Main {
ryeFile := dotsToMainRye(".")
main_rye_file(ryeFile, false, true, false, *console, code, *lang, regfn, *stin)
} else {
main_rye_repl(os.Stdin, os.Stdout, true, false, *lang, code, regfn)
}
if Option_Embed_Main {
main_rye_file("buildtemp/main.rye", false, true, false, *console, code, *lang, regfn, *stin)
} else {
// Check for --help flag
if *help {
flag.Usage()
os.Exit(0)
}

args := flag.Args()
// Check for subcommands (cont) and handle them
if len(args) > 0 {
if args[0] == "cont" || args[0] == "continue" {
fmt.Println("[continuing...]")
ryeFile := findLastConsoleSave()
main_rye_file(ryeFile, false, true, false, true, code, *lang, regfn, *stin)
} else if args[0] == "shell" {
main_rysh()
} else if args[0] == "rwk" {
main_ryk()
} else if args[0] == "here" {
if *do != "" {
main_rye_file("", false, true, true, *console, code, *lang, regfn, *stin)
} else {
main_rye_repl(os.Stdin, os.Stdout, true, true, *lang, code, regfn)
}
} else {
ryeFile := dotsToMainRye(args[0])
if flag.NFlag() == 0 && flag.NArg() == 0 {
if Option_Embed_Main {
fmt.Println("CASE OPT EMBED MAIN 2")
main_rye_file("buildtemp/main.rye", false, true, false, *console, code, *lang, regfn, *stin)
} else if Option_Do_Main {
ryeFile := dotsToMainRye(".")
main_rye_file(ryeFile, false, true, false, *console, code, *lang, regfn, *stin)
} else {
main_rye_repl(os.Stdin, os.Stdout, true, false, *lang, code, regfn)
}
} else {
if *do != "" || *sdo != "" {
main_rye_file("", false, true, false, *console, code, *lang, regfn, *stin)
// Check for --help flag
if *help {
flag.Usage()
os.Exit(0)
}

args := flag.Args()
// Check for subcommands (cont) and handle them
if len(args) > 0 {
if args[0] == "cont" || args[0] == "continue" {
fmt.Println("[continuing...]")
ryeFile := findLastConsoleSave()
main_rye_file(ryeFile, false, true, false, true, code, *lang, regfn, *stin)
} else if args[0] == "shell" {
main_rysh()
} else if args[0] == "rwk" {
main_ryk()
} else if args[0] == "here" {
if *do != "" {
main_rye_file("", false, true, true, *console, code, *lang, regfn, *stin)
} else {
main_rye_repl(os.Stdin, os.Stdout, true, true, *lang, code, regfn)
}
} else {
ryeFile := dotsToMainRye(args[0])
main_rye_file(ryeFile, false, true, false, *console, code, *lang, regfn, *stin)
}
} else {
main_rye_repl(os.Stdin, os.Stdout, true, false, *lang, code, regfn)
if *do != "" || *sdo != "" {
main_rye_file("", false, true, false, *console, code, *lang, regfn, *stin)
} else {
main_rye_repl(os.Stdin, os.Stdout, true, false, *lang, code, regfn)
}
}
}
}
Expand Down Expand Up @@ -399,6 +403,7 @@ func main_rye_file(file string, sig bool, subc bool, here bool, interactive bool
}

ps := env.NewProgramStateNEW()
ps.Embedded = Option_Embed_Main
ps.ScriptPath = file
ps.WorkingPath, _ = os.Getwd() // TODO -- WHAT SHOULD WE DO IF GETWD FAILS?
evaldo.RegisterBuiltins(ps)
Expand Down
2 changes: 1 addition & 1 deletion tests/main.rye
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ generate-doc-file: fn { filename menu } {
} |write* to-file filename + ".html"
}

menu: { "basics" "structures" "validation" "misc" "spreadsheet_mutations" }
menu: { "basics" "structures" "validation" "misc" "spreadsheet" "spreadsheet_ref" }

print-help: does {
print `# Rye's simple testing tool
Expand Down
13 changes: 13 additions & 0 deletions tests/spreadsheet.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
section "spreadsheet ref"
"Functions that mutate refed spreadsheets"
{

group "ref" ""
{ { block } }
{
equal {
spreadsheet { "a" "b" } { 1 10 2 20 3 30 } |is-ref
} false
}
}

67 changes: 53 additions & 14 deletions tests/spreadsheet_mutations.rye
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
section "spreadsheet mutations"
"Functions that mutate thawed spreadsheets"
section "spreadsheet ref"
"Functions that mutate refed spreadsheets"
{

group "add-row!" "add-row!"
comment "add-row!" "add-row!"
{ { block } }
{
equal {
spreadsheet { "title" "isbn" "price" "quantity" }
{
"Do Androids Dream of Electric Sheep?" "9781608866403" 12.99 4
"Animal Farm" "9780151002177" 2.49 10
"An Oxford Anthology of Shakespeare" "9780198129356" 19.99 10
"And Then There Were None" "9780062073488" 3.99 10
} |thaw ::cart
cart |nth 1 -> "title" } "Do Androids Dream of Electric Sheep?"
spreadsheet { "title" "isbn" "price" "quantity" }
{
"Do Androids Dream of Electric Sheep?" "9781608866403" 12.99 4
"Animal Farm" "9780151002177" 2.49 10
"An Oxford Anthology of Shakespeare" "9780198129356" 19.99 10
"And Then There Were None" "9780062073488" 3.99 10
} |thaw ::cart
cart |nth 1 -> "title"
} "Do Androids Dream of Electric Sheep?"
equal { cart |nth 3 -> "price" } 19.99

equal { cart .remove-row! 1 cart .nth 1 -> "title" } "Animal Farm"
equal {
comment {
rowdict: dict { "title" "Windowlight" "isbn" "0000000000000" "price" 0.99 "quantity" 1 }
probe rowdict
print rowdict .type?
cart .update-row! 1 rowdict
; cart .update-row! 1 rowdict
cart .nth 1 -> "isbn"
} "0000000000000"
equal {
Expand All @@ -31,6 +31,45 @@ section "spreadsheet mutations"
} "0123456789012"

}
group "ref" ""
{ { block } }
{
equal {
spreadsheet { "a" "b" } { 1 10 2 20 3 30 } |is-ref
} false
}
group "ref" ""
{ { block } }
{
equal {
spr1: ref spreadsheet { "a" "b" } { 1 10 2 20 3 30 }
spr1 .is-ref
} true
}
group "add-rows!" "add-rows!"
{ { block } }
{
equal {
spr1 .add-rows! [ 4 40 ]
spr1 .deref .length?
} 4
}
group "update-row!" "update-row!"
{ { block } }
{
equal {
spr1 .update-row! 1 dict [ "a" 101 ]
spr1 .deref .A1
} 101
}
group "remove-row!" "remove-row!"
{ { block } }
{
equal {
spr1 .remove-row! 1
spr1 .deref .length?
} 3
}

}

Loading

0 comments on commit c3bf067

Please sign in to comment.