<commit_before>package repl

import (
	"fmt"
	"github.com/GeertJohan/go.linenoise"
	"github.com/vifino/golua/lua"
	"github.com/vifino/luar"
	"strings"
)

type Dummy struct {
	Name string
}

const (
	LUA_PROMPT1 = "> "
	LUA_PROMPT2 = ">> "
	BANNER      = `
  ⡎⠑ | Carbon
  ⠣⠔ | (c) 2015 vifino
`
)

// modified https://github.com/vifino/luar/blob/master/examples/luar.go

func Run(L *lua.State) {
	luar.Register(L, "", luar.Map{
		"__DUMMY__": &Dummy{"me"},
	})

	err := L.DoString(lua_code)
	if err != nil {
		fmt.Println("initial " + err.Error())
		return
	}
	// particularly the completion logic
	complete := luar.NewLuaObjectFromName(L, "carbon._lua_candidates")
	// this function returns a string slice of candidates
	str_slice := luar.Types([]string{})

	linenoise.SetCompletionHandler(func(in string) []string {
		val, err := complete.Callf(str_slice, in)
		if err != nil || len(val) == 1 && val[0] == nil {
			return []string{}
		} else {
			return val[0].([]string)
		}
	})

	fmt.Println(BANNER)
	L.DoString(`print(jit.version)`)
	fmt.Println("")

	prompt := LUA_PROMPT1
	code := ""

	for {
		// ctrl-C/ctrl-D handling with latest update of go.linenoise
		str, err := linenoise.Line(prompt)
		if err != nil {
			return
		}
		if len(str) > 0 {
			linenoise.AddHistory(str)
			if str[0] == '=' || str[0] == '.' {
				exprs := str[1:]
				if str[0] == '=' {
					str = "print(prettify(" + exprs + "))"
				} else {
					str = "print(" + exprs + ")"
				}
			}
			continuing := false
			code = code + str
			//fmt.Println("'"+code+"'")
			err := L.DoString(code)
			if err != nil {
				errs := err.Error()
				// fmt.Println("err",errs)
				// strip line nonsense if error occurred on prompt
				idx := strings.Index(errs, ": ")
				if idx > -1 && strings.HasPrefix(errs, "[string ") {
					errs = errs[idx+2:]
				}
				// need to keep collecting line?
				if strings.HasSuffix(errs, "near '<eof>'") {
					continuing = true
				} else {
					fmt.Println("Error: " + errs)
				}
			}
			if continuing { // prompt must reflect continuing state
				prompt = LUA_PROMPT2
				code = code + "\n"
			} else {
				prompt = LUA_PROMPT1
				code = ""
			}
		}
	}
}

// pretty-printing and code completion logic in Lua
const lua_code = `
local prettify = require("serialize").simple

local tostring = tostring
local append = table.insert
local function quote (v)
	local t = type(v)
	if t == 'string' then
		return ('%q'):format(v)
	elseif t == 'function' then
		return '<fun>'
	elseif t == 'userdata' then
		return '<udata>'
	else
		return tostring(v)
	end
end
local dump
dump = function(t, options)<commit_msg>Woops.<commit_after>package repl

import (
	"fmt"
	"github.com/GeertJohan/go.linenoise"
	"github.com/vifino/golua/lua"
	"github.com/vifino/luar"
	"strings"
)

type Dummy struct {
	Name string
}

const (
	LUA_PROMPT1 = "> "
	LUA_PROMPT2 = ">> "
	BANNER      = `
  ⡎⠑ | Carbon
  ⠣⠔ | (c) 2015 vifino
`
)

// modified https://github.com/vifino/luar/blob/master/examples/luar.go

func Run(L *lua.State) {
	luar.Register(L, "", luar.Map{
		"__DUMMY__": &Dummy{"me"},
	})

	err := L.DoString(lua_code)
	if err != nil {
		fmt.Println("initial " + err.Error())
		return
	}
	// particularly the completion logic
	complete := luar.NewLuaObjectFromName(L, "carbon._lua_candidates")
	// this function returns a string slice of candidates
	str_slice := luar.Types([]string{})

	linenoise.SetCompletionHandler(func(in string) []string {
		val, err := complete.Callf(str_slice, in)
		if err != nil || len(val) == 1 && val[0] == nil {
			return []string{}
		} else {
			return val[0].([]string)
		}
	})

	fmt.Println(BANNER)
	L.DoString(`print(jit.version)`)
	fmt.Println("")

	prompt := LUA_PROMPT1
	code := ""

	for {
		// ctrl-C/ctrl-D handling with latest update of go.linenoise
		str, err := linenoise.Line(prompt)
		if err != nil {
			return
		}
		if len(str) > 0 {
			linenoise.AddHistory(str)
			if str[0] == '=' || str[0] == '.' {
				exprs := str[1:]
				if str[0] == '=' {
					str = "print(serialize.simple(" + exprs + "))"
				} else {
					str = "print(" + exprs + ")"
				}
			}
			continuing := false
			code = code + str
			//fmt.Println("'"+code+"'")
			err := L.DoString(code)
			if err != nil {
				errs := err.Error()
				// fmt.Println("err",errs)
				// strip line nonsense if error occurred on prompt
				idx := strings.Index(errs, ": ")
				if idx > -1 && strings.HasPrefix(errs, "[string ") {
					errs = errs[idx+2:]
				}
				// need to keep collecting line?
				if strings.HasSuffix(errs, "near '<eof>'") {
					continuing = true
				} else {
					fmt.Println("Error: " + errs)
				}
			}
			if continuing { // prompt must reflect continuing state
				prompt = LUA_PROMPT2
				code = code + "\n"
			} else {
				prompt = LUA_PROMPT1
				code = ""
			}
		}
	}
}

// pretty-printing and code completion logic in Lua
const lua_code = `
serialize = require("serialize")

local tostring = tostring
local append = table.insert
local function quote (v)
	local t = type(v)
	if t == 'string' then
		return ('%q'):format(v)
	elseif t == 'function' then
		return '<fun>'
	elseif t == 'userdata' then
		return '<udata>'
	else
		return tostring(v)
	end
end
local dump
dump = function(t, options)
{'fraction_non_alphanumeric': 0.16823687752355315, 'fraction_numerical': 0.007306287252451452, 'mean_word_length': 2.920120572720422, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '6303344', 'n_tokens_mistral': 2291, 'n_tokens_neox': 1987, 'n_words': 456}