/*
lval.c
-lval constructor
---lval_num
---lval_err
---lval_sym
---lval_sexpr
-lval reader
---lval_read_num
---lval_read
-lval appender and destrucctor
---lval_add
---lval_del
-lval printer
---lval_expr_print
---lval_print
---lval_println
-lval processor
---lval_eval_sexpr
---lval_eval
---lval_pop
---lval_take
---builtin_op
*/
enum { LVAL_ERR, LVAL_NUM, LVAL_SYM, LVAL_SEXPR };
typedef struct lval {
int type;
long num;
/* Error and Symbol types have some string data */
char* err;
char* sym;
/* Count and Pointer to a list of "lval*" */
int count;
struct lval** cell;
} lval;
//errno 是记录系统的最后一次错误代码。代码是一个int型的值,在errno.h中定义。查看错误代码errno是调试程序的一个重要方法。
//C 库宏 ERANGE 表示一个范围错误,它在输入参数超出数学函数定义的范围时发生,errno 被设置为 ERANGE。
/* */
lval* lval_num(long x){
lval* v=malloc(sizeof(lval));
v->type=LVAL_NUM;
v->num=x;
return v;
}
lval* lval_err(char *e){
lval* v=malloc(sizeof(lval));
v->type=LVAL_ERR;
v->err=malloc(strlen(e)+1);
strcpy(v.err,e);
return v;
}
lval* lval_sym(char *s){
lval* v=malloc(sizeof(lval));
v->type=LVAL_SYM;
v->sym=malloc(strlen(s)+1);
strcpy(v.sym,s);
return v;
}
//new s-expression
lval* lval_sexpr(){
lval* v=malloc(sizeof(lval));
v->type=LVAL_SEXPR;
v->count=0;
v->cell=NULL;
return v;
}
void lval_del(lval *v){
switch(v->type){
case LVAL_NUM:break;
case LVAL_ERR:free(v->err);break;
case LVAL_SYM:free(v->sym);break;
case LVAL_SEXPR:
//free cell_array's elem memory
for(int i=0;i<v->count;i++){
lval_del(v->cell[i]);
}
//free cell_array's pointer memory
free(v->cell);
break;
}
//free all memory
free(v);
}
lval* lval_read_num(mpc_ast_t* t) {
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ?lval_num(x) : lval_err("invalid number");
}
lval* lval_read(mpc_ast_t* t){
if(strstr(t->tag,"number")){return lval_read_num(t->contents);}
if(strstr(t->tag,"symbol")){return lval_sym(t->contents);}
lval* x=NULL;
if(strstr(t->tag,">")==0){x=lval_sexpr();}
if(strstr(t->tag,"sexpr")){x=lval_sexpr();}
for(int i=0;i<t->children_num;i++){
if (strcmp(t->children[i]->contents, "(") == 0) { continue; }
if (strcmp(t->children[i]->contents, ")") == 0) { continue; }
if (strcmp(t->children[i]->contents, "}") == 0) { continue; }
if (strcmp(t->children[i]->contents, "{") == 0) { continue; }
if (strcmp(t->children[i]->tag, "regex") == 0) { continue; }
x = lval_add(x, lval_read(t->children[i]));
}
return x;
}
lval* lval_add(lval *v,lval *x){
v->count++;
v->cell=realloc(v->cell,v->count*sizeof(lval*));
v->cell[v->count-1]=x;
return x;
}
void lval_expr_print(lval* v, char open, char close) {
putchar(open);
for (int i = 0; i < v->count; i++) {
/* Print Value contained within */
lval_print(v->cell[i]);
/* Don't print trailing space if last element */
if (i != (v->count-1)) {
putchar(' ');
}
}
putchar(close);
}
void lval_print(lval* v) {
switch (v->type) {
case LVAL_NUM: printf("%li", v->num); break;
case LVAL_ERR: printf("Error: %s", v->err); break;
case LVAL_SYM: printf("%s", v->sym); break;
case LVAL_SEXPR: lval_expr_print(v, '(', ')'); break;
}
}
void lval_println(lval* v) { lval_print(v); putchar('\n'); }
lval* lval_eval_sexpr(lval *v){
//evaluate one by one
for(int i=0;i<v->count;i++){
v->cell[i]=lval_eval(v->cell[i]);
}
//error processor
for(int i=0;i<v->count;i++){
return lval_take(v,i);
}
if(v->count==0)
return v;
if(v->count==1)
return lval_take(v,0);
lval* f = lval_pop(v, 0);
if (f->type != LVAL_SYM) {
lval_del(f); lval_del(v);
return lval_err("S-expression Does not start with symbol!");
}
/* Call builtin with operator */
lval* result = builtin_op(v, f->sym);
lval_del(f);
return result;
}
lval* lval_eval(lval *v){
if(v->type==LVAL_SEXPR)
return lval_eval_sexpr(v);
return v;
}
lval* lval_pop(lval* v,int i){
lval* x=v->cell[i];
memmove(&v->cell[i],&v->cell[i+1],sizeof(lval*)*(v->count-i-1));
v->count--;
v->cell=realloc(v->cell,sizeof(lval*)*v->count);
return x;
}
lval* lval_take(lval *v,int i){
lval *x=lval_pop(v,i);
lval_del(v);
return x;
}
lval* builtin_op(lval* a, char* op) {
/* Ensure all arguments are numbers */
for (int i = 0; i < a->count; i++) {
if (a->cell[i]->type != LVAL_NUM) {
lval_del(a);
return lval_err("Cannot operate on non-number!");
}
}
/* Pop the first element */
lval* x = lval_pop(a, 0);
/* If no arguments and sub then perform unary negation */
if ((strcmp(op, "-") == 0) && a->count == 0) {
x->num = -x->num;
}
/* While there are still elements remaining */
while (a->count > 0) {
/* Pop the next element */
lval* y = lval_pop(a, 0);
if (strcmp(op, "+") == 0) { x->num += y->num; }
if (strcmp(op, "-") == 0) { x->num -= y->num; }
if (strcmp(op, "*") == 0) { x->num *= y->num; }
if (strcmp(op, "/") == 0) {
if (y->num == 0) {
lval_del(x); lval_del(y);
x = lval_err("Division By Zero!"); break;
}
x->num /= y->num;
}
lval_del(y);
}
lval_del(a); return x;
}
Lisp2 expression
最新推荐文章于 2022-07-06 23:52:52 发布