From 87004ff7fd7ca7c08d2c4e90fc74d22ea4b18cd1 Mon Sep 17 00:00:00 2001 From: Aidan Date: Fri, 21 Aug 2020 17:37:54 -0700 Subject: [PATCH] fix build from last night --- ast/func_table.go | 15 +- cmd/shs.go | 2 +- stdlib/funcs.go | 2 +- stdlib/list.go | 2 +- stdlib/stdlib.go | 401 +++++++++++++++++++++++----------------------- stdlib/string.go | 1 + 6 files changed, 210 insertions(+), 213 deletions(-) diff --git a/ast/func_table.go b/ast/func_table.go index f6c9da3..03e16bd 100644 --- a/ast/func_table.go +++ b/ast/func_table.go @@ -18,7 +18,6 @@ package ast import ( - "fmt" "gitlab.com/whom/shs/log" ) @@ -40,7 +39,7 @@ type Function struct { // list of types (LIST, NUMBER, STRING, etc) representing args Args []Token_t - NumArgs bool // -1 means infinite + NumArgs int // -1 means infinite // lazy arg checking (use NumArgs instead of args) ArgLazy bool @@ -62,7 +61,7 @@ type FuncTable *map[string]*Function * makes sure correct arguments are passed in */ func (f Function) ParseFunction(args *Token) bool { - total = len(f.Args) + total := len(f.Args) for iter := args; iter != nil; iter = iter.Next { total -= 1 if total <= 0 { @@ -88,7 +87,7 @@ func (f Function) ParseFunction(args *Token) bool { /* same as ParseFunction but only evaluates the number of args */ func (f Function) LazyParseFunction(args *Token) bool { - if (f.NumArgs < 0) { + if f.NumArgs < 0 { return true } @@ -118,15 +117,15 @@ func (f Function) LazyParseFunction(args *Token) bool { * calls ParseFunction and increments TimesCalled */ func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token { - if not f.EvalLazy { + if !f.EvalLazy { args = args.Eval(ft, vt, !f.SymLazy) } passes := false - if f.ArgsLazy { - passes = LazyParseFunction(args) + if f.ArgLazy { + passes = f.LazyParseFunction(args) } else { - passes = ParseFunction(args) + passes = f.ParseFunction(args) } if passes { diff --git a/cmd/shs.go b/cmd/shs.go index cf58d44..bad550c 100644 --- a/cmd/shs.go +++ b/cmd/shs.go @@ -95,7 +95,7 @@ func main() { } dyn_prompt := ast.GetFunction("_SH_PROMPT", funcs) - if dyn_prompt == nil || dyn_prompt.Args != 0 { + if dyn_prompt == nil || dyn_prompt.NumArgs != 0 { dyn_prompt = nil } diff --git a/stdlib/funcs.go b/stdlib/funcs.go index a0dd068..2e39599 100644 --- a/stdlib/funcs.go +++ b/stdlib/funcs.go @@ -114,7 +114,7 @@ func DeclFunc(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Tok Name: name.Value(), TimesCalled: 0, NumArgs: numArgs, - LazyArgs: true + ArgLazy: true, } return nil } diff --git a/stdlib/list.go b/stdlib/list.go index e1ac6f6..13c1cdc 100644 --- a/stdlib/list.go +++ b/stdlib/list.go @@ -82,7 +82,7 @@ func Len(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token { } length := 0 - if input.Tag = ast.STRING { + if input.Tag == ast.STRING { length = len(input.Value()) } else { diff --git a/stdlib/stdlib.go b/stdlib/stdlib.go index 24d1de4..be21deb 100644 --- a/stdlib/stdlib.go +++ b/stdlib/stdlib.go @@ -20,7 +20,6 @@ package stdlib import ( "os" "fmt" - "gitlab.com/whom/shs/log" "gitlab.com/whom/shs/ast" "gitlab.com/whom/shs/util" ) @@ -32,316 +31,316 @@ func GenFuncTable() ast.FuncTable { var stdlib ast.FuncTable stdlib = &map[string]*ast.Function{ "if": &ast.Function{ - Function: ShsIf, + Function: ShsIf, Name: "if", - NumArgs: 3, - EvalLazy true, - ArgLazy true + NumArgs: 3, + EvalLazy: true, + ArgLazy: true, }, "while": &ast.Function{ - Function: ShsWhile, + Function: ShsWhile, Name: "while", - NumArgs: -1, - EvalLazy true, - ArgLazy true, + NumArgs: -1, + EvalLazy: true, + ArgLazy: true, }, "progn": &ast.Function{ Function: ShsProgn, - Name: "shs_progn", - NumArgs: -1, - EvalLazy: true, - ArgLazy: true, + Name: "shs_progn", + NumArgs: -1, + EvalLazy: true, + ArgLazy: true, }, "func": &ast.Function{ - Function: DeclFunc, - Name: "decl_func", - Args: []ast.Token_t{ + Function: DeclFunc, + Name: "decl_func", + Args: []ast.Token_t{ ast.STRING, ast.LIST, - ast.LIST + ast.LIST, }, - EvalLazy: true, + EvalLazy: true, }, "len": &ast.Function{ - Function: Len, - Name: "len", - NumArgs: 1, - ArgLazy: true, + Function: Len, + Name: "len", + NumArgs: 1, + ArgLazy: true, }, "head": &ast.Function{ - Function: Head, + Function: Head, Name: "head", - Args: []ast.Token_t{ - ast.LIST + Args: []ast.Token_t{ + ast.LIST, }, }, "tail": &ast.Function{ Function: Tail, - Name: "tail", - Args: []ast.Token_t{ - ast.LIST + Name: "tail", + Args: []ast.Token_t{ + ast.LIST, }, }, "slice": &ast.Function{ Function: Slice, - Name: "slice", - Args: []ast.Token_t{ + Name: "slice", + Args: []ast.Token_t{ ast.NUMBER, ast.NUMBER, - ast.LIST + ast.LIST, }, }, "export": &ast.Function{ - Function: Export, - Name: "export", - LazyEval: true, - Args: []ast.Token_t{ + Function: Export, + Name: "export", + EvalLazy: true, + Args: []ast.Token_t{ ast.STRING, - ast.LIST + ast.LIST, }, }, "input": &ast.Function{ - Function: Input, - Name: "input", - Args: []ast.Token_t{ - ast.STRING + Function: Input, + Name: "input", + Args: []ast.Token_t{ + ast.STRING, }, }, "load": &ast.Function{ - Function: Load, - Name: "load", - Args: []ast.Token_t{ - ast.STRING + Function: Load, + Name: "load", + Args: []ast.Token_t{ + ast.STRING, }, }, "bool": &ast.Function{ - Function: BoolCast, - Name: "bool", - Args: []ast.Token_t{ - ast.STRING + Function: BoolCast, + Name: "bool", + Args: []ast.Token_t{ + ast.STRING, }, }, "string": &ast.Function{ - Function: StrCast, - Name: "string", - NumArgs: 1, - ArgLazy: true, + Function: StrCast, + Name: "string", + NumArgs: 1, + ArgLazy: true, }, "number": &ast.Function{ - Function: NumCast, - Name: "number", - Args: []ast.Token{ - ast.NUMBER + Function: NumCast, + Name: "number", + Args: []ast.Token_t{ + ast.NUMBER, }, }, "...": &ast.Function{ - Function: Expand, - Name: "...", - Args: []ast.Token_t{ - ast.LIST + Function: Expand, + Name: "...", + Args: []ast.Token_t{ + ast.LIST, }, }, "append": &ast.Function{ - Function: L_append, - Name: "append", - NumArgs: -1, - ArgLazy: true, + Function: L_append, + Name: "append", + NumArgs: -1, + ArgLazy: true, }, "join": &ast.Function{ - Function: Join, - Name: "join", - Args: []ast.Token_t{ + Function: Join, + Name: "join", + Args: []ast.Token_t{ ast.STRING, - ast.LIST + ast.LIST, }, }, "split": &ast.Function{ Function: Split, - Name: "split", - Args: ast.Token_t{ + Name: "split", + Args: []ast.Token_t{ + ast.STRING, ast.STRING, - ast.STRING }, }, "substr": &ast.Function{ - Function: Substr, - Name: "substr", - Args: []ast.Token_t{ + Function: Substr, + Name: "substr", + Args: []ast.Token_t{ ast.NUMBER, ast.NUMBER, - ast.STRING + ast.STRING, }, }, "exit": &ast.Function{ - Function: ExitShell, - Name: "exit", - Args: []ast.Token_t{}, + Function: ExitShell, + Name: "exit", + Args: []ast.Token_t{}, }, "eq": &ast.Function{ - Function: Eq, - Name: "==", - Args: 2, - ArgLazy: true, + Function: Eq, + Name: "==", + NumArgs: 2, + ArgLazy: true, }, "ne": &ast.Function{ - Function: Ne, - Name: "!=", - Args: 2, - ArgLazy: true, + Function: Ne, + Name: "!=", + NumArgs: 2, + ArgLazy: true, }, "<": &ast.Function{ - Function: Lt, - Name: "<", - Args: []ast.Token_t{ + Function: Lt, + Name: "<", + Args: []ast.Token_t{ + ast.NUMBER, ast.NUMBER, - ast.NUMBER }, }, ">": &ast.Function{ - Function: Gt, - Name: ">", - Args: []ast.Token_t{ + Function: Gt, + Name: ">", + Args: []ast.Token_t{ + ast.NUMBER, ast.NUMBER, - ast.NUMBER }, }, "<=": &ast.Function{ - Function: Lte, - Name: "<=", - Args: []ast.Token_t{ + Function: Lte, + Name: "<=", + Args: []ast.Token_t{ + ast.NUMBER, ast.NUMBER, - ast.NUMBER }, }, ">=": &ast.Function{ - Function: Gte, - Name: ">=", - Args: []ast.Token_t{ + Function: Gte, + Name: ">=", + Args: []ast.Token_t{ + ast.NUMBER, ast.NUMBER, - ast.NUMBER }, }, "!": &ast.Function{ - Function: Not, - Name: "!", - Args: []ast.Token_t{ - ast.Bool + Function: Not, + Name: "!", + Args: []ast.Token_t{ + ast.BOOL, }, }, "+": &ast.Function{ - Function: Add, - Name: "add", - NumArgs: -1, - ArgLazy: true, + Function: Add, + Name: "add", + NumArgs: -1, + ArgLazy: true, }, "-": &ast.Function{ - Function: Sub, - Name: "sub", - NumArgs: -1, - ArgLazy: true, + Function: Sub, + Name: "sub", + NumArgs: -1, + ArgLazy: true, }, "*": &ast.Function{ - Function: Mult, + Function: Mult, Name: "mult", - NumArgs: -1, - ArgLazy: true, + NumArgs: -1, + ArgLazy: true, }, "/": &ast.Function{ - Function: Div, - Name: "div", - NumArgs: -1, - ArgLazy: true, + Function: Div, + Name: "div", + NumArgs: -1, + ArgLazy: true, }, "cd": &ast.Function{ - Function: Cd, - Name: "changedir", - Args: []ast.Token_t{ - ast.STRING + Function: Cd, + Name: "changedir", + Args: []ast.Token_t{ + ast.STRING, }, }, "concat": &ast.Function{ Function: Concat, - Name:"concatenate", - NumArgs: -1, - ArgLazy: true, + Name: "concatenate", + NumArgs: -1, + ArgLazy: true, }, "print": &ast.Function{ - Function: PrintStr, - Name: "print", - Args: []ast.Token_t{ - ast.STRING + Function: PrintStr, + Name: "print", + Args: []ast.Token_t{ + ast.STRING, }, }, "l": &ast.Function{ - Function: Call, + Function: Call, Name: "call", - NumArgs: -1, - ArgLazy: true, - SymLazy: true, + NumArgs: -1, + ArgLazy: true, + SymLazy: true, }, "bg": &ast.Function{ - Function: Bgcall, - Name: "background call", - NumArgs: -1, - ArgLazy: true, - SymLazy: true, + Function: Bgcall, + Name: "background call", + NumArgs: -1, + ArgLazy: true, + SymLazy: true, }, "fg": &ast.Function{ - Function: Fg, - Name: "foreground", - NumArgs: []ast.Token_t{ - ast.NUMBER + Function: Fg, + Name: "foreground", + Args: []ast.Token_t{ + ast.NUMBER, }, }, "$": &ast.Function{ - Function: ReadCmd, - Name: "read cmd", - NumnArgs: -1, - ArgLazy: true, - SymLazy: true, + Function: ReadCmd, + Name: "read cmd", + NumArgs: -1, + ArgLazy: true, + SymLazy: true, }, "?": &ast.Function{ - Function: GetExit, - Name:"get exit code", - Args: ast.Token_t{}, + Function: GetExit, + Name: "get exit code", + Args: []ast.Token_t{}, }, /* @@ -354,50 +353,50 @@ func GenFuncTable() ast.FuncTable { */ "jobs": &ast.Function{ - Function: Jobs, - Name: "list jobs", - Args: ast.Token_t{}, + Function: Jobs, + Name: "list jobs", + Args: []ast.Token_t{}, }, "info": &ast.Function{ - Function: ShInfo, - Name: "Shell Info", - Args: ast.Token_t{ - ast.SYMBOL + Function: ShInfo, + Name: "Shell Info", + Args: []ast.Token_t{ + ast.SYMBOL, }, }, "fexists": &ast.Function{ - Function: Fexists, - Name: "file exists", - Args: []ast.Token_t{ - ast.STRING + Function: Fexists, + Name: "file exists", + Args: []ast.Token_t{ + ast.STRING, }, }, "fread": &ast.Function{ - Function: Fread, - Name: "read file", - Args: []ast.Token_t{ - ast.STRING + Function: Fread, + Name: "read file", + Args: []ast.Token_t{ + ast.STRING, }, }, "fwrite": &ast.Function{ - Function: Fwrite, - Name: "write file", - Args: []ast.Token_t{ + Function: Fwrite, + Name: "write file", + Args: []ast.Token_t{ + ast.STRING, ast.STRING, - ast.STRING }, }, "fappend": &ast.Function{ - Function: Fappend, - Name:"append to file", - Args: []ast.Token_t{ + Function: Fappend, + Name: "append to file", + Args: []ast.Token_t{ + ast.STRING, ast.STRING, - ast.STRING }, }, } @@ -421,34 +420,32 @@ func ExitShell(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { * Example: (info append) */ func ShInfo(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { - switch in.Tag { - case ast.BOOL: - fmt.Printf("BOOL LITERAL\nValue: %s\n", in.Value()) - case ast.STRING: - fmt.Printf("STRING LITERAL \nValue: %s\n", in.Value()) - case ast.NUMBER: - fmt.Printf("NUMBER LITERAL \nValue: %s\n", in.Value()) - case ast.LIST: - fmt.Printf("LIST \nString Value: %s, AST:\n", in.String()) - ast.PrintSExprsIndividually(in) - case ast.SYMBOL: - repr := ast.GetVar(in.Value(), vt) - if repr != nil { - fmt.Printf("VARIABLE\nTYPE: %s\nVALUE: %s\n", ast.GetTagAsStr(repr.Tag), repr.Value()) - break - } - - funct := ast.GetFunction(in.Value(), ft) - if funct != nil { - fmt.Printf("FUNCTION\nNAME: %s\nTIMES CALLED: %s\nNUM ARGS: %d\n", funct.Name, funct.TimesCalled, funct.NumArgs) - break - } - - // TODO: print func args - - fmt.Printf("UNKNOWN SYMBOL\n") + repr := ast.GetVar(in.Value(), vt) + if repr != nil { + fmt.Printf("VARIABLE\nTYPE: %s\nVALUE: %s\n", ast.GetTagAsStr(repr.Tag), repr.Value()) + return nil } + funct := ast.GetFunction(in.Value(), ft) + if funct != nil { + fmt.Printf("FUNCTION\nNAME: %s\nTIMES CALLED: %s\n\n", funct.Name, funct.TimesCalled) + if funct.ArgLazy { + fmt.Printf("function does not evaluate args by type\nARG COUNT: %s\n", funct.NumArgs) + } else { + fmt.Printf("function evaluates args by type\nARGS: ") + for _, i := range(funct.Args) { + fmt.Printf(ast.GetTagAsStr(i)) + } + } + + fmt.Printf("SYMLAZY: %t\n", funct.SymLazy) + fmt.Printf("(can undefined symbols be passed in)\n") + fmt.Printf("EVALLAZY: %t\n", funct.EvalLazy) + fmt.Printf("(are all forms in evaluated before function call)\n") + return nil + } + + fmt.Printf("UNKNOWN SYMBOL\n") return nil } diff --git a/stdlib/string.go b/stdlib/string.go index 01d1f60..090932e 100644 --- a/stdlib/string.go +++ b/stdlib/string.go @@ -125,6 +125,7 @@ func Join(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { * Example: (substr 1 5 "Linus Torvalds") -> "inus " */ func Substr(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { + start := in end := start.Next str := end.Next