refactored to correctly use module structure

This commit is contained in:
Aidan 2020-06-17 00:28:16 -07:00
parent 640dbb183e
commit cf3255f015
No known key found for this signature in database
GPG key ID: 327711E983899316
9 changed files with 60 additions and 83 deletions

1
.gitignore vendored
View file

@ -5,6 +5,7 @@ GRPATH
# ctags # ctags
TAGS TAGS
tags*
# binaries # binaries
print_ast print_ast

View file

@ -15,25 +15,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package shs; package ast;
import ( import (
"unicode" "unicode"
) )
type token_t int type Token_t int
const ( const (
LIST token_t = iota LIST Token_t = iota
STRING token_t = iota STRING Token_t = iota
NUMBER token_t = iota NUMBER Token_t = iota
SYMBOL token_t = iota SYMBOL Token_t = iota
) )
type Token struct { type Token struct {
next *Token Next *Token
tag token_t Tag Token_t
position int Position int
_inner interface{} Inner interface{}
} }
const string_delims string = "\"'`" const string_delims string = "\"'`"
@ -54,28 +54,28 @@ func Lex(input string) *Token {
} }
*iter = new(Token) *iter = new(Token)
(*iter).position = pos (*iter).Position = pos
if is_list { if is_list {
(*iter)._inner = Lex(tok) (*iter).Inner = Lex(tok)
(*iter).tag = LIST (*iter).Tag = LIST
is_list = false is_list = false
} else { } else {
(*iter)._inner = tok (*iter).Inner = tok
if is_str { if is_str {
(*iter).tag = STRING (*iter).Tag = STRING
is_str = false is_str = false
} else if tokenIsNumber(tok) { } else if tokenIsNumber(tok) {
(*iter).tag = NUMBER (*iter).Tag = NUMBER
} else { } else {
(*iter).tag = SYMBOL (*iter).Tag = SYMBOL
} }
} }
iter = &(*iter).next iter = &(*iter).Next
} }
// returns -1 on unmatched string delim // returns -1 on unmatched string delim

View file

@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package shs package ast
type VarTable map[string]*Token type VarTable map[string]*Token

View file

@ -20,9 +20,10 @@ package main
import ( import (
"strings" "strings"
"os" "os"
"git.callpipe.com/aidan/shs" "git.callpipe.com/aidan/shs/io"
"git.callpipe.com/aidan/shs/ast"
) )
func main() { func main() {
shs.PrintSExpression(shs.Lex(strings.Join(os.Args[1:], " "))) io.PrintSExpression(ast.Lex(strings.Join(os.Args[1:], " ")))
} }

View file

@ -15,19 +15,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package shs package datatypes
import (
"git.callpipe.com/aidan/shs/ast"
)
type TokenStack struct { type TokenStack struct {
buffer []*Token buffer []*ast.Token
capacity int capacity int
} }
func (s *TokenStack) Push(v *Token) { func (s *TokenStack) Push(v *ast.Token) {
s.capacity++ s.capacity++
s.buffer = append(s.buffer, v) s.buffer = append(s.buffer, v)
} }
func (s *TokenStack) Pop() *Token { func (s *TokenStack) Pop() *ast.Token {
if s.capacity <= 0 { if s.capacity <= 0 {
return nil return nil
} }

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module git.callpipe.com/aidan/shs
go 1.14

View file

@ -15,48 +15,50 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package shs package io
import ( import (
"strings" "strings"
"fmt" "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 := "->" suffix := "->"
if arg.next == nil { if arg.Next == nil {
suffix = "" suffix = ""
} }
switch arg.tag { switch arg.Tag {
case LIST: case ast.LIST:
return fmt.Sprintf("(%s, [List])%s", "LIST", suffix) return fmt.Sprintf("(%s, [List])%s", "LIST", suffix)
default: 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 { switch tag {
case LIST: case ast.LIST:
return "LIST" return "LIST"
case STRING: case ast.STRING:
return "STRING" return "STRING"
case NUMBER: case ast.NUMBER:
return "NUMBER" return "NUMBER"
case SYMBOL: case ast.SYMBOL:
return "SYMBOL" return "SYMBOL"
} }
return "UNKNOWN" return "UNKNOWN"
} }
func PrintSExpression(arg *Token) { func PrintSExpression(arg *ast.Token) {
if arg == nil { if arg == nil {
return //TODO: Handle error here? return //TODO: Handle error here?
} }
var lists TokenStack; var lists datatypes.TokenStack;
lists.Push(arg) lists.Push(arg)
loop: loop:
@ -66,9 +68,9 @@ loop:
return return
} }
for iter := i; iter != nil; iter = iter.next { for iter := i; iter != nil; iter = iter.Next {
if iter.tag == LIST { if iter.Tag == ast.LIST {
lists.Push(iter._inner.(*Token)) lists.Push(iter.Inner.(*ast.Token))
} }
constructor.WriteString(FmtToken(iter)) constructor.WriteString(FmtToken(iter))

View file

@ -15,9 +15,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 { type Function struct {
function Operation function Operation
@ -32,7 +36,7 @@ var (
) )
// TODO: Currently only checks arg list length // TODO: Currently only checks arg list length
func ParseFunction(target *Function, args *Token) bool { func ParseFunction(target *Function, args *ast.Token) bool {
// HANDLE EXEC // HANDLE EXEC
if target.args < 0 { if target.args < 0 {
return true return true
@ -51,7 +55,7 @@ func ParseFunction(target *Function, args *Token) bool {
return true return true
} }
func CallFunction(target *Function, args *Token) *Token { func CallFunction(target *Function, args *ast.Token) *ast.Token {
if !ParseFunction(target, args) { if !ParseFunction(target, args) {
return args return args
} }

38
tags
View file

@ -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