<commit_before>package repl

import (
	"io"
	"monkey/lexer"
	"monkey/parser"
	"os"
	"path/filepath"

	"github.com/peterh/liner"
)

const PROMPT = ">> "

func Start(out io.Writer) {
	history := filepath.Join(os.TempDir(), ".monkey_history")
	l := liner.NewLiner()
	defer l.Close()

	l.SetCtrlCAborts(true)

	if f, err := os.Open(history); err == nil {
		l.ReadHistory(f)
		f.Close()
	}

	// env := object.NewEnvironment()
	for {
		if line, err := l.Prompt(PROMPT); err == nil {
			if line == "exit" {
				if f, err := os.Create(history); err == nil {
					l.WriteHistory(f)
					f.Close()
				}
				break
			}
			lex := lexer.New(line)
			p := parser.New(lex)

			program := p.ParseProgram()
			if len(p.Errors()) != 0 {
				printParserErrors(out, p.Errors())
				continue
			}
			// e := eval.Eval(program, env)
			io.WriteString(out, program.String())
			io.WriteString(out, "\n")
			l.AppendHistory(line)
		}
	}
}

func printParserErrors(out io.Writer, errors []string) {
	for _, msg := range errors {
		io.WriteString(out, "\t"+msg+"\n")
	}
}<commit_msg>restored eval in repl and changed history behavior<commit_after>package repl

import (
	"io"
	"monkey/eval"
	"monkey/lexer"
	"monkey/object"
	"monkey/parser"
	"os"
	"path/filepath"

	"github.com/peterh/liner"
)

const PROMPT = ">> "

func Start(out io.Writer) {
	history := filepath.Join(os.TempDir(), ".monkey_history")
	l := liner.NewLiner()
	defer l.Close()

	l.SetCtrlCAborts(true)

	if f, err := os.Open(history); err == nil {
		l.ReadHistory(f)
		f.Close()
	}

	env := object.NewEnvironment()
	for {
		if line, err := l.Prompt(PROMPT); err == nil {
			if line == "exit" {
				if f, err := os.Create(history); err == nil {
					l.WriteHistory(f)
					f.Close()
				}
				break
			}
			l.AppendHistory(line)
			lex := lexer.New(line)
			p := parser.New(lex)
			program := p.ParseProgram()
			if len(p.Errors()) != 0 {
				printParserErrors(out, p.Errors())
				continue
			}
			e := eval.Eval(program, env)
			io.WriteString(out, e.Inspect())
			io.WriteString(out, "\n")
		}
	}
}

func printParserErrors(out io.Writer, errors []string) {
	for _, msg := range errors {
		io.WriteString(out, "\t"+msg+"\n")
	}
}
{'fraction_non_alphanumeric': 0.18148318747121142, 'fraction_numerical': 0.0009212344541685859, 'mean_word_length': 3.0903954802259888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1942986', 'n_tokens_mistral': 973, 'n_tokens_neox': 852, 'n_words': 173}