SHS/ast/func_table.go

122 lines
2.9 KiB
Go
Raw 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 (
"fmt"
"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
// number of args required
Args int
}
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-06-21 12:29:20 -07:00
// handle infinite args
if f.Args < 0 {
return true
}
2020-06-21 12:29:20 -07:00
i := f.Args
2020-06-20 22:56:22 -07:00
for iter := args; iter != nil; iter = iter.Next {
i -= 1
}
if i != 0 {
2020-07-03 16:27:02 -07:00
log.Log(log.ERR,
"Incorrect number of arguments",
"eval")
log.Log(log.DEBUG,
fmt.Sprintf("Function %s expects %d arguments. You've provided %d arguments.",
f.Name, f.Args, f.Args - i),
"eval")
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-06-20 22:56:22 -07:00
if !f.ParseFunction(args) {
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
return f.Function(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
}