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"
|
||||
)
|
||||
|
||||
type Operation func(*ast.Token) *ast.Token
|
||||
type Operation func(*Token) *Token
|
||||
|
||||
type Function struct {
|
||||
function Operation
|
||||
function Operation
|
||||
timesCalled int
|
||||
args int
|
||||
args int // TODO: Make this a list of expected types (TAGs)
|
||||
}
|
||||
|
||||
type FuncTable map[string]*Function
|
||||
|
||||
var (
|
||||
GlobalFuncTable *FuncTable
|
||||
)
|
||||
|
||||
// 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
|
||||
if target.args < 0 {
|
||||
return true
|
||||
|
|
@ -48,27 +44,26 @@ func ParseFunction(target *Function, args *ast.Token) bool {
|
|||
}
|
||||
|
||||
if i != 0 {
|
||||
// log error here?
|
||||
// TODO: log error here
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func CallFunction(target *Function, args *ast.Token) *ast.Token {
|
||||
if !ParseFunction(target, args) {
|
||||
return args
|
||||
func (Function f) CallFunction(args *Token) *Token {
|
||||
if !ParseFunction(f, args) {
|
||||
return nil
|
||||
}
|
||||
|
||||
target.timesCalled += 1
|
||||
return target.function(args)
|
||||
f.timesCalled += 1
|
||||
return f.function(args)
|
||||
}
|
||||
|
||||
func GetFunction(arg string) *Function {
|
||||
target, ok := (*GlobalFuncTable)[arg]
|
||||
func (FuncTable table) GetFunction(arg string) *Function {
|
||||
target, ok := table[arg]
|
||||
if !ok {
|
||||
// TODO: hook into stdlib exec call
|
||||
;
|
||||
return nil
|
||||
}
|
||||
|
||||
return target
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ast;
|
||||
package ast
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
|
|
|
|||
|
|
@ -19,21 +19,27 @@ package ast
|
|||
|
||||
type VarTable map[string]*Token
|
||||
|
||||
var (
|
||||
GlobalVarTable *VarTable
|
||||
)
|
||||
func (VarTable vt) GetVar(arg string) *Token {
|
||||
val, ok := vt[arg]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
// Library represents variables defined in inner scope
|
||||
// It is assumed library is ordered from innermost scope to outermost scope
|
||||
func GetVar(arg string, library []VarTable) *Token {
|
||||
for scope, dict := range library {
|
||||
val, ok := dict[arg]
|
||||
if ok {
|
||||
scope = scope // TEMP LINE
|
||||
// TODO: maybe log which scope it was found in?
|
||||
return val
|
||||
var res *Token
|
||||
res = nil
|
||||
for i := 0; i < len(library); i += 1 {
|
||||
res = library[i].GetVar(arg)
|
||||
if res != nil {
|
||||
// TODO: Log scope res was found in?
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return res
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue