fleshed out GetVar, added Token stack collection, started printing module
This commit is contained in:
parent
516fda54b3
commit
84013cb4a0
6 changed files with 89 additions and 2 deletions
Binary file not shown.
BIN
pkg/shsh/.stack.go.swp
Normal file
BIN
pkg/shsh/.stack.go.swp
Normal file
Binary file not shown.
57
pkg/shsh/debug.go
Normal file
57
pkg/shsh/debug.go
Normal 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
22
pkg/shsh/stack.go
Normal 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
|
||||||
|
}
|
||||||
|
|
@ -14,7 +14,7 @@ const (
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
next *Token
|
next *Token
|
||||||
tag parse_tag
|
tag token_t
|
||||||
int position
|
int position
|
||||||
_inner interface{}
|
_inner interface{}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,5 +13,13 @@ var (
|
||||||
// Library represents variables defined in inner scope
|
// Library represents variables defined in inner scope
|
||||||
// It is assumed library is ordered from innermost scope to outermost scope
|
// It is assumed library is ordered from innermost scope to outermost scope
|
||||||
func GetVar(arg string, library []VarTable) *Token {
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue