Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rye here1 #103

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions env/envutil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package env

import "fmt"

Check failure on line 3 in env/envutil.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/refaktor/rye (goimports)
import "time"

func ToRyeValue(val any) Object {
switch v := val.(type) {
Expand Down Expand Up @@ -28,6 +29,8 @@
return v
case nil:
return nil
case time.Time:
return *NewString(v.Format(time.RFC3339))
default:
fmt.Println(val)
// TODO-FIXME
Expand Down
22 changes: 14 additions & 8 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ func (i String) Inspect(e Idxs) string {
// Inspect returns a string representation of the Integer.
func (i String) Probe(e Idxs) string {
s := i.Value
if len(s) > 100 {
return s[:100] + "..."
if len(s) > 80 {
return s[:80] + "..."
}
return "\"" + s + "\""
return s
// return "\"" + s + "\""
}

func (i String) Trace(msg string) {
Expand Down Expand Up @@ -252,27 +253,32 @@ type Uri struct {
}

func NewUri1(index *Idxs, path string) *Uri {
scheme := strings.Split(path, "://")[0] // + "-schema" // TODO -- this is just temporary .. so we test it further, make proper once at that level
scheme2 := strings.Split(path, "://")
scheme := scheme2[0] // + "-schema" // TODO -- this is just temporary .. so we test it further, make proper once at that level
idxSch := index.IndexWord(scheme)
kind := scheme + "-schema"
idxKind := index.IndexWord(kind)
nat := Uri{Word{idxSch}, path, Word{idxKind}}
nat := Uri{Word{idxSch}, scheme2[1], Word{idxKind}}
fmt.Println(nat)
return &nat
}

func NewUri(index *Idxs, scheme Word, path string) *Uri {
scheme2 := strings.Split(path, "://")
kindstr := strings.Split(path, "://")[0] + "-schema" // TODO -- this is just temporary .. so we test it further, make proper once at that level
idx := index.IndexWord(kindstr)
nat := Uri{scheme, path, Word{idx}}
nat := Uri{scheme, scheme2[1], Word{idx}}
// nat := Uri{Word{idxSch}, scheme2[1], Word{idxKind}}
fmt.Println(nat)
return &nat
}

func (i Uri) GetPath() string {
return strings.SplitAfter(i.Path, "://")[1]
return i.Path
}

func (i Uri) GetProtocol() string {
return strings.Split(i.Path, "://")[0]
return "TODO"
}

func (i Uri) Type() Type {
Expand Down
27 changes: 26 additions & 1 deletion evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,7 @@
},
},

/* TEMP FOR WASM 20250116
"get-input": {
Argsn: 1,
Doc: "Stops execution and gives you a Rye console, to test the code inside environment.",
Expand Down Expand Up @@ -1950,7 +1951,7 @@
return MakeArgError(ps, 1, []env.Type{env.StringType}, "input-field")
}
},
},
}, */

// DOERS

Expand Down Expand Up @@ -2394,6 +2395,30 @@
},
},

"cc": {
Argsn: 1,
Doc: "Change to context",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.RyeCtx:
ps.Ctx = &s1
return s1
default:
return MakeArgError(ps, 1, []env.Type{env.StringType}, "cc")
}
},
},

