<commit_before>from __future__ import print_function

from util import *
import nbtencoder as nbt

import math

cprintconf.name = "Generator"
cprintconf.color = bcolors.PEACH

class Command:
	def __init__(self, cmd, block="chain_command_block", init=False, conditional=False):
		self.cmd = cmd
		self.cond = conditional
		self.init = init
		self.block = block
	def __str__(self):
		return self.cmd
	def prettystr(self):
		return format("{cmd}{init}{cond}{repeat}",
			cmd = self.cmd,
			init = "\n  - Initialization" if self.init else "",
			cond = "\n  - Conditional" if self.cond else "",
			repeat="\n  - Repeating" if self.block == "repeating_command_block" else "")

class FakeCommand:
	hasdata_regex = re.compile(r":\d{1,2}$")
	def __init__(self, blockname, init):
		self.cond = False
		self.init = init
		if self.hasdata_regex.search(blockname):
			datastr = self.hasdata_regex.findall(blockname)
			self.data = int(datastr[0][1:])
		else: self.data = 0
		self.block = self.hasdata_regex.sub("", blockname)
	def __str__(self):
		return format("{block} {data}",
			block=self.block,
			data=self.data)
	def prettystr(self):
		return format("{darkgray}{block}{init}",
			block = self,
			init = "\n  - Initialization" if self.init else "")


class CmdVariable:
	def __init__(self, name, replacewith):
		self.name = name
		self.replacewith = replacewith
		self.regex = re.compile(r"\|\$"+name.lower()+r"\||\$"+name.lower()+r"\b", re.IGNORECASE)
	def sub(self, string):
		return self.regex.sub(self.replacewith, string)

def macrofunc(string, params, args):
	for i in range(min(len(args), len(params))):
		string = re.sub(r"\|" + params[i] + r"\|", args[i], string)
	return string

class CmdMacro:
	param = r"\((?:(?:(?:-?\d+(?:\.\d+)?|\"(?:[^\"\\]*(?:\\.)*)*\"),\s*)*(?:-?\d+(?:\.\d+)?|\"(?:[^\"\\]*(?:\\.)*)*\"))?\)"
	param_regex = re.compile(param)
	def __init__(self, name, params, replacewith, function=macrofunc):
		self.name = name
		self.replacewith = replacewith
		self.params = params
		self.regex = re.compile(r"\$"+name.lower()+self.param, re.IGNORECASE)
		self.function = function
	def sub(self, string):
		while self.regex.search(string):
			found = self.regex.finditer(string)
			for find in found:
				params = self.param_regex.search(find.group()).group()[1:-1]
				params = re.sub(r",\s", ",", params)
				paraml = params.split(",")
				parsedparams = []
				for i in paraml:
					if i:
						if i[0] == '"':
							i = i[1:-1].replace('\\"', '"').replace('\\\\', '\\')
						parsedparams.append(i)
				try:
					output = self.function(self.replacewith, self.params, parsedparams)
				except:
					cprint("{params} is not a valid argument list for ${funcname}.", color=bcolors.RED, params=params, funcname=self.name)
					output = ""
				string = string.replace(find.group(), output)
		return string

sin = lambda string, params, args: repr(math.sin(math.radians(float(args[0]))))
cos = lambda string, params, args: repr(math.sin(math.radians(float(args[0]))))
tan = lambda string, params, args: repr(math.sin(math.radians(float(args[0]))))
sinr= lambda string, params, args: repr(math.sin(float(args[0])))
cosr= lambda string, params, args: repr(math.sin(float(args[0])))
tanr= lambda string, params, args: repr(math.sin(float(args[0])))
floor=lambda string, params, args: repr(int(math.floor(float(args[0]))))
ceil= lambda string, params, args: repr(int(math.ceil(float(args[0]))))
rnd_l=lambda string, params, args: repr(round(float(args[0]), int(args[1])))
add = lambda string, params, args: repr(float(args[0]) + float(args[1]))
sub = lambda string, params, args: repr(float(args[0]) - float(args[1]))
mul = lambda string, params, args: repr(float(args[0]) * float(args[1]))
div = lambda string, params, args: repr(float(args[0]) / float(args[1]))
pow_l=lambda string, params, args: repr(float(args[0]) **float(args[1]))


def generate_sand(command_obj, direction):
	if isinstance(command_obj, FakeCommand):
		return normal_sand(command_obj.block, command_obj.data)
	tag = {
		"Block": nbt.noquote_str(command_obj.block),
		"TileEntityData": {
			"Command": str(command_obj),
			"TrackOutput": nbt.int_b(0)
		},
		"Time": 1,
		"id": nbt.noquote_str("FallingSand")
	}
	data = direction+8 if command_obj.cond else direction
	if data:
		tag["Data"] = data
	return tag


