diff --git a/env/env.go b/env/env.go index 51e8914b..c77e814c 100755 --- a/env/env.go +++ b/env/env.go @@ -303,6 +303,7 @@ type ProgramState struct { LiveObj *LiveEnv Dialect DoDialect Stack *EyrStack + Embedded bool } type DoDialect int @@ -335,6 +336,7 @@ func NewProgramState(ser TSeries, idx *Idxs) *ProgramState { nil, // NewLiveEnv(), Rye2Dialect, NewEyrStack(), + false, } return &ps } @@ -362,6 +364,7 @@ func NewProgramStateNEW() *ProgramState { NewLiveEnv(), Rye2Dialect, NewEyrStack(), + false, } return &ps } diff --git a/env/spreadsheet.go b/env/spreadsheet.go index 1eb7e6d9..ac42ba99 100644 --- a/env/spreadsheet.go +++ b/env/spreadsheet.go @@ -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 +} diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 10e07c4d..ef717bcc 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -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 }, @@ -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("") } diff --git a/evaldo/builtins_spreadsheet.go b/evaldo/builtins_spreadsheet.go index 2652d080..a89bdc0e 100644 --- a/evaldo/builtins_spreadsheet.go +++ b/evaldo/builtins_spreadsheet.go @@ -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!") diff --git a/loader/loader.go b/loader/loader.go index 89b9df61..72f61aa0 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -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* diff --git a/runner/buildtemp/main.rye b/runner/buildtemp/main.rye new file mode 100644 index 00000000..ed722f65 --- /dev/null +++ b/runner/buildtemp/main.rye @@ -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 + } +} + diff --git a/runner/runner.go b/runner/runner.go index eff3cf94..6d9feeac 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -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) + } } } } @@ -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) diff --git a/tests/main.rye b/tests/main.rye index d1b02d31..889a1168 100644 --- a/tests/main.rye +++ b/tests/main.rye @@ -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 diff --git a/tests/spreadsheet.rye b/tests/spreadsheet.rye new file mode 100644 index 00000000..2a5d57f8 --- /dev/null +++ b/tests/spreadsheet.rye @@ -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 + } +} + diff --git a/tests/spreadsheet_mutations.rye b/tests/spreadsheet_mutations.rye index 15c75b04..ac6cae3d 100644 --- a/tests/spreadsheet_mutations.rye +++ b/tests/spreadsheet_mutations.rye @@ -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 { @@ -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 + } } diff --git a/tests/spreadsheet_ref.rye b/tests/spreadsheet_ref.rye new file mode 100644 index 00000000..ac6cae3d --- /dev/null +++ b/tests/spreadsheet_ref.rye @@ -0,0 +1,75 @@ +section "spreadsheet ref" +"Functions that mutate refed spreadsheets" +{ + + 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?" + equal { cart |nth 3 -> "price" } 19.99 + equal { cart .remove-row! 1 cart .nth 1 -> "title" } "Animal Farm" + comment { + rowdict: dict { "title" "Windowlight" "isbn" "0000000000000" "price" 0.99 "quantity" 1 } + probe rowdict + print rowdict .type? + ; cart .update-row! 1 rowdict + cart .nth 1 -> "isbn" + } "0000000000000" + equal { + updatefn: fn { row } { dict { "title" "I see the moon" "isbn" "0123456789012" "price" 0.99 "quantity" 1 } } + cart .update-row! 3 updatefn + cart .nth 3 -> "isbn" + } "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 + } + +} + diff --git a/tests/tpl/atelier-e-light.css b/tests/tpl/atelier-e-light.css new file mode 100644 index 00000000..d0b52b0b --- /dev/null +++ b/tests/tpl/atelier-e-light.css @@ -0,0 +1,119 @@ +/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ +.prettyprint { + background: #f4f3ec; + font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; + border: 0 !important; +} + +.pln { + color: #22221b; +} + +/* Specify class=linenums on a pre to get line numbering */ +ol.linenums { + margin-top: 0; + margin-bottom: 0; + color: #878573; +} + +li.L0, +li.L1, +li.L2, +li.L3, +li.L4, +li.L5, +li.L6, +li.L7, +li.L8, +li.L9 { + padding-left: 1em; + background-color: #f4f3ec; + list-style-type: decimal; +} + +@media screen { + + /* string content */ + + .str { + color: #7d9726; + } + + /* keyword */ + + .kwd { + color: #5f9182; + } + + /* comment */ + + .com { + color: #878573; + } + + /* type name */ + + .typ { + color: #36a166; + } + + /* literal value */ + + .lit { + color: #ae7313; + } + + /* punctuation */ + + .pun { + color: #22221b; + } + + /* lisp open bracket */ + + .opn { + color: #22221b; + } + + /* lisp close bracket */ + + .clo { + color: #22221b; + } + + /* markup tag name */ + + .tag { + color: #ba6236; + } + + /* markup attribute name */ + + .atn { + color: #ae7313; + } + + /* markup attribute value */ + + .atv { + color: #5b9d48; + } + + /* declaration */ + + .dec { + color: #ae7313; + } + + /* variable name */ + + .var { + color: #ba6236; + } + + /* function name */ + + .fun { + color: #36a166; + } +} diff --git a/tests/tpl/header.html b/tests/tpl/header.html index 5fae2624..6dbeecec 100644 --- a/tests/tpl/header.html +++ b/tests/tpl/header.html @@ -27,7 +27,8 @@ Validation and Regexps · Miscellaneous - + Spreadsheets + Refed spreadsheets