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