Gmod Brainfuck Interpreter

http://filesmelt.com/dl/gmbf01.png

http://img822.imageshack.us/img822/7599/gmbf01.png

This is my first project using GMod Lua, so I just wanted to try something simple to get to know it. The screenshot is of it running numwarp. It outputs a debug log as well - here is the one for hello world:



Intitialising memory...
Success! Beginning execution...
Incremented cell 1 to 1
Incremented cell 1 to 2
Incremented cell 1 to 3
Incremented cell 1 to 4
Incremented cell 1 to 5
Incremented cell 1 to 6
Incremented cell 1 to 7
Incremented cell 1 to 8
Incremented cell 1 to 9
Incremented cell 1 to 10
Entered loop
Moved pointer to 2
Incremented cell 2 to 1
Incremented cell 2 to 2
Incremented cell 2 to 3
Incremented cell 2 to 4
Incremented cell 2 to 5
Incremented cell 2 to 6
Incremented cell 2 to 7
Moved pointer to 3
Incremented cell 3 to 1
Incremented cell 3 to 2
Incremented cell 3 to 3
Incremented cell 3 to 4
Incremented cell 3 to 5
Incremented cell 3 to 6
Incremented cell 3 to 7
Incremented cell 3 to 8
Incremented cell 3 to 9
Incremented cell 3 to 10
Moved pointer to 4
Incremented cell 4 to 1
Incremented cell 4 to 2
Incremented cell 4 to 3
Moved pointer to 5
Incremented cell 5 to 1
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 9
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 8
Incremented cell 2 to 9
Incremented cell 2 to 10
Incremented cell 2 to 11
Incremented cell 2 to 12
Incremented cell 2 to 13
Incremented cell 2 to 14
Moved pointer to 3
Incremented cell 3 to 11
Incremented cell 3 to 12
Incremented cell 3 to 13
Incremented cell 3 to 14
Incremented cell 3 to 15
Incremented cell 3 to 16
Incremented cell 3 to 17
Incremented cell 3 to 18
Incremented cell 3 to 19
Incremented cell 3 to 20
Moved pointer to 4
Incremented cell 4 to 4
Incremented cell 4 to 5
Incremented cell 4 to 6
Moved pointer to 5
Incremented cell 5 to 2
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 8
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 15
Incremented cell 2 to 16
Incremented cell 2 to 17
Incremented cell 2 to 18
Incremented cell 2 to 19
Incremented cell 2 to 20
Incremented cell 2 to 21
Moved pointer to 3
Incremented cell 3 to 21
Incremented cell 3 to 22
Incremented cell 3 to 23
Incremented cell 3 to 24
Incremented cell 3 to 25
Incremented cell 3 to 26
Incremented cell 3 to 27
Incremented cell 3 to 28
Incremented cell 3 to 29
Incremented cell 3 to 30
Moved pointer to 4
Incremented cell 4 to 7
Incremented cell 4 to 8
Incremented cell 4 to 9
Moved pointer to 5
Incremented cell 5 to 3
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 7
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 22
Incremented cell 2 to 23
Incremented cell 2 to 24
Incremented cell 2 to 25
Incremented cell 2 to 26
Incremented cell 2 to 27
Incremented cell 2 to 28
Moved pointer to 3
Incremented cell 3 to 31
Incremented cell 3 to 32
Incremented cell 3 to 33
Incremented cell 3 to 34
Incremented cell 3 to 35
Incremented cell 3 to 36
Incremented cell 3 to 37
Incremented cell 3 to 38
Incremented cell 3 to 39
Incremented cell 3 to 40
Moved pointer to 4
Incremented cell 4 to 10
Incremented cell 4 to 11
Incremented cell 4 to 12
Moved pointer to 5
Incremented cell 5 to 4
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 6
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 29
Incremented cell 2 to 30
Incremented cell 2 to 31
Incremented cell 2 to 32
Incremented cell 2 to 33
Incremented cell 2 to 34
Incremented cell 2 to 35
Moved pointer to 3
Incremented cell 3 to 41
Incremented cell 3 to 42
Incremented cell 3 to 43
Incremented cell 3 to 44
Incremented cell 3 to 45
Incremented cell 3 to 46
Incremented cell 3 to 47
Incremented cell 3 to 48
Incremented cell 3 to 49
Incremented cell 3 to 50
Moved pointer to 4
Incremented cell 4 to 13
Incremented cell 4 to 14
Incremented cell 4 to 15
Moved pointer to 5
Incremented cell 5 to 5
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 5
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 36
Incremented cell 2 to 37
Incremented cell 2 to 38
Incremented cell 2 to 39
Incremented cell 2 to 40
Incremented cell 2 to 41
Incremented cell 2 to 42
Moved pointer to 3
Incremented cell 3 to 51
Incremented cell 3 to 52
Incremented cell 3 to 53
Incremented cell 3 to 54
Incremented cell 3 to 55
Incremented cell 3 to 56
Incremented cell 3 to 57
Incremented cell 3 to 58
Incremented cell 3 to 59
Incremented cell 3 to 60
Moved pointer to 4
Incremented cell 4 to 16
Incremented cell 4 to 17
Incremented cell 4 to 18
Moved pointer to 5
Incremented cell 5 to 6
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 4
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 43
Incremented cell 2 to 44
Incremented cell 2 to 45
Incremented cell 2 to 46
Incremented cell 2 to 47
Incremented cell 2 to 48
Incremented cell 2 to 49
Moved pointer to 3
Incremented cell 3 to 61
Incremented cell 3 to 62
Incremented cell 3 to 63
Incremented cell 3 to 64
Incremented cell 3 to 65
Incremented cell 3 to 66
Incremented cell 3 to 67
Incremented cell 3 to 68
Incremented cell 3 to 69
Incremented cell 3 to 70
Moved pointer to 4
Incremented cell 4 to 19
Incremented cell 4 to 20
Incremented cell 4 to 21
Moved pointer to 5
Incremented cell 5 to 7
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 3
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 50
Incremented cell 2 to 51
Incremented cell 2 to 52
Incremented cell 2 to 53
Incremented cell 2 to 54
Incremented cell 2 to 55
Incremented cell 2 to 56
Moved pointer to 3
Incremented cell 3 to 71
Incremented cell 3 to 72
Incremented cell 3 to 73
Incremented cell 3 to 74
Incremented cell 3 to 75
Incremented cell 3 to 76
Incremented cell 3 to 77
Incremented cell 3 to 78
Incremented cell 3 to 79
Incremented cell 3 to 80
Moved pointer to 4
Incremented cell 4 to 22
Incremented cell 4 to 23
Incremented cell 4 to 24
Moved pointer to 5
Incremented cell 5 to 8
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 2
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 57
Incremented cell 2 to 58
Incremented cell 2 to 59
Incremented cell 2 to 60
Incremented cell 2 to 61
Incremented cell 2 to 62
Incremented cell 2 to 63
Moved pointer to 3
Incremented cell 3 to 81
Incremented cell 3 to 82
Incremented cell 3 to 83
Incremented cell 3 to 84
Incremented cell 3 to 85
Incremented cell 3 to 86
Incremented cell 3 to 87
Incremented cell 3 to 88
Incremented cell 3 to 89
Incremented cell 3 to 90
Moved pointer to 4
Incremented cell 4 to 25
Incremented cell 4 to 26
Incremented cell 4 to 27
Moved pointer to 5
Incremented cell 5 to 9
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 1
Returned to start of loop
Moved pointer to 2
Incremented cell 2 to 64
Incremented cell 2 to 65
Incremented cell 2 to 66
Incremented cell 2 to 67
Incremented cell 2 to 68
Incremented cell 2 to 69
Incremented cell 2 to 70
Moved pointer to 3
Incremented cell 3 to 91
Incremented cell 3 to 92
Incremented cell 3 to 93
Incremented cell 3 to 94
Incremented cell 3 to 95
Incremented cell 3 to 96
Incremented cell 3 to 97
Incremented cell 3 to 98
Incremented cell 3 to 99
Incremented cell 3 to 100
Moved pointer to 4
Incremented cell 4 to 28
Incremented cell 4 to 29
Incremented cell 4 to 30
Moved pointer to 5
Incremented cell 5 to 10
Moved pointer to 4
Moved pointer to 3
Moved pointer to 2
Moved pointer to 1
Decremented cell 1 to 0
Exited loop
Moved pointer to 2
Incremented cell 2 to 71
Incremented cell 2 to 72
Output char H (value 72) from cell 2
Moved pointer to 3
Incremented cell 3 to 101
Output char e (value 101) from cell 3
Incremented cell 3 to 102
Incremented cell 3 to 103
Incremented cell 3 to 104
Incremented cell 3 to 105
Incremented cell 3 to 106
Incremented cell 3 to 107
Incremented cell 3 to 108
Output char l (value 108) from cell 3
Output char l (value 108) from cell 3
Incremented cell 3 to 109
Incremented cell 3 to 110
Incremented cell 3 to 111
Output char o (value 111) from cell 3
Moved pointer to 4
Incremented cell 4 to 31
Incremented cell 4 to 32
Output char   (value 32) from cell 4
Moved pointer to 3
Moved pointer to 2
Incremented cell 2 to 73
Incremented cell 2 to 74
Incremented cell 2 to 75
Incremented cell 2 to 76
Incremented cell 2 to 77
Incremented cell 2 to 78
Incremented cell 2 to 79
Incremented cell 2 to 80
Incremented cell 2 to 81
Incremented cell 2 to 82
Incremented cell 2 to 83
Incremented cell 2 to 84
Incremented cell 2 to 85
Incremented cell 2 to 86
Incremented cell 2 to 87
Output char W (value 87) from cell 2
Moved pointer to 3
Output char o (value 111) from cell 3
Incremented cell 3 to 112
Incremented cell 3 to 113
Incremented cell 3 to 114
Output char r (value 114) from cell 3
Decremented cell 3 to 113
Decremented cell 3 to 112
Decremented cell 3 to 111
Decremented cell 3 to 110
Decremented cell 3 to 109
Decremented cell 3 to 108
Output char l (value 108) from cell 3
Decremented cell 3 to 107
Decremented cell 3 to 106
Decremented cell 3 to 105
Decremented cell 3 to 104
Decremented cell 3 to 103
Decremented cell 3 to 102
Decremented cell 3 to 101
Decremented cell 3 to 100
Output char d (value 100) from cell 3
Moved pointer to 4
Incremented cell 4 to 33
Output char ! (value 33) from cell 4
Moved pointer to 5
Output char 
 (value 10) from cell 5


