From 18c80a0b8613b4d5bb15068512ae1aa764bd12c2 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 23 Sep 2018 08:35:58 +0200 Subject: ... --- go/forth/forth.go | 18 ++++++++---------- 1 file 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 } -- cgit v1.2.3