func (p *parser) for_() Statement {
	pos := p.pos
	p.expect(FOR)
	name := p.val
	p.expect(NAME)
	p.expect(IN)
	iterable := p.expression()
	body := p.block()
	return &For{pos, name, iterable, body}
}
for = FOR NAME IN expression block
package parser
import (
	"fmt"
	"strconv"
	. "github.com/benhoyt/littlelang/tokenizer"
)
type Error struct {
	Position Position
	Message  string
}
func (e Error) Error() string {
	return fmt.Sprintf("parse error at %d:%d: %s", e.Position.Line, e.Position.Column, e.Message)
}
type parser struct {
	tokenizer *Tokenizer
	pos       Position
	tok       Token
	val       string
}
func (p *parser) next() {
	p.pos, p.tok, p.val = p.tokenizer.Next()
	if p.tok == ILLEGAL {
		p.error("%s", p.val)
	}
}
func (p *parser) error(format string, args ...interface{}) {
	message := fmt.Sprintf(format, args...)
	panic(Error{p.pos, message})
}
func (p *parser) expect(tok Token) {
	if p.tok != tok {
		p.error("expected %s and not %s", tok, p.tok)
	}
	p.next()
}
func (p *parser) matches(operators ...Token) bool {
	for _, operator := range operators {
		if p.tok == operator {
			return true
		}
	}
	return false
}
func (p *parser) program() *Program {
	statements := p.statements(EOF)
	return &Program{statements}
}
func (p *parser) statements(end Token) Block {
	statements := Block{}
	for p.tok != end && p.tok != EOF {
		statements = append(statements, p.statement())
	}
	return statements
}
func (p *parser) statement() Statement {
	switch p.tok {
	case IF:
		return p.if_()
	case WHILE:
		return p.while()
	case FOR:
		return p.for_()
	case RETURN:
		return p.return_()
	case FUNC:
		return p.func_()
	}
	pos := p.pos
	expr := p.expression()
	if p.tok == ASSIGN {
		pos = p.pos
		switch expr.(type) {
		case *Variable, *Subscript:
			p.next()
			value := p.expression()
			return &Assign{pos, expr, value}
		default:
			p.error("expected name, subscript, or dot expression on left side of =")
		}
	}
	return &ExpressionStatement{pos, expr}
}
func (p *parser) block() Block {
	p.expect(LBRACE)
	body := p.statements(RBRACE)
	p.expect(RBRACE)
	return body
}
func (p *parser) if_() Statement {
	pos := p.pos
	p.expect(IF)
	condition := p.expression()
	body := p.block()
	var elseBody Block
	if p.tok == ELSE {
		p.next()
		if p.tok == LBRACE {
			elseBody = p.block()
		} else if p.tok == IF {
			elseBody = Block{p.if_()}
		} else {
			p.error("expected { or if after else, not %s", p.tok)
		}
	}
	return &If{pos, condition, body, elseBody}
}
func (p *parser) while() Statement {
	pos := p.pos
	p.expect(WHILE)
	condition := p.expression()
	body := p.block()
	return &While{pos, condition, body}
}