factor/contrib/lambda/lambda.factor

33 lines
1.1 KiB
Factor
Raw Normal View History

2006-08-06 20:31:29 -04:00
#! An interpreter for lambda expressions, by Matthew Willis
REQUIRES: lazy-lists ;
2006-08-17 01:27:21 -04:00
USING: lazy-lists io strings hashtables sequences namespaces kernel ;
2006-08-06 20:31:29 -04:00
IN: lambda
2006-08-17 01:27:21 -04:00
: bound-vars ( -- lazy-list ) 65 lfrom [ ch>string ] lmap ;
2006-08-06 20:31:29 -04:00
2006-08-17 01:27:21 -04:00
: lambda>string ( expr -- string )
bound-vars swap expr>string ;
2006-08-06 20:31:29 -04:00
2006-08-17 01:27:21 -04:00
: matching-names ( names string -- string )
#! this inefficiently finds all names matching the
#! canonical representation of string
[ \ evaluate , \ lambda>string , , \ = , \ nip , ] [ ] make
hash-subset hash-keys ", " join "=> " swap append ": " append ;
2006-08-06 20:31:29 -04:00
2006-08-17 01:27:21 -04:00
: lambda-print ( names expr -- names )
lambda>string [ matching-names ] 2keep rot swap append print flush ;
2006-08-06 20:31:29 -04:00
2006-08-17 01:27:21 -04:00
: lambda-eval ( names input -- names expr )
lambda-parse [ first ] keep second
pick swap replace-names
[ swap rot set-hash ] 3keep evaluate nip ;
2006-08-06 20:31:29 -04:00
2006-08-17 01:27:21 -04:00
: lambda-boot ( -- names )
#! load the core lambda library
H{ } clone ;
2006-08-06 20:31:29 -04:00
2006-08-17 01:27:21 -04:00
: (lambda) ( names -- names )
readln dup "." = [ drop ] [ lambda-eval lambda-print (lambda) ] if ;
2006-08-06 20:31:29 -04:00
2006-08-17 01:27:21 -04:00
: lambda ( -- names )
lambda-boot (lambda) ;