Fossil SCM

Add support for built-in variables in subscript.

drh 2007-11-03 04:39 trunk
Commit a67fbd784d355e407c0025f20084a3d39ae4e1b0
1 file changed +40
--- src/subscript.c
+++ src/subscript.c
@@ -615,10 +615,24 @@
615615
{ "puts", 4, putsCmd, 0 },
616616
{ "set", 3, setCmd, 0 },
617617
{ "sub", 3, bopCmd, (void*)SBSOP_SUB },
618618
};
619619
620
+/*
621
+** A table of built-in string and integer values
622
+*/
623
+static const struct {
624
+ const char *zVar;
625
+ int nVar;
626
+ int *pI;
627
+ char *z;
628
+} aVars[] = {
629
+ { "okAdmin", 7, &g.okAdmin, 0 },
630
+ { "okSetup", 7, &g.okSetup, 0 },
631
+};
632
+
633
+
620634
621635
/*
622636
** Create a new subscript interpreter
623637
*/
624638
struct Subscript *SbS_Create(void){
@@ -667,12 +681,15 @@
667681
case SBSTT_STRING: {
668682
rc = SbS_Push(p, (char*)&zScript[1], n-2, 0);
669683
break;
670684
}
671685
case SBSTT_VERB: {
686
+ /* First look up the verb in the hash table */
672687
const SbSValue *pVal = sbs_fetch(&p->symTab, (char*)zScript, n);
673688
if( pVal==0 ){
689
+ /* If the verb is not in the hash table, look for a
690
+ ** built-in command */
674691
int upr = sizeof(aBuiltin)/sizeof(aBuiltin[0]) - 1;
675692
int lwr = 0;
676693
rc = SBS_ERROR;
677694
while( upr>=lwr ){
678695
int i = (upr+lwr)/2;
@@ -683,10 +700,33 @@
683700
}else if( c<0 ){
684701
upr = i-1;
685702
}else{
686703
lwr = i+1;
687704
}
705
+ }
706
+ if( upr<lwr ){
707
+ /* If it is not a built-in command, look for a built-in
708
+ ** variable */
709
+ upr = sizeof(aVars)/sizeof(aVars[0]) - 1;
710
+ lwr = 0;
711
+ while( upr>=lwr ){
712
+ int i = (upr+lwr)/2;
713
+ int c = strncmp(zScript, aVars[i].zVar, n);
714
+ if( c==0 ){
715
+ if( aVars[i].pI ){
716
+ SbS_PushInt(p, *aVars[i].pI);
717
+ }else{
718
+ SbS_Push(p, aVars[i].z, -1, 0);
719
+ }
720
+ rc = SBS_OK;
721
+ break;
722
+ }else if( c<0 ){
723
+ upr = i-1;
724
+ }else{
725
+ lwr = i+1;
726
+ }
727
+ }
688728
}
689729
}else if( pVal->flags & SBSVAL_VERB ){
690730
rc = pVal->u.verb.xVerb(p, pVal->u.verb.pArg);
691731
}else if( pVal->flags & SBSVAL_EXEC ){
692732
rc = SbS_Eval(p, pVal->u.str.z, pVal->u.str.size);
693733
--- src/subscript.c
+++ src/subscript.c
@@ -615,10 +615,24 @@
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
623 */
624 struct Subscript *SbS_Create(void){
@@ -667,12 +681,15 @@
667 case SBSTT_STRING: {
668 rc = SbS_Push(p, (char*)&zScript[1], n-2, 0);
669 break;
670 }
671 case SBSTT_VERB: {
 
672 const SbSValue *pVal = sbs_fetch(&p->symTab, (char*)zScript, n);
673 if( pVal==0 ){
 
 
674 int upr = sizeof(aBuiltin)/sizeof(aBuiltin[0]) - 1;
675 int lwr = 0;
676 rc = SBS_ERROR;
677 while( upr>=lwr ){
678 int i = (upr+lwr)/2;
@@ -683,10 +700,33 @@
683 }else if( c<0 ){
684 upr = i-1;
685 }else{
686 lwr = i+1;
687 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688 }
689 }else if( pVal->flags & SBSVAL_VERB ){
690 rc = pVal->u.verb.xVerb(p, pVal->u.verb.pArg);
691 }else if( pVal->flags & SBSVAL_EXEC ){
692 rc = SbS_Eval(p, pVal->u.str.z, pVal->u.str.size);
693
--- src/subscript.c
+++ src/subscript.c
@@ -615,10 +615,24 @@
615 { "puts", 4, putsCmd, 0 },
616 { "set", 3, setCmd, 0 },
617 { "sub", 3, bopCmd, (void*)SBSOP_SUB },
618 };
619
620 /*
621 ** A table of built-in string and integer values
622 */
623 static const struct {
624 const char *zVar;
625 int nVar;
626 int *pI;
627 char *z;
628 } aVars[] = {
629 { "okAdmin", 7, &g.okAdmin, 0 },
630 { "okSetup", 7, &g.okSetup, 0 },
631 };
632
633
634
635 /*
636 ** Create a new subscript interpreter
637 */
638 struct Subscript *SbS_Create(void){
@@ -667,12 +681,15 @@
681 case SBSTT_STRING: {
682 rc = SbS_Push(p, (char*)&zScript[1], n-2, 0);
683 break;
684 }
685 case SBSTT_VERB: {
686 /* First look up the verb in the hash table */
687 const SbSValue *pVal = sbs_fetch(&p->symTab, (char*)zScript, n);
688 if( pVal==0 ){
689 /* If the verb is not in the hash table, look for a
690 ** built-in command */
691 int upr = sizeof(aBuiltin)/sizeof(aBuiltin[0]) - 1;
692 int lwr = 0;
693 rc = SBS_ERROR;
694 while( upr>=lwr ){
695 int i = (upr+lwr)/2;
@@ -683,10 +700,33 @@
700 }else if( c<0 ){
701 upr = i-1;
702 }else{
703 lwr = i+1;
704 }
705 }
706 if( upr<lwr ){
707 /* If it is not a built-in command, look for a built-in
708 ** variable */
709 upr = sizeof(aVars)/sizeof(aVars[0]) - 1;
710 lwr = 0;
711 while( upr>=lwr ){
712 int i = (upr+lwr)/2;
713 int c = strncmp(zScript, aVars[i].zVar, n);
714 if( c==0 ){
715 if( aVars[i].pI ){
716 SbS_PushInt(p, *aVars[i].pI);
717 }else{
718 SbS_Push(p, aVars[i].z, -1, 0);
719 }
720 rc = SBS_OK;
721 break;
722 }else if( c<0 ){
723 upr = i-1;
724 }else{
725 lwr = i+1;
726 }
727 }
728 }
729 }else if( pVal->flags & SBSVAL_VERB ){
730 rc = pVal->u.verb.xVerb(p, pVal->u.verb.pArg);
731 }else if( pVal->flags & SBSVAL_EXEC ){
732 rc = SbS_Eval(p, pVal->u.str.z, pVal->u.str.size);
733

Keyboard Shortcuts

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