Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string>
- #include <map>
- #include <set>
- #include <vector>
- #include "math.h"
- #include "../flex/flex.h"
- FILE* outFile;
- std::string lineStr;
- void WriteLine(bool addNewline, bool finalize=false)
- {
- if(addNewline) lineStr += "\\n";
- if(lineStr.length() > 64 || finalize)
- {
- fprintf(outFile, "\"%s\"\n", lineStr.c_str());
- lineStr.clear();
- }
- }
- bool IsChainable(LexToken token)
- {
- switch(token)
- {
- case LEX_NEWLINE:
- case LEX_WHITESPACE:
- case LEX_OPERATORS:
- case LEX_MEMBER:
- return true;
- };
- return false;
- }
- std::string GetNextIDString()
- {
- static int idBuffer[512] = {0};
- static char strBuffer[512] = {0};
- const char* chrsS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
- const char* chrsI = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
- static int len = 1;
- // Build string
- memset(strBuffer, 0, 512);
- for(int i=0; i<len; ++i)
- {
- const char* chrs = (i == 0 ? chrsS : chrsI);
- strBuffer[i] = chrs[idBuffer[i]];
- }
- // Increment
- int dec = 0;
- bool carry = true;
- while(carry)
- {
- const int max = (dec == 0 ? strlen(chrsS) : strlen(chrsI)) - 1;
- ++idBuffer[dec];
- carry = (idBuffer[dec] == max);
- if(carry)
- {
- idBuffer[dec] = 0;
- carry = true;
- ++len, ++dec;
- }
- }
- std::string result(strBuffer);
- return result;
- }
- struct IDstring
- {
- IDstring() : str(GetNextIDString()){}
- std::string str;
- };
- std::map<std::string, IDstring> IDmap;
- std::set<std::string> externIDs;
- void ParseToken(const std::string& string,
- LexToken llastToken, LexToken lastToken, LexToken token, LexToken nextToken)
- {
- static bool externType = false;
- static bool preprocLine = false;
- switch(token)
- {
- case LEX_WHITESPACE:
- if(!IsChainable(lastToken) && !IsChainable(nextToken)) lineStr += ' ';
- break;
- case LEX_IDENTIFIER:
- {
- if(externType) externIDs.insert(string);
- std::string name = string;
- if(externIDs.find(string) == externIDs.end() && lastToken!=LEX_MEMBER)
- {
- name = IDmap[string].str;
- }
- lineStr += name;
- externType = false;
- break;
- }
- case LEX_VALUE:
- lineStr += string; break;
- case LEX_PREPROCESSOR:
- preprocLine = true;
- lineStr += string; break;
- case LEX_RESERVED:
- lineStr += string; break;
- case LEX_MODIFIER_EXTERN:
- lineStr += string;
- break;
- case LEX_MODIFIER_INTERN:
- lineStr += string; break;
- case LEX_TYPE:
- externType = (llastToken == LEX_MODIFIER_EXTERN);
- lineStr += string; break;
- case LEX_OPERATORS:
- lineStr += string; break;
- case LEX_MEMBER:
- lineStr += string; break;
- case LEX_UNKNOWN:
- printf("Unknown: \"%s\"\n", string.c_str());
- lineStr += string;
- break;
- case LEX_NEWLINE:
- WriteLine(preprocLine);
- preprocLine = false;
- break;
- };
- }
- void _FlexHandleToken(const char* string, LexToken token)
- {
- static std::string thisString="";
- static LexToken llastToken=LEX_NULL, lastToken=LEX_NULL, thisToken=LEX_NULL;
- if(thisToken) ParseToken(thisString, llastToken, lastToken, thisToken, token);
- thisString = string;
- llastToken = lastToken;
- lastToken = thisToken;
- thisToken = token;
- }
- int main()
- {
- const int argc = __argc;
- char** argv = __argv;
- system("cls");
- char* fileName = "test.frag";
- if(argc > 1) fileName = argv[1];
- // Try to open file
- printf("Compressing \"%s\"...\n", fileName);
- FILE* fp = 0;
- fopen_s(&fp, fileName, "r");
- if(!fp)
- {
- printf("Error: unable to open file!");
- }
- else
- {
- fclose(fp);
- const std::string outFileName = std::string(fileName) + ".out";
- fopen_s(&outFile, outFileName.c_str(), "w");
- _FlexHandleFile(fileName);
- _FlexHandleToken("", LEX_NULL);
- WriteLine(false, true);
- fclose(outFile);
- printf("Done.\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement