Example #1
0
 public function __construct($hash, $name, $value, array &$args, array $targs)
 {
     $global = get_default($args, 'global', false);
     if ($global) {
         $nameParts = LibraryManager::SplitNamespace($value);
         $value = $nameParts[\count($nameParts) - 1];
     }
     parent::__construct(InfoKind::T_FUNC, $hash, $name, $value, $args, $targs);
     grokit_assert(array_key_exists('input', $args), 'Malformed return value from function generator ' . $name . ': No input defined.');
     $this->args = $args['input'];
     grokit_assert(array_key_exists('result', $args), 'Malformed return value from function generator ' . $name . ': No result type defined.');
     $this->resultType = $args['result'];
     if (array_key_exists('deterministic', $args)) {
         $this->deterministic = $args['deterministic'];
     }
 }
Example #2
0
function parseMatch($ast)
{
    assert_ast_type($ast, NodeType::MATCH);
    $expectedType = lookupType('base::STRING_LITERAL');
    $data = ast_node_data($ast);
    $pattern = ast_get($data, NodeKey::PATTERN);
    $expr = parseExpression(ast_get($data, NodeKey::EXPR));
    $type = lookupType('bool');
    $source = ast_node_source($ast);
    grokit_assert($expr->type() == $expectedType, 'Match expected expression of type STRING_LITERAL, got ' . $expr->type() . ' from ' . $expr->source());
    $libman =& LibraryManager::GetLibraryManager();
    // Set up a constant for the pattern matcher.
    $regex_name = generate_name('_regex');
    /*
        // STL
        $const = 'const std::regex ' . $regex_name . '(' . $pattern . ', std::regex_constants::ECMAScript | std::regex_constants::optimize );';
        $value = 'std::regex_match(' . $expr->value() . ', ' . $regex_name . ')';
    
        $libman->addHeader('<regex>');
    */
    // Oniguruma
    $const = 'const PatternMatcherOnig ' . $regex_name . '(' . $pattern . ');';
    $value = $regex_name . '.Match(' . $expr->value() . ')';
    $libman->addHeader('"PatternMatcherOnig.h"');
    // Create the new expression, taking any constants required by the inner
    // expression and then adding the one we just created.
    $ret = new ExpressionInfo($source, $type, $value, false);
    $ret->absorbMeta($expr);
    $ret->addConstant($const);
    return $ret;
}
Example #3
0
 function parseClusterWP($ast, $name, $header)
 {
     ob_start();
     LibraryManager::Push();
     $res = new GenerationInfo();
     /***************   PROCESS AST   ***************/
     $attr = lookupAttribute(ast_get($ast, NodeKey::PAYLOAD));
     $attrType = $attr->type();
     grokit_assert($attrType->is('clusterable'), 'Attempting to cluster on unclusterable attribute ' . $attr->name());
     /*************** END PROCESS AST ***************/
     // Get our headers
     $myHeaders = $header . PHP_EOL . ob_get_clean();
     $filename = $name . '.cc';
     $res->addFile($filename, $name);
     _startFile($filename);
     ClusterGenerate($name, $attr);
     _endFile($filename, $myHeaders);
     LibraryManager::Pop();
     return $res;
 }
Example #4
0
<?php

$host = "localhost";
$db_user = "******";
$db_password = "";
$db_name = "library";
require_once "library_manager.php";
if (empty($_POST['command'])) {
    $command = json_decode($_GET['command']);
} else {
    $command = json_decode($_POST['command']);
}
switch ($command->{'action'}) {
    case "search_book":
        LibraryManager::search_book($host, $db_user, $db_password, $db_name, $command->{'keyword'});
        //echo '{"status":"search}';
        break;
    case "add_book":
        LibraryManager::add_book($host, $db_user, $db_password, $db_name, $command->{'name'}, $command->{'author_firstname'}, $command->{'author_surname'}, $command->{'shelf_name'});
        echo '{"status":"add"}';
        break;
    case "edit_book":
        LibraryManager::edit_book($host, $db_user, $db_password, $db_name, $command->{'id'}, $command->{'name'}, $command->{'author_firstname'}, $command->{'author_surname'}, $command->{'shelf_name'});
        echo '{"status":"edit"}';
        break;
    case "delete_book":
        LibraryManager::delete_book($host, $db_user, $db_password, $db_name, $command->{'book_id'});
        echo '{"status":"deleted"}';
        break;
}
Example #5
0
 static function edit_book($host, $db_user, $db_password, $db_name, $id, $name, $author_firstname, $author_surname, $shelf_name)
 {
     $connection = @new mysqli($host, $db_user, $db_password, $db_name);
     if ($connection->connect_errno != 0) {
         //echo "Error: ".$connection->connect_errno;
     } else {
         $author_id = LibraryManager::add_author($host, $db_user, $db_password, $db_name, $author_firstname, $author_surname);
         $shelf_id = LibraryManager::add_shelf($host, $db_user, $db_password, $db_name, $shelf_name);
         $sql = "UPDATE book SET name='{$name}', author_id='{$author_id}', shelf_id='{$shelf_id}' WHERE book.id='{$id}'";
         if ($received_data = @$connection->query($sql)) {
             //echo "BOOK $name EDITED IN THE DATABASE WITH ID $id AUTHOR ID $author_id, SHELF ID $shelf_id<br></br>";
         } else {
             //echo "WRONG QUERY! <br></br>";
         }
         $connection->close();
         //echo "CONNECTION CLOSED <br></br>";
     }
 }
