2021-02-14 02:09:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-27 03:09:43 +00:00
|
|
|
"io"
|
2021-02-14 02:09:56 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-02-16 00:24:04 +00:00
|
|
|
// Context is a set of Dictionary + Stacks + Flags representing a runtime environment
|
2021-02-14 02:09:56 +00:00
|
|
|
type Context struct {
|
2021-02-27 01:49:10 +00:00
|
|
|
Dictionary Dictionary
|
|
|
|
Stack *Stack // main stack
|
|
|
|
RStack *Stack // "return" stack, for handling loops/backward jumps
|
|
|
|
IfStack *Stack // tracks nested branches
|
|
|
|
Flags Flags
|
|
|
|
Words []string
|
|
|
|
Memory Memory
|
|
|
|
StringBuffer string
|
2021-02-27 03:09:43 +00:00
|
|
|
Output io.Writer
|
2021-02-14 02:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Eval evaulates a given line, recursively descending into given words as needed
|
|
|
|
func (c *Context) Eval(line string) error {
|
|
|
|
// state
|
|
|
|
var word []byte
|
|
|
|
|
2021-02-14 19:30:15 +00:00
|
|
|
for i := 0; i < len(line); i = i + 1 {
|
|
|
|
switch line[i] {
|
2021-02-16 00:24:04 +00:00
|
|
|
case ' ':
|
|
|
|
sword := strings.TrimSpace(string(word))
|
2021-02-14 02:09:56 +00:00
|
|
|
if len(word) == 0 {
|
2021-02-16 00:24:04 +00:00
|
|
|
// empty space, just continue...
|
2021-02-14 02:09:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-16 00:24:04 +00:00
|
|
|
// Is this a word we know?
|
|
|
|
w, _ := c.Dictionary.GetWord(sword)
|
|
|
|
|
2021-02-27 01:49:10 +00:00
|
|
|
// if we're not in immedate mode, and the word isn't immediate, add word to buffer and continue parsing
|
|
|
|
if !c.Flags.GetFlag("Immediate") && !w.Immediate {
|
2021-02-25 01:19:25 +00:00
|
|
|
c.Words = append(c.Words, sword)
|
2021-02-16 00:24:04 +00:00
|
|
|
word = []byte{}
|
2021-02-14 02:09:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-14 19:27:02 +00:00
|
|
|
|
2021-02-20 20:52:27 +00:00
|
|
|
ifcheck, _ := c.IfStack.Pick(0)
|
|
|
|
if len(c.IfStack.values) == 0 || (len(c.IfStack.values) > 0 && ifcheck == 1) || w.BranchCheck {
|
|
|
|
// run word
|
|
|
|
c.RStack.Push(i)
|
2021-02-25 01:19:25 +00:00
|
|
|
err := c.Exec(w, line[i:])
|
2021-02-21 00:22:36 +00:00
|
|
|
if err != nil {
|
2021-02-20 20:52:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
i, err = c.RStack.Pop()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while popping from return stack: %v", err)
|
|
|
|
}
|
2021-02-15 01:26:30 +00:00
|
|
|
}
|
|
|
|
word = []byte{}
|
2021-02-14 02:09:56 +00:00
|
|
|
default:
|
2021-02-16 00:24:04 +00:00
|
|
|
word = append(word, line[i])
|
2021-02-14 02:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-15 01:26:30 +00:00
|
|
|
|
2021-02-20 17:09:04 +00:00
|
|
|
// Exec wraps the branched execution of words (either built-in or user-defined)
|
2021-02-25 01:19:25 +00:00
|
|
|
func (c *Context) Exec(w Word, s string) error {
|
2021-02-15 01:26:30 +00:00
|
|
|
if w.Impl != nil {
|
|
|
|
// we have an implementation for that word. Run it.
|
2021-02-25 01:19:25 +00:00
|
|
|
err := w.Impl(s)
|
2021-02-15 01:26:30 +00:00
|
|
|
if err != nil {
|
2021-02-21 20:04:51 +00:00
|
|
|
return fmt.Errorf("error during built-in: %w", err)
|
2021-02-15 01:26:30 +00:00
|
|
|
}
|
|
|
|
} else if len(w.Source) != 0 {
|
|
|
|
// user-defined word; let's descend...
|
|
|
|
err := c.Eval(strings.Join(w.Source, " ") + " ")
|
|
|
|
if err != nil {
|
2021-02-21 20:04:51 +00:00
|
|
|
return fmt.Errorf("error during nested evaluation of word %+v: %w", w, err)
|
2021-02-15 01:26:30 +00:00
|
|
|
}
|
2021-02-25 02:27:52 +00:00
|
|
|
} else if w.Variable > 0 {
|
|
|
|
// it's a variable, let's get that address and put it on the stack
|
|
|
|
c.Stack.Push(w.Variable)
|
2021-02-21 00:18:45 +00:00
|
|
|
} else {
|
2021-02-21 00:22:36 +00:00
|
|
|
it, err := strconv.Atoi(w.Name)
|
|
|
|
if err == nil {
|
|
|
|
// it was a number! put it on the stack.
|
|
|
|
c.Stack.Push(it)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unable to parse word %s", w.Name)
|
|
|
|
}
|
2021-02-15 01:26:30 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|