2019-11-29 12:57:03 -08:00
|
|
|
/* SHS: Syntactically Homogeneous Shell
|
|
|
|
|
* Copyright (C) 2019 Aidan Hahn
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-20 21:38:46 -07:00
|
|
|
package ast
|
2020-06-17 00:28:16 -07:00
|
|
|
|
2020-07-23 12:08:29 -07:00
|
|
|
import (
|
|
|
|
|
"gitlab.com/whom/shs/log"
|
|
|
|
|
)
|
2020-06-21 11:11:57 -07:00
|
|
|
|
2020-07-18 14:40:35 -07:00
|
|
|
/* expected function header for any stdlib function
|
|
|
|
|
*/
|
2020-06-21 11:11:57 -07:00
|
|
|
type Operation func(*Token, VarTable, FuncTable) *Token
|
2019-11-28 00:39:08 -08:00
|
|
|
|
2020-07-18 14:40:35 -07:00
|
|
|
/* holds a stdlib function along with relevant metadata
|
|
|
|
|
*/
|
2019-11-28 00:39:08 -08:00
|
|
|
type Function struct {
|
2020-07-18 14:40:35 -07:00
|
|
|
// go function that list of args are passed to
|
2020-06-21 12:29:20 -07:00
|
|
|
Function Operation
|
2020-07-18 14:40:35 -07:00
|
|
|
|
|
|
|
|
// name of function
|
2020-06-21 12:29:20 -07:00
|
|
|
Name string
|
2020-07-18 14:40:35 -07:00
|
|
|
|
|
|
|
|
// number of times user has called this function
|
2020-06-21 12:29:20 -07:00
|
|
|
TimesCalled int
|
2020-07-18 14:40:35 -07:00
|
|
|
|
2020-08-21 01:37:04 -07:00
|
|
|
// list of types (LIST, NUMBER, STRING, etc) representing args
|
|
|
|
|
Args []Token_t
|
2020-08-21 17:37:54 -07:00
|
|
|
NumArgs int // -1 means infinite
|
2020-08-21 01:37:04 -07:00
|
|
|
|
|
|
|
|
// 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
|
2019-11-28 00:39:08 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-18 14:40:35 -07:00
|
|
|
/* holds a mapping of key to function
|
|
|
|
|
* passed to eval and into function calls
|
|
|
|
|
* initialized by repl at startup
|
|
|
|
|
*/
|
2020-06-21 12:46:25 -07:00
|
|
|
type FuncTable *map[string]*Function
|
2019-11-28 00:39:08 -08:00
|
|
|
|
2020-07-18 14:40:35 -07:00
|
|
|
/* validates an individual call of a function
|
|
|
|
|
* makes sure correct arguments are passed in
|
|
|
|
|
*/
|
2020-06-20 22:56:22 -07:00
|
|
|
func (f Function) ParseFunction(args *Token) bool {
|
2020-08-26 21:36:06 -07:00
|
|
|
inc := 0
|
2020-08-21 01:37:04 -07:00
|
|
|
for iter := args; iter != nil; iter = iter.Next {
|
2020-08-26 21:36:06 -07:00
|
|
|
inc += 1
|
|
|
|
|
if inc > len(f.Args) {
|
2020-08-21 01:37:04 -07:00
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"too many arguments",
|
|
|
|
|
"ftable")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 21:36:06 -07:00
|
|
|
if iter.Tag != f.Args[inc - 1] {
|
2020-08-21 01:37:04 -07:00
|
|
|
log.Log(log.ERR,
|
2020-08-21 18:02:49 -07:00
|
|
|
"argument is " + GetTagAsStr(iter.Tag) +
|
2020-08-26 21:36:06 -07:00
|
|
|
" should be " + GetTagAsStr(f.Args[inc - 1]),
|
2020-08-21 01:37:04 -07:00
|
|
|
"ftable")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 21:36:06 -07:00
|
|
|
if inc < len(f.Args) {
|
2020-08-22 12:37:06 -07:00
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"not enough args given",
|
|
|
|
|
"ftable")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 01:37:04 -07:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* same as ParseFunction but only evaluates the number of args
|
|
|
|
|
*/
|
|
|
|
|
func (f Function) LazyParseFunction(args *Token) bool {
|
2020-08-21 17:37:54 -07:00
|
|
|
if f.NumArgs < 0 {
|
2019-11-28 00:39:08 -08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 01:37:04 -07:00
|
|
|
total := 0
|
2020-06-20 22:56:22 -07:00
|
|
|
for iter := args; iter != nil; iter = iter.Next {
|
2020-08-21 01:37:04 -07:00
|
|
|
total += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if total < f.NumArgs {
|
|
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"expected more arguments, try calling info on function",
|
|
|
|
|
"ftable")
|
|
|
|
|
return false
|
2019-11-28 00:39:08 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-21 01:37:04 -07:00
|
|
|
if total > f.NumArgs {
|
2020-07-03 16:27:02 -07:00
|
|
|
log.Log(log.ERR,
|
2020-08-21 01:37:04 -07:00
|
|
|
"too many args. try calling info on function",
|
|
|
|
|
"ftable")
|
2019-11-28 00:39:08 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-18 14:40:35 -07:00
|
|
|
/* handles a call to a function
|
|
|
|
|
* calls ParseFunction and increments TimesCalled
|
|
|
|
|
*/
|
2020-06-21 11:11:57 -07:00
|
|
|
func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
|
2020-08-26 22:32:01 -07:00
|
|
|
n_args := args
|
2020-08-21 17:37:54 -07:00
|
|
|
if !f.EvalLazy {
|
2020-08-26 21:36:06 -07:00
|
|
|
n_args = args.Eval(ft, vt, f.SymLazy)
|
2020-08-21 01:37:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
passes := false
|
2020-08-21 17:37:54 -07:00
|
|
|
if f.ArgLazy {
|
2020-08-26 21:36:06 -07:00
|
|
|
passes = f.LazyParseFunction(n_args)
|
2020-08-21 01:37:04 -07:00
|
|
|
} else {
|
2020-08-26 21:36:06 -07:00
|
|
|
passes = f.ParseFunction(n_args)
|
2020-08-21 01:37:04 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-22 12:37:06 -07:00
|
|
|
if !passes {
|
2020-06-21 11:11:57 -07:00
|
|
|
log.Log(log.ERR,
|
2020-06-21 12:29:20 -07:00
|
|
|
"Couldnt call " + f.Name,
|
2020-06-21 01:30:54 -07:00
|
|
|
"eval")
|
2020-06-20 20:59:52 -07:00
|
|
|
return nil
|
2019-11-28 00:39:08 -08:00
|
|
|
}
|
|
|
|
|
|
2020-06-21 12:29:20 -07:00
|
|
|
f.TimesCalled += 1
|
2020-08-26 21:36:06 -07:00
|
|
|
return f.Function(n_args, vt, ft)
|
2019-11-28 00:39:08 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-18 14:40:35 -07:00
|
|
|
/* searches for function mapped to argument in FuncTable
|
|
|
|
|
*/
|
2020-06-21 12:46:25 -07:00
|
|
|
func GetFunction(arg string, table FuncTable) *Function {
|
|
|
|
|
target, ok := (*table)[arg]
|
2019-11-28 00:39:08 -08:00
|
|
|
if !ok {
|
2020-07-23 12:08:29 -07:00
|
|
|
log.Log(log.INFO,
|
2020-06-21 01:30:54 -07:00
|
|
|
"function " + arg + " not found",
|
2020-07-03 16:27:02 -07:00
|
|
|
"ftable")
|
2020-06-20 20:59:52 -07:00
|
|
|
return nil
|
2019-11-28 00:39:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return target
|
|
|
|
|
}
|
2020-07-18 10:44:34 -07:00
|
|
|
|
|
|
|
|
|
2020-07-18 14:40:35 -07:00
|
|
|
/* returns list of all functions in table
|
2020-07-18 10:44:34 -07:00
|
|
|
*/
|
|
|
|
|
func ListFuncs(ft FuncTable) []string {
|
|
|
|
|
keys := make([]string, len(*ft))
|
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
|
|
for k := range *ft {
|
|
|
|
|
keys[i] = k
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return keys
|
|
|
|
|
}
|