Skip to content

Commit

Permalink
modwords can now create and update values, many improvements, tests a…
Browse files Browse the repository at this point in the history
…nd examples also
  • Loading branch information
refaktor committed May 15, 2024
1 parent 8e6e8fa commit 866ce7e
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 129 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ tests/*.html
shell_*.rye
console_*.rye

buildtemp/main.rye
buildtemp/main.rye
*.bson

1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go build -tags "b_sqlite,b_http,b_sql,b_postgres,b_bson,b_mail,b_bcrypt,b_telegram,b_html,b_email,b_mail,b_mysql,b_fyne,b_contrib,b_bleve,b_devops,b_ssh," -o bin/rye
1 change: 1 addition & 0 deletions buildwasm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
env GOOS=js GOARCH=wasm go build -tags "b_wasm" -o wasm/ryeshell/main.wasm main_wasm.go ; bin/rye serve_wasm.rye
33 changes: 19 additions & 14 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,32 +243,37 @@ func (e *RyeCtx) Get2(word int) (Object, bool, *RyeCtx) {
}

func (e *RyeCtx) Set(word int, val Object) Object {
if _, exists := e.state[word]; exists {
return NewError("Can't set already set word, try using modword")
} else {
e.state[word] = val
return val
}
e.state[word] = val
return val
}

func (e *RyeCtx) Unset(word int) Object {
func (e *RyeCtx) Unset(word int, idxs *Idxs) Object {
if _, exists := e.state[word]; !exists {
return NewError("Can't unset non-existing word in this context")
return *NewError("Can't unset non-existing word " + idxs.GetWord(word) + " in this context")
} else {
delete(e.state, word)
return NewInteger(1)
}
}

func (e *RyeCtx) Mod(word int, val Object) Object {
func (e *RyeCtx) SetNew(word int, val Object, idxs *Idxs) bool {
if _, exists := e.state[word]; exists {
e.state[word] = val
return val
return false
} else {
return NewError("Can't mod an unset word, try using setword")
e.state[word] = val
return true
}
}

func (e *RyeCtx) Mod(word int, val Object) bool {
// if _, exists := e.state[word]; exists {
e.state[word] = val
return true
// } else {
// return NewError("Can't mod an unset word " + idxs.GetWord(word) + ", try using setword")
// }
}

type ProgramState struct {
Ser TSeries // current block of code
Res Object // result of expression
Expand Down Expand Up @@ -350,11 +355,11 @@ func AddToProgramState(ps *ProgramState, ser TSeries, idx *Idxs) *ProgramState {
func SetValue(ps *ProgramState, word string, val Object) {
idx, found := ps.Idx.GetIndex(word)
if found {
ps.Ctx.Set(idx, val)
ps.Ctx.SetNew(idx, val, ps.Idx)
switch valf := val.(type) {
case Function:
if valf.Pure {
ps.PCtx.Set(idx, val)
ps.PCtx.SetNew(idx, val, ps.Idx)
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func (i Modword) Inspect(e Idxs) string {
}

func (b Modword) Print(e Idxs) string {
return e.GetWord(b.Index) + ":"
return e.GetWord(b.Index) + "::"
}

func (i Modword) Trace(msg string) {
Expand All @@ -647,7 +647,7 @@ func (i Modword) Equal(o Object) bool {
}

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

//
Expand All @@ -672,7 +672,7 @@ func (i LModword) Inspect(e Idxs) string {
}

func (b LModword) Print(e Idxs) string {
return ":" + e.GetWord(b.Index)
return "::" + e.GetWord(b.Index)
}

func (i LModword) Trace(msg string) {
Expand Down Expand Up @@ -1185,8 +1185,14 @@ func NewFunction(spec Block, body Block, pure bool) *Function {
return &o
}

func NewFunctionC(spec Block, body Block, ctx *RyeCtx, pure bool, inCtx bool) *Function {
o := Function{spec.Series.Len(), spec, body, ctx, pure, "", inCtx}
func NewFunctionC(spec Block, body Block, ctx *RyeCtx, pure bool, inCtx bool, doc string) *Function {
var argn int
if doc > "" {
argn = spec.Series.Len() - 1
} else {
argn = spec.Series.Len()
}
o := Function{argn, spec, body, ctx, pure, doc, inCtx}
return &o
}

Expand Down
62 changes: 35 additions & 27 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch word := arg0.(type) {
case env.Word:
return ps.Ctx.Unset(word.Index)
return ps.Ctx.Unset(word.Index, ps.Idx)
default:
return MakeArgError(ps, 1, []env.Type{env.WordType}, "set")
}
Expand Down Expand Up @@ -818,7 +818,7 @@ var builtins = map[string]*env.Builtin{
},
},

"doc": { // ***
"doc!": { // ***
Argsn: 1,
Doc: "Sets docstring of the current context.",
Pure: true,
Expand Down Expand Up @@ -4950,25 +4950,9 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch args := arg0.(type) {
case env.Block:
var doc string
if args.Series.Len() > 0 {
var hasDoc bool
switch a := args.Series.S[len(args.Series.S)-1].(type) {
case env.String:
doc = a.Value
hasDoc = true
//fmt.Println("DOC DOC")
// default:
//return MakeBuiltinError(ps, "Series type should be string.", "fn")
}
for i, o := range args.Series.GetAll() {
if i == len(args.Series.S)-1 && hasDoc {
break
}
if o.Type() != env.WordType {
return MakeBuiltinError(ps, "Function arguments should be words", "fn")
}
}
ok, doc := util.ProcessFunctionSpec(args)
if !ok {
return MakeBuiltinError(ps, doc, "fn")
}
switch body := arg1.(type) {
case env.Block:
Expand All @@ -4992,9 +4976,13 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch args := arg0.(type) {
case env.Block:
ok, doc := util.ProcessFunctionSpec(args)
if !ok {
return MakeBuiltinError(ps, doc, "fn")
}
switch body := arg1.(type) {
case env.Block:
return *env.NewFunction(args, body, true)
return *env.NewFunctionDoc(args, body, true, doc)
default:
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "pfn")
}
Expand All @@ -5015,11 +5003,15 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch args := arg0.(type) {
case env.Block:
ok, doc := util.ProcessFunctionSpec(args)
if !ok {
return MakeBuiltinError(ps, doc, "fn")
}
switch ctx := arg1.(type) {
case env.RyeCtx:
switch body := arg2.(type) {
case env.Block:
return *env.NewFunctionC(args, body, &ctx, false, false)
return *env.NewFunctionC(args, body, &ctx, false, false, doc)
default:
ps.ErrorFlag = true
return MakeArgError(ps, 3, []env.Type{env.BlockType}, "fnc")
Expand All @@ -5046,9 +5038,13 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch args := arg0.(type) {
case env.Block:
ok, doc := util.ProcessFunctionSpec(args)
if !ok {
return MakeBuiltinError(ps, doc, "fn")
}
switch body := arg1.(type) {
case env.Block:
return *env.NewFunctionC(args, body, ps.Ctx, false, false)
return *env.NewFunctionC(args, body, ps.Ctx, false, false, doc)
default:
ps.ErrorFlag = true
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "fnc")
Expand All @@ -5071,11 +5067,15 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch args := arg0.(type) {
case env.Block:
ok, doc := util.ProcessFunctionSpec(args)
if !ok {
return MakeBuiltinError(ps, doc, "fn")
}
switch ctx := arg1.(type) {
case env.RyeCtx:
switch body := arg2.(type) {
case env.Block:
return *env.NewFunctionC(args, body, &ctx, false, false)
return *env.NewFunctionC(args, body, &ctx, false, false, doc)
default:
ps.ErrorFlag = true
return MakeArgError(ps, 3, []env.Type{env.BlockType}, "fnc")
Expand All @@ -5102,11 +5102,15 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch args := arg0.(type) {
case env.Block:
ok, doc := util.ProcessFunctionSpec(args)
if !ok {
return MakeBuiltinError(ps, doc, "fn")
}
switch ctx := arg1.(type) {
case env.RyeCtx:
switch body := arg2.(type) {
case env.Block:
return *env.NewFunctionC(args, body, &ctx, false, true)
return *env.NewFunctionC(args, body, &ctx, false, true, doc)
default:
ps.ErrorFlag = true
return MakeArgError(ps, 3, []env.Type{env.BlockType}, "fnc")
Expand Down Expand Up @@ -5134,9 +5138,13 @@ var builtins = map[string]*env.Builtin{
ctx := ps.Ctx
switch args := arg0.(type) {
case env.Block:
ok, doc := util.ProcessFunctionSpec(args)
if !ok {
return MakeBuiltinError(ps, doc, "fn")
}
switch body := arg1.(type) {
case env.Block:
return *env.NewFunctionC(args, body, ctx, false, false)
return *env.NewFunctionC(args, body, ctx, false, false, doc)
default:
ps.ErrorFlag = true
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "fnc")
Expand Down
26 changes: 11 additions & 15 deletions evaldo/evaldo.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,9 @@ func MaybeEvalOpwordOnRight(nextObj env.Object, ps *env.ProgramState, limited bo
}
//ProcOpword(nextObj, es)
idx := opword.Index
ps.Res = ps.Ctx.Set(idx, ps.Res)
if ps.Res.Type() == env.ErrorType {
ok := ps.Ctx.SetNew(idx, ps.Res, ps.Idx)
if !ok {
ps.Res = *env.NewError("Can't set already set word " + ps.Idx.GetWord(idx) + ", try using modword")
ps.ErrorFlag = true
return ps
}
Expand All @@ -314,11 +315,7 @@ func MaybeEvalOpwordOnRight(nextObj env.Object, ps *env.ProgramState, limited bo
}
//ProcOpword(nextObj, es)
idx := opword.Index
ps.Res = ps.Ctx.Mod(idx, ps.Res)
if ps.Res.Type() == env.ErrorType {
ps.ErrorFlag = true
return ps
}
ps.Ctx.Mod(idx, ps.Res)
ps.Ser.Next()
ps.SkipFlag = false
return MaybeEvalOpwordOnRight(ps.Ser.Peek(), ps, limited)
Expand Down Expand Up @@ -541,22 +538,21 @@ func EvalSetword(ps *env.ProgramState, word env.Setword) *env.ProgramState {
// es1 := EvalExpression(es)
ps1, _ := EvalExpressionInj(ps, nil, false)
idx := word.Index
ps1.Res = ps1.Ctx.Set(idx, ps1.Res)
if ps1.Res.Type() == env.ErrorType {
ps1.ErrorFlag = true
ok := ps1.Ctx.SetNew(idx, ps1.Res, ps.Idx)
if !ok {
ps.Res = *env.NewError("Can't set already set word " + ps.Idx.GetWord(idx) + ", try using modword")
ps.FailureFlag = true
ps.ErrorFlag = true
}
return ps1
return ps
}

// evaluates expression to the right and sets the result of it to a word in current context
func EvalModword(ps *env.ProgramState, word env.Modword) *env.ProgramState {
// es1 := EvalExpression(es)
ps1, _ := EvalExpressionInj(ps, nil, false)
idx := word.Index
ps1.Res = ps1.Ctx.Mod(idx, ps1.Res)
if ps1.Res.Type() == env.ErrorType {
ps1.ErrorFlag = true
}
ps1.Ctx.Mod(idx, ps1.Res)
return ps1
}

Expand Down
17 changes: 17 additions & 0 deletions examples/fib_fac/produce.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

doc! "demonstrates 6 ways to calculate a factorial and one fibbonaci"

fib1: fn { n "recursive" } { either n < 2 { n } { fib0 n - 1 |+ fib0 n - 2 } }

fac1: fn { n "recursive" } { either n > 1 { n * fac1 n - 1 } { 1 } }

fac2: fn1 { i: 0 , .produce 1 { i:: inc i , * i } }

fac3: fn { x "loop" } { acc: 1 loop x { * acc ::acc } }

fac4: fn { n i ac "recur-if\3 set a2 & a3 to 1" } { print i recur-if\3 n > i n i + 1 ac * i }

fac5: fn1 { .range* 1 |fold 'acc 1 { * acc } }

fac6: fn1 { .range* 1 |mul }

6 changes: 6 additions & 0 deletions examples/import/lib1.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
print "Start Lib"

import %lib2.rye

print "End Lib1"

5 changes: 5 additions & 0 deletions examples/import/lib2.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

; |_++++ads(

add5: fn { x } { + 5 }

6 changes: 6 additions & 0 deletions examples/import/test1.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
print "Before"

import %lib1.rye

print "After"

16 changes: 16 additions & 0 deletions examples/json/json-lincenses-example.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; blogpost:
; lobster:
; curl 'https://api.github.com/orgs/golang/repos' > repos.json

get https://api.github.com/orgs/golang/repos
|parse-json
|map { -> "license" |fix\either { "no" } { -> "key" } }
|spreadsheet* { "license" }
|group-by 'license { 'license count } |display

; | license | license_count |
; +------------------------------+
; | bsd-3-clause | 23 |
; | apache-2.0 | 5 |
; | no | 2 |

4 changes: 2 additions & 2 deletions examples/openai/petcare_step1.rye
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ print "Loading Questions and Answers:"
read %petcare.txt
|split newline
|purge { .length? = 0 }
|new-spreadsheet* { "text" }
|spreadsheet* { "text" }
|pass { .display , print "Creating embeddings ..." }
|gen-col 'embedding { text } { .create-embeddings* ai } :spr
|add-col! 'embedding { text } { .create-embeddings* ai } :spr

get-input "Enter your search phrase: "
|create-embeddings* ai :q1
Expand Down
Loading

0 comments on commit 866ce7e

Please sign in to comment.