perform arg type checking and evaluation before function call

This commit is contained in:
Aidan 2020-08-21 01:37:04 -07:00
parent 90284f2d06
commit ab340ceb0a
No known key found for this signature in database
GPG key ID: 327711E983899316
9 changed files with 223 additions and 354 deletions

View file

@ -30,8 +30,6 @@ import (
* Example: (concat "hello" " " "world")
*/
func Concat(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
var res string
for i := in; i != nil; i = i.Next {
if i.Tag == ast.LIST {
@ -54,7 +52,7 @@ func Concat(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (string 1) -> 1.0
*/
func StrCast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
body := in.Eval(ft, vt, false).String()
body := in.String()
res := &ast.Token{ Tag: ast.STRING }
res.Set(body)
return res
@ -67,22 +65,8 @@ func StrCast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (split "/path/to/file" "/") -> ("path" "to" "file")
*/
func Split(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
if in == nil || in.Tag != ast.STRING || in.Next == nil || in.Next.Tag != ast.STRING {
log.Log(log.ERR,
"Args must be two strings",
"split")
return nil
}
body := in.Value()
delim := in.Next.Value()
if len(body) < len(delim) {
log.Log(log.DEBUG,
"possibly mismatched args" +
"delimiter longer than body",
"split")
}
var res *ast.Token
builder := &res
@ -107,25 +91,11 @@ func Split(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (join ", " ("apple" "ananas" "pear")) -> "apple, ananas, pear"
*/
func Join(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
if in == nil || in.Next == nil {
log.Log(log.ERR,
"one or more arguments evaluated to nil",
"join")
return nil
}
delim := in
strs := in.Next
if delim.Tag != ast.STRING || strs.Tag != ast.LIST {
log.Log(log.ERR,
"first argument must be a string (delimiter) and second argument must be a list (strings)",
"join")
return nil
}
de := delim.Value()
res := ""
for i := strs.Expand(); i != nil; i = i.Next {
if i.Tag != ast.STRING {
log.Log(log.ERR,
@ -155,24 +125,9 @@ func Join(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (substr 1 5 "Linus Torvalds") -> "inus "
*/
func Substr(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
start := in.Eval(ft, vt, false)
if start == nil || start.Next == nil || start.Next.Next == nil {
log.Log(log.ERR,
"an argument evaluated to nil",
"substr")
return nil
}
end := start.Next
str := end.Next
if start.Tag != ast.NUMBER || end.Tag != ast.NUMBER || str.Tag != ast.STRING {
log.Log(log.ERR,
"incorrect types of args",
"substr")
return nil
}
ed_idx := 0
st_idx, err := strconv.Atoi(start.Value())
ed_idx, err = strconv.Atoi(end.Value())
@ -226,7 +181,7 @@ func Substr(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (print "Line: \n, Tab: \t")
*/
func PrintStr(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
body := in.Eval(ft, vt, false).String()
body := in.String()
if body[0] != body[len(body)-1] && body[0] != '"' {
body = "`" + body + "`"
}