add a string replace method

This commit is contained in:
Aidan 2020-08-26 22:32:01 -07:00
parent 591402b428
commit 12caeedf68
No known key found for this signature in database
GPG key ID: 327711E983899316
4 changed files with 36 additions and 3 deletions

View file

@ -123,11 +123,9 @@ func (f Function) LazyParseFunction(args *Token) bool {
* calls ParseFunction and increments TimesCalled * calls ParseFunction and increments TimesCalled
*/ */
func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token { func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
var n_args *Token n_args := args
if !f.EvalLazy { if !f.EvalLazy {
n_args = args.Eval(ft, vt, f.SymLazy) n_args = args.Eval(ft, vt, f.SymLazy)
} else {
n_args = args
} }
passes := false passes := false

View file

@ -225,6 +225,11 @@ func LaunchProcess(
* Example (l vim file.txt) * Example (l vim file.txt)
*/ */
func Call(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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 { if in.Tag == ast.LIST {
log.Log(log.ERR, "couldnt exec, target bin is a list", "call") log.Log(log.ERR, "couldnt exec, target bin is a list", "call")
return nil return nil

View file

@ -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{ "substr": &ast.Function{
Function: Substr, Function: Substr,
Name: "substr", Name: "substr",
@ -283,6 +293,7 @@ func GenFuncTable() ast.FuncTable {
"cd": &ast.Function{ "cd": &ast.Function{
Function: Cd, Function: Cd,
Name: "changedir", Name: "changedir",
SymLazy: true,
Args: []ast.Token_t{ Args: []ast.Token_t{
ast.STRING, ast.STRING,
}, },

View file

@ -84,6 +84,25 @@ func Split(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
return res 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 /* Takes two args, a delimiter and a list of strings
* Returns the list of strings concatenated together with the delimiter in between each element * Returns the list of strings concatenated together with the delimiter in between each element
* On error returns nil * On error returns nil