Task: Execute CopyPasta Language


C++: #include <fstream>
#include <iostream>
#include <sstream>
#include <streambuf>
#include <string>

#include <stdlib.h>

using namespace std;


void fatal_error(string errtext, char *argv[])
{
	cout << "%" << errtext << endl;
	cout << "usage: " << argv[0] << " [filename.cp]" << endl;
	exit(1);
}



string& ltrim(string& str, const string& chars = "\t\n\v\f\r ")
{
	str.erase(0, str.find_first_not_of(chars));
	return str;
}
string& rtrim(string& str, const string& chars = "\t\n\v\f\r ")
{
	str.erase(str.find_last_not_of(chars) + 1);
	return str;
}
string& trim(string& str, const string& chars = "\t\n\v\f\r ")
{
	return ltrim(rtrim(str, chars), chars);
}

int main(int argc, char *argv[])
{
	
	string fname = "";
	string source = "";
	try
	{
		fname = argv[1];
		ifstream t(fname);

		t.seekg(0, ios::end);
		source.reserve(t.tellg());
		t.seekg(0, ios::beg);

		source.assign((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
	}
	catch(const exception& e)
	{
		fatal_error("error while trying to read from specified file", argv);
	}

	
	string clipboard = "";

	
	int loc = 0;
	string remaining = source;
	string line = "";
	string command = "";
	stringstream ss;
	while(remaining.find("\n") != string::npos)
	{
		
		line = remaining.substr(0, remaining.find("\n"));
		command = trim(line);
		remaining = remaining.substr(remaining.find("\n") + 1);
		
		try
		{
			if(line == "Copy")
			{
				line = remaining.substr(0, remaining.find("\n"));
				remaining = remaining.substr(remaining.find("\n") + 1);
				clipboard += line;
			}
			else if(line == "CopyFile")
			{
				line = remaining.substr(0, remaining.find("\n"));
				remaining = remaining.substr(remaining.find("\n") + 1);
				if(line == "TheF*ckingCode")
					clipboard += source;
				else
				{
					string filetext = "";
					ifstream t(line);

					t.seekg(0, ios::end);
					filetext.reserve(t.tellg());
					t.seekg(0, ios::beg);

					filetext.assign((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
					clipboard += filetext;
				}
			}
			else if(line == "Duplicate")
			{
				line = remaining.substr(0, remaining.find("\n"));
				remaining = remaining.substr(remaining.find("\n") + 1);
				int amount = stoi(line);
				string origClipboard = clipboard;
				for(int i = 0; i < amount - 1; i++) {
					clipboard += origClipboard;
				}
			}
			else if(line == "Pasta!")
			{
				cout << clipboard << endl;
				return 0;
			}
			else
			{
				ss << (loc + 1);
				fatal_error("unknown command '" + command + "' encounter on line " + ss.str(), argv);
			}
		}
		catch(const exception& e)
		{
			ss << (loc + 1);
			fatal_error("error while executing command '" + command + "' on line " + ss.str(), argv);
		}

		
		loc += 2;
	}

	
	return 0;
}






Python: import sys


def fatal_error(errtext):
	print("%" + errtext)
	print("usage: " + sys.argv[0] + " [filename.cp]")
	sys.exit(1)


fname = None
source = None
try:
	fname = sys.argv[1]
	source = open(fname).read()
except:
	fatal_error("error while trying to read from specified file")


lines = source.split("\n")


clipboard = ""


loc = 0
while(loc < len(lines)):
	
	command = lines[loc].strip()

	try:
		if(command == "Copy"):
			clipboard += lines[loc + 1]
		elif(command == "CopyFile"):
			if(lines[loc + 1] == "TheF*ckingCode"):
				clipboard += source
			else:
				filetext = open(lines[loc+1]).read()
				clipboard += filetext
		elif(command == "Duplicate"):
			clipboard += clipboard * ((int(lines[loc + 1])) - 1)
		elif(command == "Pasta!"):
			print(clipboard)
			sys.exit(0)
		else:
			fatal_error("unknown command '" + command + "' encountered on line " + str(loc + 1))
	except Exception as e:
		fatal_error("error while executing command '" + command + "' on line " + str(loc + 1) + ": " + e)

	
	loc += 2