Option Strict On
Option Explicit On
Imports System
Module Module1
Sub Main()
If My.Application.CommandLineArgs.Count < 1 Then
Console.WriteLine("Usage: Brainf__k <filename>")
Console.ReadKey()
End
End If
'****************** DICHIARAZIONI *****************************
tbyte = "Public Structure bbyte" & newline & _
"Public val As Byte" & newline & _
"Public Sub inc()" & newline & _
"If Me.val = 255 Then" & newline & _
"Me.val = 0" & newline & _
"reg=1" & newline & _
"Else" & newline & _
"Me.val = me.val + 1" & newline & _
"End If" & newline & _
"End Sub" & newline & _
"Public Sub dec()" & newline & _
"If Me.val = 0 Then" & newline & _
"Me.val = 255" & newline & _
"reg=2" & newline & _
"Else" & newline & _
"Me.val = me.val - 1" & newline & _
"End If" & newline & _
"End Sub" & newline & _
"Public Sub ass(ByVal value As Byte)" & newline & _
"Me.val = value" & newline & _
"End Sub" & newline & _
"End Structure" & newline
smain = "Sub Main()" & newline
esub = "End Sub" & newline
ar = "Dim ar(1048576) As bbyte" & newline
vx = "Dim x As Integer = 0" & newline & "'Da qui inizia il codice vero e proprio" & newline
code = "Imports System" & newline & "Module Module1" & newline & "Public file As IO.FileStream" & newline & "public reg as byte" & newline & "Public fo as string" & newline
'**************** FINE DICHIARAZIONI **************************
'--------------------------------------------------------------
'**************** SCRITTURA ISTRUZIONI ************************
_inc = "ar(x).inc()" & newline
_dec = "ar(x).dec()" & newline
_in = "ar(x).val = Convert.ToByte(Console.ReadKey.KeyChar)" & newline
_out = "System.Console.Write(Convert.ToChar(ar(x).val))" & newline
_ipt = "x = x + 1" & newline
_dpt = "x = x - 1" & newline
_sloop = "do while (ar(x).val <> 0)" & newline
_eloop = "loop" & newline
_oun = "System.Console.Write((ar(x).val).ToString())" & newline
_car = "ar(x).val = reg" & newline
_scar = "reg = ar(x).val" & newline
_fop = "fo=""""" & newline & "do while ar(x).val <> 0" & newline & _
"fo = fo & convert.tochar(ar(x).val)" & newline & "loop" & newline & "fo=fo.replace(""?patch"", ""My.Application.CommandLineArgs(0).Split(""."")(0)"")" & newline & "file = New IO.FileStream(fo, IO.FileMode.OpenOrCreate)" & newline
_fin = "ar(x).val = convert.tobyte(file.readbyte())" & newline
_fout = "file.writebyte(ar(x).val)" & newline
_fcl = "file.close()" & newline & "file = nothing" & newline
_mod = "ar(x).val = ar(x).val mod ar(x-1).val" & newline
_mul = "ar(x).val = ar(x).val * ar(x-1).val" & newline
_div = "ar(x).val = ar(x).val \ ar(x-1).val" & newline
_iun = "ar(x).val = int32.parse(console.readline()) and 255" & newline
'****************** FINE ISTRUZIONI ***************************
Dim source As String = My.Computer.FileSystem.ReadAllText(My.Application.CommandLineArgs(0))
Dim x As Integer
Dim vb As New Microsoft.VisualBasic.VBCodeProvider()
Dim parameters As New CodeDom.Compiler.CompilerParameters
Dim res As CodeDom.Compiler.CompilerResults
Dim asm As System.Reflection.Assembly
For Each asm In AppDomain.CurrentDomain.GetAssemblies
parameters.ReferencedAssemblies.Add(asm.Location)
Next
parameters.GenerateExecutable = True
parameters.OutputAssembly = My.Application.CommandLineArgs(0) & ".exe"
code = code & tbyte & smain & ar & vx
Dim z As Integer
Dim temp As String
For x = 0 To source.Length - 1
If source(x) = "(" Then
For z = 1 To 9
temp = temp & source(x - 1)
Next
source.Replace(source(x - 1) & "(", temp)
temp = ""
z = 0
End If
If source(x) = ")" Then
For z = 1 To 99
temp = temp & source(x - 1)
Next
source.Replace(source(x - 1) & "(", temp)
temp = ""
z = 0
End If
If source(x) = "{" Then
For z = 1 To 999
temp = temp & source(x - 1)
Next
source.Replace(source(x - 1) & "(", temp)
temp = ""
z = 0
End If
If source(x) = "}" Then
For z = 1 To 9999
temp = temp & source(x - 1)
Next
source.Replace(source(x - 1) & "(", temp)
temp = ""
z = 0
End If
If source(x) = "+" Then
code = code & _inc
End If
If source(x) = "-" Then
code = code & _dec
End If
If source(x) = "," Then
code = code & _in
End If
If source(x) = "." Then
code = code & _out
End If
If source(x) = ">" Then
code = code & _ipt
End If
If source(x) = "<" Then
code = code & _dpt
End If
If source(x) = "[" Then
code = code & _sloop
End If
If source(x) = "]" Then
code = code & _eloop
End If
If source(x) = "?" Then
code = code & _car
End If
If source(x) = "@" Then
code = code & _scar
End If
If source(x) = "&" Then
code = code & _fop
End If
If source(x) = "'" Then
code = code & _fin
End If
If source(x) = Convert.ToChar(34) Then
code = code & _fout
End If
If source(x) = "|" Then
code = code & _fcl
End If
If source(x) = "%" Then
code = code & _mod
End If
If source(x) = "!" Then
code = code & _oun
End If
If source(x) = "*" Then
code = code & _mul
End If
If source(x) = "/" Then
code = code & _div
End If
If source(x) = "^" Then
code = code & _iun
End If
Next
code = code & esub & "End Module"
Console.Write(code)
res = vb.CompileAssemblyFromSource(parameters, code)
Dim y As Integer
Dim codes() As String = code.Split(newline.ToCharArray)
For y = 0 To res.Errors.Count - 1
Console.WriteLine("Errore " & res.Errors(y).Line & ": " & res.Errors(y).ErrorText)
Console.WriteLine("... " & codes(32) & newline)
Next
Console.WriteLine()
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("L'eseguibile è stato creato nella cartella del sorgente!")
Console.WriteLine("Premi e per uscire.")
Console.WriteLine("Premi s per salvare il codice vb del programma a scopo di studio ed uscire.")
Console.WriteLine()
Dim k As Char
k = Console.ReadKey.KeyChar
If k = "s" Then
My.Computer.FileSystem.WriteAllText(My.Application.CommandLineArgs(0) & ".vb", code, False)
End If
End Sub
End Module