`
runfriends
  • 浏览: 226433 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
lispscript js实现的lisp子集
(function(){
	listscript=run=function(_code){//it is main the function to analyze and execute listscript. its parameter code is to executed
		with(run){
			var code=_code;
			if(code instanceof Array){
				for(var i=0,l=code.length;i<l;i++){
					c=run(code[i]);
					if(c instanceof Function){
						if(c.length<=0){
							code[i]=c.call(null);
						}else if(i===0){
							return c.apply(null,code.slice(1));
						}
					}
				}
				return code;
			}else if(typeof(code)==='string'){
				 code=code.split(';');
				 for(var i=0,l=code.length;i<l;i++){
				 		run(eval(code[i]));
				 }
			}
			return Element(code);
		}
	};
	var def={
		run:run,
		NIL:[],
		__funList:[],
		toEvalString:function(arg){
			//if this array is empty, 'NIL' will be returned
			var l=arg.length;
			if(l<=0){
				return 'NIL';
			}
			var str=[];
			for(var i=0;i<l;i++){
				var item=arg[i];
				if(item instanceof Array){
					str.push(toEvalString(item));
				}else{
					str.push(item);
				}
				return [,item.join(),].join('');
			}
		},
		_:function(){//引用公设
			return arguments.length>=1?arguments[0]:[];
		},
		quote:_,
		atom:function(arg){//原子公设
			var tmp=run(arg);
			return (!(tmp instanceof Array) || tmp.length<=1)||[];
		},
		eq:function(arg1,arg2){//等值公设
			var tmp1=run(arg1),tmp2=run(arg2);
			return (!((tmp1 instanceof Array) || (tmp2 instanceof Array)) && tmp1.join('')===tmp2.join('')) ||
			       ((tmp1 instanceof Function) && (tmp2 instanceof Function) && tmp1.toString()===tmp2.toString()) ||
			       ((tmp1 instanceof Array)  && (tmp2 instanceof Array) && tmp1.length===0 && tmp2.length===0)||[];
		},
		equal:eq,
		
		car:function(arg){//表头公设
			var tmp=run(arg);
			return (tmp instanceof Array && tmp.length>0)?tmp:[];
		},
		cdr:function(arg){//余表公设
			var tmp=run(arg);
			return (tmp instanceof Array && tmp.length>0)?tmp.slice(0):[];
		},
		cons:function(arg1,arg2){//和表公设
			var tmp1=run(arg1),tmp2=run(arg2);
				return (tmp2 instanceof Array)?[tmp1].concat(tmp2):[];
		},
		cond:function(){//条件公设
			var args=arguments;
			for(var i=0,l=args.length;i<l;i++){
				var arg=args[i];
				if(arg instanceof Array){
					var cond=run(arg[0]);
					arg=arg[1];
					if(cond && arg){
						return run(arg);
					}
				}
			}
			return [];
		},
		lambda:function(args,code){//函数文法
			if(code instanceof Array){
				return Function.apply(null,args.concat(['for(var argus=arguments,i=0,l=argus.length;i<l;i++){argus[i]=run(argus[i]);}return run(',toEvalString(code),');'].join('')));
			}
			return [];
		},
		defun:function(funName,args,code){//函数定义
			var f=lambda(args,code);
			f._funName=funName;
			run[funName]=f;
		};
		lt=function(a,b){return a<b||[];};
		gt=function(a,b){return a>b||[];};
		le=function(a,b){return a<=b||[];};
		ge=function(a,b){return a>=b||[];};
		deq=function(a,b){return a===b||[]};
		sum=function(){
			var a=arguments,s=a[0]+a[1],i=0,l=a.length;
			a=Array.prototype.slice(2);
			while(i<l){
				s+=a[i++];
			};
			return s;
		},
		subtract:function(a,b){return a-b;},
		multiply:function(a,b){return a*b;},
		divide:function(a,b){return a/b;}
	};
	for(var n in def){
		run[n]=def[n];
	}
	function Element(arg){
		if(arg===null){
			return [];
		}else if(arg instanceof Function && arg.length<=0){
			return arg.call(null);
		}else{
			return arg;
		}
	}
})();
with(run){
	run([defun,'_eval',['e','a'],
				[ret,[maplist,[_,'e'],'a']]]);

	run([defun,'isNull',['x'],[]]);
	
	run([defun,'and',['x','y'],
					[cond,['x',[cond,['y',true],[true,NIL]],
						[true,NIL]]]]);
						
	run(defun,'dnoteq',['x','y'],
				[not,[deq,'x','y']]);
						
	run([defun,'not',['x'],
				[cond,['x',NIL],
						[true,true]]]);
						
	run([defun,'or',['x','y'],
					[not,[and,[not,'x'],[not,'y']]]]);
						
	run([defun,'xor',['x','y'],
				[or,[and,[not,'x'],'y'],[and,'x',[not,'y']]]]);
						
	run([defun,'join',['x','y'],
					[cond,[[isNull,'x'],'y'],
							[true,[cons,[car,'x'],['join',[cdr,'x'],'y']]]]]);
							
	run([defun,'pair',['x','y'],
					[cond,
					[[and,[isNull,'x'],[isNull,'y']],NIL],
					[[and,[not,[atom,'x']],[not,[atom,'y']]],
					 [join,[[[car,'x'],[car,'y']]],['pair',[car,'x'],[cdr,'y']]]
					]]]);
					
	run([defun,'assoc',['x','y'],
				[cond,[[eq,[car,['y']],'x'],[car,[cdr,[car,'y']]]],
						[[isNull,'y'],NIL],[true,['assoc','x',[cdr,'y']]]]]);
						
	run([defun,'ret',['e'],[car,['e']]]);
	
	run([defun,'str',['e'],[_,[_,'e']]]);
	
	run([defun,'map',['x','y'],
					[cond,[[isNull,[assoc,'x','y']],'x'],[true,[assoc,'x','y']]]]);
	
	run([defun,'maplist',['x','y'],
					[cond,
								[[atom,[_,'x']],[map,'x','y']],
								[true,[cons,['maplist',[car,[_,'x']],'y'],['maplist',[cdr,[_,'x']],'y']]]]]);
								
	run([defun,'setq',['para','val'],
					[ret,[defun,'para',[],[_eval,'val']]]]);
					
	run([defun,'foreach',['v','list','expr'],
					[cond,
							[[isNull,'list'],[]],
							[true,[cons,[_eval,[_,'expr'],[['v',[car,'list']]]],['foreach','v',[cdr,'list'],
							[_,'expr']]]]]]);
							
	run([defun,'let',[paralist],
					[foreach,"'v'",'paralist',[_,[setq,[car,"'v'"],[car,[cdr,"'v'"]]]]]]);
};
Global site tag (gtag.js) - Google Analytics