<commit_before>from lib import *
from classes import *
from sands import gen_stack
from util import cprint, bcolors
import time

def parse_for(cindex, commands, functions, variables, func_regex):
	loopedcommands = []
	command = commands[cindex]
	string = for_tag_regex.sub("", for_regex.match(command).group()).strip()[1:-2]
	arguments = string.split(",")

	spl = arguments[0].split(";")
	if len(spl) == 2:
		repl = spl[0]
		arguments[0] = spl[1]
	else:
		repl = ""

	if len(arguments) == 1:
		start, stop, step = 0.0, float(arguments[0]), 1.0
		if not stop % 1:
			start, stop, step = int(start), int(stop), int(step)
	elif len(arguments) == 2:
		start, stop, step = float(arguments[0]), float(arguments[1]), 1.0
		if not start % 1 and not stop % 1:
			start, stop, step = int(start), int(stop), int(step)
	else:
		start, stop, step = float(arguments[0]), float(arguments[1]), float(arguments[2])
		if not start % 1 and not stop % 1 and not step % 1:
			start, stop, step = int(start), int(stop), int(step)
			
	repeatcomms = []
	next_command = ""
	while cindex < len(commands)-1 and not endfor_regex.search(commands[cindex+1]):
		cindex += 1
		next_command = commands[cindex]
		if for_regex.match(next_command):
			cindex, out, functions, variables, func_regex = parse_for(cindex, commands, functions, variables, func_regex)
			repeatcomms += out
		elif next_command: 
			repeatcomms.append(next_command)

	cindex += 1
	if step:
		if step > 0:
			i, end, func = min(start, stop), max(start, stop), lessthan
		elif step < 0:
			i, end, func = max(start, stop), min(start, stop), greatthan
		while func(i, end):
			for cmd in repeatcomms:
				loopedcommands.append(re.sub(r"\|"+repl+r"\|", str(i), cmd, 0, re.IGNORECASE))
			i += step

	outcommands = []
	ccindex = 0
	while ccindex < len(loopedcommands):
		ccindex, out, functions, variables, func_regex = parse_cmd(ccindex, loopedcommands, functions, variables, func_regex)
		outcommands += out
		ccindex += 1
	return cindex, outcommands, functions, variables, func_regex

def parse_cmd(cindex, commands, functions, variables, func_regex):
	outcommands = []
	command = commands[cindex].strip()
	if not command or comment_regex.match(command): return cindex, [], functions, variables, func_regex

	for var in variables:
		command = variables[var].sub(command)
	while func_regex.search(command):
		for macro in functions:
			command = functions[macro].sub(command)

	if define_regex.match(command):
		command_split = define_tag_regex.sub("", command).split()
		if len(command_split) < 2: return cindex, [], functions, variables, func_regex

		name = command_split[0]

		contents = " ".join(command_split[1:])

		if macro_regex.match(name):
			params = param_regex.search(name).group()[1:-1].split(",")
			name = word_regex.search(name).group()
			functions[name] = CmdMacro(name, params, contents)
			func_regex = re.compile("\\$("+"|".join(map(lambda x: functions[x].name, functions))+")"+CmdMacro.param, re.IGNORECASE)
			return cindex, [], functions, variables, func_regex

		if word_regex.match(name):
			variables[name] = CmdVariable(name, contents)

	elif undefine_regex.match(command):
		command_split = undefine_regex.sub("", command).split()
		for i in command_split:
			if i in variables:
				del variables[i]
			if i in functions:
				del functions[i]

	elif import_regex.match(command):
		if context is None: return cindex, [], functions, variables, func_regex

		libraryname = import_regex.sub("", command).strip()
		if not libraryname: return cindex, [], functions, variables, func_regex

		if isinstance(context, str):
			if os.path.exists(os.path.join(context, libraryname)):
				importedcontext = context
				importedname = libraryname
				lib = open(os.path.join(context,libraryname))
			elif os.path.exists(os.path.join(context, libraryname+".1cc")):
				importedcontext = context
				importedname = libraryname+".1cc"
				lib = open(os.path.join(context, libraryname+".1cc"))
			else:
				cprint("Failed to import {lib}. File not found.", color=bcolors.RED, lib=libraryname)
				return []
		else:
			lib = None
			for i in context:
				if os.path.exists(os.path.join(i, libraryname)):
					importedcontext = i
					importedname = libraryname
					lib = open(os.path.join(i,libraryname))
					break
				elif os.path.exists(os.path.join(i, libraryname+".1cc")):
					importedcontext = i
					importedname = libraryname+".1cc"
					lib = open(os.path.join(i, libraryname+".1cc"))
					break
			if not lib:
				cprint("Failed to import {lib}. File not found.", color=bcolors.RED, lib=libraryname)
				return []

		outcommands += preprocess(lib.read().split("\n"), importedcontext, importedname)
	elif for_regex.match(command):
		cindex, out, functions, variables, func_regex = parse_for(cindex, commands, functions, variables, func_regex)
		outcommands += out
	else:
		outcommands.append(command)
	return cindex, outcommands, functions, variables, func_regex

