Execute CopyPasta Language: Difference between revisions

Added FreeBASIC
m (→‎{{header|Wren}}: Changed to Wren S/H)
(Added FreeBASIC)
 
Line 248:
usage: ./copypasta [filename.cp]
</pre>
 
=={{header|FreeBASIC}}==
Building on Python and Phix solution
<syntaxhighlight lang="vbnet">' Rosetta Code problem: https://rosettacode.org/wiki/Execute_CopyPasta_Language
' by Jjuanhdez, 05/2024
 
#define isNumeric(s) Iif(Val(s) = 0 And s <> "0", False, True)
 
Const As String prog1_cp = "Copy" & Chr(10) & "Rosetta Code" & Chr(10) & "Duplicate" & Chr(10) & "2" & Chr(10) & "Pasta!" & Chr(10) & "La Vista"
Const As String prog2_cp = "CopyFile" & Chr(10) & "pasta.txt" & Chr(10) & "Duplicate" & Chr(10) & "1" & Chr(10) & "Pasta!"
Const As String prog3_cp = "Copy" & Chr(10) & "Invalid" & Chr(10) & " Duplicate" & Chr(10) & "1" & Chr(10) & "Goto" & Chr(10) & "3" & Chr(10) & "Pasta!"
Const As String prog4_cp = "CopyFile" & Chr(10) & "TheF*ckingCode" & Chr(10) & "Duplicate" & Chr(10) & "2" & Chr(10) & "Pasta!"
Const As String prog5_cp = "Copy" & Chr(10) & "Rosetta Code" & Chr(10) & "Duplicate" & Chr(10) & "A" & Chr(10) & "Pasta!"
Const As String doesntexist_cp = ""
Const As String pasta_txt = "The pasta.txt file."
 
Dim Shared As String prog
Dim Shared As String fake_files(1 To 7)
Dim Shared As String fake_texts(1 To 7)
fake_files(1) = "prog1.cp"
fake_files(2) = "prog2.cp"
fake_files(3) = "prog3.cp"
fake_files(4) = "prog4.cp"
fake_files(5) = "prog5.cp"
fake_files(6) = "doesntexist.cp"
fake_files(7) = "pasta.txt"
 
fake_texts(1) = prog1_cp
fake_texts(2) = prog2_cp
fake_texts(3) = prog3_cp
fake_texts(4) = prog4_cp
fake_texts(5) = prog5_cp
fake_texts(6) = doesntexist_cp
fake_texts(7) = pasta_txt
 
Function GetFileLines(Byval filename As String) As String
Dim As String lines, source, linea
Dim As Integer i = 0, k
For k = Lbound(fake_files) To Ubound(fake_files)
If filename = fake_files(k) Then
source = Iif(Command() = "", fake_texts(k), prog)
lines = ""
While i <= Len(source)
linea = ""
While i <= Len(source) And Mid(source, i, 1) <> Chr(10)
linea &= Mid(source, i, 1)
i += 1
Wend
lines &= Trim(linea) & Chr(10)
i += 1
Wend
Exit For
End If
Next
Return lines
End Function
 
Sub Interpret(Byval filename As String)
Dim As String linea, tmp, cmd, arg
Dim As String pgm = GetFileLines(filename)
Dim As String clipboard = ""
Dim As Integer pc = 1, l = 0
Print "interpret(" & filename & "):"
Do
If pc >= Len(pgm) Then
Print "No Pasta! Sucha mistaka to maka"
Exit Sub
End If
cmd = Trim(Mid(pgm, pc, Instr(pc, pgm, Chr(10)) - pc))
arg = Mid(pgm, pc + Len(cmd) + 1, Instr(pc + Len(cmd) + 1, pgm, Chr(10)) - pc - Len(cmd) - 1)
Select Case cmd
Case "Copy"
clipboard &= arg
l += 4
Case "CopyFile"
Dim As Integer fileNumber = Freefile
If arg = "TheF*ckingCode" Then
Open filename For Input As #fileNumber
Else
Open arg For Input As #fileNumber
If Err > 0 Then Print "Error: Could not open file "; arg
End If
Do While Not Eof(fileNumber)
Line Input #fileNumber, linea
tmp &= linea & Chr(10)
Loop
Close #fileNumber
clipBoard &= tmp
l += 2
Case "Duplicate"
If Not isNumeric(arg) Then
Print "Line " & l & ": wrong number of repetitions."
Exit Do
End If
tmp = ""
For i As Integer = 1 To Val(arg)
tmp &= clipboard
Next
clipboard &= tmp
l += 2
Case "Pasta!"
Print clipboard
Exit Sub
Case ""
pc -= 1
Case Else
Print "Line " & l & ": unknown command """ & cmd & """"
Exit Sub
End Select
pc += Len(cmd) + Len(arg) + 2
Loop
End Sub
 
Sub Main()
Dim As String cad, cl = Command()
Dim As Integer ff = Freefile
 
If cl <> "" Then
If Open(cl For Input As #ff) = 0 Then
While Not Eof(ff)
Line Input #ff, cad
prog &= Trim(cad) & Chr(10)
Wend
Close #ff
Else
Print "Error: Could not open file "; cl
Exit Sub
End If
Interpret(cl)
Else
For i As Integer = Lbound(fake_files) To Ubound(fake_files)-1
Interpret("prog" & (i) & ".cp")
Print
Next
End If
End Sub
 
Main()
Sleep</syntaxhighlight>
{{out}}
<pre>interpret(prog1.cp):
Rosetta CodeRosetta CodeRosetta Code
 
interpret(prog2.cp):
I'm the pasta.txt file.
I'm the pasta.txt file.
 
interpret(prog3.cp):
Line 6: unknown command "Goto"
 
interpret(prog4.cp):
CopyFile
TheF*ckingCode
Duplicate
2
Pasta!
CopyFile
TheF*ckingCode
Duplicate
2
Pasta!
CopyFile
TheF*ckingCode
Duplicate
2
Pasta!
 
interpret(prog5.cp):
Line 4: wrong number of repetitions.
 
interpret(prog6.cp):
Error: Could not open file doesntexist.cp</pre>
 
=={{header|Go}}==
2,169

edits