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-20 21:38:46 -07:00
|
|
|
func (t *Token) Eval(funcs FuncTable, vars VarTable) *Token {
|
2020-06-20 20:59:52 -07:00
|
|
|
if t == nil {
|
2020-06-20 21:38:46 -07:00
|
|
|
return nil
|
2020-06-20 20:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var iter *Token
|
|
|
|
|
eligibleForSystemCall := true
|
|
|
|
|
|
2020-06-20 21:38:46 -07:00
|
|
|
var reduce func(*Token) *Token
|
|
|
|
|
reduce = func(t_ *Token) *Token {
|
2020-06-20 20:59:52 -07:00
|
|
|
if t_.Next != nil {
|
|
|
|
|
t_.Next = reduce(t_.Next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (t_.Tag) {
|
|
|
|
|
case SYMBOL:
|
2020-06-20 21:38:46 -07:00
|
|
|
maybeToken := GetVar(t_.Inner.(string), vars)
|
2020-06-20 20:59:52 -07:00
|
|
|
if maybeToken != nil {
|
|
|
|
|
tok := maybeToken.Eval(funcs, vars)
|
|
|
|
|
if tok == LIST {
|
|
|
|
|
eligibleForSystemCall = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case LIST:
|
|
|
|
|
eligibleForSystemCall = false
|
|
|
|
|
t.Inner = Eval(t_.Inner)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t_
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := reduce(t)
|
|
|
|
|
if ret.Tag == SYMBOL {
|
|
|
|
|
f := funcs.GetFunction(ret.Inner)
|
|
|
|
|
if f == nil {
|
|
|
|
|
if !eligibleForSystemCall {
|
|
|
|
|
// TODO: log error
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hook into stdlib exec
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (*f).CallFunction(ret.Next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
}
|