SHS/ast/func_table.go

78 lines
2 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
2020-06-21 11:11:57 -07:00
import "git.callpipe.com/aidan/shs/log"
type Operation func(*Token, VarTable, FuncTable) *Token
type Function struct {
2020-06-21 12:29:20 -07:00
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
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-06-21 01:30:54 -07:00
log.Log(log.ERR,
"Incorrect number of arguments",
"eval")
return false
}
return true
}
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)
}
func GetFunction(arg string, table FuncTable) *Function {
target, ok := (*table)[arg]
if !ok {
2020-06-21 01:30:54 -07:00
log.Log(log.DEBUG,
"function " + arg + " not found",
"eval")
return nil
}
return target
}