/** * example of packing three values into an expression of the form: * * (tag (v1 v2 v3)) * * COMMENTS SHOW THE STATE OF SX AS IT IS CONSTRUCTED */ sexp_t *pack(char *tag, float v1, float v2, float v3) { sexp_t *sx = new_sexp_list(new_sexp_atom(tag,strlen(tag),SEXP_BASIC)); /* sx = (tag) */ char sbuf[32]; sexp_t *vlist,*vptr; vlist = new_sexp_list(NULL); /* vlist = () */ sx->list->next = vlist; /* sx = (tag ()) */ sprintf(sbuf,"%f",v1); vlist->list = new_sexp_atom(sbuf,strlen(sbuf),SEXP_BASIC); /* vlist = (v1) */ vptr = vlist->list; /* sx = (tag (v1)) */ sprintf(sbuf,"%f",v2); vptr->next = new_sexp_atom(sbuf,strlen(sbuf),SEXP_BASIC); /* vlist = (v1 v2) */ vptr = vptr->next; /* sx = (tag (v1 v2)) */ sprintf(sbuf,"%f",v3); vptr->next = new_sexp_atom(sbuf,strlen(sbuf),SEXP_BASIC); /* vlist = (v1 v2 v3) */ /* sx = (tag (v1 v2 v3)) ---- done, return. */ return sx; }
int ml_eval(char **input, char resp[BUFSIZ], dict_t *env) { size_t len = strlen(*input); sexp_t *sx = parse_sexp(*input, len); char *v; memset(resp, 0, sizeof(*resp)); if (sx->ty != SEXP_LIST || sx->list->ty != SEXP_VALUE) { print_sexp(resp, BUFSIZ, sx); return 0; } v = sx->list->val; if (strcmp(v, "define") == 0) { insert(sx->list->next->val, sx->list->next->next, env); print_sexp(resp, BUFSIZ, sx->list->next->next); sx->list->next->next = NULL; } else if (strcmp(v, "env") == 0) { sexp_t *out, *vlist, *vptr; out = new_sexp_list(NULL); dict_t *_env = env; dict_t *next; char r2[BUFSIZ]; memset(r2, 0, BUFSIZ); while (_env != NULL) { next = _env->next; vlist = new_sexp_list(NULL); if (out->list == NULL) { out->list = vptr = vlist; } else { vptr->next = vlist; vptr = vlist; } vlist->list = new_sexp_atom(_env->varname, strlen(_env->varname), SEXP_BASIC); vlist->list->next = _env->valexp; _env = next; } print_sexp(resp, BUFSIZ, out); } else { sexp_t *ret = NULL; lookup(v, env, &ret); print_sexp(resp, BUFSIZ, ret); } return 0; }
int main(int argc, char **argv) { sexp_t *sx_in, *sx_out; int fd; char *b; size_t l = 0; CSTRING *s = NULL; pcont_t *pc; /* read data */ fd = open("testdata",O_RDONLY); if (fd <= 0) { printf("Error opening test data file ``testdata''\n"); exit(1); } b = readfile(fd,&l,1024,256); close(fd); /* report */ printf("Read %lu bytes of data.\n",(unsigned long)l); sx_in = (sexp_t *)malloc(sizeof(sexp_t)); assert(sx_in != NULL); sx_in->ty = SEXP_VALUE; sx_in->next = sx_in->list = NULL; sx_in->aty = SEXP_BINARY; sx_in->bindata = b; sx_in->binlength = l; sx_in = new_sexp_list(sx_in); printf("Created expression.\n"); print_sexp_cstr(&s,sx_in,l+1024); destroy_sexp(sx_in); b = NULL; sx_in = NULL; printf("Destroyed AST and buffer.\n"); pc = init_continuation(NULL); pc->mode = PARSER_INLINE_BINARY; pc = cparse_sexp(s->base,s->len,pc); sx_out = pc->last_sexp; printf("Parsed unparsed version back to AST.\n"); assert(sx_out != NULL); b = sx_out->list->bindata; l = sx_out->list->binlength; fd = open("testdata_out",O_RDWR|O_CREAT); if (fd <= 0) { printf("Error opening ``testdata_out'': Create empty file to write to.\n"); exit(1); } write(fd,b,l); close(fd); printf("Extracted and wrote bindata from AST.\n"); exit(1); }