Skip to content

Commit

Permalink
spreadsheet constructor accepts lists for values too, \word for pipew…
Browse files Browse the repository at this point in the history
…ord option, factor-of renamed to multiple-of, tests and examples updated, securesave improved
  • Loading branch information
refaktor committed Mar 24, 2024
1 parent 648ffa5 commit 7ba70ef
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (i Word) Equal(o Object) bool {
}

func (i Word) Dump(e Idxs) string {
return "'" + e.GetWord(i.Index)
return e.GetWord(i.Index)
}

//
Expand Down Expand Up @@ -1720,7 +1720,7 @@ func NewList(data []any) *List {
return &List{data, Word{0}}
}

func RyeToRaw(res Object) any { // TODO -- MOVE TO UTIL ... provide reverse named symetrically
func RyeToRaw(res Object) any { // TODO -- MOVE TO UTIL ... provide reverse named symmetrically
// fmt.Printf("Type: %T", res)
switch v := res.(type) {
case nil:
Expand Down
10 changes: 5 additions & 5 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (

"github.com/refaktor/rye/util"

"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sync/errgroup"
goterm "golang.org/x/term"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -747,7 +747,7 @@ var builtins = map[string]*env.Builtin{
fileName := fmt.Sprintf("console_%s.rye.enc", time.Now().Format("060102_150405"))

fmt.Print("Enter Password: ")
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
bytePassword, err := goterm.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -933,7 +933,7 @@ var builtins = map[string]*env.Builtin{

// BASIC FUNCTIONS WITH NUMBERS

"factor-of": { // ***
"multiple-of": { // ***
Argsn: 2,
Doc: "Checks if a first argument is a factor of second.",
Pure: true,
Expand All @@ -948,10 +948,10 @@ var builtins = map[string]*env.Builtin{
return *env.NewInteger(0)
}
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "factor-of")
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "multiple-of")
}
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "factor-of")
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "multiple-of")
}
},
},
Expand Down
23 changes: 13 additions & 10 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,36 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
switch header1 := arg0.(type) {
case env.Block:
header := header1.Series
cols := make([]string, header.Len())
for header.Pos() < header.Len() {
hlen := header.Len()
cols := make([]string, hlen)
for header.Pos() < hlen {
i := header.Pos()
k1 := header.Pop()
switch k := k1.(type) {
case env.String:
cols[i] = k.Value
}
}
fmt.Println(cols)
spr := env.NewSpreadsheet(cols)
switch data1 := arg1.(type) {
case env.Block:
rdata := data1.Series.S
for _, v := range rdata {
rowd := make([]any, header.Len())
for ii := 0; ii < header.Len(); ii++ {
rowd[ii] = v

for i := 0; i < len(rdata)/hlen; i++ {
rowd := make([]any, hlen)
for ii := 0; ii < hlen; ii++ {
rowd[ii] = rdata[i*hlen+ii]
}
spr.AddRow(*env.NewSpreadsheetRow(rowd, spr))
}
return *spr
case env.List:
rdata := data1.Data
for _, v := range rdata {
rowd := make([]any, header.Len())
for ii := 0; ii < header.Len(); ii++ {
rowd[ii] = v
for i := 0; i < len(rdata)/hlen; i++ {
rowd := make([]any, hlen)
for ii := 0; ii < hlen; ii++ {
rowd[ii] = rdata[i*hlen+ii]
}
spr.AddRow(*env.NewSpreadsheetRow(rowd, spr))
}
Expand Down
4 changes: 2 additions & 2 deletions examples/fizzbuzz.rye
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

for range 1 100 { :n
cases " " {
{ n .factor-of 3 } { "Fizz" }
{ n .factor-of 5 } { + "Buzz" }
{ n .multiple-of 3 } { "Fizz" }
{ n .multiple-of 5 } { + "Buzz" }
_ { n }
} |prns
}
Expand Down
2 changes: 1 addition & 1 deletion examples/project-euler/prob001.rye
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; sum all factors of 3 and 5 smaller than 1000

range 0 1000
|filter { :x all { x .factor-of 3 x .factor-of 5 } }
|filter { :x all { x .multiple-of 3 x .multiple-of 5 } }
|sum .print

; result 33165
Expand Down
2 changes: 1 addition & 1 deletion examples/rosettacode/100doors.rye
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rye .needs { xhofs }
produce 100 { } { + [ 0 ] } :doors

toggle: fn { doors step } {
.map\pos 'i { :v either i .factor-of step { not v } { v } } :ds
.map\pos 'i { :v either i .multiple-of step { not v } { v } } :ds
^if step = 100 { ds }
toggle ds step + 1
}
Expand Down
2 changes: 1 addition & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
SETWORD <- LETTER LETTERORNUM* ":"
LSETWORD <- ":" LETTER LETTERORNUM*
GETWORD <- "?" LETTER LETTERORNUM*
PIPEWORD <- "|" 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
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"regexp"
"sort"

"github.com/refaktor/rye/contrib"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"

"bufio"
"errors"
Expand All @@ -21,6 +20,7 @@ import (

"net/http"

"github.com/refaktor/rye/contrib"
"github.com/refaktor/rye/env"
"github.com/refaktor/rye/evaldo"
"github.com/refaktor/rye/loader"
Expand Down Expand Up @@ -368,7 +368,7 @@ func main_rye_file(file string, sig bool, subc bool, interactive bool, code stri

if file[len(file)-4:] == ".enc" {
fmt.Print("Enter Password: ")
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/basics.rye
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ section "Working with numbers"
equal { is-zero 0.1 } 0
}

group "factor-of"
mold\nowrap ?factor-of
group "multiple-of"
mold\nowrap ?multiple-of
{ { integer } { integer } }
{
equal { factor-of 10 2 } 1
equal { 10 .factor-of 3 } 0
equal { multiple-of 10 2 } 1
equal { 10 .multiple-of 3 } 0
}

group "odd"
Expand Down
2 changes: 1 addition & 1 deletion tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ section "Date and time functions"

}

; TODO equal { list { 3 4 5 6 } |map { .factor-of 3 } } list { 1 0 0 1 }
; TODO equal { list { 3 4 5 6 } |map { .multiple-of 3 } } list { 1 0 0 1 }

section "Math functions"
""
Expand Down
2 changes: 1 addition & 1 deletion tests/structures.rye
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ section "Higher order like functions"
equal { map { } { + 1 } } { }
equal { map { "aaa" "bb" "c" } { .length? } } { 3 2 1 }
equal { map list { "aaa" "bb" "c" } { .length? } } list { 3 2 1 }
equal { map list { 3 4 5 6 } { .factor-of 3 } } list { 1 0 0 1 }
equal { map list { 3 4 5 6 } { .multiple-of 3 } } list { 1 0 0 1 }
equal { map list { } { + 1 } } list { }
; equal { map "abc" { + "-" } .join } "a-b-c-" ; TODO doesn't work, fix join
equal { map "123" { .to-integer } } { 1 2 3 }
Expand Down
3 changes: 1 addition & 2 deletions util/securesave.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ func SaveSecure(textToEncrypt string, filename string, password string) {
}

// Write encrypted data to a file
err = os.WriteFile(filename, encryptedData, 0644)
err = os.WriteFile(filename, encryptedData, 0600)
if err != nil {
panic(err)
}

}

func ReadSecure(filename string, password string) string {
Expand Down

0 comments on commit 7ba70ef

Please sign in to comment.