This is part of a series of posts about writing a simple interpreter for a small Lisp-like language. Please see here for an overview of the series.
Last time we made all symbols readable. Now we’re going to start
working towards a symbol table, and our first step is to define equal
.
You can see the code as of this post here, and a comparison against last time here.
There’s nothing especially interesting about equal
, so today I’ll mostly refer
you to equal.c
in the repository. For integers, characters, and
symbols1, we can defer to C’s equality operator (==
). For strings, we
first compare their lengths (two strings of different length can’t be equal) and
then defer to C’s memcmp
to compare their contents.
Lists are the most interesting case, because their contents are Sky values. If
the two lists are nil
, they’re equal. If one is nil
and the other isn’t,
they’re not equal. If the first
items of the two lists are not equal
, then
the lists aren’t equal. Otherwise we repeat the process on the rest
of both
lists until we reach a conclusion.
There are a number of things that would make for a more interesting equal
,
like supporting an arbitrary number of arguments, comparing across multiple
sequence types, comparisons involving floating point numbers, etc., but none of
those are relevant for us yet, so we’re getting away with a very simple
implementation.
Next time
Next time we’ll implement a hash function.