perform arg type checking and evaluation before function call
This commit is contained in:
parent
90284f2d06
commit
ab340ceb0a
9 changed files with 223 additions and 354 deletions
|
|
@ -54,20 +54,6 @@ func AbsPath(arg string) string {
|
|||
* (cd (concat HOME "/go"))
|
||||
*/
|
||||
func Cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, true)
|
||||
|
||||
if in == nil {
|
||||
log.Log(log.ERR,
|
||||
"arguments to cd evaluated to nil!",
|
||||
"cd")
|
||||
return nil
|
||||
}
|
||||
|
||||
if in.Tag == ast.LIST {
|
||||
log.Log(log.ERR, "Couldnt change dir to a list", "cd")
|
||||
return nil
|
||||
}
|
||||
|
||||
err := os.Chdir(in.Value())
|
||||
if err != nil {
|
||||
log.Log(log.ERR, err.Error(), "cd")
|
||||
|
|
@ -84,14 +70,6 @@ func Cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
* (fexists test)
|
||||
*/
|
||||
func Fexists(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
if in == nil || in.Tag != ast.STRING {
|
||||
log.Log(log.ERR,
|
||||
"argument to fexists must be a string",
|
||||
"fexists")
|
||||
return nil
|
||||
}
|
||||
|
||||
filename := in.Value()
|
||||
out := ast.TRUE
|
||||
|
||||
|
|
@ -115,7 +93,6 @@ func Fexists(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
* (fread (concat HOME ".shsrc"))
|
||||
*/
|
||||
func Fread(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
exists := Fexists(in, vt, ft) // some waste, extra use of Eval
|
||||
if exists == nil || exists.Tag != ast.BOOL || exists.Value() == ast.FALSE {
|
||||
log.Log(log.ERR,
|
||||
|
|
@ -146,26 +123,12 @@ func Fread(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
* (fwrite "test" "one two three")
|
||||
*/
|
||||
func Fwrite(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
if in == nil || in.Tag == ast.SYMBOL || in.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"first argument must be a filename",
|
||||
"fwrite")
|
||||
return nil
|
||||
}
|
||||
|
||||
text := in.Next
|
||||
if text == nil || text.Tag == ast.SYMBOL || text.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"second argument must be stringable",
|
||||
"fwrite")
|
||||
return nil
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(
|
||||
AbsPath(in.Value()),
|
||||
[]byte(text.Value()),
|
||||
0644)
|
||||
|
||||
if err != nil {
|
||||
log.Log(log.ERR,
|
||||
"error writing file: " + err.Error(),
|
||||
|
|
@ -183,22 +146,7 @@ func Fwrite(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
* (fwrite "test" "one two three")
|
||||
*/
|
||||
func Fappend(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
if in == nil || in.Tag == ast.SYMBOL || in.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"first argument must be a filename",
|
||||
"fappend")
|
||||
return nil
|
||||
}
|
||||
|
||||
text := in.Next
|
||||
if text == nil || text.Tag == ast.SYMBOL || text.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"second argument must be stringable",
|
||||
"fappend")
|
||||
return nil
|
||||
}
|
||||
|
||||
exists := Fexists(in, vt, ft)
|
||||
if exists.Value() == ast.FALSE {
|
||||
log.Log(log.ERR,
|
||||
|
|
@ -212,14 +160,15 @@ func Fappend(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
AbsPath(in.Value()),
|
||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
|
||||
0644)
|
||||
|
||||
if err != nil {
|
||||
log.Log(log.ERR,
|
||||
"couldnt open file for append: " + err.Error(),
|
||||
"fappend")
|
||||
return nil
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
defer f.Close()
|
||||
if _, err := f.WriteString(text.Value()); err != nil {
|
||||
log.Log(log.ERR,
|
||||
"error appending to file: " + err.Error(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue