file operations in stdlib
This commit is contained in:
parent
1802bce604
commit
de3e3e5d4e
3 changed files with 177 additions and 1 deletions
|
|
@ -43,9 +43,9 @@ func not(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
|
|
||||||
func eq(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
func eq(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
out := ast.TRUE
|
out := ast.TRUE
|
||||||
second := in.Next
|
|
||||||
|
|
||||||
in = in.Eval(ft, vt, false)
|
in = in.Eval(ft, vt, false)
|
||||||
|
second := in.Next
|
||||||
|
|
||||||
if in.Tag != second.Tag {
|
if in.Tag != second.Tag {
|
||||||
out = ast.FALSE
|
out = ast.FALSE
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,33 @@ package stdlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"io/ioutil"
|
||||||
"gitlab.com/whom/shs/ast"
|
"gitlab.com/whom/shs/ast"
|
||||||
"gitlab.com/whom/shs/log"
|
"gitlab.com/whom/shs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/* Take a path, return the absolute path
|
||||||
|
* does not verify that the absolute path is correct
|
||||||
|
* currently only supports paths using forward slashes
|
||||||
|
*
|
||||||
|
* TODO: handle ~
|
||||||
|
*/
|
||||||
|
func AbsPath(arg string) string {
|
||||||
|
if arg[0] != '/' {
|
||||||
|
dir, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
log.Log(log.ERR,
|
||||||
|
"Couldnt get working directory: " + err.Error(),
|
||||||
|
"path")
|
||||||
|
return arg
|
||||||
|
}
|
||||||
|
|
||||||
|
return dir + "/" + arg
|
||||||
|
}
|
||||||
|
|
||||||
|
return arg
|
||||||
|
}
|
||||||
|
|
||||||
func cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
func cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
in = in.Eval(ft, vt, true)
|
in = in.Eval(ft, vt, true)
|
||||||
|
|
||||||
|
|
@ -44,3 +67,128 @@ func cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.NUMBER && in.Tag != ast.STRING) {
|
||||||
|
log.Log(log.ERR,
|
||||||
|
"argument to fexists must be a string or number",
|
||||||
|
"fexists")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := in.Value()
|
||||||
|
out := ast.TRUE
|
||||||
|
|
||||||
|
if _, err := os.Stat(AbsPath(filename)); err != nil {
|
||||||
|
log.Log(log.DEBUG,
|
||||||
|
"couldnt stat file: " + err.Error(),
|
||||||
|
"fexists")
|
||||||
|
out = ast.FALSE
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &ast.Token{Tag: ast.BOOL}
|
||||||
|
ret.Set(out)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
"error calling fexists or file doesnt exist",
|
||||||
|
"fread")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fname := in.Value()
|
||||||
|
text, err := ioutil.ReadFile(fname)
|
||||||
|
if err != nil {
|
||||||
|
log.Log(log.ERR,
|
||||||
|
"error reading file" + err.Error(),
|
||||||
|
"fread")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &ast.Token{Tag: ast.STRING}
|
||||||
|
ret.Set(string(text))
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
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(),
|
||||||
|
"fwrite")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
"file "+in.Value()+" does not exist",
|
||||||
|
"fappend")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
f, err := os.OpenFile(
|
||||||
|
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()
|
||||||
|
|
||||||
|
if _, err := f.WriteString(text.Value()); err != nil {
|
||||||
|
log.Log(log.ERR,
|
||||||
|
"error appending to file: " + err.Error(),
|
||||||
|
"fappend")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -232,6 +232,34 @@ func GenFuncTable() ast.FuncTable {
|
||||||
TimesCalled: 0,
|
TimesCalled: 0,
|
||||||
Args: 1,
|
Args: 1,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"fexists": &ast.Function{
|
||||||
|
Function: fexists,
|
||||||
|
Name: "file exists",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: 1,
|
||||||
|
},
|
||||||
|
|
||||||
|
"fread": &ast.Function{
|
||||||
|
Function: fread,
|
||||||
|
Name: "read file",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: 1,
|
||||||
|
},
|
||||||
|
|
||||||
|
"fwrite": &ast.Function{
|
||||||
|
Function: fwrite,
|
||||||
|
Name: "write file",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: 2,
|
||||||
|
},
|
||||||
|
|
||||||
|
"fappend": &ast.Function{
|
||||||
|
Function: fappend,
|
||||||
|
Name:"append to file",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: 2,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return stdlib
|
return stdlib
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue