Skip to content

Commit

Permalink
Merge pull request #336 from refaktor/allin
Browse files Browse the repository at this point in the history
improvements to os and pipes, and to eyr implementation
  • Loading branch information
refaktor authored Sep 4, 2024
2 parents 82d7b37 + 3ebe85e commit 06ee278
Show file tree
Hide file tree
Showing 10 changed files with 537 additions and 479 deletions.
9 changes: 9 additions & 0 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ func NewUri1(index *Idxs, path string) *Uri {
return &nat
}

func NewFileUri(index *Idxs, path string) *Uri {
scheme := "file" // + "-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}}
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
Expand Down
38 changes: 31 additions & 7 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,10 @@ var builtins = map[string]*env.Builtin{
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 val := arg0.(type) {
case env.Integer:
return *env.NewFileUri(ps.Idx, strconv.Itoa(int(val.Value))) // TODO turn to switch
case env.String:
return *env.NewUri1(ps.Idx, "file://"+val.Value) // TODO turn to switch
return *env.NewFileUri(ps.Idx, val.Value) // TODO turn to switch
default:
return MakeArgError(ps, 1, []env.Type{env.StringType}, "to-file")
}
Expand Down Expand Up @@ -2884,7 +2886,7 @@ var builtins = map[string]*env.Builtin{
},
},

"ls": {
"lc": {
Argsn: 0,
Doc: "Lists words in current context",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -2893,7 +2895,7 @@ var builtins = map[string]*env.Builtin{
},
},

"lsp": {
"lcp": {
Argsn: 0,
Doc: "Lists words in current context",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -2906,7 +2908,7 @@ var builtins = map[string]*env.Builtin{
},
},

"ls\\": {
"lc\\": {
Argsn: 1,
Doc: "Lists words in current context with string filter",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -2922,7 +2924,7 @@ var builtins = map[string]*env.Builtin{
}
},
},
"lsp\\": {
"lcp\\": {
Argsn: 1,
Doc: "Lists words in current context with string filter",
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 @@ -7855,7 +7857,8 @@ func RegisterBuiltins(ps *env.ProgramState) {
RegisterBuiltins2(Builtins_ssh, ps, "ssh")
RegisterBuiltins2(Builtins_console, ps, "console")
RegisterBuiltinsInContext(Builtins_math, ps, "math")
RegisterBuiltinsInContext(Builtins_devops, ps, "os")
osctx := RegisterBuiltinsInContext(Builtins_os, ps, "os")
RegisterBuiltinsInSubContext(Builtins_pipes, ps, osctx, "pipes")
// ## Archived modules
// RegisterBuiltins2(Builtins_gtk, ps, "gtk")
// RegisterBuiltins2(Builtins_nats, ps, "nats")
Expand All @@ -7875,7 +7878,7 @@ func RegisterBuiltins2(builtins map[string]*env.Builtin, ps *env.ProgramState, n
}
}

func RegisterBuiltinsInContext(builtins map[string]*env.Builtin, ps *env.ProgramState, name string) {
func RegisterBuiltinsInContext(builtins map[string]*env.Builtin, ps *env.ProgramState, name string) *env.RyeCtx {
BuiltinNames[name] = len(builtins)

ctx := ps.Ctx
Expand All @@ -7890,6 +7893,27 @@ func RegisterBuiltinsInContext(builtins map[string]*env.Builtin, ps *env.Program

wordIdx := ps.Idx.IndexWord(name)
ps.Ctx.Mod(wordIdx, *newctx)

return newctx
}

func RegisterBuiltinsInSubContext(builtins map[string]*env.Builtin, ps *env.ProgramState, parent *env.RyeCtx, name string) *env.RyeCtx {
BuiltinNames[name] = len(builtins)

ctx := ps.Ctx
ps.Ctx = env.NewEnv(parent) // make new context with no parent

for k, v := range builtins {
bu := env.NewBuiltin(v.Fn, v.Argsn, v.AcceptFailure, v.Pure, v.Doc+" ("+k+")")
registerBuiltin(ps, k, *bu)
}
newctx := ps.Ctx
ps.Ctx = ctx

wordIdx := ps.Idx.IndexWord(name)
ps.Ctx.Mod(wordIdx, *newctx)

return newctx
}

func registerBuiltin(ps *env.ProgramState, word string, builtin env.Builtin) {
Expand Down
5 changes: 4 additions & 1 deletion evaldo/builtins_eyr.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,11 @@ func Eyr_EvalExpression(ps *env.ProgramState) *env.ProgramState {
return ps
}

func Eyr_EvalBlockInside(ps *env.ProgramState) *env.ProgramState {
func Eyr_EvalBlockInside(ps *env.ProgramState, inj env.Object, injnow bool) *env.ProgramState {
// fmt.Println("** EVALB INSIDE")
if injnow {
ps.Stack.Push(ps, inj)
}
for ps.Ser.Pos() < ps.Ser.Len() {
// fmt.Println(ps.Ser.Pos())
ps = Eyr_EvalExpression(ps)
Expand Down
Loading

0 comments on commit 06ee278

Please sign in to comment.