<commit_before>---------------------------------------------------------------------
-- SF Compiler. Compiles code into an uninitialized Instance.
-- @author <NAME>
---------------------------------------------------------------------

SF.Compiler = {}

--- Preprocesses and Compiles code and returns an Instance
-- @param code Either a string of code, or a {path=source} table
-- @param context The context to use in the resulting Instance
-- @param mainfile If code is a table, this specifies the first file to parse.
-- @param player The "owner" of the instance
-- @param data The table to set instance.data to. Default is a new table.
-- @param dontpreprocess Set to true to skip preprocessing
-- @return True if no errors, false if errors occured.
-- @return The compiled instance, or the error message.
function SF.Compiler.Compile(code, context, mainfile, player, data, dontpreprocess)

	local instance = setmetatable({},SF.Instance)
	
	data = data or {}
	
	instance.player = player
	instance.env = setmetatable({},context.env)
	instance.env._G = instance.env
	instance.data = data
	instance.ppdata = {}
	instance.ops = 0
	instance.hooks = {}
	instance.scripts = {}
	instance.initialized = false
	instance.context = context
	instance.mainfile = mainfile
	instance.permissions = setmetatable({},context.permissions)
	
	local loaded = {}

	local function recursiveLoad(path)
		if loaded[path] then return end
		loaded[path] = true
		
		if not dontpreprocess then
			SF.Preprocessor.ParseDirectives(path,code[path],context.directives,instance.ppdata)
		end
		
		if instance.ppdata.includes and instance.ppdata.includes[path] then
			local inc = instance.ppdata.includes[path]
			for i=1,#inc do
				recursiveLoad(inc[i])
			end
		end
		
		if code[path] == "" then
			-- Passing an empty string to CompileString returns an error because Lua does not have empty statements (at least in 5.1)
			error(path..": No code.",0)
		end
		
		local func = CompileString(code[path], "SF:"..path, false)
		if type(func) == "string" then
			error(path..": "..func, 0)
		end
		debug.setfenv(func,instance.env)
		
		instance.scripts[#instance.scripts+1] = func
	end
	
	local ok, msg = pcall(recursiveLoad, mainfile)
	if ok then
		instance.permissions:assign(instance)
		return true, instance
	else return false, msg end
end
<commit_msg>Store source code in the instance<commit_after>---------------------------------------------------------------------
-- SF Compiler. Compiles code into an uninitialized Instance.
-- @author <NAME>
---------------------------------------------------------------------

SF.Compiler = {}

--- Preprocesses and Compiles code and returns an Instance
-- @param code Either a string of code, or a {path=source} table
-- @param context The context to use in the resulting Instance
-- @param mainfile If code is a table, this specifies the first file to parse.
-- @param player The "owner" of the instance
-- @param data The table to set instance.data to. Default is a new table.
-- @param dontpreprocess Set to true to skip preprocessing
-- @return True if no errors, false if errors occured.
-- @return The compiled instance, or the error message.
function SF.Compiler.Compile(code, context, mainfile, player, data, dontpreprocess)

	local instance = setmetatable({},SF.Instance)
	
	data = data or {}
	
	instance.player = player
	instance.env = setmetatable({},context.env)
	instance.env._G = instance.env
	instance.data = data
	instance.ppdata = {}
	instance.ops = 0
	instance.hooks = {}
	instance.scripts = {}
	instance.source = code
	instance.initialized = false
	instance.context = context
	instance.mainfile = mainfile
	instance.permissions = setmetatable({},context.permissions)
	
	local loaded = {}

	local function recursiveLoad(path)
		if loaded[path] then return end
		loaded[path] = true
		
		if not dontpreprocess then
			SF.Preprocessor.ParseDirectives(path,code[path],context.directives,instance.ppdata)
		end
		
		if instance.ppdata.includes and instance.ppdata.includes[path] then
			local inc = instance.ppdata.includes[path]
			for i=1,#inc do
				recursiveLoad(inc[i])
			end
		end
		
		if code[path] == "" then
			-- Passing an empty string to CompileString returns an error because Lua does not have empty statements (at least in 5.1)
			error(path..": No code.",0)
		end
		
		local func = CompileString(code[path], "SF:"..path, false)
		if type(func) == "string" then
			error(path..": "..func, 0)
		end
		debug.setfenv(func,instance.env)
		
		instance.scripts[#instance.scripts+1] = func
	end
	
	local ok, msg = pcall(recursiveLoad, mainfile)
	if ok then
		instance.permissions:assign(instance)
		return true, instance
	else return false, msg end
end

{'fraction_non_alphanumeric': 0.15778251599147122, 'fraction_numerical': 0.0029850746268656717, 'mean_word_length': 2.945332211942809, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, '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': '5077531', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1322, 'n_words': 463}