📄 vs1.0_tokens.l
字号:
%x INCLUDE DEFINE DEFSTR DEFSPACE SKIPLINE EATCOMMENT EATSTRING SAVELINE
%x MACRONAME MACROBODY MACROPARM EATMACRO EATDEFINE MODIFIER MACROPARMSTART
%x IFDEFNAME IFDEFBODY ENDMACRO MACROPARMEND
%{
#include <stdarg.h>
#include <stdlib.h>
#ifdef _WIN32
#include <io.h>
# ifdef __GNUC__
# include <sys/types.h>
# include <ctype.h>
# endif
#else
#include <sys/types.h>
#include <ctype.h>
#define _stat stat
#define _open open
#define _O_RDONLY O_RDONLY
#define _fstat fstat
#define _close close
#define stricmp strcasecmp
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "macro.h"
#include "nvparse_errors.h"
#include "vs1.0_inst_list.h"
#include "_vs1.0_parser.h"
#define yylineno line_number
#include "nvparse_externs.h"
#define yylineno line_number
int line_incr;
void LexError(char *format, ...);
void LexWarning(char *format, ...);
char *ReadTextFile(const char * filename);
unsigned int MakeRegisterMask(char *findName);
unsigned int FindSwizzleValue(char *swizzleText);
enum ERROR_VALUES {
ERROR_NONE = 0,
ERROR_MEMORY_ALLOC,
ERROR_FILE_OPEN,
ERROR_UNSUCCESSFUL_ASSEMBLE,
ERROR_TOO_MANY_PARMS,
ERROR_DEST_WRITE,
ERROR_LIST_OPEN,
ERROR_DEST_OPEN,
ERROR_NO_ARGUMENTS,
ERROR_MACRO_OVERRUN
};
//extern void GenSwitchFileNames(char *fileName);
//extern unsigned int gLinesAssembled;
unsigned int gLinesAssembled;
#define YY_INPUT(buf,result,max_size) \
{ \
int c = *myin++; \
result = (c == 0) ? YY_NULL : (buf[0] = c, 1); \
}
#define SAFEDELETEARRAY(x) if ((x) != NULL) \
delete [] (x)
#define SAFEFREE(x) if ((x) != NULL) \
free((x))
#define MAXREPLACESTRING 255
char gReplaceText[MAXREPLACESTRING+1];
//
// forward prototypes for macro functions
//
void MacroIncFunction(char *, unsigned int *, char **);
void MacroDecFunction(char *, unsigned int *, char **);
void MacroAddFunction(char *, unsigned int *, char **);
void MacroSubFunction(char *, unsigned int *, char **);
MACROFUNCTIONS gMacroFunctions[] = {
{ "inc(", MacroIncFunction },
{ "dec(", MacroDecFunction },
{ "add(", MacroAddFunction },
{ "sub(", MacroSubFunction }
};
#define NUM_MACRO_FUNCTIONS (sizeof(gMacroFunctions) / sizeof(MACROFUNCTIONS))
#define MAX_INCLUDE_DEPTH 1024
typedef struct INCLUDEINFO
{
char *fileName;
unsigned int lineNo;
YY_BUFFER_STATE buffer;
MACROENTRY *lastInvokeMacro; // save off in case nested macros.
MACROENTRY *lastParseMacro; // recursive macros
MACROTEXT *lastMacroLineParse; // save off for recursive lines of macros working on.
bool lastbInsideMacro; // save off for recursive macros
bool lastbInsideDefine; // save off for recursive macros/defines
bool lastbInsideInclude;
bool lastbProcessingIFDEF; // save off #define information
// FILE *fileHandle;
char *prevString;
char *nextString;
} INCLUDEINFO;
INCLUDEINFO gIncludeStack[MAX_INCLUDE_DEPTH];
int gIncludeStackIndex = 0;
IFDEFINFO gIfDefStack[MAX_IFDEF_DEPTH];
int gIfDefStackIndex = 0;
unsigned int &base_linenumber = gIncludeStack[0].lineNo;
bool gbInsideInclude = false;
bool gbProcessingBuiltIn = false;
bool gbProcessingDefine = false;
unsigned int gCountParen = 0;
bool gbProcessingIFDEF = false;
bool gbIFDEF = false;
bool gbCompareDefine = false;
unsigned int gIfDefStartLine;
MACROENTRY *gLastMacro;
MACROENTRY *gInvokeMacro;
MACROENTRY *gTempMacro; // until all the parameters are read
MACROENTRY *FindMacro(char *macroName);
MACROENTRY *FindNMacro(char *macroName, unsigned int sLen);
MACROFUNCTIONPTR gMacroCallFunction;
char *builtInMacros = "macro m3x2 reg1, reg2, reg3\n"
" dp3 %reg1.x, %reg2, %reg3\n"
" dp3 %reg1.y, %reg2, %inc(%reg3)\n"
"endm";
//
// local prototypes
//
void CleanUp();
void ReplaceMacroParms(char *srcLine, char *destLine,
MACROENTRY *srcParms, MACROENTRY *invParms);
MACROTEXT *SaveMacroText(char *srcText, MACROTEXT *lastMacroText);
void FreeMacroEntry(MACROENTRY *macEntry);
void EndMacroParms();
char *FindAlphaNum(char *srcStr, unsigned int *sLen);
void DebugUnhandledState();
unsigned int gCommentStartLine;
unsigned int gMacroStartLine;
char *gCurFileName = NULL;
#define MAXSAVELINE 4095
char gSaveLine[MAXSAVELINE+1];
char gMacroLine[MAXSAVELINE+1];
#if 1
#ifdef _DEBUG
#define ECHO DebugUnhandledState();
#else
#define ECHO
#endif
#endif
bool gbInsideMacro = false; // flag if we are doing a macro replace or not.
bool gbTempInsideMacro = false;
unsigned int gInvokeState = INITIAL;
MACROENTRY *gParseMacro; // which source macro entry we are using
MACROENTRY *gTempParseMacro; // temporary holder until parameters are received.
MACROTEXT *gMacroLineParse; // which line we are currently parsing inside the macro invocation
typedef enum OPCODETYPE
{
TYPE_NONE = 0,
TYPE_VERTEX_SHADER = 1,
TYPE_PIXEL_SHADER = 2
};
typedef struct OPCODEMAP
{
char *string; // string for opcode
int tokenName; // name of the corresponding token
int numArguments; // number of arguments for opcode
float version; // minimum version supported in.
int opcodeTypeFlags; // whether opcode can be used in vertex shader or pixel shader
bool opcodeModify; // if opcode modifiers can be used
bool textureOpcode; // only outputs to the texture unit
} OPCODEMAP;
#ifndef TRUE
#define TRUE true
#endif
#ifndef FALSE
#define FALSE false
#endif
OPCODEMAP theOpcodes[] = {
{ "add", ADD_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER | TYPE_PIXEL_SHADER, TRUE, FALSE },
{ "dp3", DP3_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER | TYPE_PIXEL_SHADER, TRUE, FALSE },
{ "dp4", DP4_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "dst", DST_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "exp", EXP_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "expp", EXPP_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "frc", FRC_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "lit", LIT_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "log", LOG_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "logp", LOGP_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "m3x2", M3X2_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "m3x3", M3X3_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "m3x4", M3X4_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "m4x3", M4X3_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "m4x4", M4X4_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "mad", MAD_INSTR, 4, 1.0f, TYPE_VERTEX_SHADER | TYPE_PIXEL_SHADER, TRUE, FALSE },
{ "max", MAX_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "min", MIN_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "mov", MOV_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER | TYPE_PIXEL_SHADER, TRUE, FALSE },
{ "mul", MUL_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER | TYPE_PIXEL_SHADER, TRUE, FALSE },
{ "nop", NOP_INSTR, 0, 1.0f, TYPE_VERTEX_SHADER | TYPE_PIXEL_SHADER, TRUE, FALSE },
{ "rcp", RCP_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "rsq", RSQ_INSTR, 2, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "sge", SGE_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "slt", SLT_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER, FALSE, FALSE },
{ "sub", SUB_INSTR, 3, 1.0f, TYPE_VERTEX_SHADER | TYPE_PIXEL_SHADER, TRUE, FALSE },
};
#define NUMOPCODES (sizeof(theOpcodes) / sizeof(OPCODEMAP))
OPCODEMAP *FindOpcode(char *findName);
%}
digits ([0-9]+)
digit ([0-9])
pt "."
sign [+-]?
exponent ([eE]{sign}{digits})
alpha [a-zA-Z_]
alphadigs [a-zA-Z0-9_]
notAlphaDigs ([^a-zA-Z0-9_])
identifier {alpha}{alphadigs}*
%%
<SAVELINE>.*\n {
gbProcessingDefine = false;
gSaveLine[0] = '\0';
strncat(gSaveLine, yytext, MAXSAVELINE);
// GenDebugLine();
if (gbProcessingIFDEF && (gbCompareDefine != gbIFDEF))
{
BEGIN(IFDEFBODY);
}
else
{
BEGIN(INITIAL);
}
yyless(0);
}
<SAVELINE>.* {
gbProcessingDefine = false;
gSaveLine[0] = '\0';
strncat(gSaveLine, yytext, MAXSAVELINE);
// GenDebugLine();
if (gbProcessingIFDEF && (gbCompareDefine != gbIFDEF))
{
BEGIN(IFDEFBODY);
}
else
{
BEGIN(INITIAL);
}
yyless(0);
}
a{digits}[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_ADDRESS_REG;
vs10_lval.reg.index = atoi(&yytext[1]);
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
v{digits}[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_VERTEX_ATTRIB_REG;
vs10_lval.reg.index = atoi(&yytext[1]);
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
r{digits}[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_TEMPORARY_REG;
vs10_lval.reg.index = atoi(&yytext[1]);
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
c{digits}[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_CONSTANT_MEM_REG;
vs10_lval.reg.index = atoi(&yytext[1]);
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
oT{digits}[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_TEXTURE_RESULT_REG;
vs10_lval.reg.index = atoi(&yytext[2]);
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
oD{digits}[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_COLOR_RESULT_REG;
vs10_lval.reg.index = atoi(&yytext[2]);
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
oFog[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_FOG_RESULT_REG;
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
oPos[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_POSITION_RESULT_REG;
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
oPts[ \t]*[\n]? {
// fprintf( stderr, "%s", yytext );
vs10_lval.reg.type = TYPE_POINTS_RESULT_REG;
if ( yytext[yyleng-1] == '\n' )
line_incr = 1;
return REGISTER;
}
[a-zA-Z][a-zA-Z0-9]+[ \t\n_] {
unsigned int offset;
offset = strcspn(yytext, " \t\n_");
yyless(offset);
OPCODEMAP *opcodeMap = FindOpcode(yytext);
if ( opcodeMap != NULL )
{
// fprintf( stderr, "%s\t", opcodeMap->string );
return( opcodeMap->tokenName );
}
else
{
gTempParseMacro = FindMacro(yytext);
if (gTempParseMacro != NULL)
{
if (gIncludeStackIndex >= MAX_INCLUDE_DEPTH )
{
LexError("macros nested too deeply");
exit( 1 );
}
if (gTempParseMacro->firstMacroLines != NULL)
{
gTempMacro = (MACROENTRY *)malloc(sizeof(MACROENTRY));
if (gTempMacro == NULL)
{
LexError("Out of memory allocating MACROENTRY structure.\n");
}
else
{
gTempMacro->next = NULL;
gTempMacro->prev = NULL;
gTempMacro->macroName = NULL;
gTempMacro->firstMacroParms = NULL;
gTempMacro->lastMacroParms = NULL;
gTempMacro->firstMacroLines = NULL;
gTempMacro->lastMacroLines = NULL;
gTempMacro->numParms = 0;
gTempMacro->nLines = 0;
gbTempInsideMacro = true; // flag we are currently doing a macro replace.
gInvokeState = YYSTATE;
if (gTempParseMacro->numParms > 0)
{
BEGIN(MACROPARMSTART);
}
else
{
EndMacroParms();
gbTempInsideMacro = false; // no longer waiting for macro invocation
}
}
}
}
else
{
// fprintf( stderr, "Opcode: \"%s\" not found\n", yytext );
REJECT;
}
}
//unsigned int offset;
//
//INSTRMAP *opcodeMap;
//
//offset = strcspn(yytext, " \t\n_");
//yyless(offset);
//opcodeMap = FindInstruction(yytext);
//if (opcodeMap == NULL)
//{
// REJECT;
//}
//
//yylval.opcodeInfo.opcodeMap = opcodeMap;
//
//return OPCODE;
}
";".* {
// fprintf( stderr, "%s", yytext );
char *cmt = new char[yyleng+1];
strncpy( cmt, yytext, yyleng );
cmt[0] = '#';
cmt[yyleng] = '\0';
vs10_lval.comment = cmt;
return COMMENT;
}
"//".* {
// fprintf( stderr, "%s", yytext );
char *cmt = new char[yyleng+1];
strncpy( cmt+1, yytext+1, yyleng-1 );
cmt[0] = '#';
cmt[1] = ' ';
cmt[yyleng] = '\0';
vs10_lval.comment = cmt;
return COMMENT;
}
"+"[ \t]*\n {
fprintf( stderr, "COISSUE found\n" );
yyless(yyleng-1);
//return COISSUE;
}
^[ \t]*"+"[ \t]* {
fprintf( stderr, "COISSUE found\n" );
//return COISSUE;
}
<INITIAL,MACROBODY>"/*" {
gCommentStartLine = yylineno;
yyless(0);
BEGIN(EATCOMMENT);
}
"#include"[ \t]+ {
BEGIN(INCLUDE);
}
<INCLUDE>[^ \t].*\n { /* got the include file name */
// FILE *newyyin;
char *newyyin;
char incFileName[1024];
unsigned long sLen;
bool validFileName;
if ( gIncludeStackIndex >= MAX_INCLUDE_DEPTH )
{
LexError("Includes nested too deeply, aborting\n");
exit( 1 );
}
// GenDebugLine();
// GenListString();
yylineno++;
gLinesAssembled++;
validFileName = true;
// zap "" and <>
if ((yytext[0] == '"') || (yytext[0] == '<'))
{
char *endQuote;
endQuote = strchr(&yytext[1], yytext[0]);
sLen = (endQuote - yytext)-1;
if (endQuote == NULL)
{
LexError("Unable to open include file %s\n", incFileName);
BEGIN(INITIAL);
validFileName = false;
}
else
{
incFileName[0] ='\0';
strncat(incFileName, &yytext[1], sLen);
}
}
else
{
strcpy(incFileName, yytext);
}
if (validFileName)
{
sLen = strlen(incFileName);
if ((incFileName[sLen-1] == '"') || (incFileName[sLen-1] == '>'))
{
incFileName[sLen-1] = '\0';
}
newyyin = ReadTextFile( incFileName );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -