How to compile a program in Befunge-93?
How do I save and compile a program in Be开发者_C百科funge-93?
What file extension do I need to save the file as (like fileName.what
)?
Then how do I compile it?
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.
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;
}
}
}
精彩评论