1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/*
** The implementation of the TH core. This file contains the parser, and
** the implementation of the interface in th.h.
*/
#include "config.h"
#include "th.h"
#include <string.h>
#include <assert.h>
typedef struct Th_Command Th_Command;
typedef struct Th_Frame Th_Frame;
typedef struct Th_Variable Th_Variable;
/*
** Interpreter structure.
*/
struct Th_Interp {
Th_Vtab *pVtab; /* Copy of the argument passed to Th_CreateInterp() */
char *zResult; /* Current interpreter result (Th_Malloc()ed) */
|
|
|
|
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/*
** The implementation of the TH core. This file contains the parser, and
** the implementation of the interface in th.h.
*/
#include "config.h"
#include "th.h"
#include <string.h>
#include <assert.h>
typedef struct Th_Command Th_Command;
typedef struct Th_Frame Th_Frame;
typedef struct Th_Variable Th_Variable;
typedef struct Th_InterpAndList Th_InterpAndList;
/*
** Interpreter structure.
*/
struct Th_Interp {
Th_Vtab *pVtab; /* Copy of the argument passed to Th_CreateInterp() */
char *zResult; /* Current interpreter result (Th_Malloc()ed) */
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
*/
struct Th_Variable {
int nRef; /* Number of references to this structure */
int nData; /* Number of bytes at Th_Variable.zData */
char *zData; /* Data for scalar variables */
Th_Hash *pHash; /* Data for array variables */
};
/*
** Hash table API:
*/
#define TH_HASHSIZE 257
struct Th_Hash {
Th_HashEntry *a[TH_HASHSIZE];
|
>
>
>
>
>
>
>
>
>
>
>
|
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
*/
struct Th_Variable {
int nRef; /* Number of references to this structure */
int nData; /* Number of bytes at Th_Variable.zData */
char *zData; /* Data for scalar variables */
Th_Hash *pHash; /* Data for array variables */
};
/*
** This structure is used to pass complete context information to the
** hash iteration callback functions that need a Th_Interp and a list
** to operate on, e.g. thListAppend().
*/
struct Th_InterpAndList {
Th_Interp *interp; /* Associated interpreter context */
char **pzList; /* IN/OUT: Ptr to ptr to list */
int *pnList; /* IN/OUT: Current length of *pzList */
};
/*
** Hash table API:
*/
#define TH_HASHSIZE 257
struct Th_Hash {
Th_HashEntry *a[TH_HASHSIZE];
|
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
if( pCommand->xDel ){
pCommand->xDel((Th_Interp *)pContext, pCommand->pContext);
}
Th_Free((Th_Interp *)pContext, pEntry->pData);
pEntry->pData = 0;
return 1;
}
/*
** Push a new frame onto the stack.
*/
static int thPushFrame(Th_Interp *interp, Th_Frame *pFrame){
pFrame->paVar = Th_HashNew(interp);
pFrame->pCaller = interp->pFrame;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
if( pCommand->xDel ){
pCommand->xDel((Th_Interp *)pContext, pCommand->pContext);
}
Th_Free((Th_Interp *)pContext, pEntry->pData);
pEntry->pData = 0;
return 1;
}
/*
** Argument pEntry points to an entry in a hash table. The key is
** the list element to be added.
**
** Argument pContext is a pointer to the Th_InterpAndList structure.
**
** Always returns non-zero.
*/
static int thListAppend(Th_HashEntry *pEntry, void *pContext){
Th_InterpAndList *pInterpAndList = (Th_InterpAndList *)pContext;
Th_ListAppend(pInterpAndList->interp, pInterpAndList->pzList,
pInterpAndList->pnList, pEntry->zKey, pEntry->nKey);
return 1;
}
/*
** Push a new frame onto the stack.
*/
static int thPushFrame(Th_Interp *interp, Th_Frame *pFrame){
pFrame->paVar = Th_HashNew(interp);
pFrame->pCaller = interp->pFrame;
|
2830
2831
2832
2833
2834
2835
2836
|
*z++ = zExp[i];
}
}
*z = '\0';
return Th_SetResult(interp, zBuf, -1);
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
|
*z++ = zExp[i];
}
}
*z = '\0';
return Th_SetResult(interp, zBuf, -1);
}
/*
** Appends all currently registered command names to the specified list
** and returns TH_OK upon success. Any other return value indicates an
** error.
*/
int Th_ListAppendCommands(Th_Interp *interp, char **pzList, int *pnList){
Th_InterpAndList *p = (Th_InterpAndList *)Th_Malloc(
interp, sizeof(Th_InterpAndList)
);
p->interp = interp;
p->pzList = pzList;
p->pnList = pnList;
Th_HashIterate(interp, interp->paCmd, thListAppend, p);
Th_Free(interp, p);
return TH_OK;
}
/*
** Appends all variable names for the current frame to the specified list
** and returns TH_OK upon success. Any other return value indicates an
** error. If the current frame cannot be obtained, TH_ERROR is returned.
*/
int Th_ListAppendVariables(Th_Interp *interp, char **pzList, int *pnList){
Th_Frame *pFrame = getFrame(interp, 0);
if( pFrame ){
Th_InterpAndList *p = (Th_InterpAndList *)Th_Malloc(
interp, sizeof(Th_InterpAndList)
);
p->interp = interp;
p->pzList = pzList;
p->pnList = pnList;
Th_HashIterate(interp, pFrame->paVar, thListAppend, p);
Th_Free(interp, p);
return TH_OK;
}else{
return TH_ERROR;
}
}
|