<commit_before>package main

import (
	"fmt"
	"os"

	"../dos"
	"../interpreter"
	"../lua"
)

type LuaFunction struct {
	L            lua.Lua
	registoryKey string
}

var LuaInstanceToCmd = map[uintptr]*interpreter.Interpreter{}

func (this LuaFunction) String() string {
	return "<<Lua-function>>"
}

func (this LuaFunction) Call(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
	this.L.GetField(lua.LUA_REGISTRYINDEX, this.registoryKey)
	this.L.NewTable()
	for i, arg1 := range cmd.Args {
		this.L.PushString(arg1)
		this.L.RawSetI(-2, lua.Integer(i))
	}
	save := LuaInstanceToCmd[this.L.State()]
	LuaInstanceToCmd[this.L.State()] = cmd
	err := this.L.Call(1, 0)
	LuaInstanceToCmd[this.L.State()] = save
	return interpreter.CONTINUE, err
}

const original_io_lines = "original_io_lines"

func ioLines(this lua.Lua) int {
	if this.IsString(1) {
		// io.lines("FILENAME") --> use original io.lines
		this.GetField(lua.LUA_REGISTRYINDEX, original_io_lines)
		this.PushValue(1)
		this.Call(1, 1)
	} else {
		// io.lines() --> use nyagos version<commit_msg>Let evalute lua-alias return-value as a new commandline<commit_after>package main

import (
	"fmt"
	"os"

	"../dos"
	"../interpreter"
	"../lua"
)

type LuaFunction struct {
	L            lua.Lua
	registoryKey string
}

var LuaInstanceToCmd = map[uintptr]*interpreter.Interpreter{}

func (this LuaFunction) String() string {
	return "<<Lua-function>>"
}

func (this LuaFunction) Call(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
	L := this.L
	L.GetField(lua.LUA_REGISTRYINDEX, this.registoryKey)
	L.NewTable()
	for i, arg1 := range cmd.Args {
		L.PushString(arg1)
		L.RawSetI(-2, lua.Integer(i))
	}
	save := LuaInstanceToCmd[L.State()]
	LuaInstanceToCmd[L.State()] = cmd
	err := L.Call(1, 1)
	if err == nil {
		newargs := make([]string, 0)
		if L.IsTable(-1) {
			L.PushInteger(0)
			L.GetTable(-2)
			if val, err1 := L.ToString(-1); val != "" && err1 == nil {
				newargs = append(newargs, val)
			}
			L.Pop(1)
			for i := 1; ; i++ {
				L.PushInteger(lua.Integer(i))
				L.GetTable(-2)
				if L.IsNil(-1) {
					L.Pop(1)
					break
				}
				val, err1 := L.ToString(-1)
				L.Pop(1)
				if err1 != nil {
					break
				}
				newargs = append(newargs, val)
			}
			it := cmd.Clone()
			it.Args = newargs
			it.Spawnvp()
		} else if val, err1 := L.ToString(-1); val != "" && err1 == nil {
			cmd.Interpret(val)
		}
	}
	L.Pop(1)
	LuaInstanceToCmd[this.L.State()] = save
	return interpreter.CONTINUE, err
}

const original_io_lines = "original_io_lines"

func ioLines(this lua.Lua) int {
	if this.IsString(1) {
		// io.lines("FILENAME") --> use original io.lines
		this.GetField(lua.LUA_REGISTRYINDEX, original_io_lines)
		this.PushValue(1)
		this.Call(1, 1)
	} else {
		// io.lines() --> use nyagos version
{'fraction_non_alphanumeric': 0.16325062927004674, 'fraction_numerical': 0.013664149586479683, 'mean_word_length': 3.36734693877551, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6024747', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1129, 'n_words': 221}