Wed, 19 Dec 2012 22:01:42 +0200
So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
commands.cxx | file | annotate | diff | comparison | revisions | |
commands.def | file | annotate | diff | comparison | revisions | |
common.h | file | annotate | diff | comparison | revisions | |
databuffer.h | file | annotate | diff | comparison | revisions | |
main.cxx | file | annotate | diff | comparison | revisions | |
objwriter.cxx | file | annotate | diff | comparison | revisions | |
objwriter.h | file | annotate | diff | comparison | revisions | |
parser.cxx | file | annotate | diff | comparison | revisions |
--- a/commands.cxx Wed Dec 19 13:44:18 2012 +0200 +++ b/commands.cxx Wed Dec 19 22:01:42 2012 +0200 @@ -130,7 +130,6 @@ r->MustString(); break; case TYPE_UNKNOWN: - case TYPE_FLOAT: case TYPE_VOID: break; }
--- a/commands.def Wed Dec 19 13:44:18 2012 +0200 +++ b/commands.def Wed Dec 19 22:01:42 2012 +0200 @@ -3,7 +3,7 @@ * * Syntax: number:name:returntype:numargs:maxargs[:argumentlist] */ -0:changestate:void:1:1:str(statename) +0:changestate:void:1:1:int(newstate) 1:delay:void:1:1:int(tics) 2:rand:int:2:2:int(a):int(b) 3:StringsAreEqual:bool:2:2:str(string1):str(string2) @@ -16,18 +16,18 @@ 10:LookForSuperArmor:int:2:2:int(start):bool(visibilitycheck) 11:LookForPlayerEnemies:int:1:1:int(start) 12:GetClosestPlayerEnemy:int:0:0 -13:MoveLeft:void:1:1:float(speed) -14:MoveRight:void:1:1:float(speed) -15:MoveForward:void:1:1:float(speed) -16:MoveBackwards:void:1:1:float(speed) +13:MoveLeft:void:1:1:int(speed) +14:MoveRight:void:1:1:int(speed) +15:MoveForward:void:1:1:int(speed) +16:MoveBackwards:void:1:1:int(speed) 17:StopMovement:void:0:0 18:StopForwardMovement:void:0:0 19:StopSidewaysMovement:void:0:0 20:CheckTerrain:int:2:2:int(distance):int(angle) -21:PathToGoal:int:1:1:float(speed) -22:PathToLastKnownEnemyPosition:int:1:1:float(speed) -23:PathToLastHeardSound:int:1:1:float(speed) -24:Roam:int:1:1:float(speed) +21:PathToGoal:int:1:1:int(speed) +22:PathToLastKnownEnemyPosition:int:1:1:int(speed) +23:PathToLastHeardSound:int:1:1:int(speed) +24:Roam:int:1:1:int(speed) 25:GetPathingCostToItem:int:1:1:int(item) 26:GetDistanceToItem:int:1:1:int(item) 27:GetItemName:str:1:1:int(item)
--- a/common.h Wed Dec 19 13:44:18 2012 +0200 +++ b/common.h Wed Dec 19 22:01:42 2012 +0200 @@ -50,8 +50,15 @@ // Application name and version #define APPNAME "botc" #define VERSION_MAJOR 0 -#define VERSION_MINOR 0 -#define VERSION_REVISION 999 +#define VERSION_MINOR 999 + +// Use a macro for Write so we can get the function name +// This can be pretty crucial in debugging. +#ifdef __GNUC__ +#define Write(STUFF) DoWrite (__PRETTY_FUNCTION__, STUFF) +#else +#define Write(STUFF) DoWrite (__func__, STUFF) +#endif // On Windows, files are case-insensitive #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__) @@ -72,9 +79,7 @@ TYPE_VOID, TYPE_INT, TYPE_STRING, - TYPE_FLOAT, TYPE_BOOL, - }; #define CHECK_FILE(pointer,path,action) \
--- a/databuffer.h Wed Dec 19 13:44:18 2012 +0200 +++ b/databuffer.h Wed Dec 19 22:01:42 2012 +0200 @@ -54,7 +54,7 @@ class DataBuffer { public: // The actual buffer - unsigned char* buffer; + byte* buffer; // Allocated size of the buffer unsigned int allocsize; @@ -100,7 +100,8 @@ // ==================================================================== // Write stuff to the buffer - template<class T> void Write (T stuff) { + template<class T> void DoWrite (const char* func, T stuff) { + // printf ("DoWrite: called from %s\n", func); if (writesize + sizeof (T) >= allocsize) { // We don't have enough space in the buffer to write // the stuff - thus resize. First, store the old @@ -112,6 +113,7 @@ // for the stuff we're going to write, as well as a bit // of leeway so we don't have to resize immediately again. size_t newsize = allocsize + sizeof (T) + 128; + delete buffer; buffer = new unsigned char[newsize]; allocsize = newsize; @@ -142,7 +144,7 @@ int oldsize = writesize; for (unsigned int x = 0; x < other->writesize; x++) - Write<byte> (*(other->buffer+x)); + Write (*(other->buffer+x)); // Merge its marks and references unsigned int u = 0; @@ -177,7 +179,7 @@ DataBuffer* Clone () { DataBuffer* other = new DataBuffer; for (unsigned int x = 0; x < writesize; x++) - other->Write<unsigned char> (*(buffer+x)); + other->Write (*(buffer+x)); return other; } @@ -223,7 +225,7 @@ // Write a dummy placeholder for the reference if (placeholder) - Write<word> (1234); + Write (1234); return u; } @@ -286,15 +288,15 @@ // TODO: Casting float to word causes the decimal to be lost. // Find a way to store the number without such loss. float val = atof (floatstring); - Write<word> (DH_PUSHNUMBER); - Write<word> (static_cast<word> ((val > 0) ? val : -val)); + Write (DH_PUSHNUMBER); + Write (static_cast<word> ((val > 0) ? val : -val)); if (val < 0) - Write<word> (DH_UNARYMINUS); + Write (DH_UNARYMINUS); } void WriteString (str string) { - Write<word> (DH_PUSHSTRINGINDEX); - Write<word> (PushToStringTable (string)); + Write (DH_PUSHSTRINGINDEX); + Write (PushToStringTable (string)); } };
--- a/main.cxx Wed Dec 19 13:44:18 2012 +0200 +++ b/main.cxx Wed Dec 19 22:01:42 2012 +0200 @@ -69,7 +69,6 @@ "do", "else", "event", - "float", "for", "goto", "if", @@ -116,10 +115,6 @@ str headerline = "-="; header.appendformat ("%s version %d.%d", APPNAME, VERSION_MAJOR, VERSION_MINOR); - // Include revision if non-zero - if (VERSION_REVISION) - header.appendformat (".%d", VERSION_REVISION); - headerline *= (header.len() / 2) - 1; headerline += '-'; printf ("%s\n%s\n", header.chars(), headerline.chars()); @@ -252,12 +247,10 @@ return sizeof (g_Keywords) / sizeof (const char*); } - // ============================================================================ type_e GetTypeByName (str t) { t = t.tolower(); return (t == "int") ? TYPE_INT : - (t == "float") ? TYPE_FLOAT : (t == "str") ? TYPE_STRING : (t == "void") ? TYPE_VOID : (t == "bool") ? TYPE_BOOL : @@ -272,7 +265,6 @@ case TYPE_INT: return "int"; break; case TYPE_STRING: return "str"; break; case TYPE_VOID: return "void"; break; - case TYPE_FLOAT: return "float"; break; case TYPE_BOOL: return "bool"; break; case TYPE_UNKNOWN: return "???"; break; }
--- a/objwriter.cxx Wed Dec 19 13:44:18 2012 +0200 +++ b/objwriter.cxx Wed Dec 19 22:01:42 2012 +0200 @@ -63,9 +63,9 @@ } void ObjWriter::WriteString (char* s) { - Write<long> (strlen (s)); + Write (strlen (s)); for (unsigned int u = 0; u < strlen (s); u++) - Write<char> (s[u]); + Write ((s)[u]); } void ObjWriter::WriteString (const char* s) { @@ -83,17 +83,17 @@ void ObjWriter::WriteBuffers () { // If there was no mainloop defined, write a dummy one now. if (!g_GotMainLoop) { - MainLoopBuffer->Write<long> (DH_MAINLOOP); - MainLoopBuffer->Write<long> (DH_ENDMAINLOOP); + MainLoopBuffer->Write (DH_MAINLOOP); + MainLoopBuffer->Write (DH_ENDMAINLOOP); } // Write the onenter and mainloop buffers, IN THAT ORDER for (int i = 0; i < 2; i++) { - DataBuffer* buf = (!i) ? OnEnterBuffer : MainLoopBuffer; - WriteBuffer (buf); + DataBuffer** buf = (!i) ? &OnEnterBuffer : &MainLoopBuffer; + WriteBuffer (*buf); // Clear the buffer afterwards for potential next state - buf = new DataBuffer; + *buf = new DataBuffer; } // Next state definitely has no mainloop yet @@ -107,8 +107,8 @@ return; // Write header - Write<long> (DH_STRINGLIST); - Write<long> (stringcount); + Write (DH_STRINGLIST); + Write (stringcount); // Write all strings for (unsigned int a = 0; a < stringcount; a++)
--- a/objwriter.h Wed Dec 19 13:44:18 2012 +0200 +++ b/objwriter.h Wed Dec 19 22:01:42 2012 +0200 @@ -99,13 +99,17 @@ void MoveMark (unsigned int mark); void OffsetMark (unsigned int mark, int offset); void DeleteMark (unsigned int mark); - template <class T> void Write (T stuff) { - GetCurrentBuffer ()->Write<T> (stuff); + template <class T> void DoWrite (const char* func, T stuff) { + GetCurrentBuffer ()->DoWrite (func, stuff); } // Default to word - void Write (word stuff) { - Write<word> (stuff); + void DoWrite (const char* func, word stuff) { + DoWrite<word> (func, stuff); + } + + void DoWrite (const char* func, byte stuff) { + DoWrite<byte> (func, stuff); } private:
--- a/parser.cxx Wed Dec 19 13:44:18 2012 +0200 +++ b/parser.cxx Wed Dec 19 22:01:42 2012 +0200 @@ -105,7 +105,7 @@ // stateSpawn is special - it *must* be defined. If we // encountered it, then mark down that we have it. - if (!token.icompare ("statespawn")) + if (-token == "statespawn") g_stateSpawnDefined = true; // Must end in a colon @@ -143,7 +143,7 @@ g_CurMode = MODE_EVENT; w->Write (DH_EVENT); - w->Write<word> (e->number); + w->Write (e->number); g_NumEvents++; continue; } @@ -173,14 +173,13 @@ } // ============================================================ - if (token == "int" || token == "str" || token == "float" || token == "bool") { + if (token == "int" || token == "str" || token == "bool") { // For now, only globals are supported if (g_CurMode != MODE_TOPLEVEL || g_CurState.len()) ParserError ("variables must only be global for now"); type_e type = (token == "int") ? TYPE_INT : (token == "str") ? TYPE_STRING : - (token == "float") ? TYPE_FLOAT : TYPE_BOOL; MustNext (); @@ -218,7 +217,7 @@ } // Add a reference to the mark. - w->Write<word> (DH_GOTO); + w->Write (DH_GOTO); w->AddReference (m); MustNext (";"); continue; @@ -247,7 +246,7 @@ // Use DH_IFNOTGOTO - if the expression is not true, we goto the mark // we just defined - and this mark will be at the end of the scope block. - w->Write<word> (DH_IFNOTGOTO); + w->Write (DH_IFNOTGOTO); w->AddReference (marknum); // Store it @@ -306,7 +305,7 @@ w->WriteBuffer (expr); // Instruction to go to the end if it fails - w->Write<word> (DH_IFNOTGOTO); + w->Write (DH_IFNOTGOTO); w->AddReference (mark2); // Store the needed stuff @@ -357,7 +356,7 @@ // Add the condition w->WriteBuffer (cond); - w->Write<word> (DH_IFNOTGOTO); + w->Write (DH_IFNOTGOTO); w->AddReference (mark2); // Store the marks and incrementor @@ -432,8 +431,8 @@ // NULL the switch buffer for the case-go-to statement, // we want it all under the switch, not into the case-buffers. w->SwitchBuffer = NULL; - w->Write<word> (DH_CASEGOTO); - w->Write<word> (num); + w->Write (DH_CASEGOTO); + w->Write (num); AddSwitchCase (w, NULL); SCOPE(0).casenumbers[SCOPE(0).casecursor] = num; continue; @@ -457,8 +456,8 @@ // a default. DataBuffer* b = new DataBuffer; SCOPE(0).buffer1 = b; - b->Write<word> (DH_DROP); - b->Write<word> (DH_GOTO); + b->Write (DH_DROP); + b->Write (DH_GOTO); AddSwitchCase (w, b); continue; } @@ -469,7 +468,7 @@ if (!g_ScopeCursor) ParserError ("unexpected `break`"); - w->Write<word> (DH_GOTO); + w->Write (DH_GOTO); // switch and if use mark1 for the closing point, // for and while use mark2. @@ -505,7 +504,7 @@ case SCOPETYPE_FOR: case SCOPETYPE_WHILE: case SCOPETYPE_DO: - w->Write<word> (DH_GOTO); + w->Write (DH_GOTO); w->AddReference (scopestack[curs].mark1); found = true; break; @@ -581,10 +580,6 @@ MustString (); info.val = token; break; - case TYPE_FLOAT: - MustNext (); - info.val = ParseFloat (); - break; case TYPE_UNKNOWN: case TYPE_VOID: break; @@ -637,7 +632,7 @@ // If the condition runs true, go back to the start. w->WriteBuffer (expr); - w->Write<long> (DH_IFGOTO); + w->Write (DH_IFGOTO); w->AddReference (SCOPE(0).mark1); break; } @@ -655,8 +650,8 @@ if (SCOPE(0).buffer1) w->WriteBuffer (SCOPE(0).buffer1); else { - w->Write<word> (DH_DROP); - w->Write<word> (DH_GOTO); + w->Write (DH_DROP); + w->Write (DH_GOTO); w->AddReference (SCOPE(0).mark1); } @@ -786,14 +781,14 @@ // If the script skipped any optional arguments, fill in defaults. while (curarg < comm->maxargs) { - r->Write<word> (DH_PUSHNUMBER); - r->Write<word> (comm->defvals[curarg]); + r->Write (DH_PUSHNUMBER); + r->Write (comm->defvals[curarg]); curarg++; } - r->Write<word> (DH_COMMAND); - r->Write<word> (comm->number); - r->Write<word> (comm->maxargs); + r->Write (DH_COMMAND); + r->Write (comm->number); + r->Write (comm->maxargs); return r; } @@ -895,10 +890,10 @@ // Behold, big block of writing madness! :P int mark1 = retbuf->AddMark (""); // start of "else" case int mark2 = retbuf->AddMark (""); // end of expression - retbuf->Write<word> (DH_IFNOTGOTO); // if the first operand (condition) + retbuf->Write (DH_IFNOTGOTO); // if the first operand (condition) retbuf->AddMarkReference (mark1); // didn't eval true, jump into mark1 retbuf->Merge (rb); // otherwise, perform second operand (true case) - retbuf->Write<word> (DH_GOTO); // afterwards, jump to the end, which is + retbuf->Write (DH_GOTO); // afterwards, jump to the end, which is retbuf->AddMarkReference (mark2); // marked by mark2. retbuf->MoveMark (mark1); // move mark1 at the end of the true case retbuf->Merge (tb); // perform third operand (false case) @@ -906,7 +901,7 @@ } else { // Write to buffer retbuf->Merge (rb); - retbuf->Write<word> (DataHeaderByOperator (NULL, oper)); + retbuf->Write (DataHeaderByOperator (NULL, oper)); } } @@ -1028,8 +1023,8 @@ if (reqtype != TYPE_INT) ParserError ("strlen returns int but %s is expected\n", (char*)GetTypeName (reqtype)); - b->Write<word> (DH_PUSHNUMBER); - b->Write<word> (constant->val.len ()); + b->Write (DH_PUSHNUMBER); + b->Write (constant->val.len ()); MustNext (")"); } else if (token == "(") { @@ -1055,11 +1050,8 @@ switch (constant->type) { case TYPE_BOOL: case TYPE_INT: - b->Write<word> (DH_PUSHNUMBER); - b->Write<word> (atoi (constant->val)); - break; - case TYPE_FLOAT: - b->WriteFloat (constant->val); + b->Write (DH_PUSHNUMBER); + b->Write (atoi (constant->val)); break; case TYPE_STRING: b->WriteString (constant->val); @@ -1070,8 +1062,8 @@ } } else if ((g = FindGlobalVariable (token))) { // Global variable - b->Write<word> (DH_PUSHGLOBALVAR); - b->Write<word> (g->index); + b->Write (DH_PUSHGLOBALVAR); + b->Write (g->index); } else { // If nothing else, check for literal switch (reqtype) { @@ -1085,12 +1077,12 @@ // All values are written unsigned - thus we need to write the value's // absolute value, followed by an unary minus for negatives. - b->Write<word> (DH_PUSHNUMBER); + b->Write (DH_PUSHNUMBER); long v = atol (token); - b->Write<word> (static_cast<word> (abs (v))); + b->Write (static_cast<word> (abs (v))); if (v < 0) - b->Write<word> (DH_UNARYMINUS); + b->Write (DH_UNARYMINUS); break; } case TYPE_STRING: @@ -1100,26 +1092,12 @@ MustString (true); b->WriteString (token); break; - case TYPE_FLOAT: { - str floatstring = ParseFloat (); - - // TODO: Keep this check after decimal loss is fixed, but make - // it a real precision loss check. 55.5123 -> 55.512299, this - // should probably be warned of. - float check = static_cast<float> (static_cast<word> (atof (floatstring))); - if (atof (floatstring) != check) - ParserWarning ("floating point number %f loses precision (-> %f)", - atof (floatstring), check); - - b->WriteFloat (floatstring); - break; - } } } // Negate it now if desired if (negate) - b->Write<word> (DH_NEGATELOGICAL); + b->Write (DH_NEGATELOGICAL); return b; } @@ -1149,17 +1127,17 @@ // a <<= b -> a = a << b // a >>= b -> a = a >> b if (oper == OPER_ASSIGNLEFTSHIFT || oper == OPER_ASSIGNRIGHTSHIFT) { - retbuf->Write<word> (global ? DH_PUSHGLOBALVAR : DH_PUSHLOCALVAR); - retbuf->Write<word> (var->index); + retbuf->Write (global ? DH_PUSHGLOBALVAR : DH_PUSHLOCALVAR); + retbuf->Write (var->index); retbuf->Merge (expr); - retbuf->Write<word> ((oper == OPER_ASSIGNLEFTSHIFT) ? DH_LSHIFT : DH_RSHIFT); - retbuf->Write<word> (global ? DH_ASSIGNGLOBALVAR : DH_ASSIGNLOCALVAR); - retbuf->Write<word> (var->index); + retbuf->Write ((oper == OPER_ASSIGNLEFTSHIFT) ? DH_LSHIFT : DH_RSHIFT); + retbuf->Write (global ? DH_ASSIGNGLOBALVAR : DH_ASSIGNLOCALVAR); + retbuf->Write (var->index); } else { retbuf->Merge (expr); long dh = DataHeaderByOperator (var, oper); - retbuf->Write<word> (dh); - retbuf->Write<word> (var->index); + retbuf->Write (dh); + retbuf->Write (var->index); } return retbuf;