• Gmod Brainfuck Interpreter
    43 replies, posted
[IMG]http://filesmelt.com/dl/gmbf01.png[/IMG][IMG]http://img822.imageshack.us/img822/7599/gmbf01.png[/IMG] 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: [code] 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
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 [URL="http://en.wikipedia.org/wiki/Brainfuck"]look it up[/URL] - it messes with you, but in a good way :smile:
Fun fact: it's the language with the smallest compiler: 240 bytes!
[QUOTE=Dlaor;22906332]Fun fact: it's the language with the smallest [b]interpreter[/b]: 240 bytes![/QUOTE] A compiler would take some more bytes.
[quote=Wikipedia]Several brainfuck compilers have been made smaller than 200 bytes.[/quote] [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.
[QUOTE=Overv;22906353]A compiler would take some more bytes.[/QUOTE] Then [url=http://esoteric.voxelperfect.net/wiki/Brainfuck]this page[/url] lied to me. [quote]Brainfuck was invented by Urban Müller in 1993, in an attempt to make a language for which he could write the smallest possible compiler for the Amiga OS, version 2.0. He managed to write a [highlight]240-byte compiler[/highlight]. The language was inspired by False, which had a 1024-byte compiler. Müller chose to name the language brainfuck (with the initial letter in lower case, although it is now often capitalised). It is not known to what extent Müller was aware of or influenced by Böhm's language P'' published in 1964, of which brainfuck can be considered a minor variation.[/quote]
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:
[QUOTE=DarKSunrise;22906688]You can add multiple elements onto one sheet if you use a DForm for each sheet and parent the controls to that.[/QUOTE] Ah, Thank you! I thought it would be something like that. [QUOTE=raBBish;22906740]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:[/QUOTE] 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 :/
You can define locals at the top of your file by just doing [lua]local textbox1 local textbox2 -- code[/lua] [QUOTE=Dlaor;22906622]Then [url=http://esoteric.voxelperfect.net/wiki/Brainfuck]this page[/url] lied to me.[/QUOTE] [quote=http://esoteric.voxelperfect.net/wiki/Compiler]A compiler is a program that converts code written in one language into another language[/quote] 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 < 32768 && (p[pc] = getc(stream)) != EOF; pc++) prog_len++; pc = 0; fclose(stream); for(xc = 0; xc < 32768; xc++) x[xc] = 0; xc = 0; for(pc = 0; pc < 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(); // '>' else if (p[pc] == 62) xc++; // '<' else if (p[pc] == 60) xc--; // '[' else if (p[pc] == 91) { if (x[xc] == 0) { pc++; while (l > 0 || p[pc] != 93) { if (p[pc] == 91) l++; if (p[pc] == 93) l--; pc++; } } } // ']' else if (p[pc] == 93) { pc--; while (l > 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.)
[QUOTE=r0b0tsquid;22906848]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 :/[/QUOTE] [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 [url=http://en.wikipedia.org/wiki/Scope_(programming)]scope.[/url] Also, two ninjas in one thread in under 10 minutes...
Oh god, I love you guys :glomp: Anyway, my brother's home from work, so I can't get to a computer with Gmod until tomorrow. Probably in the afternoon, 'cause I'm rowing tomorrow morning. I'll carry on replying on my phone, but I'll probably go to bed soon anyway. [editline]09:50PM[/editline] I still don't get it. How does declaring your locals at the top stop them from going out of scope when you go into a different function?:raise:
It just works like that. That's how Lua is written. If you define a local inside a function/if-statement/loop, it will be available until it reaches the end of that thing (if you define a function inside a statement the locals are also available in the function). If you define a local on the start of the file, it will be available for the whole file. [lua]local everywhere = "foobar"; if(true) then local onstatement = "foobar"; print(everywhere, onstatement); -- foobar, foobar function test() print(everywhere, onstatement); end test(); -- foobar, foobar end test(); -- foobar, foobar print(everywhere, onstatement); -- foobar, nil[/lua]
Ah, makes sense now. Anyhoo, I'm off to bed now - expect more content tomorrow! :smile:
I'd like to see someone try to make something useful in brainfuck. It's pretty much there just to be there.
Whitespace interpreter please?
[QUOTE=nicatronTg;22916400]Whitespace interpreter please?[/QUOTE] We'll see. I'll be back around two, so when I've finished some other stuff then whitespace and Ook support may be added.
Come to think of it, you should write an interpreter for just about every odd programming language out there, maybe allow you to write scripts in those languages, like lolcode.
[quote]As the name suggests, brainfuck programs tend to be difficult to comprehend.[/quote] Theres programs made from brainfuck? Anyways, thats just weird.
[QUOTE=nicatronTg;22918076]Come to think of it, you should write an interpreter for just about every odd programming language out there, maybe allow you to write scripts in ythose languages, like lolcode.[/QUOTE] I'll have a go. If you give me a list of languages you want implemented, then I'll see what I can do - starting with whitespace and Ook, because they should both be pretty easy to jit into brainfuck. Anyway, I'm on my way home now, so after I've had a shower I can start work on this thing again :dance:
I'll just leave this here: :v: [img]http://img205.imageshack.us/img205/6982/gmbf02.png[/img] and [img]http://img341.imageshack.us/img341/4308/gmbf03.png[/img] Ook interpreter is having some weird bugs, I'll post some pics of that when I get it working.
[QUOTE=Overv;22906969] -snip- [/QUOTE] Thats an interpreter not a compiler lol OT: This could be pretty useful for having a joke programming contest or something on a server? who could make the most complicated brainfuck program hehe..
Sorry, you need to Log In to post a reply to this thread.