From cf3255f015bf8ba4525692f69dc0bbd1091ca686 Mon Sep 17 00:00:00 2001 From: Aidan Date: Wed, 17 Jun 2020 00:28:16 -0700 Subject: [PATCH] refactored to correctly use module structure --- .gitignore | 1 + token.go => ast/token.go | 36 ++++++++++++------------- var_table.go => ast/var_table.go | 2 +- cmd/print_ast.go | 5 ++-- stack.go => datatypes/stack.go | 12 ++++++--- go.mod | 3 +++ debug.go => io/debug.go | 34 +++++++++++++----------- func_table.go => stdlib/func_table.go | 12 ++++++--- tags | 38 --------------------------- 9 files changed, 60 insertions(+), 83 deletions(-) rename token.go => ast/token.go (88%) rename var_table.go => ast/var_table.go (99%) rename stack.go => datatypes/stack.go (85%) create mode 100644 go.mod rename debug.go => io/debug.go (69%) rename func_table.go => stdlib/func_table.go (86%) delete mode 100644 tags diff --git a/.gitignore b/.gitignore index 7182eec..9b9aaf0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ GRPATH # ctags TAGS +tags* # binaries print_ast diff --git a/token.go b/ast/token.go similarity index 88% rename from token.go rename to ast/token.go index 42f42fa..a8d993b 100644 --- a/token.go +++ b/ast/token.go @@ -15,25 +15,25 @@ * along with this program. If not, see . */ -package shs; +package ast; import ( "unicode" ) -type token_t int +type Token_t int const ( - LIST token_t = iota - STRING token_t = iota - NUMBER token_t = iota - SYMBOL token_t = iota + LIST Token_t = iota + STRING Token_t = iota + NUMBER Token_t = iota + SYMBOL Token_t = iota ) type Token struct { - next *Token - tag token_t - position int - _inner interface{} + Next *Token + Tag Token_t + Position int + Inner interface{} } const string_delims string = "\"'`" @@ -54,28 +54,28 @@ func Lex(input string) *Token { } *iter = new(Token) - (*iter).position = pos + (*iter).Position = pos if is_list { - (*iter)._inner = Lex(tok) - (*iter).tag = LIST + (*iter).Inner = Lex(tok) + (*iter).Tag = LIST is_list = false } else { - (*iter)._inner = tok + (*iter).Inner = tok if is_str { - (*iter).tag = STRING + (*iter).Tag = STRING is_str = false } else if tokenIsNumber(tok) { - (*iter).tag = NUMBER + (*iter).Tag = NUMBER } else { - (*iter).tag = SYMBOL + (*iter).Tag = SYMBOL } } - iter = &(*iter).next + iter = &(*iter).Next } // returns -1 on unmatched string delim diff --git a/var_table.go b/ast/var_table.go similarity index 99% rename from var_table.go rename to ast/var_table.go index af79539..bb51603 100644 --- a/var_table.go +++ b/ast/var_table.go @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package shs +package ast type VarTable map[string]*Token diff --git a/cmd/print_ast.go b/cmd/print_ast.go index 44089a8..58c6f73 100644 --- a/cmd/print_ast.go +++ b/cmd/print_ast.go @@ -20,9 +20,10 @@ package main import ( "strings" "os" - "git.callpipe.com/aidan/shs" + "git.callpipe.com/aidan/shs/io" + "git.callpipe.com/aidan/shs/ast" ) func main() { - shs.PrintSExpression(shs.Lex(strings.Join(os.Args[1:], " "))) + io.PrintSExpression(ast.Lex(strings.Join(os.Args[1:], " "))) } diff --git a/stack.go b/datatypes/stack.go similarity index 85% rename from stack.go rename to datatypes/stack.go index 5c05c91..9f8d724 100644 --- a/stack.go +++ b/datatypes/stack.go @@ -15,19 +15,23 @@ * along with this program. If not, see . */ -package shs +package datatypes + +import ( + "git.callpipe.com/aidan/shs/ast" +) type TokenStack struct { - buffer []*Token + buffer []*ast.Token capacity int } -func (s *TokenStack) Push(v *Token) { +func (s *TokenStack) Push(v *ast.Token) { s.capacity++ s.buffer = append(s.buffer, v) } -func (s *TokenStack) Pop() *Token { +func (s *TokenStack) Pop() *ast.Token { if s.capacity <= 0 { return nil } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b28dc53 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.callpipe.com/aidan/shs + +go 1.14 diff --git a/debug.go b/io/debug.go similarity index 69% rename from debug.go rename to io/debug.go index 8c2ea9e..13ba2e1 100644 --- a/debug.go +++ b/io/debug.go @@ -15,48 +15,50 @@ * along with this program. If not, see . */ -package shs +package io import ( "strings" "fmt" + "git.callpipe.com/aidan/shs/ast" + "git.callpipe.com/aidan/shs/datatypes" ) -func FmtToken(arg *Token) string { +func FmtToken(arg *ast.Token) string { suffix := "->" - if arg.next == nil { + if arg.Next == nil { suffix = "" } - switch arg.tag { - case LIST: + switch arg.Tag { + case ast.LIST: return fmt.Sprintf("(%s, [List])%s", "LIST", suffix) default: - return fmt.Sprintf("(%s, %s)%s", GetTagAsStr(arg.tag), arg._inner, suffix) + return fmt.Sprintf("(%s, %s)%s", GetTagAsStr(arg.Tag), arg.Inner, suffix) } } -func GetTagAsStr(tag token_t) string { +func GetTagAsStr(tag ast.Token_t) string { switch tag { - case LIST: + case ast.LIST: return "LIST" - case STRING: + case ast.STRING: return "STRING" - case NUMBER: + case ast.NUMBER: return "NUMBER" - case SYMBOL: + case ast.SYMBOL: return "SYMBOL" } return "UNKNOWN" } -func PrintSExpression(arg *Token) { +func PrintSExpression(arg *ast.Token) { if arg == nil { return //TODO: Handle error here? } - var lists TokenStack; + var lists datatypes.TokenStack; lists.Push(arg) loop: @@ -66,9 +68,9 @@ loop: return } - for iter := i; iter != nil; iter = iter.next { - if iter.tag == LIST { - lists.Push(iter._inner.(*Token)) + for iter := i; iter != nil; iter = iter.Next { + if iter.Tag == ast.LIST { + lists.Push(iter.Inner.(*ast.Token)) } constructor.WriteString(FmtToken(iter)) diff --git a/func_table.go b/stdlib/func_table.go similarity index 86% rename from func_table.go rename to stdlib/func_table.go index 587d145..e6b551d 100644 --- a/func_table.go +++ b/stdlib/func_table.go @@ -15,9 +15,13 @@ * along with this program. If not, see . */ -package shs +package stdlib -type Operation func(*Token) *Token +import ( + "git.callpipe.com/aidan/shs/ast" +) + +type Operation func(*ast.Token) *ast.Token type Function struct { function Operation @@ -32,7 +36,7 @@ var ( ) // TODO: Currently only checks arg list length -func ParseFunction(target *Function, args *Token) bool { +func ParseFunction(target *Function, args *ast.Token) bool { // HANDLE EXEC if target.args < 0 { return true @@ -51,7 +55,7 @@ func ParseFunction(target *Function, args *Token) bool { return true } -func CallFunction(target *Function, args *Token) *Token { +func CallFunction(target *Function, args *ast.Token) *ast.Token { if !ParseFunction(target, args) { return args } diff --git a/tags b/tags deleted file mode 100644 index 814afde..0000000 --- a/tags +++ /dev/null @@ -1,38 +0,0 @@ -!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ -!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ -!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ -!_TAG_PROGRAM_NAME Exuberant Ctags // -!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ -!_TAG_PROGRAM_VERSION Development // -CallFunction func_table.go /^func CallFunction(target *Function, args *Token) *Token {$/;" f -FmtToken debug.go /^func FmtToken(arg *Token) string {$/;" f -FuncTable func_table.go /^type FuncTable map[string]*Function$/;" t -Function func_table.go /^type Function struct {$/;" t -GetFunction func_table.go /^func GetFunction(arg string) *Function {$/;" f -GetTagAsStr debug.go /^func GetTagAsStr(tag token_t) string {$/;" f -GetVar var_table.go /^func GetVar(arg string, library []VarTable) *Token {$/;" f -GlobalFuncTable func_table.go /^ GlobalFuncTable *FuncTable$/;" v -GlobalVarTable var_table.go /^ GlobalVarTable *VarTable$/;" v -LIST token.go /^ LIST token_t = iota$/;" c -Lex token.go /^func Lex(input string) *Token {$/;" f -NUMBER token.go /^ NUMBER token_t = iota$/;" c -Operation func_table.go /^type Operation func(*Token) *Token$/;" t -ParseFunction func_table.go /^func ParseFunction(target *Function, args *Token) bool {$/;" f -Pop stack.go /^func (s *TokenStack) Pop() *Token {$/;" f -PrintSExpression debug.go /^func PrintSExpression(arg *Token) {$/;" f -Push stack.go /^func (s *TokenStack) Push(v *Token) {$/;" f -STRING token.go /^ STRING token_t = iota$/;" c -SYMBOL token.go /^ SYMBOL token_t = iota$/;" c -Token token.go /^type Token struct {$/;" t -TokenStack stack.go /^type TokenStack struct {$/;" t -VarTable var_table.go /^type VarTable map[string]*Token$/;" t -main cmd/print_ast.go /^func main() {$/;" f -main cmd/print_ast.go /^package main$/;" p -shs debug.go /^package shs$/;" p -shs func_table.go /^package shs$/;" p -shs stack.go /^package shs$/;" p -shs token.go /^package shs;$/;" p -shs var_table.go /^package shs$/;" p -string_delims token.go /^const string_delims string = "\\"'`"$/;" c -tokenIsNumber token.go /^func tokenIsNumber(arg string) bool {$/;" f -token_t token.go /^type token_t int$/;" t