"mkcc": {
Argsn: 0,
Doc: "Make context with current as parent and change to it.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
ctx := ps.Ctx

Check failure on line 2416 in evaldo/builtins.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` `-r 'interface{} -> any'` (gofmt)
ps.Ctx = env.NewEnv(ctx) // make new context with current par
return ctx
},
},

"raw-context": { // **
Argsn: 1,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down
4 changes: 4 additions & 0 deletions evaldo/builtins_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func resultToJS(res env.Object) any {
return v.Value
case env.Integer:
return v.Value
case *env.Integer:
return v.Value
case env.Decimal:
return v.Value
case env.RyeCtx:
return "{ 'state': 'todo' }"
default:
Expand Down
55 changes: 48 additions & 7 deletions evaldo/builtins_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package evaldo
import (
"database/sql"
"fmt"
"strings"

"github.com/refaktor/rye/env"

Expand Down Expand Up @@ -41,6 +42,38 @@ var Builtins_mysql = map[string]*env.Builtin{
},
},

"mysql-schema//open\\pwd": {
Argsn: 2,
Doc: "Open Mysql connection.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch str := arg0.(type) {
case env.Uri:
switch pwd := arg1.(type) {
case env.String:
path := strings.Replace(str.Path, "@", ":"+pwd.Value+"@", 1)
fmt.Println(path)
db, err := sql.Open("mysql", path)
if err != nil {
// TODO --
//fmt.Println("Error1")
ps.FailureFlag = true
errMsg := fmt.Sprintf("Error opening SQL: %v", err.Error())
return MakeBuiltinError(ps, errMsg, "mysql-schema//open")
} else {
//fmt.Println("Error2")
return *env.NewNative(ps.Idx, db, "Rye-mysql")
}
default:
ps.FailureFlag = true
return MakeArgError(ps, 2, []env.Type{env.StringType}, "mysql-schema//open")
}
default:
ps.FailureFlag = true
return MakeArgError(ps, 1, []env.Type{env.UriType}, "mysql-schema//open")
}
},
},

"Rye-mysql//exec": {
Argsn: 2,
Doc: "Execute sql query in for mysql.",
Expand All @@ -54,7 +87,7 @@ var Builtins_mysql = map[string]*env.Builtin{
ser := ps.Ser
ps.Ser = str.Series
values := make([]any, 0, 2)
_, vals = SQL_EvalBlock(ps, MODE_PSQL, values)
_, vals = SQL_EvalBlock(ps, MODE_SQLITE, values)
sqlstr = ps.Res.(env.String).Value
ps.Ser = ser
case env.String:
Expand Down Expand Up @@ -103,7 +136,7 @@ var Builtins_mysql = map[string]*env.Builtin{
ser := ps.Ser
ps.Ser = str.Series
values := make([]any, 0, 2)
_, vals = SQL_EvalBlock(ps, MODE_PSQL, values)
_, vals = SQL_EvalBlock(ps, MODE_SQLITE, values)
sqlstr = ps.Res.(env.String).Value
ps.Ser = ser
case env.String:
Expand Down Expand Up @@ -143,9 +176,17 @@ var Builtins_mysql = map[string]*env.Builtin{
// storing it in the map with the name of the column as the key.
m := make(map[string]any)
for i, colName := range cols {
val := columnPointers[i].(*any)
m[colName] = *val
sr.Values = append(sr.Values, *val)
val := *columnPointers[i].(*any)
switch vval := val.(type) {
case []uint8:
// fmt.Println(val)
// fmt.Printf("%T", vval)
m[colName] = env.ToRyeValue(string(vval))
sr.Values = append(sr.Values, env.ToRyeValue(string(vval)))
default:
m[colName] = env.ToRyeValue(vval)
sr.Values = append(sr.Values, env.ToRyeValue(vval))
}
}
spr.AddRow(sr)
result = append(result, m)
Expand All @@ -157,13 +198,13 @@ var Builtins_mysql = map[string]*env.Builtin{
// fmt.Print(result)
if i == 0 {
ps.FailureFlag = true
return MakeBuiltinError(ps, "No data.", "Rye-mysql//exec")
return MakeBuiltinError(ps, "No data.", "Rye-mysql//query")
}
return *spr
//return *env.NewNative(ps.Idx, *spr, "Rye-spreadsheet")
}
} else {
return MakeBuiltinError(ps, "Empty SQL.", "Rye-mysql//exec")
return MakeBuiltinError(ps, "Empty SQL.", "Rye-mysql//query")
}

default:
Expand Down
3 changes: 2 additions & 1 deletion evaldo/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func MaybeDisplayFailureOrError(es *env.ProgramState, genv *env.Idxs) {
}
}

func DoGeneralInput(es *env.ProgramState, prompt string) {
/* THIS WAS DISABLED TEMP FOR WASM MODE .. 20250116 func DoGeneralInput(es *env.ProgramState, prompt string) {
line := liner.NewLiner()
defer line.Close()
if code, err := line.SimplePrompt(prompt); err == nil {
Expand All @@ -337,3 +337,4 @@ func DoGeneralInputField(es *env.ProgramState, prompt string) {
log.Print("Error reading line: ", err)
}
}
*/
26 changes: 26 additions & 0 deletions examples/examples-wip/goroutines/channels_2.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; example using more Rye features

jobs: new-channel 5
done: new-channel 0

go does {
print "waiting to process jobs"
forever {
read jobs |^fix {
print "recieved all jobs"
send done 1
}
|print\val "received job {{}}"
sleep 1000
}
}

loop 3 {
.send* jobs ,
.print\val "sent job {{}}"
}
close jobs

print "waiting for jobs to finish"
read done
print "finished"
2 changes: 2 additions & 0 deletions examples/examples-wip/goroutines/goroutines_channels.rye
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; original example

jobs: new-channel 5
done: new-channel 0

Expand Down
24 changes: 12 additions & 12 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,19 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
parser, _ := NewParser(`
BLOCK <- "{" SPACES SERIES* "}"
BBLOCK <- "[" SPACES SERIES* "]"
GROUP <- "(" SPACES SERIES* ")"
SERIES <- (GROUP / COMMENT / URI / EMAIL / STRING / DECIMAL / NUMBER / COMMA / SETWORD / LSETWORD / ONECHARPIPE / PIPEWORD / XWORD / OPWORD / TAGWORD / EXWORD / CPATH / FPATH / KINDWORD / GENWORD / GETWORD / WORD / VOID / BLOCK / GROUP / BBLOCK / ARGBLOCK ) SPACES
ARGBLOCK <- "{" WORD ":" WORD "}"
WORD <- LETTER LETTERORNUM* / NORMOPWORDS
GROUP <- "(" SPACES SERIES* ")"
SERIES <- (GROUP / COMMENT / URI / EMAIL / STRING / DECIMAL / NUMBER / COMMA / SETWORD / LSETWORD / ONECHARPIPE / PIPEWORD / XWORD / OPWORD / TAGWORD / EXWORD / CPATH / FPATH / KINDWORD / GENWORD / GETWORD / WORD / VOID / BLOCK / GROUP / BBLOCK / ARGBLOCK ) SPACES
ARGBLOCK <- "{" WORD ":" WORD "}"
WORD <- LETTER LETTERORNUM* / NORMOPWORDS
GENWORD <- "~" UCLETTER LCLETTERORNUM*
SETWORD <- LETTER LETTERORNUM* ":"
LSETWORD <- ":" LETTER LETTERORNUM*
GETWORD <- "?" LETTER LETTERORNUM*
PIPEWORD <- "|" LETTER LETTERORNUM* / PIPEARROWS / "|" NORMOPWORDS
ONECHARPIPE <- "|" ONECHARWORDS
ONECHARPIPE <- "|" ONECHARWORDS
OPWORD <- "." LETTER LETTERORNUM* / "." NORMOPWORDS / OPARROWS / ONECHARWORDS / "[*" LETTERORNUM*
TAGWORD <- "'" LETTER LETTERORNUM*
KINDWORD <- "~(" LETTER LETTERORNUM* ")~"?
KINDWORD <- "~(" LETTER LETTERORNUM* ")~"?
XWORD <- "<" LETTER LETTERORNUM* ">"?
EXWORD <- "</" LETTER LETTERORNUM* ">"?
STRING <- ('"' STRINGCHAR* '"') / ("$" STRINGCHAR1* "$")
Expand All @@ -367,22 +367,22 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
EMAILPART <- < ([a-zA-Z0-9._]+) >
FPATH <- "%" URIPATH*
CPATH <- WORD ( "/" WORD )+
ONECHARWORDS <- < [<>*+-=/] >
ONECHARWORDS <- < [<>*+-=/] >
NORMOPWORDS <- < ("_"[<>*+-=/]) >
PIPEARROWS <- ">>" / "~>" / "->"
OPARROWS <- "<<" / "<~" / "<-" / ">=" / "<="
LETTER <- < [a-zA-Z^(` + "`" + `] >
LETTER <- < [a-zA-Z^(` + "`" + `] >
LETTERORNUM <- < [a-zA-Z0-9-?=.\\!_+<>\]*()] >
URIPATH <- < [a-zA-Z0-9-?=.:@/\\!_> ()] >
UCLETTER <- < [A-Z] >
LCLETTERORNUM <- < [a-z0-9] >
NUMBER <- < ("-"?[0-9]+) >
DECIMAL <- < ("-"?[0-9]+.[0-9]+) >
LCLETTERORNUM <- < [a-z0-9] >
NUMBER <- < ("-"?[0-9]+) >
DECIMAL <- < ("-"?[0-9]+.[0-9]+) >
SPACE <- < [ \t\r\n] >
STRINGCHAR <- < !'"' . >
STRINGCHAR1 <- < !"$" . >
COMMA <- ","
VOID <- "_"
VOID <- "_"
COMMENT <- (";" NOTENDLINE* )
NOTENDLINE <- < !"\n" . >
`)
Expand Down
2 changes: 1 addition & 1 deletion loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func TestLoader_load_uri_min(t *testing.T) {
t.Error("Expected scheme sqlite")
}

if block.(env.Block).Series.Get(0).(env.Uri).Path != "sqlite://db" { // todo later return just the path part ... but there are more components to URI, so we do it later
if block.(env.Block).Series.Get(0).(env.Uri).Path != "db" { // todo later return just the path part ... but there are more components to URI, so we do it later
t.Error("Expected path sqlite://db")
}
}
Expand Down
Loading
Loading