Skip to content

Commit

Permalink
Merge pull request #2 from tabascoeye/master
Browse files Browse the repository at this point in the history
refactoring all structs to typedefs
  • Loading branch information
mschuetz committed May 22, 2014
2 parents d5a265b + d8acc22 commit bdf6eda
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 97 deletions.
80 changes: 40 additions & 40 deletions base.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "base.h"
#include "symbol_table.h"

struct object * car(struct object * o){
object * car(object * o){
if (!atom_p(o)) {
if (o!=nil)
return o->data->car;
Expand All @@ -15,7 +15,7 @@ struct object * car(struct object * o){
check(false, "argument not a list");
}

struct object * cdr(struct object * o){
object * cdr(object * o){
if (!atom_p(o)) {
if (o!=nil)
return o->data->cdr;
Expand All @@ -25,31 +25,31 @@ struct object * cdr(struct object * o){
check(false, "argument not a list");
}

struct object * atom_p(struct object * o){
object * atom_p(object * o){
if (o!=nil && o->atom_p)
return sym("t");
else
return nil;
}

struct object * eq(struct object * o1, struct object * o2){
object * eq(object * o1, object * o2){
if ((atom_p(o1) && atom_p(o2) && o1==o2) || (o1==o2 && o2==nil))
return sym("t");
else
return nil;
}

struct object * list(int len, ...) {
object * list(int len, ...) {
va_list ap;
struct object * start = nil;
struct object * cur = nil;
struct object * o = nil;
object * start = nil;
object * cur = nil;
object * o = nil;
int i;
start = cons(nil, nil);
cur = start;
va_start(ap, len);
for (i=0;i<len;i++){
o = va_arg(ap, struct object *);
o = va_arg(ap, object *);
cur->data->car = o;
if (i==len-1)
cur->data->cdr = nil;
Expand All @@ -61,25 +61,25 @@ struct object * list(int len, ...) {
return start;
}

struct object * cons(struct object * o1, struct object * o2){
struct object * o = (struct object *) malloc(sizeof(struct object));
object * cons(object * o1, object * o2){
object * o = (object *) malloc(sizeof(object));
o->atom_p = false;
o->function_p = false;
struct cons_cell * cc = (struct cons_cell *) malloc(sizeof(struct cons_cell));
cons_cell * cc = (cons_cell *) malloc(sizeof(cons_cell));
o->data = cc;
cc->car = o1;
cc->cdr = o2;
return o;
}

struct object * quote(struct object * o){
object * quote(object * o){
return o;
}

static void print_cons(struct object * o);
static void print_cons(object * o);
static void print_atom(atom a);

struct object * print(struct object * o){
object * print(object * o){
if (o==nil) {
printf("nil");
return nil;
Expand All @@ -93,7 +93,7 @@ struct object * print(struct object * o){
return nil;
}

static void print_cons(struct object * o){
static void print_cons(object * o){
/* printf("(");
print(car(o));
printf(" . ");
Expand All @@ -113,62 +113,62 @@ static void print_atom(atom a){
printf("%s", st_id_to_name(a));
}

struct object * null(struct object * o){
object * null(object * o){
return eq(o, nil);
}

struct object * and(struct object * x, struct object * y){
object * and(object * x, object * y){
if (!null(x) && !null(y))
return sym("t");
else
return nil;
}

struct object * not(struct object * o){
object * not(object * o){
if (o == nil)
return sym("t");
else
return nil;
}


struct object * sym(const char * name){
object * sym(const char * name){
return st_insert(name);
}

struct object * cadr(struct object * o){
object * cadr(object * o){
return car(cdr(o));
}

struct object * caddr(struct object * o){
object * caddr(object * o){
return car(cdr(cdr(o)));
}

static struct object * cadddr(struct object * o){
static object * cadddr(object * o){
return car(cdr(cdr(cdr(o))));
}

struct object * caar(struct object * o){
object * caar(object * o){
return car(car(o));
}

struct object * caddar(struct object * o){
object * caddar(object * o){
return car(cdr(cdr(car(o))));
}

struct object * cadar(struct object * o){
object * cadar(object * o){
return car(cdr(car(o)));
}

struct object * append(struct object * x, struct object * y){
object * append(object * x, object * y){
if (null(x))
return y;

return cons(car(x), append(cdr(x), y));
}


struct object * pair(struct object * x, struct object * y) {
object * pair(object * x, object * y) {
if (null(x) && null(y))
return nil;

Expand All @@ -179,7 +179,7 @@ struct object * pair(struct object * x, struct object * y) {
exit(1);
}

struct object * assoc(struct object * x, struct object * y) {
object * assoc(object * x, object * y) {
if (null(x)) {
fprintf(stderr, "nil is not a variablename\n");
exit(EXIT_FAILURE);
Expand All @@ -195,13 +195,13 @@ struct object * assoc(struct object * x, struct object * y) {
return assoc(x, cdr(y));
}

static struct object * evcon(struct object * c, struct object *a);
static struct object * evlis(struct object * m, struct object *a);
static object * evcon(object * c, object *a);
static object * evlis(object * m, object *a);

struct object * globals = nil;
struct object * globals_end = nil;
object * globals = nil;
object * globals_end = nil;

struct object * eval(struct object * e, struct object *a){
object * eval(object * e, object *a){
if (null(e))
return nil;
if (atom_p(e))
Expand Down Expand Up @@ -238,10 +238,10 @@ struct object * eval(struct object * e, struct object *a){
}

if (eq(car(e), sym("defun"))) {
struct object *name = cadr(e);
struct object *params = caddr(e);
struct object *body = cadddr(e);
struct object *pair = cons(list(2, name, list(3, sym("lambda"), params, body)), nil);
object *name = cadr(e);
object *params = caddr(e);
object *body = cadddr(e);
object *pair = cons(list(2, name, list(3, sym("lambda"), params, body)), nil);
globals_end->data->cdr = pair;
globals_end = pair;
return eval(name, a);
Expand All @@ -267,14 +267,14 @@ struct object * eval(struct object * e, struct object *a){
exit(1);
}

struct object * evcon(struct object * c, struct object *a){
object * evcon(object * c, object *a){
if (eval(caar(c), a))
return eval(cadar(c), a);

return evcon(cdr(c), a);
}

struct object * evlis(struct object * m, struct object *a){
object * evlis(object * m, object *a){
if (null(m))
return nil;

Expand Down
62 changes: 32 additions & 30 deletions base.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,47 @@ typedef int atom;

#define nil NULL

struct object {
typedef struct _cons_cell cons_cell;
typedef struct _object object;

struct _object {
bool atom_p;
bool function_p;
struct cons_cell * data;
cons_cell * data;
int symbol_index;
};

struct cons_cell {
struct object * car;
struct object * cdr;
struct _cons_cell {
object * car;
object * cdr;
};

extern struct object * globals;
extern struct object * globals_end;

struct object * car(struct object * o);
struct object * cdr(struct object * o);
struct object * quote(struct object * o);
struct object * atom_p(struct object * o);
struct object * eq(struct object * o1, struct object * o2);
struct object * cons(struct object * o1, struct object * o2);
struct object * print(struct object * o);
struct object * reader();
struct object * sym(const char * name);
struct object * list(int len, ...);
extern object * globals;
extern object * globals_end;

struct object * null(struct object * o);
struct object * and(struct object * x, struct object * y);
struct object * not(struct object * o);
object * car(object * o);
object * cdr(object * o);
object * quote(object * o);
object * atom_p(object * o);
object * eq(object * o1, object * o2);
object * cons(object * o1, object * o2);
object * print(object * o);
object * reader();
object * sym(const char * name);
object * list(int len, ...);

struct object * cadr(struct object * o);
struct object * caddr(struct object * o);
struct object * cadar(struct object * o);
struct object * caar(struct object * o);
struct object * caddar(struct object * o);
struct object * append(struct object * x, struct object * y);
struct object * pair(struct object * x, struct object * y);
struct object * assoc(struct object * x, struct object * y);
struct object * eval(struct object * e, struct object * a);
object * null(object * o);
object * and(object * x, object * y);
object * not(object * o);
object * cadr(object * o);
object * caddr(object * o);
object * cadar(object * o);
object * caar(object * o);
object * caddar(object * o);
object * append(object * x, object * y);
object * pair(object * x, object * y);
object * assoc(object * x, object * y);
object * eval(object * e, object * a);

#ifdef DEBUG
#define LOG(fmt, ...) fprintf(stderr, "%s: " fmt "\n", __PRETTY_FUNCTION__, ##__VA_ARGS__);
Expand Down
4 changes: 2 additions & 2 deletions hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdbool.h>
#include <stdint.h>

typedef struct _hashmap_entry {
typedef struct {
void * key;
void * value;
uint32_t hash;
Expand All @@ -13,7 +13,7 @@ typedef struct _hashmap_entry {
typedef uint32_t (*hashmap_hash_fn)(const void * key, const size_t size);
typedef bool (*hashmap_equals_fn)(const void * key1, const void * key2, const size_t size);

typedef struct _hashmap {
typedef struct {
hashmap_entry *entries;
double load_factor;
size_t size;
Expand Down
22 changes: 11 additions & 11 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ static void match(const char * s){
}
}

static struct object * atom_or_list();
static object * atom_or_list();

static struct object * list_body(){
static object * list_body(){
if (is_c())
return nil;
return cons(atom_or_list(), list_body());
}

static struct object * parse_list() {
static object * parse_list() {
match("(");
struct object * o;
object * o;
o = list_body();
match(")");
return o;
}

static struct object * parse_atom() {
static struct object * o;
static object * parse_atom() {
static object * o;
check_not_nil(_sym, "parse error");
if (strcmp(_sym, "nil")==0)
o = nil;
Expand All @@ -78,7 +78,7 @@ static struct object * parse_atom() {
return o;
}

static struct object * atom_or_list(){
static object * atom_or_list(){
if (is_sym())
return parse_atom();
else if (is_o())
Expand All @@ -89,19 +89,19 @@ static struct object * atom_or_list(){
}
}

static struct object * lisp_program(){
static object * lisp_program(){
return atom_or_list();
}

static struct object * parse(){
static object * parse(){
_sym = (char *)lex();
return lisp_program();
}

extern FILE * yyin;

struct object * parse_string(char *s){
struct object *o;
object * parse_string(char *s){
object *o;
const char * fn = "/tmp/tinylisp-123";
FILE * f = fopen(fn, "w");
fwrite(s, strlen(s), 1, f);
Expand Down
Loading

0 comments on commit bdf6eda

Please sign in to comment.