Skip to content

Commit

Permalink
added chat-tui example, improved http integration, webapp example, te…
Browse files Browse the repository at this point in the history
…rminal support
  • Loading branch information
refaktor committed Feb 27, 2023
1 parent 319f16e commit 00684db
Show file tree
Hide file tree
Showing 18 changed files with 586 additions and 105 deletions.
47 changes: 0 additions & 47 deletions docs/rye_via_python/first_10.py

This file was deleted.

47 changes: 0 additions & 47 deletions docs/rye_via_python/first_10.rye

This file was deleted.

158 changes: 155 additions & 3 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,35 @@ var builtins = map[string]*env.Builtin{
return arg0
},
},
"display": {
"esc": {
Argsn: 1,
Doc: "Creates an escape sequence \033{}",
Fn: func(env1 *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch arg := arg0.(type) {
case env.String:
return env.String{"\033" + arg.Value}
default:
return makeError(env1, "Arg 0 not String")
}
return arg0
},
},
"esc-val": {
Argsn: 2,
Doc: "Escapes a value and adds a newline.",
Fn: func(env1 *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch base := arg1.(type) {
case env.String:
vals := arg0.Probe(*env1.Idx)
news := strings.ReplaceAll(base.Value, "(*)", vals)
return env.String{"\033" + news}
default:
return makeError(env1, "Arg 0 not String")
}
return arg0
},
},
"interact": {
Argsn: 1,
Doc: "Work in progress Interactively displays a value.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down Expand Up @@ -962,6 +990,23 @@ var builtins = map[string]*env.Builtin{
return nil
},
},
"tui\\selection": {
Argsn: 2,
Doc: "Work in progress Interactively displays a value.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
// This is temporary implementation for experimenting what it would work like at all
// later it should belong to the object (and the medium of display, terminal, html ..., it's part of the frontend)
term.SaveCurPos()
switch bloc := arg0.(type) {
case env.Block:
obj, esc := term.DisplaySelection(bloc, ps.Idx, int(arg1.(env.Integer).Value))
if !esc {
return obj
}
}
return nil
},
},
"mold": {
Argsn: 1,
Doc: "Turn value to it's string representation.",
Expand Down Expand Up @@ -1904,6 +1949,44 @@ var builtins = map[string]*env.Builtin{
},
},

"wrap": {
Argsn: 2,
Doc: "Accepts a value and a block. It does the block, with value injected, and returns (passes on) the initial value.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch wrap := arg0.(type) {
case env.Block:
switch bloc := arg1.(type) {
case env.Block:
ser := ps.Ser
ps.Ser = wrap.Series
EvalBlockInj(ps, arg0, true)
if ps.ReturnFlag {
return ps.Res
}

ps.Ser = bloc.Series
EvalBlockInj(ps, arg0, true)
if ps.ReturnFlag {
return ps.Res
}
res := ps.Res

ps.Ser = wrap.Series
EvalBlockInj(ps, arg0, true)
ps.Ser = ser
if ps.ReturnFlag {
return ps.Res
}
return res
default:
return makeError(ps, "Arg 2 should be Block.")
}
default:
return makeError(ps, "Arg 2 should be Block.")
}
},
},

"keep": {
Argsn: 3,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down Expand Up @@ -2106,8 +2189,8 @@ var builtins = map[string]*env.Builtin{
case env.Block:
ser := ps.Ser
ps.Ser = code.Series
for i := 0; i < len(block.Value); i++ {
ps = EvalBlockInj(ps, env.String{string(block.Value[i])}, true)
for _, ch := range block.Value {
ps = EvalBlockInj(ps, env.String{string(ch)}, true)
if ps.ErrorFlag {
return ps.Res
}
Expand Down Expand Up @@ -2355,6 +2438,75 @@ var builtins = map[string]*env.Builtin{
},
},

"map\\pos": {
Argsn: 3,
Doc: "Maps values of a block to a new block by evaluating a block of code.",
Pure: true,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch list := arg0.(type) {
case env.Block:
switch accu := arg1.(type) {
case env.Word:
switch block := arg2.(type) {
case env.Block, env.Builtin:
l := list.Series.Len()
newl := make([]env.Object, l)
switch block := block.(type) {
case env.Block:
ser := ps.Ser
ps.Ser = block.Series
for i := 0; i < l; i++ {
ps.Ctx.Set(accu.Index, env.Integer{int64(i + 1)})
ps = EvalBlockInj(ps, list.Series.Get(i), true)
if ps.ErrorFlag {
return ps.Res
}
newl[i] = ps.Res
ps.Ser.Reset()
}
ps.Ser = ser
case env.Builtin:
for i := 0; i < l; i++ {
newl[i] = DirectlyCallBuiltin(ps, block, list.Series.Get(i), nil)
}
}
return *env.NewBlock(*env.NewTSeries(newl))
}
}
case env.List:
switch block := arg1.(type) {
case env.Block, env.Builtin:
l := len(list.Data)
newl := make([]interface{}, l)
switch accu := arg1.(type) {
case env.Word:
switch block := block.(type) {
case env.Block:
ser := ps.Ser
ps.Ser = block.Series
for i := 0; i < l; i++ {
ps.Ctx.Set(accu.Index, env.Integer{int64(i + 1)})
ps = EvalBlockInj(ps, JsonToRye(list.Data[i]), true)
if ps.ErrorFlag {
return ps.Res
}
newl[i] = ps.Res
ps.Ser.Reset()
}
ps.Ser = ser
case env.Builtin:
for i := 0; i < l; i++ {
newl[i] = DirectlyCallBuiltin(ps, block, JsonToRye(list.Data[i]), nil)
}
}
return *env.NewList(newl)
}
}
}
return nil
},
},

// map should at the end map over block, raw-map, etc ...
// it should accept a block of code, a function and a builtin
// it should use injected block so it doesn't need a variable defined like map [ 1 2 3 ] x [ add a 100 ]
Expand Down
49 changes: 49 additions & 0 deletions evaldo/builtins_http.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build b_http
// +build b_http

package evaldo
Expand Down Expand Up @@ -415,6 +416,54 @@ var Builtins_http = map[string]*env.Builtin{
},
},

"Go-server-request//parse-multipart-form!": {
Argsn: 1,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch req := arg0.(type) {
case env.Native:
r := req.Value.(*http.Request)
// 10 MB files max
r.ParseMultipartForm(10 << 20)
return arg0
default:
ps.FailureFlag = true
return env.NewError("first arg should be Native")
}
},
},

// file-handler: r.form-file "image"
// dst: create file-handler

"Go-server-request//form-file?": {
Argsn: 2,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch req := arg0.(type) {
case env.Native:
switch key := arg1.(type) {
case env.String:
r := req.Value.(*http.Request)

file, handler, err := r.FormFile(key.Value)
if err != nil {
ps.FailureFlag = true
return env.NewError("couldn't read form file")
}
pair := make([]env.Object, 2)
pair[0] = env.NewNative(ps.Idx, file, "rye-multipart-file")
pair[1] = env.NewNative(ps.Idx, handler, "rye-multipart-handler")
return *env.NewBlock(*env.NewTSeries(pair))
default:
ps.FailureFlag = true
return env.NewError("second arg should be String")
}
default:
ps.FailureFlag = true
return env.NewError("first arg should be Native")
}
},
},

"new-cookie-store": {
Argsn: 1,
Fn: func(env1 *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down
Loading

0 comments on commit 00684db

Please sign in to comment.