SHS/ast/eval.go

119 lines
3.2 KiB
Go
Raw Normal View History

/* 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"
2020-06-29 00:06:53 -07:00
/* determines whether or not to execute a system call
* when a function cannot be found in the functable
* (use case: shell)
* ExecFunc determines the name of the system call function to fetch
*/
var ExecWhenFuncUndef = false
var ExecFunc = "l"
2020-06-29 00:06:53 -07:00
/* Runs through an AST of tokens
* Evaluates the Tokens to determine simplest form
* Returns simplest form
*
* canFunc determines whether a symbol could be a function to call
* (true when first elem of a list)
*/
func (in *Token) Eval(funcs FuncTable, vars VarTable, cnvtUndefVars bool) *Token {
2020-06-29 00:06:53 -07:00
if in == nil {
return nil
}
2020-06-29 00:06:53 -07:00
var res *Token
2020-06-29 00:06:53 -07:00
switch in.Tag {
case BOOL, NUMBER, STRING:
2020-07-08 21:09:24 -07:00
res = in.Copy()
2020-06-29 00:06:53 -07:00
case SYMBOL:
res = GetVar(in.Value(), vars)
if res == nil {
2020-07-08 21:09:24 -07:00
res = in.Copy()
2020-06-29 00:06:53 -07:00
if GetFunction(in.Value(), funcs) == nil {
if cnvtUndefVars {
2020-07-08 21:09:24 -07:00
res.Tag = STRING
break
}
2020-07-03 01:05:25 -07:00
log.Log(log.ERR,
2020-06-29 00:06:53 -07:00
"undefined symbol: "+in.Value(),
"eval")
return nil
}
}
2020-06-29 00:06:53 -07:00
case LIST:
inner := in.Expand()
if inner == nil {
2020-07-08 21:09:24 -07:00
res = in.Copy()
2020-06-29 00:06:53 -07:00
break
}
2020-06-29 00:06:53 -07:00
if inner.Tag != SYMBOL {
in.Direct(inner.Eval(funcs, vars, cnvtUndefVars))
2020-07-08 21:09:24 -07:00
res = in.Copy()
2020-06-29 00:06:53 -07:00
break
}
makeHead := false
funct := GetFunction(inner.Value(), funcs)
if funct == nil {
if ExecWhenFuncUndef {
funct = GetFunction(ExecFunc, funcs)
makeHead = true
}
}
2020-06-29 00:06:53 -07:00
if funct != nil {
if makeHead {
inner = &Token{Next: inner}
2020-06-29 00:06:53 -07:00
}
res = funct.CallFunction(inner.Next, vars, funcs).Eval(funcs, vars, false)
2020-06-29 12:51:41 -07:00
if res == nil {
// function failed. logging is its responsibility
return nil
}
2020-06-29 00:06:53 -07:00
res.Append(in.Next)
} else {
log.Log(log.ERR,
"undefined function "+inner.Value()+" called",
"eval")
return nil
}
default:
log.Log(log.ERR,
"Eval hit unknown token type!",
"eval")
return nil
}
2020-06-29 00:06:53 -07:00
if res.Next != nil {
res.Next = res.Next.Eval(funcs, vars, cnvtUndefVars)
2020-06-29 00:06:53 -07:00
}
return res
}