Refactor matches to be transpilable

This commit is contained in:
Timothy Warren 2021-04-23 14:34:08 -04:00
parent 9c5493100f
commit a5223adfa5
5 changed files with 24 additions and 8 deletions

View File

@ -33,7 +33,7 @@ class Highlight {
*/
public static function fromPHPToken(int $token): int
{
return match($token) {
$token = match($token) {
// Delimiters
T_ARRAY,
T_CURLY_OPEN,
@ -186,6 +186,8 @@ class Highlight {
default => Highlight::NORMAL,
};
return $token;
}
/**
@ -197,7 +199,7 @@ class Highlight {
*/
public static function fromPHPChar(string $char): int
{
return match ($char) {
$hl = match ($char) {
// Delimiter characters
'[', ']', '{', '}', '(', ')', '"', "'" => Highlight::DELIMITER,
@ -207,5 +209,7 @@ class Highlight {
default => Highlight::NORMAL,
};
return $hl;
}
}

View File

@ -26,7 +26,7 @@ class FileType {
{
$ext = strstr(basename($filename), '.');
$ext = ($ext !== FALSE) ? $ext : '';
return match ($ext) {
$syntax = match ($ext) {
'.sh', '.bash' => Syntax::new(
'Shell',
[
@ -127,6 +127,8 @@ class FileType {
),
default => Syntax::default(),
};
return $syntax;
}
private function __construct(public string $name, public Syntax $syntax) {}

View File

@ -76,13 +76,15 @@ class Row {
public function __get(string $name): mixed
{
return match ($name)
$prop = match ($name)
{
'size' => strlen($this->chars),
'rsize' => strlen($this->render),
'chars' => $this->chars,
default => NULL,
};
return $prop;
}
/**

View File

@ -87,7 +87,7 @@ class Terminal {
{
$c = Terminal::read();
return match($c)
$key = match($c)
{
// Unambiguous mappings
RawKeyCode::ARROW_DOWN => KeyType::ARROW_DOWN,
@ -113,6 +113,8 @@ class Terminal {
default => $c,
};
return $key;
}
/**

View File

@ -76,7 +76,7 @@ function is_digit(string $char): bool
*/
function is_space(string $char): bool
{
return match($char) {
$isSpace = match($char) {
RawKeyCode::CARRIAGE_RETURN,
RawKeyCode::FORM_FEED,
RawKeyCode::NEWLINE,
@ -86,6 +86,8 @@ function is_space(string $char): bool
default => false,
};
return $isSpace;
}
// ----------------------------------------------------------------------------
@ -158,7 +160,7 @@ function str_has(string $haystack, string $str, ?int $offset = NULL): bool
*/
function syntax_to_color(int $hl): int
{
return match ($hl)
$color = match ($hl)
{
Highlight::COMMENT => Color::FG_CYAN,
Highlight::ML_COMMENT => Color::FG_BRIGHT_BLACK,
@ -175,6 +177,8 @@ function syntax_to_color(int $hl): int
Highlight::IDENTIFIER => Color::FG_BRIGHT_WHITE,
default => Color::FG_WHITE,
};
return $color;
}
/**
@ -191,7 +195,7 @@ function tabs_to_spaces(string $str, int $number = KILO_TAB_STOP): string
function error_code_name(int $code): string
{
return match ($code) {
$errorName = match ($code) {
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parse Error',
@ -208,6 +212,8 @@ function error_code_name(int $code): string
E_USER_DEPRECATED => 'User Deprecated',
default => 'Unknown',
};
return $errorName;
}
function saturating_add(int $a, int $b, int $max): int