Example #1
0
 if ($method == 'hint') {
     $eq['left'] = postfix($expr[0]);
     $eq['right'] = postfix($expr[1]);
     $distribute = distribute($eq);
     // var_dump($distribute);
     // var_dump($eq);
     if ($distribute != $eq) {
         $result = infix($distribute['left']) . " = " . infix($distribute['right']);
         echo json_encode(array('result' => $result, 'finalAnswer' => false));
         return "";
     }
     $firststep = firststep($distribute);
     // var_dump($firststep);
     // var_dump($distribute);
     if ($firststep != $distribute) {
         $result = infix($firststep['left']) . " = " . infix($firststep['right']);
         echo json_encode(array('result' => $result, 'finalAnswer' => false));
         return "";
     }
     $eq['left'] = analyze($firststep['left'], 'left');
     $eq['right'] = analyze($firststep['right'], 'right');
     $eq['left'] = array_pop($eq['left']);
     $eq['right'] = array_pop($eq['right']);
     // var_dump($eq);
     // var_dump($firststep);
     if ($eq != $firststep) {
         $result = $eq['left'] . "=" . $eq['right'];
         echo json_encode(array('result' => $result, 'finalAnswer' => false));
         return "";
     }
     $result = simplify($eq['left'], $eq['right']);
Example #2
0
function dja_init_operators()
{
    /*
     * Operator precedence follows Python.
     * NB - we can get slightly more accurate syntax error messages by not using the
     * same object for '==' and '='.
     * We defer variable evaluation to the lambda to ensure that terms are
     * lazily evaluated using Python's boolean parsing logic.
     */
    $operators = array('or' => infix(6, function ($context, $x, $y) {
        return $x->eval_($context) || $y->eval_($context);
    }), 'and' => infix(7, function ($context, $x, $y) {
        return $x->eval_($context) && $y->eval_($context);
    }), 'not' => prefix(8, function ($context, $x) {
        return !$x->eval_($context);
    }), 'in' => infix(9, function ($context, $x, $y) {
        return in_array($x->eval_($context), $y->eval_($context));
    }), 'not in' => infix(9, function ($context, $x, $y) {
        return !in_array($x->eval_($context), $y->eval_($context));
    }), '=' => infix(10, function ($context, $x, $y) {
        return $x->eval_($context) == $y->eval_($context);
    }), '==' => infix(10, function ($context, $x, $y) {
        return $x->eval_($context) == $y->eval_($context);
    }), '!=' => infix(10, function ($context, $x, $y) {
        return $x->eval_($context) != $y->eval_($context);
    }), '>' => infix(10, function ($context, $x, $y) {
        return $x->eval_($context) > $y->eval_($context);
    }), '>=' => infix(10, function ($context, $x, $y) {
        return $x->eval_($context) >= $y->eval_($context);
    }), '<' => infix(10, function ($context, $x, $y) {
        return $x->eval_($context) < $y->eval_($context);
    }), '<=' => infix(10, function ($context, $x, $y) {
        return $x->eval_($context) <= $y->eval_($context);
    }));
    // Assign 'id' to each:
    foreach ($operators as $key => $op) {
        $op->id = $key;
        $operators[$key] = $op;
    }
    // Hmm, globals again...
    $GLOBALS['DJA_OPERATORS'] = $operators;
}