def normal_sand(block, data=0):
	tag = {
		"Block": nbt.noquote_str(block),
		"Time": 1,
		"id": nbt.noquote_str("FallingSand")
	}
	if data:
		tag["Data"] = data
	return tag

def gen_stack(init_commands, clock_commands, mode, loud=False):
	final_command_obj = None
	if clock_commands or init_commands:
		command_sands = []

		repeatoffsets = []
		if mode == 'i':
			if clock_commands and isinstance(clock_commands[0], Command): 
				repeatoffsets.append(len(clock_commands) + 2)
			for command in clock_commands:
				if command.block == "repeating_command_block" and not command.cond and command is not clock_commands[0]:
					repeatoffsets.append(len(clock_commands) - clock_commands.index(command) + 2 + len(repeatoffsets))

		filloffset = len(init_commands) + len(repeatoffsets)
		if filloffset: filloffset += 1

		if filloffset:
			sand = normal_sand("command_block")
			if mode == 'i':
				sand["TileEntityData"] = {
					"auto": 1
				}
			command_sands.append(sand)

		for command in init_commands:
			if loud:
				cprint(command.prettystr(), allow_repeat=True)
			command_sands.append(generate_sand(command, 0))

		for offset in repeatoffsets[::-1]:
			blockdata = Command(format("blockdata ~ ~-{offset} ~ {auto:1b}", offset = offset), init=True)
			if loud:
				cprint(blockdata.prettystr(), allow_repeat=True)
			sand = generate_sand(blockdata, 0)
			command_sands.append(sand)

		if filloffset:
			fill = Command(format("fill ~ ~-1 ~ ~ ~{offset} ~ air", offset = filloffset), init=True)
			if loud:
				cprint(fill.prettystr(), allow_repeat=True)
			cprint("minecraft:barrier\n  - Initialization", color=bcolors.DARKGRAY, allow_repeat=True)
			command_sands.append(generate_sand(fill, 0))
			command_sands.append(normal_sand("barrier"))

		for command in clock_commands[::-1]:
			if command is clock_commands[0] and isinstance(command, Command):
				command.block = "repeating_command_block"
				command_sands.append(generate_sand(command, 1))
			else:
				sand = generate_sand(command, 1)
				if command.block == "repeating_command_block" and command.cond: 
					sand["TileEntityData"]["auto"] = 1
				command_sands.append(sand)
			if loud:
				cprint(command.prettystr(), allow_repeat=True)
		final_command_obj = nbt.cmd("summon FallingSand ~ ~1 ~ ", ride(command_sands, False))

	final_command = nbt.JSON2Command(final_command_obj)

	return final_command

tag_regex =         re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:)[\t]*)+", re.IGNORECASE)
init_tag_regex =    re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*INIT:", re.IGNORECASE)
cond_tag_regex =    re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*COND:", re.IGNORECASE)
repeat_tag_regex =  re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*REPEAT:", re.IGNORECASE)
block_tag_regex =   re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*BLOCK:[ \t]*(minecraft:)?[a-z_](:\d{1,2})?", re.IGNORECASE)
block_regex =       re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*BLOCK:[ \t]*", re.IGNORECASE)
define_regex =      re.compile(r"^[ \t]*DEFINE:", re.IGNORECASE)
word_regex =        re.compile(r"[a-zA-Z0-9_]+") # this regex has had me laughing for a while, but i need it
param_regex =       re.compile(r"\(([a-zA-Z0-9_]+,)*[a-zA-Z0-9_]+\)")
macro_regex =       re.compile(r"[a-zA-Z0-9_]+\(([a-zA-Z0-9_]+,)*[a-zA-Z0-9_]+\)")
undefine_regex =    re.compile(r"^[ \t]*UNDEFINE:", re.IGNORECASE)
import_regex =      re.compile(r"^[ \t]*IMPORT:", re.IGNORECASE)
for_regex =         re.compile(r"^[ \t]*FOR: \(\d+\.\.\.\d+\)", re.IGNORECASE)
endfor_regex =      re.compile(r":ENDFOR[ \t]*$", re.IGNORECASE)
<commit_msg>Squashed 'OneCommand/' changes from b8f22c2..e949c05<commit_after>from __future__ import print_function

from util import *
import nbtencoder as nbt

import math

cprintconf.name = "Generator"
cprintconf.color = bcolors.PEACH

