diff --git a/ast/func_table.go b/ast/func_table.go index 001f149..81e006b 100644 --- a/ast/func_table.go +++ b/ast/func_table.go @@ -123,11 +123,9 @@ func (f Function) LazyParseFunction(args *Token) bool { * calls ParseFunction and increments TimesCalled */ func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token { - var n_args *Token + n_args := args if !f.EvalLazy { n_args = args.Eval(ft, vt, f.SymLazy) - } else { - n_args = args } passes := false diff --git a/stdlib/shell.go b/stdlib/shell.go index 0ad770d..823bff3 100644 --- a/stdlib/shell.go +++ b/stdlib/shell.go @@ -225,6 +225,11 @@ func LaunchProcess( * Example (l vim file.txt) */ func Call(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { + if in == nil { + log.Log(log.ERR, "no arguments given", "call") + return nil + } + if in.Tag == ast.LIST { log.Log(log.ERR, "couldnt exec, target bin is a list", "call") return nil diff --git a/stdlib/stdlib.go b/stdlib/stdlib.go index dd70b5e..7f85b05 100644 --- a/stdlib/stdlib.go +++ b/stdlib/stdlib.go @@ -178,6 +178,16 @@ func GenFuncTable() ast.FuncTable { }, }, + "replace": &ast.Function{ + Function: Replace, + Name: "replace", + Args: []ast.Token_t{ + ast.STRING, + ast.STRING, + ast.STRING, + }, + }, + "substr": &ast.Function{ Function: Substr, Name: "substr", @@ -283,6 +293,7 @@ func GenFuncTable() ast.FuncTable { "cd": &ast.Function{ Function: Cd, Name: "changedir", + SymLazy: true, Args: []ast.Token_t{ ast.STRING, }, diff --git a/stdlib/string.go b/stdlib/string.go index 090932e..b3d7a72 100644 --- a/stdlib/string.go +++ b/stdlib/string.go @@ -84,6 +84,25 @@ func Split(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { return res } +/* Takes three args + * 1. source string + * 2. token to be replaced + * 3. token to swap in place + * All three args are strings + * Returns final string + */ +func Replace(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { + body := in.Value() + sub := in.Next.Value() + tok := in.Next.Next.Value() + + body = strings.ReplaceAll(body, sub, tok) + + res := &ast.Token{Tag: ast.STRING} + res.Set(body) + return res +} + /* Takes two args, a delimiter and a list of strings * Returns the list of strings concatenated together with the delimiter in between each element * On error returns nil