?? tokens.c
字號(hào):
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.
NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms. If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder.
THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.
IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
// tokens.c
//
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "slglobals.h"
///////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////// Preprocessor and Token Recorder and Playback: ////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
/*
* idstr()
* Copy a string to a malloc'ed block and convert it into something suitable
* for an ID
*
*/
char *idstr(const char *fstr)
{
size_t len;
char *str, *t;
const char *f;
len = strlen(fstr);
str = (char *) malloc(len + 1);
for (f=fstr, t=str; *f; f++) {
if (isalnum(*f)) *t++ = *f;
else if (*f == '.' || *f == '/') *t++ = '_';
}
*t = 0;
return str;
} // idstr
/*
* lCompressToken() - Rename a token so that most will fit in one byte.
*
*/
static int lCompressToken(int token)
{
return token;
} // lCompressToken
/*
* lExpandToken() - Expand a token into what the scanner wants.
*
*/
static int lExpandToken(int token)
{
return token;
} // lExpandToken
/*
* lNewBlock()
*
*/
static TokenBlock *lNewBlock(TokenStream *fTok)
{
TokenBlock *lBlock;
lBlock = (TokenBlock *) malloc(sizeof(TokenBlock) + 256);
lBlock->count = 0;
lBlock->current = 0;
lBlock->data = (unsigned char *) lBlock + sizeof(TokenBlock);
lBlock->max = 256;
lBlock->next = NULL;
if (fTok->head) {
fTok->current->next = lBlock;
} else {
fTok->head = lBlock;
}
fTok->current = lBlock;
return lBlock;
} // lNewBlock
/*
* lAddByte()
*
*/
static void lAddByte(TokenStream *fTok, unsigned char fVal)
{
TokenBlock *lBlock;
lBlock = fTok->current;
if (lBlock->count >= lBlock->max)
lBlock = lNewBlock(fTok);
lBlock->data[lBlock->count++] = fVal;
} // lAddByte
/*
* lAdd4Bytes()
*
*/
static void lAdd4Bytes(TokenStream *fTok, unsigned char *fVal)
{
TokenBlock *lBlock;
unsigned char *pc;
lBlock = fTok->current;
if (lBlock->count + 4 > lBlock->max)
lBlock = lNewBlock(fTok);
pc = &lBlock->data[lBlock->count];
lBlock->count += 4;
pc[0] = fVal[0];
pc[1] = fVal[1];
pc[2] = fVal[2];
pc[3] = fVal[3];
} // lAdd4Bytes
/*
* lReadByte() - Get the next byte from a stream.
*
*/
static int lReadByte(TokenStream *pTok)
{
TokenBlock *lBlock;
int lval = -1;
lBlock = pTok->current;
if (lBlock) {
if (lBlock->current >= lBlock->count) {
lBlock = lBlock->next;
if (lBlock)
lBlock->current = 0;
pTok->current = lBlock;
}
if (lBlock)
lval = lBlock->data[lBlock->current++];
}
return lval;
} // lReadByte
/*
* lRead4Bytes()
*
*/
static void lRead4Bytes(TokenStream *fTok, unsigned char *fVal)
{
fVal[0] = (unsigned char ) lReadByte(fTok);
fVal[1] = (unsigned char ) lReadByte(fTok);
fVal[2] = (unsigned char ) lReadByte(fTok);
fVal[3] = (unsigned char ) lReadByte(fTok);
} // lRead4Bytes
/////////////////////////////////////// Global Functions://////////////////////////////////////
/*
* InitTokenStreams()
*
*/
int InitTokenStreams(CgStruct *Cg)
{
return 1;
} // InitTokenStreams
/*
* NewTokenStream()
*
*/
TokenStream *NewTokenStream(const char *name)
{
TokenStream *pTok;
pTok = (TokenStream *) malloc(sizeof(TokenStream));
pTok->next = NULL;
pTok->name = idstr(name);
pTok->head = NULL;
pTok->current = NULL;
lNewBlock(pTok);
return pTok;
} // NewTokenStream
/*
* DeleteTokenStream()
*
*/
void DeleteTokenStream(TokenStream *pTok)
{
TokenBlock *pBlock, *nBlock;
if (pTok) {
pBlock = pTok->head;
while (pBlock) {
nBlock = pBlock->next;
free(pBlock);
pBlock = nBlock;
}
if (pTok->name)
free(pTok->name);
free(pTok);
}
} // DeleteTokenStream
/*
* RecordToken() - Add a token to the end of a list for later playback or printout.
*
*/
void RecordToken(TokenStream *pTok, int token)
{
const char *s;
// token = lCompressToken(token);
if (token > 256)
lAddByte(pTok, (unsigned char)((token & 0x7f) + 0x80));
else
lAddByte(pTok, (unsigned char)(token & 0x7f));
switch (token) {
case IDENT_SY:
case TYPEIDENT_SY:
case STRCONST_SY:
s = GetAtomString(atable, yylval.sc_ident);
while (*s)
lAddByte(pTok, (unsigned char) *s++);
lAddByte(pTok, 0);
break;
case CFLOATCONST_SY:
case FLOATCONST_SY:
case FLOATHCONST_SY:
case FLOATXCONST_SY:
lAdd4Bytes(pTok, (unsigned char *) &yylval.sc_fval);
break;
case INTCONST_SY:
lAdd4Bytes(pTok, (unsigned char *) &yylval.sc_int);
break;
case '(':
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -