李亚橙

李亚橙的博客

他的个人主页  他的博客

我用parsec写的解释器

李亚橙  2009年08月27日 星期四 20:22 | 2807次浏览 | 10条评论

参照那个《make yourselves a scheme in 48 hours》写的,不过显然大大超过了48 hours。没有中间代码生成,也没垃圾收集。有个简陋的闭包实现,有内存泄露...

示例代码,

 

var a=2;

case (a){

    of (3){

        puts('test');

    }

    of (2){

        puts('right');

    }

    else{

        puts('test');

    }

}

if (1){;;;;}

var add = function(i) {
        if (i<=0) {
                return 1;
        }
        puts(i);
        return i+add(i-1);
};
puts(add(9));

var arr=[1,2,3,4];
arr << 1 << 2;
puts(arr);

var hash={'a':1,'b':2};
hash['c']=hash['a']+hash['b'];

hash['c']=1;
puts(hash);

function makeAdder(num){
        var this={
                'count':num,
                'add':function(num){
                        this['count']=this['count']+num;
                }
        };
        return this;
}
var adder = makeAdder(2);
(adder['add'])(1);
(adder['add'])(1);
adder['add'](1);
adder['count'];

puts('************');

function makeCounter(init){
    var this={
        'n':init,
        'add':function(n){
            this['n']=this['n']+n;
            return this['n'];
        }
    };
    return this;
}
var counter=makeCounter(1);
puts((counter['add'])(2)); --3
puts(counter['add'](3));   --6

 源码: http://code.google.com/p/fineday/

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !
赵斌

回复 赵斌  2009年08月27日 星期四 21:36

我也有一个,编译的课设 http://code.google.com/p/myoc
有一个在线版http://dogsing.appspot.com
有中间代码生成,支持闭包,部分回收垃圾...
一起交流

9条回复

暂时没有评论

Zeuux © 2024

京ICP备05028076号