void internsymbol(node s, scope v){ assertpos(issym(s),s); push(complete_symbol_list,s); if (s->body.symbol.flags & intern_F) { errorpos(s,"symbol defined again ... "); return; } s->body.symbol.flags |= intern_F; if (v != NULL) reinternsymbol(s,v); /* if ( 0 == strcmp("x",tostring(s)) ) trap(); */ if (s->body.symbol.type!=keyword_T) { char *Cname; assertpos(issym(s),s); if (s->body.symbol.flags & literal_F) { Cname = tostring(s); /* no totoken here? */ if (!(s->body.symbol.flags & nouniquify_F)) Cname = uniquify(Cname); } else { Cname = totoken(tostring(s)); if (s->body.symbol.flags & (export_F | import_F)) Cname = prefixify(s->body.symbol.package,Cname); if (!(s->body.symbol.flags & nouniquify_F)) Cname = uniquify(Cname); else if (s->body.symbol.name->body.unique_string.seqno == 0) s->body.symbol.name->body.unique_string.seqno++; } s->body.symbol.Cname = Cname; } if (s->body.symbol.flags & (export_F|import_F)) exportit(s,v); }
node chktype2(node e,scope v){ node f, ftype; f = chk(e,v); if (f == bad__K) return bad_or_undefined_T; if (equal(f,type__K)) return type__T; ftype = type(f); if (ftype != type__T) { node sym; if (ftype != deferred__T) { errorpos(e,"not valid type"); return NULL; } sym = unpos(f); assert(issym(sym)); if (sym->body.symbol.value == NULL) { node t = newtype(f,NULL,FALSE); t->body.type.flags = deferred_F; assert(issym(sym)); sym->body.symbol.value = t; t->body.type.name = sym; } return sym->body.symbol.value; } return f; /* was totype(f) */ }
bool israwtypeexpr(node e) { while (ispos(e)) e = e->body.position.contents; while (issym(e)) { if (e->body.symbol.type != type__T) return FALSE; e = e->body.symbol.value; } if (!istype(e)) return FALSE; return israwtype(e); }
bool istaggedarraytypeexpr(node e){ while (ispos(e)) e = e->body.position.contents; while (issym(e)) { if (e->body.symbol.type != type__T) return FALSE; e = e->body.symbol.value; } if (istype(e)) return istaggedarraytype(e); if (!iscons(e)) return FALSE; return equal(car(e),tarray_K); }
void exportit(node s, scope v){ node package = NULL; if (v != NULL && v->previous != NULL) { package = v->previous->current_package; } if (package != NULL) { assertpos(issym(package),package); push(package->body.symbol.export_list,s); } }
char *prefixify(node package, char *name){ char buf[500]; if (package==NULL) return name; assertpos(issym(package),package); assertpos(isstr(package->body.symbol.name),package); assertpos(tostring(package) != NULL, package->body.symbol.name); buf[0]=0; setprefix(buf,package); strcat(buf,name); return strperm(buf); }
static char * normalise_whitespace(const char *p) { estring r; char lastc; /* * Run through the symbol forwards, building a new string * `r' which is the same but with probably less whitespace. * First, preallocate slightly more than enough room in `r'. */ r.append_string(p); r.truncate(); lastc = '\0'; for (;;) { /* skip any whitespace */ while (*p && isspace(*p)) p++; if (!*p) break; /* * Preserve a single space only between consecutive * alphanumerics or after commas. */ if ((issym(*p) && issym(lastc))) r.append_char(' '); /* append sequence of non-whitespace */ while (*p && !isspace(*p)) r.append_char(*p++); if (!*p) break; lastc = p[-1]; } return r.take(); }
struct POS *pos(node n) { struct POS *p; while (iscons(n)) { if (n->body.cons.pos.filename != NULL) return &n->body.cons.pos; p = pos(CAR(n)); if (p != NULL) return p; n = CDR(n); } return ( ispos(n) ? &n->body.position.pos : issym(n) && n->body.symbol.pos.filename != NULL ? &n->body.symbol.pos : istype(n) && n->body.type.name != NULL ? pos(n->body.type.name) : NULL ); }
node newsymbol(node p, node ptype, scope v, int flags){ node name; node s; struct POS *ppos = NULL; assertpos(istype(ptype),ptype); if (issym(p)) { /* defined previously so the Cname can be set by the translator */ return p; } if (isstr(p)) { name = p; p = NULL; } else if (isstrpos(p)) { ppos = &p->body.position.pos; name = p->body.position.contents; assertpos(name->tag == unique_string_tag,name); } else { errorpos(p,"defining a nonsymbol"); return NULL; } s = newnode(SYMBOL,symbol_tag); /* newnode clears the memory */ assert(s->body.symbol.flags == 0); s->body.symbol.name = name; s->body.symbol.type = ptype; s->body.symbol.pos = ppos==NULL ? empty_pos : *ppos; if (!inside_defun(v) && !(flags & tmp_F)) flags |= global_F; s->body.symbol.flags = flags & ~intern_F; if (v!=NULL && v->previous != NULL) { scope w = v->previous; assert(w->current_package==NULL || issym(w->current_package)); s->body.symbol.package = w->current_package; } if (flags & intern_F) internsymbol(s,v); return s; }
node totype(node e){ /* e is a type expression, return a unique TYPE */ node f=NULL; if (e == NULL) return NULL; if (e->tag == type_tag) return typeforward(e); if (e->tag == position_tag) e = e->body.position.contents; if (issym(e)) { node t = type(e); if (t == NULL) return NULL; assert(t == type__T); return typeforward(e->body.symbol.value); } ExpandType(e,&f); totypesRec(f); return typeforward(car(f)); /* a bit expensive */ /* later, write a simple search through */ /* existing types (the definition here is */ /* not recursive) */ }
/* takes care with symbols ending in .bwl or .bb/ww/ll */ word parse_symbol(void *result,bool getaddr, byte *etype) { char *p; void *value; byte ssize; word err; ubyte len; char size; /* if (!isalpha(*ev)) already done for us return ERRM_INVALIDSYM; */ p=ev++; while (issym(*ev)) ev++; if (issymend(*ev)) ev++; len=(ubyte)(ev-p); if ( (len>2) && (*(ev-2)=='.') ) { size=upper(*(ev-1)); if ( (size=='B') || (size=='W') || (size=='L') ) { ev-=2; len-=2; } } else if ( (len>3) && (*(ev-3)=='.') ) { size=upper(*(ev-1)); if ( ( (size=='B') || (size=='W') || (size=='L') ) && (upper(*(ev-2))==size) ) { ev-=3; len-=3; } } /* try general table */ if (!checking) { must( find_general_sym(p,len,&value,&ssize,getaddr) ); #if DOUBLES if (ssize<EXPR_NOTINT) { must( coerce_value(value,ssize,result,EXPR_LONG) ); *etype=E_NUMBER; } else { *(double*)result= ssize==EXPR_DOUBLE ? *(double*)value : *(float*)value; /* cope with singles & doubles */ *etype=E_DBLNUMBER; } #else must( coerce_value(value,ssize,result,EXPR_LONG) ); *etype=E_NUMBER; #endif } else { *etype=E_NUMBER; } return 0; }
void setcprintvalue(node sym, node val){ assert(issym(sym)); sym->body.symbol.cprintvalue = val; }
static void undefine(node s){ assertpos(issym(s),s); /* if (debug) fprintf(stderr,"undefining %s\n",tostring(s)); */ pop(s->body.symbol.name->body.unique_string.symbol_list); }
bool issym(TreeNode *p, TreeNode *q) { if(p == NULL && q == NULL) return true; if(p == NULL || q == NULL) return false; return (p->val == q->val) && issym(p->left, q->right) && issym(p->right, q->left); }
oo_token *lex(FILE *fp, size_t sz) { size_t l; char *source = malloc(sz); oo_token *head = NULL, *curr = NULL; if(!source) goto err1; if(fseek(fp, 0L, SEEK_SET) != 0) goto err0; l = fread(source, sizeof(char), sz, fp); source[++l] = 0; char *cp = source; oo_floc empty = { .line = 0, .column = 0, .offset = 0}; curr = head = alloc_token(oot_START, empty, 0, NULL); oo_floc loc = { .line = 1, .column = 1, .offset = -1 }; oo_states state = oos_start, prev = state; uint32_t end_offset = 0; oo_floc block; do { char a = *cp; oo_tokens token = oot_UNKNOWN; step(&state, &cp, &loc); if(state == oos_err) goto syn; if (state == oos_str || state == oos_ident || state == oos_num) { block.line = loc.line; block.column = loc.column; block.offset = loc.offset; } int end_of_str = (state == oos_eo_str || state == oos_eo_id || state == oos_eo_num); if (end_of_str) { /* id, num & str */ end_offset = loc.offset; int t = -1; if (state == oos_eo_id) t = oot_IDENT; if (state == oos_eo_str) t = oot_STR; if (state == oos_eo_num) t = oot_NUM; if (t == oot_IDENT || t == oot_STR || t == oot_NUM) { curr = alloc_token(t, block, end_offset - block.offset, curr); } if(state == oos_eo_id || state == oos_eo_num) { cp--; loc.offset--; } end_offset = 0; } else { /* sym */ if (state == oos_sym) { char c = *(source + (loc.offset)); token = oo_tokens_from_char(c); } if(token != oot_UNKNOWN) { curr = alloc_token(token, loc, 1, curr); } } syn: printf("o: %d, s -> %d, s_s = '%s' (%d)", (int)loc.offset, (int)state, oo_state_to_string(state), (int)a); if(isalpha(a) || isdigit(a) || issym(a)) { printf(", '%c'", a); } printf("\n"); if(state == oos_err) { if(prev == oos_in_str) { printf("Syntax error, unterminated string constant, line %d column %d\n", block.line, block.column - 1); } /* TODO: Add oot_ERR token */ curr = alloc_token(oot_EOF, loc, 1, curr); return head; } prev = state; } while(state != oos_eof); if(state == oos_eof) { curr = alloc_token(oot_EOF, loc, 0, curr); } err0: if(source) free(source), source = NULL; err1: return head; } static void step(oo_states *state, char **cp, oo_floc *loc) { int c = **cp, k = 0; int i = -1; /* unknown */ if(c != 0 && *(*cp+1) != 0) k = *(*cp +1); i = isspace(c) ? 0 : i; /* ws */ i = c == '\n' ? 1 : i; /* new line */ i = issym(c) ? 2 : i; /* sym */ i = isdigit(c) ? 3 : i; /* numeric */ i = isid(c) ? 4 : i; /* id */ i = c == '"' ? 5 : i; /* str */ i = isesc(c, k) ? 6 : i; /* escape */ i = c == 0 ? 7 : i; /* eof */ if(i == -1) printf("---> '%c'\n", c); oo_branch *b = &table[*state][i]; *state = b->state; if(b->advance) { if(b->state == oos_nl) { loc->line++; loc->column = 0; } loc->column++; loc->offset++; (*cp)++; } } static oo_tokens oo_tokens_from_char(int t) { if (t == '~') return oot_TILDE; if (t == '`') return oot_GACCENT; if (t == '!') return oot_BANG; if (t == '@') return oot_AT; if (t == '#') return oot_POUND; if (t == '$') return oot_DOLLAR; if (t == '%') return oot_PERCENT; if (t == '^') return oot_EXP; if (t == '&') return oot_AND; if (t == '*') return oot_STAR; if (t == '(') return oot_LPAREN; if (t == ')') return oot_RPAREN; if (t == '-') return oot_MINUS; if (t == '=') return oot_EQUAL; if (t == '_') return oot_UNDER; if (t == '+') return oot_PLUS; if (t == '[') return oot_LBRACKET; if (t == ']') return oot_RBRACKET; if (t == '\\') return oot_BSLASH; if (t == '{') return oot_LBRACE; if (t == '}') return oot_RBRACE; if (t == '|') return oot_BAR; if (t == ';') return oot_SEMI; if (t == '\'') return oot_SQUOTE; if (t == ':') return oot_COLON; if (t == '"') return oot_DQUOTE; if (t == ',') return oot_COMMA; if (t == '.') return oot_DOT; if (t == '/') return oot_FSLASH; if (t == '<') return oot_LT; if (t == '>') return oot_GT; if (t == '?') return oot_WHAT; return oot_UNKNOWN; } char *string_from_oo_tokens(oo_tokens t) { if (t == oot_START) return "oot_START"; if (t == '~') return "oot_TILDE"; if (t == '`') return "oot_GACCENT"; if (t == '!') return "oot_BANG"; if (t == '@') return "oot_AT"; if (t == '#') return "oot_POUND"; if (t == '$') return "oot_DOLLAR"; if (t == '%') return "oot_PERCENT"; if (t == '^') return "oot_EXP"; if (t == '&') return "oot_AND"; if (t == '*') return "oot_STAR"; if (t == '(') return "oot_LPAREN"; if (t == ')') return "oot_RPAREN"; if (t == '-') return "oot_MINUS"; if (t == '=') return "oot_EQUAL"; if (t == '_') return "oot_UNDER"; if (t == '+') return "oot_PLUS"; if (t == '[') return "oot_LBRACKET"; if (t == ']') return "oot_RBRACKET"; if (t == '\\') return "oot_BSLASH"; if (t == '{') return "oot_LBRACE"; if (t == '}') return "oot_RBRACE"; if (t == '|') return "oot_BAR"; if (t == ';') return "oot_SEMI"; if (t == '\'') return "oot_SQUOTE"; if (t == ':') return "oot_COLON"; if (t == '"') return "oot_DQUOTE"; if (t == ',') return "oot_COMMA"; if (t == '.') return "oot_DOT"; if (t == '/') return "oot_FSLASH"; if (t == '<') return "oot_LT"; if (t == '>') return "oot_GT"; if (t == '?') return "oot_WHAT"; if (t == oot_IDENT) return "oot_IDENT"; if (t == oot_NUM) return "oot_NUM"; if (t == oot_STR) return "oot_STR"; if (t == oot_EOF) return "oot_EOF"; return "oot_UNKNOWN"; } static char *oo_state_to_string(oo_states state) { if (state == oos_start) return "oos_start"; if (state == oos_ws) return "oos_ws"; if (state == oos_nl) return "oos_nl"; if (state == oos_sym) return "oos_sym"; if (state == oos_num) return "oos_num"; if (state == oos_ident) return "oos_ident"; if (state == oos_str) return "oos_str"; if (state == oos_esc) return "oos_esc"; if (state == oos_in_str) return "oos_in_str"; if (state == oos_in_id) return "oos_in_id"; if (state == oos_in_num) return "oos_in_num"; if (state == oos_eo_str) return "oos_eo_str"; if (state == oos_eo_id) return "oos_eo_id"; if (state == oos_eo_num) return "oos_eo_num"; if (state == oos_err) return "oos_err"; if (state == oos_eof) return "oos_eof"; return "undefined"; } static int issym(int c) { return c == '[' || c == ']' || c == '\\' || c == '{' || c == '}' || c == '|' || c == ';' || c == '\''|| c == ':' || c == '"' || c == ',' || c == '.' || c == '/' || c == '<' || c == '>' || c == '?' || c == '~' || c == '`' || c == '!' || c == '@' || c == '#' || c == '$' || c == '%' || c == '^' || c == '&' || c == '*' || c == '(' || c == ')' || c == '-' || c == '_' || c == '=' || c == '+' ; } static int isid(int c) { return isalpha(c) || c == '_'; } static int isesc(int c, int k) { if(c == 0) return 0; if(k == 0) return 0; if(c == '\\') { if (k == '"') return 1; if (k == '\\') return 1; if (k == 'r') return 1; if (k == 'n') return 1; } return 0; }
void reinternsymbol(node s, scope v){ assertpos(issym(s),s); push(s->body.symbol.name->body.unique_string.symbol_list,s); push(v->symbols,s); }
char * normalise_mangled(const char *sym) { #ifdef HAVE_LIBBFD string_var buf = sym; int func_count = 0; char *p; p = (char *)buf.data() + strlen(buf.data()) - 1; if (strchr(buf.data(), LEFT_BRACKET) == 0) return buf.take(); /* not mangled */ /* * Use a simple state machine to strip off the return type. * It's simple (rather than trivial) to handle return types * which are pointers to functions. */ do { while (p > buf.data() && isspace(*p)) p--; if (*p == RIGHT_BRACKET) { int bracket_count = 0; do { switch (*p--) { case LEFT_BRACKET: bracket_count--; break; case RIGHT_BRACKET: bracket_count++; break; } } while (p > buf.data() && bracket_count > 0); } while (p > buf.data() && isspace(*p)) p--; if (*p == RIGHT_BRACKET) { *p-- = '\0'; func_count++; } else if (*p == LEFT_BRACKET) { *p = ' '; --func_count; } else { while (p > buf.data() && issym(*p)) p--; if (p > buf.data() && (*p == '*' || *p == '&')) *p-- = ' '; } } while (func_count); /* * At this point `p' points to (some whitespace before) * the symbol with the return type stripped. Now normalise * the whitespace. */ buf = normalise_whitespace(p); /* * The C++ main routine is mangled as just "main". */ if (!strcmp(buf, "main(int,char**)") || !strcmp(buf, "main(int,char**,char**)")) buf = "main"; return buf.take(); #else return g_strdup(sym); #endif }
int main(int argc, char*argv[]) { int number; char formula_string[10][MAX_FORUMLA_LEN] ; //= "sin("STR(PI)"/2)*cos(0)"; SYM_PAIR sym_value_map[MAX_SYM_NUM]; int sym_num = 0; real ret = 0; int i; int len; int j = 0; int save_str_num = 0; char * newline = NULL; while (1) { newline = rl_gets(); if (newline == NULL) { continue; } else { if (!strcmp(newline, "exit")) { break; } else { if(issym(newline)) { if (sym_num < MAX_SYM_NUM) { get_sym_name_value(newline, &sym_value_map[sym_num]); sym_num++; } } else { F_NODE *formula = NULL; formula = parse(newline); ret = compute(formula, sym_value_map, sym_num); printf("result: %s = %lf\n", newline, ret); } } } } /* for (i = 0; i < 10; ++i) { printf("Please input the symbol and value:\n"); scanf("%s %lf", sym_value_map[0].sym_name, &sym_value_map[0].sym_value); sym_num = 1; getchar(); printf("Please input the formula (without space!!!):\n"); // scanf("%s", formula_string); // TODO input data. // gets(formula_string); fgets(formula_string[j], MAX_FORUMLA_LEN, stdin); len = strlen(formula_string[j]); if (formula_string[j][len - 1] == '\n') { formula_string[j][len - 1] = '\0'; } F_NODE *formula = NULL; formula = parse(formula_string[j]); ret = compute(formula, sym_value_map, sym_num); printf("%s = %lf\n", formula_string[j], ret); free_node(formula); j = (j + 1) % 10; save_str_num++; if (save_str_num > 10 ) { save_str_num = 10; } }*/ return 0; }
/* Test if the expression is a symbol */ static exp_t * prim_sym(exp_t *args) { chkargs("symbol?", args, 1); return issym(car(args)) ? true : false; }
bool isSymmetric(TreeNode *root) { if(root == NULL) return true; return issym(root->left, root->right); }