How do I save and compile a program in Befunge-93?
What file extension do I need to save the file as (like fileName.what
)?
Then how do I compile it?
Asked
Active
Viewed 1,210 times
1

Quuxplusone
- 23,928
- 8
- 94
- 159

David
- 14,569
- 34
- 78
- 107
2 Answers
5
Befunge is typically an interpreted language. In fact it was designed to be difficult to compile because the programs are self-modifying (but that hasn't stopped people from trying to write a compiler).
You should save the file however you like (a .bf
extension is typically used) and run the interpreter (bef), passing the filename as a command line argument.
You can see the usage instructions in the source code for the interpreter.

ggorlen
- 44,755
- 7
- 76
- 106

Mark Byers
- 811,555
- 193
- 1,581
- 1,452
-
do i have an interpreter on my computer automatically though or do i have to download one? Where do i do this at? – David Apr 08 '10 at 20:45
-
@David: The reference implementation is found here: http://catseye.tc/projects/bef/. You download the source files and compile it using your favorite C compiler. There are some notes here: http://catseye.tc/about/prereqs.html#ansi_c – Mark Byers Apr 08 '10 at 20:47
-
@David: And I hope you are aware that Befunge is an esoteric / joke language? ;-) – Mark Byers Apr 08 '10 at 20:50
-
i am aware but i thought it might be fun to learn. I don't understnad how to use the intepreter. Is that source code you linked me to something i have to compile and run then? how do im compile it on my computer? – David Apr 08 '10 at 20:53
-
@David: Yes, you compile the source to get an executable called `bef`, or `bef.exe` on Windows, and you can run that from the command line to execute your Befunge source. – Mark Byers Apr 08 '10 at 20:58
-
how do i compile it though? i only know how to compile .java files with javac. im sort of new at this. – David Apr 08 '10 at 21:00
-
Nowadays there's good javascript implementations so that you can experiment with Befunge in your browser http://www.bedroomlan.org/tools/befunge-playground#prog=hello,mode=edit – Demur Rumed Apr 24 '16 at 18:27
0
As it is an interpreted language, it usually won't be compiled. A C# Befunge 93 interpreter below, that will read code as an array[] of strings.
string befunge93(string[] p) {
for (int i=0;i<p.Length;i++)
p[i]=p[i].Replace("\"","ª"); // REMOVE QUOTATIONS TO MAKE THINGS EASIER
int l=0; int c=0; // current line and colum
var stack=new Stack<int>(new int[20000]); // initialize stack with many zeroes.
string res=""; // output limited to 1000 chars
int limit=100000; // max commands limited to 10^5
bool smode=false; // string mode
char direction='>';
//-------- MAIN LOOP ---------------
while (res.Length < 1000 && limit--> 0 )
{
var ch=p[l][c];
if (ch=='ª') // ", TOGGLE STRING MODE
smode = !smode;
else if (smode) // STRING MODE => PUSH CHAR
stack.Push((int)ch);
else if (ch==',') // OUTPUT CHAR FROM STACK
res+=(char)stack.Pop();
else if (new Regex(@"[><^v]").IsMatch(""+ch)) // CHANGE DIRECTION
direction = ch;
else if (new Regex(@"\d").IsMatch(""+ch)) // CHANGE DIRECTION
stack.Push(ch-'0');
else if (ch=='*') // MULTIPLICATION
stack.Push(stack.Pop()*stack.Pop());
else if (ch=='+') // SUM
stack.Push(stack.Pop()+stack.Pop());
else if (ch=='`') // GREATER THEN
stack.Push(stack.Pop()>=stack.Pop()?0:1);
else if (ch=='!') // NOT
stack.Push(stack.Pop()==0?1:0);
else if (ch=='.') // OUTPUT NUMBER
res+=$"{stack.Pop()} ";
else if (ch=='#') // JUMP NEXT COMMAND
move();
else if (ch=='$') // DISCARD ITEM FROM STACK
stack.Pop();
else if (ch=='\\') // SWAP
{ var a=stack.Pop(); var b=stack.Pop(); stack.Push(a); stack.Push(b); }
else if (ch=='%') // modulo
{ var a=stack.Pop(); var b=stack.Pop(); stack.Push(b%a); }
else if (ch=='-') // MINUS
{ var a=stack.Pop(); var b=stack.Pop(); stack.Push(b-a); }
else if (ch=='/') // DIVISION
{ var a=stack.Pop(); var b=stack.Pop(); stack.Push(b/a); }
else if (ch==':') // DUPLICATE
stack.Push(stack.First());
else if (ch=='_' || ch=='|') // CONDITIONALS: MOVE IF STACK ZERO, LEFT OTHERWISE
{
var last = stack.Pop();
if (ch=='_') direction=last==0?'>':'<'; // right if stack was zero. Left otherwise
else direction=last==0?'v':'^'; // ch=='|'
}
else if (ch==' '); // SPACE - DO NOTHING.
else return res;
move();
}
return res;
void move() // move cursor
{
switch(direction)
{
case '>':
if (++c==p[0].Length) c=0; break;
case '<':
if (--c==-1) c=p[0].Length-1; break;
case 'v':
if (++l==p.Length) l=0; break;
case '^':
if (--l==-1) l=p.Length-1; break;
default: break;
}
}
}

Zuabros
- 252
- 1
- 5