2008-04-17 02:37:03 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! Copyright (C) 2008 James Cash
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! See http://factorcode.org/license.txt for BSD license.
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								USING: kernel peg sequences arrays strings combinators.lib
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								namespaces combinators math bake locals locals.private accessors
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								vectors syntax lisp.parser assocs parser sequences.lib words quotations
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								fry ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-17 12:37:31 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								IN: lisp
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-17 02:37:03 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-21 20:25:55 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								DEFER: convert-form
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								DEFER: funcall
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-18 12:53:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								DEFER: lookup-var
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-21 20:25:55 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Functions to convert s-exps to quotations
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-body ( s-exp -- quot )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ ] [ convert-form compose ] reduce ; inline
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-21 20:25:55 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-if ( s-exp -- quot )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    rest first3 [ convert-form ] tri@ '[ @ , , if ] ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-begin ( s-exp -- quot )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    rest [ convert-form ] [ ] map-as '[ , [ funcall ] each ] ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-cond ( s-exp -- quot )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    rest [ body>> first2 [ convert-form ] bi@ [ '[ @ funcall ] ] dip 2array ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    { } map-as '[ , cond ]  ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-27 03:03:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-general-form ( s-exp -- quot )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    unclip convert-form swap convert-body swap '[ , @ funcall ] ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-15 22:14:53 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! words for convert-lambda  
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-24 16:35:42 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								<PRIVATE  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-14 02:19:52 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: localize-body ( assoc body -- assoc newbody )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ dup lisp-symbol? [ over dupd [ name>> ] dip at swap or ]
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-14 02:19:52 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                     [ dup s-exp? [ body>> localize-body <s-exp> ] when ] if
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                   ] map ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: localize-lambda ( body vars -- newbody newvars )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    make-locals dup push-locals swap
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ swap localize-body <s-exp> convert-form swap pop-locals ] dip swap ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                   
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: split-lambda ( s-exp -- body vars )                   
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    first3 -rot nip [ body>> ] bi@ [ name>> ] map ; inline
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-11 19:38:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: rest-lambda ( body vars -- quot )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    "&rest" swap [ index ] [ remove ] 2bi
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    localize-lambda <lambda>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    '[ , cut '[ @ , ] , compose ] ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-11 19:38:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: normal-lambda ( body vars -- quot )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    localize-lambda <lambda> '[ , compose ] ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-15 22:14:53 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								PRIVATE>
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-lambda ( s-exp -- quot )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-quoted ( s-exp -- quot )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    second 1quotation ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-list-form ( s-exp -- quot )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    dup first dup lisp-symbol?
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ name>>
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      { { "lambda" [ convert-lambda ] }
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        { "quote" [ convert-quoted ] }
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-26 17:20:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        { "if" [ convert-if ] }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        { "begin" [ convert-begin ] }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        { "cond" [ convert-cond ] }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								       [ drop convert-general-form ]
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      } case ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ drop convert-general-form ] if ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-21 20:25:55 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: convert-form ( lisp-form -- quot )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    { { [ dup s-exp? ] [ body>> convert-list-form ] }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    { [ dup lisp-symbol? ] [ '[ , lookup-var ] ] }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ 1quotation ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    } cond ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: lisp-string>factor ( str -- quot )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    lisp-expr parse-result-ast convert-form lambda-rewrite call ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								SYMBOL: lisp-env
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-14 20:46:22 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								ERROR: no-such-var var ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: init-env ( -- )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    H{ } clone lisp-env set ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-04-30 16:59:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: lisp-define ( name quot -- )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    swap lisp-env get set-at ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: lisp-get ( name -- word )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    dup lisp-env get at [ ] [ no-such-var throw ] ?if ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-18 12:53:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: lookup-var ( lisp-symbol -- quot )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    name>> lisp-get ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-05 12:49:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: funcall ( quot sym -- * )
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    dup lisp-symbol?  [ lookup-var ] when call ; inline
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-15 22:14:53 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: define-primitve ( name vocab word -- )  
							 | 
						
					
						
							
								
									
										
										
										
											2008-05-26 04:35:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    swap lookup 1quotation '[ , compose call ] lisp-define ;
							 |