This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
projects:minilisp [2006/02/22 16:08] leonb created |
projects:minilisp [2016/02/17 09:56] (current) leonb [MiniLisp] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
+ | |||
+ | {{lambda.jpg? | ||
+ | Many years working with [[lush|Lush]] have taken their toll. | ||
+ | There are many things I really dislike doing in C or C++ | ||
+ | when I know how easy they would be in Lisp. | ||
+ | The essential lisp data structure is called the S-Expression. | ||
+ | S-Expressions can represent lots of complicated things | ||
+ | using a simple printable format. | ||
+ | reason why many people find XML or JSON appealing. | ||
+ | But I keep returning to S-Expression because they are so much simpler. | ||
+ | |||
+ | |||
+ | ===== MiniExp ===== | ||
+ | |||
+ | At some point I decided to simply write a C++ library | ||
+ | to handle the basic lisp data structure: [[wp> | ||
+ | The implementation fits in a single C++ file, | ||
+ | including a garbage collector | ||
+ | The full documentation is available as comments in | ||
+ | the header file '' | ||
+ | |||
+ | MiniExp handles four basic types of S-expressions: | ||
+ | |||
+ | * Integers, in range [-2< | ||
+ | * Symbols, which are small strings represented by a unique handle. | ||
+ | * Pairs, which are the basic components of lists. | ||
+ | * Objects, which encapsulate any C++ object derived from class '' | ||
+ | |||
+ | Examples: | ||
+ | |||
+ | minivar_t a_symbol = miniexp_symbol(" | ||
+ | minivar_t a_number = miniexp_number(23); | ||
+ | minivar_t a_list = miniexp_cons(a_symbol, | ||
+ | minivar_t an_object = miniexp_string(" | ||
+ | |||
+ | The type of the S-expression is '' | ||
+ | But we often store them into variables of type '' | ||
+ | The difference is garbage collection. | ||
+ | The garbage collector preserves all S-expressions stored in a '' | ||
+ | and all S-expressions recursively pointed by preserved S-expresssions. | ||
+ | |||
+ | To print a S-expression, | ||
+ | |||
+ | if (miniexp_numberp(exp)) | ||
+ | printf(" | ||
+ | else if (miniexp_symbolp(exp)) | ||
+ | printf(" | ||
+ | else if (miniexp_consp(exp)) | ||
+ | .... | ||
+ | |||
+ | But there is an easier way: | ||
+ | miniexp_print(exp); | ||
+ | and a prettier way: | ||
+ | miniexp_pprint(exp, | ||
+ | |||
+ | You can also read S-expressions from the standard input | ||
+ | minivar_t exp = miniexp_read(); | ||
+ | |||
+ | See the documentation in file '' | ||
+ | |||
+ | |||
+ | |||
+ | ===== MiniLisp ===== | ||
+ | |||
+ | During the development of '' | ||
+ | appeared that the best way to test s-expressions was to make | ||
+ | the last step and program a complete lisp interpreter. | ||
+ | |||
+ | Let's see what it can do: | ||
+ | |||
+ | MiniLisp, (C) 2005, Leon Bottou. | ||
+ | Available under the GNU General Public Licence. | ||
+ | (+ 2 3) | ||
+ | = 5 | ||
+ | | ||
+ | = square | ||
+ | | ||
+ | = 25 | ||
+ | | ||
+ | = sum | ||
+ | (sum '(1 8 7 3)) | ||
+ | = 19 | ||
+ | | ||
+ | (defun (sum l) (if (consp l) (+ (car l) (sum (cdr l))) 0)) | ||
+ | = () | ||
+ | | ||
+ | (defmacro (pretty f) | ||
+ | (let ((s (if (symbolp f) (list (list quote f))))) | ||
+ | (list (quote let) | ||
+ | (list (list (quote s) (list (quote funcdef) f . s))) | ||
+ | (list (quote pprint) (quote s) 72) | ||
+ | () ) ) ) | ||
+ | = () | ||
+ | |||
+ | |||
+ | The interpreter is very slow because symbol lookup is not optimized. | ||
+ | But it makes a nice example of using MiniExp. | ||
+ | ===== Download | ||
+ | |||
+ | The most recent MiniExp source code is available from the DjVu Git repository. | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | The documentation is in file '' | ||
+ | You can also check the MiniLisp source: | ||
+ | |||
+ | * [[http:// | ||