Execute CopyPasta Language: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 211:
try
if (command = "Copy")
else if ($command = "CopyFile")
if (lines[loc + 1] = "TheF*ckingCode")
else
filetext = new(Nanoquery.IO.File, lines[loc + 1]).readAll()
end
else if (command = "Duplicate")
else if (command = "Pasta!")
println clipboard
Line 232:
// increment past the command and the next line
end</lang>
Line 271:
{{out}}
<pre>$ java -jar ../nanoquery-2.
Rosetta CodeRosetta Code
$ java -jar ../nanoquery-2.
I'm the pasta.txt file.
$ java -jar ../nanoquery-2.
%unknown command '' encountered on line 5
usage: copypasta.nq [filename.cp]
$ java -jar ../nanoquery-2.
CopyFile
TheF*ckingCode
Line 291:
Pasta!
$ java -jar ../nanoquery-2.
%error while trying to read from specified file
usage: copypasta.nq [filename.cp]
</pre>
=={{header|Python}}==
{{trans|Nanoquery}}
<lang Python>import sys
# a function to handle fatal errors
def fatal_error(errtext):
print("%" + errtext)
print("usage: " + sys.argv[0] + " [filename.cp]")
sys.exit(1)
# get a filename from the command line and read the file in
fname = None
source = None
try:
fname = sys.argv[1]
source = open(fname).read()
except:
fatal_error("error while trying to read from specified file")
# convert the source to lines of code
lines = source.split("\n")
# a variable to represent the 'clipboard'
clipboard = ""
# loop over the lines that were read
loc = 0
while(loc < len(lines)):
# check which command is on this line
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)
# increment past the command and the next line
loc += 2
</lang>
The following files were used for testing:
<br /><strong>prog1.cp</strong>
<pre>Copy
Rosetta Code
Duplicate
2
Pasta!</pre>
<strong>prog2.cp</strong>
<pre>CopyFile
pasta.txt
Duplicate
1
Pasta!</pre>
<strong>prog3.cp</strong>
<pre>Copy
Invalid
Duplicate
1
Goto
3
Pasta!</pre>
<strong>prog4.cp</strong>
<pre>CopyFile
TheF*ckingCode
Duplicate
2
Pasta!</pre>
<strong>pasta.txt</strong>
<pre>I'm the pasta.txt file.</pre>
{{out}}
<pre>$ python3 copypasta.py prog1.cp
Rosetta CodeRosetta Code
$ python3 copypasta.py prog2.cp
I'm the pasta.txt file.
$ python3 copypasta.py prog3.cp
%unknown command '' encountered on line 5
usage: copypasta.nq [filename.cp]
$ python3 copypasta.py prog4.cp
CopyFile
TheF*ckingCode
Duplicate
2
Pasta!
CopyFile
TheF*ckingCode
Duplicate
2
Pasta!
$ python3 copypasta.py doesntexist.cp
%error while trying to read from specified file
usage: copypasta.nq [filename.cp]
|