diff --git a/cmd/shsh/shsh.go b/cmd/shsh/shsh.go index e69de29..5caa406 100644 --- a/cmd/shsh/shsh.go +++ b/cmd/shsh/shsh.go @@ -0,0 +1,14 @@ +package main + +import ( + "bufio" + "os" + "shsh" +) + +func main() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + PrintSExpression(Lex(scanner.Text())) + } +} diff --git a/pkg/shsh/.token.go.swp b/pkg/shsh/.token.go.swp deleted file mode 100644 index a2b523f..0000000 Binary files a/pkg/shsh/.token.go.swp and /dev/null differ diff --git a/pkg/shsh/debug.go b/pkg/shsh/debug.go index ad16f5e..290720e 100644 --- a/pkg/shsh/debug.go +++ b/pkg/shsh/debug.go @@ -2,6 +2,7 @@ package shsh import ( "strings" + "fmt" ) func FmtToken(arg *Token) string { @@ -12,11 +13,11 @@ func FmtToken(arg *Token) string { switch arg.tag { case LIST: - return sprintf("(%s, List @ %p, %d)%s", "LIST", arg._inner, + return fmt.Sprintf("(%s, List @ %p, %d)%s", "LIST", arg._inner, arg.position, suffix) default: - return sprintf("(%s, %s, %d)%s", GetTagAsStr(arg.tag), arg._inner, + return fmt.Sprintf("(%s, %s, %d)%s", GetTagAsStr(arg.tag), arg._inner, arg.position, suffix) } } @@ -32,26 +33,24 @@ func GetTagAsStr(tag token_t) string { case SYMBOL: return "SYMBOL" } + return "UNKNOWN" } func PrintSExpression(arg *Token) { - var lists Stack; - lists.push(arg) + var lists TokenStack; + lists.Push(arg) loop: var constructor strings.Builder - i := lists.pop() + i := lists.Pop() if i == nil { - goto done + return } - for (iter := i; iter != nil; iter = i.next { + for iter := i; iter != nil; iter = i.next { constructor.WriteString(FmtToken(iter)) } print(constructor.String()) goto loop - -done: - // TODO: Perhaps print some debug analytics here } diff --git a/pkg/shsh/func_table.go b/pkg/shsh/func_table.go index 50821d5..230a4b3 100644 --- a/pkg/shsh/func_table.go +++ b/pkg/shsh/func_table.go @@ -1,13 +1,9 @@ package shsh -import ( - "strings" -) - type Operation func(*Token) *Token type Function struct { - func operation + function Operation timesCalled int args int } @@ -26,7 +22,7 @@ func ParseFunction(target *Function, args *Token) bool { } i := target.args - for iter = &args; *iter; iter = &(*iter.next) { + for iter := &args; *iter != nil; iter = &((*iter).next) { i -= 1 } @@ -44,11 +40,11 @@ func CallFunction(target *Function, args *Token) *Token { } target.timesCalled += 1 - return target.operation(args) + return target.function(args) } -func GetFunction(string arg) *Function { - target, ok := GlobalFuncTable[arg] +func GetFunction(arg string) *Function { + target, ok := (*GlobalFuncTable)[arg] if !ok { // TODO: hook into stdlib exec call ; diff --git a/pkg/shsh/stack.go b/pkg/shsh/stack.go index 21d7daa..0dbb7df 100644 --- a/pkg/shsh/stack.go +++ b/pkg/shsh/stack.go @@ -2,21 +2,21 @@ package shsh type TokenStack struct { buffer []*Token - int capacity + capacity int } -func (s *Stack) Push(v *Token) { +func (s *TokenStack) Push(v *Token) { s.capacity++ - *s.buffer = append(*s.buffer, v) + s.buffer = append(s.buffer, v) } -func (s *Stack) Pop() *Token { +func (s *TokenStack) Pop() *Token { if s.capacity <= 0 { return nil } s.capacity-- - res := *s.buffer[len(*s.buffer) - 1] - *s = *s.buffer[ :len(*s) - 1] - return ret + res := s.buffer[len(s.buffer) - 1] + s.buffer = s.buffer[:len(s.buffer) - 1] + return res } diff --git a/pkg/shsh/token.go b/pkg/shsh/token.go index 40d85a0..24805e1 100644 --- a/pkg/shsh/token.go +++ b/pkg/shsh/token.go @@ -2,6 +2,7 @@ package shsh; import ( "strings" + "unicode" ) type token_t int @@ -15,7 +16,7 @@ const ( type Token struct { next *Token tag token_t - int position + position int _inner interface{} } @@ -25,9 +26,9 @@ func Lex(input string) *Token { delim := ' ' var tok strings.Builder iter_alloced := false - is_list = false - is_str = false - is_num = true + is_list := false + is_str := false + is_num := true for pos, char := range input { switch char { @@ -41,24 +42,24 @@ func Lex(input string) *Token { case delim: *iter = new(Token) - *iter.position = pos + (*iter).position = pos if is_list { - *iter._inner = Lex(tok.String()) + (*iter)._inner = Lex(tok.String()) is_list = false - *iter._inner = LIST + (*iter)._inner = LIST } else { - *iter._inner = tok.String() - if is_string { - *iter.tag = STRING + (*iter)._inner = tok.String() + if is_str { + (*iter).tag = STRING is_str = false } else if is_num { - *iter.tag = NUMBER + (*iter).tag = NUMBER } else { - *iter.tag = SYMBOL + (*iter).tag = SYMBOL } } @@ -66,12 +67,12 @@ func Lex(input string) *Token { delim = ' ' default: - is_num = is_num && IsDigit(char) + is_num = is_num && unicode.IsDigit(char) tok.WriteRune(char) } if iter_alloced { - iter = &(*iter.next) + iter = &((*iter).next) iter_alloced = false tok.Reset() } diff --git a/pkg/shsh/var_table.go b/pkg/shsh/var_table.go index 0d7d807..9ab28e2 100644 --- a/pkg/shsh/var_table.go +++ b/pkg/shsh/var_table.go @@ -1,9 +1,5 @@ package shsh -import ( - "strings" -) - type VarTable map[string]*Token var ( @@ -13,9 +9,10 @@ var ( // 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 dict, scope := library { + 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 }