<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
	    xml:lang="en-US" 
	    xml:base="http://peter.michaux.ca">
  <id>http://peter.michaux.ca/</id>
  <title>peter.michaux.ca</title>
  <updated>2010-01-27T18:58:00-08:00</updated>
  <link href="/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="/" rel="alternate" type="text/html"/>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-conclusion</id>
    <title>Scheme from Scratch - Bootstrap - Conclusion</title>
    <updated>2010-01-27T18:58:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-conclusion" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The main goal of this Scheme from Scratch series, as the title implies, was to record the creation of a bootstrap Scheme interpreter in a low-level language like C. This bootstrap interpreter could to be used to self-compile a Scheme compiler written in Scheme. It took about three weeks and the interpter&amp;rsquo;s code is less than a couple thousand lines lines of code. It is not efficient code but it is straightforward and easy to understand. It doesn&amp;rsquo;t have macros or continuations but it is enough to write a compiler. It does have closures so it has a better feature set then some languages do.&lt;/p&gt;

&lt;h2&gt;Going Meta-Circular&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;read&lt;/code&gt;, &lt;code&gt;eval&lt;/code&gt;, and &lt;code&gt;write&lt;/code&gt; primitives in Scheme make it very easy to write a little Scheme REPL in Scheme. It almost feels like cheating.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme 
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (define env (environment))
ok
&gt; (define (repl)
    (write-char #\])
    (write-char #\space)
    (write (eval (read) env))
    (write-char #\newline)
    (repl))
ok
&gt; (repl)
] (cons 1 2)
(1 . 2)
] (define (double x) (* 2 x))
ok
] (double 21)
42&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;The bootstrap interpreter was modeled after the first meta-circular interpreter in SICP. The ability to implement a programming language in our new interpreter implementation is a great test. You can download the &lt;a href=&quot;http://mitpress.mit.edu/sicp/code/ch4-mceval.scm&quot;&gt;SICP meta-circular evaluator&lt;/a&gt; (&lt;a href=&quot;/cache/ch4-mceval.scm&quot;&gt;cache&lt;/a&gt;) and run it yourself. I first wrote a little helper file called &lt;code&gt;run-meta-circular.scm&lt;/code&gt; to load and start everything that is required.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;; Add standard definitions to complete our 
; Scheme implementation: cadr, length, etc.
(load &quot;stdlib.scm&quot;)

