void add(struct oper op, char finish_rem, int curline) { if (finish_rem == 'f') { if (op.type == LEFT) pusho(&o, op); else if (sizeo(&o) == 0) pusho(&o, op); else { while (sizeo(&o) > 0 && topo(&o).type != LEFT && priority[topo(&o).type] >= priority[op.type]) { struct command* newcom = (struct command*)malloc(sizeof(struct command)); newcom->input = newcom->output = NULL; newcom->type = types[topo(&o).type]; if (sizec(&s) < 2) printError(curline); newcom->u.command[1] = topc(&s); popc(&s); newcom->u.command[0] = topc(&s); popc(&s); pushc(&s, newcom); popo(&o); } if (op.type == RIGHT && topo(&o).type == LEFT) popo(&o); else pusho(&o, op); if (op.type == RIGHT) { struct command* ncom = (struct command*)malloc(sizeof(struct command)); ncom->input = ncom->output = NULL; ncom->type = SUBSHELL_COMMAND; ncom->u.subshell_command = topc(&s); popc(&s); pushc(&s, ncom); } } } else if (finish_rem == 't') { while (sizeo(&o) > 0) { struct command* newcom = (struct command*)malloc(sizeof(struct command)); newcom->input = newcom->output = NULL; newcom->type = types[topo(&o).type]; if (sizec(&s) < 2) printError(curline); newcom->u.command[1] = topc(&s); popc(&s); newcom->u.command[0] = topc(&s); popc(&s); pushc(&s, newcom); popo(&o); } } }
char * read_token(void) { int c; int left_paren = 0; char *p; p = poolp; c = readc(); while (isspace(c)) c = readc(); do { if (c == '(') { left_paren++; pushc(c); } else if (c == ')') { if (left_paren == 0) { break; } else { left_paren--; pushc(c); } } else if (isspace(c) && left_paren == 0) { do c = readc(); while (isspace(c)); break; } else if (c == '@') { c = readc(); if (c == '\'') { (void)read_symbol(0); poolp--; } else if (c == '[') { (void)read_symbol(1); poolp--; } else if (c == '@') { pushc(c); } else { char *name; unreadc(c); poolp = name = read_function(); (void)translate_function(poolp); } } else { pushc(c); } c = readc(); } while (1); unreadc(c); pushc('\0'); return(p); }
unsigned get_arg(void) { SCRATCH int c; SCRATCH unsigned a, u; a = NUM; u = 0; bad = FALSE; switch (lex() -> attr & TYPE) { case SEP: exp_error('S'); a = NONE; break; case EOL: unlex(); a = NONE; break; case REG: a = token.valu; break; case OPR: if (token.valu == '(') { if ((lex() -> attr & TYPE) == REG) { a = token.valu; lex(); switch (a) { case C: a = C_IND; break; case BC: case DE: case SP: a += SP_IND - SP; break; case HL: a = HL_IND; break; case IX: a = IX_IND; goto do_ix_ind; case IY: a = IY_IND; do_ix_ind: if ((token.attr & TYPE) == OPR && (token.valu == '+' || token.valu == '-')) { unlex(); if ((u = eval(LPREN)) > 0x7f && u < 0xff80) exp_error('V'); } break; default: exp_error('R'); break; } if ((token.attr & TYPE) != OPR || token.valu != ')') exp_error(')'); break; } else { unlex(); u = eval(LPREN); trash(); pushc(c = popc()); if (c == ',' || c == '\n') { a = NUM_IND; break; } token.attr = VAL; token.valu = u; } } case VAL: case STR: unlex(); u = eval(START); unlex(); break; } arg.valu = bad ? 0 : u; return arg.attr = a; }
void get_function(void) { function = read_function(); function_symbol = search_symbol(function, &function_code, 0); if (function_symbol == NULL) { function_symbol = poolp; pushstr("ECL_NIL"); pushc('\0'); } function_c_name = translate_function(function); }
void infixtoprefix(char a[],char x[]) { char y[1000]; int i,n=strlen(a),topx=-1,topy=-1; for(i=n-1;i>=0;i--) { if((a[i]>=48&&a[i]<=57)||(a[i]>=65&&a[i]<=90)||(a[i]>=97&&a[i]<=122)) { pushc(y,&topy,a[i]); } else { while((topx!=-1)&&(priority(pushtopc(x,&topx))>priority(a[i]))) { pushc(y,&topy,popc(x,&topx)); } pushc(x,&topx,a[i]); } } while(topx!=-1) { pushc(y,&topy,popc(x,&topx)); } while(topy!=-1) { pushc(x,&topx,popc(y,&topy)); } pushc(x,&topx,'\0'); }
char * read_symbol(int code) { char c, *name = poolp; char end = code? ']' : '\''; c = readc(); while (c != end) { if (c == '_') c = '-'; pushc(c); c = readc(); } pushc(0); name = search_symbol(poolp = name, 0, code); if (name == NULL) { name = poolp; printf("\nUnknown symbol: %s\n", name); pushstr("unknown"); } return name; }
char * read_function() { char c, *name = poolp; c = readc(); if (c == '"') { c = readc(); while (c != '"') { pushc(c); c = readc(); } pushc(0); return name; } while (c != '(' && !isspace(c) && c != ')' && c != ',') { if (c == '_') c = '-'; pushc(c); c = readc(); } unreadc(c); pushc(0); return name; }
char * search_function(char *name) { int i; for (i = 0; cl_symbols[i].name != NULL; i++) { if (cl_symbols[i].translation != NULL && !strcasecmp(name, cl_symbols[i].name)) { name = poolp; pushstr(cl_symbols[i].translation); pushc(0); return name; } } return name; }
void pof_pf() { char postfix[20],prefix[20],ch[2],temp1[2],temp2[2]; int i,len; printf("Enter the postfix expression: "); scanf("%s",postfix); len=strlen(postfix); prefix[0]='\0'; for(i=0;i<len;i++) { ch[0]=postfix[i]; ch[1]='\0'; switch(ch[0]) { case '+': case '-': case '*': case '/': case '$': temp1[0]=popc(); temp2[0]=popc(); temp1[1]='\0'; temp2[1]='\0'; strcat(prefix,ch); //printf("%s",prefix); strcat(prefix,temp2); strcat(prefix,temp1); break; default: pushc(ch[0]); break; } } printf("The prefix expression is : %s",prefix); }
/* generate html from a markup fragment */ void ___mkd_reparse(char *bfr, int size, mkd_flag_t flags, MMIOT *f, char *esc) { MMIOT sub; struct escaped e; ___mkd_initmmiot(&sub, f->footnotes); sub.flags = f->flags | flags; sub.cb = f->cb; sub.ref_prefix = f->ref_prefix; if ( esc ) { sub.esc = &e; e.up = f->esc; e.text = esc; } else sub.esc = f->esc; push(bfr, size, &sub); pushc(0, &sub); S(sub.in)--; text(&sub); ___mkd_emblock(&sub); Qwrite(T(sub.out), S(sub.out), f); /* inherit the last character printed from the reparsed * text; this way superscripts can work when they're * applied to something embedded in a link */ f->last = sub.last; ___mkd_freemmiot(&sub, f->footnotes); }
void toBack(char str[]){ int stillint = 0; int str_len = getlength(str); int num = 0; int i = 0; for(i = 0; i < str_len; i++){ char c = str[i]; if(isint(c)){ if(stillint){ if((i+1) != str_len){ if(isint(str[i+1])){ poly_exp[num] = c; num++; } else{ stillint = 0; poly_exp[num] = c; num++; poly_exp[num] = '~'; num++; } } else{ poly_exp[num] = c; num++; } } else{ poly_exp[num] = c; num++; stillint = 1; } } else{ if(stillint){ poly_exp[num] = '~'; num++; stillint = 0; } if(c == '('){ pushc(c); }else{ if(c == ')'){ while(ctop() != '('){ poly_exp[num] = ctop(); num++; popc(); } popc(); }else{ if(empytc()){ pushc(c); }else{ if(getType(c) > getType(ctop())){ pushc(c); }else{ while((1 - empytc()) && getType(c) <= getType(ctop())){ poly_exp[num] = popc(); num++; } pushc(c); } } } } } } while(empytc() == 0){ poly_exp[num] = ctop(); num++; popc(); } }
int main() { char infx[MAX]; int c, j, i, a, b; start: printf("\n1.Convert Infix to Postfix\n2.Evaluate Postfix\n3.Exit\n : "); scanf("%d", &c); if(c == 1) { top = topc = -1; pofx[0] = '\0'; printf("\nEnter an infix expression : "); scanf("%s", infx); strcat(infx, ")"); pushc('('); for(i = 0; infx[i] != '\0'; i++) { if(infx[i] == '(') pushc('('); else if(isalnum(infx[i])) { pofx[j = strlen(pofx)] = infx[i]; pofx[j + 1] = '\0'; } else if(infx[i] == ')') { while(peepc() != '(') { pofx[j = strlen(pofx)] = popc(); pofx[j+ 1] = '\0'; } popc(); } else if(ispunct(infx[i])) { while(prec(peepc(), infx[i]) >= 0) { pofx[j = strlen(pofx)] = popc(); pofx[j+ 1] = '\0'; } pushc(infx[i]); } } printf("\nPostfix Expression is : "); for(i = 0; pofx[i] != '\0' ;i++) printf("%c ", pofx[i]); } else if(c == 2) { top = topc = -1; printf("\nEnter Postfix expression : "); scanf("%s", pofx); strcat(pofx, "#"); for(i = 0; pofx[i] != '#'; i++) { if(isdigit(pofx[i])) push(pofx[i] - '0'); else { a = pop(); b = pop(); if(pofx[i] == '+') push(b + a); else if(pofx[i] == '-') push(b - a); else if(pofx[i] == '*') push(b * a); else if(pofx[i] == '/') push(b / a); else if(pofx[i] == '^') push(pow(b, a)); } } printf("\nAnswer is : %d\n", pop()); } else if(c == 3) return 0; else printf("\nWrong Choice!\n"); goto start; }
command_stream_t make_command_stream(int(*getbyte) (void *), void *arg) { initStackc(&s); initStacko(&o); struct command_stream *command_stream_t = (struct command_stream *)malloc(sizeof(struct command_stream)); initStream(command_stream_t); struct command*com = (struct command*)malloc(sizeof(struct command)); com->type = SIMPLE_COMMAND; com->input = com->output = NULL; struct oper o; char c; bool extra = false; bool done = false; int csize = 10; com->u.word = (char**)malloc(10 * sizeof(char*)); int count = 0; int length = 0; int mlength = 12; char* word = (char*)malloc(12 * sizeof(char)); c = getbyte(arg); int curline = 1; skipws(getbyte, arg, &c, &curline); while (c >= 0) { done = false; if (!isValidChar(c)) printError(curline); extra = false; if (isvalid(c)) { word[length] = c; length++; if (length >= mlength) { mlength += 5; word = (char*)realloc(word, mlength*sizeof(char)); } } else { if (c == '#') { while ((c = (char)getbyte(arg)) >= 0 && c != '\n'); } else if (c == '\\') { while (c == '\\') { c = (char)getbyte(arg); c = (char)getbyte(arg); } continue; } else if (c == ' ') { if (length > 0) { addWord(&com, &count, &length, word); if (count == csize - 1) { csize += 5; com->u.word = (char**)realloc(com->u.word, csize*sizeof(int)); } } else { c = (char)getbyte(arg); continue; } } else if (c == '|' || c == '&' || c == ';' || c == '\n' || c == '(' || c == ')') { /*if (com->type == SUBSHELL_COMMAND) { com = (struct command*)malloc(sizeof(struct command)); com->type = SIMPLE_COMMAND; com->u.word = (char**)malloc(10 * sizeof(char*)); com->input = com->output = NULL; count = 0; csize = 10; } */ addWord(&com, &count, &length, word); if (c == '|') { c = (char)getbyte(arg); if (c == '|') { o.type = OR; } else { o.type = PIPE; } } else if (c == '&') { o.type = AND; c = (char)getbyte(arg); if (c != '&') printError(curline); } else if (c == ';') { o.type = SEQ; c = (char)getbyte(arg); } else if (c == '(') { o.type = LEFT; } else if (c == ')') { o.type = RIGHT; } else if (c == '\n') { curline += 1; while ((c = (char)getbyte(arg)) == ' '); if (c < 0) break; while (c == '\\') { c = (char)getbyte(arg); c = (char)getbyte(arg); } if (c == '#') { while ((c = getbyte(arg)) >= 0 && c != '\n'); } if (c != '\n' && (com->u.word != NULL || com->u.subshell_command != NULL)) { o.type = SEQ; extra = true; } else { extra = false; if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } o.type = SEQ; add(o, 't', curline); insert(command_stream_t, topc(&s)); popc(&s); done = true; reset(&com, &word, &length, &count, &csize, &mlength, 'f'); c = (char)getbyte(arg); skipws(getbyte, arg, &c, &curline); continue; } } if (o.type != PIPE && o.type != SEQ) { c = (char)getbyte(arg); } if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } add(o, 'f', curline); if (o.type == RIGHT) reset(&com, &word, &length, &count, &csize, &mlength, 't'); else reset(&com, &word, &length, &count, &csize, &mlength, 'f'); if (o.type != RIGHT) { skipws(getbyte, arg, &c,&curline); } extra = true; } else if (c == '>' || c == '<') { char* ptr; int insize = 12; ptr = (char*)malloc(insize * sizeof(char)); int i = 0; char t = c; while ((c = (char)getbyte(arg)) == ' '); if (!isvalid(c)) printError(curline); while ((c >= 0)) { if (!isvalid(c)) { extra = true; break; } ptr[i] = c; i++; if (i == insize - 1) { insize += 5; ptr = (char*)realloc(ptr, insize*sizeof(char)); } c = (char)getbyte(arg); } ptr[i] = '\0'; if (t == '<') com->input = ptr; else com->output = ptr; } } if (!extra) c = (char)getbyte(arg); } if (!done) { addWord(&com, &count, &length, word); if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } o.type = SEQ; add(o, 't', curline); if (sizec(&s) != 1) printError(curline); insert(command_stream_t, topc(&s)); popc(&s); } return command_stream_t; }
void main_loop(void) { int c; int in_defun=0; char *p; lineno = 1; reset(); put_lineno(); LOOP: c = jump_to_at(); if (c == ')') { if (!in_defun) error("unmatched @) found"); in_defun = 0; putc('}',out); reset(); goto LOOP; } else if (c == '\'') { char *p; poolp = pool; p = read_symbol(0); pushc('\0'); fprintf(out,"%s",p); goto LOOP; } else if (c == '[') { char *p; poolp = pool; p = read_symbol(1); pushc('\0'); fprintf(out,"%s",p); goto LOOP; } else if (c != '(') { char *p; unreadc(c); poolp = pool; poolp = p = read_function(); fprintf(out,"%s",translate_function(poolp)); goto LOOP; } p = read_token(); if (strcmp(p, "defun") == 0) { if (in_defun) error("@) expected before new function definition"); in_defun = 1; get_function(); get_lambda_list(); put_fhead(); put_lineno(); c = jump_to_at(); put_declaration(); put_lineno(); } else if (strcmp(p, "return") == 0) { tab_save = tab; get_return(); put_return(); } else error_symbol(p); goto LOOP; }
char * search_symbol(char *name, int *symbol_code, int code) { int i; for (i = 0; cl_symbols[i].name != NULL; i++) { if (!strcasecmp(name, cl_symbols[i].name)) { name = poolp; if (code) { pushstr("ecl_make_fixnum(/*"); pushstr(cl_symbols[i].name); pushstr("*/"); if (i >= 1000) pushc((i / 1000) % 10 + '0'); if (i >= 100) pushc((i / 100) % 10 + '0'); if (i >= 10) pushc((i / 10) % 10 + '0'); pushc(i % 10 + '0'); pushstr(")"); pushc(0); } else if (i == 0) { pushstr("ECL_NIL"); pushc(0); } else { pushstr("ECL_SYM(\""); pushstr(cl_symbols[i].name); pushstr("\","); if (i >= 1000) pushc((i / 1000) % 10 + '0'); if (i >= 100) pushc((i / 100) % 10 + '0'); if (i >= 10) pushc((i / 10) % 10 + '0'); pushc(i % 10 + '0'); pushstr(")"); pushc(0); } if (symbol_code) *symbol_code = i; return name; } } return NULL; }
void pushstr(const char *s) { while (*s) pushc(*(s++)); }