#include "brb.h" #include "tier0/commandline.h" #include "tier2/ifilesystem.h" class CTokenParser { public: const char *PeekToken(); bool IsToken( const char *szString ); const char *PeekStringLiteral(); bool Continue(); Token_t *m_pTokens; Token_t *m_pTokensEnd; Token_t *m_pCurrentToken; }; const char *CTokenParser::PeekToken() { if ( m_pCurrentToken->m_bIsQuoted ) return NULL; return m_pCurrentToken->m_szValue; } bool CTokenParser::IsToken( const char *szString ) { if ( !V_strcmp(szString, m_pCurrentToken->m_szValue)) return true; return false; }; const char *CTokenParser::PeekStringLiteral() { if ( !m_pCurrentToken->m_bIsQuoted ) return NULL; return m_pCurrentToken->m_szValue; } bool CTokenParser::Continue() { m_pCurrentToken++; if ( m_pCurrentToken == m_pTokensEnd ) return false; return true; } void CompileErrorExpectedToken( Token_t *pToken, const char *szToken ) { if (pToken->m_bIsQuoted) V_printf("%d:%d: expected %s but got string literal\n", pToken->m_iLine, pToken->m_iCharacter, szToken); else V_printf("%d:%d: expected %s but got %s\n", pToken->m_iLine, pToken->m_iCharacter, szToken, pToken->m_szValue.GetString()); exit(1); } struct BExpression_t { enum BExpressionType { BEXPRESSION_TYPE_NEW, BEXPRESSION_TYPE_ADD, BEXPRESSION_TYPE_SUBTRACT, } m_eType; CUtlVector m_children; }; BExpression_t ParseVar( CTokenParser *pParser ) { const char *szToken = pParser->PeekToken(); return szToken; }; void ParseRValue( CTokenParser *pParser ) { if (pParser->PeekToken() == "(") { pParser->PeekToken(); ParseRValue(pParser) } BVar_t var = ParseVar( pParser ); } void ParseStatement( CTokenParser *pParser ) { if (pParser->IsToken("return")) { pParser->Continue(); ParseRValue( pParser ); if (!pParser->IsToken(";")) CompileErrorExpectedToken(pParser->m_pCurrentToken, ";"); V_printf(" ret\n"); pParser->Continue(); } } void ParseGlobal( CTokenParser *pParser ) { const char *szObjectName = pParser->PeekToken(); pParser->Continue(); V_printf("%s:\n", szObjectName); if (pParser->IsToken("(")) { pParser->Continue(); if (!pParser->IsToken(")")) CompileErrorExpectedToken(pParser->m_pCurrentToken, ")"); pParser->Continue(); if (!pParser->IsToken("{")) CompileErrorExpectedToken(pParser->m_pCurrentToken, "{"); pParser->Continue(); while (!pParser->IsToken("}")) { ParseStatement( pParser ); } pParser->Continue(); } else { CompileErrorExpectedToken(pParser->m_pCurrentToken, "("); } }; int main( int argc, char **argv ) { CommandLine()->CreateCommandLine(argc, argv); CreateInterfaceFn filesystemFactory = Sys_GetFactory("filesystem_std"); filesystem = (IFileSystem*)filesystemFactory(FILESYSTEM_INTERFACE_VERSION, NULL); IFileHandle *pFile = filesystem->Open(argv[1], FILEMODE_READ); const char *szFileContents = filesystem->ReadString(pFile); filesystem->Close(pFile); CUtlVector tokens = Tokenize(szFileContents); CTokenParser parser; parser.m_pTokens = tokens.GetData(); parser.m_pTokensEnd = tokens.GetData()+tokens.GetSize(); parser.m_pCurrentToken = tokens.GetData()-1; parser.Continue(); ParseGlobal(&parser); V_free((void*)szFileContents); }