forked from orangeduck/BuildYourOwnLisp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapter16_bonus_projects.html
132 lines (67 loc) · 13.1 KB
/
chapter16_bonus_projects.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<h1>Bonus Projects <small>• Chapter 16</small></h1>
<h2 id='only_the_beginning'>Only the Beginning</h2> <hr/>
<p>Although we've done a lot with our Lisp, it is still some way off from a fully complete, production-strength programming language. If you tried to use it for any sufficiently large project there are a number of issues you would eventually run into and improvements you'd have to make. Solving these problems would be what would bring it more into the scope of a fully fledged programming language.</p>
<p>Here are some of these issues you would likely encounter, potential solutions to these problems, and some other fun ideas for other improvements. Some may take a few hundred lines of code, others a few thousand. The choice of what to tackle is up to you. If you've become fond of your language you may enjoy doing some of these projects.</p>
<h2 id='native_types'>Native Types</h2> <hr/>
<p>Currently our language only wraps the native C <code>long</code> and <code>char*</code> types. This is pretty limiting if you want to do any kind of useful computation. Our operations on these data types are also pretty limited. Ideally our language should wrap all of the native C types and allow for methods of manipulating them. One of the most important additions would be the ability to manipulate decimal numbers. For this you could wrap the <code>double</code> type and relevant operations. With more than one number type we need to make sure the arithmetic operators such as <code>+</code> and <code>-</code> work on them all, and them in combination.</p>
<p>Adding support for native types should be interesting for people wishing to do computation with decimal and floating-point numbers in their language.</p>
<h2 id='user_defined_types'>User Defined Types</h2> <hr/>
<p>As well as adding support for native types it would be good to give users the ability to add their own new types, just like how we use structs in C. The syntax or method you use to do this would be up to you. This is a really essential part making our language usable for any reasonably sized project.</p>
<p>This task may be interesting to anyone who has a specific idea of how they would like to develop the language, and what they want a final design to look like.</p>
<div class='pull-right alert alert-warning' style="margin: 15px; text-align: center;">
<img src="/static/img/list.png" alt="list" class="img-responsive" width="228px" height="410px"/>
<p><small>Important List • Play! BE HAPPY and go home.</small></p>
</div>
<h2 id='list_literal'>List Literal</h2> <hr/>
<p>Some lisps use square brackets <code>[]</code> to give a literal notation for lists of evaluated values lists. This syntactic sugar for writing something like <code>list 100 (+ 10 20) 300</code>. Instead it lets you write <code>[100 (+ 10 20) 300]</code>. In some situations this is clearly nicer, but it does use up the <code>[]</code> characters which could possibly be used for more interesting purposes.</p>
<p>This should be a simple addition for people looking to try out adding extra syntax.</p>
<h2 id='operating_system_interaction'>Operating System Interaction</h2> <hr/>
<p>One essential part of bootstrapping a language is to give it proper abilities for opening, reading, and writing files. This means wrapping all the C functions such as <code>fread</code>, <code>fwrite</code>, <code>fgetc</code>, etc in Lisp equivalents. This is a fairly straight forward task, but does require writing quite a large number of wrapper functions. This is why we've not done it for our language so far.</p>
<p>On a similar note it would be great to give our language access to whatever operating systems calls are appropriate. We should give it the ability to change directory, list files in a directory and that sort of thing. This is an easy task but again requires a lot of wrapping of C functions. It is essential for any real practical use of this language as a scripting language.</p>
<p>People who wish to make use of their language for doing simple scripting tasks and string manipulation may be interested in implementing this project.</p>
<h2 id='macros'>Macros</h2> <hr/>
<p>Many other Lisps allow you to write things like <code>(def x 100)</code> to define the value <code>100</code> to <code>x</code>. In our lisp this wouldn't work because it would attempt to evaluate the <code>x</code> to whatever value was stored as <code>x</code> in the environment. In other Lisps these functions are called <em>macros</em>, and when encountered they stop the evaluation of their arguments, and manipulate them un-evaluated. They let you write things that look like normal function calls, but actually do complex and interesting things.</p>
<p>These are fun thing to have in a language. They make it so you can add a little bit of <em>magic</em> to some of the workings. In many cases this can make syntax nicer or allow a user to not repeat themselves.</p>
<p>I like how our language handles things like <code>def</code> and <code>if</code> without resorting to macros, but if you dislike how it works currently, and want it to be more similar to conventional Lisps, this might be something you are interested in implementing.</p>
<h2 id='variable_hashtable'>Variable Hashtable</h2> <hr/>
<p>At the moment when we lookup variable names in our language we just do a linear search over all of the variables in the environment. This gets more and more inefficient the more variables we have defined.</p>
<p>A more efficient way to do this is to implement a <em>Hash Table</em>. This technique converts the variable name to an integer and uses this to index into an array of a known size to find the value associated with this symbol. This is a really important data structure in programming and will crop up everywhere because of its fantastic performance under heavy loads.</p>
<p>Anyone who is interested in learning more about data structures and algorithms would be smart to take a shot at implementing this data structure or one of its variations.</p>
<h2 id='pool_allocation'>Pool Allocation</h2> <hr/>
<p>Our Lisp is very simple, it is not fast. Its performance is relative to some scripting languages such as Python and Ruby. Most of the performance overhead in our program comes from the fact that doing almost anything requires us to construct and destruct <code>lval</code>. We therefore call <code>malloc</code> very often. This is a slow function as it requires the operating system to do some management for us. When doing calculations there is lots of copying, allocation and deallocation of <code>lval</code> types.</p>
<p>If we wish to reduce this overhead we need to lower the number of calls to <code>malloc</code>. One method of doing this is to call <code>malloc</code> once at the beginning of the program, allocating a large pool of memory. We should then replace all our <code>malloc</code> calls with calls to some function that slices and dices up this memory for use in the program. This means that we are emulating some of the behaviour of the operating system, but in a faster local way. This idea is called <em>memory pool allocation</em> and is a common technique used in game development, and other performance sensitive applications.</p>
<p>This can be tricky to implement correctly, but conceptually does not need to be complex. If you want a quick method for getting large gains in performance, looking into this might interest you.</p>
<h2 id='garbage_collection'>Garbage Collection</h2> <hr/>
<p>Almost all other implementations of Lisps assign variables differently to ours. They do not store a copy of a value in the environment, but actually a pointer, or reference, to it directly. Because pointers are used, rather than copies, just like in C, there is much less overhead required when using large data structures.</p>
<div class='pull-left alert alert-warning' style="margin: 15px; text-align: center;">
<img src="/static/img/garbage.png" alt="garbage" class="img-responsive" width="249px" height="289px"/>
<p><small>Garbage Collection • Pick up that can.</small></p>
</div>
<p>If we store pointers to values, rather than copies, we need to ensure that the data pointed to is not deleted before some other value tries to make use of it. We want to delete it when there are no longer any references to it. One method to do this, called <em>Mark and Sweep</em>, is to monitor those values that are in the environment, as well as every value that has been allocated. When a variable is put into the environment it, and everything it references, is <em>marked</em>. Then, when we wish to free memory, we can then iterate over every allocation, and delete any that are not marked.</p>
<p>This is called <em>Garbage Collection</em> and is an integral part to many programming languages. As with pool allocation, implementing a <em>Garbage Collector</em> does not need to be complicated, but it does need to be done carefully, in particularly if you wish to make it efficient. Implementing this would be essential to making this language practical for working with large amounts of data. A particularly good tutorial on implementing a garbage collector in C can be found <a href="http://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/">here</a>.</p>
<p>This should interest anyone who is concerned with the language's performance and wishes to change the semantics of how variables are stored and modified in the language.</p>
<h2 id='tail_call_optimisation'>Tail Call Optimisation</h2> <hr/>
<p>Our programming language uses <em>recursion</em> to do its looping. This is conceptually a really clever way to do it, but practically it is quite poor. Recursive functions call themselves to collect all of the partial results of a computation, and only then combine all the results together. This is a wasteful way of doing computation when partial results can be accumulated as some total over a loop. This is particularly problematic for loops that are intended to run for many, or infinite, iterations.</p>
<p>Some recursive functions can be automatically converted to corresponding <code>while</code> loops, which accumulate totals step by step, rather than altogether. This automatic conversion is called <em>tail call optimisation</em> and is an essential optimisation for programs that do a lot of looping using recursion.</p>
<p>People who are interested in compiler optimisations and the correspondences between different forms of computation might find this project interesting.</p>
<h2 id='lexical_scoping'>Lexical Scoping</h2> <hr/>
<p>When our language tries to lookup a variable that has been undefined it throws an error. It would be better if it could tell us which variables are undefined before evaluating the program. This would let us avoid typos and other annoying bugs. Finding these issues before the program is run is called <em>lexical scoping</em>, and uses the rules for variable definition to try and infer which variables are defined and which aren't at each point in the program, without doing any evaluation.</p>
<p>This could be a difficult task to get exactly right, but should be interesting to anyone who wants to make their programming language safer to use, and less bug-prone.</p>
<div class='pull-right alert alert-warning' style="margin: 15px; text-align: center;">
<img src="/static/img/static.png" alt="static" class="img-responsive" width="265px" height="439px"/>
<p><small>Static Electricity • A hair-raising alternative.</small></p>
</div>
<h2 id='static_typing'>Static Typing</h2> <hr/>
<p>Every value in our program has an associated type with it. This we know before any evaluation has taken place. Our builtin functions also only take certain types as input. We should be able to use this information to infer the types of new user defined functions and values. We can also use this information to check that functions are being called with the correct types before we run the program. This will reduce any errors stemming from calling functions with incorrect types before evaluation. This checking is called <em>static typing</em>.</p>
<p>Type systems are a really interesting and fundamental part of computer science. They are currently the best method we know of detecting errors before running a program. Anyone interesting in programming language safety and type systems should find this project really interesting.</p>
<h2 id='conclusion'>Conclusion</h2> <hr/>
<p>Many thanks for reading this book. I hope you've found something of interest in its pages. If you did enjoy it please tell your friends about it. If you are going to continue developing your language then best of luck and I hope you learn many more things about C, programming languages, and computer science.</p>
<p>Most of all I hope you've had fun building your own Lisp. Until next time!</p>
<h2>Navigation</h2>
<table class="table" style='table-layout: fixed;'>
<tr>
<td class="text-left"><a href="chapter15_standard_library"><h4>‹ Standard Library</h4></a></td>
<td class="text-center"><a href="contents"><h4>• Contents •</h4></a></td>
<td class="text-right"><a href="credits"><h4>Credits ›</h4></a></td>
</tr>
</table>