# HG changeset patch # User Teemu Piippo # Date 1405993233 -10800 # Node ID 73d057b030d055ebfc76c0ec2457406828901431 # Parent 1c40bb4f8221b284ff7cd8b77086fff77d78bbcb - a bit more refactoring, adjusted the main commandline interface diff -r 1c40bb4f8221 -r 73d057b030d0 src/format.cpp --- a/src/format.cpp Tue Jul 22 02:52:25 2014 +0300 +++ b/src/format.cpp Tue Jul 22 04:40:33 2014 +0300 @@ -76,9 +76,11 @@ ofs++; } - if (!isdigit (fmt[pos + ofs])) + if (not isdigit (fmt[pos + ofs])) + { formatError (fmtstr, "bad format string, expected digit with optional " "modifier after '%%'", pos); + } int i = fmt[pos + ofs] - '0'; @@ -108,7 +110,7 @@ // void error (const String& msg) { - Lexer* lx = Lexer::getCurrentLexer(); + Lexer* lx = Lexer::GetCurrentLexer(); String fileinfo; if (lx != null and lx->hasValidToken()) diff -r 1c40bb4f8221 -r 73d057b030d0 src/lexer.cpp --- a/src/lexer.cpp Tue Jul 22 02:52:25 2014 +0300 +++ b/src/lexer.cpp Tue Jul 22 04:40:33 2014 +0300 @@ -29,29 +29,29 @@ #include #include "lexer.h" -static StringList gFileNameStack; -static Lexer* gMainLexer = null; +static StringList FileNameStack; +static Lexer* MainLexer = null; // ============================================================================= // Lexer::Lexer() { - ASSERT_EQ (gMainLexer, null); - gMainLexer = this; + ASSERT_EQ (MainLexer, null); + MainLexer = this; } // ============================================================================= // Lexer::~Lexer() { - gMainLexer = null; + MainLexer = null; } // ============================================================================= // void Lexer::processFile (String fileName) { - gFileNameStack << fileName; + FileNameStack << fileName; FILE* fp = fopen (fileName, "r"); if (fp == null) @@ -72,7 +72,7 @@ mustGetFromScanner (sc,Token::String); String fileName = sc.getTokenText(); - if (gFileNameStack.contains (fileName)) + if (FileNameStack.contains (fileName)) error ("attempted to #include %1 recursively", sc.getTokenText()); processFile (fileName); @@ -98,7 +98,7 @@ } m_tokenPosition = m_tokens.begin() - 1; - gFileNameStack.removeOne (fileName); + FileNameStack.removeOne (fileName); } // ============================================================================ @@ -190,10 +190,10 @@ tok.text = sc.getTokenText(); error ("at %1:%2: expected %3, got %4", - gFileNameStack.last(), + FileNameStack.last(), sc.getLine(), - describeTokenType (tt), - describeToken (&tok)); + DescribeTokenType (tt), + DescribeToken (&tok)); } } @@ -217,10 +217,10 @@ elif (toknames.isEmpty() == false) toknames += ", "; - toknames += describeTokenType (tokType); + toknames += DescribeTokenType (tokType); } - error ("expected %1, got %2", toknames, describeToken (token())); + error ("expected %1, got %2", toknames, DescribeToken (token())); } // ============================================================================= @@ -239,7 +239,7 @@ } } - error ("expected one of %1, got %2", syms, describeToken (token())); + error ("expected one of %1, got %2", syms, DescribeToken (token())); return -1; } @@ -248,13 +248,13 @@ void Lexer::tokenMustBe (Token tok) { if (tokenType() != tok) - error ("expected %1, got %2", describeTokenType (tok), - describeToken (token())); + error ("expected %1, got %2", DescribeTokenType (tok), + DescribeToken (token())); } // ============================================================================= // -String Lexer::describeTokenPrivate (Token tokType, Lexer::TokenInfo* tok) +String Lexer::DescribeTokenPrivate (Token tokType, Lexer::TokenInfo* tok) { if (tokType < LastNamedToken) return "\"" + LexerScanner::GetTokenString (tokType) + "\""; @@ -301,9 +301,9 @@ // ============================================================================= // -Lexer* Lexer::getCurrentLexer() +Lexer* Lexer::GetCurrentLexer() { - return gMainLexer; + return MainLexer; } // ============================================================================= @@ -339,6 +339,7 @@ void Lexer::mustGetSymbol (const String& a) { mustGetNext (Token::Any); + if (token()->text != a) error ("expected \"%1\", got \"%2\"", a, token()->text); } diff -r 1c40bb4f8221 -r 73d057b030d0 src/lexer.h --- a/src/lexer.h Tue Jul 22 02:52:25 2014 +0300 +++ b/src/lexer.h Tue Jul 22 04:40:33 2014 +0300 @@ -64,7 +64,7 @@ String describeCurrentPosition(); String describeTokenPosition(); - static Lexer* getCurrentLexer(); + static Lexer* GetCurrentLexer(); inline bool hasValidToken() const { @@ -103,14 +103,14 @@ } // If @tok is given, describes the token. If not, describes @tok_type. - static inline String describeTokenType (Token toktype) + static inline String DescribeTokenType (Token toktype) { - return describeTokenPrivate (toktype, null); + return DescribeTokenPrivate (toktype, null); } - static inline String describeToken (TokenInfo* tok) + static inline String DescribeToken (TokenInfo* tok) { - return describeTokenPrivate (tok->type, tok); + return DescribeTokenPrivate (tok->type, tok); } private: @@ -121,7 +121,7 @@ void mustGetFromScanner (LexerScanner& sc, Token tt =Token::Any); void checkFileHeader (LexerScanner& sc); - static String describeTokenPrivate (Token tok_type, TokenInfo* tok); + static String DescribeTokenPrivate (Token tok_type, TokenInfo* tok); }; #endif // BOTC_LEXER_H diff -r 1c40bb4f8221 -r 73d057b030d0 src/main.cpp --- a/src/main.cpp Tue Jul 22 02:52:25 2014 +0300 +++ b/src/main.cpp Tue Jul 22 04:40:33 2014 +0300 @@ -44,9 +44,6 @@ // I guess there should be a better way to do this. if (argc == 2 and String (argv[1]) == "-l") { - print ("Begin list of commands:\n"); - print ("------------------------------------------------------\n"); - BotscriptParser parser; parser.setReadOnly (true); parser.parseBotscript ("botc_defs.bts"); @@ -54,41 +51,32 @@ for (CommandInfo* comm : getCommands()) print ("%1\n", comm->signature()); - print ("------------------------------------------------------\n"); - print ("End of command list\n"); + exit (0); + } + + if (argc == 2 and String (argv[1]) == "-v") + { + // Print header + String header; + header = format (APPNAME " %1", versionString (true)); + +#ifdef DEBUG + header += " (debug build)"; +#endif + + print ("%1\n", header); exit (0); } if (argc < 2) { + fprintf (stderr, APPNAME " %s\n", versionString (false).c_str()); fprintf (stderr, "usage: %s [outfile] # compiles botscript\n", argv[0]); fprintf (stderr, " %s -l # lists commands\n", argv[0]); + fprintf (stderr, " %s -v # displays version info\n", argv[0]); exit (1); } - // Print header - String header; - String headerline; - header = format (APPNAME " version %1", versionString (true)); - -#ifdef DEBUG - if (header.firstIndexOf ("(") != -1) - header += ", "; - else - header += " ("; - - header += "debug build"; -#endif - - if (header.firstIndexOf ("(") != -1) - header += ")"; - - for (int i = 0; i < header.length() / 2; ++i) - headerline += "-="; - - headerline += '-'; - print ("%2\n\n%1\n\n%2\n\n", header, headerline); - String outfile; if (argc < 3) @@ -146,11 +134,11 @@ DataType getTypeByName (String token) { token = token.toLowercase(); - return (token == "int") ? TYPE_Int : - (token == "str") ? TYPE_String : - (token == "void") ? TYPE_Void : - (token == "bool") ? TYPE_Bool : - TYPE_Unknown; + return (token == "int") ? TYPE_Int + : (token == "str") ? TYPE_String + : (token == "void") ? TYPE_Void + : (token == "bool") ? TYPE_Bool + : TYPE_Unknown; } @@ -176,32 +164,27 @@ // String makeVersionString (int major, int minor, int patch) { - String ver = format ("%1.%2", major, minor); + String ver = String::fromNumber (major); + ver += "." + String::fromNumber (minor); if (patch != 0) - { - ver += "."; - ver += patch; - } + ver += "." + patch; return ver; } // ============================================================================= // -String versionString (bool) +String versionString (bool longform) { -#if defined(GIT_DESCRIPTION) and defined (DEBUG) - String tag (GIT_DESCRIPTION); - String version = tag; + String result = makeVersionString (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); - if (longform and tag.endsWith ("-pre")) - version += "-" + String (GIT_HASH).mid (0, 8); +#ifdef SVN_REVISION_STRING + if (longform) + result += "-" SVN_REVISION_STRING; +#else + (void) longform; // shuts up GCC +#endif - return version; -#elif VERSION_PATCH != 0 - return format ("%1.%2.%3", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); -#else - return format ("%1.%2", VERSION_MAJOR, VERSION_MINOR); -#endif + return result; } diff -r 1c40bb4f8221 -r 73d057b030d0 src/main.h --- a/src/main.h Tue Jul 22 02:52:25 2014 +0300 +++ b/src/main.h Tue Jul 22 04:40:33 2014 +0300 @@ -41,6 +41,10 @@ #include "botStuff.h" #include "tokens.h" +#define VERSION_MAJOR 1 +#define VERSION_MINOR 0 +#define VERSION_PATCH 0 + String makeObjectFileName (String s); DataType getTypeByName (String token); String dataTypeName (DataType type); diff -r 1c40bb4f8221 -r 73d057b030d0 src/parser.cpp --- a/src/parser.cpp Tue Jul 22 02:52:25 2014 +0300 +++ b/src/parser.cpp Tue Jul 22 04:40:33 2014 +0300 @@ -1082,7 +1082,7 @@ default: break; } - error ("WTF bad operator token %1", m_lexer->describeToken (m_lexer->token())); + error ("WTF bad operator token %1", m_lexer->DescribeToken (m_lexer->token())); return (AssignmentOperator) 0; }