📄 clilib.c
字号:
StringTable_p = (SlotTable_t **)memory_allocate(sttst_InitParams.CPUPartition_p, sizeof(SlotTable_t*)*STRING_TABLE_INIT_SIZE);
if (StringTable_p == NULL)
{
STTBX_Print(("Line table initialization : not enough memory !\n" ));
}
else
{
for (i=0; i<STRING_TABLE_INIT_SIZE; i++)
{
StringTable_p[i] = (SlotTable_t *)memory_allocate(sttst_InitParams.CPUPartition_p, MAX_STTBX_LEN);
if (StringTable_p[i] == NULL)
{
STTBX_Print(("Line table initialization : not enough memory !\n" ));
break;
}
}
}
}
/* ========================================================================
allocate memory for token table
=========================================================================== */
static void InitTokenTable(void)
{
U8 i;
TokenTableIndex = -1;
MaxSimultaneousTokenReached = 0;
TokenTable_p = (SlotTable_t **)memory_allocate(sttst_InitParams.CPUPartition_p, sizeof(SlotTable_t*)*TOKEN_TABLE_INIT_SIZE);
if (TokenTable_p == NULL)
{
STTBX_Print(("Token table initialization : not enough memory !\n" ));
}
else
{
for (i=0; i< TOKEN_TABLE_INIT_SIZE; i++)
{
TokenTable_p[i] = (SlotTable_t *)memory_allocate(sttst_InitParams.CPUPartition_p, CLI_MAX_LINE_LENGTH);
if (TokenTable_p[i] == NULL)
{
STTBX_Print(("Token table initialization : not enough memory !\n" ));
break;
}
}
}
}
/* ========================================================================
allocate memory for parse table
=========================================================================== */
static void InitParseTable(void)
{
U8 i;
U16 AllocSize;
ParseTableIndex = -1;
MaxSimultaneousParseReached = 0;
ParseTable_p = (STTST_Parse_t **)memory_allocate(sttst_InitParams.CPUPartition_p, sizeof(STTST_Parse_t*)*TOKEN_TABLE_INIT_SIZE);
if (ParseTable_p == NULL)
{
STTBX_Print(("Parse table initialization : not enough memory !\n" ));
}
else
{
AllocSize = sizeof(STTST_Parse_t) + CLI_MAX_LINE_LENGTH;
for (i=0; i< PARSE_TABLE_INIT_SIZE; i++)
{
ParseTable_p[i] = (STTST_Parse_t *)memory_allocate(sttst_InitParams.CPUPartition_p, AllocSize);
if (ParseTable_p[i] == NULL)
{
STTBX_Print(("Parse table initialization : not enough memory !\n" ));
break;
}
}
}
}
/* ========================================================================
deletes all symbol entries in table created above a given nest level.
if the last entry is deleted, the table store is deallocated.
=========================================================================== */
static void purge_symbols(S16 level)
{
S32 cnt;
symtab_t *symbol_p;
BOOL ExitLoop = FALSE;
cnt = 1;
while(!ExitLoop && (cnt <= SymbolCount))
{
symbol_p = Symtab_p[SymbolCount - cnt];
ExitLoop = (symbol_p->depth <= level);
if (!(ExitLoop))
{
if (symbol_p->name_p != NULL)
{
memory_deallocate( sttst_InitParams.CPUPartition_p, symbol_p->name_p );
}
if (symbol_p->type == CLI_STR_SYMBOL)
{
memory_deallocate( sttst_InitParams.CPUPartition_p, symbol_p->value.str_val);
}
if (symbol_p->type == CLI_MAC_SYMBOL)
{
macro_t *l_p,*nl_p;
l_p = symbol_p->value.mac_val;
while (l_p != (macro_t*)NULL)
{
nl_p = l_p->line_p;
memory_deallocate( sttst_InitParams.CPUPartition_p, (char*)l_p);
l_p = nl_p;
}
}
memory_deallocate( sttst_InitParams.CPUPartition_p, (char*)symbol_p);
cnt++;
}
}
SymbolCount = SymbolCount - (cnt-1);
if (SymbolCount == 0)
{
memory_deallocate( sttst_InitParams.CPUPartition_p, (char*) Symtab_p);
}
}
/* ========================================================================
adds symbol structure to table and checks for name clashes, scope
clashes, invalid identifiers and lack of table space. A valid name
must contain alphanumerics only, plus '_', with
the first character alphabetic.
=========================================================================== */
static symtab_t *insert_symbol(const char * const token_p, S16 type)
{
symtab_t *symbol_p;
symtab_t *oldsym_p;
S16 cnt;
BOOL valid;
partition_t *mem_pt;
/* check that symbol table has a spare slot and issue a warning if close */
symbol_p = (symtab_t*) NULL;
if (SymbolCount >= (MaxSymbolCount - 1))
{
sttst_Print("Cannot insert \"%s\" in symbol table - table is full!\n",token_p);
}
else
{
valid = TRUE;
cnt = 0;
while (token_p[cnt] != '\0')
{
if (((token_p[cnt] < 'A') || (token_p[cnt] > 'Z')) &&
((token_p[cnt] < 'a') || (token_p[cnt] > 'z')) &&
((token_p[cnt] < '0') || (token_p[cnt] > '9') || (cnt == 0)) &&
((token_p[cnt] != '_') || (cnt == 0)))
{
valid = FALSE;
}
cnt++;
}
if (!valid)
{
sttst_Print("Cannot insert \"%s\" in symbol table - invalid symbol name\n",
token_p);
}
else
{
/* carry on with insertion process, checking for scoped name clashes */
/* look for a symbol of the same type and matching name. This can
be ok if it was declared in a macro level less than the current one*/
oldsym_p = look_for(token_p, CLI_ANY_SYMBOL);
if ((oldsym_p != (symtab_t*) NULL) && (oldsym_p->depth >= MacroDepth))
{
sttst_Print("Cannot insert \"%s\" in symbol table - name clash within current scope\n",
token_p);
}
else
{
mem_pt = memory_allocate( sttst_InitParams.CPUPartition_p, sizeof(symtab_t));
symbol_p = (symtab_t*) mem_pt;
mem_pt = memory_allocate(sttst_InitParams.CPUPartition_p, strlen(token_p)+1);
symbol_p->name_p = (char *)mem_pt;
if ((symbol_p == NULL) || (symbol_p->name_p == NULL))
{
sttst_Print("Cannot insert \"%s\" in symbol table - no memory available\n",
token_p);
symbol_p = NULL;
}
else
{
/* sttst_Print("Inserted symbol \"%s\" using space at %x\n", token_p, symbol_p); */
strcpy(symbol_p->name_p, token_p);
symbol_p->name_len = strlen(token_p);
symbol_p->type = type;
symbol_p->depth = MacroDepth;
/* insert new structure in table and warn if near full */
Symtab_p[SymbolCount] = symbol_p;
/* sttst_Print("Inserted symbol \"%s\" at slot %d\n", token_p, SymbolCount); */
SymbolCount++;
if (SymbolCount >= (MaxSymbolCount - 10))
{
sttst_Print("Warning: Symbol table nearly full - (%ld of %ld entries)\n",
SymbolCount, MaxSymbolCount);
}
}
}
}
}
return(symbol_p);
}
/* ========================================================================
allows deletion of a symbol from table, providing its been declared
at the current MacroDepth
=========================================================================== */
static BOOL delete_symbol(const char * const token_p)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
U16 Index;
symbol_p = look_for(token_p, CLI_ANY_SYMBOL); /* look for any type */
if (symbol_p == (symtab_t*)NULL)
{
IsBad = TRUE;
}
else
{
if ((symbol_p->fixed) || (symbol_p->depth != MacroDepth))
{
IsBad = TRUE;
}
else
{
/* free symbol name storage */
memory_deallocate(sttst_InitParams.CPUPartition_p, symbol_p->name_p);
symbol_p->name_p = NULL;
/* delete string storage */
if (symbol_p->type == CLI_STR_SYMBOL)
{
memory_deallocate(sttst_InitParams.CPUPartition_p, symbol_p->value.str_val);
}
/* delete any macro line buffers */
if (symbol_p->type == CLI_MAC_SYMBOL)
{
macro_t *l_p,*nl_p;
l_p = symbol_p->value.mac_val;
while (l_p != (macro_t*)NULL)
{
nl_p = l_p->line_p;
memory_deallocate(sttst_InitParams.CPUPartition_p, (char*)l_p);
l_p = nl_p;
}
}
/* mark symbol as unused, ready for purge when nest level is stripped */
symbol_p->type = CLI_NO_SYMBOL;
/* defragment symbol table */
/* find deleted symbol index in table */
Index = 0;
while (Symtab_p[Index]->type != CLI_NO_SYMBOL)
{
Index++;
}
/* move next symbols backward from 1 slot in table */
while(Index < (SymbolCount-1))
{
Symtab_p[Index] = Symtab_p[Index+1] ;
Index++;
}
Symtab_p[SymbolCount-1] = NULL;
SymbolCount--;
/* Actually de allocate the deleted symbol */
memory_deallocate(sttst_InitParams.CPUPartition_p, (char*)symbol_p);
}
}
return(IsBad);
}
/* ========================================================================
creates or updates an integer symbol table entry
=========================================================================== */
/*BOOL assign_integer(char * token_p, long value, BOOL constant)*/
BOOL STTST_AssignInteger(const char *const token_p, S32 value, BOOL constant)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
if (strlen(token_p) != 0)
{
symbol_p = look_for(token_p, CLI_INT_SYMBOL);
if ((symbol_p == (symtab_t*) NULL) && (token_p[0] != '\0'))
{
symbol_p = insert_symbol(token_p, CLI_INT_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
IsBad = TRUE;
}
else
{
symbol_p->fixed = constant;
if (symbol_p->fixed)
{
symbol_p->info_p = "integer constant";
}
else
{
symbol_p->info_p = "integer variable";
}
symbol_p->value.int_val = value;
}
}
else
{
if (symbol_p->fixed)
{
IsBad = TRUE;
}
else
{
symbol_p->value.int_val = value;
}
}
}
return(IsBad);
}
/* ========================================================================
creates an integer symbol table entry without looking for existing symbol
with a name match
=========================================================================== */
static BOOL create_integer(const char *const token_p, S32 value, BOOL constant)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
if (strlen(token_p) != 0)
{
symbol_p = insert_symbol(token_p, CLI_INT_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
IsBad = TRUE;
}
else
{
symbol_p->fixed = constant;
if (symbol_p->fixed)
{
symbol_p->info_p = "integer constant";
}
else
{
symbol_p->info_p = "integer variable";
}
symbol_p->value.int_val = value;
}
}
return(IsBad);
}
/* ========================================================================
creates or updates a floating point symbol table entry
=========================================================================== */
/*BOOL assign_float(char *token_p, double value, BOOL constant)*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -