ast package full godoc
This commit is contained in:
parent
956044cfae
commit
fec3550702
7 changed files with 68 additions and 26 deletions
|
|
@ -19,18 +19,35 @@ package ast
|
|||
|
||||
import "gitlab.com/whom/shs/log"
|
||||
|
||||
/* expected function header for any stdlib function
|
||||
*/
|
||||
type Operation func(*Token, VarTable, FuncTable) *Token
|
||||
|
||||
/* holds a stdlib function along with relevant metadata
|
||||
*/
|
||||
type Function struct {
|
||||
// go function that list of args are passed to
|
||||
Function Operation
|
||||
|
||||
// name of function
|
||||
Name string
|
||||
|
||||
// number of times user has called this function
|
||||
TimesCalled int
|
||||
Args int // TODO: Make this a list of expected types (TAGs)
|
||||
|
||||
// number of args required
|
||||
Args int
|
||||
}
|
||||
|
||||
/* holds a mapping of key to function
|
||||
* passed to eval and into function calls
|
||||
* initialized by repl at startup
|
||||
*/
|
||||
type FuncTable *map[string]*Function
|
||||
|
||||
// TODO: Currently only checks arg list length
|
||||
/* validates an individual call of a function
|
||||
* makes sure correct arguments are passed in
|
||||
*/
|
||||
func (f Function) ParseFunction(args *Token) bool {
|
||||
// handle infinite args
|
||||
if f.Args < 0 {
|
||||
|
|
@ -52,6 +69,9 @@ func (f Function) ParseFunction(args *Token) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
/* handles a call to a function
|
||||
* calls ParseFunction and increments TimesCalled
|
||||
*/
|
||||
func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
|
||||
if !f.ParseFunction(args) {
|
||||
log.Log(log.ERR,
|
||||
|
|
@ -64,6 +84,8 @@ func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
|
|||
return f.Function(args, vt, ft)
|
||||
}
|
||||
|
||||
/* searches for function mapped to argument in FuncTable
|
||||
*/
|
||||
func GetFunction(arg string, table FuncTable) *Function {
|
||||
target, ok := (*table)[arg]
|
||||
if !ok {
|
||||
|
|
@ -77,7 +99,7 @@ func GetFunction(arg string, table FuncTable) *Function {
|
|||
}
|
||||
|
||||
|
||||
/* lists all functions in table
|
||||
/* returns list of all functions in table
|
||||
*/
|
||||
func ListFuncs(ft FuncTable) []string {
|
||||
keys := make([]string, len(*ft))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue