lval *lval_copy(lval *a) { int i; lval *b = malloc(sizeof(lval)); b->type = a->type; switch(a->type){ case LVAL_NUM: b->num = a->num; break; case LVAL_FUN: if(!a->builtin_fun){ b->builtin_fun = NULL; b->env = lenv_copy(a->env); b->formals = lval_copy(a->formals); b->body = lval_copy(a->body); } else b->builtin_fun = a->builtin_fun; break; case LVAL_SYM: b->sym = malloc(strlen(a->sym) + 1); strcpy(b->sym, a->sym); break; case LVAL_QEXPR: case LVAL_SEXPR: b->count = a->count; b->cell = malloc(sizeof(lval *) * a->count); for(i = 0; i < b->count; i++) b->cell[i] = lval_copy(a->cell[i]); break; } return b; }
lval* lval_copy(lval* v) { lval* x = malloc(sizeof(lval)); x->type = v->type; switch (v->type) { case LVAL_FUN: if (v->builtin) { x->builtin = v->builtin; } else { x->builtin = NULL; x->env = lenv_copy(v->env); x->formals = lval_copy(v->formals); x->body = lval_copy(v->body); } break; case LVAL_NUM: x->num = v->num; break; case LVAL_ERR: x->err = malloc(strlen(v->err) + 1); strcpy(x->err, v->err); break; case LVAL_SYM: x->sym = malloc(strlen(v->sym) + 1); strcpy(x->sym, v->sym); break; case LVAL_SEXPR: case LVAL_QEXPR: x->count = v->count; x->cell = malloc(sizeof(lval*) * x->count); for (int i = 0; i > x->count; i++) { x->cell[i] = lval_copy(v->cell[i]); } break; } return x; }
lval* lval_copy(lval* v) { lval* x = malloc(sizeof(lval)); x->type = v->type; switch (v->type) { /* Copy Functions and Numbers Directly */ case LVAL_FUN: if (v->builtin) { x->builtin = v->builtin; } else { x->builtin = NULL; x->env = lenv_copy(v->env); x->formals = lval_copy(v->formals); x->body = lval_copy(v->body); } break; case LVAL_NUM: case LVAL_BOOL: x->num = v->num; break; /* Copy Strings using malloc and strcpy */ case LVAL_ERR: x->err = malloc(strlen(v->err) + 1); strcpy(x->err, v->err); break; case LVAL_SYM: x->sym = malloc(strlen(v->sym) + 1); strcpy(x->sym, v->sym); break; case LVAL_STR: x->str = malloc(strlen(v->str) + 1); strcpy(x->str, v->str); break; /* Copy Lists by copying each sub-expression */ case LVAL_SEXPR: case LVAL_QEXPR: x->count = v->count; x->cell = malloc(sizeof(lval*) * x->count); for (int i = 0; i < x->count; i++) { x->cell[i] = lval_copy(v->cell[i]); } break; } return x; }
lval* lval_copy(lval* v) { lval* x = (lval*)calloc(1, sizeof(lval)); if (NULL == x) return NULL; x->type = v->type; switch (v->type) { case LVAL_FUN: if (v->builtin) x->builtin = v->builtin; else { x->builtin = NULL; x->env = lenv_copy(v->env); x->formals = lval_copy(v->formals); x->body = lval_copy(v->body); } break; case LVAL_DBL: x->data.dbl = v->data.dbl; break; case LVAL_LNG: x->data.lng = v->data.lng; break; case LVAL_ERR: x->err = v->err; break; case LVAL_SYM: x->sym = (char*)malloc(strlen(v->sym) + 1); strcpy(x->sym, v->sym); break; case LVAL_SEXPR: case LVAL_QEXPR: x->count = v->count; x->cell = malloc(sizeof(lval*) * x->count); for (int i = 0; i < x->count; i++) x->cell[i] = lval_copy(v->cell[i]); break; default: // something terrible happened v->type = LVAL_ERR; v->err = LERR_OTHER; break; } return x; }
lval* lval_copy(lval *v) { lval* x; gl_log(L_DEBUG, "Copying lval of type: %s", ltype_name(v->type)); switch (v->type) { case LVAL_FUN: x = new_lval(v->type, 0); if (v->builtin) { x->builtin = v->builtin; } else { x->builtin = NULL; x->env = lenv_copy(v->env); x->formals = lval_copy(v->formals); x->body = lval_copy(v->body); } break; case LVAL_NUM: x = new_lval(v->type, 0); x->num = v->num; break; case LVAL_ERR: x = new_lval(v->type, strlen(v->err) + 1); x->err = malloc(strlen(v->err) + 1); strcpy(x->err, v->err); break; case LVAL_SYM: x = new_lval(v->type, strlen(v->sym) + 1); x->sym = malloc(strlen(v->sym) + 1); strcpy(x->sym, v->sym); break; case LVAL_STR: x = new_lval(v->type, strlen(v->str) + 1); x->str = malloc(strlen(v->str) + 1); strcpy(x->str, v->str); break; case LVAL_SEXPR: case LVAL_QEXPR: x = new_lval(v->type, sizeof(lval*) * v->count); x->count = v->count; x->cell = malloc(sizeof(lval*) * x->count); for (int i = 0; i < x->count; i++) { x->cell[i] = lval_copy(v->cell[i]); } break; } x->type = v->type; return x; }
struct lval* lval_copy(struct lval* v) { struct lval* x = malloc(sizeof(struct lval)); x->type = v->type; switch(v->type) { case LVAL_BOOL: x->flag = v->flag; break; case LVAL_NUM: x->num = v->num; break; case LVAL_ERR: STR_COPY(x->err, v->err); break; case LVAL_SYM: STR_COPY(x->sym, v->sym); break; case LVAL_FUN: x->fun_type = v->fun_type; switch (v->fun_type) { case LVAL_FUN_BUILTIN: STR_COPY(x->name, v->name); x->builtin = v->builtin; break; case LVAL_FUN_LAMBDA: x->env = lenv_copy(v->env); x->args = lval_copy(v->args); x->body = lval_copy(v->body); break; } break; case LVAL_SEXP: case LVAL_QEXP: x->count = v->count; x->cell = malloc(sizeof(struct lval*) * x->count); for (int i = 0; i < x->count; i++) { x->cell[i] = lval_copy(v->cell[i]); } break; } return x; }