void GLEPolish::eval(GLEArrayImpl* stk, const char *exp, double *x) throw(ParserError) { int rtype = 1, cp = 0; GLEPcodeList pc_list; GLEPcode pcode(&pc_list); polish(exp, pcode, &rtype); *x = evalDouble(stk, &pc_list, (int*)&pcode[0], &cp); }
void test_expression_evaluator_each(GLEPolish* polish, const std::string& expression, const std::string& expectedValue) { int cp = 0; int rtype = 0; GLEPcodeList pc_list; GLEPcode pcode(&pc_list); polish->polish(expression.c_str(), pcode, &rtype); GLERC<GLEArrayImpl> stk(new GLEArrayImpl()); std::ostringstream msg; msg << expression << ": "; if (is_float(expectedValue)) { GLEMemoryCell* mc = evalGeneric(stk.get(), &pc_list, (int*)&pcode[0], &cp); gle_memory_cell_check(mc, GLEObjectTypeDouble); double expectedDouble = tokenizer_string_to_double(expectedValue.c_str()); msg << mc->Entry.DoubleVal << " == " << expectedValue; if (expectedDouble == 0.0) { unit_test_msg(fabs(mc->Entry.DoubleVal) < CUTILS_REL_PREC_FINE, msg.str()); } else { unit_test_msg(equals_rel_fine(mc->Entry.DoubleVal, expectedDouble), msg.str()); } } else { GLERC<GLEString> result(evalString(stk.get(), &pc_list, (int*)&pcode[0], &cp, true)); std::string computedString(result->toUTF8()); msg << computedString << " == " << expectedValue; unit_test_msg(expectedValue == computedString, msg.str()); } }
void whatis(Lsym *l) { int t; int def; Type *ti; if(l == 0) { fundefs(); return; } def = 0; if(l->v->set) { t = l->v->type; Bprint(bout, "%s variable", typenames[t]); if(t == TINT || t == TFLOAT) Bprint(bout, " format %c", l->v->fmt); if(l->v->comt) Bprint(bout, " complex %s", l->v->comt->base->name); Bputc(bout, '\n'); def = 1; } if(l->lt) { Bprint(bout, "complex %s {\n", l->name); for(ti = l->lt; ti; ti = ti->next) { if(ti->type) { if(ti->fmt == 'a') { Bprint(bout, "\t%s %d %s;\n", ti->type->name, ti->offset, ti->tag->name); } else { Bprint(bout, "\t'%c' %s %d %s;\n", ti->fmt, ti->type->name, ti->offset, ti->tag->name); } } else Bprint(bout, "\t'%c' %d %s;\n", ti->fmt, ti->offset, ti->tag->name); } Bprint(bout, "};\n"); def = 1; } if(l->proc) { Bprint(bout, "defn %s(", l->name); pexpr(l->proc->left); Bprint(bout, ") {\n"); pcode(l->proc->right, 1); Bprint(bout, "}\n"); def = 1; } if(l->builtin) { Bprint(bout, "builtin function\n"); def = 1; } if(def == 0) Bprint(bout, "%s is undefined\n", l->name); }
GLEMemoryCell* GLEPolish::evalGeneric(GLEArrayImpl* stk, const char *exp) throw(ParserError) { int cp = 0; int rtype = 0; GLEPcodeList pc_list; GLEPcode pcode(&pc_list); polish(exp, pcode, &rtype); return ::evalGeneric(stk, &pc_list, (int*)&pcode[0], &cp); }
void GLEPolish::evalString(GLEArrayImpl* stk, const char *exp, string *str, bool allownum) throw(ParserError) { int rtype = allownum ? 0 : 2; int cp = 0; GLEPcodeList pc_list; GLEPcode pcode(&pc_list); polish(exp, pcode, &rtype); GLERC<GLEString> result(::evalString(stk, &pc_list, (int*)&pcode[0], &cp, allownum)); *str = result->toUTF8(); }
void GLEPolish::internalEval(const char *exp, double *x) throw(ParserError) { // difference with eval: no try / catch int rtype = 1, cp = 0; GLEPcodeList pc_list; GLEPcode pcode(&pc_list); internalPolish(exp, pcode, &rtype); GLERC<GLEArrayImpl> stk(new GLEArrayImpl()); *x = evalDouble(stk.get(), &pc_list, (int*)&pcode[0], &cp); }
void HuffmanByteDec::build(const HuffmanCode& hcode) { std::vector<CodeInfo> pcode(hcode.size()); for (unsigned int i = 0; i < hcode.size(); ++i) pcode[i] = CodeInfo(hcode[i], i); sort(pcode.begin(), pcode.end()); _size = hcode.size(); int tb = 0; create(pcode, 0, pcode.size(), tb); }
void GLEPolish::internalEvalString(const char* exp, string* str) throw(ParserError) { // difference with eval_string: no try / catch int rtype = 2, cp = 0; GLEPcodeList pc_list; GLEPcode pcode(&pc_list); internalPolish(exp, pcode, &rtype); GLERC<GLEArrayImpl> stk(new GLEArrayImpl()); GLERC<GLEString> result(::evalString(stk.get(), &pc_list, (int*)&pcode[0], &cp, true)); *str = result->toUTF8(); }
void slist(Node *n, int d) { if(n == 0) return; if(n->op == OLIST) Bprint(bout, "%.*s{\n", d-1, tabs); pcode(n, d); if(n->op == OLIST) Bprint(bout, "%.*s}\n", d-1, tabs); }
void pcode(Node *n, int d) { Node *r, *l; if(n == 0) return; r = n->right; l = n->left; switch(n->op) { default: Bprint(bout, "%.*s", d, tabs); pexpr(n); Bprint(bout, ";\n"); break; case OLIST: pcode(n->left, d); pcode(n->right, d); break; case OLOCAL: Bprint(bout, "%.*slocal", d, tabs); while(l) { Bprint(bout, " %s", l->sym->name); l = l->left; if(l == 0) Bprint(bout, ";\n"); else Bprint(bout, ","); } break; case OCOMPLEX: Bprint(bout, "%.*scomplex %s %s;\n", d, tabs, n->sym->name, l->sym->name); break; case OIF: Bprint(bout, "%.*sif ", d, tabs); pexpr(l); d++; Bprint(bout, " then\n"); if(r && r->op == OELSE) { slist(r->left, d); Bprint(bout, "%.*selse\n", d-1, tabs); slist(r->right, d); } else slist(r, d); break; case OWHILE: Bprint(bout, "%.*swhile ", d, tabs); pexpr(l); d++; Bprint(bout, " do\n"); slist(r, d); break; case ORET: Bprint(bout, "%.*sreturn ", d, tabs); pexpr(l); Bprint(bout, ";\n"); break; case ODO: Bprint(bout, "%.*sloop ", d, tabs); pexpr(l->left); Bprint(bout, ", "); pexpr(l->right); Bprint(bout, " do\n"); slist(r, d+1); } }
int main (int argc, char **argv){ // Vector with only integers, used as a datastore int stack[STACK_SIZE] = {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}; // Result of Pushdown Automata int productions[50], i; // Binary Tree char btree[TREE_SIZE] = {""}; // Reverse Polish Notation string rpn; FILE *input; for (i = 0;i<TREE_SIZE;i++){ btree[i] = '_'; productions[i] = -1; } // Initial symbol btree[0] = 'E'; btree[TREE_SIZE] = '\0'; if (argc == 2){ // Expr string expr = argv[1]; int size = strlen(expr); if(strlen(expr)<50){ FILE *output = fopen("output.txt", "w"); if(output!=NULL){ fprintf(output,"Run: %s %s \n", argv[0], argv[1]); printf("Step 1. Catch regular expression ... done!\n"); // Execution of Pushdown Automata results in set of productions printf("Step 2. Running pushdown automata ... "); pushDownAutomata(expr,productions,output); printf("done!\n"); // Apply productions to build ASA printf("Step 3. Apply productions to build an btree ... "); ada_to_asa(btree,productions); fprintf(output,"\nBtree: %s", btree); printf("done!\n"); printf("Step 4. Mirroring the btree ... "); mirror(btree); fprintf(output,"\nMirrored btree: %s", btree); printf("done!\n"); // B-tree -> Reverse Polish Notation printf("Step 5. Running postorder trasversal on btree ... "); rpn = (string) malloc(50*sizeof(string)); postOrder(btree,size, 0, rpn); fprintf(output,"\nReverse polish notation: %s\n", rpn); printf("done!\n"); // RPN -> PCI printf("Step 6. Convert RPN into pcode instructions ... "); Tinstruction * instructions = toInstruction(rpn, size); printf("done!\n"); // Array of instructions printf("Step 7. Running pcode machine ... "); fprintf(output,"\nOutput:\n"); fprintf(output,"%-10s %-7s %-7s %-7s %-7s %-15s %s\n","Inst","Level","Arg","Top","Counter","Base","Stack"); pcode(instructions, stack, output); printf("done!\n"); free(rpn); // Closing File fclose(output); printf("Output file generated.\n"); }else{ printf("\nError when opening the output file."); } }else{ printf("\nToo long expression"); } }else{ printf("Insufficient params, running like this %s \"expr\"\n", argv[0]); } return 0; }