class Command:
	def __init__(self, cmd, block="chain_command_block", init=False, conditional=False):
		self.cmd = cmd
		self.cond = conditional
		self.init = init
		self.block = block
	def __str__(self):
		return self.cmd
	def prettystr(self):
		return format("{cmd}{init}{cond}{repeat}",
			cmd = self.cmd,
			init = "\n  - Initialization" if self.init else "",
			cond = "\n  - Conditional" if self.cond else "",
			repeat="\n  - Repeating" if self.block == "repeating_command_block" else "")

class FakeCommand:
	hasdata_regex = re.compile(r":\d{1,2}$")
	def __init__(self, blockname, init):
		self.cond = False
		self.init = init
		if self.hasdata_regex.search(blockname):
			datastr = self.hasdata_regex.findall(blockname)
			self.data = int(datastr[0][1:])
		else: self.data = 0
		self.block = self.hasdata_regex.sub("", blockname)
	def __str__(self):
		return format("{block} {data}",
			block=self.block,
			data=self.data)
	def prettystr(self):
		return format("{darkgray}{block}{init}",
			block = self,
			init = "\n  - Initialization" if self.init else "")


class CmdVariable:
	def __init__(self, name, replacewith):
		self.name = name
		self.replacewith = replacewith
		self.regex = re.compile(r"\|\$"+name.lower()+r"\||\$"+name.lower()+r"\b", re.IGNORECASE)
	def sub(self, string):
		return self.regex.sub(self.replacewith, string)

def macrofunc(string, params, args):
	for i in range(min(len(args), len(params))):
		string = re.sub(r"\|" + params[i] + r"\|", args[i], string)
	return string

class CmdMacro:
	param = r"\((?:(?:(?:-?\d+(?:\.\d+)?|\"(?:[^\"\\]*(?:\\.)*)*\"),\s*)*(?:-?\d+(?:\.\d+)?|\"(?:[^\"\\]*(?:\\.)*)*\"))?\)"
	param_regex = re.compile(param)
	def __init__(self, name, params, replacewith, function=macrofunc):
		self.name = name
		self.replacewith = replacewith
		self.params = params
		self.regex = re.compile(r"\$"+name.lower()+self.param, re.IGNORECASE)
		self.function = function
	def sub(self, string):
		while self.regex.search(string):
			found = self.regex.finditer(string)
			for find in found:
				params = self.param_regex.search(find.group()).group()[1:-1]
				params = re.sub(r",\s", ",", params)
				paraml = params.split(",")
				parsedparams = []
				for i in paraml:
					if i:
						if i[0] == '"':
							i = i[1:-1].replace('\\"', '"').replace('\\\\', '\\')
						parsedparams.append(i)
				try:
					output = self.function(self.replacewith, self.params, parsedparams)
				except:
					cprint("{params} is not a valid argument list for ${funcname}.", color=bcolors.RED, params=params, funcname=self.name)
					output = ""
				string = string.replace(find.group(), output)
		return string

sin = lambda string, params, args: repr(math.sin(math.radians(float(args[0]))))
cos = lambda string, params, args: repr(math.sin(math.radians(float(args[0]))))
tan = lambda string, params, args: repr(math.sin(math.radians(float(args[0]))))
sinr= lambda string, params, args: repr(math.sin(float(args[0])))
cosr= lambda string, params, args: repr(math.sin(float(args[0])))
tanr= lambda string, params, args: repr(math.sin(float(args[0])))
floor=lambda string, params, args: repr(int(math.floor(float(args[0]))))
ceil= lambda string, params, args: repr(int(math.ceil(float(args[0]))))
rnd_l=lambda string, params, args: repr(round(float(args[0]), int(args[1])))
add = lambda string, params, args: repr(float(args[0]) + float(args[1]))
sub = lambda string, params, args: repr(float(args[0]) - float(args[1]))
mul = lambda string, params, args: repr(float(args[0]) * float(args[1]))
div = lambda string, params, args: repr(float(args[0]) / float(args[1]))
pow_l=lambda string, params, args: repr(float(args[0]) **float(args[1]))


def generate_sand(command_obj, direction):
	if isinstance(command_obj, FakeCommand):
		return normal_sand(command_obj.block, command_obj.data)
	tag = {
		"Block": nbt.noquote_str(command_obj.block),
		"TileEntityData": {
			"Command": str(command_obj),
			"TrackOutput": nbt.int_b(0)
		},
		"Time": 1,
		"id": nbt.noquote_str("FallingSand")
	}
	data = direction+8 if command_obj.cond else direction
	if data:
		tag["Data"] = data
	return tag


