Fossil SCM
Add support for built-in variables in subscript.
Commit
a67fbd784d355e407c0025f20084a3d39ae4e1b0
Parent
92f6081d11c4361…
1 file changed
+40
+40
| --- src/subscript.c | ||
| +++ src/subscript.c | ||
| @@ -615,10 +615,24 @@ | ||
| 615 | 615 | { "puts", 4, putsCmd, 0 }, |
| 616 | 616 | { "set", 3, setCmd, 0 }, |
| 617 | 617 | { "sub", 3, bopCmd, (void*)SBSOP_SUB }, |
| 618 | 618 | }; |
| 619 | 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 | + | |
| 620 | 634 | |
| 621 | 635 | /* |
| 622 | 636 | ** Create a new subscript interpreter |
| 623 | 637 | */ |
| 624 | 638 | struct Subscript *SbS_Create(void){ |
| @@ -667,12 +681,15 @@ | ||
| 667 | 681 | case SBSTT_STRING: { |
| 668 | 682 | rc = SbS_Push(p, (char*)&zScript[1], n-2, 0); |
| 669 | 683 | break; |
| 670 | 684 | } |
| 671 | 685 | case SBSTT_VERB: { |
| 686 | + /* First look up the verb in the hash table */ | |
| 672 | 687 | const SbSValue *pVal = sbs_fetch(&p->symTab, (char*)zScript, n); |
| 673 | 688 | if( pVal==0 ){ |
| 689 | + /* If the verb is not in the hash table, look for a | |
| 690 | + ** built-in command */ | |
| 674 | 691 | int upr = sizeof(aBuiltin)/sizeof(aBuiltin[0]) - 1; |
| 675 | 692 | int lwr = 0; |
| 676 | 693 | rc = SBS_ERROR; |
| 677 | 694 | while( upr>=lwr ){ |
| 678 | 695 | int i = (upr+lwr)/2; |
| @@ -683,10 +700,33 @@ | ||
| 683 | 700 | }else if( c<0 ){ |
| 684 | 701 | upr = i-1; |
| 685 | 702 | }else{ |
| 686 | 703 | lwr = i+1; |
| 687 | 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 | + } | |
| 688 | 728 | } |
| 689 | 729 | }else if( pVal->flags & SBSVAL_VERB ){ |
| 690 | 730 | rc = pVal->u.verb.xVerb(p, pVal->u.verb.pArg); |
| 691 | 731 | }else if( pVal->flags & SBSVAL_EXEC ){ |
| 692 | 732 | rc = SbS_Eval(p, pVal->u.str.z, pVal->u.str.size); |
| 693 | 733 |
| --- 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 |