summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-09-23 08:35:58 +0200
committerDimitri Sokolyuk <demon@dim13.org>2018-09-23 08:35:58 +0200
commit18c80a0b8613b4d5bb15068512ae1aa764bd12c2 (patch)
treefdcf02b793ef71609ec5eedc0b7712d6a34bafab
parent77efe0fa4e9c6c485421cc7950c9853e30faab49 (diff)
...
-rw-r--r--go/forth/forth.go18
1 files changed, 8 insertions, 10 deletions
diff --git a/go/forth/forth.go b/go/forth/forth.go
index 9bfba42..3215f77 100644
--- a/go/forth/forth.go
+++ b/go/forth/forth.go
@@ -182,21 +182,18 @@ func compile(dict dictionary, l *lexer) ([]word, error) {
}
}
-type lexer struct {
- fields []string
- pos int
-}
+type lexer []string
-func NewLexer(line string) *lexer {
- return &lexer{fields: strings.Fields(line)}
+func NewLexer(line string) lexer {
+ return strings.Fields(line)
}
func (l *lexer) Next() (string, error) {
- if l.pos >= len(l.fields) {
+ if len(*l) == 0 {
return "", ErrEOL
}
- s := l.fields[l.pos]
- l.pos++
+ s := (*l)[0]
+ *l = (*l)[1:]
return s, nil
}
@@ -213,7 +210,8 @@ func Forth(v []string) ([]int, error) {
s := new(stack)
for _, line := range v {
// compile
- words, err := compile(dict, NewLexer(line))
+ l := NewLexer(line)
+ words, err := compile(dict, &l)
if err != nil {
return nil, err
}