IVY DOM


 

人生就像一副复杂拼图
每个人总有属於自己的记忆碎片
优质美国空间-老薛主机|IVY DOM|Flowline|

联系我

RSS




一月 29, 2013

[开源]vb.net写的brainfuck解释器

Brainfuck是一种极小化的计算机语言
下面是这八种状态的描述,其中每个状态由一个字符标识:[1]
字符
含义
>
指针加一
<
指针减一
+
指针指向的字节的值加一
-
指针指向的字节的值减一
.
输出指针指向的单元内容(ASCⅡ码)
,
输入内容到指针指向的单元(ASCⅡ码)
[
如果指针指向的单元值为零,向后跳转到对应的]指令的次一指令处
]
如果指针指向的单元值不为零,向前跳转到对应的[指令的次一指令处
(按照更节省时间的简单说法,”]”也可以说成“向后跳转到对应的”[“状态”。这两解释是一样的。)
(第三种同价的说法,”[“意思是”向前跳转到对应的”]””,]意思是”向后跳转到对应的[指令的次一指令处,如果指针指向的字节非零。”)
Brainfuck程序可以用下面的替换方法翻译成C语言(假设ptr是char*类型):
Brainfuck
C
>
++ptr;
<
–ptr;
+
++*ptr;
-
–*ptr;
.
putchar(*ptr);
,
*ptr =getchar();
[
while (*ptr) {
]
}
当前位置清零
[-] 将当前指针的值归零
之前位置清零
[[-]<] 将当前指针以及之前的指针归零
字符I/O
,. 从键盘读取一个字符并输出到屏幕上。
简单的循环
,[.,] 这是一个连续从键盘读取字符并回显到屏幕上的循环。注意,这里假定0表示输入结束,事实上有些系统并非如此。以-1和”未改变”作为判断依据的程序代码分别是”,+[-.,+]”和”,[.[-],]”。
指针维护
>,[.>,] 通过移动指针保存所有的输入,供后面的程序使用。
加法
[->+<]
把当前位置的值加到后面的单元中(破坏性的加,它导致左边的单元被归零)。
以上摘自百度百科
Public Class Form1
    Const codes = "+++++[>++++++[>+?<-]<-]"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim aa(4) As Byte
        'putnum(aa, "222")
        execute(codes)
    End Sub
    Sub putnum(ByVal temp() As Byte, ByVal datas As Byte)
        Dim a, tmp As String
        Dim i As Integer
        a = datas.ToString
        For i = 1 To Len(a)
            tmp = Mid(CByte(a), i, 1)
            Debug.WriteLine(tmp)
            temp(i) = Asc(tmp)
            Debug.WriteLine(Asc(tmp))
        Next
        temp(i) = 32
    End Sub
    Sub execute(ByVal codesignal As String)
        Dim length, pcode, pmemory, match, index As Integer
        Dim memory(1024) As Byte
        Dim test As String
        Dim n(1024) As Byte
        Dim myEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
        Dim syllablecode As Byte() = myEncoding.GetBytes(codesignal)
        Debug.WriteLine(syllablecode)
        length = syllablecode.Length
        pcode = 1
        pmemory = 1
        TextBox2.Text = ""
        TextBox1.Focus()
        index = 0
        While pcode <= length
            If syllablecode(pcode) = 60 Then '<
                pmemory -= 1
            ElseIf syllablecode(pcode) = 62 Then '>
                pmemory += 1
            ElseIf syllablecode(pcode) = 43 Then '+
                memory(pmemory) += 1
            ElseIf syllablecode(pcode) = 45 Then '-
                memory(pmemory) -= 1
            ElseIf syllablecode(pcode) = 44 Then ',
                test = InputBox("", , "0")
                memory(pmemory) = Asc(test)
            ElseIf syllablecode(pcode) = 46 Then ',
                n(index) = memory(pmemory)
                TextBox2.Text = n.ToString
            ElseIf syllablecode(pcode) = 91 Then '[
                match = 1
                If memory(pmemory) = 0 Then
                    While pcode <= length
                        pcode += 1
                        If syllablecode(pcode) = 93 Then
                            match -= 1
                            If (match = 0) Then
                                Exit While
                            End If
                        ElseIf syllablecode(pcode) = 91 Then
                            match += 1
                        End If
                    End While
                End If
            ElseIf syllablecode(pcode) = 93 Then ']
                match = 1
                If memory(pmemory) <> 0 Then
                    While pcode > length
                        pcode -= 1
                        If syllablecode(pcode) = 91 Then
                            match -= 1
                            If (match = 0) Then
                                Exit While
                            End If
                        ElseIf syllablecode(pcode) = 93 Then
                            match += 1
                        End If
                    End While
                End If
            ElseIf syllablecode(pcode) = 63 Then '?
                putnum(n, memory(pmemory))
                TextBox2.Text = n.ToString
            Else
                MsgBox("代码有误!")
                Exit While
            End If
            index += 1
            pcode += 1
        End While
    End Sub
End Class

相关文章

返回
    • Pronoses
    • 一月 29th, 2013

    沙发

  1. 赠送电信HG56 无线网信息U盘存档破解代码:
    Function jiami(str)
    len1 = Len(str)
    out = ""
    For i = 1 To len1
    c1 = Right(Left(str, i), 1)
    c2 = Asc(c1)
    c3 = c2 * 2
    If c3 > 127 Then
    c3 = c3 – 127
    End If
    out = out & Chr(c3)
    Next
    f.output.Value = out
    End Function
    Function jiemi(str)
    len1 = Len(str)
    out = ""
    For i = 1 To len1
    c1 = Right(Left(str, i), 1)
    c2 = Asc(c1)
    If c2 Mod 2 = 0 Then
    c3 = c2 / 2
    Else
    c3 = (c2 + 127) / 2
    End If
    out = out & Chr(c3)
    Next
    Text2.Text = out
    End Function

  2. 这语言很牛逼吗

  3. 算是用VB写的brainfuck的编译器?

  4. 虽不明,但觉厉!

  1. 暂无 Trackback

You must be logged in to post a comment.