Skip to content
Permalink
Browse files

env now exists

  • Loading branch information...
jfo committed May 6, 2016
1 parent f5d50ab commit 38483fea3045683f5ddd0525f24bdb4d444cdca9
Showing with 51 additions and 40 deletions.
  1. +1 −1 makefile
  2. +22 −20 src/builtins.c
  3. +8 −7 src/builtins.h
  4. +3 −1 src/cell.h
  5. +6 −0 src/env.c
  6. +1 −1 src/env.h
  7. +5 −5 src/eval.c
  8. +2 −1 src/eval.h
  9. +2 −1 src/main.c
  10. +1 −3 test.sld
@@ -4,7 +4,7 @@ CFLAGS = -Wall -Werror
OBJDIR=obj
vpath %.c src

objects = $(addprefix $(OBJDIR)/, util.o cell.o eval.o builtins.o print.o read.o main.o)
objects = $(addprefix $(OBJDIR)/, util.o cell.o eval.o builtins.o print.o read.o env.o main.o)

sild: $(objects)
$(CC) $(CFLAGS) -o sild $(objects)
@@ -26,29 +26,30 @@ static void arity_check(char *caller_name, int args, C *c) {
}
}

C *quote(C *operand) {
C *quote(C *operand, Env *env) {
arity_check("quote", 1, operand);
return operand;
}

C *car(C *operand) {
C *car(C *operand, Env *env) {
arity_check("car", 1, operand);

operand = eval(operand);
if (operand->type != LIST) {
operand = eval(operand, env);
if (operand->type != LIST || operand->val.list->type == NIL) {
exit(1);
}

C* outcell = operand->val.list;
free_cell(operand->val.list->next);
free_one_cell(operand->val.list->next);
outcell->next = &nil;
free(operand);
return outcell;
}

C *cdr(C *operand) {
C *cdr(C *operand, Env *env) {
arity_check("cdr", 1, operand);

operand = eval(operand);
operand = eval(operand, env);
if (operand->type != LIST || operand->val.list->type == NIL) {
exit(1);
}
@@ -58,11 +59,12 @@ C *cdr(C *operand) {
return operand;
}

C *cons(C *operand) {
C *cons(C *operand, Env *env) {
arity_check("cons", 2, operand);

operand = eval(operand);
C *operand2 = eval(operand->next);
C *operand2 = eval(operand->next, env);
operand = eval(operand, env);

if (operand2->type != LIST) {
exit(1);
}
@@ -71,9 +73,9 @@ C *cons(C *operand) {
return operand2;
}

C *atom(C *operand) {
C *atom(C *operand, Env *env) {
arity_check("atom", 1, operand);
operand = eval(operand);
operand = eval(operand, env);

C *out;
if (operand->type == LIST && operand->val.list->type != NIL) {
@@ -85,10 +87,10 @@ C *atom(C *operand) {
return out;
}

C *eq(C *operand) {
C *eq(C *operand, Env *env) {
arity_check("eq", 2, operand);
operand = eval(operand);
C *operand2 = eval(operand->next);
operand = eval(operand, env);
C *operand2 = eval(operand->next, env);

C *out;
if (
@@ -120,12 +122,12 @@ C *eq(C *operand) {
}


C *cond(C *operand) {
C *cond(C *operand, Env *env) {
if (operand->type == NIL) {
fprintf(stderr, "\nArityError: cond expected at least 1 argument, got none.");
exit(1);
} else if (operand->next->type == NIL) {
return eval(operand);
return eval(operand, env);
}
// assigning 3 operands to their own vars.
C *op1 = operand;
@@ -135,21 +137,21 @@ C *cond(C *operand) {
// isolating the first two arguments
op1->next = &nil;
op2->next = &nil;
op1 = eval(op1);
op1 = eval(op1, env);
if (!(op1->type == LIST && op1->val.list->type == NIL)) {
// free the boolean expression statement
free_cell(op1);
// free anything else that was passed in
free_cell(op3);
// return the evalled second arg.
return eval(op2);
return eval(op2, env);
} else {
// won't need these anymore!
free_cell(op1);
free_cell(op2);

if (op3->type != NIL) {
return cond(op3);
return cond(op3, env);
} else {
// if the op3 type is NIL, you've reached the end of the cond form
// without encountering any true predicates, and should return the
@@ -3,13 +3,14 @@

#include "cell.h"
#include "print.h"
#include "env.h"

C *quote(C*);
C *car(C*);
C *cdr(C*);
C *cons(C*);
C *atom(C*);
C *eq(C*);
C *cond(C*);
C *quote(C*, Env*);
C *car(C*, Env*);
C *cdr(C*, Env*);
C *cons(C*, Env*);
C *atom(C*, Env*);
C *eq(C*, Env*);
C *cond(C*, Env*);

#endif
@@ -3,9 +3,11 @@

enum CellType { NIL, LABEL, LIST, BUILTIN };

struct Env;

struct funcval {
char *name;
struct C *(*addr)(struct C*);
struct C *(*addr)(struct C*, struct Env*);
};

typedef union V {
@@ -0,0 +1,6 @@
#include "env.h"
#include "cell.h"

C *get(Env* env, C *key) {
return empty_list();
}
@@ -5,7 +5,7 @@

typedef struct Env Env;

C *set(Env*,C *key, C *value);
C *set(Env*, C *key, C *value);
C *get(Env*, C *key);
C *delete(Env*, C *key);

@@ -9,12 +9,12 @@
/* eval/apply */
/* ---------- */

static C *apply(C* c) {
static C *apply(C* c, Env *env) {
switch (c->type) {
case BUILTIN:
return c->val.func.addr(c->next);
return c->val.func.addr(c->next, env);
case LIST:
return apply(eval(c));
return apply(eval(c, env), env);
case LABEL:
fprintf(stderr, "\nError: attempted to apply non-procedure %s\n", c->val.label);
exit(1);
@@ -24,11 +24,11 @@ static C *apply(C* c) {
}
}

C *eval(C* c) {
C *eval(C* c, Env *env) {
switch (c->type) {
case LIST:
{
C *out = apply(eval(c->val.list));
C *out = apply(eval(c->val.list, env), env);
out->next = c->next;
free(c);
return out;
@@ -2,7 +2,8 @@
#define EVAL_GUARD

#include "cell.h"
#include "env.h"

C *eval(C* c);
C *eval(C* c, Env *env);

#endif
@@ -14,8 +14,9 @@ void eval_file(const char *filename) {
}

C * c;
Env * env = NULL;
while((c = read(fp)) != &nil) {
c = eval(c);
c = eval(c, env);
print(c);
free_cell(c);
}
@@ -1,3 +1 @@
((cond (eq 1 2) quote
(atom (quote (3))) cdr
(atom (quote ())) car) (cdr (quote (5 6 7))))
(cons something somethingelse)

0 comments on commit 38483fe

Please sign in to comment.
You can’t perform that action at this time.