refactored ast tables, added eval method
This commit is contained in:
parent
cf3255f015
commit
78973ac067
4 changed files with 100 additions and 29 deletions
70
ast/eval.go
Normal file
70
ast/eval.go
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
/* 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
|
||||||
|
}
|
||||||
|
|
@ -21,22 +21,18 @@ import (
|
||||||
"git.callpipe.com/aidan/shs/ast"
|
"git.callpipe.com/aidan/shs/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Operation func(*ast.Token) *ast.Token
|
type Operation func(*Token) *Token
|
||||||
|
|
||||||
type Function struct {
|
type Function struct {
|
||||||
function Operation
|
function Operation
|
||||||
timesCalled int
|
timesCalled int
|
||||||
args int
|
args int // TODO: Make this a list of expected types (TAGs)
|
||||||
}
|
}
|
||||||
|
|
||||||
type FuncTable map[string]*Function
|
type FuncTable map[string]*Function
|
||||||
|
|
||||||
var (
|
|
||||||
GlobalFuncTable *FuncTable
|
|
||||||
)
|
|
||||||
|
|
||||||
// TODO: Currently only checks arg list length
|
// TODO: Currently only checks arg list length
|
||||||
func ParseFunction(target *Function, args *ast.Token) bool {
|
func (Function f) ParseFunction(args *ast.Token) bool {
|
||||||
// HANDLE EXEC
|
// HANDLE EXEC
|
||||||
if target.args < 0 {
|
if target.args < 0 {
|
||||||
return true
|
return true
|
||||||
|
|
@ -48,27 +44,26 @@ func ParseFunction(target *Function, args *ast.Token) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
// log error here?
|
// TODO: log error here
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func CallFunction(target *Function, args *ast.Token) *ast.Token {
|
func (Function f) CallFunction(args *Token) *Token {
|
||||||
if !ParseFunction(target, args) {
|
if !ParseFunction(f, args) {
|
||||||
return args
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
target.timesCalled += 1
|
f.timesCalled += 1
|
||||||
return target.function(args)
|
return f.function(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFunction(arg string) *Function {
|
func (FuncTable table) GetFunction(arg string) *Function {
|
||||||
target, ok := (*GlobalFuncTable)[arg]
|
target, ok := table[arg]
|
||||||
if !ok {
|
if !ok {
|
||||||
// TODO: hook into stdlib exec call
|
return nil
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return target
|
return target
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package ast;
|
package ast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
|
||||||
|
|
@ -19,21 +19,27 @@ package ast
|
||||||
|
|
||||||
type VarTable map[string]*Token
|
type VarTable map[string]*Token
|
||||||
|
|
||||||
var (
|
func (VarTable vt) GetVar(arg string) *Token {
|
||||||
GlobalVarTable *VarTable
|
val, ok := vt[arg]
|
||||||
)
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
// Library represents variables defined in inner scope
|
// Library represents variables defined in inner scope
|
||||||
// It is assumed library is ordered from innermost scope to outermost scope
|
// It is assumed library is ordered from innermost scope to outermost scope
|
||||||
func GetVar(arg string, library []VarTable) *Token {
|
func GetVar(arg string, library []VarTable) *Token {
|
||||||
for scope, dict := range library {
|
var res *Token
|
||||||
val, ok := dict[arg]
|
res = nil
|
||||||
if ok {
|
for i := 0; i < len(library); i += 1 {
|
||||||
scope = scope // TEMP LINE
|
res = library[i].GetVar(arg)
|
||||||
// TODO: maybe log which scope it was found in?
|
if res != nil {
|
||||||
return val
|
// TODO: Log scope res was found in?
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue