package main

import (
	"bufio"
	"fmt"
	"strings"
)

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

func opPrint(val1 value) value {
	s := escapeValue(val1.val)
	if s != "" {
		if s[0] == '"' {
			fmt.Println(s[1:])
		} else if s[0] == '[' {
			list, space := strings.Split(s, " "), false
			for i := 0; i < len(list); i++ {
				if space && list[i] != "]" {
					fmt.Print(" ")
				}
				fmt.Print(list[i])
				space = list[i] != "["
			}
			fmt.Println()
		} else {
			fmt.Println(s)
		}
	}
	return val1
}

func opReadList(scanner *bufio.Scanner) value {
	list, line := []string{}, false
	for scanner.Scan() {
		s := scanner.Text()
		if s == "\n" && line {
			break
		}
		line = true
		// todo
		if s != "\n" {
			list = append(list, s)
		}
	}
	return value{val: "[ " + strings.Join(list, " ") + " ]"}
}
