brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

软件和网站开发以及相关技术探讨
回复
头像
cjxgm
帖子: 1952
注册时间: 2010-04-23 20:40
系统: Arch Linux
来自: 浙江·杭州
联系:

brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#1

帖子 cjxgm » 2012-09-02 16:52

忙了两个礼拜,终于用 perl :em70 写成了
有类似于C语言的语法,支持函数与递归!
欢迎围观:https://github.com/cjxgm/brainsuck
图片

Q:这是啥?
A:把一个类似C语言的语言编译成 brainfuck 语言的编译器。

Q:编译成 brainfuck 有意义吗?
A:有。至少你可以用它来装13。用高级语言写个程序,然后轻松转成 brainfuck,然后分享给别人……别人一定会说:Is your brain fucked?

Q:真的类似C语言?
A:嗯嗯,还是有点小区别的。看示例程序:
[c]# test

main()
{
@ x;

while (x != 'x') {
"what? ";
x = getc();
while (x == 10 || x == 13) x = getc(); # skip newline

if (x == 'a') "Hi!";
else if (x-'0' == 2) "Wow!";
else {"> "; putc(x);}
putc(10); # newline
}
}[/c]

Q:编译以后是什么样的?
A:[c]>>>+[<[->-]>[-<>+>->]<+[<[->-]>[-<<[->+>+<<]>>[-<<+>>]++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++[-<->]<[[-]>+<]+>[-<+>>]<[++>]->]<+[
<[->-]>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++
+.[-],<[-]>[-<+>]++++>->]<+[<[->-]>[-<<[-]>>+<<->]<+[<[->-]>[-<<[->+>+
<<]>>[-<<+>>]++++++++++[-<->]<[[-]>+<]+>[-<->]<[[-]>+<]>[-<+>]<<[->>+>
+<<<]>>>[-<<<+>>>]+++++++++++++[-<->]<[[-]>+<]+>[-<->]<[[-]>+<]>[-<+>]
<[-<+>]<[[-]>+<]+>[-<++++>>]<[+++++>]->]<+[<[->-]>[-<,<[-]>[-<+>]++++>
->]<+[<[->-]>[-<<[->+>+<<]>>[-<<+>>]++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[-<->]<
[[-]>+<]+>[-<->]<[[-]>+<]+>[-<++++++++>+++++++>>]<[++++++++>++++++++>]
->]<+[<[->-]>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++
++++++++++++++++++++++++.[-]->]<+[<[->-]>[-<<<[->>+>+<<<]>>>[-<<<+>>>]
++++++++++++++++++++++++++++++++++++++++++++++++[-<->]++[-<->]<[[-]>+<
]+>[-<->]<[[-]>+<]+>[-<+++++++++++>++++++++++>>]<[+++++++++++>++++++++
+++>]->]<+[<[->-]>[-<++++++++++.[-][-]+>->]<+[<[->-]>[-<++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++.[
-]->]<+[<[->-]>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++.[-]++++++++++++++++++++++++++++++++.[-]<<<[->>>+>+<<<<]>>>>
[-<<<<+>>>>]<.[-][-]->]<+[<[->-]>[-<->]<+[-]]]]]]]]]]]]]+>>[-<<->>]<<]
<<[/c]
Clanjor Prods. | Develop for Developers. (C++, Lua) | 作曲编曲 | 实时渲染引擎
JiangHui
论坛版主
帖子: 21308
注册时间: 2007-08-02 20:29
系统: 窓辺とうこ

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#2

帖子 JiangHui » 2012-09-02 17:08

good job
装逼利器
头像
ee.zsy
帖子: 41
注册时间: 2010-05-09 16:10
来自: 微风的河岸
联系:

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#3

帖子 ee.zsy » 2012-11-06 17:27

:em20 好可怕

----

:em03 模仿着写了一下,刚开个头,特性还没啥

代码: 全选