Example #6
0
 public function __construct($hash, $name, $value, array $args, array $origArgs)
 {
     parent::__construct(InfoKind::T_TYPE, $hash, $name, $value, $args, $origArgs[0]);
     $iterator = 'ColumnIterator<' . $value . '>';
     if (array_key_exists('complex', $args)) {
         $iter = $args['complex'];
         if ($iter !== false) {
             $iterator = str_replace('@type', $this->value(), $iter);
         }
     }
     $this->iterator = $iterator;
     if (array_key_exists('destroy', $args)) {
         $destroy = $args['destroy'];
         if (is_bool($destroy)) {
             $this->destroy = $destroy;
         } else {
             grokit_error("Expected boolean value for 'destroy' attribute of datatype " . $this->value());
         }
     }
     if (array_key_exists('dictionary', $args)) {
         $this->dictionary = $args['dictionary'];
     }
     if (array_key_exists('fixed_size', $args)) {
         $this->fixedSize = $args['fixed_size'];
     }
     // Deal with operators defined by the type.
     if (array_key_exists('unary_operators', $args)) {
         foreach ($args['unary_operators'] as $op) {
             LibraryManager::AlphaOperator($op, 1, $this);
         }
     }
     if (array_key_exists('binary_operators', $args)) {
         foreach ($args['binary_operators'] as $op) {
             LibraryManager::AlphaOperator($op, 2, $this);
         }
     }
     if (array_key_exists('constructors', $args)) {
         foreach ($args['constructors'] as $cstr) {
             $cArgs = get_first_key($cstr, ['args', 0]);
             $cDet = get_first_key_default($cstr, ['deterministic', 1], true);
             $myType = $this->value();
             $myName = end(explode('::', $myType));
             $cName = get_first_key_default($cstr, ['c_name', 2], $myName);
             $callback = function ($args, $t_args = []) use($cArgs, $cDet, $myType, $cName) {
                 $myArgs = array_map('lookupType', $cArgs);
                 $myRet = lookupType($myType);
                 $retVal = ['kind' => 'FUNCTION', 'input' => $myArgs, 'result' => $myRet, 'deterministic' => $cDet];
                 if ($cName !== null) {
                     $retVal['name'] = $cName;
                 }
                 return $retVal;
             };
             declareFunctionGlobal('', $myType, $cArgs, $callback);
         }
     }
     if (array_key_exists('functions', $args)) {
         foreach ($args['functions'] as $fnct) {
             $fName = get_first_key($fnct, ['name', 0]);
             $fArgs = get_first_key($fnct, ['args', 1]);
             $fRet = get_first_key($fnct, ['result', 2]);
             $fDet = get_first_key_default($fnct, ['deterministic', 3], false);
             $fGlobal = get_first_key_default($fnct, ['global', 4], false);
             $cName = get_first_key_default($fnct, ['c_name', 5], null);
             foreach ($fArgs as &$arg) {
                 $arg = str_replace('@type', $this->value(), $arg);
             }
             $fRet = str_replace('@type', $this->value(), $fRet);
             $callback = function ($args, $t_args = []) use($fArgs, $fRet, $fDet, $cName, $fGlobal) {
                 $myArgs = array_map('lookupType', $fArgs);
                 $myRet = lookupType($fRet);
                 $retVal = ['kind' => 'FUNCTION', 'input' => $myArgs, 'result' => $myRet, 'deterministic' => $fDet, 'global' => $fGlobal];
                 if ($cName !== null) {
                     $retVal['name'] = $cName;
                 }
                 return $retVal;
             };
             $fNS = $fGlobal ? 'BASE' : LibraryManager::ExtractNamespace($this->value());
             $fName = LibraryManager::JoinNamespace($fNS, $fName);
             declareFunctionGlobal('', $fName, $fArgs, $callback);
         }
     }
     if (array_key_exists('methods', $args)) {
         foreach ($args['methods'] as $method) {
             $mName = get_first_key($method, ['name', 0]);
             $mArgs = get_first_key($method, ['args', 1]);
             $mRet = get_first_key($method, ['result', 2]);
             $mDet = get_first_key_default($method, ['deterministic', 3], false);
             if ($mRet == '@type') {
                 $mRet = $this->value();
             }
             foreach ($mArgs as &$mArg) {
                 if ($mArg == '@type') {
                     $mArg = $this->value();
                 }
             }
             //fwrite(STDERR, 'Defining method ' . $this->value() . '->' . $mName .
             //'(' . implode(', ', $mArgs ) . ') : ' . $mRet . PHP_EOL );
             $info = new MethodInfo($mName, $mArgs, $mRet, $mDet);
             if (!array_key_exists($mName, $this->methods)) {
                 $this->methods[$mName] = [$info];
             } else {
                 foreach ($this->methods[$mName] as $cm) {
                     if ($info == $cm) {
                         grokit_error('Attempting to multiply define method ' . $this->value() . '->' . $mName . '(' . implode(', ', $mArgs) . ')');
                     }
                 }
                 $this->methods[$mName][] = $info;
             }
         }
     }
     if (array_key_exists('describe_json', $args)) {
         $this->describers['json'] = $args['describe_json'];
     }
 }