static bool isBefore(Tree k1, Tree k2) { // before comparing replace (type . label) by label if(isList(k1)) { k1 = tl(k1); } if(isList(k2)) { k2 = tl(k2); } // fprintf(stderr, "isBefore("); print(k1, stderr); fprintf(stderr,", "); print(k2, stderr); fprintf(stderr,")\n"); Sym s1, s2; if(!isSym(k1->node(), &s1)) { ERROR("the node of the tree is not a symbol", k1); } if(!isSym(k2->node(), &s2)) { ERROR("the node of the tree is not a symbol", k2); } // fprintf (stderr, "strcmp(\"%s\", \"%s\") = %d\n", name(s1), name(s2), strcmp(name(s1), name(s2))); return strcmp(name(s1), name(s2)) < 0; }
bool isSym(TreeNode* leftTree, TreeNode* rightTree) { if (!leftTree && !rightTree) return true; if (!leftTree || !rightTree) return false; if (leftTree->val != rightTree->val) return false; if (!isSym(leftTree->left, rightTree->right)) return false; if (!isSym(leftTree->right, rightTree->left)) return false; return true; //return (leftTree->val == rightTree->val) && isSym(leftTree->left, rightTree->right) && isSym(leftTree->right, rightTree->left); }
bool isSym(TreeNode* left, TreeNode* right){ if(left == NULL && right == NULL) return true; else if(left != NULL && right != NULL) { return (left->val == right->val && isSym(left->right, right->left)&& isSym(left->left, right->right)); } else return false; }
// (use sym . prg) -> any // (use (sym ..) . prg) -> any any doUse(any x) { any y; x = cdr(x); if (isSym(y = car(x))) { bindFrame f; Bind(y,f); x = prog(cdr(x)); Unbind(f); } else { struct { // bindFrame struct bindFrame *link; int i, cnt; struct {any sym; any val;} bnd[length(y)]; } f; f.link = Env.bind, Env.bind = (bindFrame*)&f; f.i = f.cnt = 0; do { f.bnd[f.cnt].sym = car(y); f.bnd[f.cnt].val = val(car(y)); ++f.cnt; } while (isCell(y = cdr(y))); x = prog(cdr(x)); while (--f.cnt >= 0) val(f.bnd[f.cnt].sym) = f.bnd[f.cnt].val; Env.bind = f.link; } return x; }
Token DnInLexer::String() { std::string buf; consume(); do { buf.append(1, c); consume(); } while(isDecimal() || isLetter() || isSym()); consume(); return Token(STRING, buf); }
// (bind 'sym|lst . prg) -> any any doBind(any ex) { any x, y; x = cdr(ex); if (isNum(y = EVAL(car(x)))) argError(ex, y); if (isNil(y)) return prog(cdr(x)); if (isSym(y)) { bindFrame f; Bind(y,f); x = prog(cdr(x)); Unbind(f); return x; } { struct { // bindFrame struct bindFrame *link; int i, cnt; struct {any sym; any val;} bnd[length(y)]; } f; f.link = Env.bind, Env.bind = (bindFrame*)&f; f.i = f.cnt = 0; do { if (isNum(car(y))) argError(ex, car(y)); if (isSym(car(y))) { f.bnd[f.cnt].sym = car(y); f.bnd[f.cnt].val = val(car(y)); } else { f.bnd[f.cnt].sym = caar(y); f.bnd[f.cnt].val = val(caar(y)); val(caar(y)) = cdar(y); } ++f.cnt; } while (isCell(y = cdr(y))); x = prog(cdr(x)); while (--f.cnt >= 0) val(f.bnd[f.cnt].sym) = f.bnd[f.cnt].val; Env.bind = f.link; return x; } }
bool isSymmetric(TreeNode *root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(root == NULL) { return true; } return isSym(root->left, root->right); }
bool isSym(TreeNode *r1,TreeNode *r2) { bool iseq=false; if(r1==NULL&&r2==NULL) return true; if(r1==NULL||r2==NULL) return false; if(r1->val==r2->val) iseq=true; if(iseq) { bool one=isSym(r1->left,r2->right); bool two=isSym(r1->right,r2->left); return one&&two; } return false; }
bool isSym(TreeNode *left, TreeNode *right) { if(left == NULL && right == NULL) { return true; } if(left == NULL || right == NULL) { return false; } if(left->val != right->val) { return false; } if(!isSym(left->right, right->left)) { return false; } if(!isSym(left->left, right->right)) { return false; } return true; }
bool isBoxIdent(Tree t0, const char** str) { Tree t1; Sym s; if ( isTree(t0, gGlobal->BOXIDENT, t1) && isSym(t1->node(), &s) ) { *str = name(s); return true; } else { return false; } }
Token DnLexer::Id() { std::string buf; int resv = -1; do { buf.append(1, c); consume(); } while(isDecimal() || isLetter() || isSym()); if((resv = isReserved(buf)) != -1){ /* Is this id reserved word? */ return Token(2 + resv, buf); } return Token(ID, buf); }
static void traceIndent(int i, any x, char *s) { if (i > 64) i = 64; while (--i >= 0) Env.put(' '); if (isSym(x)) print(x); else print(car(x)), space(), print(cdr(x)), space(), print(val(This)); outString(s); }
// (isa 'cls|typ 'any) -> obj | NIL any doIsa(any ex) { any x; cell c1; x = cdr(ex), Push(c1, EVAL(car(x))); x = cdr(x), x = EVAL(car(x)); if (isSym(x)) { Fetch(ex,x); drop(c1); if (isSym(data(c1))) return isa(data(c1), x)? x : Nil; while (isCell(data(c1))) { if (!isa(car(data(c1)), x)) return Nil; data(c1) = cdr(data(c1)); } return x; } drop(c1); return Nil; }
// (do 'flg|num ['any | (NIL 'any . prg) | (T 'any . prg) ..]) -> any any doDo(any x) { any y, z, a; cell c1; x = cdr(x); if (isNil(data(c1) = EVAL(car(x)))) return Nil; Save(c1); if (isNum(data(c1))) { if (isNeg(data(c1))) { drop(c1); return Nil; } data(c1) = bigCopy(data(c1)); } x = cdr(x), z = Nil; for (;;) { if (isNum(data(c1))) { if (IsZero(data(c1))) { drop(c1); return z; } digSub1(data(c1)); } y = x; do { if (!isNum(z = car(y))) { if (isSym(z)) z = val(z); else if (isNil(car(z))) { z = cdr(z); if (isNil(a = EVAL(car(z)))) { drop(c1); return prog(cdr(z)); } val(At) = a; z = Nil; } else if (car(z) == T) { z = cdr(z); if (!isNil(a = EVAL(car(z)))) { val(At) = a; drop(c1); return prog(cdr(z)); } z = Nil; } else z = evList(z); } } while (isCell(y = cdr(y))); } }
// (type 'any) -> lst any doType(any ex) { any x, y, z; x = cdr(ex), x = EVAL(car(x)); if (isSym(x)) { Fetch(ex,x); z = x = val(x); while (isCell(x)) { if (!isCell(car(x))) { y = x; while (isSym(car(x))) { if (!isCell(x = cdr(x))) return isNil(x)? y : Nil; if (z == x) return Nil; } return Nil; } if (z == (x = cdr(x))) return Nil; } } return Nil; }
static bool isa(any cls, any x) { any z; z = x = val(x); while (isCell(x)) { if (!isCell(car(x))) { while (isSym(car(x))) { if (isExt(car(x))) return NO; if (cls == car(x) || isa(cls, car(x))) return YES; if (!isCell(x = cdr(x)) || z == x) return NO; } return NO; } if (z == (x = cdr(x))) return NO; } return NO; }
// (meth 'obj ['any ..]) -> any any doMeth(any ex) { any x, y; cell c1; x = cdr(ex), Push(c1, EVAL(car(x))); NeedSym(ex,data(c1)); Fetch(ex,data(c1)); for (TheKey = car(ex); ; TheKey = val(TheKey)) { if (!isSym(TheKey)) err(ex, TheKey, "Bad message"); if (isNum(val(TheKey))) { TheCls = NULL; if (y = method(data(c1))) { x = evMethod(data(c1), y, cdr(x)); drop(c1); return x; } err(ex, TheKey, "Bad message"); } } }
// (dm sym . fun|cls2) -> sym // (dm (sym . cls) . fun|cls2) -> sym // (dm (sym sym2 [. cls]) . fun|cls2) -> sym any doDm(any ex) { any x, y, msg, cls; x = cdr(ex); if (!isCell(car(x))) msg = car(x), cls = val(Class); else { msg = caar(x); cls = !isCell(cdar(x))? cdar(x) : get(isNil(cddar(x))? val(Class) : cddar(x), cadar(x)); } if (msg != T) redefine(ex, msg, val(Meth)); if (isSym(cdr(x))) { y = val(cdr(x)); for (;;) { if (!isCell(y) || !isCell(car(y))) err(ex, msg, "Bad message"); if (caar(y) == msg) { x = car(y); break; } y = cdr(y); } } for (y = val(cls); isCell(y) && isCell(car(y)); y = cdr(y)) if (caar(y) == msg) { if (!equal(cdr(x), cdar(y))) redefMsg(msg, cls); cdar(y) = cdr(x); putSrc(cls, msg); return msg; } if (!isCell(car(x))) val(cls) = cons(x, val(cls)); else val(cls) = cons(cons(msg, cdr(x)), val(cls)); putSrc(cls, msg); return msg; }
static void plisp_spih_prin(unsigned id, any x) { if (!isNil(x)) { if (isNum(x)) outNum_spi(id, unBox(x)); else if (isSym(x)) { int i, c; word w; u8 byte; for (x = name(x), c = getByte1(&i, &w, &x); c; c = getByte(&i, &w, &x)) { if (c != '^') { byte = c; platform_spi_send_recv(id, byte); } else if (!(c = getByte(&i, &w, &x))) { byte = '^'; platform_spi_send_recv(id, byte); } else if (c == '?') { byte = 127; platform_spi_send_recv(id, byte); } else { c &= 0x1F; byte = (u8)c; platform_spi_send_recv(id, byte); } } } else { while (plisp_spih_prin(id, car(x)), !isNil(x = cdr(x))) { if (!isCell(x)) { plisp_spih_prin(id, x); break; } } } } }
static void ptermh_prin(any x) { if (!isNil(x)) { if (isNum(x)) outNum_term(unBox(x)); else if (isSym(x)) { int i, c; word w; u8 byte; for (x = name(x), c = getByte1(&i, &w, &x); c; c = getByte(&i, &w, &x)) { if (c != '^') { byte = c; term_putch(byte); } else if (!(c = getByte(&i, &w, &x))) { byte = '^'; term_putch(byte); } else if (c == '?') { byte = 127; term_putch(byte); } else { c &= 0x1F; byte = (u8)c; term_putch(byte); } } } else { while (ptermh_prin(car(x)), !isNil(x = cdr(x))) { if (!isCell(x)) { ptermh_prin(x); break; } } } } }
void print (Tree t, FILE* out) { int i; double f; Sym s; void* p; if (printlist(t, out)) return; Node n = t->node(); if (isInt(n, &i)) fprintf (out, "%d", i); else if (isDouble(n, &f)) fprintf (out, "%f", f); else if (isSym(n, &s)) fprintf (out, "%s", name(s)); else if (isPointer(n, &p)) fprintf (out, "#%p", p); int k = t->arity(); if (k > 0) { char sep = '['; for (int i=0; i<k; i++) { fputc(sep, out); sep = ','; print(t->branch(i), out); } fputc(']', out); } }
// (try 'msg 'obj ['any ..]) -> any any doTry(any ex) { any x, y; cell c1, c2; x = cdr(ex), Push(c1, EVAL(car(x))); NeedSym(ex,data(c1)); x = cdr(x), Push(c2, EVAL(car(x))); if (isSym(data(c2))) { if (isExt(data(c2))) { if (!isLife(data(c2))) return Nil; db(ex,data(c2),1); } TheKey = data(c1), TheCls = NULL; if (y = method(data(c2))) { x = evMethod(data(c2), y, cdr(x)); drop(c1); return x; } } drop(c1); return Nil; }
bool isSym(TreeNode *root1, TreeNode *root2) { if (root1 == NULL && root2 == NULL) return true; if (root1->val == root2->val && root1->left->val == root2->right->val && root1->right->val == root2->left->val) return isSym(root1->left, root2->right) && isSym(root1->right, root2->left); return false; }
bool isSymmetric(TreeNode *root) { if (root == NULL) return true; return isSym(root->left, root->right); }
bool isSym(TreeNode* left, TreeNode* right) { if(!left && !right) return true; if(!left || !right) return false; if(left->val != right->val) return false; return isSym(left->left, right->right) && isSym(left->right, right->left); }
bool isSym(TreeNode* lt, TreeNode* rt){ if (!lt && !rt) return true; if ((!lt && rt) || (lt && !rt) || (lt->val != rt->val)) return false; return ( isSym(lt->left, rt->right)) && (isSym(lt->right, rt->left)); }
bool Lexer::isId() { return isLetter() || isSym(); }
bool isSymmetric(TreeNode *root) { if (!root) return true; else return isSym(root->left, root->right); }
// (for sym 'num ['any | (NIL 'any . prg) | (T 'any . prg) ..]) -> any // (for sym|(sym2 . sym) 'lst ['any | (NIL 'any . prg) | (T 'any . prg) ..]) -> any // (for (sym|(sym2 . sym) 'any1 'any2 [. prg]) ['any | (NIL 'any . prg) | (T 'any . prg) ..]) -> any any doFor(any x) { any y, body, cond, a; cell c1; struct { // bindFrame struct bindFrame *link; int i, cnt; struct {any sym; any val;} bnd[2]; } f; f.link = Env.bind, Env.bind = (bindFrame*)&f; f.i = 0; if (!isCell(y = car(x = cdr(x))) || !isCell(cdr(y))) { if (!isCell(y)) { f.cnt = 1; f.bnd[0].sym = y; f.bnd[0].val = val(y); } else { f.cnt = 2; f.bnd[0].sym = cdr(y); f.bnd[0].val = val(cdr(y)); f.bnd[1].sym = car(y); f.bnd[1].val = val(car(y)); val(f.bnd[1].sym) = Zero; } y = Nil; x = cdr(x), Push(c1, EVAL(car(x))); if (isNum(data(c1))) val(f.bnd[0].sym) = Zero; body = x = cdr(x); for (;;) { if (isNum(data(c1))) { val(f.bnd[0].sym) = bigCopy(val(f.bnd[0].sym)); digAdd(val(f.bnd[0].sym), 2); if (bigCompare(val(f.bnd[0].sym), data(c1)) > 0) break; } else { if (!isCell(data(c1))) break; val(f.bnd[0].sym) = car(data(c1)); if (!isCell(data(c1) = cdr(data(c1)))) data(c1) = Nil; } if (f.cnt == 2) { val(f.bnd[1].sym) = bigCopy(val(f.bnd[1].sym)); digAdd(val(f.bnd[1].sym), 2); } do { if (!isNum(y = car(x))) { if (isSym(y)) y = val(y); else if (isNil(car(y))) { y = cdr(y); if (isNil(a = EVAL(car(y)))) { y = prog(cdr(y)); goto for1; } val(At) = a; y = Nil; } else if (car(y) == T) { y = cdr(y); if (!isNil(a = EVAL(car(y)))) { val(At) = a; y = prog(cdr(y)); goto for1; } y = Nil; } else y = evList(y); } } while (isCell(x = cdr(x))); x = body; } for1: drop(c1); if (f.cnt == 2) val(f.bnd[1].sym) = f.bnd[1].val; val(f.bnd[0].sym) = f.bnd[0].val; Env.bind = f.link; return y; } if (!isCell(car(y))) { f.cnt = 1; f.bnd[0].sym = car(y); f.bnd[0].val = val(car(y)); } else { f.cnt = 2; f.bnd[0].sym = cdar(y); f.bnd[0].val = val(cdar(y)); f.bnd[1].sym = caar(y); f.bnd[1].val = val(caar(y)); val(f.bnd[1].sym) = Zero; } y = cdr(y); val(f.bnd[0].sym) = EVAL(car(y)); y = cdr(y), cond = car(y), y = cdr(y); Push(c1,Nil); body = x = cdr(x); while (!isNil(a = EVAL(cond))) { val(At) = a; if (f.cnt == 2) { val(f.bnd[1].sym) = bigCopy(val(f.bnd[1].sym)); digAdd(val(f.bnd[1].sym), 2); } do { if (!isNum(data(c1) = car(x))) { if (isSym(data(c1))) data(c1) = val(data(c1)); else if (isNil(car(data(c1)))) { data(c1) = cdr(data(c1)); if (isNil(a = EVAL(car(data(c1))))) { data(c1) = prog(cdr(data(c1))); goto for2; } val(At) = a; data(c1) = Nil; } else if (car(data(c1)) == T) { data(c1) = cdr(data(c1)); if (!isNil(a = EVAL(car(data(c1))))) { val(At) = a; data(c1) = prog(cdr(data(c1))); goto for2; } data(c1) = Nil; } else data(c1) = evList(data(c1)); } } while (isCell(x = cdr(x))); if (isCell(y)) val(f.bnd[0].sym) = prog(y); x = body; } for2: if (f.cnt == 2) val(f.bnd[1].sym) = f.bnd[1].val; val(f.bnd[0].sym) = f.bnd[0].val; Env.bind = f.link; return Pop(c1); }
// (run 'any ['cnt ['lst]]) -> any any doRun(any x) { any y; cell c1; bindFrame *p; x = cdr(x), data(c1) = EVAL(car(x)), x = cdr(x); if (!isNum(data(c1))) { Save(c1); if (!isNum(y = EVAL(car(x))) || !(p = Env.bind)) data(c1) = isSym(data(c1))? val(data(c1)) : run(data(c1)); else { int cnt, n, i, j; struct { // bindFrame struct bindFrame *link; int i, cnt; struct {any sym; any val;} bnd[length(x)]; } f; x = cdr(x), x = EVAL(car(x)); j = cnt = (int)unBox(y); n = f.i = f.cnt = 0; do { ++n; if ((i = p->i) <= 0 && (p->i -= cnt, i == 0)) { for (i = 0; i < p->cnt; ++i) { y = val(p->bnd[i].sym); val(p->bnd[i].sym) = p->bnd[i].val; p->bnd[i].val = y; } if (p->cnt && p->bnd[0].sym == At && !--j) break; } } while (p = p->link); while (isCell(x)) { for (p = Env.bind, j = n; ; p = p->link) { if (p->i < 0) for (i = 0; i < p->cnt; ++i) { if (p->bnd[i].sym == car(x)) { f.bnd[f.cnt].val = val(f.bnd[f.cnt].sym = car(x)); val(car(x)) = p->bnd[i].val; ++f.cnt; goto next; } } if (!--j) break; } next: x = cdr(x); } f.link = Env.bind, Env.bind = (bindFrame*)&f; data(c1) = isSym(data(c1))? val(data(c1)) : prog(data(c1)); while (--f.cnt >= 0) val(f.bnd[f.cnt].sym) = f.bnd[f.cnt].val; Env.bind = f.link; do { for (p = Env.bind, i = n; --i; p = p->link); if (p->i < 0 && (p->i += cnt) == 0) for (i = p->cnt; --i >= 0;) { y = val(p->bnd[i].sym); val(p->bnd[i].sym) = p->bnd[i].val; p->bnd[i].val = y; } } while (--n); } drop(c1); } return data(c1); }