Fossil SCM

Add basic math operators to subscript.

drh 2007-11-03 04:23 trunk
Commit 92f6081d11c4361b32dcac2346a5b72a45867025
1 file changed +42 -29
+42 -29
--- src/subscript.c
+++ src/subscript.c
@@ -547,33 +547,39 @@
547547
SbS_Pop(p, 1);
548548
SbS_PushInt(p, !n);
549549
return 0;
550550
}
551551
552
-/*
553
-** Subscript command: INTEGER INTEGER max INTEGER
554
-*/
555
-static int maxCmd(struct Subscript *p, void *pNotUsed){
556
- int a, b;
557
- if( SbS_RequireStack(p, 2, "max") ) return 1;
558
- a = SbS_StackValueInt(p, 0);
559
- b = SbS_StackValueInt(p, 1);
560
- SbS_Pop(p, 2);
561
- SbS_PushInt(p, a>b ? a : b);
562
- return 0;
563
-}
564
-
565
-/*
566
-** Subscript command: INTEGER INTEGER and INTEGER
567
-*/
568
-static int andCmd(struct Subscript *p, void *pNotUsed){
569
- int a, b;
570
- if( SbS_RequireStack(p, 2, "max") ) return 1;
571
- a = SbS_StackValueInt(p, 0);
572
- b = SbS_StackValueInt(p, 1);
573
- SbS_Pop(p, 2);
574
- SbS_PushInt(p, a && b);
552
+#define SBSOP_ADD 1
553
+#define SBSOP_SUB 2
554
+#define SBSOP_MUL 3
555
+#define SBSOP_DIV 4
556
+#define SBSOP_AND 5
557
+#define SBSOP_OR 6
558
+#define SBSOP_MIN 7
559
+#define SBSOP_MAX 8
560
+
561
+/*
562
+** Subscript command: INTEGER INTEGER <binary-op> INTEGER
563
+*/
564
+static int bopCmd(struct Subscript *p, void *pOp){
565
+ int a, b, c;
566
+ if( SbS_RequireStack(p, 2, "BINARY-OP") ) return 1;
567
+ a = SbS_StackValueInt(p, 0);
568
+ b = SbS_StackValueInt(p, 1);
569
+ switch( (int)pOp ){
570
+ case SBSOP_ADD: c = a+b; break;
571
+ case SBSOP_SUB: c = a-b; break;
572
+ case SBSOP_MUL: c = a*b; break;
573
+ case SBSOP_DIV: c = b!=0 ? a/b : 0; break;
574
+ case SBSOP_AND: c = a && b; break;
575
+ case SBSOP_OR: c = a || b; break;
576
+ case SBSOP_MIN: c = a<b ? a : b; break;
577
+ case SBSOP_MAX: c = a<b ? b : a; break;
578
+ }
579
+ SbS_Pop(p, 2);
580
+ SbS_PushInt(p, c);
575581
return 0;
576582
}
577583
578584
/*
579585
** Subscript command: STRING puts
@@ -594,16 +600,23 @@
594600
*/
595601
static const struct {
596602
const char *zCmd;
597603
int nCmd;
598604
int (*xCmd)(Subscript*,void*);
605
+ void *pArg;
599606
} aBuiltin[] = {
600
- { "and", 3, andCmd },
601
- { "max", 3, maxCmd },
602
- { "not", 3, notCmd },
603
- { "puts", 4, putsCmd },
604
- { "set", 3, setCmd },
607
+ { "add", 3, bopCmd, (void*)SBSOP_AND },
608
+ { "and", 3, bopCmd, (void*)SBSOP_AND },
609
+ { "div", 3, bopCmd, (void*)SBSOP_DIV },
610
+ { "max", 3, bopCmd, (void*)SBSOP_MAX },
611
+ { "min", 3, bopCmd, (void*)SBSOP_MIN },
612
+ { "mul", 3, bopCmd, (void*)SBSOP_MUL },
613
+ { "not", 3, notCmd, 0 },
614
+ { "or", 2, bopCmd, (void*)SBSOP_OR },
615
+ { "puts", 4, putsCmd, 0 },
616
+ { "set", 3, setCmd, 0 },
617
+ { "sub", 3, bopCmd, (void*)SBSOP_SUB },
605618
};
606619
607620
608621
/*
609622
** Create a new subscript interpreter
@@ -663,11 +676,11 @@
663676
rc = SBS_ERROR;
664677
while( upr>=lwr ){
665678
int i = (upr+lwr)/2;
666679
int c = strncmp(zScript, aBuiltin[i].zCmd, n);
667680
if( c==0 ){
668
- rc = aBuiltin[i].xCmd(p, 0);
681
+ rc = aBuiltin[i].xCmd(p, aBuiltin[i].pArg);
669682
break;
670683
}else if( c<0 ){
671684
upr = i-1;
672685
}else{
673686
lwr = i+1;
674687
--- src/subscript.c
+++ src/subscript.c
@@ -547,33 +547,39 @@
547 SbS_Pop(p, 1);
548 SbS_PushInt(p, !n);
549 return 0;
550 }
551
552 /*
553 ** Subscript command: INTEGER INTEGER max INTEGER
554 */
555 static int maxCmd(struct Subscript *p, void *pNotUsed){
556 int a, b;
557 if( SbS_RequireStack(p, 2, "max") ) return 1;
558 a = SbS_StackValueInt(p, 0);
559 b = SbS_StackValueInt(p, 1);
560 SbS_Pop(p, 2);
561 SbS_PushInt(p, a>b ? a : b);
562 return 0;
563 }
564
565 /*
566 ** Subscript command: INTEGER INTEGER and INTEGER
567 */
568 static int andCmd(struct Subscript *p, void *pNotUsed){
569 int a, b;
570 if( SbS_RequireStack(p, 2, "max") ) return 1;
571 a = SbS_StackValueInt(p, 0);
572 b = SbS_StackValueInt(p, 1);
573 SbS_Pop(p, 2);
574 SbS_PushInt(p, a && b);
 
 
 
 
 
 
575 return 0;
576 }
577
578 /*
579 ** Subscript command: STRING puts
@@ -594,16 +600,23 @@
594 */
595 static const struct {
596 const char *zCmd;
597 int nCmd;
598 int (*xCmd)(Subscript*,void*);
 
599 } aBuiltin[] = {
600 { "and", 3, andCmd },
601 { "max", 3, maxCmd },
602 { "not", 3, notCmd },
603 { "puts", 4, putsCmd },
604 { "set", 3, setCmd },
 
 
 
 
 
 
605 };
606
607
608 /*
609 ** Create a new subscript interpreter
@@ -663,11 +676,11 @@
663 rc = SBS_ERROR;
664 while( upr>=lwr ){
665 int i = (upr+lwr)/2;
666 int c = strncmp(zScript, aBuiltin[i].zCmd, n);
667 if( c==0 ){
668 rc = aBuiltin[i].xCmd(p, 0);
669 break;
670 }else if( c<0 ){
671 upr = i-1;
672 }else{
673 lwr = i+1;
674
--- src/subscript.c
+++ src/subscript.c
@@ -547,33 +547,39 @@
547 SbS_Pop(p, 1);
548 SbS_PushInt(p, !n);
549 return 0;
550 }
551
552 #define SBSOP_ADD 1
553 #define SBSOP_SUB 2
554 #define SBSOP_MUL 3
555 #define SBSOP_DIV 4
556 #define SBSOP_AND 5
557 #define SBSOP_OR 6
558 #define SBSOP_MIN 7
559 #define SBSOP_MAX 8
560
561 /*
562 ** Subscript command: INTEGER INTEGER <binary-op> INTEGER
563 */
564 static int bopCmd(struct Subscript *p, void *pOp){
565 int a, b, c;
566 if( SbS_RequireStack(p, 2, "BINARY-OP") ) return 1;
567 a = SbS_StackValueInt(p, 0);
568 b = SbS_StackValueInt(p, 1);
569 switch( (int)pOp ){
570 case SBSOP_ADD: c = a+b; break;
571 case SBSOP_SUB: c = a-b; break;
572 case SBSOP_MUL: c = a*b; break;
573 case SBSOP_DIV: c = b!=0 ? a/b : 0; break;
574 case SBSOP_AND: c = a && b; break;
575 case SBSOP_OR: c = a || b; break;
576 case SBSOP_MIN: c = a<b ? a : b; break;
577 case SBSOP_MAX: c = a<b ? b : a; break;
578 }
579 SbS_Pop(p, 2);
580 SbS_PushInt(p, c);
581 return 0;
582 }
583
584 /*
585 ** Subscript command: STRING puts
@@ -594,16 +600,23 @@
600 */
601 static const struct {
602 const char *zCmd;
603 int nCmd;
604 int (*xCmd)(Subscript*,void*);
605 void *pArg;
606 } aBuiltin[] = {
607 { "add", 3, bopCmd, (void*)SBSOP_AND },
608 { "and", 3, bopCmd, (void*)SBSOP_AND },
609 { "div", 3, bopCmd, (void*)SBSOP_DIV },
610 { "max", 3, bopCmd, (void*)SBSOP_MAX },
611 { "min", 3, bopCmd, (void*)SBSOP_MIN },
612 { "mul", 3, bopCmd, (void*)SBSOP_MUL },
613 { "not", 3, notCmd, 0 },
614 { "or", 2, bopCmd, (void*)SBSOP_OR },
615 { "puts", 4, putsCmd, 0 },
616 { "set", 3, setCmd, 0 },
617 { "sub", 3, bopCmd, (void*)SBSOP_SUB },
618 };
619
620
621 /*
622 ** Create a new subscript interpreter
@@ -663,11 +676,11 @@
676 rc = SBS_ERROR;
677 while( upr>=lwr ){
678 int i = (upr+lwr)/2;
679 int c = strncmp(zScript, aBuiltin[i].zCmd, n);
680 if( c==0 ){
681 rc = aBuiltin[i].xCmd(p, aBuiltin[i].pArg);
682 break;
683 }else if( c<0 ){
684 upr = i-1;
685 }else{
686 lwr = i+1;
687

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button