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 value{word: `"` + s}
}

func opPrint(val1 value) value {
	if val1.tp == typeNumber {
		fmt.Println(val1.num)
	} else if val1.tp == typeBool {
		fmt.Println(val1.b)
	} else if val1.tp == typeWord {
		fmt.Println(escapeWord(val1.word[1:]))
	} else {
		list, space := strings.Split(makeList(val1.list), " "), false
		for i := 0; i < len(list); i++ {
			if space && list[i] != "]" {
				fmt.Print(" ")
			}
			fmt.Print(escapeWord(list[i]))
			space = list[i] != "["
		}
		fmt.Println()
	}
	return val1
}

func opReadList(scanner *scanProvider) value {
	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).word[1:])
		}
	}
	return value{tp: typeList, list: list}
}
