SHS/ast/func_table.go

177 lines
4.2 KiB
Go
Raw Permalink Normal View History

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
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
2020-07-18 14:40:35 -07:00
/* holds a stdlib function along with relevant metadata
*/
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
// 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
// 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
}
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
*/
type FuncTable *map[string]*Function
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
for iter := args; iter != nil; iter = iter.Next {
2020-08-26 21:36:06 -07:00
inc += 1
if inc > len(f.Args) {
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] {
log.Log(log.ERR,
"argument is " + GetTagAsStr(iter.Tag) +
2020-08-26 21:36:06 -07:00
" should be " + GetTagAsStr(f.Args[inc - 1]),
"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
}
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 {
return true
}
total := 0
2020-06-20 22:56:22 -07:00
for iter := args; iter != nil; iter = iter.Next {
total += 1
}
if total < f.NumArgs {
log.Log(log.ERR,
"expected more arguments, try calling info on function",
"ftable")
return false
}
if total > f.NumArgs {
2020-07-03 16:27:02 -07:00
log.Log(log.ERR,
"too many args. try calling info on function",
"ftable")
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)
}
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)
} else {
2020-08-26 21:36:06 -07:00
passes = f.ParseFunction(n_args)
}
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")
return nil
}
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)
}
2020-07-18 14:40:35 -07:00
/* searches for function mapped to argument in FuncTable
*/
func GetFunction(arg string, table FuncTable) *Function {
target, ok := (*table)[arg]
if !ok {
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")
return nil
}
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
}