├── .gitignore ├── pure.c ├── test.sh ├── pureLisp.c ├── Makefile ├── tests ├── lambda_test.lisp ├── tak.lisp └── closure_test.lisp ├── LICENSE ├── pureLisp.h └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.exe 3 | test 4 | -------------------------------------------------------------------------------- /pure.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twinbird/pure/HEAD/pure.c -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twinbird/pure/HEAD/test.sh -------------------------------------------------------------------------------- /pureLisp.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twinbird/pure/HEAD/pureLisp.c -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | pure: pure.o pureLisp.o 2 | 3 | clean: 4 | rm -rf *.o 5 | rm -rf *.exe 6 | rm -rf test 7 | rm -rf *.stackdump 8 | -------------------------------------------------------------------------------- /tests/lambda_test.lisp: -------------------------------------------------------------------------------- 1 | (define cadr 2 | (lambda (x) 3 | (car 4 | (cdr x)))) 5 | 6 | (print 7 | (cadr 8 | (quote (1 2 3)))) 9 | -------------------------------------------------------------------------------- /tests/tak.lisp: -------------------------------------------------------------------------------- 1 | (define tak (lambda (x y z) 2 | (if (<= x y) 3 | y 4 | (tak 5 | (tak (- x 1) y z) 6 | (tak (- y 1) z x) 7 | (tak (- z 1) x y))))) 8 | 9 | (print (tak 10 5 0)) 10 | -------------------------------------------------------------------------------- /tests/closure_test.lisp: -------------------------------------------------------------------------------- 1 | (define adder-gen 2 | (lambda (x) 3 | (lambda (y) 4 | (+ x y)))) 5 | 6 | (define inc (adder-gen 1)) 7 | (define 2up (adder-gen 2)) 8 | 9 | (print (inc 1)) 10 | (print (inc 2)) 11 | (print (2up 1)) 12 | (print (2up 2)) 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 twinbird 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pureLisp.h: -------------------------------------------------------------------------------- 1 | #define MAX_TOKEN_LENGTH 256 2 | #define GC_THRESHOLD_BYTES 512000000 // 512MB 3 | 4 | typedef enum _objType { 5 | TYPE_PAIR, 6 | TYPE_INTEGER, 7 | TYPE_SYMBOL, 8 | TYPE_STRING, 9 | TYPE_NIL, 10 | TYPE_ENV, 11 | TYPE_PRIMITIVE, 12 | TYPE_T, 13 | TYPE_FUNCTION 14 | } ObjType; 15 | 16 | typedef enum _gcmark { 17 | USED, 18 | UNUSED 19 | } GCMark; 20 | 21 | // Primitive function type 22 | typedef struct _object *(*Primitive)(struct _object*, struct _object*); 23 | 24 | typedef struct _object { 25 | ObjType type; 26 | GCMark gcmark; 27 | struct _object *next; 28 | union { 29 | struct { 30 | struct _object *car; 31 | struct _object *cdr; 32 | } pair; 33 | int integer; 34 | char *symbol; 35 | char *string; 36 | struct { 37 | struct _object *vars; 38 | struct _object *up; 39 | } env; 40 | Primitive primitive; 41 | struct { 42 | struct _object *params; 43 | struct _object *body; 44 | struct _object *applyEnv; 45 | } function; 46 | }; 47 | } Object; 48 | 49 | extern Object *TopEnv; 50 | 51 | void initialize(); 52 | Object *read(Object *env, FILE *fp); 53 | Object *eval(Object *env, Object *obj); 54 | void print(Object *obj); 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pure 2 | 3 | Implementation of pure lisp. 4 | (But already it's not 'pure'.pure has many optional functions.) 5 | 6 | ## Build & Test 7 | 8 | ```sh 9 | $ make 10 | $ make test 11 | $ ./test 12 | ``` 13 | 14 | ## Usage 15 | 16 | ###### 1.REPL interface 17 | 18 | ```sh 19 | $ ./pure 20 | > 1 21 | 1 22 | > 23 | ``` 24 | 25 | ###### 2. Running source file 26 | 27 | ```sh 28 | $ ./pure [filename] 29 | ``` 30 | 31 | ###### 3. Running from stdin source code 32 | 33 | ```sh 34 | $ echo "(print 1)" | ./pure 35 | 1 36 | ``` 37 | 38 | ## Spec 39 | 40 | #### Special Form 41 | 42 | Implements the following Special Form. 43 | 44 | * if 45 | * quote 46 | * lambda 47 | * define 48 | 49 | #### Primitive Functions 50 | 51 | Implements the following functions. 52 | 53 | * atom 54 | * eq 55 | * car 56 | * cdr 57 | * cons 58 | * print 59 | * \+ 60 | * \- 61 | * \* 62 | * / 63 | * % 64 | * < 65 | * \> 66 | * <= 67 | * \>= 68 | 69 | #### Garbage Collection 70 | 71 | pure has adopted a conservative Mark & Sweep GC. 72 | 73 | #### Scope 74 | 75 | pure has adopted a lexical scope. 76 | 77 | ## Example 78 | 79 | Implementation of [tak function](https://en.wikipedia.org/wiki/Tak_(function) is as follows: 80 | 81 | ```lisp 82 | (define tak (lambda (x y z) 83 | (if (<= x y) 84 | y 85 | (tak 86 | (tak (- x 1) y z) 87 | (tak (- y 1) z x) 88 | (tak (- z 1) x y))))) 89 | 90 | (print (tak 10 5 0)) 91 | ``` 92 | 93 | ## License 94 | 95 | MIT 96 | --------------------------------------------------------------------------------