parser location doesn't need to be in the context, does it?

This commit is contained in:
David 2021-02-14 14:30:15 -05:00
parent 852aaa6387
commit 626e90d54c

21
eval.go
View File

@ -11,7 +11,6 @@ type Context struct {
Dictionary Dictionary
Stack *Stack
RStack *Stack
I int // current parser location
}
// Eval evaulates a given line, recursively descending into given words as needed
@ -22,30 +21,30 @@ func (c *Context) Eval(line string) error {
var comment bool
immediate := true
for c.I = 0; c.I < len(line); c.I = c.I + 1 {
switch line[c.I] {
for i := 0; i < len(line); i = i + 1 {
switch line[i] {
case '(', ')': // comments
if len(word) == 0 {
if line[c.I] == '(' {
if line[i] == '(' {
comment = true
continue
}
comment = false
continue
} else {
word = append(word, line[c.I])
word = append(word, line[i])
}
case ':', ';': // COMPILE/IMMEDIATE mode swapping
if len(word) == 0 {
if line[c.I] == ':' {
if line[i] == ':' {
immediate = false
} else if line[c.I-1] == ' ' && line[c.I] == ';' {
} else if line[i-1] == ' ' && line[i] == ';' {
c.Dictionary.AddWord(words[0], nil, words[1:])
word = []byte{}
immediate = true
}
} else {
word = append(word, line[c.I])
word = append(word, line[i])
}
case ' ':
if !immediate { // continue building our subroutine if we're not in immediate mode...
@ -82,12 +81,12 @@ func (c *Context) Eval(line string) error {
word = []byte{}
} else if len(w.Source) != 0 {
// user-defined word; let's descend...
c.RStack.Push(c.I)
c.RStack.Push(i)
err := c.Eval(strings.Join(w.Source, " ") + " ")
if err != nil {
return err
}
c.I, err = c.RStack.Pop()
i, err = c.RStack.Pop()
if err != nil {
return fmt.Errorf("error while popping from return stack: %v", err)
}
@ -95,7 +94,7 @@ func (c *Context) Eval(line string) error {
}
default:
if !comment {
word = append(word, line[c.I])
word = append(word, line[i])
}
}
}