package main

import (
	"fmt"
	"strings"
)

func opRead(scanner *scanProvider) value {
	s := "\n"
	for s == "\n" && scanner.Scan() {
		s = scanner.Text()
	}
	if s == "\n" {
		panic(fmt.Errorf("read: %s", errUnexpectedEndOfInput))
	}
	return &_unknown{s: `"` + s}
}

func opPrint(val1 value) value {
	if isList(val1) {
		list, space := strings.Split(makeList(toList(val1)), " "), false
		for i := 1; i < len(list)-1; i++ {
			if space && list[i] != "]" {
				fmt.Print(" ")
			}
			fmt.Print(escapeWord(list[i]))
			space = list[i] != "["
		}
		fmt.Println()
	} else {
		fmt.Println(escapeWord(val1.String()))
	}
	return val1
}

func opReadList(scanner *scanProvider) *_list {
	list, line := []string{}, false
	for scanner.Scan() {
		s := scanner.Text()
		if s == "\n" && line {
			break
		}
		line = true
		if s != "\n" {
			list = append(list, parseWord(`"`+s).Value())
		}
	}
	return &_list{list: list}
}
