Ejemplo n.º 1
0
bool Cond::eval(int var, const Func* fun) const {
	int l = lhs->eval(var, fun);
	int r = rhs->eval(var, fun);
	switch (cmp) {
		case LT: return l < r;
		case GT: return l > r;
		case EQ: return l == r;
		default: throw 2;
	}
}
Ejemplo n.º 2
0
int main()
{
	Expr t = Expr("*", Expr("-", 5), Expr("+", 3, 4));
	cout << t << " = " << t.eval() << endl;
	cout << "((-5)*(3+4)) = -35" << endl;

	t = Expr("*", t, t);
	cout << t << " = " << t.eval() << endl;
	cout << "(((-5)*(3+4))*((-5)*(3+4))) = 1225" << endl;
	system("pause");
	return 0;
}
Ejemplo n.º 3
0
int Table::query(const vector<string> &names, const Expr expr,
             vector<vector<int> > &results) const {
    int count = 0;
    if (data.size() <= 0) {
        return 0;
    }

    // get queried columns
    if (std::find(names.begin(), names.end(), "*") != names.end()) { // all
        for (const auto &record : data) {
            if (expr.eval(record, indexes)) {
                // columns occurring in the where clause (if any)
                // should be in the schema of the table
                results.push_back(record);
                count++;
            }
        }
        return count;
    } else {  // selected columns
        // all columns (except *) in the select list should be in
        // the schema of the table
        for(const auto &name : names) {
            if (std::find(columns.begin(), columns.end(), name)
                == columns.end()) {
                throw DataBaseError(string("Column ") + name
                                    + string(" is not in the schema"));
            }
        }

        // use int index for reordering
        vector<int> query_indexes;
        for (const auto &name : names) {
            auto it = indexes.find(name);
            query_indexes.push_back(it->second);
        }

        for (const auto &record : data) {
            if (expr.eval(record, indexes)) {
                // columns occurring in the where clause (if any)
                // should be in the schema of the table
                vector<int> reordered;
                for (const auto &idx : query_indexes) {
                    reordered.push_back(record[idx]);
                }
                results.push_back(reordered);
                count++;
            }
        }

        return count;
    }

}
Ejemplo n.º 4
0
int main() {
  Expr * e =
    new Ap(
      new Ap(
        new Lam("x",
          new Lam("y",
            new Var("y"))),
        new Val(new IntValue(5))),
      new Val(new IntValue(3)));
  shared_ptr<Value> v = e->eval({});
  printf("%d\n", dynamic_pointer_cast<IntValue>(v)->getInt());
  return 0;
}
Ejemplo n.º 5
0
int main() {
    printf("\nCOMP6035 Coursework 2: Template Metaprogramming. \n"
            "Part I -- Expression Templates\n"
            "\nEvaluating expression x + (x-2) * (x-3), with x = 5 \n\n");

    //Based on operator precedence (BODMAS), the expression is
    //interpreted as x + ((x-2) * (x-3))
    typedef ADD<X, MULT<SUB<X, LIT<2> >, SUB<X, LIT<3> > > > Expr;
    Expr expr;
    printf("Answer = %d\n\n", expr.eval(5));

    return 0;
}
Ejemplo n.º 6
0
Archivo: eval.cpp Proyecto: tov/ipd
value_ptr Closure::apply(const std::vector<value_ptr>& actuals) const
{
    // Checked by operator():
    assert(actuals.size() == formals_.size());

    Environment env    = env_;
    auto        formal = formals_.begin();
    auto        actual = actuals.begin();

    for (; formal != formals_.end(); ++formal, ++actual) {
        env = env.extend(*formal, *actual);
    }

    return body_->eval(env);
}
Ejemplo n.º 7
0
int Table::del(const Expr expr) {
    if (data.size() <= 0) {
        return 0;
    }

    auto it = data.begin();
    int count = 0;

    // columns occurring in the where clause (if any)
    // should be in the schema of the table
    while (it != data.end()) {
        if (expr.eval(*it, indexes)) {
            it = data.erase(it);
            count++;
        } else {
            it++;
        }
    }

    return count;
}
Ejemplo n.º 8
0
int main( int argc, char *argv[] )
{
  // Open the program's source.
  if ( argc != 2 )
    usage();
  FILE *fp = fopen( argv[ 1 ], "r" );
  if ( !fp ) {
    fprintf( stderr, "Can't open file: %s\n", argv[ 1 ] );
    usage();
  }

  // Parse the whole program source into an expression object.
  // The parser uses a one-token lookahead to help parsing compound expressions.
  char tok[ MAX_TOKEN + 1 ];
  Expr *expr = parse( expectToken( tok, fp ), fp );
  
  // If this is a legal input, there shouldn't be any extra tokens at the end.
  if ( nextToken( tok, fp ) ) {
    fprintf( stderr, "line %d: unexpected token \"%s\"\n", linesRead(), tok );
    exit( EXIT_FAILURE );
  }

  fclose( fp );

  // Run the program.
  Context *ctxt = makeContext();
  char *result = expr->eval( expr, ctxt );

  // Everything evaluates to a dynamically allocated string, but we don't
  // do anything with the one out of the top-level expression.
  free( result );

  // We're done, free everything.
  freeContext( ctxt );
  expr->destroy( expr );

  return EXIT_SUCCESS;
}
Ejemplo n.º 9
0
Archivo: main.cpp Proyecto: CCJY/coliru
S evaluate(const Expr & expr) 
{
  return expr.eval();
}
void display(Expr const& e){
	cout << e << " = " << e.eval() << endl;
}
Ejemplo n.º 11
0
 A(Expr expr):value(expr.eval()){}
