Solarian Programmer

My programming ramblings

Implementing Scheme in C++ - Special forms

Posted on November 21, 2011 by Paul

The code for this article is on GitHub: https://github.com/sol-prog/schm.

In my last post I’ve started to implement a Scheme interpreter in C++. This article continues with the implementation of six Scheme special forms: quote, if, set!, define, lambda and begin.

I’m not, currently, interested in the interpreter’s performance, but rather in the clarity of the implementation. The first version of the interpreter, the one presented in my last post, was not particularly well structured, this article presents a completely restructured code and a working version of Scheme. I’m sure there are bugs in my implementation and probably memory leaks.

The first version of the interpreter was able to do simple arithmetic calculations and return the results of evaluating an s-expression as a C++ string. Because now we aim to be able to create Scheme variables and procedures, the eval function was changed accordingly. The eval function will now return a Cell object that can store a variable value or a Scheme procedure:

►  Continue reading


Implementing Scheme in C++ - Introduction

Posted on November 14, 2011 by Paul

The code for this article is on GitHub: https://github.com/sol-prog/schm.

Update: 2011/11/21: The code presented in this article was completely restructured (one instruction per line, classes implementation separated from the definition, more comments etc …) in the second article from this series.

Implementing a high order programming language in a low level language, like C++ (Assembly is too low level for my background and C … well there are already a few Scheme implementations in C), has always been a fascinating subject for me. Writing a program that interprets other programs is a great and fun experience for anyone, it is almost like a rite or passage for a programmer.

My purpose in starting this series of articles is to better understand some of the fundamentals of the Scheme programming language and how an interpreter works. A secondary purpose will be to test my Scheme implementation on some of the examples and exercises presented in SICP (I will probably skip the Picture language presented in Chapter 2). This will allow me to redo some of the exercises from the book on my own Scheme implementation and in the same time on a mature implementation like Gambit Scheme, for comparison purposes.

►  Continue reading


C++11 lambda tutorial

Posted on November 1, 2011 by Paul

This article was updated in 28 August 2014

Don’t forget to check my short introduction to generic lambdas for C++14.

In my previous tutorials I’ve presented some of the newest C++11 additions to the language: regular expressions and raw strings. Another useful language addition is lambda, a lambda expression allows you to define and use anonymous functions inline (functions with a body but without a name). You can use a lambda expression instead of a function object, avoiding the need to create a separate class and a function definition. This is useful for example when you need a one-line expression to interact with many of the STL algorithms.

Lambda expressions are currently available in all major C++ compilers GCC, Visual Studio and Clang.

Let’s create a (useless) simple example of lambda expression that will return a number:

1 []() -> int { return 4; }();

►  Continue reading


C++11 regex tutorial part 2

Posted on October 20, 2011 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/regex_tutorial.

In the first part of this tutorial we have used regex_match to verify the user input. The regex_match algorithm in C++11 will return true only if the target string match exactly the regular expression, e.g. [[:digit:]]+ will match exactly "123", "456" and not "123e-05" or "456.22".

If you need a partial match of a given string you could use regex_search which will return true if it finds a partial match in the target string (a match on a substring of the original string). One can also retrieve the result of regex_search in an smatch object.

►  Continue reading


C++11 raw strings literals tutorial

Posted on October 16, 2011 by Paul

Now, that I have a working system that can compile both regular expressions and raw strings literals, it is time to show you how you can further simplify the examples from the regex tutorial.

Basically a raw string literal is a string in which the escape characters (like \n \t or \" ) of C++ are not processed. A raw string literal starts with R"( and ends in )", let's see an in an example the difference between a normal string and a raw string in C++:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string normal_str="First line.\nSecond line.\nEnd of message.\n";
 9     string raw_str=R"(First line.\nSecond line.\nEnd of message.\n)";
10     cout<<normal_str<<endl;
11     cout<<raw_str<<endl;
12     return(0);
13 }

►  Continue reading


C++11 regex tutorial

Posted on October 12, 2011 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/regex_tutorial.

Now that C++11 is a published standard and most of the mainstream compilers already have some of the C++11 standard implemented it is time to learn or relearn what has practically became a new language. As I explore myself some of the newest features of the standard, I plan to blog about my progress and hopefully help other people to start learning C++11.

I will start a series of short tutorials in which I will present one feature of the language at a time illustrated with working, complete, examples. Each post will also have a short paragraph about on which compilers and operating system I've tested my code. I will test each example on Linux (g++-4.6.1 and clang), Mac OSX (g++-4.6.1 and clang) and finally Windows (Visual C++ 2010 and g++-4.6.1). If you find that one of the examples presented here works with a different combination of compiler/OS, please drop me a note and I will include this in my post.

►  Continue reading


Scientific graphics in C# - part 2

Posted on October 9, 2011 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/GraphDemo2.

In the first part of this tutorial I've shown you how to plot some data on a Windows Form using C#. It is time now to improve the graphical aspect of our DemoGraph, we will start by properly formatting the numbers from the x axis, we could do this by modifying the Plot.cs class:

1 ...
2 chart.Series.Add("Series0");
3 chart.Series[0].ChartType = SeriesChartType.Line;
4 chart.ChartAreas[0].AxisX.LabelStyle.Format = "{F2}";
5 ...

►  Continue reading


Scientific graphics in C#

Posted on October 6, 2011 by Paul

The code for this tutorial is on GitHub: https://github.com/sol-prog/GraphDemo1

Imagine for a moment that you are stranded on a deserted island only with your laptop, your task is to write a compiled application that will let the user load a file with numbers and plot these on a graphic. For the sake of argument let's suppose you have a Windows based computer and, because Windows does not include any compiler by default, we'll also suppose you have Visual Studio 2010 installed. What would you do ?

Last week I was in a similar situation when my internet connection was down for two days and I was pressed by a client to add some 2D graphical capability to a C# application, the deadline for this project was quickly arriving and no third party library was allowed, only what is included by default with VS 2010. My solution was to use the Chart control for plotting the data on a Windows Form. The by-product of this project was this post in which I will show you how to add to a C# application a plotting area that will allow you to plot and save the graphics as high quality images.

►  Continue reading