implementation of Range Operator Properly in PHP
How to Implement the Range Operator in PHP?
PHP/Opensource

How to Implement the Range Operator in PHP?

There are several innumerable posts available on internet about PHP. Here, in this blog post we will see how Range operator can be re implemented in PHP?

Implementing Range Operator in PHP:

  • Updating the lexer: This will make us aware about new operator syntax so that it can be transformed into token.
  • Updating Parser: This will show the use of range operator along with its precedence and association with other operators.
  • Updating compilation stage: Here, we can get the abstract syntax tree traversed.
  • Updating Zend VM: Here, we can handle interpretation of new opcode for operator while executing the script.

Range Operator in PHP

Range Operator:

This is added to PHP and is denoted by (/>). It is defined in following parameters:

  • It will have only one incrementation step.
  • Both operands should be either integers or floats.
  • Min= max then one element array that consists of min is returned.

In case any of the following is not satisfied then you may receive an error exception. It occurs if

  • One of the operand is neither an integer nor a float,
  • Min >max,
  • Range max-min is too large.

Updating Lexer:

Here, the new token must be registered in the lexer so that while tokenizing the source code, it can be transformed into T_RANGE token. The Zend Language scanner must be updated by adding the following code:

ST_IN_SCRIPTING"|>" {
RETURN_TOKEN(T_RANGE);

Lexer is in the state of ST_IN_SCRIPTING mode. This means it will only match the |> character sequence when it is available in normal scripting mode. The code between curly braces is C code which will get executed when this operator is found in source code.

Here, it simply returns T_RANGE token. This must be declared in Zend Language Parser y file.

Updating Parser:

The parser must be updated so that it can validate the new T_RANGE token that is used in PHP scripts. It is responsible for the precedence and association of new operator as well as generating the abstract syntax tree node for constructing new code.

This entire process is carried in Zend language parser.y grammar file that contains token definitions and production rules will be used to generate the parser form.

Updating the Compilation stage:

For updating the compilation stage; parser outputs an AST which then recursively is traversed where functions are used to execute every node in the AST. These functions provide opcodes which will later be executed by Zend VM at the time of interpretation.

This is done in Zend compilation file. Here, we need to add our new AST node name in the large switch statement in zend_compile_expr  function and then Zend compile range is to be defined in the same file.

Updating ZEND VM:

In order to handle the execution of new opcode, ZEND VM is to be updated. It involves updating Zend file with the help of following code:

ZEND_VM_HANDLER(182, ZEND_RANGE, CONST|TMP|VAR|CV, CONST|TMP|VAR|CV)
{
USE_OPLINE
zend_free_op free_op1, free_op2;
zval *op1, *op2, *result, tmp;
SAVE_OPLINE();
op1 = GET_OP1_ZVAL_PTR_DEREF(BP_VAR_R);
op2 = GET_OP2_ZVAL_PTR_DEREF(BP_VAR_R);
result = EX_VAR(opline->result.var);
// if both operands are integers
if (Z_TYPE_P(op1) == IS_LONG && Z_TYPE_P(op2) == IS_LONG) {
// for when min and max are integers
} else if ( // if both operands are either integers or doubles
(Z_TYPE_P(op1) == IS_LONG || Z_TYPE_P(op1) == IS_DOUBLE)
&& (Z_TYPE_P(op2) == IS_LONG || Z_TYPE_P(op2) == IS_DOUBLE)
) {
// for when min and max are either integers or floats
} else {
// for when min and max are neither integers nor floats
}
FREE_OP1();
FREE_OP2();
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

Take Away:

Hope this blog post will help you to implement range operator properly. Let us know how we have helped you. For more such posts, stay tuned to Softqube Technologies, provider of cost effective PHP development services.

Hari Patel

Hari Patel

I am the Managing Director of Softqube Technologies Pvt. Ltd., a modern-day digital transformation, design and development service provider. We provide services to businesses of all verticals across the globe. I believe and live by a mission that I help more entrepreneurs to build, launch and grow profitable businesses.