Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
sir Kamba

Brainfuck compiler

Recommended Posts

Brainfuck compiler for opencomputer

 

Some time ago i was informed of a programming language called brainfuck, and thought it would be fun make a compiler for opencomputer.

Don't know what brainfuck is, then look here: http://en.wikipedia.org/wiki/Brainfuck

 

I honestly don't see this as something that is very usefull under normal circumstances, but it gives a "easy" way to program microcontrollers.

It is an interresting new and different look on programming (1993 beeing new).

If you find a bug or would request a new function/alter an existing function please let me know.

 

 

Usage

brainfuck <[path] inputfile> <[int] memorysize> <[path] outputfile> <[normal/debug/eeprom] compiler type>

 

inputfile:            Is the file with the brainfuck code that you want to compile.

memorysize:     Defines how many memorycells the program is allowed to use.

outputfile:          Is the file the compilator will save the comiled code as.

compiler type:   Defines how the compiler will compile the code:

                              normal:   this is the default way of compiling the code. it will run on openos

                              debug:    In this mode the compiled code will run slower and print out some key values during execution.

                              eeprom:  this mode allows the compiled code to be compiled and run on an eeprom, this was designed for microcontrollers, but will run i a computer.

                                             Support for drones might be added in the future. 

                                             The eeprom mode is also optimized in terms of size, but it sacrefices readabilit (if there were some to begin with).

                                            it compiles to an average of 5bytes per brainfuck command.

 

 

requirement:

 

At least 256KB of memory to execute the code. More is better but not necessary.

A tier 1 redstone card. (tier 2 card will work, but does not give any additional functionality)

For normal and debug mode: a gpu + a monitor.(any tier) 

 

 

documentation:

 

All standard brainfuck commands are supported and working, but on a eeprom build ',' will read from a output placeholder variable and '.' will write to said variable.

That beeing said ALL* existing brainfuck code should work with this compiler.

In addition to the forementioned cells this dialect of brainfuck has a holding variable for ease of use.

Also I added extra commands for added functionality and again for ease of use.

 

Due to the way lua is programmed this is a valid code [++>}  this aswell  ?+;B] even though they do not use the correct terminators.

 

*ALL: due to the lack of code to test ALL might not mean all, but all code i got to test worked.

 

 

Equivalent lua code and full list of commands:

	
        mem={}   --the "cells"
        mem.ptr  --the pointer
        mem.hold --the holding variable

        [">"]=mem.ptr=(mem.ptr+1)%mem.size
	["<"]=mem.ptr=(mem.ptr-1)%mem.size
	["+"]=mem[mem.ptr]=mem[mem.ptr]+1
	["-"]=mem[mem.ptr]=mem[mem.ptr]-1
	["."]=mem:output()  --prints char(mem.ptr)
	[","]=mem:input()   --gets a number from user and stores it in mem[mem.ptr]
	["["]=while (mem[mem.ptr]~=0) do
	["]"]=end
	["{"]=while mem[mem.ptr]~=mem.hold do
	["}"]=end
	["B"]=break
	["#"]=mem.ptr=mem[mem.ptr]%mem.size
	["="]=mem[mem.ptr]=mem.ptr
	["M"]=mem.hold=mem[mem.ptr]
	["L"]=mem[mem.ptr]=mem.hold
	["A"]=mem.hold=(mem.hold+mem[mem.ptr])
	["S"]=mem.hold=(mem.hold-mem[mem.ptr])
	["T"]=note.play((mem[mem.ptr]%1980)+20,(mem[mem.hold])/100)
	["R"]=mem.hold=rs.getInput(mem[mem.ptr]%6)
	["E"]=rs.setOutput(mem[mem.ptr]%6,mem[(mem.ptr-1)%mem.size])
	["&"]=mem.hold=tonum(tobool(mem.hold) and tobool(mem[mem.ptr])) --logic and between mem.hold and mem[mem.ptr]
	["|"]=mem.hold=tonum(tobool(mem.hold) or  tobool(mem[mem.ptr])) --logic or between mem.hold and mem[mem.ptr]
	["!"]=mem.hold=tonum(not tobool(mem.hold))                      --logic not  mem.hold = not mem.hold
	["?"]=if tobool(mem[mem.ptr]) then
	[":"]=else
	[";"]=end
        [ n ]=mem[mem.ptr]=n   -- where n i a positive integer
        [ X ] where X is any other character X is ignored

examples:

 

here is a code that adds the redstone signal on the left and right side and outputs it to the front, and exits when it gets a sigal from behind.

<0>0E1E2E3E4E5E

1[
0RL>
1RL>
2RL>
3RL>
4RL>
5RL>
<<M>>L<M>AL>3E

0#<++RL?B;0M#1]

Here is the compiled file: compiled.txt

 

 

The Compiler is here: brainfuck.txt

 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.