SHS/ast/eval.go

73 lines
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
2020-06-21 01:30:54 -07:00
import "git.callpipe.com/aidan/shs/log"
2020-06-20 21:38:46 -07:00
func (t *Token) Eval(funcs FuncTable, vars VarTable) *Token {
if t == nil {
2020-06-20 21:38:46 -07:00
return nil
}
eligibleForSystemCall := true
2020-06-20 21:38:46 -07:00
var reduce func(*Token) *Token
reduce = func(t_ *Token) *Token {
if t_.Next != nil {
t_.Next = reduce(t_.Next)
}
switch (t_.Tag) {
case SYMBOL:
maybeToken := GetVar(t_.Inner.(string), vars)
if maybeToken != nil {
tok := maybeToken.Eval(funcs, vars)
2020-06-20 22:56:22 -07:00
if tok.Tag == LIST {
eligibleForSystemCall = false
}
}
case LIST:
eligibleForSystemCall = false
2020-06-20 22:56:22 -07:00
t_.Inner = t_.Inner.(*Token).Eval(funcs, vars)
}
return t_
}
ret := reduce(t)
if ret.Tag == SYMBOL {
f := GetFunction(ret.Inner.(string), funcs)
if f == nil {
if !eligibleForSystemCall {
2020-06-21 01:30:54 -07:00
log.Log(log.DEBUG,
2020-06-21 13:13:46 -07:00
"could not find definition for symbol " + ret.Inner.(string),
2020-06-21 01:30:54 -07:00
"eval")
return nil
}
// hook into stdlib exec
2020-06-20 22:56:22 -07:00
return nil // TODO: Thats gotta change
}
2020-06-21 01:30:54 -07:00
return (*f).CallFunction(ret.Next, vars, funcs)
}
return ret
}