#lang scheme
(define (o x) 
  (define (op name identity)
    (letrec ((self
              (case-lambda
                (() (list identity))
                ((x) (self identity x))
                ((x y) (list x y name))
                ((x y . z)
                 (append 
                  (self x y)
                  (apply append (map (lambda (x) (list x name)) z))))
                )))self))
  (let ((+ (op '+ 0))
        (- (op '- 0))
        (* (op '* 1))
        (/ (op '/ 1)))
    (apply(cadr (assoc(car x)`((+,+)(-,-)(*,*)(/,/))) )(cdr x))))
(define (p x) (if (pair? x) (o (cons (car x) (map p (cdr x)))) x))
(define (q x) (apply append (map (lambda (x) (case x
                                               ((+) '("[-<+>]<"))
                                               ((-) '("[-<->]<"))
                                               ((*) '("<[->" "[->+>+<<]" ">>[-<<+>>]<<" "<]" ">[-]" ">[-<<+>>]<<"))
                                               ((/) '("<[>" "[->+>+<<]" ">>[-<<+>>]<" "[-<<->>]>>+<<<" "<]" ">[-]" ">>>[-<<<<+>>>>]<<<<"))
                                               (else (list ">[-]" (make-string x #\+) ""))
                                               )) x)))
(define (flat x)(if (pair? x) (apply append (map flat x)) (list x)))
(define (compile x)(list (apply string-append (q (flat (p x)))) (eval x (make-base-namespace))))
(compile '(* 2 (/ 8 2 2) 3 (+ 1 2) 1))
运行得到

代码: 全选

>[-]++>[-]++++++++>[-]++<[>[->+>+<<]>>[-<<+>>]<[-<<->>]>>+<<<<]>[-]>>>[-<<<<+>>>>]<<<<>[-]++<[>[->+>+<<]>>[-<<+>>]<[-<<->>]>>+<<<<]>[-]>>>[-<<<<+>>>>]<<<<<[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[-<<+>>]<<>[-]+++<[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[-<<+>>]<<>[-]+>[-]++[-<+>]<<[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[-<<+>>]<<>[-]+<[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[-<<+>>]<<
执行

代码: 全选

http://www.iamcal.com/misc/bf_debug/
说明

代码: 全选

当前刚刚仅支持 +-*/ 的说,而且除法不整除时会执行错误(结果异常或者不停机)
TODO

代码: 全选

1.好想把 defun/funcall 给搞定...
2.把整数除法给实现对了...
3.把乘法中的那个减号移到循环结束的地方...
上次由 ee.zsy 在 2012-11-07 16:07,总共编辑 3 次。
头像
liu滔
帖子: 7214
注册时间: 2010-10-09 23:01

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#4

帖子 liu滔 » 2012-11-06 20:02

从微博过来膜拜一下 :em70
头像
reverland
帖子: 1317
注册时间: 2011-11-26 15:57
系统: windows xp
联系:

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#5

帖子 reverland » 2012-11-06 20:56

从微薄过来仰望下 :em03
托在github上的jekyll博客
Always Look on the Bright Side of Life
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#6

帖子 枫叶饭团 » 2012-11-06 20:58

蛋碎.exe
头像
ee.zsy
帖子: 41
注册时间: 2010-05-09 16:10
来自: 微风的河岸
联系:

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#7

帖子 ee.zsy » 2012-11-07 17:27

:em01 整数除法搞定了,虽然是取的上界,现在四则运算齐全了哈哈...

代码: 全选

#lang racket
(define (assert e m)(if (not e)(error m)#t))
(define (o x) 
  (define (op name identity)
    (letrec ((self
              (case-lambda
                (() (list identity))
                ((x) (self identity x))
                ((x y) (list x y name))
                ((x y . z)
                 (append 
                  (self x y)
                  (apply append (map (lambda (x) (list x name)) z))))
                )))self))
  (let ((+ (op '+ 0))
        (- (op '- 0))
        (* (op '* 1))
        (/ (op '/ 1)))
    (apply(cadr (or (assoc(car x)`((+,+)(-,-)(*,*)(/,/))) #f))(cdr x))))
(define (p x) (if (pair? x) (o (cons (car x) (map p (cdr x)))) x))
(define (str . xs)
  (apply string-append (map (lambda (x)(cond
                                         ((char? x) (make-string 1 x))
                                         (else x))) xs)))
(define (q x)
  (define inc #\+)
  (define dec #\-)
  (define rht #\>)
  (define lft #\<)
  (define next #\>)
  (define push #\>)
  (define pop #\<)
  (define (rep x n)
    (make-string (abs n) (if (>= n 0) x 
                             (cadr (assoc x '((#\< #\>)(#\> #\<)(#\+ #\-)(#\- #\+)))))))
  (define dup (string-append "[->+>+<<]" ">>[-<<+>>]<"))
  (define (cpy to) (str (tms (rep rht to) inc next inc pop (rep lft to)) (wth (+ 1 to) (mov (- 0 1 to)))))
  (define clr "[-]")
  (define (mov offset) (string-append "[-" (rep #\> offset) "+" (rep #\> (- offset)) "]"))
  (define (mov2 offset times) (string-append "[-" (rep #\> offset) (rep #\+ times) (rep #\> (- offset)) "]"))
  (define (wth offset . code) (string-append (rep #\> offset) (apply str code) (rep #\< offset)))
  (define (tms . code) (string-append "[" (apply str code) "-]"))
  (define (if-pos . code) (string-append "[" (apply str code) "[-]]"))
  (define (lop . code) (string-append "[" (apply str code) "]"))
  (apply append (map (lambda (x) (case x
                                   ((nop) '(""))
                                   ((dup) `(,(cpy 1) ">"))
                                   ((dup) '("[->+>+<<]" ">>[-<<+>>]<"))
                                   ((zro) '(">[-]"))
                                   ((clr) '("[-]"))
                                   ((+) `(,(mov -1) "<"))
                                   ((-) `(,(mov2 -1 -1) "<"))
                                   ((*) `("<" ,(tms (wth 1 dup "<")) ,(wth 1 clr) ,(wth 2 (mov -2))))
                                   ((/) `("<" ,(lop (wth 1 dup (tms (wth -2 (cpy 5) (wth 5 (if-pos (wth -5 "-"))))) "<") (wth 4 "+") )  ,(wth 1 clr) ,(wth 4 (mov -4))))
                                   ;((/) `("<" ,(lop (wth 1 dup (tms (wth -2 "-"))  "<") (wth 4 "+") )  ,(wth 1 clr) ,(wth 4 (mov -4))))
                                   (else (list ">[-]" (make-string x #\+) "#"))
                                   )) x)))
(define (flat x)(if (pair? x) (apply append (map flat x)) (list x)))
(define (compile x) (list (apply str (q (flat (p x)))) (eval x (make-base-namespace))))

(compile '(* 2 (/ 8 2 2) 3 (+ 1 2) 1))
(compile '(/ 3 2 1))
还有一个略复杂一些的例子 http://pastebin.com/ppzWXECh
上次由 ee.zsy 在 2012-11-11 15:59,总共编辑 1 次。
头像
liu滔
帖子: 7214
注册时间: 2010-10-09 23:01

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#8

帖子 liu滔 » 2012-11-07 18:08

楼上好强 :em20
jtshs256
帖子: 22323
注册时间: 2010-07-19 21:41
系统: OS X

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#9

帖子 jtshs256 » 2012-11-07 18:21

那个ee没来,来了这个ee
躺平
头像
liu滔
帖子: 7214
注册时间: 2010-10-09 23:01

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#10

帖子 liu滔 » 2012-11-07 20:37

这个ee很厉害的 :em03
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#11

帖子 枫叶饭团 » 2012-11-07 21:33

又来膜拜锤子大大:)
头像
灰色小狼
帖子: 4585
注册时间: 2008-12-06 10:38
系统: Arch

Re: brainsuck:高级语言到 brainfuck 编译器(支持函数与递归)

#12

帖子 灰色小狼 » 2012-11-07 21:38

your left brain has nothing right
your right brain has nothing left
回复