new operator

From: Date: Tue, 27 Jul 1999 18:58:09 +0000
Subject: new operator
Groups: php.dev 
Request: Send a blank email to php-dev+get-9242@lists.php.net to get a copy of this message
I've added a ** operator to php to handle powers, ala perl. I know one could use the pow() function, but having it as an operator is much more convenient for people like myself that work with math. Any chance of this getting added? diff -u3 php3/language-parser.y php3-new/language-parser.y --- php3/language-parser.y Fri May 14 06:00:24 1999 +++ php3-new/language-parser.y Tue Jul 27 14:25:47 1999 @@ -210,6 +210,7 @@ %left SHIFT_LEFT SHIFT_RIGHT %left '+' '-' '.' %left '*' '/' '%' +%right POWER %right '!' '~' INCREMENT DECREMENT INT_CAST DOUBLE_CAST STRING_CAST ARRAY_CAST OBJECT_CAST '@' %right '[' %nonassoc NEW @@ -514,6 +515,7 @@ | expr '*' expr { if (GLOBAL(Execute)) mul_function(&$$,&$1,&$3 _INLINE_TLS); } | expr '/' expr { if (GLOBAL(Execute)) div_function(&$$,&$1,&$3 _INLINE_TLS); } | expr '%' expr { if (GLOBAL(Execute)) mod_function(&$$,&$1,&$3 _INLINE_TLS); } + | expr POWER expr { if (GLOBAL(Execute)) pow_function(&$$,&$1,&$3 _INLINE_TLS); } | expr SHIFT_LEFT expr { if (GLOBAL(Execute)) shift_left_function(&$$,&$1,&$3 _INLINE_TLS); } | expr SHIFT_RIGHT expr { if (GLOBAL(Execute)) shift_right_function(&$$,&$1,&$3 _INLINE_TLS); } | '+' expr { if (GLOBAL(Execute)) { pval tmp; tmp.value.lval=0; tmp.type=IS_LONG; add_function(&$$,&tmp,&$2 _INLINE_TLS); } } diff -u3 php3/language-scanner.lex php3-new/language-scanner.lex --- php3/language-scanner.lex Fri Jul 2 12:41:50 1999 +++ php3-new/language-scanner.lex Tue Jul 27 14:24:06 1999 @@ -669,6 +669,10 @@ return DECREMENT; } +<IN_PHP>"**" { + return POWER; +} + <IN_PHP>"==" { return IS_EQUAL; } diff -u3 php3/operators.c php3-new/operators.c --- php3/operators.c Mon Mar 15 11:27:13 1999 +++ php3-new/operators.c Tue Jul 27 14:27:47 1999 @@ -37,6 +37,7 @@ #include "php.h" #include "functions/number.h" #include <stdio.h> +#include <math.h> #if HAVE_STRING_H #include <string.h> #else @@ -480,6 +481,16 @@ result->type = IS_LONG; result->value.lval = op1->value.lval % op2->value.lval; + return SUCCESS; +} + +int pow_function(pval *result, pval *op1, pval *op2 INLINE_TLS) +{ + convert_to_double(op1); + convert_to_double(op2); + + result->type = IS_DOUBLE; + result->value.dval = pow(op1->value.dval, op2->value.dval); return SUCCESS; } diff -u3 php3/operators.h php3-new/operators.h --- php3/operators.h Tue Feb 9 08:28:38 1999 +++ php3-new/operators.h Tue Jul 27 14:26:36 1999 @@ -46,6 +46,7 @@ extern int mul_function(pval *result, pval *op1, pval *op2 INLINE_TLS); extern int div_function(pval *result, pval *op1, pval *op2 INLINE_TLS); extern int mod_function(pval *result, pval *op1, pval *op2 INLINE_TLS); +extern int pow_function(pval *result, pval *op1, pval *op2 INLINE_TLS); extern int boolean_or_function(pval *result, pval *op1, pval *op2); extern int boolean_xor_function(pval *result, pval *op1, pval *op2); extern int boolean_and_function(pval *result, pval *op1, pval *op2);

« previous php.dev (#9242) next »