BFコンパイラー
以下のようにonelinerで可能です。
ただしLanguage::BF 0.03が必要です。
CodeRepos経由
で、
- svn co svn.coderepos.org/share/lang/perl/Language-BF
- cd Language-BF/trunk
- perl Makefile.PL
- make install
するか、CPANにVersion 0.03が現れるのをお待ち下さい。
Dan the Brainf.cker
1 2 3 4 | perl -MLanguage::BF \
-e 'print Language::BF->new_from_file(shift)->as_perl' t/hello.bf \
| perl
Hello World!
|
Posted feedbacks - Scala
ひさびさの一番かな? 入力を文字列にしたい場合なんかは Console.withInを使うとできます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import scala.io.Source.fromFile
object BF{
def main(args:Array[String]) = {
if(args.size == 0) print("scala BF [sourcecode filename]")
else
args.foreach{f=> (new Machine).interpret(fromFile(f).mkString("")) }
}
}
object Machine {
final val MEM = 0xffffff
final val VAL = 0xff
}
class Machine{
var _p = 0
val _mem = new Array[int](Machine.MEM)
def p_=(v:int) = _p = v&Machine.MEM
def p = _p
def mem_=(v:int) = _mem(p)=v&Machine.VAL
def mem = _mem(p)
def abort = error("Missing corresponding parenthesis.")
def interpret(code:String):unit = interpret(code.toArray)
def interpret(code:Array[char]):unit = {
val (s,m) = ((List[int](), List[(int,int)]()) /: code.zipWithIndex){
(r,c) => c._1 match {
case '[' => (c._2::r._1, r._2)
case ']' => r._1 match {
case x::xs => (xs, (x,c._2)::(c._2,x)::r._2)
case _ => abort
}
case _ => r
}}
if(s.size > 0) abort
val parenMap = Map(m:_*)
var i = -1;while({i=i+1;i<code.size}) code(i) match {
case '>' => p = p+1
case '<' => p = p-1
case '+' => mem = mem+1
case '-' => mem = mem-1
case '.' => print(mem.asInstanceOf[char])
case ',' => mem = readChar.asInstanceOf[int]
case '[' if mem == 0 => i = parenMap(i)
case ']' if mem != 0 => i = parenMap(i)
case _ => ()
}
}
}
|
題意を読み間違えてたので。 こっちのほうがかなり楽です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import scala.io.Source.fromFile
object BFC{
def main(args:Array[String]) = {
if(args.size == 0){
println("Usage:scala BFC [sourcecode filename]")
}else{
println("""
object Machine {
final val MEM = 0xffffff
final val VAL = 0xff
}
class Machine{
var _p = 0
val _mem = new Array[int](Machine.MEM)
def p_=(v:int) = _p = v&Machine.MEM
def p = _p
def mem_=(v:int) = _mem(p)=v&Machine.VAL
def mem = _mem(p)
def eval = {
""")
val code = fromFile(args(0)).mkString("").toList
if(code.count('['==_) != code.count(']'==_)) {
error("Missing corresponding parenthesis.")
}
code.foreach(c=>println(c match {
case '>' => "p = p+1"
case '<' => "p = p-1"
case '+' => "mem = mem+1"
case '-' => "mem = mem-1"
case '.' => "print(mem.asInstanceOf[char])"
case ',' => "mem = readChar.asInstanceOf[int]"
case '[' => "while(mem != 0){"
case ']' => "}"
case _ => ""
}))
println("}};(new Machine).eval;")
}
}
}
|





dankogai
#3886()
Rating0/2=0.00
「どう書く?」でまだ出ていないのが不思議なお題。それがBF処理系。 ここでは、BFで書かれたソースを、同じ言語に変換するコンパイラーを募集します。
私自身、すでにPerlとJavaScriptに関しては http://blog.livedoor.jp/dankogai/archives/50545151.html でやっているのですが、他の言語バージョンも是非見たいので。
Dan the Brainf.ucker
see: Brainfuck - Wikipedia
1 reply [ reply ]