Ejemplo n.º 12
0
int main() {
    Expr::addFunction("sqr", sqr);
    Expr::addFunction("len2", len2);

    struct Test { const char *expr; int err; double result; } tests[] = {
        {"1", -1, 1.0},
        {"1+1", -1, 2.0},
        {"3 * 4", -1, 12.0},
        {"((1))", -1, 1.0},
        {"3*--3", -1, 9.0},
        {"1+2*(3+4)", -1, 15.0},
        {"1+2*(3+4*(5+6.5))", -1, 99.0},
        {"x1", -1, 100.0},
        {"x1+y1*2", -1, 500},
        {"1--1", -1, 2},
        {"1/(8--8) ; this comment is ignored", -1, 0.0625},
        {"1/;internal comment\n(8--8)", -1, 0.0625},
        {"1<2", -1, 1.0},
        {"1<=2", -1, 1.0},
        {"1>2", -1, 0.0},
        {"1>=2", -1, 0.0},
        {"1==2", -1, 0.0},
        {"1!=2", -1, 1.0},
        {"1!=2", -1, 1.0},
        {"!(2<1)", -1, 1.0},
        {"!!2", -1, 1.0},
        {"1<2 && 3<4", -1, 1.0},
        {"1<2 && 3>4", -1, 0.0},
        {"1<2 || 3<4", -1, 1.0},
        {"1<2 || 3>4", -1, 1.0},
        {"(0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1)!=1.0", -1, 1.0}, // Approximation!

        {"1 | 2 == 3", -1, 1.0},
        {"3 ^ 2 == 1", -1, 1.0},
        {"3 ^ 1 == 2", -1, 1.0},
        {"-1 & 7 == 7", -1, 1.0},
        {"-1 >> 1 == -1", -1, 1.0},
        {"-1 << 1 == -2", -1, 1.0},
        {"(1 << 16)-1 >> 8 == 255", -1, 1.0},

        {"abs(-1)", -1, 1.0},
        {"cos(1-1)", -1, 1.0},
        {"abs(cos(3.141592654 * 0.5)) < 1E-6", -1, 1.0},
        {"cos()", 4, -1},
        {"atan2(12)", 8, -1},
        {"atan2(0, 1)", -1, 0},
        {"abs(atan(1)-3.141592654/4) < 1E-6", -1, 1.0},
        {"random() != 1.2", -1, 1.0},
        {"abs(log(1024)/log(2) - 10) < 1E-6", -1, 1.0},
        {"floor(pow(2, 8) + 0.5) == 256", -1, 1.0},
        {"abs(sqrt(2)*sqrt(2) - 2) < 1E-6", -1, 1.0},
        {"abs(tan(3.141592654/4)-1) < 1E-6", -1, 1.0},
        {"abs(log(exp(13)) - 13) < 1E-6", -1, 1.0},
        {"abs(len2(3,4) - 5) < 1E-6", -1, 1.0},
        {"sqr(3) == 9", -1, 1.0},

        {"1+z2*4", 4, -1},
        {"1+2*", 4, -1},
        {"1+(2*3", 6, -1},
        {"1+2()", 3, -1},
        {"+1", 0, -1},
    };

    std::map<std::string, double> vars;
    vars["x0"] = 3.14;
    vars["y0"] = 2.718;
    vars["x1"] = 100;
    vars["y1"] = 200;

    int errors = 0;
    int ntests = sizeof(tests)/sizeof(tests[0]);
    for (int i=0; i<ntests; i++) {
        double res = -1;
        try {
            Expr expr = Expr::parse(tests[i].expr, vars);
            if (tests[i].err != -1) throw Expr::Error("Parsing should have failed");
            res = expr.eval();
            if (tests[i].result != res) throw Expr::Error("Unexpected result");
        } catch (Expr::Error& err) {
            if (tests[i].err == err.position) {
                // Ok; parsing error position is correct
            } else {
                errors++;
                printf("TEST FAILED: \"%s\" (err=%i, result=%0.3f) --> res=%0.3f\n%s (position=%i)\n\n",
                       tests[i].expr, tests[i].err, tests[i].result,
                       res, err.what(), err.position);
            }
        }
    }
    printf("%i errors on %i tests\n", errors, ntests);

    int w=640, h=480;
    vars["k"] = 10*3.141592654 / ((w*w+h*h)/4);
    double& y = vars["y"];
    double& x = vars["x"];
    std::vector<unsigned char> img(w*h);
    Expr e("((128 + sin(((x-320)*(x-320) + (y-240)*(y-240))*k)*127) ^"
           " (255 * ((floor(x/128)+floor(y/96)) & 1))) + random()*32-16", vars);
    printf("Expression compiled code:\n%s\n", e.disassemble().c_str());
    clock_t start = clock();
    for (int rep=0; rep<10; rep++) {
        int i = 0;
        for (y=0; y<h; y++) {
            for (x=0; x<w; x++) {
                int ie = int(e);
                if (ie < 0) ie = 0; if (ie > 255) ie = 255;
                img[i++] = ie;
            }
        }
    }
    clock_t stop = clock();
    printf("Test image generated in %0.3fms (%.0f pixels/sec)\n",
           (stop - start)*100.0/CLOCKS_PER_SEC,
           double(w*h*10)*CLOCKS_PER_SEC/(stop-start+1));
    FILE *f = fopen("test.pgm", "wb");
    if (f) {
        fprintf(f, "P5\n%i %i 255\n", w, h);
        fwrite(&img[0], 1, w*h, f);
        fclose(f);
    } else {
        fprintf(stderr, "Error generating test.pgm\n");
    }

    clock_t start2 = clock();
    double k = vars["k"];
    for (int rep=0; rep<10; rep++) {
        int i = 0;
        for (y=0; y<h; y++) {
            for (x=0; x<w; x++) {
                int ie = int(
                    (int(128 + sin(((x-320)*(x-320) + (y-240)*(y-240))*k)*127) ^
                     int(255 * (int(floor(x/128)+floor(y/96)) & 1))) + myrandom()*32-16);
                if (ie < 0) ie = 0; if (ie > 255) ie = 255;
                img[i++] = ie;
            }
        }
    }
    clock_t stop2 = clock();
    printf("Test image generated natively in %0.3fms (%.0f pixels/sec)\n",
           (stop2 - start2)*100.0/CLOCKS_PER_SEC,
           double(w*h*10)*CLOCKS_PER_SEC/(stop2-start2+1));
    f = fopen("test_native.pgm", "wb");
    if (f) {
        fprintf(f, "P5\n%i %i 255\n", w, h);
        fwrite(&img[0], 1, w*h, f);
        fclose(f);
    } else {
        fprintf(stderr, "Error generating test_native.pgm\n");
    }

    return errors != 0;
}