UnQL

Check-in [d06f20c02a]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the bitwise expression operators (&, |, <<, >> and ~). Also fix the unary minus operator.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: d06f20c02a9f5f1d2d452c2959b4cd9125b828cc
User & Date: dan 2011-07-15 18:14:32
Context
2011-07-15
18:22
Keywords "true", "false", and "null" are now lower-case. ("NULL" can also optionally be all upper-case.) Use && and || instead of AND and OR. check-in: 340cf0441f user: drh tags: trunk
18:14
Add the bitwise expression operators (&, |, <<, >> and ~). Also fix the unary minus operator. check-in: d06f20c02a user: dan tags: trunk
16:11
Add support for logical expression operators AND, OR and NOT. check-in: b4ea7f84c7 user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/expr.c.

361
362
363
364
365
366
367




368
369
370
371





372



373














374
375
376
377
378
379
380
381
382
383
384
385
386
387





























































388
389
390
391
392
393
394
        pRes->u.r = rLeft+rRight;
        pRes->eJType = XJD1_REAL;
      }
      xjd1JsonFree(pJLeft);
      xjd1JsonFree(pJRight);
      break;
    }




    case TK_MINUS: {
      pJLeft = xjd1ExprEval(p->u.bi.pLeft);
      pJRight = xjd1ExprEval(p->u.bi.pRight);
      xjd1JsonToReal(pJLeft, &rLeft);





      xjd1JsonToReal(pJRight, &rRight);



      pRes->u.r = rLeft-rRight;














      pRes->eJType = XJD1_REAL;
      xjd1JsonFree(pJLeft);
      xjd1JsonFree(pJRight);
      break;
    }

    case TK_NOT: {
      if( xjd1ExprTrue(p->u.bi.pLeft) ){
        pRes->eJType = XJD1_FALSE;
      }else{
        pRes->eJType = XJD1_TRUE;
      }
      break;
    }






























































    default: {
      pRes->eJType = XJD1_NULL;
      break;
    }
  }
  return pRes;







>
>
>
>


<

>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>


<











>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







361
362
363
364
365
366
367
368
369
370
371
372
373

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
        pRes->u.r = rLeft+rRight;
        pRes->eJType = XJD1_REAL;
      }
      xjd1JsonFree(pJLeft);
      xjd1JsonFree(pJRight);
      break;
    }


    case TK_STAR:
    case TK_SLASH:
    case TK_MINUS: {
      pJLeft = xjd1ExprEval(p->u.bi.pLeft);

      xjd1JsonToReal(pJLeft, &rLeft);
      if( p->u.bi.pRight==0 ){
        assert( p->eType==TK_MINUS );
        pRes->u.r = -1.0 * rLeft;
      }else{
        pJRight = xjd1ExprEval(p->u.bi.pRight);
        xjd1JsonToReal(pJRight, &rRight);

        switch( p->eType ){
          case TK_MINUS: 
            pRes->u.r = rLeft-rRight; 
            break;

          case TK_SLASH: 
            if( rRight!=0.0 ){
              pRes->u.r = rLeft / rRight; 
            }
            break;

          case TK_STAR: 
            pRes->u.r = rLeft * rRight; 
            break;
        }
        xjd1JsonFree(pJRight);
      }
      pRes->eJType = XJD1_REAL;
      xjd1JsonFree(pJLeft);

      break;
    }

    case TK_NOT: {
      if( xjd1ExprTrue(p->u.bi.pLeft) ){
        pRes->eJType = XJD1_FALSE;
      }else{
        pRes->eJType = XJD1_TRUE;
      }
      break;
    }

    /* Bitwise operators: &, |, <<, >> and ~.
    **
    ** These follow the javascript conventions. Arguments are converted to
    ** 64-bit real numbers, and then to 32-bit signed integers. The bitwise
    ** operation is performed and the result converted back to a 64-bit
    ** real number.
    **
    ** TBD: When XJD1 is enhance to feature an arbitrary precision integer
    ** type, these will have to change somehow.
    **
    ** This block also contains the implementation of the modulo operator.
    ** As it requires the same 32-bit integer conversions as the bitwise
    ** operators.
    */
    case TK_REM:
    case TK_RSHIFT:
    case TK_LSHIFT:
    case TK_BITAND:
    case TK_BITOR: {
      int iLeft;
      int iRight;

      pJLeft = xjd1ExprEval(p->u.bi.pLeft);
      pJRight = xjd1ExprEval(p->u.bi.pRight);
      xjd1JsonToReal(pJLeft, &rLeft);
      xjd1JsonToReal(pJRight, &rRight);
      iLeft = rLeft;
      iRight = rRight;

      pRes->eJType = XJD1_REAL;
      switch( p->eType ){
        case TK_RSHIFT: 
          if( iRight>=32 ){
            pRes->u.r = (double)(iLeft<0 ? -1 : 0);
          }else{
            pRes->u.r = (double)(iLeft >> iRight);
          }
          break;

        case TK_LSHIFT:
          if( iRight>=32 ){
            pRes->u.r = 0;
          }else{
            pRes->u.r = (double)(iLeft << iRight);
          }
          break;

        case TK_BITAND: pRes->u.r = (double)(iLeft & iRight); break;
        case TK_BITOR:  pRes->u.r = (double)(iLeft | iRight); break;
        case TK_REM:    pRes->u.r = (double)(iLeft % iRight); break;
      }
      break;
    }

    case TK_BITNOT:
      pJLeft = xjd1ExprEval(p->u.bi.pLeft);
      xjd1JsonToReal(pJLeft, &rLeft);
      pRes->eJType = XJD1_REAL;
      pRes->u.r = (double)(~((int)rLeft));
      break;

    default: {
      pRes->eJType = XJD1_NULL;
      break;
    }
  }
  return pRes;

Changes to test/base02.test.

45
46
47
48
49
50
51























































.testcase 5
SELECT NOT TRUE  FROM c1;
SELECT NOT FALSE FROM c1;
SELECT NOT "cat" FROM c1;
SELECT NOT "" FROM c1;
.result false true false true































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
.testcase 5
SELECT NOT TRUE  FROM c1;
SELECT NOT FALSE FROM c1;
SELECT NOT "cat" FROM c1;
SELECT NOT "" FROM c1;
.result false true false true

.testcase 6
SELECT 1 & 2               FROM c1;
SELECT "5" & 4             FROM c1;
.result 0 4

.testcase 7
SELECT 1 | 2               FROM c1;
SELECT "5" | 4             FROM c1;
.result 3 5

.testcase 8
SELECT  1  << 2            FROM c1;
SELECT "5" << 4            FROM c1;
.result 4 80

.testcase 9
SELECT  1  >> 2            FROM c1;
SELECT "5" >> 4            FROM c1;
.result 0 0

.testcase 10
SELECT  ~1                 FROM c1;
SELECT  ~"5"               FROM c1;
.result -2 -6

.testcase 11
SELECT 1 * 7 FROM c1;
SELECT 45 * 4 FROM c1;
SELECT 67 * 2 FROM c1;
.result 7 180 134

.testcase 12
SELECT 1 % 7 FROM c1;
SELECT 45 % 4 FROM c1;
SELECT 67 % 2 FROM c1;
.result 1 1 1

.testcase 13
SELECT 1 / 7 FROM c1;
SELECT 45 / 4 FROM c1;
SELECT 67 / 2 FROM c1;
.result 0.1428571428571428 11.25 33.5

.testcase 14
SELECT 1 + 7 FROM c1;
SELECT 45 + 4 FROM c1;
SELECT 67 + 2 FROM c1;
.result 8 49 69

.testcase 15
SELECT 1 - 7 FROM c1;
SELECT 45 - 4 FROM c1;
SELECT 67 - 2 FROM c1;
.result -6 41 65