Jump to content

Execute CopyPasta Language: Difference between revisions

m (→‎{{header|Go}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 180:
 
=={{header|zkl}}==
<lang zkl>var clipBoard=Data(), srcNm=vm.arglist[0];
<lang zkl></lang>
pasta:=File(srcNm).read(); // zkl pastaprog.cp
<lang zkl></lang>
foreach line in (pasta){
switch(line.strip().toLower()){
case("copy"){ clipBoard.clear(__lineWalker.next()) }
case("copyfile"){
n:=next(__lineWalker);
if(n.strip().toLower()=="TheF*ckingCode") clipBoard.clear(pasta);
else clipBoard.clear(File(n).read());
}
case("duplicate"){
n,t := next(__lineWalker,True), clipBoard.copy();
do(n){ t.append(clipBoard) } // noop if n<1
clipBoard=t;
}
case("pasta!"){ println(clipBoard.text); break; }
case(""){}
else{ error(__lineWalker,"Unknown command: ") }
}
}
fcn error(w,msg){
println("%s: %d: %s%s".fmt(srcNm, w.n, msg, w.value));
System.exit(1)
}
fcn next(w,wantInt=False){
try{
t:=w.next().strip();
if(wantInt) t=t.toInt();
return(t)
}catch{ error(w,wantInt and "Not an int: " or "Error: ") }
<lang zkl>}</lang>
Input programs:
<pre style="height:15ex">
//////////////prog.cp:
Copy
Rosetta Code
Duplicate
2
Pasta!
 
//////////////prog2.cp:
CopyFile
pasta.txt
Duplicate
1
Pasta!
 
/////////prog3.cp:
Copy
Invalid
Duplicate
1
 
Goto
3
Pasta!
 
//////////////pasta.txt:
I'm the pasta.txt file.
</pre>
{{out}}
<pre>
$ zkl copyPasta.zkl prog.cp
Rosetta Code
Rosetta Code
Rosetta Code
 
$ zkl copyPasta.zkl prog2.cp
I'm the pasta.txt file.
I'm the pasta.txt file.
 
$ zkl copyPasta.zkl prog3.cp
prog3.cp: 6: Unknown command: Goto
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.