/* 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" ) /* determines whether or not to execute a system call * when a function cannot be found in the functable * (use case: shell) * ExecFunc determines the name of the system call function to fetch */ var ExecWhenFuncUndef = false var ExecFunc = "l" /* Runs through an AST of tokens * Evaluates the Tokens to determine simplest form * Returns simplest form * * canFunc determines whether a symbol could be a function to call * (true when first elem of a list) */ func (in *Token) Eval(funcs FuncTable, vars VarTable) *Token { if in == nil { return nil } var res *Token switch in.Tag { case BOOL, NUMBER, STRING: res = in case SYMBOL: res = GetVar(in.Value(), vars) if res == nil { res = in if GetFunction(in.Value(), funcs) == nil { log.Log(log.ERR, "undefined symbol: "+in.Value(), "eval") return nil } } res.Next = in.Next case LIST: inner := in.Expand() if inner == nil { res = in break } if inner.Tag != SYMBOL { in.Direct(inner.Eval(funcs, vars)) res = in break } makeHead := false funct := GetFunction(inner.Value(), funcs) if funct == nil { if ExecWhenFuncUndef { funct = GetFunction(ExecFunc, funcs) makeHead = true } } if funct != nil { if makeHead { inner = &Token{inner: inner} } res = funct.CallFunction(inner.Next, vars, funcs).Eval(funcs, vars) if res == nil { // function failed. logging is its responsibility return nil } res.Append(in.Next) } else { log.Log(log.ERR, "undefined function "+inner.Value()+" called", "eval") return nil } default: log.Log(log.ERR, "Eval hit unknown token type!", "eval") return nil } if res.Next != nil { res.Next = res.Next.Eval(funcs, vars) } return res }