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-28 13:47:44 -07:00
|
|
|
import "gitlab.com/whom/shs/log"
|
2020-06-21 01:30:54 -07:00
|
|
|
|
2020-06-28 14:13:44 -07:00
|
|
|
var CallExecutablesFromUndefFuncCalls = false
|
|
|
|
|
var CallExecutableToken = "l"
|
|
|
|
|
|
2020-06-21 15:32:47 -07:00
|
|
|
func (t *Token) Eval(funcs FuncTable, vars VarTable) (*Token, bool) {
|
2020-06-20 20:59:52 -07:00
|
|
|
if t == nil {
|
2020-06-21 15:32:47 -07:00
|
|
|
return nil, false
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 21:38:46 -07:00
|
|
|
var reduce func(*Token) *Token
|
|
|
|
|
reduce = func(t_ *Token) *Token {
|
2020-06-21 15:32:47 -07:00
|
|
|
var unwrap bool
|
|
|
|
|
|
2020-06-20 20:59:52 -07:00
|
|
|
if t_.Next != nil {
|
|
|
|
|
t_.Next = reduce(t_.Next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (t_.Tag) {
|
|
|
|
|
case SYMBOL:
|
2020-06-21 12:46:25 -07:00
|
|
|
maybeToken := GetVar(t_.Inner.(string), vars)
|
2020-06-20 20:59:52 -07:00
|
|
|
if maybeToken != nil {
|
2020-06-21 15:32:47 -07:00
|
|
|
tok, _ := maybeToken.Eval(funcs, vars)
|
2020-06-21 13:59:09 -07:00
|
|
|
tok.Next = t_.Next
|
|
|
|
|
return tok
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case LIST:
|
2020-06-21 15:32:47 -07:00
|
|
|
t_.Inner, unwrap = t_.Inner.(*Token).Eval(funcs, vars)
|
|
|
|
|
if unwrap {
|
|
|
|
|
next := t_.Next
|
|
|
|
|
t_ = t_.Inner.(*Token)
|
|
|
|
|
if t_ == nil {
|
|
|
|
|
log.Log(log.DEBUG, "nil Inner on list unwrap", "eval")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := &t_
|
|
|
|
|
for (*i).Next != nil {
|
|
|
|
|
i = &((*i).Next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(*i).Next = next
|
|
|
|
|
}
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t_
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := reduce(t)
|
2020-06-21 15:32:47 -07:00
|
|
|
if ret == nil {
|
|
|
|
|
log.Log(log.INFO, "reduce returned nil", "eval")
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 14:13:44 -07:00
|
|
|
//if symbol in front of a list, could be a function call
|
2020-06-20 20:59:52 -07:00
|
|
|
if ret.Tag == SYMBOL {
|
2020-06-21 12:46:25 -07:00
|
|
|
f := GetFunction(ret.Inner.(string), funcs)
|
2020-06-20 20:59:52 -07:00
|
|
|
if f == nil {
|
2020-06-28 14:13:44 -07:00
|
|
|
if CallExecutablesFromUndefFuncCalls {
|
|
|
|
|
f = GetFunction(CallExecutableToken, funcs)
|
|
|
|
|
if f == nil {
|
|
|
|
|
log.Log(log.DEBUG, "Symbol " + ret.Inner.(string) +
|
|
|
|
|
" has no definition in function table. Additionally " +
|
|
|
|
|
"the configured LoadExecutableToken is also not defined",
|
|
|
|
|
"eval")
|
|
|
|
|
return ret, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// see the use of CallFunction below
|
|
|
|
|
ret = &Token{Next: ret}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
log.Log(log.DEBUG,
|
|
|
|
|
"could not find definition for symbol " + ret.Inner.(string),
|
|
|
|
|
"eval")
|
|
|
|
|
return ret, false
|
|
|
|
|
}
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-21 15:32:47 -07:00
|
|
|
return (*f).CallFunction(ret.Next, vars, funcs), true
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-21 15:32:47 -07:00
|
|
|
return ret, false
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|