SHS/ast/func_table.go
2020-07-18 10:44:34 -07:00

92 lines
2.2 KiB
Go

/* 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/>.
*/
package ast
import "gitlab.com/whom/shs/log"
type Operation func(*Token, VarTable, FuncTable) *Token
type Function struct {
Function Operation
Name string
TimesCalled int
Args int // TODO: Make this a list of expected types (TAGs)
}
type FuncTable *map[string]*Function
// TODO: Currently only checks arg list length
func (f Function) ParseFunction(args *Token) bool {
// handle infinite args
if f.Args < 0 {
return true
}
i := f.Args
for iter := args; iter != nil; iter = iter.Next {
i -= 1
}
if i != 0 {
log.Log(log.ERR,
"Incorrect number of arguments",
"eval")
return false
}
return true
}
func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
if !f.ParseFunction(args) {
log.Log(log.ERR,
"Couldnt call " + f.Name,
"eval")
return nil
}
f.TimesCalled += 1
return f.Function(args, vt, ft)
}
func GetFunction(arg string, table FuncTable) *Function {
target, ok := (*table)[arg]
if !ok {
log.Log(log.DEBUG,
"function " + arg + " not found",
"ftable")
return nil
}
return target
}
/* lists all functions in table
*/
func ListFuncs(ft FuncTable) []string {
keys := make([]string, len(*ft))
i := 0
for k := range *ft {
keys[i] = k
i++
}
return keys
}