simple B compiler
This commit is contained in:
@@ -3,6 +3,127 @@
|
||||
#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<BExpression_t> 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);
|
||||
@@ -15,5 +136,13 @@ int main( int argc, char **argv )
|
||||
filesystem->Close(pFile);
|
||||
|
||||
|
||||
CUtlVector<Token_t> 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user