为了理解这种语言的流程,我重载了一些C#操作符,以获得Brainfuck符号的伪版本。
我有这个版本:
void Main()
{
/*
* Write `Hello World!`
* using operators overloading for brainfuck symbols
*/
var _ = new brainfuck();
_ = + + + + + + + + + + _ ; // set cell #0 to 10
while (_)
{
_ = + + + + + + + (_ > 1) ; // add 7 to #1
_ = + + + + + + + + + + (_ > 1) ; // add 10 to #2
_ = + + + (_ > 1) ; // add 3 to #3
_ = + (_ > 1) ; // add 1 to #4
_ = - (_ < 1 < 1 < 1 < 1) ; // decrement 0
}
_ = ~ + + (_ > 1) ; // write 'H'
_ = ~ + (_ > 1) ; // write 'e'
_ = ~ + + + + + + + _ ; //write 'l'
_ = ~ _ ; //write 'l'
_ = ~ + + + _ ; // write 'o'
_ = ~ + + (_ > 1) ; // write ' '
_ = ~ + + + + + + + + + + + + + + + (_ < 1 < 1) ; // write 'W'
_ = ~ (_ > 1) ; // write 'o'
_ = ~ + + + _ ; // write 'r'
_ = ~ - - - - - - _ ; // write 'l'
_ = ~ - - - - - - - - _ ; // write 'd'
_ = ~ + (_ > 1) ; // write '!'
_ = ~ (_ > 1) ; // write '\n'
}
public class brainfuck
{
private List<int> cells;
private int currentCell;
public brainfuck()
{
cells = new List<int>{0};
}
public static brainfuck operator + (brainfuck bf)
{
bf.plus();
return bf;
}
public static brainfuck operator - (brainfuck bf)
{
bf.minus();
return bf;
}
public static brainfuck operator > (brainfuck bf, int pos = 1)
{
bf.right();
return bf;
}
public static brainfuck operator < (brainfuck bf, int pos = 1)
{
bf.left();
return bf;
}
public static brainfuck operator ~ (brainfuck bf)
{
bf.write();
return bf;
}
public static brainfuck operator ! (brainfuck bf)
{
bf.read();
return bf;
}
public static implicit operator bool (brainfuck bf)
{
return bf.has_data();
}
private int plus()
{
return ++cells[currentCell];
}
private int minus()
{
return --cells[currentCell];
}
private void right()
{
if (currentCell + 1 == cells.Count)
cells.Add(0);
currentCell++;
}
private void left()
{
if (currentCell > 0)
currentCell--;
}
private int current()
{
return cells[currentCell];
}
private void write()
{
Console.Write((char)current());
}
private void read()
{
var input = Console.ReadLine();
int value = Int32.TryParse(input, out value) ? value : 0;
current(value);
}
private bool has_data()
{
return current() > 0;
}
private void current(int value)
{
cells[currentCell] = value;
}
public override string ToString()
{
return String.Join(",",cells);
}
}我不得不使用[]命令对使用while语句进行欺骗,从Brainfuck阅读器的角度来看,流程非常奇怪,因为<,>命令在原来的Hello中在+,-之前!例如,与C#程序是相反的。
你觉得我能得到比现在更接近Brainfuck的版本吗?
发布于 2016-12-08 04:37:56
您可以使用[]命令对稍微靠近一些。
实际上,这是非常琐碎的:
向brainfuck类(public void this[Action bf])添加一个索引器,并将while循环放在其中。
索引器看起来应该是:
public brainfuck this[Action action]
{
get
{
while (this)
{
action.Invoke();
}
return this;
}
}你可以称之为:
_ = _[() => {
_ = + + + + + + + (_ > 1) ; // add 7 to #1
_ = + + + + + + + + + + (_ > 1) ; // add 10 to #2
_ = + + + (_ > 1) ; // add 3 to #3
_ = + (_ > 1) ; // add 1 to #4
_ = - (_ < 1 < 1 < 1 < 1) ; // decrement 0
}];用在后部是不是很疼?是。是在Brainfuck精神里吗?是。
它仍然不是真正的brainfuck语法,但是您将无法使用C#实现。
在那之后,你这里真的很乱,需要清理。
第一:资本化。在C#中,我们使用PascalCase公共成员和类型(和所有方法),以及camelCase私有和本地成员和类型。
public class Brainfuck
private bool HasData()因为"Brainfuck“是一个词,所以”操“中的"F”不是大写的。
operator >和operator <中的可选参数默认值根本不起作用,请删除它们。
public static brainfuck operator > (brainfuck bf, int pos)
{
bf.right();
return bf;
}
public static brainfuck operator < (brainfuck bf, int pos)
{
bf.left();
return bf;
}永远使用牙套,即使你不需要它们。
private void Right()
{
if (currentCell + 1 == cells.Count)
{
cells.Add(0);
}
currentCell++;
}在任何地方使用int而不是Int32。
int value = int.TryParse(input, out value) ? value : 0;一旦我们应用了所有这些更改,修复了一些间隔,我们就得到了一个看起来不错的类:
public class Brainfuck
{
private List<int> cells;
private int currentCell;
public Brainfuck()
{
cells = new List<int>{0};
}
public Brainfuck this[Action action]
{
get
{
while (this)
{
action.Invoke();
}
return this;
}
}
public static Brainfuck operator + (Brainfuck bf)
{
bf.Plus();
return bf;
}
public static Brainfuck operator - (Brainfuck bf)
{
bf.Minus();
return bf;
}
public static Brainfuck operator > (Brainfuck bf, int pos)
{
bf.Right();
return bf;
}
public static Brainfuck operator < (Brainfuck bf, int pos)
{
bf.Left();
return bf;
}
public static Brainfuck operator ~ (Brainfuck bf)
{
bf.Write();
return bf;
}
public static Brainfuck operator ! (Brainfuck bf)
{
bf.Read();
return bf;
}
public static implicit operator bool (Brainfuck bf)
{
return bf.HasData();
}
private int Plus()
{
return ++cells[currentCell];
}
private int Minus()
{
return --cells[currentCell];
}
private void Right()
{
if (currentCell + 1 == cells.Count)
{
cells.Add(0);
}
currentCell++;
}
private void Left()
{
if (currentCell > 0)
{
currentCell--;
}
}
private int Current()
{
return cells[currentCell];
}
private void Write()
{
Console.Write((char)Current());
}
private void Read()
{
var input = Console.ReadLine();
int value = int.TryParse(input, out value) ? value : 0;
Current(value);
}
private bool HasData()
{
return Current() > 0;
}
private void Current(int value)
{
cells[currentCell] = value;
}
public override string ToString()
{
return String.Join(",",cells);
}
}我们的“”代码看起来是:
void Main()
{
/*
* Write `Hello World!`
* using operators overloading for brainfuck symbols
*/
var _ = new Brainfuck();
_ = + + + + + + + + + + _ ; // set cell #0 to 10
_ = _[() => {
_ = + + + + + + + (_ > 1) ; // add 7 to #1
_ = + + + + + + + + + + (_ > 1) ; // add 10 to #2
_ = + + + (_ > 1) ; // add 3 to #3
_ = + (_ > 1) ; // add 1 to #4
_ = - (_ < 1 < 1 < 1 < 1) ; // decrement 0
}];
_ = ~ + + (_ > 1) ; // write 'H'
_ = ~ + (_ > 1) ; // write 'e'
_ = ~ + + + + + + + _ ; //write 'l'
_ = ~ _ ; //write 'l'
_ = ~ + + + _ ; // write 'o'
_ = ~ + + (_ > 1) ; // write ' '
_ = ~ + + + + + + + + + + + + + + + (_ < 1 < 1) ; // write 'W'
_ = ~ (_ > 1) ; // write 'o'
_ = ~ + + + _ ; // write 'r'
_ = ~ - - - - - - _ ; // write 'l'
_ = ~ - - - - - - - - _ ; // write 'd'
_ = ~ + (_ > 1) ; // write '!'
_ = ~ (_ > 1) ; // write '\n'
}总的来说,还不算太糟。我是不会这么做的,但那是因为我给C#播音器写了一个Brainfuck。干得好。:)
https://codereview.stackexchange.com/questions/139971
复制相似问题