changed table types to support implementing 'let', also integrated stdlib into repl

This commit is contained in:
Aidan 2020-06-21 12:46:25 -07:00
parent b01415d786
commit 9c25ac21f9
No known key found for this signature in database
GPG key ID: 327711E983899316
5 changed files with 16 additions and 58 deletions

View file

@ -34,7 +34,7 @@ func (t *Token) Eval(funcs FuncTable, vars VarTable) *Token {
switch (t_.Tag) {
case SYMBOL:
maybeToken := vars.GetVar(t_.Inner.(string))
maybeToken := GetVar(t_.Inner.(string), vars)
if maybeToken != nil {
tok := maybeToken.Eval(funcs, vars)
if tok.Tag == LIST {
@ -52,7 +52,7 @@ func (t *Token) Eval(funcs FuncTable, vars VarTable) *Token {
ret := reduce(t)
if ret.Tag == SYMBOL {
f := funcs.GetFunction(ret.Inner.(string))
f := GetFunction(ret.Inner.(string), funcs)
if f == nil {
if !eligibleForSystemCall {
log.Log(log.DEBUG,

View file

@ -28,7 +28,7 @@ type Function struct {
Args int // TODO: Make this a list of expected types (TAGs)
}
type FuncTable map[string]*Function
type FuncTable *map[string]*Function
// TODO: Currently only checks arg list length
func (f Function) ParseFunction(args *Token) bool {
@ -64,8 +64,8 @@ func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
return f.Function(args, vt, ft)
}
func (table FuncTable) GetFunction(arg string) *Function {
target, ok := table[arg]
func GetFunction(arg string, table FuncTable) *Function {
target, ok := (*table)[arg]
if !ok {
log.Log(log.DEBUG,
"function " + arg + " not found",

View file

@ -17,10 +17,10 @@
package ast
type VarTable map[string]*Token
type VarTable *map[string]*Token
func (vt VarTable) GetVar(arg string) *Token {
val, ok := vt[arg]
func GetVar(arg string, vt VarTable) *Token {
val, ok := (*vt)[arg]
if !ok {
return nil
}
@ -30,11 +30,11 @@ func (vt VarTable) GetVar(arg string) *Token {
// 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 {
func GetVarFromTables(arg string, library []VarTable) *Token {
var res *Token
res = nil
for i := 0; i < len(library); i += 1 {
res = library[i].GetVar(arg)
res = GetVar(arg, library[i])
if res != nil {
// TODO: Log scope res was found in?
break

View file

@ -25,6 +25,7 @@ import (
"strconv"
"git.callpipe.com/aidan/shs/ast"
"git.callpipe.com/aidan/shs/log"
"git.callpipe.com/aidan/shs/stdlib"
)
const (
@ -53,6 +54,9 @@ func main() {
var vars ast.VarTable
var funcs ast.FuncTable
funcs = stdlib.GenFuncTable()
vars = &map[string]*ast.Token{}
useHist := false
var histFile *os.File
var err error

View file

@ -19,11 +19,11 @@ package stdlib
import (
"git.callpipe.com/aidan/shs/ast"
"git.callpipe.com/aidan/shs/log"
)
func GenFuncTable() ast.FuncTable {
var stdlib = ast.FuncTable{
var stdlib ast.FuncTable
stdlib = &map[string]*ast.Function{
"...": &ast.Function{
Function: expand,
Name: "...",
@ -41,49 +41,3 @@ func GenFuncTable() ast.FuncTable {
return stdlib
}
// TODO: put list funcs in their own file
/* EXPAND
* Takes 1 element: a list
* retuns a sequence of elements (list contents)
* in event a not-list is passed in, returns the arg.
*/
func expand(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
if input.Tag != ast.LIST {
log.Log(log.DEBUG, "expand called on not a list", "expand")
return input
}
return input.Inner.(*ast.Token)
}
/* L_APPEND (append from repl)
* Appends N elements to the end of a list
* Arg one is a list, next args are appended
* if no args are a list, a list is made from all args
*/
func l_append(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
src := input
if input.Tag != ast.LIST {
// TODO: Position, if I can figure out what to do with it
src = &ast.Token{Tag: ast.LIST, Inner: input}
return src
}
// deref inner first
i := src.Inner.(*ast.Token)
iter := &i
if *iter == nil {
src.Inner = input.Next
return src
}
for (*iter).Next != nil {
iter = &((*iter).Next)
}
(*iter).Next = input.Next
return src
}