Skip to content

Commit

Permalink
Devops1 - ryel tools POC, small fixes, script embedding (#207)
Browse files Browse the repository at this point in the history
* changing Rye shell to basic ansi colors, so it themable in terminals and works in emacs ansi-term

* added testing for failures in various do functions, ssh sever basics

* new funcs like nl and pink, ssh integration and example, errors tests starting

* ryel tools, embed_main flag, small fixes all around
  • Loading branch information
refaktor authored May 4, 2024
1 parent 68fabd3 commit 9a67814
Show file tree
Hide file tree
Showing 26 changed files with 199 additions and 110 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ dist/
tests/*.html
shell_*.rye
console_*.rye

buildtemp/main.rye
39 changes: 0 additions & 39 deletions _sandbox/ryel.rye

This file was deleted.

12 changes: 12 additions & 0 deletions bin/ryel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# This is a first **proof of concept** only, the naming, the functionality might change. Please propose changes if you see any faults,
# especially security implications.

FILE=$PWD/ryel
if [ -f "$FILE" ]; then
# echo "Starting local rye interpreter"
$FILE "$@"
else
echo "You don't have local Ryel binary yet. Define ryel.mod if needed and run ryelc build"
fi
8 changes: 8 additions & 0 deletions bin/ryelc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# This is a first **proof of concept** only, the naming, the functionality might change. Please propose changes if you see any faults,
# especially security implications.

# Assumes normal rye build (at least tiny is on the PATH), but we don't define RYE_HOME or anything like this for now, maybe we should

rye $RYE_HOME/util/ryel.rye $@
4 changes: 2 additions & 2 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (b Block) Inspect(e Idxs) string {

func (b Block) Print(e Idxs) string {
var r strings.Builder
r.WriteString("{ ")
// r.WriteString("{ ")
for i := 0; i < b.Series.Len(); i += 1 {
if b.Series.Get(i) != nil {
r.WriteString(b.Series.Get(i).Print(e))
Expand All @@ -419,7 +419,7 @@ func (b Block) Print(e Idxs) string {
r.WriteString("[NIL]")
}
}
r.WriteString("}")
// r.WriteString("}")
return r.String()
}

Expand Down
21 changes: 11 additions & 10 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
v := s1.Data[idx]
ok := true
if ok {
if len(s1.Data) >= int(idx)+1 {
v := s1.Data[idx]
return env.ToRyeValue(v)
} else {
ps.FailureFlag = true
Expand All @@ -249,9 +248,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
v := s1.Data[idx]
ok := true
if ok {
if len(s1.Data) >= int(idx)+1 {
v := s1.Data[idx]
return env.ToRyeValue(v)
} else {
ps.FailureFlag = true
Expand All @@ -265,9 +263,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
v := s1.Series.Get(int(idx))
ok := true
if ok {
if len(s1.Series.S) >= int(idx)+1 {
v := s1.Series.Get(int(idx))
return v
} else {
ps.FailureFlag = true
Expand Down Expand Up @@ -7405,7 +7402,11 @@ 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 *env.NewString(strings.Join(os.Args[2:], " "))
if len(os.Args) > 1 {
return *env.NewString(strings.Join(os.Args[2:], " "))
} else {
return *env.NewString("")
}
// block, _ := loader.LoadString(os.Args[0], false)
// return block
},
Expand Down
3 changes: 1 addition & 2 deletions evaldo/builtins_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func __input(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Ob
func __open(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s := arg0.(type) {
case env.Uri:
path := strings.Split(s.Path, "://")
file, err := os.Open(path[1])
file, err := os.Open(s.Path)
if err != nil {
return makeError(ps, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ var Builtins_math = map[string]*env.Builtin{
return DialectMath(ps, arg0)
},
},
"math": {
"calc": {
Argsn: 1,
Doc: "Do math dialect",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down
2 changes: 1 addition & 1 deletion evaldo/builtins_psutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"ls": {
"lsd": {
Argsn: 0,
Doc: "Returns current working directory.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down
47 changes: 26 additions & 21 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,38 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
},
},

"sort-col!": {
Argsn: 2,
"sort-by!": {
Argsn: 3,
Doc: "Sorts row by given column.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
dir, ok := arg2.(env.Word)
if !ok {
return MakeArgError(ps, 3, []env.Type{env.WordType}, "sort-col!")
}
var dirAsc bool
if dir.Index == ps.Idx.IndexWord("asc") {
dirAsc = true
} else if dir.Index == ps.Idx.IndexWord("desc") {
dirAsc = false
} else {
return MakeBuiltinError(ps, "Direction can be just asc or desc.", "sort-col!")
}
switch spr := arg0.(type) {
case env.Spreadsheet:
switch col := arg1.(type) {
case env.String:
if dirAsc {
SortByColumn(ps, &spr, col.Value)
} else {
SortByColumnDesc(ps, &spr, col.Value)
}
return spr
case env.Word:
SortByColumn(ps, &spr, ps.Idx.GetWord(col.Index))
if dirAsc {
SortByColumn(ps, &spr, ps.Idx.GetWord(col.Index))
} else {
SortByColumnDesc(ps, &spr, ps.Idx.GetWord(col.Index))
}
return spr
default:
return MakeArgError(ps, 2, []env.Type{env.WordType}, "sort-col!")
Expand All @@ -514,24 +537,6 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
}
},
},
"sort-col\\desc!": {
Argsn: 2,
Doc: "Sorts rows by given column, descending.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch spr := arg0.(type) {
case env.Spreadsheet:
switch col := arg1.(type) {
case env.Word:
SortByColumnDesc(ps, &spr, ps.Idx.GetWord(col.Index))
return spr
default:
return MakeArgError(ps, 2, []env.Type{env.WordType}, "sort-col\\desc!")
}
default:
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType}, "sort-col\\desc!")
}
},
},
"columns": {
Argsn: 2,
Doc: "Returs spreasheet with just given columns.",
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ github.com/refaktor/go-peg v0.0.0-20220116201714-31e3dfa8dc7d h1:FXrWUGgPRzhaZIB
github.com/refaktor/go-peg v0.0.0-20220116201714-31e3dfa8dc7d/go.mod h1:iIkrsFobLIWX8kQ6Oqj4cl4nwdMSE92DWpWwk9YlG9s=
github.com/refaktor/liner v1.2.10 h1:MjbQj9EfNuSFnNk9zan37xpkNIRpsWKenyIBg7FZdVw=
github.com/refaktor/liner v1.2.10/go.mod h1:ziZSGVYZ4OzZ9kbeB254MtIrxxQlDibULRQGlDi1iK8=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
29 changes: 21 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,28 @@ func main() {

evaldo.ShowResults = !*silent

var code string
if *do != "" {
code = *do
}

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

var code string
if *do != "" {
code = *do
}

args := flag.Args()
// Check for subcommands (cont) and handle them
if len(args) > 0 {
Expand Down Expand Up @@ -218,7 +225,7 @@ func dotsToMainRye(ryeFile string) string {
re := regexp.MustCompile(`^\.$|/\.$`)
if re.MatchString(ryeFile) {
main_path := ryeFile[:len(ryeFile)-1] + "main.rye"
if _, err := os.Stat(main_path); err == nil {
if _, err := os.Stat(main_path); err == nil || Option_Embed_Main {
_, err := os.ReadFile(main_path)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -397,7 +404,13 @@ func main_rye_file(file string, sig bool, subc bool, interactive bool, code stri

content = util.ReadSecure(file, password)
} else if file != "" {
bcontent, err := os.ReadFile(file)
var bcontent []byte
var err error
if Option_Embed_Main {
bcontent, err = Rye_files.ReadFile(file)
} else {
bcontent, err = os.ReadFile(file)
}
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 6 additions & 0 deletions option_do_main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build do_main
// +build do_main

package main

const Option_Do_Main bool = true
6 changes: 6 additions & 0 deletions option_do_main_not.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !do_main
// +build !do_main

package main

const Option_Do_Main bool = false
13 changes: 13 additions & 0 deletions option_embed_main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build embed_main
// +build embed_main

package main

import (
"embed"
)

const Option_Embed_Main bool = true

//go:embed buildtemp/main.rye
var Rye_files embed.FS
10 changes: 10 additions & 0 deletions option_embed_main_not.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !embed_main
// +build !embed_main

package main

import "embed"

const Option_Embed_Main bool = false

var Rye_files embed.FS
2 changes: 1 addition & 1 deletion planing.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Pratik is adding standard math functions from https://pkg.go.dev/math to the con
- Create standard commands / utils like cd / ls / mkdir / cp / mv [+-]
- Integrate awesome script library for many standard piping commands https://github.com/bitfield/script [--]

### do_main build flag and Android test
### do_main build flag and Android test [+-]

if build flag do_main is used make the dot behaviour work even without the dot. Usefull for distributing binary and main.rye , also to test to produce a mobil APK with Fyne.

Expand Down
2 changes: 1 addition & 1 deletion serve_wasm.rye
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

rye .needs { http }

new-server ":8085"
http-server ":8085"
|handle "/" new-static-handler %wasm
|serve

16 changes: 9 additions & 7 deletions tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -382,29 +382,31 @@ section "Math functions"
equal { do\in math { atan 1 } } 0.7853981633974483
}

group "atan2"
group "atan2"
mold\nowrap ""
{ { string } }
{
equal { do\in math { atan2 3 4 } } 0.6435011087932844
}

group "atanh"
group "atanh"
mold\nowrap ""
{ { string } }
{
equal { do\in math { atanh 0.5 } } 0.5493061443340548
}

group "ceil"
group "ceil"
mold\nowrap ""
{ { string } }
{
equal { do\in math { ceil 5.7 } } 6.000000
equal { do\in math { ceil 4.9 } } 5.000000
}

group "cbrt"

; TODO add sin and cos ... need PI constant

group "cbrt"
mold\nowrap ""
{ { string } }
{
Expand Down
Loading

0 comments on commit 9a67814

Please sign in to comment.