/* 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 . */ package ast import "gitlab.com/whom/shs/log" func (t *Token) Eval(funcs FuncTable, vars VarTable) (*Token, bool) { if t == nil { return nil, false } var reduce func(*Token) *Token reduce = func(t_ *Token) *Token { var unwrap bool 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) tok.Next = t_.Next return tok } case LIST: 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 } } return t_ } ret := reduce(t) if ret == nil { log.Log(log.INFO, "reduce returned nil", "eval") return nil, false } if ret.Tag == SYMBOL { f := GetFunction(ret.Inner.(string), funcs) if f == nil { log.Log(log.DEBUG, "could not find definition for symbol " + ret.Inner.(string), "eval") return nil, false } return (*f).CallFunction(ret.Next, vars, funcs), true } return ret, false }