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

@ -38,8 +38,18 @@ type Function struct {
// number of times user has called this function // number of times user has called this function
TimesCalled int TimesCalled int
// number of args required // list of types (LIST, NUMBER, STRING, etc) representing args
Args int Args []Token_t
NumArgs bool // -1 means infinite
// lazy arg checking (use NumArgs instead of args)
ArgLazy bool
// dont fail on undefined symbol (passed to eval when parsing args)
SymLazy bool
// dont eval args at all, leave that to the function
EvalLazy bool
} }
/* holds a mapping of key to function /* holds a mapping of key to function
@ -52,24 +62,52 @@ type FuncTable *map[string]*Function
* makes sure correct arguments are passed in * makes sure correct arguments are passed in
*/ */
func (f Function) ParseFunction(args *Token) bool { func (f Function) ParseFunction(args *Token) bool {
// handle infinite args total = len(f.Args)
if f.Args < 0 { for iter := args; iter != nil; iter = iter.Next {
total -= 1
if total <= 0 {
log.Log(log.ERR,
"too many arguments",
"ftable")
return false
}
if iter.Tag != f.Args[len(f.Args) - total] {
log.Log(log.ERR,
"argument of type " + GetTagAsStr(iter.Tag) +
"passed in when " + GetTagAsStr(f.Args[len(f.Args) - total]) +
" was expected",
"ftable")
return false
}
}
return true
}
/* same as ParseFunction but only evaluates the number of args
*/
func (f Function) LazyParseFunction(args *Token) bool {
if (f.NumArgs < 0) {
return true return true
} }
i := f.Args total := 0
for iter := args; iter != nil; iter = iter.Next { for iter := args; iter != nil; iter = iter.Next {
i -= 1 total += 1
} }
if i != 0 { if total < f.NumArgs {
log.Log(log.ERR, log.Log(log.ERR,
"Incorrect number of arguments", "expected more arguments, try calling info on function",
"eval") "ftable")
log.Log(log.DEBUG, return false
fmt.Sprintf("Function %s expects %d arguments. You've provided %d arguments.", }
f.Name, f.Args, f.Args - i),
"eval") if total > f.NumArgs {
log.Log(log.ERR,
"too many args. try calling info on function",
"ftable")
return false return false
} }
@ -80,7 +118,18 @@ func (f Function) ParseFunction(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 {
if !f.ParseFunction(args) { if not f.EvalLazy {
args = args.Eval(ft, vt, !f.SymLazy)
}
passes := false
if f.ArgsLazy {
passes = LazyParseFunction(args)
} else {
passes = ParseFunction(args)
}
if passes {
log.Log(log.ERR, log.Log(log.ERR,
"Couldnt call " + f.Name, "Couldnt call " + f.Name,
"eval") "eval")

View file

@ -36,14 +36,6 @@ import (
* Example: (number "3.4") * Example: (number "3.4")
*/ */
func NumCast(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func NumCast(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
in = in.Eval(f, a, false)
if in.Tag != ast.STRING {
log.Log(log.ERR,
"only a string can successfully be cast to a number",
"number_cast")
return nil
}
if !ast.StrIsNumber(in.Value()) { if !ast.StrIsNumber(in.Value()) {
log.Log(log.ERR, log.Log(log.ERR,
"string failed number cast", "string failed number cast",
@ -64,9 +56,6 @@ func NumCast(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
*/ */
func Add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func Add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
var res float64 var res float64
in = in.Eval(f, a, false)
for i := in; i != nil; i = i.Next { for i := in; i != nil; i = i.Next {
if i.Tag != ast.NUMBER { if i.Tag != ast.NUMBER {
log.Log(log.ERR, "Non-number given to ADD", "add") log.Log(log.ERR, "Non-number given to ADD", "add")
@ -112,9 +101,6 @@ func Add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
func Sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func Sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
var res float64 var res float64
var sub float64 var sub float64
in = in.Eval(f, a, false)
for i := in; i != nil; i = i.Next { for i := in; i != nil; i = i.Next {
if i.Tag != ast.NUMBER { if i.Tag != ast.NUMBER {
log.Log(log.ERR, "Non-number given to SUB", "sub") log.Log(log.ERR, "Non-number given to SUB", "sub")
@ -166,9 +152,6 @@ func Sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
*/ */
func Mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func Mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
res := 1.0 res := 1.0
in = in.Eval(f, a, false)
for i := in; i != nil; i = i.Next { for i := in; i != nil; i = i.Next {
if i.Tag != ast.NUMBER { if i.Tag != ast.NUMBER {
log.Log(log.ERR, "Non-number given to MULT", "mult") log.Log(log.ERR, "Non-number given to MULT", "mult")
@ -214,9 +197,6 @@ func Mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
*/ */
func Div(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func Div(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
var res float64 var res float64
in = in.Eval(f, a, false)
for i := in; i != nil; i = i.Next { for i := in; i != nil; i = i.Next {
inner := 0.0 inner := 0.0

View file

@ -30,14 +30,6 @@ import (
* Example: (bool "F") * Example: (bool "F")
*/ */
func BoolCast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func BoolCast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
if in.Tag == ast.LIST || in.Tag == ast.NUMBER {
log.Log(log.ERR,
"only strings successfully cast to bool",
"bool cast")
return nil
}
body := in.Value() body := in.Value()
if body != ast.TRUE && body != ast.FALSE { if body != ast.TRUE && body != ast.FALSE {
log.Log(log.ERR, log.Log(log.ERR,
@ -52,13 +44,6 @@ func BoolCast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
} }
func Not(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func Not(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
if in.Tag != ast.BOOL {
log.Log(log.ERR, "non-bool argument to 'not'", "not")
return nil
}
out := ast.TRUE out := ast.TRUE
if in.Value() == ast.TRUE { if in.Value() == ast.TRUE {
out = ast.FALSE out = ast.FALSE
@ -69,10 +54,9 @@ func Not(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
return t return t
} }
// Lazy args this
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
in = in.Eval(ft, vt, false)
second := in.Next second := in.Next
if in.Tag != second.Tag { if in.Tag != second.Tag {
@ -155,13 +139,6 @@ func Lt(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
out := ast.TRUE out := ast.TRUE
second := in.Next second := in.Next
in = in.Eval(ft, vt, false)
if in.Tag != ast.NUMBER || second.Tag != ast.NUMBER {
log.Log(log.ERR, "non-number argument to numeric boolean operator", ">/<=")
return nil
}
l, _ := strconv.ParseInt(in.Value(), 10, 64) l, _ := strconv.ParseInt(in.Value(), 10, 64)
r, _ := strconv.ParseInt(second.Value(), 10, 64) r, _ := strconv.ParseInt(second.Value(), 10, 64)
@ -178,13 +155,6 @@ func Gt(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
out := ast.TRUE out := ast.TRUE
second := in.Next second := in.Next
in = in.Eval(ft, vt, false)
if in.Tag != ast.NUMBER || second.Tag != ast.NUMBER {
log.Log(log.ERR, "non-number argument to numeric boolean operator", ">/<=")
return nil
}
l, _ := strconv.ParseInt(in.Value(), 10, 64) l, _ := strconv.ParseInt(in.Value(), 10, 64)
r, _ := strconv.ParseInt(second.Value(), 10, 64) r, _ := strconv.ParseInt(second.Value(), 10, 64)

View file

@ -54,20 +54,6 @@ func AbsPath(arg string) string {
* (cd (concat HOME "/go")) * (cd (concat HOME "/go"))
*/ */
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)
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()) err := os.Chdir(in.Value())
if err != nil { if err != nil {
log.Log(log.ERR, err.Error(), "cd") 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) * (fexists test)
*/ */
func Fexists(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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() filename := in.Value()
out := ast.TRUE out := ast.TRUE
@ -115,7 +93,6 @@ func Fexists(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* (fread (concat HOME ".shsrc")) * (fread (concat HOME ".shsrc"))
*/ */
func Fread(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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 exists := Fexists(in, vt, ft) // some waste, extra use of Eval
if exists == nil || exists.Tag != ast.BOOL || exists.Value() == ast.FALSE { if exists == nil || exists.Tag != ast.BOOL || exists.Value() == ast.FALSE {
log.Log(log.ERR, 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") * (fwrite "test" "one two three")
*/ */
func Fwrite(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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 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( err := ioutil.WriteFile(
AbsPath(in.Value()), AbsPath(in.Value()),
[]byte(text.Value()), []byte(text.Value()),
0644) 0644)
if err != nil { if err != nil {
log.Log(log.ERR, log.Log(log.ERR,
"error writing file: " + err.Error(), "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") * (fwrite "test" "one two three")
*/ */
func Fappend(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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 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) exists := Fexists(in, vt, ft)
if exists.Value() == ast.FALSE { if exists.Value() == ast.FALSE {
log.Log(log.ERR, log.Log(log.ERR,
@ -212,14 +160,15 @@ func Fappend(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
AbsPath(in.Value()), AbsPath(in.Value()),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.O_APPEND|os.O_CREATE|os.O_WRONLY,
0644) 0644)
if err != nil { if err != nil {
log.Log(log.ERR, log.Log(log.ERR,
"couldnt open file for append: " + err.Error(), "couldnt open file for append: " + err.Error(),
"fappend") "fappend")
return nil return nil
} }
defer f.Close()
defer f.Close()
if _, err := f.WriteString(text.Value()); err != nil { if _, err := f.WriteString(text.Value()); err != nil {
log.Log(log.ERR, log.Log(log.ERR,
"error appending to file: " + err.Error(), "error appending to file: " + err.Error(),

View file

@ -113,7 +113,8 @@ func DeclFunc(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Tok
Function: inner, Function: inner,
Name: name.Value(), Name: name.Value(),
TimesCalled: 0, TimesCalled: 0,
Args: numArgs, NumArgs: numArgs,
LazyArgs: true
} }
return nil return nil
} }

View file

@ -30,13 +30,7 @@ import (
* in event a not-list is passed in, returns the arg. * in event a not-list is passed in, returns the arg.
*/ */
func Expand(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token { func Expand(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
input = input.Eval(funcs, vars, false) return input.Expand()
if input.Tag != ast.LIST {
log.Log(log.INFO, "expand called on not a list", "expand")
return input
}
return input.Eval(funcs, vars, false).Expand()
} }
/* L_APPEND (append from repl) /* L_APPEND (append from repl)
@ -45,7 +39,6 @@ func Expand(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token
* if no args are a list, a list is made from all args * if no args are a list, a list is made from all args
*/ */
func L_append(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token { func L_append(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
input = input.Eval(funcs, vars, false)
src := input src := input
if input.Tag != ast.LIST { if input.Tag != ast.LIST {
@ -74,24 +67,28 @@ func L_append(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Tok
} }
/* Len /* Len
* Returns length of list as a number * Returns length of list or string as a number
* Returns nil if not a list * Returns nil if not a list or string
* *
* Example: () -> 0 * Example: () -> 0
* Example: (1 2 3) -> 3 * Example: (1 2 3) -> 3
*/ */
func Len(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token { func Len(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
input = input.Eval(funcs, vars, false) if input.Tag != ast.LIST && input.Tag != ast.STRING {
if input.Tag != ast.LIST {
log.Log(log.ERR, log.Log(log.ERR,
"non-list as parameter to head", "must provide list or strinig",
"head") "head")
return nil return nil
} }
length := 0 length := 0
for iter := input.Expand(); iter != nil; iter = iter.Next { if input.Tag = ast.STRING {
length += 1 length = len(input.Value())
} else {
for iter := input.Expand(); iter != nil; iter = iter.Next {
length += 1
}
} }
ret := &ast.Token{Tag: ast.NUMBER} ret := &ast.Token{Tag: ast.NUMBER}
@ -106,14 +103,6 @@ func Len(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
* Example: (head (2 3 4)) -> 2 * Example: (head (2 3 4)) -> 2
*/ */
func Head(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token { func Head(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
input = input.Eval(funcs, vars, false)
if input.Tag != ast.LIST {
log.Log(log.ERR,
"non-list as parameter to head",
"head")
return nil
}
li := input.Expand().Copy() li := input.Expand().Copy()
if li != nil { if li != nil {
li.Next = nil li.Next = nil
@ -129,15 +118,6 @@ func Head(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
* Example: (tail (2 3 4)) -> 4 * Example: (tail (2 3 4)) -> 4
*/ */
func Tail(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token { func Tail(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
input = input.Eval(funcs, vars, false)
if input.Tag != ast.LIST {
log.Log(log.ERR,
"non-list as parameter to head",
"head")
return nil
}
iter := input.Expand() iter := input.Expand()
for iter != nil && iter.Next != nil { for iter != nil && iter.Next != nil {
iter = iter.Next iter = iter.Next
@ -158,25 +138,10 @@ func Tail(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
* Example: (slice 0 0 (1 2 3)) -> (1) * Example: (slice 0 0 (1 2 3)) -> (1)
*/ */
func Slice(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token { func Slice(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
input = input.Eval(funcs, vars, false)
start := input start := input
end := input.Next end := input.Next
source := end.Next source := end.Next
if start.Tag != ast.NUMBER || end.Tag != ast.NUMBER {
log.Log(log.ERR,
"start and end must both be integers",
"slice")
return nil
}
if source.Tag != ast.LIST {
log.Log(log.ERR,
"non-list as parameter to head",
"head")
return nil
}
st, err := strconv.ParseInt(start.Value(), 10, 64) st, err := strconv.ParseInt(start.Value(), 10, 64)
en, errr := strconv.ParseInt(end.Value(), 10, 64) en, errr := strconv.ParseInt(end.Value(), 10, 64)

View file

@ -225,11 +225,6 @@ 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 {
in = in.Eval(ft, vt, true)
if in == nil {
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
@ -261,11 +256,6 @@ func Call(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (bg vim file.txt) * Example: (bg vim file.txt)
*/ */
func Bgcall(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func Bgcall(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, true)
if in == nil {
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
@ -304,14 +294,6 @@ func Fg(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
return nil return nil
} }
in = in.Eval(ft, vt, false)
if in.Tag != ast.NUMBER && in.Tag != ast.STRING {
log.Log(log.ERR,
"must supply a number or string to fg",
"fg")
return nil
}
pid, err := strconv.ParseFloat(in.Value(), 64) pid, err := strconv.ParseFloat(in.Value(), 64)
if err != nil { if err != nil {
log.Log(log.ERR, log.Log(log.ERR,
@ -374,12 +356,6 @@ func Jobs(in *ast.Token, vt ast.VarTable, fg ast.FuncTable) *ast.Token {
* Example: ($ echo hello world) * Example: ($ echo hello world)
*/ */
func ReadCmd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func ReadCmd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, true)
if in == nil {
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
@ -434,13 +410,6 @@ func GetExit(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* (this function not added to functable in stdlib.go) * (this function not added to functable in stdlib.go)
*/ */
func Kill(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func Kill(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, true)
if in.Tag == ast.LIST {
log.Log(log.ERR, "non-number argument to kill function", "kill")
return nil
}
pid, err := strconv.ParseInt(in.Value(), 10, 64) pid, err := strconv.ParseInt(in.Value(), 10, 64)
if err != nil { if err != nil {
log.Log(log.ERR, "error parsing arg to kill: " + err.Error(), "kill") log.Log(log.ERR, "error parsing arg to kill: " + err.Error(), "kill")

View file

@ -34,274 +34,314 @@ func GenFuncTable() ast.FuncTable {
"if": &ast.Function{ "if": &ast.Function{
Function: ShsIf, Function: ShsIf,
Name: "if", Name: "if",
TimesCalled: 0, NumArgs: 3,
Args: 3, EvalLazy true,
ArgLazy true
}, },
"while": &ast.Function{ "while": &ast.Function{
Function: ShsWhile, Function: ShsWhile,
Name: "while", Name: "while",
TimesCalled: 0, NumArgs: -1,
Args: -1, EvalLazy true,
ArgLazy true,
}, },
"progn": &ast.Function{ "progn": &ast.Function{
Function: ShsProgn, Function: ShsProgn,
Name: "shs_progn", Name: "shs_progn",
TimesCalled: 0, NumArgs: -1,
Args: -1, EvalLazy: true,
ArgLazy: true,
}, },
"func": &ast.Function{ "func": &ast.Function{
Function: DeclFunc, Function: DeclFunc,
Name: "decl_func", Name: "decl_func",
TimesCalled: 0, Args: []ast.Token_t{
Args: 3, ast.STRING,
ast.LIST,
ast.LIST
},
EvalLazy: true,
}, },
"len": &ast.Function{ "len": &ast.Function{
Function: Len, Function: Len,
Name: "len", Name: "len",
TimesCalled: 0, NumArgs: 1,
Args: 1, ArgLazy: true,
}, },
"head": &ast.Function{ "head": &ast.Function{
Function: Head, Function: Head,
Name: "head", Name: "head",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.LIST
},
}, },
"tail": &ast.Function{ "tail": &ast.Function{
Function: Tail, Function: Tail,
Name: "tail", Name: "tail",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.LIST
},
}, },
"slice": &ast.Function{ "slice": &ast.Function{
Function: Slice, Function: Slice,
Name: "slice", Name: "slice",
TimesCalled: 0, Args: []ast.Token_t{
Args: 3, ast.NUMBER,
ast.NUMBER,
ast.LIST
},
}, },
"export": &ast.Function{ "export": &ast.Function{
Function: Export, Function: Export,
Name: "export", Name: "export",
TimesCalled: 0, LazyEval: true,
Args: 2, Args: []ast.Token_t{
ast.STRING,
ast.LIST
},
}, },
"input": &ast.Function{ "input": &ast.Function{
Function: Input, Function: Input,
Name: "input", Name: "input",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.STRING
},
}, },
"load": &ast.Function{ "load": &ast.Function{
Function: Load, Function: Load,
Name: "load", Name: "load",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.STRING
},
}, },
"bool": &ast.Function{ "bool": &ast.Function{
Function: BoolCast, Function: BoolCast,
Name: "bool", Name: "bool",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.STRING
},
}, },
"string": &ast.Function{ "string": &ast.Function{
Function: StrCast, Function: StrCast,
Name: "string", Name: "string",
TimesCalled: 0, NumArgs: 1,
Args: 1, ArgLazy: true,
}, },
"number": &ast.Function{ "number": &ast.Function{
Function: NumCast, Function: NumCast,
Name: "number", Name: "number",
TimesCalled: 0, Args: []ast.Token{
Args: 1, ast.NUMBER
},
}, },
"...": &ast.Function{ "...": &ast.Function{
Function: Expand, Function: Expand,
Name: "...", Name: "...",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.LIST
},
}, },
"append": &ast.Function{ "append": &ast.Function{
Function: L_append, Function: L_append,
Name: "append", Name: "append",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
}, },
"join": &ast.Function{ "join": &ast.Function{
Function: Join, Function: Join,
Name: "join", Name: "join",
TimesCalled: 0, Args: []ast.Token_t{
Args: 2, ast.STRING,
ast.LIST
},
}, },
"split": &ast.Function{ "split": &ast.Function{
Function: Split, Function: Split,
Name: "split", Name: "split",
TimesCalled: 0, Args: ast.Token_t{
Args: 2, ast.STRING,
ast.STRING
},
}, },
"substr": &ast.Function{ "substr": &ast.Function{
Function: Substr, Function: Substr,
Name: "substr", Name: "substr",
TimesCalled: 0, Args: []ast.Token_t{
Args: 3, ast.NUMBER,
ast.NUMBER,
ast.STRING
},
}, },
"exit": &ast.Function{ "exit": &ast.Function{
Function: ExitShell, Function: ExitShell,
Name: "exit", Name: "exit",
TimesCalled: 0, Args: []ast.Token_t{},
Args: 0,
}, },
"eq": &ast.Function{ "eq": &ast.Function{
Function: Eq, Function: Eq,
Name: "==", Name: "==",
TimesCalled: 0,
Args: 2, Args: 2,
ArgLazy: true,
}, },
"ne": &ast.Function{ "ne": &ast.Function{
Function: Ne, Function: Ne,
Name: "!=", Name: "!=",
TimesCalled: 0,
Args: 2, Args: 2,
ArgLazy: true,
}, },
"<": &ast.Function{ "<": &ast.Function{
Function: Lt, Function: Lt,
Name: "<", Name: "<",
TimesCalled: 0, Args: []ast.Token_t{
Args: 2, ast.NUMBER,
ast.NUMBER
},
}, },
">": &ast.Function{ ">": &ast.Function{
Function: Gt, Function: Gt,
Name: ">", Name: ">",
TimesCalled: 0, Args: []ast.Token_t{
Args: 2, ast.NUMBER,
ast.NUMBER
},
}, },
"<=": &ast.Function{ "<=": &ast.Function{
Function: Lte, Function: Lte,
Name: "<=", Name: "<=",
TimesCalled: 0, Args: []ast.Token_t{
Args: 2, ast.NUMBER,
ast.NUMBER
},
}, },
">=": &ast.Function{ ">=": &ast.Function{
Function: Gte, Function: Gte,
Name: ">=", Name: ">=",
TimesCalled: 0, Args: []ast.Token_t{
Args: 2, ast.NUMBER,
ast.NUMBER
},
}, },
"!": &ast.Function{ "!": &ast.Function{
Function: Not, Function: Not,
Name: "!", Name: "!",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.Bool
},
}, },
"+": &ast.Function{ "+": &ast.Function{
Function: Add, Function: Add,
Name: "add", Name: "add",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
}, },
"-": &ast.Function{ "-": &ast.Function{
Function: Sub, Function: Sub,
Name: "sub", Name: "sub",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
}, },
"*": &ast.Function{ "*": &ast.Function{
Function: Mult, Function: Mult,
Name: "mult", Name: "mult",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
}, },
"/": &ast.Function{ "/": &ast.Function{
Function: Div, Function: Div,
Name: "div", Name: "div",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
}, },
"cd": &ast.Function{ "cd": &ast.Function{
Function: Cd, Function: Cd,
Name: "changedir", Name: "changedir",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.STRING
},
}, },
"concat": &ast.Function{ "concat": &ast.Function{
Function: Concat, Function: Concat,
Name:"concatenate", Name:"concatenate",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
}, },
"print": &ast.Function{ "print": &ast.Function{
Function: PrintStr, Function: PrintStr,
Name: "print", Name: "print",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.STRING
},
}, },
"l": &ast.Function{ "l": &ast.Function{
Function: Call, Function: Call,
Name: "call", Name: "call",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
SymLazy: true,
}, },
"bg": &ast.Function{ "bg": &ast.Function{
Function: Bgcall, Function: Bgcall,
Name: "background call", Name: "background call",
TimesCalled: 0, NumArgs: -1,
Args: -1, ArgLazy: true,
SymLazy: true,
}, },
"fg": &ast.Function{ "fg": &ast.Function{
Function: Fg, Function: Fg,
Name: "foreground", Name: "foreground",
TimesCalled: 0, NumArgs: []ast.Token_t{
Args: 1, ast.NUMBER
},
}, },
"$": &ast.Function{ "$": &ast.Function{
Function: ReadCmd, Function: ReadCmd,
Name: "read cmd", Name: "read cmd",
TimesCalled: 0, NumnArgs: -1,
Args: -1, ArgLazy: true,
SymLazy: true,
}, },
"?": &ast.Function{ "?": &ast.Function{
Function: GetExit, Function: GetExit,
Name:"get exit code", Name:"get exit code",
TimesCalled: 0, Args: ast.Token_t{},
Args: 0,
}, },
/* /*
@ -309,7 +349,6 @@ func GenFuncTable() ast.FuncTable {
"kill": &ast.Function{ "kill": &ast.Function{
Function: kill, Function: kill,
Name: "kill job", Name: "kill job",
TimesCalled: 0,
Args: 1, Args: 1,
}, },
*/ */
@ -317,43 +356,49 @@ func GenFuncTable() ast.FuncTable {
"jobs": &ast.Function{ "jobs": &ast.Function{
Function: Jobs, Function: Jobs,
Name: "list jobs", Name: "list jobs",
TimesCalled: 0, Args: ast.Token_t{},
Args: 0,
}, },
"info": &ast.Function{ "info": &ast.Function{
Function: ShInfo, Function: ShInfo,
Name: "Shell Info", Name: "Shell Info",
TimesCalled: 0, Args: ast.Token_t{
Args: 1, ast.SYMBOL
},
}, },
"fexists": &ast.Function{ "fexists": &ast.Function{
Function: Fexists, Function: Fexists,
Name: "file exists", Name: "file exists",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.STRING
},
}, },
"fread": &ast.Function{ "fread": &ast.Function{
Function: Fread, Function: Fread,
Name: "read file", Name: "read file",
TimesCalled: 0, Args: []ast.Token_t{
Args: 1, ast.STRING
},
}, },
"fwrite": &ast.Function{ "fwrite": &ast.Function{
Function: Fwrite, Function: Fwrite,
Name: "write file", Name: "write file",
TimesCalled: 0, Args: []ast.Token_t{
Args: 2, ast.STRING,
ast.STRING
},
}, },
"fappend": &ast.Function{ "fappend": &ast.Function{
Function: Fappend, Function: Fappend,
Name:"append to file", Name:"append to file",
TimesCalled: 0, Args: []ast.Token_t{
Args: 2, ast.STRING,
ast.STRING
},
}, },
} }
@ -395,10 +440,12 @@ func ShInfo(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
funct := ast.GetFunction(in.Value(), ft) funct := ast.GetFunction(in.Value(), ft)
if funct != nil { if funct != nil {
fmt.Printf("FUNCTION\nNAME: %s\nTIMES CALLED: %s\nNUM ARGS: %d\n", funct.Name, funct.TimesCalled, funct.Args) fmt.Printf("FUNCTION\nNAME: %s\nTIMES CALLED: %s\nNUM ARGS: %d\n", funct.Name, funct.TimesCalled, funct.NumArgs)
break break
} }
// TODO: print func args
fmt.Printf("UNKNOWN SYMBOL\n") fmt.Printf("UNKNOWN SYMBOL\n")
} }
@ -413,14 +460,6 @@ func ShInfo(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (print (input)) * Example: (print (input))
*/ */
func Input(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func Input(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
if in.Tag != ast.STRING && in.Tag != ast.NUMBER {
log.Log(log.ERR,
"argument to input must be a string or number",
"input")
return nil
}
prompt := in.Value() prompt := in.Value()
var output string var output string
@ -438,14 +477,6 @@ func Input(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
* Example: (load "myscript.shs") * Example: (load "myscript.shs")
*/ */
func Load(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func Load(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, true)
if in.Tag != ast.STRING {
log.Log(log.ERR,
"argument to load must be a string",
"load")
return nil
}
bp := in.Value() bp := in.Value()
bp = AbsPath(bp) bp = AbsPath(bp)
util.LoadScript(bp, vt, ft) util.LoadScript(bp, vt, ft)

View file

@ -30,8 +30,6 @@ import (
* Example: (concat "hello" " " "world") * Example: (concat "hello" " " "world")
*/ */
func Concat(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { func Concat(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
var res string var res string
for i := in; i != nil; i = i.Next { for i := in; i != nil; i = i.Next {
if i.Tag == ast.LIST { 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 * Example: (string 1) -> 1.0
*/ */
func StrCast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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 := &ast.Token{ Tag: ast.STRING }
res.Set(body) res.Set(body)
return res 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") * Example: (split "/path/to/file" "/") -> ("path" "to" "file")
*/ */
func Split(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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() body := in.Value()
delim := in.Next.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 var res *ast.Token
builder := &res 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" * Example: (join ", " ("apple" "ananas" "pear")) -> "apple, ananas, pear"
*/ */
func Join(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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 delim := in
strs := in.Next 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() de := delim.Value()
res := "" res := ""
for i := strs.Expand(); i != nil; i = i.Next { for i := strs.Expand(); i != nil; i = i.Next {
if i.Tag != ast.STRING { if i.Tag != ast.STRING {
log.Log(log.ERR, 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 " * Example: (substr 1 5 "Linus Torvalds") -> "inus "
*/ */
func Substr(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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 end := start.Next
str := end.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 ed_idx := 0
st_idx, err := strconv.Atoi(start.Value()) st_idx, err := strconv.Atoi(start.Value())
ed_idx, err = strconv.Atoi(end.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") * Example: (print "Line: \n, Tab: \t")
*/ */
func PrintStr(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { 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] != '"' { if body[0] != body[len(body)-1] && body[0] != '"' {
body = "`" + body + "`" body = "`" + body + "`"
} }