; Add extra definitions needed by
; the meta-circular implementation.
(define true #t)
(define false #f)
; Cheat a bit but actually results
; in a better REPL
(define display write)
(define newline (lambda () (write-char #\newline)))

; Load the meta-circular implementation
; exactly as it appears in SICP.
(load &quot;ch4-mceval.scm&quot;)

; Run the meta-circular interpreter.
; These lines appear but are commented out 
; in the meta-circular implementation.
(define the-global-environment (setup-environment))
(driver-loop)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With that file it is easy to fire up the meta-circular interpreter exactly as it is written in SICP.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (load &quot;run-meta-circular.scm&quot;)


&quot;;;; M-Eval input:&quot;
(define a (cons 1 2))

&quot;;;; M-Eval value:&quot;
ok

&quot;;;; M-Eval input:&quot;
(car a)

&quot;;;; M-Eval value:&quot;
1

&quot;;;; M-Eval input:&quot;
((lambda (x) x) 21)

&quot;;;; M-Eval value:&quot;
21&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately the SICP code doesn&amp;rsquo;t define many primitives in the meta-circular interrupter so the above example is not too rich. If you expand the interpreter you can just fill your boots with meta-circular goodness.&lt;/p&gt;

&lt;p&gt;It was really great to see in the GitHub feeds that other people were running the meta-circular interpreter days ago and I hadn&amp;rsquo;t even mentioned it yet.&lt;/p&gt;

&lt;p style=&quot;text-align:center;&quot;&gt;- - - -&lt;/p&gt;

&lt;p&gt;The reason for documenting this whole adventure was I couldn&amp;rsquo;t find a book that I could just buy and read implementing Scheme in C. Maybe one exists and I didn&amp;rsquo;t search hard enough. Maybe one should still be written. Maybe these articles has for will fill the need for at least a few readers. Regardless it has been fun documenting it and watching other enjoy implementing their own interpreters along the way. Enjoy your interpreter!&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_22-garbage-collection&quot;&gt;Garbage Collection&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_22-garbage-collection</id>
    <title>Scheme from Scratch - Bootstrap v0.22 - Garbage Collection</title>
    <updated>2010-01-26T10:15:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_22-garbage-collection" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;Garbage collection is an essential part of a production Scheme system. Bootstrap Scheme is not a production Scheme system. It is intended to be just enough of a Scheme interpreter to execute a Scheme compiler once. If your computer has enough memory that it won&amp;rsquo;t run out during the execution of a compiler than you don&amp;rsquo;t need to implement a garbage collector in Bootstrap Scheme.&lt;/p&gt;

&lt;p&gt;I think that the simplest garbage collection system you could add to the interpreter is a mark-and-sweep system. Mark-and-sweep is easier to implement than many collectors, isn&amp;rsquo;t foiled by circular references, and doesn&amp;rsquo;t require hardware-specific knowledge. The garbage collection pauses in a mark-and-sweep system can be noticeably long but that doesn&amp;rsquo;t matter for a bootstrap interpreter.&lt;/p&gt;

&lt;p&gt;The C function &lt;code&gt;alloc_object&lt;/code&gt; in the interpreter currently calls &lt;code&gt;malloc&lt;/code&gt; for every object allocated and doesn&amp;rsquo;t record anything about what memory has been allocated. The fundamental goal is to replace &lt;code&gt;alloc_object&lt;/code&gt; so a new version of it manipulates a heap of preallocated objects that can be recycled.&lt;/p&gt;

&lt;p&gt;This page describes one way to implement a mark-and-sweep algorithm. This is basically the mark-and-sweep algorithm I&amp;rsquo;ve seen in use in some interpreters.&lt;/p&gt;

&lt;p&gt;When the interpreter begins, the C &lt;code&gt;init&lt;/code&gt; function in the model layer is executed. This is the right place to set up a heap of Scheme objects. You can statically allocate or dynamically &lt;code&gt;malloc&lt;/code&gt; enough space for thousands of Scheme objects (i.e. &lt;code&gt;struct object&lt;/code&gt;). Each object needs a new field, &lt;code&gt;char&amp;nbsp;mark&lt;/code&gt;, that must be set to &lt;code&gt;0&lt;/code&gt; during initialization. Each object also needs a &lt;code&gt;struct&amp;nbsp;object&amp;nbsp;*next&lt;/code&gt; field to be used for tracking which memory is free or active. At the beginning, all memory is free. Link all the heap objects together into a linked list with the &lt;code&gt;next&lt;/code&gt; field. The last object in the list can point to C&amp;rsquo;s &lt;code&gt;NULL&lt;/code&gt;. Assign the head of this linked list to a C global variable &lt;code&gt;free_list&lt;/code&gt;. There is also a global &lt;code&gt;active_list&lt;/code&gt; that is set to &lt;code&gt;NULL&lt;/code&gt; now and that will hold all objects in use by the program.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;alloc_object&lt;/code&gt; is called, it looks to see if the &lt;code&gt;free_list&lt;/code&gt; has an available object. If the list does have an available object then &lt;code&gt;alloc_object&lt;/code&gt; moves that object from the &lt;code&gt;free_list&lt;/code&gt; to the &lt;code&gt;active_list&lt;/code&gt; and returns that object. If the &lt;code&gt;free_list&lt;/code&gt; is empty then &lt;code&gt;alloc_object&lt;/code&gt; calls a &lt;code&gt;gc&lt;/code&gt; function to run the mark-and-sweep algorithm. When &lt;code&gt;gc&lt;/code&gt; returns, &lt;code&gt;alloc_object&lt;/code&gt; can recheck for available objects on the &lt;code&gt;free_list&lt;/code&gt; and return one if there is one or print an error message and &lt;code&gt;exit&lt;/code&gt; the program.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;gc&lt;/code&gt; function is reasonably simple with its two phases: mark and sweep.&lt;/p&gt;

&lt;p&gt;In the mark phase, the garbage collector starts at all the &amp;ldquo;root&amp;rdquo; objects (described below) and walks all reachable memory (e.g. pairs can point to other objects) and marks each object. Each reachable object will have its mark field set to &lt;code&gt;1&lt;/code&gt;. When all reachable objects are marked, then move on to the sweep phase.&lt;/p&gt;

&lt;p&gt;In the sweep phase, the garbage collector starts at the top of the &lt;code&gt;active_list&lt;/code&gt; and examines each object in that list. Objects that have &lt;code&gt;mark&lt;/code&gt; set to &lt;code&gt;0&lt;/code&gt; are moved to the &lt;code&gt;free_list&lt;/code&gt;. (Symbols and strings also need to have their &lt;code&gt;data.symbol.value&lt;/code&gt; field, for example, &lt;code&gt;free&lt;/code&gt;d.) Objects in the &lt;code&gt;active_list&lt;/code&gt; that have &lt;code&gt;mark&lt;/code&gt; set to &lt;code&gt;1&lt;/code&gt; have their &lt;code&gt;mark&lt;/code&gt; set back to &lt;code&gt;0&lt;/code&gt; in preparation for the next mark phase.&lt;/code&gt;
  
&lt;p&gt;All of that is pretty easy and nicely contained to a very small area of the C source code. Where everything becomes messy is keeping track of the roots for the mark-and-sweep algorithm. The root objects are the ones where the mark phase starts. These are objects that may not have any other objects pointing to them but should still not be garbage collected. &lt;code&gt;the_global_environment&lt;/code&gt; is an example of a known root object. It is easy to have the sweep phase start at all the known roots.&lt;/p&gt;

&lt;p&gt;What isn&amp;rsquo;t easy is managing the stack roots. Have a look at the following C function, for example,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object *make_if(object *predicate, object *consequent,
                object *alternative) {
    return cons(if_symbol,
                cons(predicate,
                     cons(consequent,
                          cons(alternative,
                               the_empty_list))));
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are a total of four calls to &lt;code&gt;cons&lt;/code&gt;. Each call to &lt;code&gt;cons&lt;/code&gt; will call &lt;code&gt;alloc_object&lt;/code&gt;. Each call to &lt;code&gt;alloc_object&lt;/code&gt; can potentially trigger a round of garbage collection. The objects returned by the three inner &lt;code&gt;cons&lt;/code&gt; calls above are not rooted. That is, there is no known garbage collection root, that when followed, will lead to those returned objects. We need a way to protect the returned objects so that an outer call to &lt;code&gt;cons&lt;/code&gt; doesn&amp;rsquo;t move the previous returned value back to the &lt;code&gt;free_list&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We need to maintain a global &lt;code&gt;stack_root&lt;/code&gt; list of these otherwise unprotected objects. The mark phase can then use these &lt;code&gt;stack_root&lt;/code&gt; objects as additional roots. In order to do this, the &lt;code&gt;make_if&lt;/code&gt; function needs to be changed to push and pop from the &lt;code&gt;stack_root&lt;/code&gt; list.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object *make_if(object *predicate, object *consequent,
                object *alternative) {
    object *result;
    
    push_stack_root(&amp;amp;result);
    result = cons(alternative, the_empty_list);
    result = cons(consequent, result);
    result = cons(predicate, result);
    result = cons(if_symbol, result);
    pop_stack_root();
    return result;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ugg. Not pretty, is it? What is neat, and keeps the implementation from being even uglier, is that it is a &lt;em&gt;pointer&lt;/em&gt; to the &lt;code&gt;result&lt;/code&gt; pointer that is pushed onto the &lt;code&gt;stack_root&lt;/code&gt; list. That way, anything pointed to by &lt;code&gt;result&lt;/code&gt; is protected. You&amp;rsquo;ll notice that the &lt;code&gt;result&lt;/code&gt; isn&amp;rsquo;t protected when it is returned to &lt;code&gt;make_if&lt;/code&gt;&amp;rsquo;s caller. It is up to that caller to protect the returned object.&lt;/p&gt;

&lt;p&gt;If you forget to protect a stack variable you can have dangling pointer. If you forget to pop a stack root you will have a memory leak. The garbage collection code is complex and invades almost all functions. Code with care!&lt;/p&gt;

&lt;p&gt;I think you can see why I didn&amp;rsquo;t cloud the implementation up to now with garbage collection worries. Focusing on the model, reader and evaluation layers was plenty as they were being introduced. I think worrying about garbage collection along the way would have made things intimidating and less fun.&lt;/p&gt;

&lt;p&gt;Other types of garbage collectors, with hardware-specific knowledge, allow you to avoid making this mess. The &lt;a href=&quot;http://www.hpl.hp.com/personal/Hans_Boehm/gc/&quot;&gt;Bohem conservative garbage collector&lt;/a&gt; is one such library you could link to your interpreter. If you have a look in the source code of that library you will probably be more frightened by it than you are by the approach I&amp;rsquo;ve presented.&lt;/p&gt;

&lt;p&gt;One more article tomorrow to wrap up things.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_21-standard-library&quot;&gt;Standard Library&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-conclusion&quot;&gt;Conclusion&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_21-standard-library</id>
    <title>Scheme from Scratch - Bootstrap v0.21 - Standard Library</title>
    <updated>2010-01-25T10:35:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_21-standard-library" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;With I/O in place, especially the &lt;code&gt;load&lt;/code&gt; form, we can continue to expand our Scheme implementation by programming new compound procedures in Scheme. The standard library defines forms like &lt;code&gt;cadr&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;not&lt;/code&gt;, etc. Feel free to add whatever you think you would like to have available.&lt;/p&gt;

&lt;p&gt;A sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (load &quot;stdlib.scm&quot;)
stdlib-loaded
&gt; (define (double x) (* 2 x))
ok
&gt; (map double '(0 1 2 3)) 
(0 2 4 6)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is no C programming for this version. That feels like a move in the right direction given a bootstrap interpreter is to eventually allow programming in the implemented language.&lt;/p&gt;

&lt;p&gt;Three weeks to the day and even though there isn&amp;rsquo;t garbage collection, the functionality of the language is complete. As long as programs are not too big, run too long, or process too much data, the experience of using your bootstrap interpreter is what it set out to be. Sit back and enjoy watching it run. :-)&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.21&quot;&gt;v0.21 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_20-io&quot;&gt;I/O&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_22-garbage-collection&quot;&gt;Garbage Collection&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_20-io</id>
    <title>Scheme from Scratch - Bootstrap v0.20 - I/O</title>
    <updated>2010-01-24T15:00:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_20-io" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The last major missing piece of the puzzle for a bootstrap interpreter is the ability to work with files. Scheme has several input and output primitive procedures and I&amp;rsquo;m implementing the ones I think will be useful. It is a slightly larger amount to implement than some days have been but hopefully quite straightforward for anyone who has come this far.&lt;/p&gt;

&lt;p&gt;input:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;load&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;read&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;read-char&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;peek-char&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;input-port?&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;open-input-file&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;close-input-file&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;eof-object?&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;output:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;write&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;write-char&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;output-port?&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;open-output-file&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;close-output-file&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;error&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (define out (open-output-port &quot;asdf.txt&quot;))
ok
&gt; (write-car #\c out)
ok
&gt; (close-output-port out)
ok
&gt; (load &quot;program.scm&quot;)
program-loaded
&gt; (error &quot;bad move&quot;)
&quot;bad move&quot;
exiting&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Some of these functions required refactoring in other areas of the interpreter. I refactored the C &lt;code&gt;read&lt;/code&gt; function to handle the end of a source file properly. I refactored the C &lt;code&gt;write&lt;/code&gt; function to take a stream parameter so that the Scheme &lt;code&gt;write&lt;/code&gt; could write to any port. I&amp;rsquo;ll backport these changes to previous versions eventually.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m not doing any of the the &lt;code&gt;current-input-port&lt;/code&gt;, &lt;code&gt;call-with-input-port&lt;/code&gt;, and &lt;code&gt;with-input-from-file&lt;/code&gt; business. If a port is not specified as an optional parameter to &lt;code&gt;read&lt;/code&gt;, &lt;code&gt;write-char&lt;/code&gt;, etc then C&amp;rsquo;s &lt;code&gt;stdin&lt;/code&gt; and &lt;code&gt;stdout&lt;/code&gt; are used.&lt;/p&gt;

&lt;p&gt;I added the &lt;code&gt;error&lt;/code&gt; output form which &lt;code&gt;write&lt;/code&gt;s all of its arguments and then exits. This form is not required by R5RS, for example, but is useful.&lt;/p&gt;

&lt;p&gt;There is still a little bit to do but we are oh so close.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.20&quot;&gt;v0.20 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_19-eval&quot;&gt;Eval&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_21-standard-library&quot;&gt;Standard Library&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_19-eval</id>
    <title>Scheme from Scratch - Bootstrap v0.19 - Eval</title>
    <updated>2010-01-23T10:36:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_19-eval" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The &lt;code&gt;eval&lt;/code&gt; form is a bit like &lt;code&gt;apply&lt;/code&gt;. They are both procedures with an unusual tail call requirements.&lt;/p&gt;

&lt;p&gt;The second argument to &lt;code&gt;eval&lt;/code&gt; is an environment, so exposing &lt;code&gt;eval&lt;/code&gt; also requires exposing some environment-related procedures. All together I implemented the following procedures for this version:&lt;/p&gt;

&lt;dl&gt;
  &lt;dt&gt;&lt;code&gt;interaction-environment&lt;/code&gt;&lt;/dt&gt;
  &lt;dd&gt;Returns the top-level environment of the current REPL session.&lt;/dd&gt;
  &lt;dt&gt;&lt;code&gt;null-environment&lt;/code&gt;&lt;/dt&gt;
  &lt;dd&gt;Returns an empty environment with only syntactic keywords like &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;define&lt;/code&gt;, etc.&lt;/dd&gt;
  &lt;dt&gt;&lt;code&gt;environment&lt;/code&gt;&lt;/dt&gt;
  &lt;dd&gt;Returns a new environment like the environment present when the REPL starts.&lt;/dd&gt;
  &lt;dt&gt;&lt;code&gt;eval&lt;/code&gt;&lt;/dt&gt;
  &lt;dd&gt;Evaluates an expression in a given environment.&lt;/dd&gt;
&lt;/ul&gt;

&lt;p&gt;Sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (define env (environment))
ok
&gt; (eval '(define z 25) env)
ok
&gt; (eval 'z env)
25&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I used the same trick for &lt;code&gt;eval&lt;/code&gt; as I did for &lt;code&gt;apply&lt;/code&gt; yesterday. There is a dummy primitive procedure but the exposed Scheme &lt;code&gt;eval&lt;/code&gt; procedure lives inside the C &lt;code&gt;eval&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;null-environment&lt;/code&gt; and &lt;code&gt;environment&lt;/code&gt; procedures required a bit of refactoring in the model initialization. At some point I may backport those changes to previous versions with environments as it is just good programming anyway.&lt;/p&gt;

&lt;p&gt;It feels like we are on the home stretch. There isn't much remaining to complete the bootstrap interpreter and none of it is very difficult.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.19&quot;&gt;v0.19 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_18-apply&quot;&gt;Apply&lt;/a&gt;&lt;br&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_20-io&quot;&gt;I/O&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_18-apply</id>
    <title>Scheme from Scratch - Bootstrap v0.18 - Apply</title>
    <updated>2010-01-22T09:57:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_18-apply" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The &lt;code&gt;apply&lt;/code&gt; form is an interesting beast in Scheme. It is a procedure so can be passed around and assigned to variables. It has a special tail call requirement that most other primitive procedure has and that needs to handled carefully.&lt;/p&gt;

&lt;p&gt;Sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (apply + '(1 2 3))
6
&gt; (apply + 1 2 '(3))
6
&gt; (apply + 1 2 3 '())
6&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first example above is the important one to have. I implemented &lt;code&gt;apply&lt;/code&gt; to allow the last two examples for the fun of the implementation.&lt;/p&gt;

&lt;p&gt;In my implementation, I did set up &lt;code&gt;apply&lt;/code&gt; as a primitive procedure but it is one where the C function for the primitive procedure should never be called. It is a regular primitive procedure because that means apply can be passed around in the Scheme programs. I implemented a small trick in &lt;code&gt;eval&lt;/code&gt; to handle &lt;code&gt;apply&lt;/code&gt; and have the tail call requirement satisfied. I don&amp;rsquo;t want to enter the world of trampolines for my primitive procedures.&lt;/p&gt;

&lt;p&gt;I have a list of what remains. I looks like there are only about six more days of implementation for Bootstrap Scheme and then a wrap-up article.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.18&quot;&gt;v0.18 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_17-and-and-or&quot;&gt;And and Or&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_19-eval&quot;&gt;Eval&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_17-and-and-or</id>
    <title>Scheme from Scratch - Bootstrap v0.17 - And and Or</title>
    <updated>2010-01-21T09:45:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_17-and-and-or" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;One more round of library syntax: the &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt; forms. These could be split into two different versions but they are so similar I think biting them off in one chunk is justified. If you do &lt;code&gt;and&lt;/code&gt; first then &lt;code&gt;or&lt;/code&gt; will be easy.&lt;/p&gt;

&lt;p&gt;Sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (and 1 2 3)
3
&gt; (and)
#t
&gt; (or #f 2 #t)    
2
&gt; (or)
#f&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;An important aspect of &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt; is they are short-circuiting. Here is an example demonstrating the importance of &lt;code&gt;and&lt;/code&gt; short-circuiting when side-effects are involved:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&gt; (define a 1)
ok
&gt; (and #f (set! a 2))
#f
&gt; a
1
&gt; (and #t (set! a 2))
ok
&gt; a
2&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are not already consulting the Scheme spec when implementing features in your interpreter, now might be a good time to give that a try. I have R5RS printed on real paper which is handy.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m not 100% sure but I think it might now be possible to run all the examples in &lt;b&gt;The Little Schemer&lt;/b&gt; in your interpreter.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve enjoyed the watch feature on GitHub to follow other&amp;rsquo;s progress. It is motivating to see implementations moving forward and really great to see people have actually implemented features I haven&amp;rsquo;t discussed yet: &lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;load&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.17&quot;&gt;v0.17 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_16-let&quot;&gt;Let&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_18-apply&quot;&gt;Apply&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_16-let</id>
    <title>Scheme from Scratch - Bootstrap v0.16 - Let</title>
    <updated>2010-01-20T08:08:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_16-let" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;We could have lived our lives without adding &lt;code&gt;cond&lt;/code&gt;. It is a convenience form and there are a few others that are handy to have around: &lt;code&gt;let&lt;/code&gt; is one of them.&lt;/p&gt;

&lt;p&gt;Sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (let ((x (+ 1 1))
        (y (- 5 2)))
    (+ x y))
5&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;let&lt;/code&gt; form is also &amp;ldquo;library syntax&amp;rdquo; and so the implementation is another abstract syntax tree manipulation. This time it is a manipulation that converts the &lt;code&gt;let&lt;/code&gt; form to a procedure application with a &lt;code&gt;lambda&lt;/code&gt; form.&lt;/p&gt;

&lt;p&gt;At this point you may be thinking these abstract syntax tree manipulations could become tedious to implement. Macros exist for this reason and so that the programmer can invent his own new library syntax. We only need a few bits of library syntax for a bootstrap interpreter and it is easier to implement directly in C then to implement macros in C and then the library syntax in Scheme.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.16&quot;&gt;v0.16 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_15-cond&quot;&gt;Cond&lt;/a&gt;&lt;br&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_17-and-and-or&quot;&gt;And and Or&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_15-cond</id>
    <title>Scheme from Scratch - Bootstrap v0.15 - Cond</title>
    <updated>2010-01-19T08:55:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_15-cond" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The only remaining part of the first meta-circular interpreter of SICP still to port to our interpreter is the &lt;code&gt;cond&lt;/code&gt; form.&lt;/p&gt;

&lt;p&gt;Sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (cond (#f          1)
        ((eq? 'a 'a) 2)
        (else        3))
2&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;cond&lt;/code&gt; form is &amp;ldquo;library syntax&amp;rdquo; and so the implementation of &lt;code&gt;cond&lt;/code&gt; is an abstract syntax tree manipulation to convert the form to a series of nested &lt;code&gt;if&lt;/code&gt; forms. The conversion algorithm is spelled out for us in SICP.&lt;/p&gt;

&lt;p&gt;SICP notes and discusses how inefficient the implementation is. Every time a single &lt;code&gt;cond&lt;/code&gt; form is evaluated it is (re)converted to nested &lt;code&gt;if&lt;/code&gt; forms. This is inefficient and the second meta-circular interpreter addresses this issue; however, for the purpose of a bootstrap intereter, simple is more important than efficient.&lt;/p&gt;

&lt;p&gt;We&amp;rsquo;ve reached an important milestone along our path. The port from SICP is complete. Any extra functionality we need for our interpreter to be a useful bootstrap interpreter we will need to figure out how to implement on our own.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.15&quot;&gt;v0.15 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_14-begin&quot;&gt;Begin&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_16-let&quot;&gt;Let&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-bootstrap-v0_14-begin</id>
    <title>Scheme from Scratch - Bootstrap v0.14 - Begin</title>
    <updated>2010-01-18T10:13:00</updated>
    <link href="/articles/scheme-from-scratch-bootstrap-v0_14-begin" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The last few days have been some meaty implementation steps. Today is quite light with the implementation of the &lt;code&gt;begin&lt;/code&gt; form.&lt;/p&gt;

&lt;p&gt;Sample REPL session:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
&gt; (begin 1 2 3)
3&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;begin&lt;/code&gt; form doesn&amp;rsquo;t introduce a new lexical scope like a &lt;code&gt;lambda&lt;/code&gt; does but &lt;code&gt;begin&lt;/code&gt; does have a tail position that must be handled correctly.&lt;/p&gt;

&lt;p&gt;As noted by readers in the comments below, a compound procedure has an implicit &lt;code&gt;begin&lt;/code&gt; form for the body of the procedure. The procedure application portion of &lt;code&gt;eval&lt;/code&gt; can be refactored to use this fact. I&amp;rsquo;ve made this change to the code. Perhaps introducing &lt;code&gt;begin&lt;/code&gt; before &lt;code&gt;lambda&lt;/code&gt; would have been a better pedagogical progression but I was getting impatient to get to &lt;code&gt;lambda&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://github.com/petermichaux/bootstrap-scheme/tree/v0.14&quot;&gt;v0.14 branch&lt;/a&gt; on github for this version.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_13-lambda-the-ultimate&quot;&gt;Lambda the Ultimate&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_15-cond&quot;&gt;Cond&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>


</feed>
