add some scaffolding for functions as their own type

This commit is contained in:
NunoSempere 2023-05-02 10:33:08 -04:00
parent 14113fb636
commit 3e217cbdbe
2 changed files with 55 additions and 8 deletions

BIN
mumble

Binary file not shown.

View File

@ -12,23 +12,44 @@
} }
// Types // Types
typedef struct lispval {
int type; // Types: Forward declarations
double num; // I don't understand how this works
char* err; // and in particular why lispval is repeated twice after the typedef struct
char* sym; struct lispval;
int count; struct lispenv;
struct lispval** cell; // list of lisval* typedef struct lispval lispval;
} lispval; typedef struct lispenv lispenv;
typedef lispval*(*lispbuiltin)(lispenv*, lispval*);
// this defines the lispbuiltin type
// which seems to be a pointer to a function which takes in a lispenv*
// and a lispval* and returns a lispval*
// Types: Actual types
enum { enum {
LISPVAL_NUM, LISPVAL_NUM,
LISPVAL_ERR, LISPVAL_ERR,
LISPVAL_SYM, LISPVAL_SYM,
LISPVAL_FUNC,
LISPVAL_SEXPR, LISPVAL_SEXPR,
LISPVAL_QEXPR, LISPVAL_QEXPR,
}; };
typedef struct lispval {
int type;
double num;
char* err;
char* sym;
lispbuiltin func;
char* funcname;
int count;
struct lispval** cell; // list of lisval*
} lispval;
enum { enum {
LISPERR_DIV_ZERO, LISPERR_DIV_ZERO,
LISPERR_BAD_OP, LISPERR_BAD_OP,
@ -68,6 +89,17 @@ lispval* lispval_sym(char* symbol)
return v; return v;
} }
lispval* lispval_func(lispbuiltin func, char* funcname){
if(VERBOSE) printf("\nAllocated sym");
lispval* v = malloc(sizeof(lispval));
v->type = LISPVAL_FUNC;
v->count = 0;
v->sym = malloc(strlen(funcname) + 1);
strcpy(v->funcname, funcname);
v->func = func;
return v;
}
lispval* lispval_sexpr(void) lispval* lispval_sexpr(void)
{ {
if(VERBOSE) printf("\nAllocated sexpr"); if(VERBOSE) printf("\nAllocated sexpr");
@ -107,6 +139,15 @@ void delete_lispval(lispval* v)
free(v->sym); free(v->sym);
v->sym = NULL; v->sym = NULL;
break; break;
case LISPVAL_FUNC:
if(VERBOSE) printf("\nFreed func");
if (v->funcname != NULL)
free(v->funcname);
v->funcname = NULL;
// Don't do anything with v->func for now
// Though we could delete the pointer to the function later
// free(v->func);
break;
case LISPVAL_SEXPR: case LISPVAL_SEXPR:
case LISPVAL_QEXPR: case LISPVAL_QEXPR:
if(VERBOSE) printf("\nFreed s/qexpr"); if(VERBOSE) printf("\nFreed s/qexpr");
@ -247,6 +288,9 @@ void print_lispval_parenthesis(lispval* v)
case LISPVAL_SYM: case LISPVAL_SYM:
printf("%s ", v->sym); printf("%s ", v->sym);
break; break;
case LISPVAL_FUNC:
printf("<function name: %s pointer: %p>", v->funcname, v->func);
break;
case LISPVAL_SEXPR: case LISPVAL_SEXPR:
printf("( "); printf("( ");
for (int i = 0; i < v->count; i++) { for (int i = 0; i < v->count; i++) {
@ -302,6 +346,9 @@ lispval* clone_lispval(lispval* old)
case LISPVAL_SYM: case LISPVAL_SYM:
new = lispval_sym(old->sym); new = lispval_sym(old->sym);
break; break;
case LISPVAL_FUNC:
new = lispval_func(old->func, old->funcname);
break;
case LISPVAL_SEXPR: case LISPVAL_SEXPR:
new = lispval_sexpr(); new = lispval_sexpr();
break; break;