#include #include #include #include #include "paper.h" #include "impl.h" static List **tail; List* construct(Node *l) { List *lh, **save; save = tail; lh = 0; tail = &lh; build(l); tail = save; return lh; } int listlen(List *l) { int len; len = 0; while(l) { len++; l = l->next; } return len; } void build(Node *n) { List *l; Node res; if(n == 0) return; switch(n->op) { case OLIST: build(n->left); build(n->right); return; default: ppexpr(n, &res); l = pplist(res.store.type); l->store = res.store; *tail = l; tail = &l->next; } } List* addlist(List *l, List *r) { List *f; if(l == 0) return r; for(f = l; f->next; f = f->next) ; f->next = r; return l; } void append(Node *r, Node *list, Node *val) { List *l, *f; l = pplist(val->store.type); l->store = val->store; l->next = 0; r->op = OCONST; r->store.type = TLIST; if(list->store.l == 0) { list->store.l = l; r->store.l = l; return; } for(f = list->store.l; f->next; f = f->next) ; f->next = l; r->store.l = list->store.l; } int listcmp(List *l, List *r) { if(l == r) return 1; while(l) { if(r == 0) return 0; if(l->store.type != r->store.type) return 0; switch(l->store.type) { case TINT: if(l->store.ival != r->store.ival) return 0; break; case TFLOAT: if(l->store.fval != r->store.fval) return 0; break; case TSTRING: if(scmp(l->store.string, r->store.string) == 0) return 0; break; case TLIST: if(listcmp(l->store.l, r->store.l) == 0) return 0; break; } l = l->next; r = r->next; } if(l != r) return 0; return 1; } void nthelem(List *l, int n, Node *res) { if(n < 0) pperror("negative index in []"); while(l && n--) l = l->next; res->op = OCONST; if(l == nil) { res->store.type = TLIST; res->store.l = nil; return; } res->store = l->store; } void nthlist(List *l, int n, Node *res) { if(n < 0) pperror("negative index in []"); while(l && n--) l = l->next; res->op = OCONST; if(l == nil) { res->store.type = TLIST; res->store.l = nil; return; } res->store.type = TLIST; res->store.l = l; } void delete(List *l, int n, Node *res) { List **tl; if(n < 0) pperror("negative index in delete"); res->op = OCONST; res->store.type = TLIST; res->store.l = l; for(tl = &res->store.l; l && n--; l = l->next) tl = &l->next; if(l == 0) pperror("element beyond end of list"); *tl = l->next; } List* listvar(char *s, vlong v) { List *l, *tl; tl = pplist(TLIST); l = pplist(TSTRING); tl->store.l = l; l->store.string = strnode(s); l->next = pplist(TINT); l = l->next; l->store.ival = v; return tl; }