function _defineNumericOperators($namespacePrefix, $numericTypes, $ops, $boolOps) { // Set up all of the operators between different numeric types. for ($leftInd = 0; $leftInd < \count($numericTypes); $leftInd += 1) { for ($rightInd = 0; $rightInd < $leftInd; $rightInd += 1) { $left = $namespacePrefix . $numericTypes[$leftInd]; $right = $namespacePrefix . $numericTypes[$rightInd]; $ret = $left; foreach ($ops as $name => $opval) { if (is_int($name)) { $v = null; $op = $opval; } else { $v = $opval; $op = $name; } declareOperator($op, [$left, $right], _makeOpCall($left, $right, $ret, $v)); declareOperator($op, [$right, $left], _makeOpCall($right, $left, $ret, $v)); } foreach ($boolOps as $op) { declareOperator($op, [$left, $right], _makeOpCall($left, $right, 'base::bool')); declareOperator($op, [$right, $left], _makeOpCall($right, $left, 'base::bool')); } } } }
?> ; } <?php $gContent .= ob_get_clean(); ?> ////////////// // Operators // all operators defined by C++ <?php return array('kind' => 'TYPE', "system_headers" => array("cstdlib", "cstdio", "cinttypes", 'limits'), "complex" => false, "global_content" => $gContent, 'binary_operators' => ['+', '-', '*', '/', '%', '==', '!=', '>', '<', '>=', '<='], 'unary_operators' => ['+', '-'], 'constructors' => $constructors, 'functions' => $functions, 'properties' => ['integral', 'numeric', '_primative_'], 'describe_json' => DescribeJson('integer'), 'extras' => ['size.bytes' => 2]); } declareOperator('//', ['base::SMALLINT', 'base::SMALLINT'], function ($args) { $l = lookupType('base::SMALLINT'); return ['kind' => 'OPERATOR', 'name' => '/', 'input' => [$l, $l], 'result' => $l, 'deterministic' => true]; }); // Define the constructors from the various other types. foreach (['BYTE', 'SMALLINT', 'INT', 'BIGINT', 'FLOAT', 'DOUBLE'] as $type) { $fullType = 'base::' . $type; $call = function ($args, $targs = []) use($fullType) { $arg = lookupType($fullType); $ret = lookupType('base::SMALLINT'); return array('kind' => 'FUNCTION', 'input' => [$arg], 'result' => $ret, 'deterministic' => true); }; declareFunctionGlobal('base', 'SMALLINT', [$fullType], $call); } declareSynonym("base::SMALL", "base::SMALLINT");
?> <?php return ['kind' => 'TYPE', 'complex' => false, 'system_headers' => $system_headers, 'user_headers' => ['Config.h'], 'binary_operators' => $bin_operators, 'global_content' => $globalContent, 'constructors' => $constructors, 'methods' => $methods, 'functions' => $functions, 'libraries' => $libraries, 'properties' => ['clusterable'], 'describe_json' => DescribeJson('datetime', DescribeJsonStatic(['format' => 'YYYY-MM-DD HH:mm:ss'])), 'extras' => ['size.bytes' => 4]]; } // DATETIME + INT = DATETIME declareOperator('+', ['base::DATETIME', 'base::INT'], function ($args, $targs = []) { $dateType = lookupType('base::DATETIME'); $intType = lookupType('base::INT'); return ['kind' => 'OPERATOR', 'input' => [$dateType, $intType], 'result' => $dateType, 'deterministic' => true]; }); // DATETIME - INT = DATETIME declareOperator('-', ['base::DATETIME', 'base::INT'], function ($args, $targs = []) { $dateType = lookupType('base::DATETIME'); $intType = lookupType('base::INT'); return ['kind' => 'OPERATOR', 'input' => [$dateType, $intType], 'result' => $dateType, 'deterministic' => true]; }); // DATETIME - DATETIME = INT declareOperator('-', ['base::DATETIME', 'base::DATETIME'], function ($args, $targs = []) { $dateType = lookupType('base::DATETIME'); $intType = lookupType('base::INT'); return ['kind' => 'OPERATOR', 'input' => [$dateType, $dateType], 'result' => $intType, 'deterministic' => true]; }); foreach (['==', '!=', '<', '>', '<=', '>='] as $op) { declareOperator($op, ['base::DATETIME', 'base::INT'], function ($args, $targs = []) { $dateType = lookupType('base::DATETIME'); $intType = lookupType('base::INT'); $boolType = lookupType('base::BOOL'); return ['kind' => 'OPERATOR', 'input' => [$dateType, $intType], 'result' => $boolType, 'deterministic' => true]; }); }
public static function AlphaOperator($op, $nArgs, DataType $alpha) { $aVal = $alpha->value(); switch ($nArgs) { case 1: grokit_assert(array_key_exists($op, self::$unaryOperators), 'Attempted to generate invalid unary operator ' . $op . ' with alpha type ' . $alpha); $form = self::$unaryOperators[$op]; switch ($form) { case 'ata': $args = [$alpha->value()]; $call = function () use($alpha) { return array('kind' => InfoKind::T_OP, 'input' => [$alpha], 'result' => $alpha); }; break; } break; case 2: // Binary operator grokit_assert(array_key_exists($op, self::$binaryOperators), 'Attempted to generate invalid binary operator ' . $op . ' with alpha type ' . $alpha); $form = self::$binaryOperators[$op]; switch ($form) { case 'aata': $args = [$aVal, $aVal]; $call = function () use($alpha) { return array('kind' => InfoKind::T_OP, 'input' => [$alpha, $alpha], 'result' => $alpha); }; break; case 'aatb': $args = [$aVal, $aVal]; $call = function () use($alpha) { return array('kind' => InfoKind::T_OP, 'input' => [$alpha, $alpha], 'result' => lookupType('base::bool')); }; break; } } declareOperator($op, $args, $call); }
$globalContent = ob_get_clean(); ?> <?php return array('kind' => 'TYPE', "system_headers" => array("assert.h"), "user_headers" => array("datetimedefs.h", "datecalc.h", "Constants.h", "Config.h"), "complex" => false, 'binary_operators' => ['==', '!=', '>', '<', '>=', '<='], 'global_content' => $globalContent, 'constructors' => $constructors, 'methods' => $methods, 'functions' => $functions, 'properties' => ['clusterable'], 'describe_json' => DescribeJson('date', DescribeJsonStatic(['format' => 'YYYY/MM/DD'])), 'extras' => ['size.bytes' => 4]); } // end of function // Define + and - operators // DATE + INT = DATE declareOperator('+', ['base::DATE', 'base::INT'], function ($args, $targs = []) { $dateType = lookupType('base::DATE'); $intType = lookupType('base::INT'); return array('kind' => 'OPERATOR', 'input' => [$dateType, $intType], 'result' => $dateType, 'deterministic' => true); }); // DATE - INT = DATE declareOperator('-', ['base::DATE', 'base::INT'], function ($args, $targs = []) { $dateType = lookupType('base::DATE'); $intType = lookupType('base::INT'); return array('kind' => 'OPERATOR', 'input' => [$dateType, $intType], 'result' => $dateType, 'deterministic' => true); }); // DATE - DATE = INT declareOperator('-', ['base::DATE', 'base::DATE'], function ($args, $targs = []) { $dateType = lookupType('base::DATE'); $intType = lookupType('base::INT'); return array('kind' => 'OPERATOR', 'input' => [$dateType, $dateType], 'result' => $intType, 'deterministic' => true); }); ?>