commit 6e55bf1456269d50a3ee669414efaf367a8b737d
parent ae5cf706da3138ad4c93b5d264e47d8165ab1dcb
Author: sin <sin@2f30.org>
Date:   Wed, 14 May 2014 14:50:06 +0100
Add the quote identifier as well
Diffstat:
3 files changed, 23 insertions(+), 15 deletions(-)
diff --git a/parser.c b/parser.c
@@ -18,7 +18,6 @@ enum objtype {
 	OString,
 	OEmptylist,
 	OPair,
-	OQuote,
 };
 
 struct object {
@@ -60,6 +59,9 @@ struct object {
 	} d;
 };
 
+/* builtin symbols */
+struct object *quotesym;
+
 static struct object *
 newobject(void)
 {
@@ -189,23 +191,12 @@ pair(FILE *in)
 	return cons(car, cdr);
 }
 
-static struct object *
-quote(void)
-{
-	struct object *o;
-
-	o = newobject();
-	o->type = OQuote;
-	return o;
-}
-
 struct object *
 sexpression(FILE *in)
 {
 	struct tok t;
 	char *l;
 
-	initenv();
 	t = gettok(in);
 	l = lexeme(&t);
 	switch (t.type) {
@@ -228,7 +219,7 @@ sexpression(FILE *in)
 	case TLparen:
 		return pair(in);
 	case TQuote:
-		return cons(quote(), cons(sexpression(in), emptylist()));
+		return cons(quotesym, cons(sexpression(in), emptylist()));
 	default:
 		return error("unhandled token");
 	}
@@ -243,7 +234,8 @@ quoted(struct object *o)
 
 	if (o->type == OPair) {
 		car = o->d.p.car;
-		return car->type == OQuote;
+		return car->type == OIdentifier &&
+			strcmp(car->d.i.s, "quote") == 0;
 	}
 	return 0;
 }
@@ -254,7 +246,7 @@ eval(struct object *o)
 	struct object *otmp;
 
 	switch (o->type) {
-	/* self-evaluating objects */
+		/* self-evaluating objects */
 	case OError:
 	case OEof:
 	case OBoolean:
@@ -327,3 +319,17 @@ print(struct object *o)
 		break;
 	}
 }
+
+int
+init(void)
+{
+	struct object *o;
+
+	initenv();
+	o = newobject();
+	o->type = OIdentifier;
+	o->d.i.s = "quote";
+	quotesym = o;
+	addsym("quote", quotesym);
+	return 0;
+}
diff --git a/parser.h b/parser.h
@@ -2,3 +2,4 @@
 struct object *sexpression(FILE *);
 struct object *eval(struct object *);
 void print(struct object *);
+int init(void);
diff --git a/repl.c b/repl.c
@@ -7,6 +7,7 @@ int
 main(void)
 {
 	puts("Welcome to iris - use ^C to quit");
+	init();
 	do {
 		printf("> ");
 		fflush(stdout);