fleshed out GetVar, added Token stack collection, started printing module

This commit is contained in:
Aidan Hahn 2019-11-28 08:57:12 -08:00
parent 516fda54b3
commit 84013cb4a0
No known key found for this signature in database
GPG key ID: 327711E983899316
6 changed files with 89 additions and 2 deletions

BIN
pkg/shsh/.stack.go.swp Normal file

Binary file not shown.

57
pkg/shsh/debug.go Normal file
View file

@ -0,0 +1,57 @@
package shsh
import (
"collections"
)
func FmtToken(arg *Token) string {
suffix := ""
if arg.next != nil {
suffix = "->"
}
switch arg.tag {
case LIST:
return sprintf("(%s, List @ %p, %d)%s", "LIST", arg._inner,
arg.position, suffix)
default:
return sprintf("(%s, %s, %d)%s", GetTagAsStr(arg.tag), arg._inner,
arg.position, suffix)
}
}
func GetTagAsStr(tag token_t) string {
switch tag {
case LIST:
return "LIST"
case STRING:
return "STRING"
case NUMBER:
return "NUMBER"
case SYMBOL:
return "SYMBOL"
}
}
func PrintSExpression(arg *Token) {
var lists Stack;
lists.push(arg)
loop:
// string builder?
i := lists.pop()
if i == nil {
goto done
}
for (iter := i; iter != nil; iter = i.next {
// add each string to string builder?
}
// print stringbuilder?
goto loop
done:
// TODO: Perhaps print some debug analytics here
}

22
pkg/shsh/stack.go Normal file
View file

@ -0,0 +1,22 @@
package shsh
type TokenStack struct {
buffer []*Token
int capacity
}
func (s *Stack) Push(v *Token) {
s.capacity++
*s.buffer = append(*s.buffer, v)
}
func (s *Stack) 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
}

View file

@ -14,7 +14,7 @@ const (
type Token struct {
next *Token
tag parse_tag
tag token_t
int position
_inner interface{}
}

View file

@ -13,5 +13,13 @@ 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 {
val, ok := dict[arg]
if ok {
// TODO: maybe log which scope it was found in?
return val
}
}
return nil
}