2020-06-20 20:59:52 -07: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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package ast
|
|
|
|
|
|
2020-06-29 19:15:00 -07:00
|
|
|
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-28 14:13:44 -07:00
|
|
|
|
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)
|
|
|
|
|
*/
|
2020-06-29 19:15:00 -07:00
|
|
|
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-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
var res *Token
|
2020-06-21 15:32:47 -07:00
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
switch in.Tag {
|
|
|
|
|
case BOOL, NUMBER, STRING:
|
|
|
|
|
res = in
|
2020-06-20 20:59:52 -07:00
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
case SYMBOL:
|
|
|
|
|
res = GetVar(in.Value(), vars)
|
|
|
|
|
if res == nil {
|
|
|
|
|
res = in
|
2020-06-20 20:59:52 -07:00
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
if GetFunction(in.Value(), funcs) == nil {
|
2020-06-29 19:15:00 -07:00
|
|
|
if cnvtUndefVars {
|
|
|
|
|
in.Tag = STRING
|
|
|
|
|
res = in
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"undefined symbol: "+in.Value(),
|
|
|
|
|
"eval")
|
|
|
|
|
return nil
|
2020-06-21 15:32:47 -07:00
|
|
|
}
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
2020-06-29 00:06:53 -07:00
|
|
|
res.Next = in.Next
|
2020-06-20 20:59:52 -07:00
|
|
|
|
|
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
case LIST:
|
|
|
|
|
inner := in.Expand()
|
|
|
|
|
if inner == nil {
|
|
|
|
|
res = in
|
|
|
|
|
break
|
|
|
|
|
}
|
2020-06-21 15:32:47 -07:00
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
if inner.Tag != SYMBOL {
|
2020-06-29 19:15:00 -07:00
|
|
|
in.Direct(inner.Eval(funcs, vars, cnvtUndefVars))
|
2020-06-29 00:06:53 -07:00
|
|
|
res = in
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
makeHead := false
|
|
|
|
|
funct := GetFunction(inner.Value(), funcs)
|
|
|
|
|
if funct == nil {
|
|
|
|
|
if ExecWhenFuncUndef {
|
|
|
|
|
funct = GetFunction(ExecFunc, funcs)
|
|
|
|
|
makeHead = true
|
2020-06-28 14:13:44 -07:00
|
|
|
}
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
if funct != nil {
|
|
|
|
|
if makeHead {
|
2020-06-29 19:15:00 -07:00
|
|
|
inner = &Token{Next: inner}
|
2020-06-29 00:06:53 -07:00
|
|
|
}
|
2020-06-29 19:15:00 -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-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-29 00:06:53 -07:00
|
|
|
if res.Next != nil {
|
2020-06-29 19:15:00 -07:00
|
|
|
res.Next = res.Next.Eval(funcs, vars, cnvtUndefVars)
|
2020-06-29 00:06:53 -07:00
|
|
|
}
|
|
|
|
|
return res
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|