Skip to content

Commit

Permalink
More optimizations to the whole thing.
Browse files Browse the repository at this point in the history
- Optimizations:
  - More calls to PHP functions made with explicit global namespace.
  - Optimized parsing and node preprocessing.
    - Stuff that can be made during AST preprocessing (node reducing)
      is now done at that point (eg. Common::ensureIndexed()) and thus
      is done only once - when parsing Primi source code.
  - Fetching undefined variable from context no longer throws exceptions
    but returns ordinary PHP null instead.
  - Removed (reduced) some indirections when accessing stuff.
  - NumberValue uses ctype_digit() for detecting numeric integers.
  - More strict_types!.
- array_push() no longer returns pushed item back (seems unnecessary).
  • Loading branch information
smuuf committed Feb 25, 2020
1 parent 5340aa8 commit d9d53d7
Show file tree
Hide file tree
Showing 37 changed files with 1,121 additions and 980 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"require": {
"php": ">=7.1",
"smuuf/php-peg": "^2"
"smuuf/php-peg": "=2.1.2"
},
"require-dev": {
"smuuf/koloader": "dev-master",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions primi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Composer's autoload.
require __DIR__ . "/vendor/autoload.php";

error_reporting(E_ALL);
set_error_handler(function($severity, $message, $file, $line) {
throw new \ErrorException($message, 0, $severity, $file, $line);
}, E_ALL);
Expand Down
6 changes: 4 additions & 2 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function setVariables(array $pairs, bool $global = false) {

}

public function getVariable(string $name): Value {
public function getVariable(string $name): ?Value {

// Variables of current context instance have higher priority than
// global variables.
Expand All @@ -70,7 +70,9 @@ public function getVariable(string $name): Value {
return self::$globals[$name];
}

throw new InternalUndefinedVariableException($name);
// This should be slightly faster than throwsin exceptions for undefined
// variables.
return null;

}

Expand Down
12 changes: 6 additions & 6 deletions src/exceptions/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

class ErrorException extends InternalException {

public function __construct($msg, $line = false, $pos = false) {
public function __construct($msg, $line = \false, $pos = \false) {

// Second argument might be a node from AST tree, so extract position
// from the node.
if (is_array($line)) {
$pos = $line['pos'] ?? false;
$line = $line['line'] ?? false;
if (\is_array($line)) {
$pos = $line['_p'] ?? \false;
$line = $line['_l'] ?? \false;
}

if ($line !== false && $pos !== false) {
$msg = sprintf('%s @ line %s, position %s', $msg, $line, $pos);
if ($line !== \false && $pos !== \false) {
$msg = \sprintf('%s @ line %s, position %s', $msg, $line, $pos);
}

parent::__construct($msg);
Expand Down
7 changes: 0 additions & 7 deletions src/exceptions/InternalUndefinedVariableException.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/extensions/ExtensionHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ExtensionHub extends \Smuuf\Primi\StrictObject {
public static function add($extension) {

// We allow registering extensions in bulk.
if (is_array($extension)) {
if (\is_array($extension)) {
foreach ($extension as $ext) {
self::add($ext);
}
Expand All @@ -28,7 +28,7 @@ public static function add($extension) {
}

$processed = self::process($extension);
self::$extensions = array_replace(self::$extensions, $processed);
self::$extensions = \array_replace(self::$extensions, $processed);

}

Expand Down Expand Up @@ -63,15 +63,15 @@ protected static function process(string $class): array {
// "... all methods with any of the given attributes will be returned."
$public = $classRef->getMethods(\ReflectionMethod::IS_PUBLIC);
$static = $classRef->getMethods(\ReflectionMethod::IS_STATIC);
$methods = array_intersect($static, $public);
$methods = \array_intersect($static, $public);

$result = [];
foreach ($methods as $methodRef) {

$methodName = $methodRef->getName();

// Skip magic methods.
if (strpos($methodName, '__') === 0) {
if (\strpos($methodName, '__') === 0) {
continue;
}

Expand Down
17 changes: 9 additions & 8 deletions src/extensions/psl/ArrayExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smuuf\Primi\Psl;

use \Smuuf\Primi\Extension;
Expand All @@ -21,20 +23,19 @@ public static function array_copy(ArrayValue $arr): ArrayValue {
public static function array_length(ArrayValue $arr): NumberValue {
return new NumberValue((string) count($arr->value));
}

public static function array_reverse(ArrayValue $arr): Value {
return new ArrayValue(array_reverse($arr->value));
return new ArrayValue(\array_reverse($arr->value));
}

public static function array_random(ArrayValue $arr): Value {
return $arr->value[array_rand($arr->value)];
return $arr->value[\array_rand($arr->value)];
}

public static function array_shuffle(ArrayValue $arr): ArrayValue {

// Do NOT modify the original array argument (as PHP would do).
$copy = clone $arr;
shuffle($copy->value);
\shuffle($copy->value);

return $copy;

Expand Down Expand Up @@ -71,7 +72,7 @@ public static function array_has(ArrayValue $arr, Value $key): BoolValue {

}

public static function array_get(ArrayValue $arr, Value $key, Value $default = null): Value {
public static function array_get(ArrayValue $arr, Value $key, Value $default = \null): Value {

// Allow only some value types.
Common::allowTypes($key, StringValue::class, NumberValue::class);
Expand All @@ -86,7 +87,7 @@ public static function array_number_of(ArrayValue $arr, Value $needle): NumberVa

// We must convert Primi values back to PHP values for the
// array_count_values function to work.
$phpValues = array_map(function($item) {
$phpValues = \array_map(function($item) {
return $item->value;
}, $arr->value);

Expand All @@ -97,9 +98,9 @@ public static function array_number_of(ArrayValue $arr, Value $needle): NumberVa

}

public static function array_push(ArrayValue $arr, Value $value): Value {
public static function array_push(ArrayValue $arr, Value $value): NullValue {
$arr->value[] = $value;
return $value;
return new NullValue;
}

public static function array_pop(ArrayValue $arr): Value {
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/psl/RegexExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smuuf\Primi\Psl;

use \Smuuf\Primi\Structures\StringValue;
Expand All @@ -19,9 +21,7 @@ public static function regex_match(
StringValue $haystack
): Value {

$match = \preg_match($regex->value, $haystack->value, $matches);

if (!$match) {
if (!\preg_match($regex->value, $haystack->value, $matches)) {
return new BoolValue(false);
}

Expand Down
Loading

0 comments on commit d9d53d7

Please sign in to comment.