Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Sunday, August 16, 2009

Not Equal Is Not Not And Equal

In Ruby, != is tokenized as tNEQ instead of ! and =. Although the Ruby parser must be able to parse a sequence of ! and = as not-equal operator, it is considered as one token for ease. We cannot write those two symbols separately, but nobody wants to do.

By the way, send suggested that it would be very comprehensible to introduce a new not-equal operator ><. I wrote a patch for it for ruby.

diff --git a/parse.y b/parse.y
index 33660a2..77e6cbe 100644
--- a/parse.y
+++ b/parse.y
@@ -6586,6 +6586,11 @@ parser_yylex(struct parser_params *parser)
         return '<';

       case '>':
+        if ((c = nextc()) == '<' ) {
+            lex_state = EXPR_BEG;
+            return tNEQ;
+        }
+        pushback(c);
         switch (lex_state) {
           case EXPR_FNAME: case EXPR_DOT:
             lex_state = EXPR_ARG; break;

As a result, now we can run such a code.

codeshot

No comments:

Post a Comment

Followers