def preprocess(commands, context = None, filename = None):
	currtime = time.localtime()
	variables = {
		"file": CmdVariable("file", str(filename)),
		"date": CmdVariable("date", format("{month}/{day}/{year}", # I'm american >:D
			month = currtime.tm_mon,
			day = currtime.tm_mday,
			year = currtime.tm_year
		)), 
		"time": CmdVariable("time", format("{hour}:{minute}:{second}",
			hour = currtime.tm_hour,
			minute = currtime.tm_min,
			second = currtime.tm_sec<commit_msg>fix some bugs with FOR loops<commit_after>from lib import *
from classes import *
from sands import gen_stack
from util import cprint, bcolors
import time

def parse_for(cindex, commands, functions, variables, func_regex):
	loopedcommands = []
	command = commands[cindex]
	string = for_tag_regex.sub("", for_regex.match(command).group()).strip()[1:-2]
	arguments = string.split(",")

	spl = arguments[0].split(";")
	if len(spl) == 2:
		repl = spl[0]
		arguments[0] = spl[1]
	else:
		repl = ""

	if len(arguments) == 1:
		start, stop, step = 0.0, float(arguments[0]), 1.0
		if not stop % 1:
			start, stop, step = int(start), int(stop), int(step)
	elif len(arguments) == 2:
		start, stop, step = float(arguments[0]), float(arguments[1]), 1.0
		if not start % 1 and not stop % 1:
			start, stop, step = int(start), int(stop), int(step)
	else:
		start, stop, step = float(arguments[0]), float(arguments[1]), float(arguments[2])
		if not start % 1 and not stop % 1 and not step % 1:
			start, stop, step = int(start), int(stop), int(step)
			
	repeatcomms = []
	next_command = ""
	while cindex < len(commands)-1 and not endfor_regex.search(commands[cindex+1]):
		cindex += 1
		next_command = commands[cindex]
		if for_regex.match(next_command):
			cindex, out, functions, variables, func_regex = parse_for(cindex, commands, functions, variables, func_regex)
			repeatcomms += out
		elif next_command: 
			repeatcomms.append(next_command)

	repl = re.compile(r"\|"+repl+r"\|", re.IGNORECASE)

	cindex += 1
	if step:
		if step > 0:
			i, end, func = min(start, stop), max(start, stop), lessthan
		elif step < 0:
			i, end, func = max(start, stop), min(start, stop), greatthan
		while func(i, end):
			for cmd in repeatcomms:
				loopedcommands.append(repl.sub(str(i), cmd))
			i += step
	
	return cindex, loopedcommands, functions, variables, func_regex

def endparse_for(commands, functions, variables, func_regex):
	outcommands = []
	ccindex = 0
	while ccindex < len(commands):
		ccindex, out, functions, variables, func_regex = parse_cmd(ccindex, commands, functions, variables, func_regex)
		outcommands += out
		ccindex += 1
	return outcommands

def parse_cmd(cindex, commands, functions, variables, func_regex):
	outcommands = []
	command = commands[cindex].strip()
	if not command or comment_regex.match(command): return cindex, [], functions, variables, func_regex

	for var in variables:
		command = variables[var].sub(command)
	while func_regex.search(command):
		for macro in functions:
			command = functions[macro].sub(command)

	if define_regex.match(command):
		command_split = define_tag_regex.sub("", command).split()
		if len(command_split) < 2: return cindex, [], functions, variables, func_regex

		name = command_split[0]

		contents = " ".join(command_split[1:])

		if macro_regex.match(name):
			params = param_regex.search(name).group()[1:-1].split(",")
			name = word_regex.search(name).group()
			functions[name] = CmdMacro(name, params, contents)
			func_regex = re.compile("\\$("+"|".join(map(lambda x: functions[x].name, functions))+")"+CmdMacro.param, re.IGNORECASE)
			return cindex, [], functions, variables, func_regex

		if word_regex.match(name):
			variables[name] = CmdVariable(name, contents)

	elif undefine_regex.match(command):
		command_split = undefine_regex.sub("", command).split()
		for i in command_split:
			if i in variables:
				del variables[i]
			if i in functions:
				del functions[i]

	elif import_regex.match(command):
		if context is None: return cindex, [], functions, variables, func_regex

		libraryname = import_regex.sub("", command).strip()
		if not libraryname: return cindex, [], functions, variables, func_regex

		if isinstance(context, str):
			if os.path.exists(os.path.join(context, libraryname)):
				importedcontext = context
				importedname = libraryname
				lib = open(os.path.join(context,libraryname))
			elif os.path.exists(os.path.join(context, libraryname+".1cc")):
				importedcontext = context
				importedname = libraryname+".1cc"
				lib = open(os.path.join(context, libraryname+".1cc"))
			else:
				cprint("Failed to import {lib}. File not found.", color=bcolors.RED, lib=libraryname)
				return []
		else:
			lib = None
			for i in context:
				if os.path.exists(os.path.join(i, libraryname)):
					importedcontext = i
					importedname = libraryname
					lib = open(os.path.join(i,libraryname))
					break
				elif os.path.exists(os.path.join(i, libraryname+".1cc")):
					importedcontext = i
					importedname = libraryname+".1cc"
					lib = open(os.path.join(i, libraryname+".1cc"))
					break
			if not lib:
				cprint("Failed to import {lib}. File not found.", color=bcolors.RED, lib=libraryname)
				return []

		outcommands += preprocess(lib.read().split("\n"), importedcontext, importedname)
	elif for_regex.match(command):
		cindex, out, functions, variables, func_regex = parse_for(cindex, commands, functions, variables, func_regex)
		outcommands += endparse_for(out, functions, variables, func_regex)
	else:
		outcommands.append(command)
	return cindex, outcommands, functions, variables, func_regex

def preprocess(commands, context = None, filename = None):
	currtime = time.localtime()
	variables = {
		"file": CmdVariable("file", str(filename)),
		"date": CmdVariable("date", format("{month}/{day}/{year}", # I'm american >:D
			month = currtime.tm_mon,
			day = currtime.tm_mday,
			year = currtime.tm_year
		)), 
		"time": CmdVariable("time", format("{hour}:{minute}:{second}",
			hour = currtime.tm_hour,
			minute = currtime.tm_min,
			second = currtime.tm_sec
{'fraction_non_alphanumeric': 0.14272685818513453, 'fraction_numerical': 0.008481532147742818, 'mean_word_length': 4.480259870064968, 'pattern_counts': {'":': 6, '<': 11, '<?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': '3642302', 'n_tokens_mistral': 4063, 'n_tokens_neox': 3631, 'n_words': 926}