Skip to content

Commit

Permalink
Merge pull request #10 from willcipriano/feature/for_loops
Browse files Browse the repository at this point in the history
For Loops
  • Loading branch information
willcipriano authored Jun 29, 2021
2 parents 8db594d + 3bc84bb commit cce7cf1
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 181 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD 99)
set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)

add_executable(Connery main.c mpc.c util.c util.h hashtable.c hashtable.h cval.h cval.c trace.c trace.h)
add_executable(Connery main.c mpc.c util.c util.h hashtable.c hashtable.h cval.h cval.c trace.c trace.h strings.c strings.h)
include_directories(${CURL_INCLUDE_DIR})
target_link_libraries(Connery PRIVATE /usr/lib/x86_64-linux-gnu/libedit.so ${CURL_LIBRARIES})

Expand Down
37 changes: 21 additions & 16 deletions src/cval.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define CASSERT(args, cond, fmt, ...) \
if (!(cond)) {\
cval* err = cval_error(fmt, ##__VA_ARGS__); \
cval* err = cval_fault(fmt, ##__VA_ARGS__); \
cval_delete(args); \
return err;}

Expand All @@ -36,7 +36,7 @@ char* ctype_name(int t) {
switch(t) {
case CVAL_FUNCTION: return "Function";
case CVAL_NUMBER: return "Number";
case CVAL_ERROR: return "Error";
case CVAL_FAULT: return "Fault";
case CVAL_SYMBOL: return "Symbol";
case CVAL_S_EXPRESSION: return "S-Expression";
case CVAL_Q_EXPRESSION: return "Q-Expression";
Expand Down Expand Up @@ -85,9 +85,9 @@ cval* cval_string (char* s) {
return v;
}

cval* cval_error(char* fmt, ...) {
cval* cval_fault(char* fmt, ...) {
cval* value = malloc(sizeof(cval));
value->type = CVAL_ERROR;
value->type = CVAL_FAULT;
va_list va;
va_start(va, fmt);
value->err = malloc(512);
Expand Down Expand Up @@ -152,7 +152,7 @@ void cval_delete(cval* value) {
}
break;

case CVAL_ERROR:
case CVAL_FAULT:
free(value->err);
break;

Expand Down Expand Up @@ -233,7 +233,7 @@ cval* cval_copy(cval* v) {
case CVAL_FLOAT:
x->fnum = v->fnum;

case CVAL_ERROR:
case CVAL_FAULT:
x->err = malloc(strlen(v->err) + 1);
strcpy(x->err, v->err);
break;
Expand Down Expand Up @@ -292,7 +292,7 @@ cval* cval_call(cenv* e, cval* f, cval* a) {

if(f->formals->count == 0) {
cval_delete(a);
return cval_error("Function pashed too many argumentsh. Got %i, Expected %s", given, total);
return cval_fault("Function pashed too many argumentsh. Got %i, Expected %s", given, total);
}

cval* sym = cval_pop(f->formals, 0);
Expand All @@ -301,7 +301,7 @@ cval* cval_call(cenv* e, cval* f, cval* a) {

if (f->formals->count != 1) {
cval_delete(a);
return cval_error("Function format invalid. shymbol '&' not followed by shingle shymbol.");
return cval_fault("Function format invalid. shymbol '&' not followed by shingle shymbol.");
}

cval* nsym = cval_pop(f->formals, 0);
Expand All @@ -323,7 +323,7 @@ cval* cval_call(cenv* e, cval* f, cval* a) {
if (f->formals->count > 0 && strcmp(f->formals->cell[0]->sym, "&") == 0) {

if (f->formals->count > 0 && strcmp(f->formals->cell[0]->sym, "&") == 0) {
return cval_error("Function format invalid. shymbol '&' not followed by shingle shymbol.");
return cval_fault("Function format invalid. shymbol '&' not followed by shingle shymbol.");
}

cval_delete(cval_pop(f->formals, 0));
Expand Down Expand Up @@ -352,7 +352,7 @@ cval* cval_evaluate_s_expression(cenv* env, cval* value) {
}

for (int i = 0; i < value->count; i++) {
if (value->cell[i]->type == CVAL_ERROR) {return cval_take(value, i);}
if (value->cell[i]->type == CVAL_FAULT) {return cval_take(value, i);}
}

if (value->count == 0) {
Expand All @@ -368,6 +368,9 @@ cval* cval_evaluate_s_expression(cenv* env, cval* value) {
if (f->type == CVAL_S_EXPRESSION) {
return cval_evaluate_s_expression(env, f);
}
else {
return cval_fault("I'm afraid thatsh not valid connery, lad.");
}
}

cval* result = cval_call(env, f, value);
Expand All @@ -384,7 +387,7 @@ cval* cenv_get(cenv* e, cval* k) {
if (e->par) {
return cenv_get(e->par, k);
} else {
return cval_error("Unbound shymbol '%s' not defined in shcope!", k->sym);
return cval_fault("Unbound shymbol '%s' not defined in shcope!", k->sym);
}
}

Expand All @@ -397,6 +400,7 @@ cval* cval_evaluate(cenv* env, cval* value) {

if (value->type == CVAL_S_EXPRESSION) {
return cval_evaluate_s_expression(env, value);

}
return value;
}
Expand Down Expand Up @@ -425,7 +429,8 @@ cval* builtin_var(cenv* e, cval* a, char* func) {
CASSERT_TYPE("def", syms, i, CVAL_SYMBOL)
}

CASSERT(a, (syms->count == a->count-1), "Function '%s' pashed too many arguments for symbols. Got %i, Expected %i", func, syms->count, a->count-1);
CASSERT(a, (syms->count == a->count - 1),
"Function '%s' pashed too many arguments for symbols. Got %i, Expected %i", func, syms->count, a->count - 1);

for (int i = 0; i < syms->count; i++) {
if (strcmp(func, "def") == 0) {
Expand Down Expand Up @@ -470,14 +475,14 @@ cval* cval_read_num(mpc_ast_t* t) {
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ?
cval_number(x) : cval_error("that'sh an invalid number");
cval_number(x) : cval_fault("that'sh an invalid number");
}

cval* cval_read_float(mpc_ast_t* t) {
errno = 0;
long double x = strtold(t->contents, NULL);
return errno != ERANGE ?
cval_float(x) : cval_error("that'sh a invalid float");
cval_float(x) : cval_fault("that'sh a invalid float");
}

cval* cval_read_string(mpc_ast_t* t) {
Expand Down Expand Up @@ -634,11 +639,11 @@ bool cval_print(cval* value) {
}
break;

case CVAL_ERROR:
case CVAL_FAULT:
#if SYSTEM_LANG==0
printf("shtirred: %s", value->err);
#else
printf("error: %s", value->err);
printf("fault: %s", value->err);
#endif
break;

Expand Down
4 changes: 2 additions & 2 deletions src/cval.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ typedef struct cenv cenv;
typedef cval *(*cbuiltin)(cenv *, cval *);

enum {
CVAL_NUMBER, CVAL_ERROR, CVAL_SYMBOL, CVAL_FUNCTION,
CVAL_NUMBER, CVAL_FAULT, CVAL_SYMBOL, CVAL_FUNCTION,
CVAL_S_EXPRESSION, CVAL_Q_EXPRESSION, CVAL_STRING, CVAL_FLOAT,
CVAL_BOOLEAN
};
Expand Down Expand Up @@ -51,7 +51,7 @@ cval *cval_float(long double x);

cval *cval_string(char *s);

cval *cval_error(char *fmt, ...);
cval *cval_fault(char *fmt, ...);

cval *cval_symbol(char *s);

Expand Down
Loading

0 comments on commit cce7cf1

Please sign in to comment.