add return stack

This commit is contained in:
David 2021-02-14 11:58:43 -05:00
parent bce7e3df4e
commit 7212f3d9f6
3 changed files with 43 additions and 0 deletions

View File

@ -184,6 +184,43 @@ func (b *Builtins) Emit(s *Stack) func() error {
}
}
// ToR pops from the stack to the return stack
func (b *Builtins) ToR(s *Stack, r *Stack) func() error {
return func() error {
r1, err := s.Pop()
if err != nil {
return err
}
r.Push(r1)
return nil
}
}
// RFrom pops from the return stack to the stack
func (b *Builtins) RFrom(s *Stack, r *Stack) func() error {
return func() error {
r1, err := r.Pop()
if err != nil {
return err
}
s.Push(r1)
return nil
}
}
// RFetch copies from the return stack to the stack
func (b *Builtins) RFetch(s *Stack, r *Stack) func() error {
return func() error {
r1, err := r.Pop()
if err != nil {
return err
}
r.Push(r1)
s.Push(r1)
return nil
}
}
// CR prints a newline to standard out
func (b *Builtins) CR() func() error {
return func() error {

View File

@ -10,6 +10,7 @@ import (
type Context struct {
Dictionary Dictionary
Stack *Stack
RStack *Stack
}
// Eval evaulates a given line, recursively descending into given words as needed

View File

@ -10,6 +10,7 @@ import (
func main() {
stack := Stack{values: []int{}}
rstack := Stack{values: []int{}}
dict := Dictionary{}
b := &Builtins{}
@ -26,12 +27,16 @@ func main() {
dict.AddWord("WORDS", b.Words(dict), "")
dict.AddWord(".S", b.Debug(&stack), "")
dict.AddWord("EMIT", b.Emit(&stack), "")
dict.AddWord("R>", b.RFrom(&stack, &rstack), "")
dict.AddWord(">R", b.ToR(&stack, &rstack), "")
dict.AddWord("R@", b.RFetch(&stack, &rstack), "")
dict.AddWord("CR", b.CR(), "")
dict.AddWord("QUIT", b.Quit(), "")
c := Context{
Dictionary: dict,
Stack: &stack,
RStack: &rstack,
}
reader := bufio.NewReader(os.Stdin)