def normal_sand(block, data=0):
	tag = {
		"Block": nbt.noquote_str(block),
		"Time": 1,
		"id": nbt.noquote_str("FallingSand")
	}
	if data:
		tag["Data"] = data
	return tag

def gen_stack(init_commands, clock_commands, mode, loud=False):
	final_command_obj = None
	if clock_commands or init_commands:
		command_sands = []

		repeatoffsets = []
		if mode == 'i':
			if clock_commands and isinstance(clock_commands[0], Command): 
				repeatoffsets.append(len(clock_commands) + 2)
			for command in clock_commands:
				if command.block == "repeating_command_block" and not command.cond and command is not clock_commands[0]:
					repeatoffsets.append(len(clock_commands) - clock_commands.index(command) + 2 + len(repeatoffsets))

		filloffset = len(init_commands) + len(repeatoffsets)
		if filloffset: filloffset += 1

		if filloffset:
			sand = normal_sand("command_block")
			if mode == 'i':
				sand["TileEntityData"] = {
					"auto": 1
				}
			command_sands.append(sand)

		for command in init_commands:
			if loud:
				cprint(command.prettystr(), allow_repeat=True)
			command_sands.append(generate_sand(command, 0))

		for offset in repeatoffsets[::-1]:
			blockdata = Command(format("blockdata ~ ~-{offset} ~ {auto:1b}", offset = offset), init=True)
			if loud:
				cprint(blockdata.prettystr(), allow_repeat=True)
			sand = generate_sand(blockdata, 0)
			command_sands.append(sand)

		if filloffset:
			fill = Command(format("fill ~ ~-1 ~ ~ ~{offset} ~ air", offset = filloffset), init=True)
			if loud:
				cprint(fill.prettystr(), allow_repeat=True)
			cprint("minecraft:barrier\n  - Initialization", color=bcolors.DARKGRAY, allow_repeat=True)
			command_sands.append(generate_sand(fill, 0))
			command_sands.append(normal_sand("barrier"))

		for command in clock_commands[::-1]:
			if command is clock_commands[0] and isinstance(command, Command):
				command.block = "repeating_command_block"
				command_sands.append(generate_sand(command, 1))
			else:
				sand = generate_sand(command, 1)
				if command.block == "repeating_command_block" and command.cond: 
					sand["TileEntityData"]["auto"] = 1
				command_sands.append(sand)
			if loud:
				cprint(command.prettystr(), allow_repeat=True)
		final_command_obj = nbt.cmd("summon FallingSand ~ ~1 ~ ", ride(command_sands, False))

	final_command = nbt.JSON2Command(final_command_obj)

	return final_command

tag_regex =         re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:)[ \t]*)+", re.IGNORECASE)
init_tag_regex =    re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*INIT:", re.IGNORECASE)
cond_tag_regex =    re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*COND:", re.IGNORECASE)
repeat_tag_regex =  re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*REPEAT:", re.IGNORECASE)
block_tag_regex =   re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*BLOCK:[ \t]*(minecraft:)?[a-z_](:\d{1,2})?", re.IGNORECASE)
block_regex =       re.compile(r"^[ \t]*((INIT:|COND:|REPEAT:|BLOCK:)[ \t]*)*BLOCK:[ \t]*", re.IGNORECASE)
define_regex =      re.compile(r"^[ \t]*DEFINE:", re.IGNORECASE)
word_regex =        re.compile(r"[a-zA-Z0-9_]+") # this regex has had me laughing for a while, but i need it
param_regex =       re.compile(r"\(([a-zA-Z0-9_]+,)*[a-zA-Z0-9_]+\)")
macro_regex =       re.compile(r"[a-zA-Z0-9_]+\(([a-zA-Z0-9_]+,)*[a-zA-Z0-9_]+\)")
undefine_regex =    re.compile(r"^[ \t]*UNDEFINE:", re.IGNORECASE)
import_regex =      re.compile(r"^[ \t]*IMPORT:", re.IGNORECASE)
for_regex =         re.compile(r"^[ \t]*FOR: \(\d+\.\.\.\d+\)", re.IGNORECASE)
endfor_regex =      re.compile(r":ENDFOR[ \t]*$", re.IGNORECASE)

{'fraction_non_alphanumeric': 0.18667255075022066, 'fraction_numerical': 0.009141344092800404, 'mean_word_length': 4.552327616380819, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, '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': '1720967', 'n_tokens_mistral': 6474, 'n_tokens_neox': 5852, 'n_words': 1157}