refactored to correctly use module structure
This commit is contained in:
parent
640dbb183e
commit
cf3255f015
9 changed files with 60 additions and 83 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -5,6 +5,7 @@ GRPATH
|
|||
|
||||
# ctags
|
||||
TAGS
|
||||
tags*
|
||||
|
||||
# binaries
|
||||
print_ast
|
||||
|
|
|
|||
|
|
@ -15,25 +15,25 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package shs
|
||||
package ast
|
||||
|
||||
type VarTable map[string]*Token
|
||||
|
||||
|
|
@ -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:], " ")))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,19 +15,23 @@
|
|||
* 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 {
|
||||
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
|
||||
}
|
||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module git.callpipe.com/aidan/shs
|
||||
|
||||
go 1.14
|
||||
|
|
@ -15,48 +15,50 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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))
|
||||
|
|
@ -15,9 +15,13 @@
|
|||
* 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 {
|
||||
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
|
||||
}
|
||||
38
tags
38
tags
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue