diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..50e5c0e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "files.associations": { + "optional": "c", + "system_error": "c", + "array": "c", + "functional": "c", + "tuple": "c", + "type_traits": "c", + "utility": "c" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..08d9005 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/README.md b/README.md index bff9fb6..3d526a1 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,9 @@ able to run on low powered devices. The added primitives: * strings and conversion +* operators *, /, mod * predicates >, >=, <=, not, -* functions list, atom, length, reverse, progn, load. +* functions list, atom, length, reverse, progn, print, println, load, exit, gensym. This has the side effect of being much faster as well, since all these primitives are compiled instead of being interpreted. @@ -23,15 +24,27 @@ In this version, instead of passing a file using pipes, you simply pass the file ./minilisp f1 f2 etc The files all share the same environment, so all the symbols, functions and macros defined -in f1 can be reused in the following files. The REPL is summonned after execution, unless we pass the option --r to the command line, in which case the interpreted quits immediately at the end. +in f1 can be reused in the following files. The REPL is started after execution, unless we pass the option +-r to the command line, in which case the interpreter quits immediately at the end. You can also pass a simple Lisp command as parameter: ./minilisp -x "(+ 1 1)" -The Lisp command will be evaluated and then the REPL is summonned. +The Lisp command will be evaluated and then the REPL is started. However we can quit immediately after execution with -r (or --no-repl). +The options, which can be given before or after the file names, are: + +``` +-x, --exec CODE evaluate the Lisp code CODE +-r, --no-repl don't enter the read-eval-print loop +-H, --no-history don't load nor save the history of the REPL +-h, --help print the help and exit +``` + +When an expression fails, the error is reported on the standard error output +as `file[line]: message` and the evaluation goes on with the next expression. + ## REPL Shortcuts ``` @@ -81,12 +94,22 @@ CTRL-X CTRL-X GOTO MARK CTRL-Z SUSPEND PROCESS ``` +CTRL-D on an empty line leaves the REPL. Lines starting with a slash are REPL commands: + +``` +/memory display the amount of memory used and the size of the heap +/help display a short help +``` + The REPL also saves the history of commands in the file history.txt -This file is loaded at startup, so one can recall previous commands. +This file is loaded at startup, so one can recall previous commands +(unless the option -H is given). Known bugs: * recall of multiline commands does not work as expected. -* this doesn't have tail call optimization, so expect crashes with sometimes with surprisingly short lists. +* this doesn't have tail call optimization, and each call uses a good deal of + the C stack. A recursion deeper than a few thousand calls stops with the error + "Stack overflow: the recursion is too deep". Original README (completed) =============== @@ -127,12 +150,20 @@ MiniLisp comes with a comprehensive test suite. In order to run the tests, give $ make test +The tests run twice, the second time with the environment variable +`MINILISP_ALWAYS_GC=1`, which makes the garbage collector run at every memory +allocation and makes the old heap inaccessible, so that any stale pointer +crashes immediately. `MINILISP_DEBUG_GC=1` prints a line on the standard error +output at each collection. The heap starts at 4 MB (per semi-space) and doubles +when more than half of it is still in use after a collection. + Language features ----------------- MiniLisp is a traditional Lisp interpreter. It reads one expression at a time -from the standard input, evaluates it, and then prints out the return value of -the expression. Here is an example of a valid input. +from a file, from the `-x` option or from the REPL, evaluates it, and then +prints out the return value of the expression. Here is an example of a valid +input. (+ 1 2) @@ -140,15 +171,16 @@ The above expression prints "3". ### Literals -MiniLisp supports integer literals, `()`, `t`, symbols, and list literals. +MiniLisp supports integer literals, string literals, `()`, `t`, symbols, and +list literals. -* Integer literals are positive or negative integers. +* Integer literals are positive or negative 64 bit integers. +* String literals are written between double quotes. The escape sequences `\n`, + `\t`, `\r`, `\"` and `\\` are recognized. * `()` is the only false value. It also represents the empty list. * `t` is a predefined variable evaluated to itself. It's a preferred way to represent a true value, while any non-`()` value is considered to be true. * Symbols are objects with unique name. They are used to represent identifiers. - Because MiniLisp does not have string type, symbols are sometimes used as a - substitute for strings too. * List literals are cons cells. It's either a regular list whose last element's cdr is `()` or an dotted list ending with any non-`()` value. A dotted list is written as `(a . b)`. @@ -177,23 +209,34 @@ car. cell ; -> (x . b) `length` and `reverse` operate either on their arguments, or a single list or a string. +`reverse` never modifies its arguments: it returns a new list or a new string. (length '(1 2 3)) ; -> 3 (length 1 2 t) ; -> 3 (length "1 2 3") ; -> 5 (reverse '(a b c)) ; -> (c b a) + (reverse 1 2 3) ; -> (3 2 1) (reverse "1234") ; -> "4321" (reverse '((a) b "c")) ; -> (c b (a)) ### Numeric operators +Integers are 64 bit signed integers. The result of an operation that overflows +wraps around. Numeric operators only accept integers. + `+` returns the sum of the arguments. + (+) ; -> 0 (+ 1) ; -> 1 (+ 1 2) ; -> 3 (+ 1 2 3) ; -> 6 +`*` returns the product of the arguments. + + (*) ; -> 1 + (* 2 3 4) ; -> 24 + `-` negates the value of the argument if only one argument is given. (- 3) ; -> -3 @@ -204,6 +247,15 @@ If multiple arguments are given, `-` subtracts each argument from the first one. (- 5 2) ; -> 3 (- 5 2 7) ; -> -4 +`/` divides the first argument by each of the following ones, and `mod` returns +the remainder of the same division. The division of integers is truncated +towards zero. Dividing by zero is an error. + + (/ 7 2) ; -> 3 + (/ -7 2) ; -> -3 + (mod 7 2) ; -> 1 + (mod -7 2) ; -> -1 + `=` takes two arguments and returns `t` if the two are the same integer. (= 11 11) ; -> t @@ -220,9 +272,10 @@ The other numerical predicates `>`, `<=`, `>=` work in a similar fashion. ### Conditionals -`(if cond then else)` is the only conditional in the language. It first +`(if cond then else ...)` is the only conditional in the language. It first evaluates *cond*. If the result is a true value, *then* is evaluated. Otherwise -*else* is evaluated. +the *else* expressions are evaluated in sequence, and the value of the last one +is returned (`()` if there is none). ### Loops @@ -231,28 +284,32 @@ evaluates *cond*. If the result is a true value, *then* is evaluated. Otherwise If you are familiar with Scheme, you might be wondering if you could write a loop by tail recursion in MiniLisp. The answer is no. Tail calls consume stack -space in MiniLisp, so a loop written as recursion will fail with the memory -exhaustion error. +space in MiniLisp, so a loop written as recursion will fail with the "Stack +overflow" error after a few thousand iterations. ### Imperative programming `(progn expr expr ...)` executes several expressions in sequence. - ( progn (print "I own ") - (defun add(x y)(+ x y)) - (print (add 3 7) - (println " cents") ) ; -> prints "I own 10 cents" + (progn (print "I own ") + (defun add (x y) (+ x y)) + (print (add 3 7)) + (println " cents")) ; -> prints "I own 10 cents" ### Equivalence test operators -`eq` takes two arguments and returns `t` if the objects are the same. What `eq` -really does is a pointer comparison, so two objects happened to have the same -contents but actually different are considered to not be the same by `eq`. -`eq` can also compare two strings. +`eq` takes two arguments and returns `t` if the objects are the same. Integers +are compared by value and strings by content. For all the other objects `eq` does +a pointer comparison, so two lists that happened to have the same contents but +are actually different are considered to not be the same by `eq`. + + (eq 1000 1000) ; -> t + (eq 'a 'a) ; -> t + (eq '(1) '(1)) ; -> () ### String functions -`eq` can also compare two strings. +`eq` compares strings by content. (eq "Hello" "Hello") ; -> t (eq "Hello" "hello") ; -> () @@ -273,13 +330,15 @@ contents but actually different are considered to not be the same by `eq`. ### Output operators -`print` prints a given object to the standard output. +`print` prints its arguments to the standard output, separated by a space. +Strings are printed without the quotes. (print 3) ; -> "3" (print '(hello world)) ; -> "(hello world)" (print "hello" "world") ; -> "hello world" -`println` does the same, adding a return at the end. +`println` does the same, adding a return at the end. Without argument, it only +prints a return. ### Definitions @@ -297,7 +356,7 @@ you can assign to a variable using `define`. ((lambda (x) (+ x x)) 6) ; do the same thing without assignment The other way is `defun`. `(defun fn (args ...) expr ...)` is short for -`(define fn (lambda (args ...) expr ...)`. +`(define fn (lambda (args ...) expr ...))`. ;; Define "double" using defun (defun double (x) (+ x x)) @@ -312,10 +371,12 @@ parameter as a list. Variables are lexically scoped and have indefinite extent. References to "outer" variables remain valid even after the function that created the variables -returns. +returns. Calling a function with more or fewer arguments than it has +parameters is an error, unless the last parameter is a dotted one as above. ;; A countup function. We use lambda to introduce local variables because we - ;; do not have "let" and the like. + ;; do not have "let" and the like. (The file examples/library.lisp defines + ;; "let", "when", "and", "or" etc. as macros). (define counter ((lambda (count) (lambda () @@ -347,8 +408,12 @@ is not defined. ### System functions `load` loads a Lisp file and evaluates all its content, adding it to the environment. - (load "example/nqueens.lisp") -> run the file and store its evaluated functions - and macros + (load "examples/nqueens.lisp") -> run the file and store its evaluated functions + and macros + +A relative path is first looked up in the directory of the file that calls `load`, +then in the current directory. For instance, `(load "./library.lisp")` in +examples/hanoi.lisp loads examples/library.lisp, wherever minilisp is started from. `exit` quits the interpreter and returns the integer passed as parameter. @@ -380,7 +445,7 @@ the control is passed to the function. `gensym` creates a new symbol which will never be `eq` to any other symbol other than itself. Useful for writing a macro that introduces new identifiers. - (gensym) ; -> a new symbol + (gensym) ; -> a new symbol, such as G__0 ### Comments diff --git a/examples/aoc_day1.clij b/examples/aoc_day1.clij new file mode 100644 index 0000000..74b3b27 --- /dev/null +++ b/examples/aoc_day1.clij @@ -0,0 +1,55 @@ +(define input + '(3 4 +4 3 +2 5 +1 3 +3 9 +3 3)) + +; for map, reduce, quicksort +(load "examples/library.lisp") + +(defun null?(x) (eq () x)) +(defun cadr(lst) (car (cdr lst))) +(defun cddr(lst) (cdr (cdr lst))) + +(defun transpose (lst) + (if (null? lst) + (list '() '()) + (let result (transpose (cddr lst)) + (list + (cons (car lst) (car result)) + (cons (cadr lst) (cadr result)))) + )) + +(defun abs(x) + (if (< x 0) (- 0 x) x)) + +(defun map2 (fn lst1 lst2) + (if (or (null? lst1) (null? lst2)) + () + (cons (fn (car lst1) (car lst2)) + (map2 fn (cdr lst1) (cdr lst2))))) + +; Now we can write our complete solution: +(defun sum-of-differences (columns) + (reduce + + (map (map2 - (sort (car columns)) (sort (cadr columns))) abs) + 0)) + +(define columns (transpose input)) +(println (sum-of-differences columns)) + +; Part 2 +(defun unique-elements (list2 list1) + (if (eq () list2) + () + (if (member list1 (car list2)) + (unique-elements (cdr list2) list1) + (cons (car list2) + (unique-elements (cdr list2) list1))))) + +(define sum (reduce + (cadr columns) 0)) +(define uniques (unique-elements (cadr columns)(car columns))) +(println uniques) +(println (- sum (reduce + uniques 0))) \ No newline at end of file diff --git a/examples/func.clij b/examples/func.clij new file mode 100644 index 0000000..d054630 --- /dev/null +++ b/examples/func.clij @@ -0,0 +1,38 @@ +; take: returns first n elements of a list +(defun null?(x) (eq () x)) + +(defun take(lst n) + (if (or (= n 0) (null? lst)) + '() + (cons (car lst) + (take (cdr lst) (- n 1))))) + +; drop: removes first n elements from a list +(defun drop(lst n) + (if (or (= n 0) (null? lst)) + lst + (drop (cdr lst) (- n 1)))) + +; partition: splits list into sublists of size n +(defun partition(lst n) + (if (null? lst) + '() + (cons (take lst n) + (partition (drop lst n) n)))) + +; Example usage: +(define sample-list (list 1 2 3 4 5 6)) + +; take examples +(take sample-list 0) ; => () +(take sample-list 3) ; => (1 2 3) +(take sample-list 6) ; => (1 2 3 4 5 6) + +; drop examples +(drop sample-list 0) ; => (1 2 3 4 5 6) +(drop sample-list 3) ; => (4 5 6) +(drop sample-list 6) ; => () + +; partition examples +(partition sample-list 2) ; => ((1 2) (3 4) (5 6)) +(partition sample-list 3) ; => ((1 2 3) (4 5 6)) \ No newline at end of file diff --git a/examples/hanoi.lisp b/examples/hanoi.lisp index cc37abe..47974c4 100644 --- a/examples/hanoi.lisp +++ b/examples/hanoi.lisp @@ -1,4 +1,4 @@ -(load "examples/library.lisp") +(load "library.lisp") (defun hanoi-print (disk from to) (println (string-concat "Move disk " disk diff --git a/examples/life.lisp b/examples/life.lisp index 8f968fa..c1d32b8 100644 --- a/examples/life.lisp +++ b/examples/life.lisp @@ -2,7 +2,7 @@ ;;; Conway's game of life ;;; -(load "examples/library.lisp") +(load "library.lisp") (define width 15) (define height 15) diff --git a/examples/nqueens.lisp b/examples/nqueens.lisp index c1d6220..8ffb771 100644 --- a/examples/nqueens.lisp +++ b/examples/nqueens.lisp @@ -8,7 +8,7 @@ ;;; This program solves N-queens puzzle by depth-first backtracking. ;;; -(load "examples/library.lisp") +(load "library.lisp") ;;; ;;; N-queens solver diff --git a/history.txt b/history.txt new file mode 100644 index 0000000..86171d2 --- /dev/null +++ b/history.txt @@ -0,0 +1,40 @@ +(println (cadr columns)) +(sum (+ (cadr columns))) +(+ (cadr columns)) +(+ '(1 2 3)) +(+ 1 2 3) +(+ '(1 2 3)) +(+ (1 2 3)) +(+ (list 1 2 3)) +(let a 5 b 12) +(let a 5 (let b 12)) +(let a 5) +(let a (5)) +(let (a 5)) +(let (a 5) (println "toto")) +(let (a 5) (println a )) +(let (a 5 (println a )) +(let (a 5 (println a ))) +(quicksort '(6 3 89 32 )) +(qsort '(6 3 89 32 )) +(iota 10) +(qsort (iota 10)) +(qsort (iota 20)) +(sort (iota 20)) +(sort (iota 120)) +(sort (reverse(iota 120))) +(+ '( 1 2 3)) +(+ 1 2 3) +(apply + '(1 2 3)) +(reduce + '(1 2 3)) +(reduce + '(1 2 3) 0) +(exists-in 5 (car columns)) +(exists-in 3 (car columns)) +(car columns) +(exists-in (car columns) 3) +(member 3 (car columns) ) +(member (car columns) 3) +(exists-in (car columns) (cadr columns)) ++ 2 3 +(+ 2 3) +(+ 1 2) diff --git a/src/gc.c b/src/gc.c index 3f4dab0..ea87de8 100644 --- a/src/gc.c +++ b/src/gc.c @@ -1,28 +1,36 @@ #include #include +#include #include #include #include #include #include "gc.h" -extern void error(char *fmt, int line_num, ...); extern filepos_t filepos; -// The pointer pointing to the beginning of the current heap +// The pointer pointing to the beginning of the current heap (the semi-space we allocate from) void *memory; -// The pointer pointing to the beginning of the old heap +// The size in bytes of the current heap +size_t heap_size = MEMORY_SIZE; + +// The unused semi-space. It is kept mapped between two collections, so that a collection does not +// have to ask the kernel for fresh pages every time. It becomes the to-space of the next GC. +static void *spare_space; +static size_t spare_size; + +// The old heap while a collection is running static void *from_space; +static size_t from_size; // The number of bytes allocated from the heap size_t mem_nused = 0; // Flags to debug GC - bool gc_running = false; - bool debug_gc = false; - bool always_gc = false; - +bool gc_running = false; +bool debug_gc = false; +bool always_gc = false; // The list containing all symbols. Such data structure is traditionally called the "obarray", but I // avoid using it as a variable name as this is not an array but a list. @@ -35,6 +43,31 @@ static inline size_t roundup(size_t var, size_t size) { return (var + size - 1) & ~(size - 1); } +// Returns true if the environment variable is defined and not the empty string. +static bool getEnvFlag(char *name) { + char *val = getenv(name); + return val && val[0]; +} + +static void *map_space(size_t size) { + void *p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + return p == MAP_FAILED ? NULL : p; +} + +// Allocates the two semi-spaces and reads the debug flags. Must be called once at startup. +void gc_init(void) { + debug_gc = getEnvFlag("MINILISP_DEBUG_GC"); + always_gc = getEnvFlag("MINILISP_ALWAYS_GC"); + heap_size = spare_size = MEMORY_SIZE; + memory = map_space(heap_size); + spare_space = map_space(spare_size); + if (!memory || !spare_space) { + fputs("Cannot allocate the heap\n", stderr); + exit(1); + } + mem_nused = 0; +} + // Allocates memory block. This may start GC if we don't have enough memory. Obj *alloc(void *root, int type, size_t size) { // The object must be large enough to contain a pointer for the forwarding pointer. Make it @@ -49,27 +82,30 @@ Obj *alloc(void *root, int type, size_t size) { // boundary as the pointer. size = roundup(size, sizeof(void *)); - // If the debug flag is on, allocate a new memory space to force all the existing objects to - // move to new addresses, to invalidate the old addresses. By doing this the GC behavior becomes - // more predictable and repeatable. If there's a memory bug that the C variable has a direct - // reference to a Lisp object, the pointer will become invalid by this GC call. Dereferencing - // that will immediately cause SEGV. - if (always_gc && !gc_running) - gc(root); - + // If the debug flag is on, run a GC at every allocation to force all the existing objects to + // move to new addresses, and make the old heap inaccessible. By doing this the GC behavior + // becomes more predictable and repeatable. If there's a memory bug that the C variable has a + // direct reference to a Lisp object, the pointer will become invalid by this GC call. + // Dereferencing that will immediately cause SEGV. // Otherwise, run GC only when the available memory is not large enough. - if (!always_gc && MEMORY_SIZE < mem_nused + size) + if (always_gc || heap_size < mem_nused + size) gc(root); - // Terminate the program if we couldn't satisfy the memory request. This can happen if the - // requested size was too large or the from-space was filled with too many live objects. - if (MEMORY_SIZE < mem_nused + size) + // The request is larger than what is left after GC: grow the heap. + while (heap_size < mem_nused + size && heap_size < MAX_HEAP_SIZE) + if (!gc_collect(root, heap_size * 2)) + break; + + // Give up if we couldn't satisfy the memory request. This can happen if the requested size + // was too large or the heap was filled with too many live objects. + if (heap_size < mem_nused + size) error("Memory exhausted", filepos.line_num); - // Allocate the object. - Obj *obj = memory + mem_nused; + // Allocate the object. The heap is not necessarily zeroed (it can be a recycled semi-space). + Obj *obj = (Obj *)((uint8_t *)memory + mem_nused); obj->type = type; obj->size = size; + obj->line_num = 0; mem_nused += size; return obj; } @@ -92,7 +128,7 @@ static inline Obj *forward(Obj *obj) { // If the object's address is not in the from-space, the object is not managed by GC nor it // has already been moved to the to-space. ptrdiff_t offset = (uint8_t *)obj - (uint8_t *)from_space; - if (offset < 0 || MEMORY_SIZE <= offset) + if (offset < 0 || (size_t)offset >= from_size) return obj; // The pointer is pointing to the from-space, but the object there was a tombstone. Follow the @@ -112,10 +148,6 @@ static inline Obj *forward(Obj *obj) { return newloc; } -void *alloc_semispace() { - return mmap(NULL, MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); -} - // Copies the root objects. static void forward_root_objects(void *root) { Symbols = forward(Symbols); @@ -125,26 +157,26 @@ static void forward_root_objects(void *root) { frame[i] = forward(frame[i]); } - -// Returns true if the environment variable is defined and not the empty string. -static bool getEnvFlag(char *name) { - char *val = getenv(name); - return val && val[0]; -} - -// Implements Cheney's copying garbage collection algorithm. -// http://en.wikipedia.org/wiki/Cheney%27s_algorithm -void gc(void *root) { +// Copies all the live objects to a to-space of new_size bytes, which becomes the heap. Returns +// false, leaving the heap untouched, if the new to-space could not be allocated. +bool gc_collect(void *root, size_t new_size) { assert(!gc_running); - gc_running = true; - // Debug flags - debug_gc = getEnvFlag("MINILISP_DEBUG_GC"); - always_gc = getEnvFlag("MINILISP_ALWAYS_GC"); + // Get the to-space: reuse the spare semi-space when it has the right size. + void *to_space = spare_space; + if (spare_size != new_size) { + to_space = map_space(new_size); + if (!to_space) + return false; + munmap(spare_space, spare_size); + } else if (always_gc) { + mprotect(to_space, new_size, PROT_READ | PROT_WRITE); + } + gc_running = true; - // Allocate a new semi-space. from_space = memory; - memory = alloc_semispace(); + from_size = heap_size; + memory = to_space; // Initialize the two pointers for GC. Initially they point to the beginning of the to-space. scan1 = scan2 = memory; @@ -178,16 +210,35 @@ void gc(void *root) { scan1->up = forward(scan1->up); break; default: - error("Bug: copy: unknown type %d", scan1->type); + // The heap is corrupted: we cannot recover from this, nor unwind safely. + fprintf(stderr, "Bug: copy: unknown type %d\n", scan1->type); + abort(); } scan1 = (Obj *)((uint8_t *)scan1 + scan1->size); } - // Finish up GC. - munmap(from_space, MEMORY_SIZE); + // Finish up GC: the old heap becomes the spare semi-space. In debug mode, make it inaccessible + // so that any stale pointer to it crashes immediately. size_t old_nused = mem_nused; + spare_space = from_space; + spare_size = from_size; + if (always_gc) + mprotect(spare_space, spare_size, PROT_NONE); + heap_size = new_size; mem_nused = (size_t)((uint8_t *)scan1 - (uint8_t *)memory); if (debug_gc) fprintf(stderr, "GC: %zu bytes out of %zu bytes copied.\n", mem_nused, old_nused); gc_running = false; + return true; +} + +// Implements Cheney's copying garbage collection algorithm. +// http://en.wikipedia.org/wiki/Cheney%27s_algorithm +// If more than half of the heap is still in use afterwards, the heap is doubled, otherwise we +// would collect again and again after every few allocations. +void gc(void *root) { + gc_collect(root, heap_size); + while (mem_nused > heap_size / 2 && heap_size < MAX_HEAP_SIZE) + if (!gc_collect(root, heap_size * 2)) + break; } diff --git a/src/gc.h b/src/gc.h index a2c1022..5d0841a 100644 --- a/src/gc.h +++ b/src/gc.h @@ -9,10 +9,14 @@ // Memory management //====================================================================== -// The size of the heap in byte -#define MEMORY_SIZE 65536 * 4 +// The initial size in bytes of each semi-space. The heap grows (up to MAX_HEAP_SIZE) when too much +// of it is still in use after a collection. +#define MEMORY_SIZE (65536 * 64) +#define MAX_HEAP_SIZE ((size_t)1 << 30) extern void *gc_root; // root of memory +extern size_t heap_size; // current size in bytes of one semi-space +extern size_t mem_nused; // number of bytes currently allocated in the heap // Currently we are using Cheney's copying GC algorithm, with which the available memory is split // into two halves and all objects are moved from one half to another every time GC is invoked. That @@ -69,8 +73,9 @@ static inline void** add_root_frame(void **prev_frame, int size, void *frame[]) Obj **var4 = (Obj **)(root_frame + 4); -void *alloc_semispace(); +void gc_init(void); Obj *alloc(void *root, int type, size_t size); void gc(void *root); +bool gc_collect(void *root, size_t new_size); #endif \ No newline at end of file diff --git a/src/minilisp.c b/src/minilisp.c index 96c2d9e..37bb72c 100644 --- a/src/minilisp.c +++ b/src/minilisp.c @@ -7,8 +7,11 @@ #include #include #include +#include #include #include +#include +#include #include "minilisp.h" #include "gc.h" @@ -16,7 +19,7 @@ jmp_buf context; extern filepos_t filepos; -void error(char *fmt, int line_num, ...) { +void error(const char *fmt, int line_num, ...) { va_list ap; va_start(ap, line_num); fprintf(stderr, "%s[%d]: ", filepos.filename, line_num); @@ -43,9 +46,20 @@ extern Obj *Symbols; void *gc_root = NULL; // root of memory -extern Obj *alloc(void *root, int type, size_t size); +// Small integers are preallocated: they are never managed by GC (like the constants above) and +// computing with them does not allocate. +#define SMALL_INT_MIN (-128) +#define SMALL_INT_MAX 1023 +static Obj small_ints[SMALL_INT_MAX - SMALL_INT_MIN + 1]; + +static void init_small_ints(void) { + for (int i = 0; i < SMALL_INT_MAX - SMALL_INT_MIN + 1; i++) + small_ints[i] = (Obj){ .type = TINT, .size = sizeof(Obj), .value = i + SMALL_INT_MIN }; +} static Obj *make_int(void *root, long long value) { + if (SMALL_INT_MIN <= value && value <= SMALL_INT_MAX) + return &small_ints[value - SMALL_INT_MIN]; Obj *r = alloc(root, TINT, sizeof(long long)); r->value = value; r->line_num = filepos.line_num; @@ -60,7 +74,7 @@ static Obj *cons(void *root, Obj **car, Obj **cdr) { return cell; } -static Obj *make_symbol(void *root, char *name) { +static Obj *make_symbol(void *root, const char *name) { Obj *sym = alloc(root, TSYMBOL, strlen(name) + 1); strcpy(sym->name, name); sym->line_num = filepos.line_num; @@ -113,38 +127,69 @@ static Obj *acons(void *root, Obj **x, Obj **y, Obj **a) { //====================================================================== #define SYMBOL_MAX_LEN 200 -const char symbol_chars[] = "~!@#$%^&*-_=+:/?<>"; - -void swap(char *left, char *right) { - char tmp = *left; - *left = *right; - *right = tmp; -} +static const char symbol_chars[] = "~!@#$%^&*-_=+:/?<>"; static Obj *read_expr(void *root); -static int peek(void) { - int c = getchar(); - ungetc(c, stdin); - return c; +// The text being read. Reading directly from a buffer is much faster than going through stdio, +// and it does not require to hijack stdin. +typedef struct { + const char *text; + size_t len; + size_t pos; +} reader_t; + +static reader_t reader; + +void set_input(const char *text, size_t len) { + reader = (reader_t){ text, len, 0 }; +} + +static inline int peek(void) { + return reader.pos < reader.len ? (unsigned char)reader.text[reader.pos] : EOF; } static int read_char(void) { - int c = getchar(); + int c = peek(); + if (c == EOF) + return EOF; + reader.pos++; if (c == '\n') { filepos.line_num++; - if (peek() == '\r') { - getchar(); - } + if (peek() == '\r') + reader.pos++; } else if (c == '\r') { filepos.line_num++; - if (peek() == '\n') { - getchar(); - } + if (peek() == '\n') + reader.pos++; } return c; } +static bool is_symbol_char(int c) { + return c > 0 && strchr(symbol_chars, c) != NULL; +} + +// The C stack is the limit of the recursion depth. Fail with a Lisp error before reaching it, +// instead of crashing with SEGV. +static uintptr_t stack_limit = 0; + +static void init_stack_limit(void) { + char here; + struct rlimit rl; + size_t size = 8 << 20; + if (getrlimit(RLIMIT_STACK, &rl) == 0 && rl.rlim_cur != RLIM_INFINITY) + size = rl.rlim_cur; + // Keep a safety margin for the C library and the error handling. + stack_limit = (uintptr_t)&here - (size - (size >> 3) - (64 << 10)); +} + +static inline void check_stack(int line_num) { + char here; + if ((uintptr_t)&here < stack_limit) + error("Stack overflow: the recursion is too deep", line_num); +} + // Destructively reverses the given list. static Obj *reverse(Obj *p) { Obj *ret = Nil; @@ -159,22 +204,15 @@ static Obj *reverse(Obj *p) { // Skips the input until newline is found. Newline is one of \r, \r\n or \n. static void skip_line(void) { - for (;;) { - char c = getchar(); - if (c == EOF || c == '\n'){ - filepos.line_num++; - return; - } - if (c == '\r') { - if (peek() == '\n') - getchar(); - return; - } - } + int c; + // read_char() counts the lines and swallows the second character of \r\n and \n\r. + while ((c = read_char()) != EOF && c != '\n' && c != '\r') + ; } // Reads a list. Note that '(' has already been read. static Obj *read_list(void *root) { + check_stack(filepos.line_num); DEFINE3(root, obj, head, last); *head = Nil; for (;;) { @@ -184,7 +222,11 @@ static Obj *read_list(void *root) { if (*obj == Cparen) return reverse(*head); if (*obj == Dot) { + if (*head == Nil) + error("Unexpected dot at the beginning of a list", filepos.line_num); *last = read_expr(root); + if (!*last || *last == Cparen || *last == Dot) + error("Expression expected after dot", filepos.line_num); if (read_expr(root) != Cparen) error("Closed parenthesis expected after dot", filepos.line_num); Obj *ret = reverse(*head); @@ -197,7 +239,7 @@ static Obj *read_list(void *root) { // May create a new symbol. If there's a symbol with the same name, it will not create a new symbol // but return the existing one. -static Obj *intern(void *root, char *name) { +static Obj *intern(void *root, const char *name) { for (Obj *p = Symbols; p != Nil; p = p->cdr) if (strcmp(name, p->car->name) == 0) return p->car; @@ -217,9 +259,13 @@ static Obj *read_quote(void *root) { return *tmp; } -static long long read_number(int val) { - while (isdigit(peek())) - val = val * 10 + (read_char() - '0'); +static long long read_number(long long val) { + while (isdigit(peek())) { + int digit = read_char() - '0'; + if (val > (LLONG_MAX - digit) / 10) + error("Integer too large", filepos.line_num); + val = val * 10 + digit; + } return val; } @@ -227,7 +273,7 @@ static Obj *read_symbol(void *root, char c) { char buf[SYMBOL_MAX_LEN + 1]; buf[0] = c; int len = 1; - while (isalnum(peek()) || strchr(symbol_chars, peek())) { + while (isalnum(peek()) || is_symbol_char(peek())) { if (SYMBOL_MAX_LEN <= len) error("Symbol name too long", filepos.line_num); buf[len++] = read_char(); @@ -262,7 +308,7 @@ static Obj *read_string(void *root) { static Obj *read_expr(void *root) { for (;;) { - char c = read_char(); + int c = read_char(); if (c == ' ' || c == '\n' || c == '\r' || c == '\t') continue; if (c == ';') { @@ -283,7 +329,7 @@ static Obj *read_expr(void *root) { return make_int(root, read_number(c - '0')); if (c == '-' && isdigit(peek())) return make_int(root, -read_number(0)); - if (isalpha(c) || strchr(symbol_chars, c)) + if (isalpha(c) || is_symbol_char(c)) return read_symbol(root, c); if (c == EOF) return NULL; @@ -378,12 +424,15 @@ static Obj *push_env(void *root, Obj **env, Obj **vars, Obj **vals) { } if (*vars != Nil) *map = acons(root, vars, vals, map); + else if (*vals != Nil) + error("Cannot apply function: number of argument does not match", filepos.line_num); return make_env(root, map, env); } // Evaluates the list elements from head and returns the last return value. static Obj *progn(void *root, Obj **env, Obj **list) { DEFINE2(root, lp, r); + *r = Nil; for (*lp = *list; *lp != Nil; *lp = (*lp)->cdr) { *r = (*lp)->car; *r = eval(root, env, r); @@ -395,11 +444,14 @@ static Obj *progn(void *root, Obj **env, Obj **list) { static Obj *eval_list(void *root, Obj **env, Obj **list) { DEFINE4(root, head, lp, expr, result); *head = Nil; - for (lp = list; *lp != Nil; *lp = (*lp)->cdr) { + // Walk with our own slot: the caller's list must be left untouched. + for (*lp = *list; (*lp)->type == TCELL; *lp = (*lp)->cdr) { *expr = (*lp)->car; *result = eval(root, env, expr); *head = cons(root, result, head); } + if (*lp != Nil) + error("Improper list of arguments", (*lp)->line_num); return reverse(*head); } @@ -458,6 +510,7 @@ static Obj *macroexpand(void *root, Obj **env, Obj **obj) { // Evaluates the S expression. static Obj *eval(void *root, Obj **env, Obj **obj) { + check_stack((*obj)->line_num); switch ((*obj)->type) { case TINT: case TPRIMITIVE: @@ -478,14 +531,25 @@ static Obj *eval(void *root, Obj **env, Obj **obj) { case TCELL: { // Function application form DEFINE3(root, fn, expanded, args); - *expanded = macroexpand(root, env, obj); - if (*expanded != *obj) - return eval(root, env, expanded); - *fn = (*obj)->car; - *fn = eval(root, env, fn); *args = (*obj)->cdr; + if ((*obj)->car->type == TSYMBOL) { + // Look the head symbol up only once: it is either a macro to expand or the function + // to call. + Obj *bind = find(env, (*obj)->car); + if (!bind) + error("Undefined symbol: %s", (*obj)->car->line_num, (*obj)->car->name); + *fn = bind->cdr; + if ((*fn)->type == TMACRO) { + *expanded = apply_func(root, env, fn, args); + return eval(root, env, expanded); + } + } else { + *fn = (*obj)->car; + *fn = eval(root, env, fn); + } if ((*fn)->type != TPRIMITIVE && (*fn)->type != TFUNCTION) - error("The head of a list must be a function", (*obj)->line_num); + error("The head of a list must be a function", + (*fn)->line_num ? (*fn)->line_num : (*obj)->line_num); return apply(root, env, fn, args); } default: @@ -514,8 +578,9 @@ static Obj *prim_quote(void *root, Obj **env, Obj **list) { static Obj *prim_atom(void *root, Obj **env, Obj **list) { if (length(*list) != 1) - error("atom takes ontly 1 argument", (*list)->line_num); - return ((*list)->car->type != TCELL) ? True : Nil; + error("atom takes only 1 argument", (*list)->line_num); + Obj *values = eval_list(root, env, list); + return (values->car->type != TCELL) ? True : Nil; } // (cons expr expr) @@ -530,7 +595,7 @@ static Obj *prim_cons(void *root, Obj **env, Obj **list) { // (car ) static Obj *prim_car(void *root, Obj **env, Obj **list) { Obj *args = eval_list(root, env, list); - if (args->car->type != TCELL || args->cdr != Nil) + if (args == Nil || args->car->type != TCELL || args->cdr != Nil) error("Malformed car", (*list)->line_num); return args->car->car; } @@ -538,7 +603,7 @@ static Obj *prim_car(void *root, Obj **env, Obj **list) { // (cdr ) static Obj *prim_cdr(void *root, Obj **env, Obj **list) { Obj *args = eval_list(root, env, list); - if (args->car->type != TCELL || args->cdr != Nil) + if (args == Nil || args->car->type != TCELL || args->cdr != Nil) error("Malformed cdr", (*list)->line_num); return args->car->cdr; } @@ -575,16 +640,16 @@ static Obj *prim_while(void *root, Obj **env, Obj **list) { *cond = (*list)->car; while (eval(root, env, cond) != Nil) { *exprs = (*list)->cdr; - eval_list(root, env, exprs); + progn(root, env, exprs); } return Nil; } // (gensym) static Obj *prim_gensym(void *root, Obj **env, Obj **list) { - static int count = 0; - char buf[10]; - snprintf(buf, sizeof(buf), "G__%d", count++); + static unsigned long count = 0; + char buf[32]; + snprintf(buf, sizeof(buf), "G__%lu", count++); return make_symbol(root, buf); } @@ -612,68 +677,84 @@ static Obj *prim_length(void *root, Obj **env, Obj **list) { return make_int(root, len); } -// (reverse ... | reverse ) -static Obj *prim_reverse(void *root, Obj **env, Obj **list) { - Obj *args = eval_list(root, env, list); - int len = length(args); - if (len != 1) { - return reverse(args); - } - else { - Obj *car = args->car; - if (car != Nil) { - if (car->type == TCELL) { - return reverse(car); - } - else if(car->type == TSTRING){ - char *left = car->name, - *right = left + strlen(car->name) - 1; - while (left <= right) { - swap(left, right); - left++, right--; - } - } - else { - error("When reverse has a single argument, it must be a list", - (*list)->line_num); - } - } - return car; +// Returns a new list containing the elements of *list in the reverse order. *list is left untouched. +static Obj *reverse_copy(void *root, Obj **list) { + DEFINE3(root, p, head, elem); + *head = Nil; + for (*p = *list; (*p)->type == TCELL; *p = (*p)->cdr) { + *elem = (*p)->car; + *head = cons(root, elem, head); } + return *head; } -#define PRIM_ARITHMETIC_OP(PRIM_OP, OP, OPEQ) \ -static Obj *PRIM_OP(void *root, Obj **env, Obj **list) { \ - Obj *args = eval_list(root, env, list); \ - long long r = args->car->value; \ - for (Obj *p = args->cdr; p != Nil; p = p->cdr) { \ - if (p->car->type != TINT) \ - error(#OP " takes only numbers", (*list)->line_num); \ - r OPEQ p->car->value; \ - } \ - return make_int(root, r); \ +// (reverse ... | reverse | reverse ) +// The arguments are never modified: a new list or string is returned. +static Obj *prim_reverse(void *root, Obj **env, Obj **list) { + DEFINE1(root, args); + *args = eval_list(root, env, list); + if (length(*args) != 1) + return reverse(*args); // The list of evaluated arguments is ours: reverse it in place + *args = (*args)->car; + if (*args == Nil) + return Nil; + if ((*args)->type == TCELL) + return reverse_copy(root, args); + if ((*args)->type == TSTRING) { + size_t len = strlen((*args)->name); + Obj *r = alloc(root, TSTRING, len + 1); + r->line_num = filepos.line_num; + const char *src = (*args)->name; // Read after alloc, which may have moved the string + for (size_t i = 0; i < len; i++) + r->name[i] = src[len - 1 - i]; + r->name[len] = '\0'; + return r; + } + error("When reverse has a single argument, it must be a list or a string", + (*list)->line_num); } -// (+ ...) -PRIM_ARITHMETIC_OP(prim_plus, +, += ) -PRIM_ARITHMETIC_OP(prim_mult, *, *= ) -PRIM_ARITHMETIC_OP(prim_div , /, /= ) -PRIM_ARITHMETIC_OP(prim_modulo, %, %= ) - -// (- ...) -static Obj *prim_minus(void *root, Obj **env, Obj **list) { +// (op ...) with op one of + - * / % +// Arithmetic wraps around on overflow, like the unsigned arithmetic does. +static Obj *arith(void *root, Obj **env, Obj **list, char op) { Obj *args = eval_list(root, env, list); for (Obj *p = args; p != Nil; p = p->cdr) if (p->car->type != TINT) - error("- takes only numbers", (*list)->line_num); - if (args->cdr == Nil) - return make_int(root, -args->car->value); - long long r = args->car->value; - for (Obj *p = args->cdr; p != Nil; p = p->cdr) - r -= p->car->value; - return make_int(root, r); + error("%c takes only numbers", (*list)->line_num, op); + if (args == Nil) { + if (op == '+') return make_int(root, 0); + if (op == '*') return make_int(root, 1); + error("%c takes at least 1 number", (*list)->line_num, op); + } + unsigned long long r = args->car->value; + if (op == '-' && args->cdr == Nil) + return make_int(root, (long long)(0 - r)); + for (Obj *p = args->cdr; p != Nil; p = p->cdr) { + long long v = p->car->value; + switch (op) { + case '+': r += v; break; + case '-': r -= v; break; + case '*': r *= v; break; + default: // '/' and '%' + if (v == 0) + error("Division by zero", (*list)->line_num); + if (v == -1) // Avoids the overflow of LLONG_MIN / -1 + r = op == '/' ? 0 - r : 0; + else + r = op == '/' ? (long long)r / v : (long long)r % v; + } + } + return make_int(root, (long long)r); } +// (+ ...) +static Obj *prim_plus(void *root, Obj **env, Obj **list) { return arith(root, env, list, '+'); } +// (- ...) +static Obj *prim_minus(void *root, Obj **env, Obj **list) { return arith(root, env, list, '-'); } +static Obj *prim_mult(void *root, Obj **env, Obj **list) { return arith(root, env, list, '*'); } +static Obj *prim_div(void *root, Obj **env, Obj **list) { return arith(root, env, list, '/'); } +static Obj *prim_modulo(void *root, Obj **env, Obj **list) { return arith(root, env, list, '%'); } + // (op ) #define PRIM_COMPARISON_OP(PRIM_OP, OP) \ static Obj *PRIM_OP(void *root, Obj **env, Obj **list) { \ @@ -701,28 +782,49 @@ static Obj *prim_not(void *root, Obj **env, Obj **list) { return values->car == Nil ? True : Nil; } -extern void process_file(char *fname, Obj **env, Obj **expr); +// Returns a newly allocated path for a file to load. A relative path is first looked up in the +// directory of the file being loaded, then in the current directory. +__attribute__((noinline)) static char *resolve_path(const char *name) { + const char *current = filepos.filename; + const char *slash = current ? strrchr(current, '/') : NULL; + if (name[0] != '/' && slash) { + size_t dirlen = slash - current + 1; + char *path = malloc(dirlen + strlen(name) + 1); + if (path) { + memcpy(path, current, dirlen); + strcpy(path + dirlen, name); + if (access(path, R_OK) == 0) + return path; + free(path); + } + } + return strdup(name); +} static Obj *prim_load(void *root, Obj **env, Obj **list) { DEFINE1(root, expr); Obj *args = eval_list(root, env, list); - if (args->car->type != TSTRING){ + if (length(args) != 1 || args->car->type != TSTRING) error("load: filename must be a string", (*list)->line_num); - } - char *name = args->car->name; - - // Save old context and set up new one for error handling + // The name must be copied: the string lives in the GC heap and would move. + char *name = resolve_path(args->car->name); + if (!name) + error("Out of memory in load", (*list)->line_num); + + // Save the old context and set up a new one for error handling jmp_buf old_context; memcpy(&old_context, &context, sizeof(jmp_buf)); - - if (setjmp(context) == 0) { - filepos_t calling_file = filepos; - process_file(name, env, expr); - filepos = calling_file; - } - - // Restore old context + filepos_t calling_file = filepos; + reader_t calling_reader = reader; + + if (setjmp(context) == 0) + process_file(root, name, env, expr); + + // Restore the old context, the position in the calling file and the calling text memcpy(&context, &old_context, sizeof(jmp_buf)); + filepos = calling_file; + reader = calling_reader; + free(name); return Nil; } @@ -789,16 +891,20 @@ static Obj *prim_macroexpand(void *root, Obj **env, Obj **list) { return macroexpand(root, env, body); } -// (print ...) +// (print expr ...) +// Evaluates and prints each argument, separated by a space. static Obj *prim_print(void *root, Obj **env, Obj **list) { - DEFINE1(root, tmp); - *tmp = (*list)->car; - print(eval(root, env, tmp)); + DEFINE2(root, lp, tmp); + for (*lp = *list; (*lp)->type == TCELL; *lp = (*lp)->cdr) { + if (*lp != *list) + fputc(' ', stdout); + *tmp = (*lp)->car; + print(eval(root, env, tmp)); + } return Nil; } - -// (println ...) +// (println expr ...) static Obj *prim_println(void *root, Obj **env, Obj **list) { prim_print(root, env, list); fputc('\n', stdout); @@ -831,6 +937,8 @@ static Obj *prim_eq(void *root, Obj **env, Obj **list) { Obj *values = eval_list(root, env, list); Obj *first = values->car; Obj *second = values->cdr->car; + if (first->type == TINT && second->type == TINT) + return first->value == second->value ? True : Nil; if (first->type == TSTRING){ if (second->type == TSTRING) return strcmp(first->name, second->name) == 0 ? True : Nil; @@ -841,46 +949,44 @@ static Obj *prim_eq(void *root, Obj **env, Obj **list) { } // String primitives +static size_t int_to_str(char *buf, long long val) { + return (size_t)snprintf(buf, 24, "%lld", val); +} + +// (string-concat ...) static Obj *prim_string_concat(void *root, Obj **env, Obj **list) { - Obj *args = eval_list(root, env, list); - + DEFINE1(root, args); + *args = eval_list(root, env, list); + char num[24]; + // First pass: calculate total length needed - size_t total_len = 1; // Start with 1 for null terminator - for (Obj *p = args; p != Nil; p = p->cdr) { - if (p->car->type != TSTRING && p->car->type != TINT) - error("string-concat arguments must be strings or numbers", - (*list)->line_num); - if (p->car->type == TINT) { - long long val = p->car->value; - char var[22]; - snprintf(var, sizeof(var), "%lld", val); - total_len += strlen(var); - } - else { + size_t total_len = 0; + for (Obj *p = *args; p != Nil; p = p->cdr) { + if (p->car->type == TINT) + total_len += int_to_str(num, p->car->value); + else if (p->car->type == TSTRING) total_len += strlen(p->car->name); - } + else + error("string-concat arguments must be strings or numbers", (*list)->line_num); } - - char *buf = malloc(total_len); - if (!buf) - error("Out of memory in string-concat", (*list)->line_num); - buf[0] = '\0'; - - // Second pass: concatenate all strings - for (Obj *p = args; p != Nil; p = p->cdr) { + + // The result is written directly in the new string. alloc may move the arguments, which + // are reachable through the root. + Obj *result = alloc(root, TSTRING, total_len + 1); + result->line_num = filepos.line_num; + + // Second pass: copy all the strings + char *out = result->name; + for (Obj *p = *args; p != Nil; p = p->cdr) { if (p->car->type == TINT) { - long long val = p->car->value; - char var[22]; - snprintf(var, sizeof(var), "%lld", val); - strcat(buf, var); - } - else { - strcat(buf, p->car->name); + out += int_to_str(out, p->car->value); + } else { + size_t n = strlen(p->car->name); + memcpy(out, p->car->name, n); + out += n; } } - - Obj *result = make_string(root, buf); - free(buf); + *out = '\0'; return result; } @@ -888,22 +994,31 @@ static Obj *prim_symbol_to_string(void *root, Obj **env, Obj **list) { Obj *args = eval_list(root, env, list); if (length(args) != 1) error("symbol->string requires 1 argument", (*list)->line_num); - + if (args->car->type != TSYMBOL) error("symbol->string argument must be a symbol", (*list)->line_num); - - return make_string(root, args->car->name); + + // The name is copied out of the GC heap before allocating + char name[SYMBOL_MAX_LEN + 1]; + strncpy(name, args->car->name, sizeof(name) - 1); + name[sizeof(name) - 1] = '\0'; + return make_string(root, name); } static Obj *prim_string_to_symbol(void *root, Obj **env, Obj **list) { Obj *args = eval_list(root, env, list); if (length(args) != 1) error("string->symbol requires 1 argument", (*list)->line_num); - + if (args->car->type != TSTRING) error("string->symbol argument must be a string", (*list)->line_num); - - return intern(root, args->car->name); + if (strlen(args->car->name) > SYMBOL_MAX_LEN) + error("Symbol name too long", (*list)->line_num); + + // The name is copied out of the GC heap before allocating + char name[SYMBOL_MAX_LEN + 1]; + strcpy(name, args->car->name); + return intern(root, name); } static Obj *prim_exit(void *root, Obj **env, Obj **list) { @@ -912,7 +1027,7 @@ static Obj *prim_exit(void *root, Obj **env, Obj **list) { Obj *values = eval_list(root, env, list); Obj *first = values->car; if (first->type != TINT) - error("* must be an integer", (*list)->line_num); + error("exit takes an integer", (*list)->line_num); exit(first->value); } @@ -929,33 +1044,37 @@ static void define_constants(void *root, Obj **env) { add_variable(root, env, sym, &True); } -static size_t read_file(char *fname, char **text) { - size_t length = 0; - FILE *f = fopen(fname, "r"); +// Reads the whole file in a newly allocated buffer. Returns its length, or -1 on failure. +static long read_file(char *fname, char **text) { + *text = NULL; + FILE *f = fopen(fname, "rb"); if (!f) { printf("Failed to load file %s\n", fname); - return 0; + return -1; } - filepos.filename = fname; fseek(f, 0, SEEK_END); - length = ftell(f); + long length = ftell(f); fseek(f, 0, SEEK_SET); - + if (length < 0) { + printf("Failed to read file %s\n", fname); + fclose(f); + return -1; + } + *text = malloc(length + 1); if (!*text) { puts("Out of memory."); fclose(f); - return 0; + return -1; } - size_t read = fread(*text, 1, length, f); - if (read != length) { + if (fread(*text, 1, length, f) != (size_t)length) { printf("Failed to read entire file %s\n", fname); free(*text); *text = NULL; fclose(f); - return 0; + return -1; } (*text)[length] = '\0'; @@ -963,36 +1082,25 @@ static size_t read_file(char *fname, char **text) { return length; } -void process_file(char *fname, Obj **env, Obj **expr) { +// Evaluates all the expressions of a file. fname must stay valid until the function returns. +void process_file(void *root, char *fname, Obj **env, Obj **expr) { char *text = NULL; - size_t len = read_file(fname, &text); - if (len == 0) return; - - // Save old stdin - FILE *old_stdin = stdin; - // Create a single stream for the entire file - FILE *stream = fmemopen(text, len, "r"); - if (!stream) { - free(text); - error("Failed to create memory stream for %s", (*expr)->line_num, fname); - return; - } + long len = read_file(fname, &text); + if (len < 0) return; - // Redirect stdin to the memory stream - stdin = stream; + filepos_t calling_file = filepos; + reader_t calling_reader = reader; + set_input(text, len); filepos.filename = fname; filepos.file_len = len; filepos.line_num = 1; - int eval_input(void *root, Obj **env, Obj **expr); - // Process expressions until we reach end of file - while (!feof(stream)) { - eval_input(gc_root, env, expr); - } + // eval_input stops at the first error: go on with the rest of the file + while (reader.pos < reader.len) + eval_input(root, env, expr); - // Cleanup - stdin = old_stdin; - fclose(stream); + reader = calling_reader; + filepos = calling_file; free(text); } @@ -1015,7 +1123,6 @@ static void define_primitives(void *root, Obj **env) { add_primitive(root, env, "length", prim_length); add_primitive(root, env, "reverse", prim_reverse); add_primitive(root, env, "<", prim_lt); - add_primitive(root, env, "<", prim_lt); add_primitive(root, env, ">", prim_gt); add_primitive(root, env, "<=", prim_lte); add_primitive(root, env, ">=", prim_gte); @@ -1042,18 +1149,18 @@ static void define_primitives(void *root, Obj **env) { // Entry point //====================================================================== -extern void *memory; - void init_minilisp(Obj **env) { // Memory allocation - extern void *alloc_semispace(); - memory = alloc_semispace(); + gc_init(); + init_small_ints(); + init_stack_limit(); - // Constants and primitives + // Constants and primitives. env is a slot of the caller's root frame (gc_root), which must be + // used as the root: the environment would not survive a GC otherwise. Symbols = Nil; - *env = make_env(NULL, &Nil, &Nil); - define_constants(NULL, env); - define_primitives(NULL, env); + *env = make_env(gc_root, &Nil, &Nil); + define_constants(gc_root, env); + define_primitives(gc_root, env); } int eval_input(void *root, Obj **env, Obj **expr) { @@ -1063,9 +1170,9 @@ int eval_input(void *root, Obj **env, Obj **expr) { if (!*expr) return 0; if (*expr == Cparen) - error("Stray close parenthesis", (*expr)->line_num); + error("Stray close parenthesis", filepos.line_num); if (*expr == Dot) - error("Stray dot", (*expr)->line_num); + error("Stray dot", filepos.line_num); print(eval(root, env, expr)); putc('\n', stdout); } diff --git a/src/minilisp.h b/src/minilisp.h index d264226..9cf8621 100644 --- a/src/minilisp.h +++ b/src/minilisp.h @@ -84,9 +84,18 @@ typedef struct { int line_num; } filepos_t; -void error(char *fmt, int line_num, ...); +// Reports an error and jumps back to the end of the current top-level evaluation. +void error(const char *fmt, int line_num, ...) __attribute__((noreturn, format(printf, 1, 3))); void init_minilisp(Obj **env); -int eval_input(void *input, Obj **env, Obj **expr); + +// Sets the text the reader will consume. The text must stay valid until it is fully evaluated. +void set_input(const char *text, size_t len); + +// Evaluates all the expressions of the current input. Errors are reported on stderr. +int eval_input(void *root, Obj **env, Obj **expr); + +// Loads and evaluates a file. +void process_file(void *root, char *fname, Obj **env, Obj **expr); #endif // _MINILISP_H_ diff --git a/src/repl.c b/src/repl.c index 308c178..576a494 100644 --- a/src/repl.c +++ b/src/repl.c @@ -3,6 +3,7 @@ #include #include #endif +#include #include #include "../bestline/bestline.h" #include "ketopt.h" @@ -52,21 +53,15 @@ char *hints(const char *buf, const char **ansi1, const char **ansi2) { // This struct keeps track of the current file/line being evaluated filepos_t filepos = {"", 0, 1}; +static bool no_history = false; + void minilisp(char *text, size_t length, bool with_repl, Obj **env, Obj **expr) { - // Process file + // Process the code given on the command line if(text != NULL){ - // Save old stdin - FILE * old_stdin = stdin; - // Open a memory stream - FILE * stream = fmemopen(text, length, "r"); - // Redirect stdin to the in memory stream in order to use getchar() - stdin = stream; - eval_input(text, env, expr); + set_input(text, length); + eval_input(gc_root, env, expr); free(text); - // restore stdin - stdin = old_stdin; - fclose(stream); } // REPL @@ -75,23 +70,24 @@ void minilisp(char *text, size_t length, bool with_repl, Obj **env, Obj **expr) char prompt[15] = ""; filepos = (filepos_t){ .filename = "", .file_len = 0, .line_num = 1 }; sprintf(prompt, "%d:", promptnum); + errno = 0; char *line = bestline(prompt); - if(line == NULL) continue; - // Save old stdin - FILE * old_stdin = stdin; - // Open a memory stream - FILE * stream = fmemopen(line, strlen(line), "r"); - // Redirect stdin to the in memory stream in order to use getchar() - stdin = stream; - + if (line == NULL) { + if (errno == EAGAIN) // Ctrl-C + continue; + break; // end of input (Ctrl-D) + } + if (line[0] != '\0' && line[0] != '/') { - eval_input(line, env, expr); - bestlineHistoryAdd(line); - bestlineHistorySave("history.txt"); + set_input(line, strlen(line)); + eval_input(gc_root, env, expr); + if (!no_history) { + bestlineHistoryAdd(line); + bestlineHistorySave("history.txt"); + } } else if (line[0] == '/') { if (!strncmp(line, "/memory", 7)){ - extern size_t mem_nused; - printf("Memory used: %ld / Total: %d\n", mem_nused, MEMORY_SIZE); + printf("Memory used: %zu / Total: %zu\n", mem_nused, heap_size); } else if (!strncmp(line, "/help", 5)){ puts("Type Ctrl-C to quit."); @@ -104,14 +100,10 @@ void minilisp(char *text, size_t length, bool with_repl, Obj **env, Obj **expr) } free(line); - // restore stdin - stdin = old_stdin; - fclose(stream); } } -static bool no_history = false; static char *one_liner = NULL; static int num_files = 0; static char **filenames; @@ -145,7 +137,8 @@ void parse_args(int argc, char **argv) { (allows distinguishing between missing required argument and unknown option) */ while (true) { - int c = ketopt(&option, argc, argv, 0, "-:x:Hrh", long_options); + // permute: the options may be given after the file names + int c = ketopt(&option, argc, argv, 1, "-:x:Hrh", long_options); if (c == -1) break; int idx = option.longidx; @@ -202,8 +195,8 @@ void parse_args(int argc, char **argv) { // regular arguments: run files num_files = argc - option.ind; filenames = (char **) malloc(num_files * sizeof(char *)); - for (int i = 1; i <= num_files; i++) { - filenames[i-1] = strdup(argv[i]); + for (int i = 0; i < num_files; i++) { + filenames[i] = strdup(argv[option.ind + i]); } } @@ -216,8 +209,7 @@ int main(int argc, char **argv) { for (int i = 0; i < num_files; i++) { printf("Loading %s\n", filenames[i]); - void process_file(char *fname, Obj **env, Obj **expr); - process_file(filenames[i], env, expr); + process_file(gc_root, filenames[i], env, expr); free(filenames[i]); } free(filenames); diff --git a/test.sh b/test.sh index 4420da0..5b29847 100755 --- a/test.sh +++ b/test.sh @@ -163,6 +163,9 @@ run counter 3 ' (counter) (counter)' +run print 'hello world 3()' '(print "hello" "world" 3)' +run println '()' '(println)' + run progn 'I own 10 cents()' '(progn (print "I own ") (defun add(x y)(+ x y)) (print (add 3 7)) diff --git a/test3 b/test3 new file mode 100644 index 0000000..8773d54 --- /dev/null +++ b/test3 @@ -0,0 +1,48 @@ +; test include +(load "examples/library.lisp") + + +; Make a set (remove duplicates) +(reduce (lambda (acc x) + (if (member acc x) + acc + (cons x acc))) + '(1 2 2 3 1 4 3 5) + ()) + +; sort +(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8)) +(println(quicksort (iota 100))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8 12 35 48 453 75 90 53 90 456785 785 789))) +(println(quicksort (reverse (iota 100)))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8 12 35 48 453 75 90 53 90 456785 785 789))) +(println(quicksort (reverse (iota 100)))) + +(println(reduce (lambda (acc x) + (if (member acc x) + acc + (cons x acc))) + '(1 2 2 3 1 4 3 5) + ())) + (println(quicksort (iota 100))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8 12 35 48 453 75 90 53 90 456785 785 789))) +(println(quicksort (iota 100))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8 12 35 48 453 75 90 53 90 456785 785 789))) +(println(quicksort (iota 100))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8 12 35 48 453 75 90 53 90 456785 785 789))) +(println(quicksort (reverse (iota 100)))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8))) +(println(quicksort '(3 1 4 1 5 9 2 8 5 3 5 8 9 8 12 35 48 453 75 90 53 90 456785 785 789))) +(println(quicksort (reverse (iota 130)))) +(println(reduce (lambda (acc x) + (if (member acc x) + acc + (cons x acc))) + '(1 2 2 3 1 4 3 5) + ())) +(println(quicksort (iota 100))) \ No newline at end of file