I’ll add settings and stuff, once I get DPropertySheets sorted out - as far as I can tell from the examples, you can only add one item to each sheet. I might need some help with this bit :saddowns:

Source, if you guys want it:



local frame = vgui.Create("DFrame")    
frame:SetPos(100, 100)
frame:SetSize(400, 240)
frame:SetTitle("GMod Brainfuck by r0b0tsquid")
    

frame:MakePopup()

local buttonRun = vgui.Create("DButton")
buttonRun:SetParent(frame)
buttonRun:SetText( "Run!" )
buttonRun:SetPos( 220, 40 )
buttonRun:SetSize( 160, 30 )
buttonRun.DoClick = function ()
    chat.AddText("Intitialising memory...")
    data = {}
    for i = 1, 30000 do
        data* = 0
    end
    chat.AddText("Success! Beginning execution...")
    i = 1
    p = 1
    ip = 1
    lc = 0
    
    prog = codeBox:GetValue()
    inpt = inputBox:GetValue()
    outputBox:SetValue("")
    
    while i <= string.len(prog) do
        v = string.byte(prog, i)
        if v == 43 then
            data[p] = data[p] + 1
            chat.AddText("Incremented cell "..p.." to "..data[p])
            if data[p] > 255 then data[p] = 0 end
        elseif v == 45 then
            data[p] = data[p] - 1
            chat.AddText("Decremented cell "..p.." to "..data[p])
            if data[p] < 0 then data[p] = 255 end
        elseif v == 60 then
            p = p - 1
            chat.AddText("Moved pointer to "..p)
            if p < 0 then
                p = 30000
                chat.AddText("Wrapped pointer to "..p)
            end
        elseif v == 62 then
            p = p + 1
            chat.AddText("Moved pointer to "..p)
            if p > 30000 then
                p = 1
                chat.AddText("Wrapped pointer to "..p)
            end
        elseif v == 46 then
            outputBox:SetValue(outputBox:GetValue()..string.char(data[p]))
            chat.AddText("Output char "..string.char(data[p]).." (value "..data[p]..") from cell "..p)
        elseif v == 44 then
            if ip <= string.len(inpt) then
                data[p] = string.byte(inpt, ip)
                chat.AddText("Cell "..p.." set to "..data[p])
                ip = ip + 1
            else
                data[p] = 0
            end
        elseif v == 91 then
            if data[p] == 0 then
                chat.AddText("Skipped loop")
                lco = lc
                lc = lc + 1
                vb = 0
                while lc ~= lco do
                    i = i + 1
                    vb = string.byte(prog, i)
                    if vb == 91 then lc = lc + 1 end
                    if vb == 93 then lc = lc - 1 end
                end
            else
                chat.AddText("Entered loop")
                lc = lc + 1
            end
        elseif v == 93 then
            if data[p] ~= 0 then
                chat.AddText("Returned to start of loop")
                lco = lc
                lc = lc + 1
                vb = 0
                while lc ~= lco do
                    i = i - 1
                    vb = string.byte(prog, i)
                    if vb == 91 then lc = lc - 1 end
                    if vb == 93 then lc = lc + 1 end
                end
            else
                lc = lc - 1
                chat.AddText("Exited loop")
            end
        end
    i = i + 1
    end
end
 
inputBox = vgui.Create("DTextEntry")
inputBox:SetParent(frame)
inputBox:SetText("Enter Input:")
inputBox:SetPos( 20, 40 )
inputBox:SetSize( 160, 30 )

codeBox = vgui.Create("DTextEntry")
codeBox:SetParent(frame)
codeBox:SetText(",>++++++[<-------->-],[<+>-]<.")
codeBox:SetMultiline(true)
codeBox:SetPos( 20, 90 )
codeBox:SetSize( 160, 130 )

outputBox = vgui.Create("DTextEntry")
outputBox:SetParent(frame)
outputBox:SetText("Lulz")
outputBox:SetMultiline(true)
outputBox:SetPos( 220, 90 )
outputBox:SetSize( 160, 130 )


Jesus, brainfuck fucks up my brain

I don’t get it… :saddowns:

Brainfuck’s a language, with eight commands. It’s designed to be almost impossible to do anything useful with. You might want to look it up - it messes with you, but in a good way :smile:

Fun fact: it’s the language with the smallest compiler: 240 bytes!

A compiler would take some more bytes.

[editline]09:05PM[/editline]

Or is that just Wikipedia being wrong?

That can’t be true. If it’s a text based compiler, it’s under 100 characters. Otherwise, it’s still tiny.

If I remember correctly this was done by someone a long time ago. Nonetheless, nice work.

Then this page lied to me.

Sort of an odd first project to get into lua with. You seem to have experience with programming in general and lua isn’t really a tough one to get the hang of. You should spend most of your time figuring out source’s ins and outs so you can actually hack something together.

Thank you :smile:
I would have searched first, but, well, you know…

Anyway, does anyone have any ideas about these DPropertySheets?

You can add multiple elements onto one sheet if you use a DForm for each sheet and parent the controls to that.

From the small piece I can see (can’t scroll codeboxes with Android :saddowns:) you seem to be using lots of global variables, I suggest changing those to locals. And with property sheets, you add a panel as the item, then parent other components to that.

[editline]11:21PM[/editline]

:ninja:

Ah, Thank you! I thought it would be something like that.

Yeah - I can’t make my text boxes local, because otherwise I can’t access them from my button’s click callback. I think. As for the other stuff, I guess I’m just out of the habit: I usually write stuff for my DS and PSP, so I’m used to being the only thing running at the time :confused:

You can define locals at the top of your file by just doing
[lua]local textbox1
local textbox2

– code[/lua]

Doesn’t look like it does that to me:

[cpp]#include<stdio.h>

int main(int argc, char **argv) {
int pc, args, xc, prog_len, l = 0;
int x[32768];
int p[32768];

FILE *stream, *fopen();

for (args = 1; args < argc; args++) {

stream = fopen(argv[args], "r");

prog_len = 0;

for (pc = 0; pc &lt; 32768 && (p[pc] = getc(stream)) != EOF; pc++)
  prog_len++;

pc = 0;

fclose(stream);

for(xc = 0; xc &lt; 32768; xc++)
  x[xc] = 0;

xc = 0;

for(pc = 0; pc &lt; prog_len; pc++) {

  // '+'
  if      (p[pc] == 43) x[xc]++;

  // '-'
  else if (p[pc] == 45) x[xc]--;

  // '.'
  else if (p[pc] == 46) putchar(x[xc]);

  // ','
  else if (p[pc] == 44) x[xc] = getchar();

  // '&gt;'
  else if (p[pc] == 62) xc++;

  // '&lt;'
  else if (p[pc] == 60) xc--;

  // '['
  else if (p[pc] == 91) {
    if (x[xc] == 0) {
      pc++;
      while (l &gt; 0 || p[pc] != 93) {
        if (p[pc] == 91) l++;
        if (p[pc] == 93) l--;
        pc++;
      }
    }
  }

  // ']'
  else if (p[pc] == 93) {
    pc--;
    while (l &gt; 0 || p[pc] != 91) {
      if (p[pc] == 93) l++;
      if (p[pc] == 91) l--;
      pc--;
    }
    pc--;
  }
}  

}
putchar(10);
return 0;
}[/cpp]

(I can’t find Muller’s original one right now.)

[lua]local codeBox
local someBtn = vgui.Create(“DButton”)
someBtn.OnClick = function() codeBox:DoStuff() end
codeBox = vgui.Create(“DCantRemember”)[/lua]

:science:

[editline]11:32PM[/editline]

I blame Android for the ninja :saddowns:

Wait, what? How does that work :monocle:

[editline]09:36PM[/editline]

Shouldn’t it be out of scope?

The wonderful magic of scope.

Also, two ninjas in one thread in under 10 minutes…

Depends on where you declare it. Declare a local in the top of your file and you’ll be able to use it throughout the file.

That makes 3, raBBish :ninja: