aboutsummaryrefslogtreecommitdiff
path: root/atom.c
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2014-05-14 22:27:34 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2014-05-14 22:27:34 +0300
commitd45a5b21de22438c004e1db96a8f154da09cdc0e (patch)
treec2a7bd2a4d08de03ef5e11932d9a1abf85110c80 /atom.c
downloadlispish-d45a5b21de22438c004e1db96a8f154da09cdc0e.tar.gz
lispish-d45a5b21de22438c004e1db96a8f154da09cdc0e.zip
Initial commit
- tokenizing, parsing and basic eval support - arithmetic (+, -, *, /) - quote - atom for checking if the arg is an atom (i.e. not a list) - eq for checking equality - > for checking order - if - some unit testing - simple repl - mem management needs improvement :-)
Diffstat (limited to 'atom.c')
-rw-r--r--atom.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/atom.c b/atom.c
new file mode 100644
index 0000000..73ecd7e
--- /dev/null
+++ b/atom.c
@@ -0,0 +1,100 @@
+#include "atom.h"
+#include "list.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct atom true_atom;
+struct atom false_atom;
+struct atom nil_atom;
+
+struct atom *atom_new(char type)
+{
+ struct atom *atom = calloc(1, sizeof(*atom));
+ atom->type = type;
+ return atom;
+}
+
+struct atom *atom_new_int(long l)
+{
+ struct atom *atom = atom_new(ATOM_INT);
+ atom->l = l;
+ return atom;
+}
+
+struct atom *atom_new_str(const char *str, int len)
+{
+ struct atom *atom = atom_new(ATOM_STR);
+ atom->str.str = strndup(str, len);
+ atom->str.len = len;
+ return atom;
+}
+
+struct atom *atom_new_sym(const char *sym, int len)
+{
+ struct atom *atom = atom_new_str(sym, len);
+ atom->type = ATOM_SYMBOL;
+ return atom;
+}
+
+struct atom *atom_new_list(struct list *list)
+{
+ struct atom *atom = atom_new(ATOM_LIST);
+ atom->list = list;
+ return atom;
+}
+
+#ifdef BUILD_TEST
+
+#include "test_util.h"
+
+TEST(atom_new)
+{
+ struct atom *atom = atom_new(ATOM_STR);
+ ASSERT_TRUE(atom != NULL);
+ ASSERT_EQ(ATOM_STR, atom->type);
+}
+
+TEST(atom_new_int)
+{
+ struct atom *atom = atom_new_int(42);
+ ASSERT_TRUE(atom != NULL);
+ ASSERT_EQ(42, atom->l);
+}
+
+TEST(atom_new_str)
+{
+ struct atom *atom = atom_new_str("foobar", 6);
+ ASSERT_TRUE(atom != NULL);
+ ASSERT_STREQ("foobar", atom->str.str);
+
+ atom = atom_new_str("foobar", 3);
+ ASSERT_TRUE(atom != NULL);
+ ASSERT_STREQ("foo", atom->str.str);
+
+ ASSERT_EQ(ATOM_STR, atom->type);
+}
+
+TEST(atom_new_sym)
+{
+ struct atom *atom = atom_new_sym("foobar", 6);
+ ASSERT_TRUE(atom != NULL);
+ ASSERT_STREQ("foobar", atom->str.str);
+
+ atom = atom_new_sym("foobar", 3);
+ ASSERT_TRUE(atom != NULL);
+ ASSERT_STREQ("foo", atom->str.str);
+
+ ASSERT_EQ(ATOM_SYMBOL, atom->type);
+}
+
+TEST(atom_new_list)
+{
+ struct list list;
+ struct atom *atom = atom_new_list(&list);
+ ASSERT_TRUE(atom != NULL);
+ ASSERT_EQ(&list, atom->list);
+ ASSERT_EQ(ATOM_LIST, atom->type);
+}
+
+#endif