C *copy_cell(C *c) { switch (c->type) { case LABEL: return makecell(LABEL, (V){ scpy(c->val.label) }, copy_cell(c->next)); case LIST: return makecell(LIST, (V){ .list = copy_cell(c->val.list) }, copy_cell(c->next)); case BUILTIN: return makecell(BUILTIN, (V){ .func = { scpy(c->val.func.name), c->val.func.addr} }, copy_cell(c->next));
Cell* parse(string sexpr) { // is_legal(sexpr); // delete the whitesapce at the begining and end // such that the first and last character are not white space clearwhitespace(sexpr); // if (sexpr.length() == 0) { // return nil; // } if (sexpr.length() == 0) { return nil; } if ( !is_legalexpr(sexpr)) { return nil; } // check whether is single symbol // i.e. leaf cell // if (string::npos == sexpr.find('(')) { if ('(' != sexpr[0]) { // this is leaf cell // bulid this leaf cell Cell* root = makecell( sexpr ); return root; } // the first and last character are '(' and ')', respectively // delete the two characters int length = sexpr.size(); sexpr = sexpr.substr(1, length-2); clearwhitespace(sexpr); length = sexpr.size(); // if ( inparsecar ) { // if (sexpr == "") { // Cell* ec = new Cell("()"); // return ec; // } // } // separate the s-expression into two left and right subsexps Cell* root = separate_parse(sexpr); return root; }