如何在Befunge-93中保存和编译程序?
保存文件需要什么文件扩展名(如fileName.what
)?
那我该怎么编译呢?
答案 0 :(得分:5)
Befunge通常是一种解释性语言。事实上,它被设计为难以编译,因为程序是自我修改的(但这并没有阻止人们尝试编写编译器)。
您应该随意保存文件(通常使用.bf
扩展名)并运行解释器(bef),将文件名作为命令行参数传递。
答案 1 :(得分:0)
由于它是一种解释语言,因此通常不会被编译。下面是一个C#Befunge 93解释器,它将解释为字符串的数组[]。
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;
}
}
}