Execute CopyPasta Language: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: simpler quoting) |
|||
Line 766: | Line 766: | ||
Program never ends!</pre> |
Program never ends!</pre> |
||
=={{header|Phix}}== |
|||
Fakes five files so it can be run w/o any fiddly setup, but will handle (differently-named!) real files as well.<br> |
|||
If run without command-line parameters it fakes five runs.<br> |
|||
Assumes if clipboard is "hello\n", Duplicate 2 should leave it as "hello\nhello\nhello\n", ie original & original*2. |
|||
<lang Phix>constant prog1_cp = """ |
|||
Copy |
|||
Rosetta Code |
|||
Duplicate |
|||
2 |
|||
Pasta! |
|||
La Vista |
|||
""", |
|||
prog2_cp = """ |
|||
CopyFile |
|||
pasta.txt |
|||
Duplicate |
|||
1 |
|||
Pasta! |
|||
""", |
|||
prog3_cp = """ |
|||
Copy |
|||
Invalid |
|||
Duplicate |
|||
1 |
|||
Goto |
|||
3 |
|||
Pasta! |
|||
""", |
|||
prog4_cp = """ |
|||
CopyFile |
|||
TheF*ckingCode |
|||
Duplicate |
|||
2 |
|||
Pasta! |
|||
""", |
|||
pasta_txt = """ |
|||
I'm the pasta.txt file. |
|||
""", |
|||
fake_files = {"prog1.cp","prog2.cp","prog3.cp","prog4.cp","pasta.txt"}, |
|||
fake_texts = { prog1_cp , prog2_cp , prog3_cp , prog4_cp , pasta_txt } |
|||
function get_file_lines(string filename) |
|||
sequence lines |
|||
integer k = find(filename,fake_files) |
|||
if k then |
|||
lines = split(fake_texts[k],"\n") |
|||
elsif get_file_type(filename)!=FILETYPE_FILE then |
|||
crash("file not found:%s",{filename}) |
|||
else |
|||
lines = get_text(filename,GT_LF_STRIPPED) |
|||
end if |
|||
return lines |
|||
end function |
|||
procedure interpret(string filename) |
|||
printf(1,"\ninterpret(%s):\n",{filename}) |
|||
sequence pgm = get_file_lines(filename)&{""} |
|||
string clipboard = "" |
|||
integer pc = 1 |
|||
while true do |
|||
if pc>=length(pgm) then crash("No Pasta! Sucha mistaka to maka") end if |
|||
string cmd = trim(pgm[pc]), arg = pgm[pc+1] |
|||
switch cmd do |
|||
case "Copy": clipboard &= arg&"\n" |
|||
case "CopyFile": clipboard &= join(iff(arg="TheF*ckingCode"?pgm:get_file_lines(arg)),"\n") |
|||
case "Duplicate": clipboard &= join(repeat(clipboard,to_integer(arg)),"") |
|||
case "Pasta!": puts(1,clipboard); return |
|||
case "": pc -= 1; break -- (skip blank lines [w/o arg]) |
|||
else crash("unknown command: %s on line %d",{cmd,pc}) |
|||
end switch |
|||
pc += 2 |
|||
end while |
|||
end procedure |
|||
procedure main() |
|||
sequence cl = command_line() |
|||
if length(cl)=2 then |
|||
for i=1 to 5 do |
|||
try |
|||
interpret(sprintf("prog%d.cp",i)) |
|||
catch e |
|||
?e[E_USER] |
|||
end try |
|||
end for |
|||
elsif length(cl)=3 then |
|||
interpret(cl[3]) |
|||
else |
|||
crash("usage: CopyPasta filename") |
|||
end if |
|||
end procedure |
|||
main()</lang> |
|||
{{out}} |
|||
<pre> |
|||
interpret(prog1.cp): |
|||
Rosetta Code |
|||
Rosetta Code |
|||
Rosetta Code |
|||
interpret(prog2.cp): |
|||
I'm the pasta.txt file. |
|||
I'm the pasta.txt file. |
|||
interpret(prog3.cp): |
|||
"crash(unknown command: Goto on line 6)" |
|||
interpret(prog4.cp): |
|||
CopyFile |
|||
TheF*ckingCode |
|||
Duplicate |
|||
2 |
|||
Pasta! |
|||
CopyFile |
|||
TheF*ckingCode |
|||
Duplicate |
|||
2 |
|||
Pasta! |
|||
CopyFile |
|||
TheF*ckingCode |
|||
Duplicate |
|||
2 |
|||
Pasta! |
|||
interpret(prog5.cp): |
|||
"crash(file not found:prog5.cp)" |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |