Sign in to follow this  
RyanA

Data structure thoughts for implementing lisp in C

Recommended Posts

Hey all! I've been thinking about writing my own little programming language (an interpreter first then possibly a compiler). To keep things simple I am using the prefix syntax of the lisp-style languages so I don't have to worry too much about complex parsing techniques. The issue I'm having is deciding a quick/efficient way of handling the core data type. I'd like things to be dynamic. Here's what I'm thinking about doing:
struct obj {
        /* what type of value this object carries */
	int type;
	
	union {
                char *id; /* identifier value */
		int    i; /* integer value */
		float  f; /* floating-point value */
		char  *s; /* string value */
		
		/* cons cell value */
		struct {
			struct obj *car;
			struct obj *cdr;
		} c;
	} value;
};



The goal for the language to eventually be able to write something like:
(def square-value (n) (mult n n))
or..
(def greet (name) (print "Why, hello " name))
Which is why each object has to have the ability to be any possible type. Is there a better/faster/more efficient way to go about this in C? Any suggestions or comments would be appreciated! Thanks!

Share this post


Link to post
Share on other sites
As mentioned before, GC would be a good idea. During your mark-and-sweep algorithm, you will basically copy your cons nodes to another area in memory, and will therefore need to point to the new position of every object that has already been moved by the garbage collector. So, I would suggest adding a new type:

struct obj {
/* what type of value this object carries */
int type;

union {
char *id; /* identifier value */
int i; /* integer value */
float f; /* floating-point value */
char *s; /* string value */
struct obj *mov; /* moved by GC */

/* cons cell value */
struct {
struct obj *car;
struct obj *cdr;
} c;
} value;
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this