SHS/ast/eval.go

71 lines
1.8 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
type Tree *Token
func (Tree t) Eval(FuncTable funcs, VarTable vars) Tree {
if t == nil {
return
}
var iter *Token
eligibleForSystemCall := true
reduce := func(Tree t_) Tree {
if t_.Next != nil {
t_.Next = reduce(t_.Next)
}
switch (t_.Tag) {
case SYMBOL:
maybeToken := GetVar(t_.Inner, vars)
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
}